2013-07-01 13:43:17 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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 "nsCOMPtr.h"
|
|
|
|
#include "nsGeoPosition.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIDOMGeoPositionError.h"
|
|
|
|
#include "CoreLocationLocationProvider.h"
|
2013-10-25 02:10:10 +00:00
|
|
|
#include "nsCocoaFeatures.h"
|
2013-07-03 15:56:26 +00:00
|
|
|
#include "prtime.h"
|
2015-02-10 15:45:36 +00:00
|
|
|
#include "mozilla/Telemetry.h"
|
2015-02-28 00:15:32 +00:00
|
|
|
#include "MLSFallback.h"
|
2013-07-01 13:43:17 +00:00
|
|
|
|
2013-07-10 17:07:50 +00:00
|
|
|
#include <CoreLocation/CLError.h>
|
|
|
|
#include <CoreLocation/CLLocation.h>
|
|
|
|
#include <CoreLocation/CLLocationManager.h>
|
|
|
|
#include <CoreLocation/CLLocationManagerDelegate.h>
|
2013-07-01 13:43:17 +00:00
|
|
|
|
2013-10-21 00:53:09 +00:00
|
|
|
#include <objc/objc.h>
|
|
|
|
#include <objc/objc-runtime.h>
|
|
|
|
|
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
|
2013-07-01 13:43:17 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
static const CLLocationAccuracy kHIGH_ACCURACY = kCLLocationAccuracyBest;
|
|
|
|
static const CLLocationAccuracy kDEFAULT_ACCURACY = kCLLocationAccuracyNearestTenMeters;
|
|
|
|
|
|
|
|
@interface LocationDelegate : NSObject <CLLocationManagerDelegate>
|
|
|
|
{
|
|
|
|
CoreLocationLocationProvider* mProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init:(CoreLocationLocationProvider*)aProvider;
|
|
|
|
- (void)locationManager:(CLLocationManager*)aManager
|
|
|
|
didFailWithError:(NSError *)aError;
|
2015-01-27 07:26:00 +00:00
|
|
|
- (void)locationManager:(CLLocationManager*)aManager didUpdateLocations:(NSArray*)locations;
|
2013-07-01 13:43:17 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation LocationDelegate
|
|
|
|
- (id) init:(CoreLocationLocationProvider*) aProvider
|
|
|
|
{
|
2015-02-06 08:09:39 +00:00
|
|
|
if ((self = [super init])) {
|
2013-07-01 13:43:17 +00:00
|
|
|
mProvider = aProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)locationManager:(CLLocationManager*)aManager
|
|
|
|
didFailWithError:(NSError *)aError
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService> console =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE_VOID(console);
|
|
|
|
|
|
|
|
NSString* message =
|
|
|
|
[@"Failed to acquire position: " stringByAppendingString: [aError localizedDescription]];
|
|
|
|
|
|
|
|
console->LogStringMessage(NS_ConvertUTF8toUTF16([message UTF8String]).get());
|
|
|
|
|
|
|
|
if ([aError code] == kCLErrorDenied) {
|
2015-01-27 07:26:00 +00:00
|
|
|
mProvider->NotifyError(nsIDOMGeoPositionError::PERMISSION_DENIED);
|
|
|
|
return;
|
2013-07-01 13:43:17 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 00:15:32 +00:00
|
|
|
// The CL provider does not fallback to GeoIP, so use NetworkGeolocationProvider for this.
|
|
|
|
// The concept here is: on error, hand off geolocation to MLS, which will then report
|
|
|
|
// back a location or error. We can't call this with no delay however, as this method
|
|
|
|
// is called with an error code of 0 in both failed geolocation cases, and also when
|
|
|
|
// geolocation is not immediately available.
|
|
|
|
// The 2 sec delay is arbitrarily large enough that CL has a reasonable head start and
|
|
|
|
// if it is likely to succeed, it should complete before the MLS provider.
|
|
|
|
// Take note that in locationManager:didUpdateLocations: the handoff to MLS is stopped.
|
|
|
|
mProvider->CreateMLSFallbackProvider();
|
2013-07-01 13:43:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 07:26:00 +00:00
|
|
|
- (void)locationManager:(CLLocationManager*)aManager didUpdateLocations:(NSArray*)aLocations
|
2013-07-01 13:43:17 +00:00
|
|
|
{
|
2015-01-27 07:26:00 +00:00
|
|
|
if (aLocations.count < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProvider->CancelMLSFallbackProvider();
|
|
|
|
|
|
|
|
CLLocation* location = [aLocations objectAtIndex:0];
|
|
|
|
|
2013-07-01 13:43:17 +00:00
|
|
|
nsCOMPtr<nsIDOMGeoPosition> geoPosition =
|
2015-01-27 07:26:00 +00:00
|
|
|
new nsGeoPosition(location.coordinate.latitude,
|
|
|
|
location.coordinate.longitude,
|
|
|
|
location.altitude,
|
|
|
|
location.horizontalAccuracy,
|
|
|
|
location.verticalAccuracy,
|
|
|
|
location.course,
|
|
|
|
location.speed,
|
2013-07-01 13:43:17 +00:00
|
|
|
PR_Now());
|
|
|
|
|
|
|
|
mProvider->Update(geoPosition);
|
2015-02-10 15:45:36 +00:00
|
|
|
Telemetry::Accumulate(Telemetry::GEOLOCATION_OSX_SOURCE_IS_MLS, false);
|
2013-07-01 13:43:17 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2015-01-27 07:26:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(CoreLocationLocationProvider::MLSUpdate, nsIGeolocationUpdate);
|
|
|
|
|
|
|
|
CoreLocationLocationProvider::MLSUpdate::MLSUpdate(CoreLocationLocationProvider& parentProvider)
|
|
|
|
: mParentLocationProvider(parentProvider)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-28 00:15:32 +00:00
|
|
|
CoreLocationLocationProvider::MLSUpdate::~MLSUpdate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-27 07:26:00 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CoreLocationLocationProvider::MLSUpdate::Update(nsIDOMGeoPosition *position)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMGeoPositionCoords> coords;
|
|
|
|
position->GetCoords(getter_AddRefs(coords));
|
|
|
|
if (!coords) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mParentLocationProvider.Update(position);
|
2015-02-10 15:45:36 +00:00
|
|
|
Telemetry::Accumulate(Telemetry::GEOLOCATION_OSX_SOURCE_IS_MLS, true);
|
2015-01-27 07:26:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CoreLocationLocationProvider::MLSUpdate::LocationUpdatePending()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CoreLocationLocationProvider::MLSUpdate::NotifyError(uint16_t error)
|
|
|
|
{
|
|
|
|
mParentLocationProvider.NotifyError(error);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-01 13:43:17 +00:00
|
|
|
class CoreLocationObjects {
|
|
|
|
public:
|
|
|
|
NS_METHOD Init(CoreLocationLocationProvider* aProvider) {
|
|
|
|
mLocationManager = [[CLLocationManager alloc] init];
|
|
|
|
NS_ENSURE_TRUE(mLocationManager, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
mLocationDelegate = [[LocationDelegate alloc] init:aProvider];
|
|
|
|
NS_ENSURE_TRUE(mLocationDelegate, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
mLocationManager.desiredAccuracy = kDEFAULT_ACCURACY;
|
|
|
|
mLocationManager.delegate = mLocationDelegate;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
~CoreLocationObjects() {
|
|
|
|
if (mLocationManager) {
|
|
|
|
[mLocationManager release];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLocationDelegate) {
|
|
|
|
[mLocationDelegate release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LocationDelegate* mLocationDelegate;
|
|
|
|
CLLocationManager* mLocationManager;
|
|
|
|
};
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(CoreLocationLocationProvider, nsIGeolocationProvider)
|
2013-07-01 13:43:17 +00:00
|
|
|
|
|
|
|
CoreLocationLocationProvider::CoreLocationLocationProvider()
|
2015-01-27 07:26:00 +00:00
|
|
|
: mCLObjects(nullptr), mMLSFallbackProvider(nullptr)
|
2013-07-01 13:43:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-28 00:15:32 +00:00
|
|
|
CoreLocationLocationProvider::~CoreLocationLocationProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-01 13:43:17 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CoreLocationLocationProvider::Startup()
|
|
|
|
{
|
|
|
|
if (!mCLObjects) {
|
|
|
|
nsAutoPtr<CoreLocationObjects> clObjs(new CoreLocationObjects());
|
|
|
|
|
|
|
|
nsresult rv = clObjs->Init(this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mCLObjects = clObjs.forget();
|
|
|
|
}
|
|
|
|
|
2015-01-14 19:02:00 +00:00
|
|
|
// Must be stopped before starting or response (success or failure) is not guaranteed
|
|
|
|
[mCLObjects->mLocationManager stopUpdatingLocation];
|
2013-07-01 13:43:17 +00:00
|
|
|
[mCLObjects->mLocationManager startUpdatingLocation];
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CoreLocationLocationProvider::Watch(nsIGeolocationUpdate* aCallback)
|
|
|
|
{
|
|
|
|
if (mCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCallback = aCallback;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CoreLocationLocationProvider::Shutdown()
|
|
|
|
{
|
|
|
|
NS_ENSURE_STATE(mCLObjects);
|
|
|
|
|
|
|
|
[mCLObjects->mLocationManager stopUpdatingLocation];
|
|
|
|
|
|
|
|
delete mCLObjects;
|
|
|
|
mCLObjects = nullptr;
|
2015-01-27 07:26:00 +00:00
|
|
|
|
|
|
|
if (mMLSFallbackProvider) {
|
|
|
|
mMLSFallbackProvider->Shutdown();
|
|
|
|
mMLSFallbackProvider = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-01 13:43:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CoreLocationLocationProvider::SetHighAccuracy(bool aEnable)
|
|
|
|
{
|
|
|
|
NS_ENSURE_STATE(mCLObjects);
|
|
|
|
|
|
|
|
mCLObjects->mLocationManager.desiredAccuracy =
|
|
|
|
(aEnable ? kHIGH_ACCURACY : kDEFAULT_ACCURACY);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:04:21 +00:00
|
|
|
void
|
2013-07-01 13:43:17 +00:00
|
|
|
CoreLocationLocationProvider::Update(nsIDOMGeoPosition* aSomewhere)
|
|
|
|
{
|
|
|
|
if (aSomewhere && mCallback) {
|
|
|
|
mCallback->Update(aSomewhere);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:04:21 +00:00
|
|
|
void
|
2013-07-01 13:43:17 +00:00
|
|
|
CoreLocationLocationProvider::NotifyError(uint16_t aErrorCode)
|
|
|
|
{
|
|
|
|
mCallback->NotifyError(aErrorCode);
|
|
|
|
}
|
2015-01-27 07:26:00 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
CoreLocationLocationProvider::CreateMLSFallbackProvider()
|
|
|
|
{
|
|
|
|
if (mMLSFallbackProvider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-28 00:15:32 +00:00
|
|
|
mMLSFallbackProvider = new MLSFallback();
|
|
|
|
mMLSFallbackProvider->Startup(new MLSUpdate(*this));
|
2015-01-27 07:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CoreLocationLocationProvider::CancelMLSFallbackProvider()
|
|
|
|
{
|
|
|
|
if (!mMLSFallbackProvider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMLSFallbackProvider->Shutdown();
|
|
|
|
mMLSFallbackProvider = nullptr;
|
|
|
|
}
|