Files
palemoon27/dom/system/windows/WindowsLocationProvider.cpp
T
roytam1 5851545f5e import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1193227 - Exclude tests from Mac B2G / Mulet packages. r=spohl (bc6219e278)
- Bug 1224000 - Install defaults/permissions file under browser/ instead of under browser/chrome/browser. r=mshal,r=MattN (9502588a4f)
- Bug 1244999 - Move icon-related DEFINES to moz.build in browser/app. r=mshal (b50bd16362)
- bits of Bug 1077148 part 4 (a0ee9d1e43)
- Bug 1264162 - Move branding installation to branding-common.mozbuild; r=glandium (96153e2196)
- align configures a little (69326315ba)
- Bug 1082983 - Enable the Windows console in ASAN builds; r=glandium (3839846239)
- Bug 1266875 - Remove custom install rules; r=chmanchester (91f673ee9f)
- remove allow warnings (3891acce36)
- Bug 1266129 - Upgrade ffvpx to 3.0.2. . r=ajones (fcbb57fdc5)
- Bug 1268138 - Call StringSplitString directly for internal use. r=till (1edf155d43)
- Bug 1268034 - Part 1: Reset constructor slot of GlobalObject to undefined when it fails to initialize constructor. r=till (1eaebb838b)
- Bug 1268034 - Part 2: Call setConstructor and initBuiltinConstructor after defining properties in all init function. r=till (fcf42ec3b7)
- bug 1139012 - telemetry for MLS vs win8 geolocation response. r=cpetrson (26745928e7)
- Bug 1253159 - Remove locationUpdatePending and restore request timeout. r=jdm (e37e1d992d)
- Bug 1250709 - Upgrade browser to gcc 4.9.3, r=glandium (2dc1262978)
- Bug 1250709 - Clobber builds, r=terrence (e56a27abcc)
- CSE some multiply-repeated ToFloatRegister(...) and such in some of the JIT backends into local variables for readability. No bug, r=bbouvier (7ee653c3c8)
- Bug 1266180 - Port unboxed object getprop stub to CacheIR. r=efaust (104e6aabae)
- Bug 1266695 - Port typed object getprop stub to CacheIR. r=efaust (3b627b5bd6)
- Bug 1263811 - Do not attach optimized IC for arguments element access if any arguments element has been overridden. r=jandem (7ffb405dd1)
- Bug 1266434 - Make Debugger::findScripts delazify scipts in a separate phase now this can GC r=jimb a=abuillings (b08013336e)
- Bug 1204170 - Fix icns file for b2g graphene builds. r=spohl (e43e2e9d4f)
- Bug 1201672 - Package more files for B2G desktop on OS X. r=spohl (e6cb92bd0d)
- Bug 1268863 - Follow-up to appease rooting hazard analysis (rs=sfink) (4f6aaafe27)
- Bug 1266649: TraceLogger - Handle failing to add to pointermap gracefully, r=bbouvier (12c006b2ed)
- Bug 1268740 - Change AllocateArrayBuffer to receive byteLength with |count * unit| format. r=lth (5f435125bc)
- Bug 1267755 - Print objects' class names in JS::ubi::dumpPaths; r=jimb (bace99e63d)
- Bug 1267737 - Request edge names from JS::ubi::RootList before calling `init`; r=jimb (1b858d7a0c)
- Bug 1266639 - Don't separately heap-allocate PLDHashTables within XPCMaps. r=mrbkap. (17bc8c82cc)
- Bug 1263205 - BaldrMonkey: add temporary Wasm.experimentalVersion (r=bbouvier) (f71b47386e)
- Bug 1219098 - Odin: Share JSFunction objects to make less garbage (r=bbouvier) (f602609a78)
2024-08-23 09:31:58 +08:00

290 lines
6.9 KiB
C++

