Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsCOMPtr.h" |
michael@0 | 8 | #include "nsGeoPosition.h" |
michael@0 | 9 | #include "nsIConsoleService.h" |
michael@0 | 10 | #include "nsServiceManagerUtils.h" |
michael@0 | 11 | #include "nsIDOMGeoPositionError.h" |
michael@0 | 12 | #include "CoreLocationLocationProvider.h" |
michael@0 | 13 | #include "nsCocoaFeatures.h" |
michael@0 | 14 | #include "prtime.h" |
michael@0 | 15 | |
michael@0 | 16 | #include <CoreLocation/CLError.h> |
michael@0 | 17 | #include <CoreLocation/CLLocation.h> |
michael@0 | 18 | #include <CoreLocation/CLLocationManager.h> |
michael@0 | 19 | #include <CoreLocation/CLLocationManagerDelegate.h> |
michael@0 | 20 | |
michael@0 | 21 | #include <objc/objc.h> |
michael@0 | 22 | #include <objc/objc-runtime.h> |
michael@0 | 23 | |
michael@0 | 24 | #include "nsObjCExceptions.h" |
michael@0 | 25 | |
michael@0 | 26 | using namespace mozilla; |
michael@0 | 27 | |
michael@0 | 28 | static const CLLocationAccuracy kHIGH_ACCURACY = kCLLocationAccuracyBest; |
michael@0 | 29 | static const CLLocationAccuracy kDEFAULT_ACCURACY = kCLLocationAccuracyNearestTenMeters; |
michael@0 | 30 | |
michael@0 | 31 | @interface LocationDelegate : NSObject <CLLocationManagerDelegate> |
michael@0 | 32 | { |
michael@0 | 33 | CoreLocationLocationProvider* mProvider; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | - (id)init:(CoreLocationLocationProvider*)aProvider; |
michael@0 | 37 | - (void)locationManager:(CLLocationManager*)aManager |
michael@0 | 38 | didFailWithError:(NSError *)aError; |
michael@0 | 39 | |
michael@0 | 40 | /* XXX (ggp) didUpdateToLocation is supposedly deprecated in favor of |
michael@0 | 41 | * locationManager:didUpdateLocations, which is undocumented and didn't seem to |
michael@0 | 42 | * work for me. This should be changed in the future, though. |
michael@0 | 43 | */ |
michael@0 | 44 | - (void)locationManager:(CLLocationManager*)aManager |
michael@0 | 45 | didUpdateToLocation:(CLLocation *)aNewLocation |
michael@0 | 46 | fromLocation:(CLLocation *)aOldLocation; |
michael@0 | 47 | @end |
michael@0 | 48 | |
michael@0 | 49 | @implementation LocationDelegate |
michael@0 | 50 | - (id) init:(CoreLocationLocationProvider*) aProvider |
michael@0 | 51 | { |
michael@0 | 52 | if (self = [super init]) { |
michael@0 | 53 | mProvider = aProvider; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | return self; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | - (void)locationManager:(CLLocationManager*)aManager |
michael@0 | 60 | didFailWithError:(NSError *)aError |
michael@0 | 61 | { |
michael@0 | 62 | nsCOMPtr<nsIConsoleService> console = |
michael@0 | 63 | do_GetService(NS_CONSOLESERVICE_CONTRACTID); |
michael@0 | 64 | |
michael@0 | 65 | NS_ENSURE_TRUE_VOID(console); |
michael@0 | 66 | |
michael@0 | 67 | NSString* message = |
michael@0 | 68 | [@"Failed to acquire position: " stringByAppendingString: [aError localizedDescription]]; |
michael@0 | 69 | |
michael@0 | 70 | console->LogStringMessage(NS_ConvertUTF8toUTF16([message UTF8String]).get()); |
michael@0 | 71 | |
michael@0 | 72 | uint16_t err = nsIDOMGeoPositionError::POSITION_UNAVAILABLE; |
michael@0 | 73 | if ([aError code] == kCLErrorDenied) { |
michael@0 | 74 | err = nsIDOMGeoPositionError::PERMISSION_DENIED; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | mProvider->NotifyError(err); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | - (void)locationManager:(CLLocationManager*)aManager |
michael@0 | 81 | didUpdateToLocation:(CLLocation *)aNewLocation |
michael@0 | 82 | fromLocation:(CLLocation *)aOldLocation |
michael@0 | 83 | { |
michael@0 | 84 | nsCOMPtr<nsIDOMGeoPosition> geoPosition = |
michael@0 | 85 | new nsGeoPosition(aNewLocation.coordinate.latitude, |
michael@0 | 86 | aNewLocation.coordinate.longitude, |
michael@0 | 87 | aNewLocation.altitude, |
michael@0 | 88 | aNewLocation.horizontalAccuracy, |
michael@0 | 89 | aNewLocation.verticalAccuracy, |
michael@0 | 90 | aNewLocation.course, |
michael@0 | 91 | aNewLocation.speed, |
michael@0 | 92 | PR_Now()); |
michael@0 | 93 | |
michael@0 | 94 | mProvider->Update(geoPosition); |
michael@0 | 95 | } |
michael@0 | 96 | @end |
michael@0 | 97 | |
michael@0 | 98 | class CoreLocationObjects { |
michael@0 | 99 | public: |
michael@0 | 100 | NS_METHOD Init(CoreLocationLocationProvider* aProvider) { |
michael@0 | 101 | mLocationManager = [[CLLocationManager alloc] init]; |
michael@0 | 102 | NS_ENSURE_TRUE(mLocationManager, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 103 | |
michael@0 | 104 | mLocationDelegate = [[LocationDelegate alloc] init:aProvider]; |
michael@0 | 105 | NS_ENSURE_TRUE(mLocationDelegate, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 106 | |
michael@0 | 107 | mLocationManager.desiredAccuracy = kDEFAULT_ACCURACY; |
michael@0 | 108 | mLocationManager.delegate = mLocationDelegate; |
michael@0 | 109 | |
michael@0 | 110 | return NS_OK; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | ~CoreLocationObjects() { |
michael@0 | 114 | if (mLocationManager) { |
michael@0 | 115 | [mLocationManager release]; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | if (mLocationDelegate) { |
michael@0 | 119 | [mLocationDelegate release]; |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | LocationDelegate* mLocationDelegate; |
michael@0 | 124 | CLLocationManager* mLocationManager; |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | NS_IMPL_ISUPPORTS(CoreLocationLocationProvider, nsIGeolocationProvider) |
michael@0 | 128 | |
michael@0 | 129 | CoreLocationLocationProvider::CoreLocationLocationProvider() |
michael@0 | 130 | : mCLObjects(nullptr) |
michael@0 | 131 | { |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | NS_IMETHODIMP |
michael@0 | 135 | CoreLocationLocationProvider::Startup() |
michael@0 | 136 | { |
michael@0 | 137 | if (!mCLObjects) { |
michael@0 | 138 | nsAutoPtr<CoreLocationObjects> clObjs(new CoreLocationObjects()); |
michael@0 | 139 | |
michael@0 | 140 | nsresult rv = clObjs->Init(this); |
michael@0 | 141 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 142 | |
michael@0 | 143 | mCLObjects = clObjs.forget(); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | [mCLObjects->mLocationManager startUpdatingLocation]; |
michael@0 | 147 | return NS_OK; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | NS_IMETHODIMP |
michael@0 | 151 | CoreLocationLocationProvider::Watch(nsIGeolocationUpdate* aCallback) |
michael@0 | 152 | { |
michael@0 | 153 | if (mCallback) { |
michael@0 | 154 | return NS_OK; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | mCallback = aCallback; |
michael@0 | 158 | return NS_OK; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | NS_IMETHODIMP |
michael@0 | 162 | CoreLocationLocationProvider::Shutdown() |
michael@0 | 163 | { |
michael@0 | 164 | NS_ENSURE_STATE(mCLObjects); |
michael@0 | 165 | |
michael@0 | 166 | [mCLObjects->mLocationManager stopUpdatingLocation]; |
michael@0 | 167 | |
michael@0 | 168 | delete mCLObjects; |
michael@0 | 169 | mCLObjects = nullptr; |
michael@0 | 170 | return NS_OK; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | NS_IMETHODIMP |
michael@0 | 174 | CoreLocationLocationProvider::SetHighAccuracy(bool aEnable) |
michael@0 | 175 | { |
michael@0 | 176 | NS_ENSURE_STATE(mCLObjects); |
michael@0 | 177 | |
michael@0 | 178 | mCLObjects->mLocationManager.desiredAccuracy = |
michael@0 | 179 | (aEnable ? kHIGH_ACCURACY : kDEFAULT_ACCURACY); |
michael@0 | 180 | |
michael@0 | 181 | return NS_OK; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | void |
michael@0 | 185 | CoreLocationLocationProvider::Update(nsIDOMGeoPosition* aSomewhere) |
michael@0 | 186 | { |
michael@0 | 187 | if (aSomewhere && mCallback) { |
michael@0 | 188 | mCallback->Update(aSomewhere); |
michael@0 | 189 | } |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | void |
michael@0 | 193 | CoreLocationLocationProvider::NotifyError(uint16_t aErrorCode) |
michael@0 | 194 | { |
michael@0 | 195 | mCallback->NotifyError(aErrorCode); |
michael@0 | 196 | } |