michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsGeoPosition.h" michael@0: #include "nsIConsoleService.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsIDOMGeoPositionError.h" michael@0: #include "CoreLocationLocationProvider.h" michael@0: #include "nsCocoaFeatures.h" michael@0: #include "prtime.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "nsObjCExceptions.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: static const CLLocationAccuracy kHIGH_ACCURACY = kCLLocationAccuracyBest; michael@0: static const CLLocationAccuracy kDEFAULT_ACCURACY = kCLLocationAccuracyNearestTenMeters; michael@0: michael@0: @interface LocationDelegate : NSObject michael@0: { michael@0: CoreLocationLocationProvider* mProvider; michael@0: } michael@0: michael@0: - (id)init:(CoreLocationLocationProvider*)aProvider; michael@0: - (void)locationManager:(CLLocationManager*)aManager michael@0: didFailWithError:(NSError *)aError; michael@0: michael@0: /* XXX (ggp) didUpdateToLocation is supposedly deprecated in favor of michael@0: * locationManager:didUpdateLocations, which is undocumented and didn't seem to michael@0: * work for me. This should be changed in the future, though. michael@0: */ michael@0: - (void)locationManager:(CLLocationManager*)aManager michael@0: didUpdateToLocation:(CLLocation *)aNewLocation michael@0: fromLocation:(CLLocation *)aOldLocation; michael@0: @end michael@0: michael@0: @implementation LocationDelegate michael@0: - (id) init:(CoreLocationLocationProvider*) aProvider michael@0: { michael@0: if (self = [super init]) { michael@0: mProvider = aProvider; michael@0: } michael@0: michael@0: return self; michael@0: } michael@0: michael@0: - (void)locationManager:(CLLocationManager*)aManager michael@0: didFailWithError:(NSError *)aError michael@0: { michael@0: nsCOMPtr console = michael@0: do_GetService(NS_CONSOLESERVICE_CONTRACTID); michael@0: michael@0: NS_ENSURE_TRUE_VOID(console); michael@0: michael@0: NSString* message = michael@0: [@"Failed to acquire position: " stringByAppendingString: [aError localizedDescription]]; michael@0: michael@0: console->LogStringMessage(NS_ConvertUTF8toUTF16([message UTF8String]).get()); michael@0: michael@0: uint16_t err = nsIDOMGeoPositionError::POSITION_UNAVAILABLE; michael@0: if ([aError code] == kCLErrorDenied) { michael@0: err = nsIDOMGeoPositionError::PERMISSION_DENIED; michael@0: } michael@0: michael@0: mProvider->NotifyError(err); michael@0: } michael@0: michael@0: - (void)locationManager:(CLLocationManager*)aManager michael@0: didUpdateToLocation:(CLLocation *)aNewLocation michael@0: fromLocation:(CLLocation *)aOldLocation michael@0: { michael@0: nsCOMPtr geoPosition = michael@0: new nsGeoPosition(aNewLocation.coordinate.latitude, michael@0: aNewLocation.coordinate.longitude, michael@0: aNewLocation.altitude, michael@0: aNewLocation.horizontalAccuracy, michael@0: aNewLocation.verticalAccuracy, michael@0: aNewLocation.course, michael@0: aNewLocation.speed, michael@0: PR_Now()); michael@0: michael@0: mProvider->Update(geoPosition); michael@0: } michael@0: @end michael@0: michael@0: class CoreLocationObjects { michael@0: public: michael@0: NS_METHOD Init(CoreLocationLocationProvider* aProvider) { michael@0: mLocationManager = [[CLLocationManager alloc] init]; michael@0: NS_ENSURE_TRUE(mLocationManager, NS_ERROR_NOT_AVAILABLE); michael@0: michael@0: mLocationDelegate = [[LocationDelegate alloc] init:aProvider]; michael@0: NS_ENSURE_TRUE(mLocationDelegate, NS_ERROR_NOT_AVAILABLE); michael@0: michael@0: mLocationManager.desiredAccuracy = kDEFAULT_ACCURACY; michael@0: mLocationManager.delegate = mLocationDelegate; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: ~CoreLocationObjects() { michael@0: if (mLocationManager) { michael@0: [mLocationManager release]; michael@0: } michael@0: michael@0: if (mLocationDelegate) { michael@0: [mLocationDelegate release]; michael@0: } michael@0: } michael@0: michael@0: LocationDelegate* mLocationDelegate; michael@0: CLLocationManager* mLocationManager; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(CoreLocationLocationProvider, nsIGeolocationProvider) michael@0: michael@0: CoreLocationLocationProvider::CoreLocationLocationProvider() michael@0: : mCLObjects(nullptr) michael@0: { michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: CoreLocationLocationProvider::Startup() michael@0: { michael@0: if (!mCLObjects) { michael@0: nsAutoPtr clObjs(new CoreLocationObjects()); michael@0: michael@0: nsresult rv = clObjs->Init(this); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mCLObjects = clObjs.forget(); michael@0: } michael@0: michael@0: [mCLObjects->mLocationManager startUpdatingLocation]; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: CoreLocationLocationProvider::Watch(nsIGeolocationUpdate* aCallback) michael@0: { michael@0: if (mCallback) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: mCallback = aCallback; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: CoreLocationLocationProvider::Shutdown() michael@0: { michael@0: NS_ENSURE_STATE(mCLObjects); michael@0: michael@0: [mCLObjects->mLocationManager stopUpdatingLocation]; michael@0: michael@0: delete mCLObjects; michael@0: mCLObjects = nullptr; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: CoreLocationLocationProvider::SetHighAccuracy(bool aEnable) michael@0: { michael@0: NS_ENSURE_STATE(mCLObjects); michael@0: michael@0: mCLObjects->mLocationManager.desiredAccuracy = michael@0: (aEnable ? kHIGH_ACCURACY : kDEFAULT_ACCURACY); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: CoreLocationLocationProvider::Update(nsIDOMGeoPosition* aSomewhere) michael@0: { michael@0: if (aSomewhere && mCallback) { michael@0: mCallback->Update(aSomewhere); michael@0: } michael@0: } michael@0: michael@0: void michael@0: CoreLocationLocationProvider::NotifyError(uint16_t aErrorCode) michael@0: { michael@0: mCallback->NotifyError(aErrorCode); michael@0: }