| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "config.h" | 26 #include "config.h" |
| 27 | 27 |
| 28 #include "modules/geolocation/GeolocationController.h" | 28 #include "modules/geolocation/GeolocationController.h" |
| 29 | 29 |
| 30 #include "content/public/common/mojo_geoposition.mojom.h" |
| 31 #include "core/dom/Document.h" |
| 30 #include "core/page/Page.h" | 32 #include "core/page/Page.h" |
| 31 #include "modules/geolocation/GeolocationClient.h" | |
| 32 #include "modules/geolocation/GeolocationError.h" | 33 #include "modules/geolocation/GeolocationError.h" |
| 33 #include "modules/geolocation/GeolocationPosition.h" | 34 #include "modules/geolocation/GeolocationPosition.h" |
| 35 #include "mojo/application/public/cpp/connect.h" |
| 36 #include "mojo/application/public/interfaces/service_provider.mojom.h" |
| 37 #include "platform/UserGestureIndicator.h" |
| 34 | 38 |
| 35 namespace blink { | 39 namespace blink { |
| 36 | 40 |
| 37 GeolocationController::GeolocationController(LocalFrame& frame, GeolocationClien
t* client) | 41 GeolocationController::GeolocationController(Geolocation* geolocation) |
| 38 : PageLifecycleObserver(frame.page()) | 42 : PageLifecycleObserver(geolocation->document()->page()) |
| 39 , m_client(client) | 43 , m_geolocation(geolocation) |
| 40 , m_hasClientForTest(false) | |
| 41 , m_isClientUpdating(false) | |
| 42 { | 44 { |
| 43 if (!frame.isMainFrame() && frame.page()->mainFrame()->isLocalFrame()) { | |
| 44 // internals.setGeolocationClientMock is per page. | |
| 45 GeolocationController* mainController = GeolocationController::from(fram
e.page()->deprecatedLocalMainFrame()); | |
| 46 if (mainController->hasClientForTest()) | |
| 47 setClientForTest(mainController->client()); | |
| 48 } | |
| 49 } | 45 } |
| 50 | 46 |
| 51 void GeolocationController::startUpdatingIfNeeded() | 47 void GeolocationController::startUpdatingIfNeeded() |
| 52 { | 48 { |
| 53 if (m_isClientUpdating) | 49 if (m_geolocationService || !serviceProvider()) |
| 54 return; | 50 return; |
| 55 m_isClientUpdating = true; | 51 mojo::ConnectToService(serviceProvider(), &m_geolocationService); |
| 56 m_client->startUpdating(); | 52 if (m_enableHighAccuracy) |
| 53 m_geolocationService->SetHighAccuracy(true); |
| 54 QueryNextPosition(); |
| 57 } | 55 } |
| 58 | 56 |
| 59 void GeolocationController::stopUpdatingIfNeeded() | 57 void GeolocationController::stopUpdatingIfNeeded() |
| 60 { | 58 { |
| 61 if (!m_isClientUpdating) | 59 m_geolocationService.reset(); |
| 62 return; | |
| 63 m_isClientUpdating = false; | |
| 64 m_client->stopUpdating(); | |
| 65 } | 60 } |
| 66 | 61 |
| 67 GeolocationController::~GeolocationController() | 62 void GeolocationController::setEnableHighAccuracy(bool enableHighAccuracy) |
| 68 { | 63 { |
| 69 ASSERT(m_observers.isEmpty()); | 64 if (m_geolocationService && enableHighAccuracy != m_enableHighAccuracy) { |
| 70 #if !ENABLE(OILPAN) | 65 m_geolocationService->SetHighAccuracy(enableHighAccuracy); |
| 71 if (m_hasClientForTest) { | |
| 72 m_client->controllerForTestRemoved(this); | |
| 73 m_hasClientForTest = false; | |
| 74 } | 66 } |
| 75 #endif | 67 m_enableHighAccuracy = enableHighAccuracy; |
| 76 } | 68 } |
| 77 | 69 |
| 78 PassOwnPtrWillBeRawPtr<GeolocationController> GeolocationController::create(Loca
lFrame& frame, GeolocationClient* client) | 70 void GeolocationController::QueryNextPosition() |
| 79 { | 71 { |
| 80 return adoptPtrWillBeNoop(new GeolocationController(frame, client)); | 72 m_geolocationService->QueryNextPosition( |
| 73 [this](content::MojoGeopositionPtr geoposition) |
| 74 { |
| 75 onQueryNextPositionDone(geoposition.Pass()); |
| 76 }); |
| 81 } | 77 } |
| 82 | 78 |
| 83 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAc
curacy) | 79 void GeolocationController::onQueryNextPositionDone(content::MojoGeopositionPtr
geoposition) |
| 84 { | 80 { |
| 85 // This may be called multiple times with the same observer, though removeOb
server() | 81 auto geolocationService = m_geolocationService.get(); |
| 86 // is called only once with each. | 82 if (geoposition->valid) { |
| 87 bool wasEmpty = m_observers.isEmpty(); | 83 positionChanged(GeolocationPosition::create( |
| 88 m_observers.add(observer); | 84 geoposition->timestamp, |
| 89 if (enableHighAccuracy) | 85 geoposition->latitude, |
| 90 m_highAccuracyObservers.add(observer); | 86 geoposition->longitude, |
| 91 | 87 geoposition->accuracy, |
| 92 if (m_client) { | 88 // Lowest point on land is at approximately -400 meters. |
| 93 if (enableHighAccuracy) | 89 geoposition->altitude > -10000., |
| 94 m_client->setEnableHighAccuracy(true); | 90 geoposition->altitude, |
| 95 if (wasEmpty && page() && page()->visibilityState() == PageVisibilitySta
teVisible) | 91 geoposition->altitude_accuracy >= 0., |
| 96 startUpdatingIfNeeded(); | 92 geoposition->altitude_accuracy, |
| 93 geoposition->heading >= 0. && geoposition->heading <= 360., |
| 94 geoposition->heading, |
| 95 geoposition->speed >= 0., |
| 96 geoposition->speed)); |
| 97 } else { |
| 98 GeolocationError::ErrorCode code; |
| 99 switch (geoposition->error_code) { |
| 100 case content::MojoGeoposition::ERROR_CODE_ERROR_CODE_NONE: |
| 101 return; |
| 102 case content::MojoGeoposition::ERROR_CODE_ERROR_CODE_PERMISSION_DENIED: |
| 103 code = GeolocationError::PermissionDenied; |
| 104 break; |
| 105 case content::MojoGeoposition::ERROR_CODE_ERROR_CODE_POSITION_UNAVAILABL
E: |
| 106 code = GeolocationError::PositionUnavailable; |
| 107 break; |
| 108 case content::MojoGeoposition::ERROR_CODE_ERROR_CODE_TIMEOUT: |
| 109 return; |
| 110 } |
| 111 errorOccurred(GeolocationError::create( |
| 112 code, String::fromUTF8(geoposition->error_message.data(), geopositio
n->error_message.size()))); |
| 97 } | 113 } |
| 114 if (m_geolocationService && m_geolocationService.get() == geolocationService
) |
| 115 QueryNextPosition(); |
| 98 } | 116 } |
| 99 | 117 |
| 100 void GeolocationController::removeObserver(Geolocation* observer) | 118 GeolocationController::~GeolocationController() = default; |
| 119 |
| 120 void GeolocationController::startUpdating(bool enableHighAccuracy) |
| 101 { | 121 { |
| 102 if (!m_observers.contains(observer)) | 122 // This may be called multiple times, though stopUpdating() is called only |
| 103 return; | 123 // once. |
| 104 | 124 m_updating = true; |
| 105 m_observers.remove(observer); | 125 if (enableHighAccuracy) |
| 106 m_highAccuracyObservers.remove(observer); | 126 setEnableHighAccuracy(true); |
| 107 | 127 if (page() && page()->visibilityState() == PageVisibilityStateVisible) |
| 108 if (m_client) { | 128 startUpdatingIfNeeded(); |
| 109 if (m_observers.isEmpty()) | |
| 110 stopUpdatingIfNeeded(); | |
| 111 else if (m_highAccuracyObservers.isEmpty()) | |
| 112 m_client->setEnableHighAccuracy(false); | |
| 113 } | |
| 114 } | 129 } |
| 115 | 130 |
| 116 void GeolocationController::requestPermission(Geolocation* geolocation) | 131 void GeolocationController::stopUpdating() |
| 117 { | 132 { |
| 118 if (m_client) | 133 m_updating = false; |
| 119 m_client->requestPermission(geolocation); | 134 stopUpdatingIfNeeded(); |
| 135 m_enableHighAccuracy = false; |
| 120 } | 136 } |
| 121 | 137 |
| 122 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation) | 138 void GeolocationController::requestPermission() |
| 123 { | 139 { |
| 124 if (m_client) | 140 if (!m_permissionService) { |
| 125 m_client->cancelPermissionRequest(geolocation); | 141 if (!serviceProvider()) { |
| 142 m_geolocation->setIsAllowed(false); |
| 143 return; |
| 144 } |
| 145 mojo::ConnectToService(serviceProvider(), &m_permissionService); |
| 146 } |
| 147 m_permissionService->RequestPermission(content::PERMISSION_NAME_GEOLOCATION, |
| 148 // FIXME: Add a type converter for WTF::String. |
| 149 m_geolocation->executionContext()->securityOrigin()->toString().utf8().d
ata(), |
| 150 UserGestureIndicator::processingUserGesture(), |
| 151 [this](content::PermissionStatus status) |
| 152 { |
| 153 onRequestPermissionDone(status); |
| 154 }); |
| 155 } |
| 156 |
| 157 void GeolocationController::cancelPermissionRequest() |
| 158 { |
| 159 m_permissionService.reset(); |
| 160 } |
| 161 |
| 162 void GeolocationController::onRequestPermissionDone(content::PermissionStatus st
atus) |
| 163 { |
| 164 m_geolocation->setIsAllowed(status == content::PERMISSION_STATUS_GRANTED); |
| 165 m_permissionService.reset(); |
| 126 } | 166 } |
| 127 | 167 |
| 128 void GeolocationController::positionChanged(GeolocationPosition* position) | 168 void GeolocationController::positionChanged(GeolocationPosition* position) |
| 129 { | 169 { |
| 130 if (!position) { | 170 if (!position) { |
| 131 errorOccurred(GeolocationError::create(GeolocationError::PositionUnavail
able, "PositionUnavailable")); | 171 errorOccurred(GeolocationError::create(GeolocationError::PositionUnavail
able, "PositionUnavailable")); |
| 132 return; | 172 return; |
| 133 } | 173 } |
| 134 m_lastPosition = position; | 174 m_lastPosition = position; |
| 135 HeapVector<Member<Geolocation>> observersVector; | 175 m_geolocation->positionChanged(); |
| 136 copyToVector(m_observers, observersVector); | |
| 137 for (size_t i = 0; i < observersVector.size(); ++i) | |
| 138 observersVector[i]->positionChanged(); | |
| 139 } | 176 } |
| 140 | 177 |
| 141 void GeolocationController::errorOccurred(GeolocationError* error) | 178 void GeolocationController::errorOccurred(GeolocationError* error) |
| 142 { | 179 { |
| 143 HeapVector<Member<Geolocation>> observersVector; | 180 m_geolocation->setError(error); |
| 144 copyToVector(m_observers, observersVector); | |
| 145 for (size_t i = 0; i < observersVector.size(); ++i) | |
| 146 observersVector[i]->setError(error); | |
| 147 } | 181 } |
| 148 | 182 |
| 149 GeolocationPosition* GeolocationController::lastPosition() | 183 GeolocationPosition* GeolocationController::lastPosition() |
| 150 { | 184 { |
| 151 if (m_lastPosition.get()) | 185 if (m_lastPosition.get()) |
| 152 return m_lastPosition.get(); | 186 return m_lastPosition.get(); |
| 153 | 187 |
| 154 if (!m_client) | 188 return nullptr; |
| 155 return 0; | |
| 156 | |
| 157 return m_client->lastPosition(); | |
| 158 } | |
| 159 | |
| 160 void GeolocationController::setClientForTest(GeolocationClient* client) | |
| 161 { | |
| 162 if (m_hasClientForTest) | |
| 163 m_client->controllerForTestRemoved(this); | |
| 164 m_client = client; | |
| 165 m_hasClientForTest = true; | |
| 166 | |
| 167 client->controllerForTestAdded(this); | |
| 168 } | 189 } |
| 169 | 190 |
| 170 void GeolocationController::pageVisibilityChanged() | 191 void GeolocationController::pageVisibilityChanged() |
| 171 { | 192 { |
| 172 if (m_observers.isEmpty() || !m_client) | 193 if (page() && page()->visibilityState() == PageVisibilityStateVisible && m_u
pdating) |
| 173 return; | |
| 174 | |
| 175 if (page() && page()->visibilityState() == PageVisibilityStateVisible) | |
| 176 startUpdatingIfNeeded(); | 194 startUpdatingIfNeeded(); |
| 177 else | 195 else |
| 178 stopUpdatingIfNeeded(); | 196 stopUpdatingIfNeeded(); |
| 179 } | 197 } |
| 180 | 198 |
| 181 const char* GeolocationController::supplementName() | 199 mojo::ServiceProvider* GeolocationController::serviceProvider() const |
| 182 { | 200 { |
| 183 return "GeolocationController"; | 201 return m_geolocation->executionContext()->serviceProvider(); |
| 184 } | 202 } |
| 185 | 203 |
| 186 DEFINE_TRACE(GeolocationController) | 204 DEFINE_TRACE(GeolocationController) |
| 187 { | 205 { |
| 188 visitor->trace(m_client); | |
| 189 visitor->trace(m_lastPosition); | 206 visitor->trace(m_lastPosition); |
| 190 visitor->trace(m_observers); | |
| 191 visitor->trace(m_highAccuracyObservers); | |
| 192 WillBeHeapSupplement<LocalFrame>::trace(visitor); | |
| 193 PageLifecycleObserver::trace(visitor); | 207 PageLifecycleObserver::trace(visitor); |
| 194 } | 208 } |
| 195 | 209 |
| 196 void provideGeolocationTo(LocalFrame& frame, GeolocationClient* client) | |
| 197 { | |
| 198 WillBeHeapSupplement<LocalFrame>::provideTo(frame, GeolocationController::su
pplementName(), GeolocationController::create(frame, client)); | |
| 199 } | |
| 200 | |
| 201 } // namespace blink | 210 } // namespace blink |
| OLD | NEW |