/* -*- 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 "nsGeoPosition.h"
#include "nsIDOMGeoPositionError.h"
#include "nsComponentManagerUtils.h"
#include "prtime.h"
#include "MLSFallback.h"
#include "mozilla/Telemetry.h"
namespace mozilla {
namespace dom {
NS_IMPL_ISUPPORTS(WindowsLocationProvider::MLSUpdate, nsIGeolocationUpdate);
WindowsLocationProvider::MLSUpdate::MLSUpdate(nsIGeolocationUpdate* aCallback)
: mCallback(aCallback)
{
}
NS_IMETHODIMP
WindowsLocationProvider::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
WindowsLocationProvider::MLSUpdate::NotifyError(uint16_t aError)
{
if (!mCallback) {
return NS_ERROR_FAILURE;
}
return mCallback->NotifyError(aError);
}
class LocationEvent final : public ILocationEvents
{
public:
LocationEvent(nsIGeolocationUpdate* aCallback, WindowsLocationProvider *aProvider)
: mCallback(aCallback), mProvider(aProvider), mCount(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:
nsCOMPtr<nsIGeolocationUpdate> mCallback;
RefPtr<WindowsLocationProvider> mProvider;
ULONG mCount;
};
STDMETHODIMP_(ULONG)
LocationEvent::AddRef()
{
return InterlockedIncrement(&mCount);
}
STDMETHODIMP_(ULONG)
LocationEvent::Release()
{
ULONG count = InterlockedDecrement(&mCount);
if (!count) {
delete this;
return 0;
}
return count;
}
STDMETHODIMP
LocationEvent::QueryInterface(REFIID iid, void** ppv)
{
if (iid == IID_IUnknown) {
*ppv = static_cast<IUnknown*>(this);
} else if (iid == IID_ILocationEvents) {
*ppv = static_cast<ILocationEvents*>(this);
} else {
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
STDMETHODIMP
LocationEvent::OnStatusChanged(REFIID aReportType,
LOCATION_REPORT_STATUS aStatus)
{
if (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.
if (aStatus == REPORT_RUNNING) {
// location is found by Windows Location provider, we use it.
mProvider->CancelMLSProvider();
return S_OK;
}
// Cannot get current location at this time. We use MLS instead until
// Location API returns RUNNING status.
if (NS_SUCCEEDED(mProvider->CreateAndWatchMLSProvider(mCallback))) {
return S_OK;
}
// Cannot watch location by MLS provider. We must return error by
// Location API.
uint16_t err;
switch (aStatus) {
case REPORT_ACCESS_DENIED:
err = nsIDOMGeoPositionError::PERMISSION_DENIED;
break;
case REPORT_NOT_SUPPORTED:
case REPORT_ERROR:
err = nsIDOMGeoPositionError::POSITION_UNAVAILABLE;
break;
default:
return S_OK;
}
mCallback->NotifyError(err);
return S_OK;
}
STDMETHODIMP
LocationEvent::OnLocationChanged(REFIID aReportType,
ILocationReport *aReport)
{
if (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 = 0.0;
latLongReport->GetAltitude(&alt);
DOUBLE herror = 0.0;
latLongReport->GetErrorRadius(&herror);
DOUBLE verror = 0.0;
latLongReport->GetAltitudeError(&verror);
RefPtr<nsGeoPosition> position =
new nsGeoPosition(latitude, longitude, alt, herror, verror, 0.0, 0.0,
PR_Now());
mCallback->Update(position);
Telemetry::Accumulate(Telemetry::GEOLOCATION_WIN8_SOURCE_IS_MLS, false);
return S_OK;
}
NS_IMPL_ISUPPORTS(WindowsLocationProvider, nsIGeolocationProvider)
WindowsLocationProvider::WindowsLocationProvider()
{
}
WindowsLocationProvider::~WindowsLocationProvider()
{
}
NS_IMETHODIMP
WindowsLocationProvider::Startup()
{
RefPtr<ILocation> location;
if (FAILED(::CoCreateInstance(CLSID_Location, nullptr, CLSCTX_INPROC_SERVER,
IID_ILocation,
getter_AddRefs(location)))) {
// We will use MLS provider
return NS_OK;
}
IID reportTypes[] = { IID_ILatLongReport };
if (FAILED(location->RequestPermissions(nullptr, reportTypes, 1, FALSE))) {
// We will use MLS provider
return NS_OK;
}
mLocation = location;
return NS_OK;
}
NS_IMETHODIMP
WindowsLocationProvider::Watch(nsIGeolocationUpdate* aCallback)
{
if (mLocation) {
RefPtr<LocationEvent> event = new LocationEvent(aCallback, this);
if (SUCCEEDED(mLocation->RegisterForReport(event, IID_ILatLongReport, 0))) {
return NS_OK;
}
}
// Cannot use Location API. We will use MLS instead.
mLocation = nullptr;
return CreateAndWatchMLSProvider(aCallback);
}
NS_IMETHODIMP
WindowsLocationProvider::Shutdown()
{
if (mLocation) {
mLocation->UnregisterForReport(IID_ILatLongReport);
mLocation = nullptr;
}
CancelMLSProvider();
return NS_OK;
}
NS_IMETHODIMP
WindowsLocationProvider::SetHighAccuracy(bool enable)
{
if (!mLocation) {
// MLS provider doesn't support HighAccuracy
return NS_OK;
}
LOCATION_DESIRED_ACCURACY desiredAccuracy;
if (enable) {
desiredAccuracy = LOCATION_DESIRED_ACCURACY_HIGH;
} else {
desiredAccuracy = LOCATION_DESIRED_ACCURACY_DEFAULT;
}
if (FAILED(mLocation->SetDesiredAccuracy(IID_ILatLongReport,
desiredAccuracy))) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
nsresult
WindowsLocationProvider::CreateAndWatchMLSProvider(
nsIGeolocationUpdate* aCallback)
{
if (mMLSProvider) {
return NS_OK;
}
mMLSProvider = new MLSFallback();
return mMLSProvider->Startup(new MLSUpdate(aCallback));
}
void
WindowsLocationProvider::CancelMLSProvider()
{
if (!mMLSProvider) {
return;
}
mMLSProvider->Shutdown();
mMLSProvider = nullptr;
}
} // namespace dom
} // namespace mozilla