summaryrefslogtreecommitdiffstats
path: root/dom/system/windows/location/WindowsLocationProvider.cpp
blob: 92a6f2c9ccbef62be903a710a53535e86aeed760 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "WindowsLocationProvider.h"
#include "WindowsLocationParent.h"
#include "mozilla/dom/WindowsUtilsParent.h"
#include "GeolocationPosition.h"
#include "nsComponentManagerUtils.h"
#include "mozilla/ipc/UtilityProcessManager.h"
#include "mozilla/ipc/UtilityProcessSandboxing.h"
#include "prtime.h"
#include "MLSFallback.h"
#include "mozilla/Attributes.h"
#include "mozilla/Logging.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/Telemetry.h"
#include "mozilla/dom/GeolocationPositionErrorBinding.h"

namespace mozilla::dom {

LazyLogModule gWindowsLocationProviderLog("WindowsLocationProvider");
#define LOG(...) \
  MOZ_LOG(gWindowsLocationProviderLog, LogLevel::Debug, (__VA_ARGS__))

class MLSUpdate : public nsIGeolocationUpdate {
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSIGEOLOCATIONUPDATE
  explicit MLSUpdate(nsIGeolocationUpdate* aCallback) : mCallback(aCallback) {}

 private:
  nsCOMPtr<nsIGeolocationUpdate> mCallback;
  virtual ~MLSUpdate() {}
};

NS_IMPL_ISUPPORTS(MLSUpdate, nsIGeolocationUpdate);

NS_IMETHODIMP
MLSUpdate::Update(nsIDOMGeoPosition* aPosition) {
  if (!mCallback) {
    return NS_ERROR_FAILURE;
  }

  nsCOMPtr<nsIDOMGeoPositionCoords> coords;
  aPosition->GetCoords(getter_AddRefs(coords));
  if (!coords) {
    return NS_ERROR_FAILURE;
  }
  Telemetry::Accumulate(Telemetry::GEOLOCATION_WIN8_SOURCE_IS_MLS, true);
  return mCallback->Update(aPosition);
}
NS_IMETHODIMP
MLSUpdate::NotifyError(uint16_t aError) {
  if (!mCallback) {
    return NS_ERROR_FAILURE;
  }
  nsCOMPtr<nsIGeolocationUpdate> callback(mCallback);
  return callback->NotifyError(aError);
}

NS_IMPL_ISUPPORTS(WindowsLocationProvider, nsIGeolocationProvider)

WindowsLocationProvider::WindowsLocationProvider() {
  LOG("WindowsLocationProvider::WindowsLocationProvider(%p)", this);
  MOZ_ASSERT(XRE_IsParentProcess());
  MaybeCreateLocationActor();
}

WindowsLocationProvider::~WindowsLocationProvider() {
  LOG("WindowsLocationProvider::~WindowsLocationProvider(%p,%p,%p)", this,
      mActor.get(), mActorPromise.get());
  Send__delete__();
  ReleaseUtilityProcess();
  CancelMLSProvider();
}

void WindowsLocationProvider::MaybeCreateLocationActor() {
  LOG("WindowsLocationProvider::MaybeCreateLocationActor(%p)", this);
  if (mActor || mActorPromise) {
    return;
  }

  auto utilityProc = mozilla::ipc::UtilityProcessManager::GetSingleton();
  MOZ_ASSERT(utilityProc);

  // Create a PWindowsLocation actor in the Windows utility process.
  // This will attempt to launch the process if it doesn't already exist.
  RefPtr<WindowsLocationProvider> self = this;
  auto wuPromise = utilityProc->GetWindowsUtilsPromise();
  mActorPromise = wuPromise->Then(
      GetCurrentSerialEventTarget(), __func__,
      [self](RefPtr<WindowsUtilsParent> wup) {
        self->mActorPromise = nullptr;
        auto actor = MakeRefPtr<WindowsLocationParent>(self);
        if (!wup->SendPWindowsLocationConstructor(actor)) {
          LOG("WindowsLocationProvider(%p) SendPWindowsLocationConstructor "
              "failed",
              self.get());
          actor->DetachFromLocationProvider();
          self->mActor = nullptr;
          return WindowsLocationPromise::CreateAndReject(false, __func__);
        }
        LOG("WindowsLocationProvider connected to actor (%p,%p,%p)", self.get(),
            self->mActor.get(), self->mActorPromise.get());
        self->mActor = actor;
        return WindowsLocationPromise::CreateAndResolve(self->mActor, __func__);
      },

      [self](nsresult aError) {
        LOG("WindowsLocationProvider failed to connect to actor (%p,%p,%p)",
            self.get(), self->mActor.get(), self->mActorPromise.get());
        self->mActorPromise = nullptr;
        return WindowsLocationPromise::CreateAndReject(false, __func__);
      });

  if (mActor) {
    // Utility process already existed and mActorPromise was resolved
    // immediately.
    mActorPromise = nullptr;
  }
}

void WindowsLocationProvider::ReleaseUtilityProcess() {
  LOG("WindowsLocationProvider::ReleaseUtilityProcess(%p)", this);
  auto utilityProc = mozilla::ipc::UtilityProcessManager::GetIfExists();
  if (utilityProc) {
    utilityProc->ReleaseWindowsUtils();
  }
}

template <typename Fn>
bool WindowsLocationProvider::WhenActorIsReady(Fn&& fn) {
  if (mActor) {
    return fn(mActor);
  }

  if (mActorPromise) {
    mActorPromise->Then(
        GetCurrentSerialEventTarget(), __func__,
        [fn](const RefPtr<WindowsLocationParent>& actor) {
          Unused << fn(actor.get());
          return actor;
        },
        [](bool) { return false; });
    return true;
  }

  // The remote process failed to start.
  return false;
}

bool WindowsLocationProvider::SendStartup() {
  LOG("WindowsLocationProvider::SendStartup(%p)", this);
  MaybeCreateLocationActor();
  return WhenActorIsReady(
      [](WindowsLocationParent* actor) { return actor->SendStartup(); });
}

bool WindowsLocationProvider::SendRegisterForReport(
    nsIGeolocationUpdate* aCallback) {
  LOG("WindowsLocationProvider::SendRegisterForReport(%p)", this);
  RefPtr<WindowsLocationProvider> self = this;
  RefPtr<nsIGeolocationUpdate> cb = aCallback;
  return WhenActorIsReady([self, cb](WindowsLocationParent* actor) {
    MOZ_ASSERT(!self->mCallback);
    if (actor->SendRegisterForReport()) {
      self->mCallback = cb;
      return true;
    }
    return false;
  });
}

bool WindowsLocationProvider::SendUnregisterForReport() {
  LOG("WindowsLocationProvider::SendUnregisterForReport(%p)", this);
  RefPtr<WindowsLocationProvider> self = this;
  return WhenActorIsReady([self](WindowsLocationParent* actor) {
    self->mCallback = nullptr;
    if (actor->SendUnregisterForReport()) {
      return true;
    }
    return false;
  });
}

bool WindowsLocationProvider::SendSetHighAccuracy(bool aEnable) {
  LOG("WindowsLocationProvider::SendSetHighAccuracy(%p)", this);
  return WhenActorIsReady([aEnable](WindowsLocationParent* actor) {
    return actor->SendSetHighAccuracy(aEnable);
  });
}

bool WindowsLocationProvider::Send__delete__() {
  LOG("WindowsLocationProvider::Send__delete__(%p)", this);
  return WhenActorIsReady([self = RefPtr{this}](WindowsLocationParent*) {
    if (WindowsLocationParent::Send__delete__(self->mActor)) {
      if (self->mActor) {
        self->mActor->DetachFromLocationProvider();
        self->mActor = nullptr;
      }
      return true;
    }
    return false;
  });
}

void WindowsLocationProvider::RecvUpdate(
    RefPtr<nsIDOMGeoPosition> aGeoPosition) {
  LOG("WindowsLocationProvider::RecvUpdate(%p)", this);
  if (!mCallback) {
    return;
  }

  mCallback->Update(aGeoPosition.get());

  Telemetry::Accumulate(Telemetry::GEOLOCATION_WIN8_SOURCE_IS_MLS, false);
}

void WindowsLocationProvider::RecvFailed(uint16_t err) {
  LOG("WindowsLocationProvider::RecvFailed(%p)", this);
  // Cannot get current location at this time.  We use MLS instead.
  if (mMLSProvider || !mCallback) {
    return;
  }

  if (NS_SUCCEEDED(CreateAndWatchMLSProvider(mCallback))) {
    return;
  }

  // No ILocation and no MLS, so we have failed completely.
  // We keep strong references to objects that we need to guarantee
  // will live past the NotifyError callback.
  RefPtr<WindowsLocationProvider> self = this;
  nsCOMPtr<nsIGeolocationUpdate> callback = mCallback;
  callback->NotifyError(err);
}

void WindowsLocationProvider::ActorStopped() {
  // ActorDestroy has run.  Make sure UtilityProcessHost no longer tries to use
  // it.
  ReleaseUtilityProcess();

  if (mWatching) {
    // Treat as remote geolocation error, which will cause it to fallback
    // to MLS if it hasn't already.
    mWatching = false;
    RecvFailed(GeolocationPositionError_Binding::POSITION_UNAVAILABLE);
    return;
  }

  MOZ_ASSERT(!mActorPromise);
  if (mActor) {
    mActor->DetachFromLocationProvider();
    mActor = nullptr;
  }
}

NS_IMETHODIMP
WindowsLocationProvider::Startup() {
  LOG("WindowsLocationProvider::Startup(%p, %p, %p)", this, mActor.get(),
      mActorPromise.get());
  // If this fails, we will use the MLS fallback.
  SendStartup();
  return NS_OK;
}

NS_IMETHODIMP
WindowsLocationProvider::Watch(nsIGeolocationUpdate* aCallback) {
  LOG("WindowsLocationProvider::Watch(%p, %p, %p, %p, %d)", this, mActor.get(),
      mActorPromise.get(), aCallback, mWatching);
  if (mWatching) {
    return NS_OK;
  }

  if (SendRegisterForReport(aCallback)) {
    mWatching = true;
    return NS_OK;
  }

  // Couldn't send request.  We will use MLS instead.
  return CreateAndWatchMLSProvider(aCallback);
}

NS_IMETHODIMP
WindowsLocationProvider::Shutdown() {
  LOG("WindowsLocationProvider::Shutdown(%p, %p, %p)", this, mActor.get(),
      mActorPromise.get());

  if (mWatching) {
    SendUnregisterForReport();
    mWatching = false;
  }

  CancelMLSProvider();
  return NS_OK;
}

NS_IMETHODIMP
WindowsLocationProvider::SetHighAccuracy(bool enable) {
  LOG("WindowsLocationProvider::SetHighAccuracy(%p, %p, %p, %s)", this,
      mActor.get(), mActorPromise.get(), enable ? "true" : "false");
  if (mMLSProvider) {
    // Ignored when running MLS fallback.
    return NS_OK;
  }

  if (!SendSetHighAccuracy(enable)) {
    return NS_ERROR_FAILURE;
  }

  // Since we SendSetHighAccuracy asynchronously, we cannot say for sure
  // that it will succeed.  If it does fail then we will get a
  // RecvFailed IPC message, which will cause a fallback to MLS.
  return NS_OK;
}

nsresult WindowsLocationProvider::CreateAndWatchMLSProvider(
    nsIGeolocationUpdate* aCallback) {
  LOG("WindowsLocationProvider::CreateAndWatchMLSProvider"
      "(%p, %p, %p, %p, %p)",
      this, mMLSProvider.get(), mActor.get(), mActorPromise.get(), aCallback);

  if (mMLSProvider) {
    return NS_OK;
  }

  mMLSProvider = new MLSFallback(0);
  return mMLSProvider->Startup(new MLSUpdate(aCallback));
}

void WindowsLocationProvider::CancelMLSProvider() {
  LOG("WindowsLocationProvider::CancelMLSProvider"
      "(%p, %p, %p, %p, %p)",
      this, mMLSProvider.get(), mActor.get(), mActorPromise.get(),
      mCallback.get());

  if (!mMLSProvider) {
    return;
  }

  mMLSProvider->Shutdown();
  mMLSProvider = nullptr;
}

#undef LOG

}  // namespace mozilla::dom