| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008, 2009, 2010, 2011 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008, 2009, 2010, 2011 Apple Inc. All Rights Reserved. |
| 3 * Copyright (C) 2009 Torch Mobile, Inc. | 3 * Copyright (C) 2009 Torch Mobile, Inc. |
| 4 * Copyright 2010, The Android Open Source Project | 4 * Copyright 2010, The Android Open Source Project |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 8 * are met: | 8 * are met: |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #include "modules/geolocation/Geolocation.h" | 28 #include "modules/geolocation/Geolocation.h" |
| 29 | 29 |
| 30 #include "core/dom/Document.h" | 30 #include "core/dom/Document.h" |
| 31 #include "core/frame/Deprecation.h" | 31 #include "core/frame/Deprecation.h" |
| 32 #include "core/frame/OriginsUsingFeatures.h" | 32 #include "core/frame/OriginsUsingFeatures.h" |
| 33 #include "core/frame/Settings.h" | 33 #include "core/frame/Settings.h" |
| 34 #include "modules/geolocation/Coordinates.h" | 34 #include "modules/geolocation/Coordinates.h" |
| 35 #include "modules/geolocation/GeolocationController.h" | |
| 36 #include "modules/geolocation/GeolocationError.h" | 35 #include "modules/geolocation/GeolocationError.h" |
| 37 #include "modules/geolocation/GeolocationPosition.h" | 36 #include "platform/mojo/MojoHelper.h" |
| 38 #include "platform/weborigin/SecurityOrigin.h" | 37 #include "public/platform/ServiceRegistry.h" |
| 39 #include "wtf/CurrentTime.h" | 38 #include "wtf/CurrentTime.h" |
| 40 | 39 |
| 41 namespace blink { | 40 namespace blink { |
| 41 namespace { |
| 42 | 42 |
| 43 static const char permissionDeniedErrorMessage[] = "User denied Geolocation"; | 43 static const char permissionDeniedErrorMessage[] = "User denied Geolocation"; |
| 44 static const char failedToStartServiceErrorMessage[] = "Failed to start Geolocat
ion service"; | 44 static const char failedToStartServiceErrorMessage[] = "Failed to start Geolocat
ion service"; |
| 45 static const char framelessDocumentErrorMessage[] = "Geolocation cannot be used
in frameless documents"; | 45 static const char framelessDocumentErrorMessage[] = "Geolocation cannot be used
in frameless documents"; |
| 46 | 46 |
| 47 static Geoposition* createGeoposition(GeolocationPosition* position) | 47 static Geoposition* createGeoposition(const mojom::blink::Geoposition& position) |
| 48 { | 48 { |
| 49 if (!position) | |
| 50 return nullptr; | |
| 51 | |
| 52 Coordinates* coordinates = Coordinates::create( | 49 Coordinates* coordinates = Coordinates::create( |
| 53 position->latitude(), | 50 position.latitude, |
| 54 position->longitude(), | 51 position.longitude, |
| 55 position->canProvideAltitude(), | 52 // Lowest point on land is at approximately -400 meters. |
| 56 position->altitude(), | 53 position.altitude > -10000., |
| 57 position->accuracy(), | 54 position.altitude, |
| 58 position->canProvideAltitudeAccuracy(), | 55 position.accuracy, |
| 59 position->altitudeAccuracy(), | 56 position.altitude_accuracy >= 0., |
| 60 position->canProvideHeading(), | 57 position.altitude_accuracy, |
| 61 position->heading(), | 58 position.heading >= 0. && position.heading <= 360., |
| 62 position->canProvideSpeed(), | 59 position.heading, |
| 63 position->speed()); | 60 position.speed >= 0., |
| 64 return Geoposition::create(coordinates, convertSecondsToDOMTimeStamp(positio
n->timestamp())); | 61 position.speed); |
| 62 return Geoposition::create(coordinates, convertSecondsToDOMTimeStamp(positio
n.timestamp)); |
| 65 } | 63 } |
| 66 | 64 |
| 67 static PositionError* createPositionError(GeolocationError* error) | 65 static PositionError* createPositionError(mojom::blink::Geoposition::ErrorCode m
ojomErrorCode, const String& error) |
| 68 { | 66 { |
| 69 PositionError::ErrorCode code = PositionError::POSITION_UNAVAILABLE; | 67 PositionError::ErrorCode errorCode = PositionError::POSITION_UNAVAILABLE; |
| 70 switch (error->code()) { | 68 switch (mojomErrorCode) { |
| 71 case GeolocationError::PermissionDenied: | 69 case mojom::blink::Geoposition::ErrorCode::PERMISSION_DENIED: |
| 72 code = PositionError::PERMISSION_DENIED; | 70 errorCode = PositionError::PERMISSION_DENIED; |
| 73 break; | 71 break; |
| 74 case GeolocationError::PositionUnavailable: | 72 case mojom::blink::Geoposition::ErrorCode::POSITION_UNAVAILABLE: |
| 75 code = PositionError::POSITION_UNAVAILABLE; | 73 errorCode = PositionError::POSITION_UNAVAILABLE; |
| 74 break; |
| 75 case mojom::blink::Geoposition::ErrorCode::NONE: |
| 76 case mojom::blink::Geoposition::ErrorCode::TIMEOUT: |
| 77 NOTREACHED(); |
| 76 break; | 78 break; |
| 77 } | 79 } |
| 80 return PositionError::create(errorCode, error); |
| 81 } |
| 78 | 82 |
| 79 return PositionError::create(code, error->message()); | 83 } // namespace |
| 80 } | |
| 81 | 84 |
| 82 Geolocation* Geolocation::create(ExecutionContext* context) | 85 Geolocation* Geolocation::create(ExecutionContext* context) |
| 83 { | 86 { |
| 84 Geolocation* geolocation = new Geolocation(context); | 87 Geolocation* geolocation = new Geolocation(context); |
| 85 geolocation->suspendIfNeeded(); | |
| 86 return geolocation; | 88 return geolocation; |
| 87 } | 89 } |
| 88 | 90 |
| 89 Geolocation::Geolocation(ExecutionContext* context) | 91 Geolocation::Geolocation(ExecutionContext* context) |
| 90 : ActiveDOMObject(context) | 92 : ContextLifecycleObserver(context) |
| 93 , PageLifecycleObserver(document()->page()) |
| 91 , m_geolocationPermission(PermissionUnknown) | 94 , m_geolocationPermission(PermissionUnknown) |
| 92 { | 95 { |
| 93 } | 96 } |
| 94 | 97 |
| 95 Geolocation::~Geolocation() | 98 Geolocation::~Geolocation() |
| 96 { | 99 { |
| 97 ASSERT(m_geolocationPermission != PermissionRequested); | 100 ASSERT(m_geolocationPermission != PermissionRequested); |
| 98 } | 101 } |
| 99 | 102 |
| 100 DEFINE_TRACE(Geolocation) | 103 DEFINE_TRACE(Geolocation) |
| 101 { | 104 { |
| 102 visitor->trace(m_oneShots); | 105 visitor->trace(m_oneShots); |
| 103 visitor->trace(m_watchers); | 106 visitor->trace(m_watchers); |
| 104 visitor->trace(m_pendingForPermissionNotifiers); | 107 visitor->trace(m_pendingForPermissionNotifiers); |
| 105 visitor->trace(m_lastPosition); | 108 visitor->trace(m_lastPosition); |
| 106 visitor->trace(m_requestsAwaitingCachedPosition); | 109 ContextLifecycleObserver::trace(visitor); |
| 107 ActiveDOMObject::trace(visitor); | 110 PageLifecycleObserver::trace(visitor); |
| 108 } | 111 } |
| 109 | 112 |
| 110 Document* Geolocation::document() const | 113 Document* Geolocation::document() const |
| 111 { | 114 { |
| 112 return toDocument(getExecutionContext()); | 115 return toDocument(getExecutionContext()); |
| 113 } | 116 } |
| 114 | 117 |
| 115 LocalFrame* Geolocation::frame() const | 118 LocalFrame* Geolocation::frame() const |
| 116 { | 119 { |
| 117 return document() ? document()->frame() : 0; | 120 return document() ? document()->frame() : 0; |
| 118 } | 121 } |
| 119 | 122 |
| 120 void Geolocation::stop() | 123 void Geolocation::contextDestroyed() |
| 121 { | 124 { |
| 122 LocalFrame* frame = this->frame(); | 125 m_permissionService.reset(); |
| 123 if (frame && m_geolocationPermission == PermissionRequested) | |
| 124 GeolocationController::from(frame)->cancelPermissionRequest(this); | |
| 125 | |
| 126 // The frame may be moving to a new page and we want to get the permissions
from the new page's client. | |
| 127 m_geolocationPermission = PermissionUnknown; | |
| 128 cancelAllRequests(); | 126 cancelAllRequests(); |
| 129 stopUpdating(); | 127 stopUpdating(); |
| 128 m_geolocationPermission = PermissionDenied; |
| 130 m_pendingForPermissionNotifiers.clear(); | 129 m_pendingForPermissionNotifiers.clear(); |
| 131 } | 130 m_lastPosition = nullptr; |
| 132 | 131 ContextLifecycleObserver::clearContext(); |
| 133 Geoposition* Geolocation::lastPosition() | 132 PageLifecycleObserver::clearContext(); |
| 134 { | |
| 135 LocalFrame* frame = this->frame(); | |
| 136 if (!frame) | |
| 137 return 0; | |
| 138 | |
| 139 m_lastPosition = createGeoposition(GeolocationController::from(frame)->lastP
osition()); | |
| 140 | |
| 141 return m_lastPosition; | |
| 142 } | 133 } |
| 143 | 134 |
| 144 void Geolocation::recordOriginTypeAccess() const | 135 void Geolocation::recordOriginTypeAccess() const |
| 145 { | 136 { |
| 146 ASSERT(frame()); | 137 ASSERT(frame()); |
| 147 | 138 |
| 148 Document* document = this->document(); | 139 Document* document = this->document(); |
| 149 ASSERT(document); | 140 ASSERT(document); |
| 150 | 141 |
| 151 // It is required by isSecureContext() but isn't | 142 // It is required by isSecureContext() but isn't |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 if (isDenied()) | 202 if (isDenied()) |
| 212 notifier->setFatalError(PositionError::create(PositionError::PERMISSION_
DENIED, permissionDeniedErrorMessage)); | 203 notifier->setFatalError(PositionError::create(PositionError::PERMISSION_
DENIED, permissionDeniedErrorMessage)); |
| 213 else if (haveSuitableCachedPosition(notifier->options())) | 204 else if (haveSuitableCachedPosition(notifier->options())) |
| 214 notifier->setUseCachedPosition(); | 205 notifier->setUseCachedPosition(); |
| 215 else if (!notifier->options().timeout()) | 206 else if (!notifier->options().timeout()) |
| 216 notifier->startTimer(); | 207 notifier->startTimer(); |
| 217 else if (!isAllowed()) { | 208 else if (!isAllowed()) { |
| 218 // if we don't yet have permission, request for permission before callin
g startUpdating() | 209 // if we don't yet have permission, request for permission before callin
g startUpdating() |
| 219 m_pendingForPermissionNotifiers.add(notifier); | 210 m_pendingForPermissionNotifiers.add(notifier); |
| 220 requestPermission(); | 211 requestPermission(); |
| 221 } else if (startUpdating(notifier)) | 212 } else { |
| 213 startUpdating(notifier); |
| 222 notifier->startTimer(); | 214 notifier->startTimer(); |
| 223 else | 215 } |
| 224 notifier->setFatalError(PositionError::create(PositionError::POSITION_UN
AVAILABLE, failedToStartServiceErrorMessage)); | |
| 225 } | 216 } |
| 226 | 217 |
| 227 void Geolocation::fatalErrorOccurred(GeoNotifier* notifier) | 218 void Geolocation::fatalErrorOccurred(GeoNotifier* notifier) |
| 228 { | 219 { |
| 229 // This request has failed fatally. Remove it from our lists. | 220 // This request has failed fatally. Remove it from our lists. |
| 230 m_oneShots.remove(notifier); | 221 m_oneShots.remove(notifier); |
| 231 m_watchers.remove(notifier); | 222 m_watchers.remove(notifier); |
| 232 | 223 |
| 233 if (!hasListeners()) | 224 if (!hasListeners()) |
| 234 stopUpdating(); | 225 stopUpdating(); |
| 235 } | 226 } |
| 236 | 227 |
| 237 void Geolocation::requestUsesCachedPosition(GeoNotifier* notifier) | 228 void Geolocation::requestUsesCachedPosition(GeoNotifier* notifier) |
| 238 { | 229 { |
| 239 // This is called asynchronously, so the permissions could have been denied | 230 DCHECK(isAllowed()); |
| 240 // since we last checked in startRequest. | 231 |
| 241 if (isDenied()) { | 232 notifier->runSuccessCallback(m_lastPosition); |
| 242 notifier->setFatalError(PositionError::create(PositionError::PERMISSION_
DENIED, permissionDeniedErrorMessage)); | 233 |
| 243 return; | 234 // If this is a one-shot request, stop it. Otherwise, if the watch still |
| 235 // exists, start the service to get updates. |
| 236 if (m_oneShots.contains(notifier)) { |
| 237 m_oneShots.remove(notifier); |
| 238 } else if (m_watchers.contains(notifier)) { |
| 239 if (notifier->options().timeout()) |
| 240 startUpdating(notifier); |
| 241 notifier->startTimer(); |
| 244 } | 242 } |
| 245 | 243 |
| 246 m_requestsAwaitingCachedPosition.add(notifier); | |
| 247 | |
| 248 // If permissions are allowed, make the callback | |
| 249 if (isAllowed()) { | |
| 250 makeCachedPositionCallbacks(); | |
| 251 return; | |
| 252 } | |
| 253 | |
| 254 // Request permissions, which may be synchronous or asynchronous. | |
| 255 requestPermission(); | |
| 256 } | |
| 257 | |
| 258 void Geolocation::makeCachedPositionCallbacks() | |
| 259 { | |
| 260 // All modifications to m_requestsAwaitingCachedPosition are done | |
| 261 // asynchronously, so we don't need to worry about it being modified from | |
| 262 // the callbacks. | |
| 263 for (GeoNotifier* notifier : m_requestsAwaitingCachedPosition) { | |
| 264 notifier->runSuccessCallback(lastPosition()); | |
| 265 | |
| 266 // If this is a one-shot request, stop it. Otherwise, if the watch still | |
| 267 // exists, start the service to get updates. | |
| 268 if (m_oneShots.contains(notifier)) | |
| 269 m_oneShots.remove(notifier); | |
| 270 else if (m_watchers.contains(notifier)) { | |
| 271 if (!notifier->options().timeout() || startUpdating(notifier)) | |
| 272 notifier->startTimer(); | |
| 273 else | |
| 274 notifier->setFatalError(PositionError::create(PositionError::POS
ITION_UNAVAILABLE, failedToStartServiceErrorMessage)); | |
| 275 } | |
| 276 } | |
| 277 | |
| 278 m_requestsAwaitingCachedPosition.clear(); | |
| 279 | |
| 280 if (!hasListeners()) | 244 if (!hasListeners()) |
| 281 stopUpdating(); | 245 stopUpdating(); |
| 282 } | 246 } |
| 283 | 247 |
| 284 void Geolocation::requestTimedOut(GeoNotifier* notifier) | 248 void Geolocation::requestTimedOut(GeoNotifier* notifier) |
| 285 { | 249 { |
| 286 // If this is a one-shot request, stop it. | 250 // If this is a one-shot request, stop it. |
| 287 m_oneShots.remove(notifier); | 251 m_oneShots.remove(notifier); |
| 288 | 252 |
| 289 if (!hasListeners()) | 253 if (!hasListeners()) |
| 290 stopUpdating(); | 254 stopUpdating(); |
| 291 } | 255 } |
| 292 | 256 |
| 293 bool Geolocation::haveSuitableCachedPosition(const PositionOptions& options) | 257 bool Geolocation::haveSuitableCachedPosition(const PositionOptions& options) |
| 294 { | 258 { |
| 295 Geoposition* cachedPosition = lastPosition(); | 259 if (!m_lastPosition) |
| 296 if (!cachedPosition) | |
| 297 return false; | 260 return false; |
| 261 DCHECK(isAllowed()); |
| 298 if (!options.maximumAge()) | 262 if (!options.maximumAge()) |
| 299 return false; | 263 return false; |
| 300 DOMTimeStamp currentTimeMillis = convertSecondsToDOMTimeStamp(currentTime())
; | 264 DOMTimeStamp currentTimeMillis = convertSecondsToDOMTimeStamp(currentTime())
; |
| 301 return cachedPosition->timestamp() > currentTimeMillis - options.maximumAge(
); | 265 return m_lastPosition->timestamp() > currentTimeMillis - options.maximumAge(
); |
| 302 } | 266 } |
| 303 | 267 |
| 304 void Geolocation::clearWatch(int watchID) | 268 void Geolocation::clearWatch(int watchID) |
| 305 { | 269 { |
| 306 if (watchID <= 0) | 270 if (watchID <= 0) |
| 307 return; | 271 return; |
| 308 | 272 |
| 309 if (GeoNotifier* notifier = m_watchers.find(watchID)) | 273 if (GeoNotifier* notifier = m_watchers.find(watchID)) |
| 310 m_pendingForPermissionNotifiers.remove(notifier); | 274 m_pendingForPermissionNotifiers.remove(notifier); |
| 311 m_watchers.remove(watchID); | 275 m_watchers.remove(watchID); |
| 312 | 276 |
| 313 if (!hasListeners()) | 277 if (!hasListeners()) |
| 314 stopUpdating(); | 278 stopUpdating(); |
| 315 } | 279 } |
| 316 | 280 |
| 317 void Geolocation::setIsAllowed(bool allowed) | 281 void Geolocation::onGeolocationPermissionUpdated(mojom::blink::PermissionStatus
status) |
| 318 { | 282 { |
| 319 // This may be due to either a new position from the service, or a cached po
sition. | 283 // This may be due to either a new position from the service, or a cached po
sition. |
| 320 m_geolocationPermission = allowed ? PermissionAllowed : PermissionDenied; | 284 m_geolocationPermission = status == mojom::blink::PermissionStatus::GRANTED
? PermissionAllowed : PermissionDenied; |
| 285 m_permissionService.reset(); |
| 321 | 286 |
| 322 // Permission request was made during the startRequest process | 287 // While we iterate through the list, we need not worry about the list being
modified as the permission |
| 323 if (!m_pendingForPermissionNotifiers.isEmpty()) { | 288 // is already set to Yes/No and no new listeners will be added to the pendin
g list. |
| 324 handlePendingPermissionNotifiers(); | 289 for (GeoNotifier* notifier : m_pendingForPermissionNotifiers) { |
| 325 m_pendingForPermissionNotifiers.clear(); | 290 if (isAllowed()) { |
| 326 return; | 291 // Start all pending notification requests as permission granted. |
| 292 // The notifier is always ref'ed by m_oneShots or m_watchers. |
| 293 startUpdating(notifier); |
| 294 notifier->startTimer(); |
| 295 } else { |
| 296 notifier->setFatalError(PositionError::create(PositionError::PERMISS
ION_DENIED, permissionDeniedErrorMessage)); |
| 297 } |
| 327 } | 298 } |
| 328 | 299 m_pendingForPermissionNotifiers.clear(); |
| 329 if (!isAllowed()) { | |
| 330 PositionError* error = PositionError::create(PositionError::PERMISSION_D
ENIED, permissionDeniedErrorMessage); | |
| 331 error->setIsFatal(true); | |
| 332 handleError(error); | |
| 333 m_requestsAwaitingCachedPosition.clear(); | |
| 334 return; | |
| 335 } | |
| 336 | |
| 337 // If the service has a last position, use it to call back for all requests. | |
| 338 // If any of the requests are waiting for permission for a cached position, | |
| 339 // the position from the service will be at least as fresh. | |
| 340 if (lastPosition()) | |
| 341 makeSuccessCallbacks(); | |
| 342 else | |
| 343 makeCachedPositionCallbacks(); | |
| 344 } | 300 } |
| 345 | 301 |
| 346 void Geolocation::sendError(GeoNotifierVector& notifiers, PositionError* error) | 302 void Geolocation::sendError(GeoNotifierVector& notifiers, PositionError* error) |
| 347 { | 303 { |
| 348 for (GeoNotifier* notifier : notifiers) | 304 for (GeoNotifier* notifier : notifiers) |
| 349 notifier->runErrorCallback(error); | 305 notifier->runErrorCallback(error); |
| 350 } | 306 } |
| 351 | 307 |
| 352 void Geolocation::sendPosition(GeoNotifierVector& notifiers, Geoposition* positi
on) | 308 void Geolocation::sendPosition(GeoNotifierVector& notifiers, Geoposition* positi
on) |
| 353 { | 309 { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 void Geolocation::requestPermission() | 412 void Geolocation::requestPermission() |
| 457 { | 413 { |
| 458 if (m_geolocationPermission != PermissionUnknown) | 414 if (m_geolocationPermission != PermissionUnknown) |
| 459 return; | 415 return; |
| 460 | 416 |
| 461 LocalFrame* frame = this->frame(); | 417 LocalFrame* frame = this->frame(); |
| 462 if (!frame) | 418 if (!frame) |
| 463 return; | 419 return; |
| 464 | 420 |
| 465 m_geolocationPermission = PermissionRequested; | 421 m_geolocationPermission = PermissionRequested; |
| 422 frame->serviceRegistry()->connectToRemoteService( |
| 423 mojo::GetProxy(&m_permissionService)); |
| 424 m_permissionService.set_connection_error_handler(sameThreadBindForMojo(&Geol
ocation::onPermissionConnectionError, this)); |
| 466 | 425 |
| 467 // Ask the embedder: it maintains the geolocation challenge policy itself. | 426 // Ask the embedder: it maintains the geolocation challenge policy itself. |
| 468 GeolocationController::from(frame)->requestPermission(this); | 427 m_permissionService->RequestPermission( |
| 428 mojom::blink::PermissionName::GEOLOCATION, |
| 429 getExecutionContext()->getSecurityOrigin()->toString(), |
| 430 sameThreadBindForMojo(&Geolocation::onGeolocationPermissionUpdated, this
)); |
| 469 } | 431 } |
| 470 | 432 |
| 471 void Geolocation::makeSuccessCallbacks() | 433 void Geolocation::makeSuccessCallbacks() |
| 472 { | 434 { |
| 473 ASSERT(lastPosition()); | 435 ASSERT(m_lastPosition); |
| 474 ASSERT(isAllowed()); | 436 ASSERT(isAllowed()); |
| 475 | 437 |
| 476 GeoNotifierVector oneShotsCopy; | 438 GeoNotifierVector oneShotsCopy; |
| 477 copyToVector(m_oneShots, oneShotsCopy); | 439 copyToVector(m_oneShots, oneShotsCopy); |
| 478 | 440 |
| 479 GeoNotifierVector watchersCopy; | 441 GeoNotifierVector watchersCopy; |
| 480 m_watchers.getNotifiersVector(watchersCopy); | 442 m_watchers.getNotifiersVector(watchersCopy); |
| 481 | 443 |
| 482 // Clear the lists before we make the callbacks, to avoid clearing notifiers | 444 // Clear the lists before we make the callbacks, to avoid clearing notifiers |
| 483 // added by calls to Geolocation methods from the callbacks, and to prevent | 445 // added by calls to Geolocation methods from the callbacks, and to prevent |
| 484 // further callbacks to these notifiers. | 446 // further callbacks to these notifiers. |
| 485 m_oneShots.clear(); | 447 m_oneShots.clear(); |
| 486 | 448 |
| 487 // Also clear the set of notifiers waiting for a cached position. All the | 449 sendPosition(oneShotsCopy, m_lastPosition); |
| 488 // oneshots and watchers will receive a position now, and if they happen to | 450 sendPosition(watchersCopy, m_lastPosition); |
| 489 // be lingering in that set, avoid this bug: http://crbug.com/311876 . | |
| 490 m_requestsAwaitingCachedPosition.clear(); | |
| 491 | |
| 492 sendPosition(oneShotsCopy, lastPosition()); | |
| 493 sendPosition(watchersCopy, lastPosition()); | |
| 494 | 451 |
| 495 if (!hasListeners()) | 452 if (!hasListeners()) |
| 496 stopUpdating(); | 453 stopUpdating(); |
| 497 } | 454 } |
| 498 | 455 |
| 499 void Geolocation::positionChanged() | 456 void Geolocation::positionChanged() |
| 500 { | 457 { |
| 501 ASSERT(isAllowed()); | 458 ASSERT(isAllowed()); |
| 502 | 459 |
| 503 // Stop all currently running timers. | 460 // Stop all currently running timers. |
| 504 stopTimers(); | 461 stopTimers(); |
| 505 | 462 |
| 506 makeSuccessCallbacks(); | 463 makeSuccessCallbacks(); |
| 507 } | 464 } |
| 508 | 465 |
| 509 void Geolocation::setError(GeolocationError* error) | 466 void Geolocation::startUpdating(GeoNotifier* notifier) |
| 510 { | 467 { |
| 511 handleError(createPositionError(error)); | 468 m_updating = true; |
| 512 } | 469 if (notifier->options().enableHighAccuracy() && !m_enableHighAccuracy) { |
| 513 | 470 m_enableHighAccuracy = true; |
| 514 bool Geolocation::startUpdating(GeoNotifier* notifier) | 471 if (m_geolocationService) |
| 515 { | 472 m_geolocationService->SetHighAccuracy(true); |
| 516 LocalFrame* frame = this->frame(); | 473 } |
| 517 if (!frame) | 474 updateGeolocationServiceConnection(); |
| 518 return false; | |
| 519 | |
| 520 GeolocationController::from(frame)->addObserver(this, notifier->options().en
ableHighAccuracy()); | |
| 521 return true; | |
| 522 } | 475 } |
| 523 | 476 |
| 524 void Geolocation::stopUpdating() | 477 void Geolocation::stopUpdating() |
| 525 { | 478 { |
| 526 LocalFrame* frame = this->frame(); | 479 m_updating = false; |
| 527 if (!frame) | 480 updateGeolocationServiceConnection(); |
| 481 m_enableHighAccuracy = false; |
| 482 } |
| 483 |
| 484 void Geolocation::updateGeolocationServiceConnection() |
| 485 { |
| 486 if (!getExecutionContext() || !page() || !page()->isPageVisible() || !m_upda
ting) { |
| 487 m_geolocationService.reset(); |
| 488 m_disconnectedGeolocationService = true; |
| 489 return; |
| 490 } |
| 491 if (m_geolocationService) |
| 528 return; | 492 return; |
| 529 | 493 |
| 530 GeolocationController::from(frame)->removeObserver(this); | 494 frame()->serviceRegistry()->connectToRemoteService(mojo::GetProxy(&m_geoloca
tionService)); |
| 495 m_geolocationService.set_connection_error_handler(sameThreadBindForMojo(&Geo
location::onGeolocationConnectionError, this)); |
| 496 if (m_enableHighAccuracy) |
| 497 m_geolocationService->SetHighAccuracy(true); |
| 498 queryNextPosition(); |
| 531 } | 499 } |
| 532 | 500 |
| 533 void Geolocation::handlePendingPermissionNotifiers() | 501 void Geolocation::queryNextPosition() |
| 534 { | 502 { |
| 535 // While we iterate through the list, we need not worry about list being mod
ified as the permission | 503 m_geolocationService->QueryNextPosition( |
| 536 // is already set to Yes/No and no new listeners will be added to the pendin
g list. | 504 sameThreadBindForMojo(&Geolocation::onPositionUpdated, this)); |
| 537 for (GeoNotifier* notifier : m_pendingForPermissionNotifiers) { | 505 } |
| 538 if (isAllowed()) { | 506 |
| 539 // start all pending notification requests as permission granted. | 507 void Geolocation::onPositionUpdated(mojom::blink::GeopositionPtr position) |
| 540 // The notifier is always ref'ed by m_oneShots or m_watchers. | 508 { |
| 541 if (startUpdating(notifier)) | 509 m_disconnectedGeolocationService = false; |
| 542 notifier->startTimer(); | 510 if (position->valid) { |
| 543 else | 511 m_lastPosition = createGeoposition(*position); |
| 544 notifier->setFatalError(PositionError::create(PositionError::POS
ITION_UNAVAILABLE, failedToStartServiceErrorMessage)); | 512 positionChanged(); |
| 545 } else { | 513 } else { |
| 546 notifier->setFatalError(PositionError::create(PositionError::PERMISS
ION_DENIED, permissionDeniedErrorMessage)); | 514 handleError(createPositionError(position->error_code, position->error_me
ssage)); |
| 547 } | |
| 548 } | 515 } |
| 516 if (!m_disconnectedGeolocationService) |
| 517 queryNextPosition(); |
| 518 } |
| 519 |
| 520 void Geolocation::pageVisibilityChanged() |
| 521 { |
| 522 updateGeolocationServiceConnection(); |
| 523 } |
| 524 |
| 525 void Geolocation::onGeolocationConnectionError() |
| 526 { |
| 527 PositionError* error = PositionError::create(PositionError::POSITION_UNAVAIL
ABLE, failedToStartServiceErrorMessage); |
| 528 error->setIsFatal(true); |
| 529 handleError(error); |
| 530 } |
| 531 |
| 532 void Geolocation::onPermissionConnectionError() |
| 533 { |
| 534 onGeolocationPermissionUpdated(mojom::blink::PermissionStatus::DENIED); |
| 549 } | 535 } |
| 550 | 536 |
| 551 } // namespace blink | 537 } // namespace blink |
| OLD | NEW |