summaryrefslogtreecommitdiffstats
path: root/netwerk/dns/mdns/libmdns/nsDNSServiceDiscovery.cpp
blob: cec8033d18d94aeefacb5aa8c6715e274fa43f37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsDNSServiceDiscovery.h"
#include "MDNSResponderOperator.h"
#include "nsICancelable.h"
#include "nsXULAppAPI.h"
#include "private/pprio.h"

namespace mozilla {
namespace net {

namespace {

inline void
StartService()
{
  /*** STUB ***/
}

inline void
StopService()
{
  /*** STUB ***/
}

class ServiceCounter
{
public:
  static bool IsServiceRunning()
  {
    return !!sUseCount;
  }

private:
  static uint32_t sUseCount;

protected:
  ServiceCounter()
  {
    MOZ_ASSERT(NS_IsMainThread());
    if (!sUseCount++) {
      StartService();
    }
  }

  virtual ~ServiceCounter()
  {
    MOZ_ASSERT(NS_IsMainThread());
    if (!--sUseCount) {
      StopService();
    }
  }
};

uint32_t ServiceCounter::sUseCount = 0;

class DiscoveryRequest final : public nsICancelable
                             , private ServiceCounter
{
public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSICANCELABLE

  explicit DiscoveryRequest(nsDNSServiceDiscovery* aService,
                            nsIDNSServiceDiscoveryListener* aListener);

private:
  virtual ~DiscoveryRequest() { Cancel(NS_OK); }

  RefPtr<nsDNSServiceDiscovery> mService;
  nsIDNSServiceDiscoveryListener* mListener;
};

NS_IMPL_ISUPPORTS(DiscoveryRequest, nsICancelable)

DiscoveryRequest::DiscoveryRequest(nsDNSServiceDiscovery* aService,
                                   nsIDNSServiceDiscoveryListener* aListener)
  : mService(aService)
  , mListener(aListener)
{
}

NS_IMETHODIMP
DiscoveryRequest::Cancel(nsresult aReason)
{
  if (mService) {
    mService->StopDiscovery(mListener);
  }

  mService = nullptr;
  return NS_OK;
}

class RegisterRequest final : public nsICancelable
                            , private ServiceCounter
{
public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSICANCELABLE

  explicit RegisterRequest(nsDNSServiceDiscovery* aService,
                           nsIDNSRegistrationListener* aListener);

private:
  virtual ~RegisterRequest() { Cancel(NS_OK); }

  RefPtr<nsDNSServiceDiscovery> mService;
  nsIDNSRegistrationListener* mListener;
};

NS_IMPL_ISUPPORTS(RegisterRequest, nsICancelable)

RegisterRequest::RegisterRequest(nsDNSServiceDiscovery* aService,
                                 nsIDNSRegistrationListener* aListener)
  : mService(aService)
  , mListener(aListener)
{
}

NS_IMETHODIMP
RegisterRequest::Cancel(nsresult aReason)
{
  if (mService) {
    mService->UnregisterService(mListener);
  }

  mService = nullptr;
  return NS_OK;
}

} // namespace anonymous

NS_IMPL_ISUPPORTS(nsDNSServiceDiscovery, nsIDNSServiceDiscovery)

nsDNSServiceDiscovery::~nsDNSServiceDiscovery()
{
}

nsresult
nsDNSServiceDiscovery::Init()
{
  if (!XRE_IsParentProcess()) {
    MOZ_ASSERT(false, "nsDNSServiceDiscovery can only be used in parent process");
    return NS_ERROR_FAILURE;
  }
  return NS_OK;
}

NS_IMETHODIMP
nsDNSServiceDiscovery::StartDiscovery(const nsACString& aServiceType,
                                      nsIDNSServiceDiscoveryListener* aListener,
                                      nsICancelable** aRetVal)
{
  MOZ_ASSERT(aRetVal);

  nsresult rv;
  if (NS_WARN_IF(NS_FAILED(rv = StopDiscovery(aListener)))) {
    return rv;
  }

  nsCOMPtr<nsICancelable> req = new DiscoveryRequest(this, aListener);
  RefPtr<BrowseOperator> browserOp = new BrowseOperator(aServiceType,
                                                          aListener);
  if (NS_WARN_IF(NS_FAILED(rv = browserOp->Start()))) {
    return rv;
  }

  mDiscoveryMap.Put(aListener, browserOp);

  req.forget(aRetVal);

  return NS_OK;
}

NS_IMETHODIMP
nsDNSServiceDiscovery::StopDiscovery(nsIDNSServiceDiscoveryListener* aListener)
{
  nsresult rv;

  RefPtr<BrowseOperator> browserOp;
  if (!mDiscoveryMap.Get(aListener, getter_AddRefs(browserOp))) {
    return NS_OK;
  }

  browserOp->Cancel(); // cancel non-started operation
  if (NS_WARN_IF(NS_FAILED(rv = browserOp->Stop()))) {
    return rv;
  }

  mDiscoveryMap.Remove(aListener);
  return NS_OK;
}

NS_IMETHODIMP
nsDNSServiceDiscovery::RegisterService(nsIDNSServiceInfo* aServiceInfo,
                                       nsIDNSRegistrationListener* aListener,
                                       nsICancelable** aRetVal)
{
  MOZ_ASSERT(aRetVal);

  nsresult rv;
  if (NS_WARN_IF(NS_FAILED(rv = UnregisterService(aListener)))) {
    return rv;
  }

  nsCOMPtr<nsICancelable> req = new RegisterRequest(this, aListener);
  RefPtr<RegisterOperator> registerOp = new RegisterOperator(aServiceInfo,
                                                               aListener);
  if (NS_WARN_IF(NS_FAILED(rv = registerOp->Start()))) {
    return rv;
  }

  mRegisterMap.Put(aListener, registerOp);

  req.forget(aRetVal);

  return NS_OK;
}

NS_IMETHODIMP
nsDNSServiceDiscovery::UnregisterService(nsIDNSRegistrationListener* aListener)
{
  nsresult rv;

  RefPtr<RegisterOperator> registerOp;
  if (!mRegisterMap.Get(aListener, getter_AddRefs(registerOp))) {
    return NS_OK;
  }

  registerOp->Cancel(); // cancel non-started operation
  if (NS_WARN_IF(NS_FAILED(rv = registerOp->Stop()))) {
    return rv;
  }

  mRegisterMap.Remove(aListener);
  return NS_OK;
}

NS_IMETHODIMP
nsDNSServiceDiscovery::ResolveService(nsIDNSServiceInfo* aServiceInfo,
                                      nsIDNSServiceResolveListener* aListener)
{
  if (!ServiceCounter::IsServiceRunning()) {
    return NS_ERROR_FAILURE;
  }

  nsresult rv;

  RefPtr<ResolveOperator> resolveOp = new ResolveOperator(aServiceInfo,
                                                            aListener);
  if (NS_WARN_IF(NS_FAILED(rv = resolveOp->Start()))) {
    return rv;
  }

  return NS_OK;
}

} // namespace net
} // namespace mozilla