summaryrefslogtreecommitdiffstats
path: root/dom/system/windows/location/WindowsLocationChild.cpp
blob: 514ae4610f181c95c8164ab1764c7a9391277fe0 (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
/* -*- 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 "WindowsLocationChild.h"
#include "nsCOMPtr.h"
#include "WindowsLocationProvider.h"
#include "mozilla/dom/GeolocationPosition.h"
#include "mozilla/dom/GeolocationPositionErrorBinding.h"
#include "mozilla/Telemetry.h"
#include "nsIGeolocationProvider.h"

#include <locationapi.h>

namespace mozilla::dom {

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

class LocationEvent final : public ILocationEvents {
 public:
  explicit LocationEvent(WindowsLocationChild* aActor)
      : mActor(aActor), mRefCnt(0) {}

  // IUnknown interface
  STDMETHODIMP_(ULONG) AddRef() override;
  STDMETHODIMP_(ULONG) Release() override;
  STDMETHODIMP QueryInterface(REFIID iid, void** ppv) override;

  // ILocationEvents interface
  STDMETHODIMP OnStatusChanged(REFIID aReportType,
                               LOCATION_REPORT_STATUS aStatus) override;
  STDMETHODIMP OnLocationChanged(REFIID aReportType,
                                 ILocationReport* aReport) override;

 private:
  // Making this a WeakPtr breaks the following cycle of strong references:
  // WindowsLocationChild -> ILocation -> ILocationEvents (this)
  //   -> WindowsLocationChild.
  WeakPtr<WindowsLocationChild> mActor;

  ULONG mRefCnt;
};

STDMETHODIMP_(ULONG)
LocationEvent::AddRef() { return InterlockedIncrement(&mRefCnt); }

STDMETHODIMP_(ULONG)
LocationEvent::Release() {
  ULONG count = InterlockedDecrement(&mRefCnt);
  if (!count) {
    delete this;
    return 0;
  }
  return count;
}

STDMETHODIMP
LocationEvent::QueryInterface(REFIID iid, void** ppv) {
  if (!ppv) {
    return E_INVALIDARG;
  }

  if (iid == IID_IUnknown) {
    *ppv = static_cast<IUnknown*>(this);
  } else if (iid == IID_ILocationEvents) {
    *ppv = static_cast<ILocationEvents*>(this);
  } else {
    *ppv = nullptr;
    return E_NOINTERFACE;
  }

  AddRef();
  return S_OK;
}

STDMETHODIMP
LocationEvent::OnStatusChanged(REFIID aReportType,
                               LOCATION_REPORT_STATUS aStatus) {
  LOG("LocationEvent::OnStatusChanged(%p, %p, %s, %04x)", this, mActor.get(),
      aReportType == IID_ILatLongReport ? "true" : "false",
      static_cast<uint32_t>(aStatus));

  if (!mActor || aReportType != IID_ILatLongReport) {
    return S_OK;
  }

  // When registering event, REPORT_INITIALIZING is fired at first.
  // Then, when the location is found, REPORT_RUNNING is fired.
  // We ignore those messages.
  uint16_t err;
  switch (aStatus) {
    case REPORT_ACCESS_DENIED:
      err = GeolocationPositionError_Binding::PERMISSION_DENIED;
      break;
    case REPORT_NOT_SUPPORTED:
    case REPORT_ERROR:
      err = GeolocationPositionError_Binding::POSITION_UNAVAILABLE;
      break;
    default:
      return S_OK;
  }

  mActor->SendFailed(err);
  return S_OK;
}

STDMETHODIMP
LocationEvent::OnLocationChanged(REFIID aReportType, ILocationReport* aReport) {
  LOG("LocationEvent::OnLocationChanged(%p, %p, %s)", this, mActor.get(),
      aReportType == IID_ILatLongReport ? "true" : "false");

  if (!mActor || aReportType != IID_ILatLongReport) {
    return S_OK;
  }

  RefPtr<ILatLongReport> latLongReport;
  if (FAILED(aReport->QueryInterface(IID_ILatLongReport,
                                     getter_AddRefs(latLongReport)))) {
    return E_FAIL;
  }

  DOUBLE latitude = 0.0;
  latLongReport->GetLatitude(&latitude);

  DOUBLE longitude = 0.0;
  latLongReport->GetLongitude(&longitude);

  DOUBLE alt = UnspecifiedNaN<double>();
  latLongReport->GetAltitude(&alt);

  DOUBLE herror = 0.0;
  latLongReport->GetErrorRadius(&herror);

  DOUBLE verror = UnspecifiedNaN<double>();
  latLongReport->GetAltitudeError(&verror);

  double heading = UnspecifiedNaN<double>();
  double speed = UnspecifiedNaN<double>();

  // nsGeoPositionCoords will convert NaNs to null for optional properties of
  // the JavaScript Coordinates object.
  RefPtr<nsGeoPosition> position =
      new nsGeoPosition(latitude, longitude, alt, herror, verror, heading,
                        speed, PR_Now() / PR_USEC_PER_MSEC);
  mActor->SendUpdate(position);

  return S_OK;
}

WindowsLocationChild::WindowsLocationChild() {
  LOG("WindowsLocationChild::WindowsLocationChild(%p)", this);
}

WindowsLocationChild::~WindowsLocationChild() {
  LOG("WindowsLocationChild::~WindowsLocationChild(%p)", this);
}

::mozilla::ipc::IPCResult WindowsLocationChild::RecvStartup() {
  LOG("WindowsLocationChild::RecvStartup(%p, %p)", this, mLocation.get());
  if (mLocation) {
    return IPC_OK();
  }

  RefPtr<ILocation> location;
  if (FAILED(::CoCreateInstance(CLSID_Location, nullptr, CLSCTX_INPROC_SERVER,
                                IID_ILocation, getter_AddRefs(location)))) {
    LOG("WindowsLocationChild(%p) failed to create ILocation", this);
    // We will use MLS provider
    SendFailed(GeolocationPositionError_Binding::POSITION_UNAVAILABLE);
    return IPC_OK();
  }

  IID reportTypes[] = {IID_ILatLongReport};
  if (FAILED(location->RequestPermissions(nullptr, reportTypes, 1, FALSE))) {
    LOG("WindowsLocationChild(%p) failed to set ILocation permissions", this);
    // We will use MLS provider
    SendFailed(GeolocationPositionError_Binding::POSITION_UNAVAILABLE);
    return IPC_OK();
  }

  mLocation = location;
  return IPC_OK();
}

::mozilla::ipc::IPCResult WindowsLocationChild::RecvSetHighAccuracy(
    bool aEnable) {
  LOG("WindowsLocationChild::RecvSetHighAccuracy(%p, %p, %s)", this,
      mLocation.get(), aEnable ? "true" : "false");

  // We sometimes call SetHighAccuracy before Startup, so we save the
  // request and set it later, in RegisterForReport.
  mHighAccuracy = aEnable;

  return IPC_OK();
}

::mozilla::ipc::IPCResult WindowsLocationChild::RecvRegisterForReport() {
  LOG("WindowsLocationChild::RecvRegisterForReport(%p, %p)", this,
      mLocation.get());

  if (!mLocation) {
    SendFailed(GeolocationPositionError_Binding::POSITION_UNAVAILABLE);
    return IPC_OK();
  }

  LOCATION_DESIRED_ACCURACY desiredAccuracy;
  if (mHighAccuracy) {
    desiredAccuracy = LOCATION_DESIRED_ACCURACY_HIGH;
  } else {
    desiredAccuracy = LOCATION_DESIRED_ACCURACY_DEFAULT;
  }

  if (NS_WARN_IF(FAILED(mLocation->SetDesiredAccuracy(IID_ILatLongReport,
                                                      desiredAccuracy)))) {
    SendFailed(GeolocationPositionError_Binding::POSITION_UNAVAILABLE);
    return IPC_OK();
  }

  auto event = MakeRefPtr<LocationEvent>(this);
  if (NS_WARN_IF(
          FAILED(mLocation->RegisterForReport(event, IID_ILatLongReport, 0)))) {
    SendFailed(GeolocationPositionError_Binding::POSITION_UNAVAILABLE);
  }

  LOG("WindowsLocationChild::RecvRegisterForReport successfully registered");
  return IPC_OK();
}

::mozilla::ipc::IPCResult WindowsLocationChild::RecvUnregisterForReport() {
  LOG("WindowsLocationChild::RecvUnregisterForReport(%p, %p)", this,
      mLocation.get());

  if (!mLocation) {
    return IPC_OK();
  }

  // This will free the LocationEvent we created in RecvRegisterForReport.
  Unused << NS_WARN_IF(
      FAILED(mLocation->UnregisterForReport(IID_ILatLongReport)));

  // The ILocation object is not reusable.  Unregistering, restarting and
  // re-registering for reports does not work;  the callback is never
  // called in that case.  For that reason, we re-create the ILocation
  // object with a call to Startup after unregistering if we need it again.
  mLocation = nullptr;
  return IPC_OK();
}

void WindowsLocationChild::ActorDestroy(ActorDestroyReason aWhy) {
  LOG("WindowsLocationChild::ActorDestroy(%p, %p)", this, mLocation.get());
  mLocation = nullptr;
}

}  // namespace mozilla::dom