Chromium Code Reviews| Index: third_party/WebKit/Source/modules/geolocation/testing/GeolocationClientMock.cpp |
| diff --git a/third_party/WebKit/Source/modules/geolocation/testing/GeolocationClientMock.cpp b/third_party/WebKit/Source/modules/geolocation/testing/GeolocationClientMock.cpp |
| index 5e7e0d9b8f0177e895fbefe3c9ec2a9cafaa3a7d..a6d4f124299669f5343d8042d1517cd14d074da9 100644 |
| --- a/third_party/WebKit/Source/modules/geolocation/testing/GeolocationClientMock.cpp |
| +++ b/third_party/WebKit/Source/modules/geolocation/testing/GeolocationClientMock.cpp |
| @@ -32,45 +32,67 @@ |
| #include "config.h" |
| #include "GeolocationClientMock.h" |
| +#include "core/dom/Document.h" |
| #include "modules/geolocation/GeolocationController.h" |
| #include "modules/geolocation/GeolocationError.h" |
| #include "modules/geolocation/GeolocationPosition.h" |
| namespace blink { |
| -GeolocationClientMock::GeolocationClientMock() |
| - : m_hasError(false) |
| - , m_controllerTimer(this, &GeolocationClientMock::controllerTimerFired) |
| - , m_permissionTimer(this, &GeolocationClientMock::permissionTimerFired) |
| - , m_isActive(false) |
| - , m_permissionState(PermissionStateUnset) |
| +GeolocationClientMock* GeolocationClientMock::from(Document* document) |
| { |
| + GeolocationClientMock* supplement = static_cast<GeolocationClientMock*>(WillBeHeapSupplement<Document>::from(*document, supplementName())); |
| + if (!supplement) { |
| + supplement = new GeolocationClientMock(document); |
| + WillBeHeapSupplement<Document>::provideTo(*document, supplementName(), adoptPtrWillBeNoop(supplement)); |
| + document->setServiceProvider(supplement); |
|
Marijn Kruisselbrink
2015/09/30 19:56:55
The constructor of GeolocationClientMock would hav
Sam McNally
2015/10/01 01:54:50
Done.
|
| + } |
| + return supplement; |
| +} |
| + |
| +GeolocationClientMock::GeolocationClientMock(ExecutionContext* context) |
| + : m_context(context) |
| + , m_permissionState(permission::STATUS_ASK) |
| + , m_geolocationBinding(this) |
| + , m_permissionBinding(this) |
| +{ |
| + m_context->setServiceProvider(this); |
| } |
| GeolocationClientMock::~GeolocationClientMock() |
| { |
| - ASSERT(!m_isActive); |
| + if (m_context->serviceProvider() == this) |
| + m_context->setServiceProvider(nullptr); |
| } |
| void GeolocationClientMock::setPosition(GeolocationPosition* position) |
| { |
| - m_lastPosition = position; |
| - clearError(); |
| - asyncUpdateController(); |
| + m_lastPosition = geolocation::Geoposition::New(); |
| + m_lastPosition->latitude = position->latitude(); |
| + m_lastPosition->longitude = position->longitude(); |
| + m_lastPosition->altitude = position->canProvideAltitude() ? position->altitude() : -10000.; |
| + m_lastPosition->accuracy = position->accuracy(); |
| + m_lastPosition->altitude_accuracy = position->canProvideAltitudeAccuracy() ? position->altitudeAccuracy() : -1; |
| + m_lastPosition->heading = position->canProvideHeading() ? position->heading() : -1; |
| + m_lastPosition->speed = position->canProvideSpeed() ? position->speed() : -1; |
| + m_lastPosition->timestamp = position->timestamp(); |
| + m_lastPosition->error_message = ""; |
| + m_lastPosition->valid = true; |
| + runGeolocationCallbacks(); |
| } |
| void GeolocationClientMock::setPositionUnavailableError(const String& errorMessage) |
| { |
| - m_hasError = true; |
| - m_errorMessage = errorMessage; |
| - m_lastPosition = nullptr; |
| - asyncUpdateController(); |
| + m_lastPosition = geolocation::Geoposition::New(); |
| + m_lastPosition->error_code = geolocation::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
| + m_lastPosition->error_message = errorMessage.utf8().data(); |
| + runGeolocationCallbacks(); |
| } |
| void GeolocationClientMock::setPermission(bool allowed) |
| { |
| - m_permissionState = allowed ? PermissionStateAllowed : PermissionStateDenied; |
| - asyncUpdatePermission(); |
| + m_permissionState = allowed ? permission::STATUS_GRANTED : permission::STATUS_DENIED; |
| + runPermissionCallbacks(); |
| } |
| int GeolocationClientMock::numberOfPendingPermissionRequests() const |
| @@ -78,115 +100,90 @@ int GeolocationClientMock::numberOfPendingPermissionRequests() const |
| return m_pendingPermissions.size(); |
| } |
| -void GeolocationClientMock::requestPermission(Geolocation* geolocation) |
| +void GeolocationClientMock::SetHighAccuracy(bool highAccuracy) |
| { |
| - m_pendingPermissions.add(geolocation); |
| - if (m_permissionState != PermissionStateUnset) |
| - asyncUpdatePermission(); |
| -} |
| - |
| -void GeolocationClientMock::cancelPermissionRequest(Geolocation* geolocation) |
| -{ |
| - // Called from Geolocation::disconnectFrame() in response to LocalFrame destruction. |
| - m_pendingPermissions.remove(geolocation); |
| - if (m_pendingPermissions.isEmpty() && m_permissionTimer.isActive()) |
| - m_permissionTimer.stop(); |
| + // FIXME: We need to add some tests regarding "high accuracy" mode. |
| + // See https://bugs.webkit.org/show_bug.cgi?id=49438 |
| } |
| -void GeolocationClientMock::controllerForTestAdded(GeolocationController* controller) |
| +void GeolocationClientMock::QueryNextPosition(const QueryNextPositionCallback& callback) |
| { |
| - m_controllers.add(controller); |
| + m_pendingGeolocations.append(callback); |
| + runGeolocationCallbacks(); |
| } |
| -void GeolocationClientMock::controllerForTestRemoved(GeolocationController* controller) |
| +void GeolocationClientMock::HasPermission(permission::Name permission, |
| + const mojo::String& origin, |
| + const HasPermissionCallback& callback) |
| { |
| - m_controllers.remove(controller); |
| + callback.Run(permission::STATUS_ASK); |
| } |
| -void GeolocationClientMock::asyncUpdatePermission() |
| +void GeolocationClientMock::RequestPermission(permission::Name permission, |
| + const mojo::String& origin, |
| + bool userGesture, |
| + const RequestPermissionCallback& callback) |
| { |
| - ASSERT(m_permissionState != PermissionStateUnset); |
| - if (!m_permissionTimer.isActive()) |
| - m_permissionTimer.startOneShot(0, FROM_HERE); |
| + m_pendingPermissions.append(callback); |
| + runPermissionCallbacks(); |
| } |
| -void GeolocationClientMock::permissionTimerFired(Timer<GeolocationClientMock>* timer) |
| +void GeolocationClientMock::RequestPermissions(mojo::Array<permission::Name> permissions, |
| + const mojo::String& origin, |
| + bool userGesture, |
| + const RequestPermissionsCallback& callback) |
| { |
| - ASSERT_UNUSED(timer, timer == &m_permissionTimer); |
| - ASSERT(m_permissionState != PermissionStateUnset); |
| - bool allowed = m_permissionState == PermissionStateAllowed; |
| - GeolocationSet::iterator end = m_pendingPermissions.end(); |
| - |
| - // Once permission has been set (or denied) on a Geolocation object, there can be |
| - // no further requests for permission to the mock. Consequently the callbacks |
| - // which fire synchronously from Geolocation::setIsAllowed() cannot reentrantly modify |
| - // m_pendingPermissions. |
| - for (GeolocationSet::iterator it = m_pendingPermissions.begin(); it != end; ++it) |
| - (*it)->setIsAllowed(allowed); |
| - m_pendingPermissions.clear(); |
| } |
| -void GeolocationClientMock::startUpdating() |
| +void GeolocationClientMock::RevokePermission(permission::Name permission, |
| + const mojo::String& origin, |
| + const RevokePermissionCallback& callback) |
| { |
| - ASSERT(!m_isActive); |
| - m_isActive = true; |
| - asyncUpdateController(); |
| } |
| -void GeolocationClientMock::stopUpdating() |
| +void GeolocationClientMock::GetNextPermissionChange( |
| + permission::Name permission, |
| + const mojo::String& origin, |
| + permission::Status lastKnownStatus, |
| + const GetNextPermissionChangeCallback& callback) |
| { |
| - ASSERT(m_isActive); |
| - m_isActive = false; |
| - m_controllerTimer.stop(); |
| } |
| -void GeolocationClientMock::setEnableHighAccuracy(bool) |
| +void GeolocationClientMock::ConnectToService(const mojo::String& interfaceName, mojo::ScopedMessagePipeHandle pipe) |
| { |
|
Marijn Kruisselbrink
2015/09/30 19:56:55
While there is only one feature that uses mojo fro
Sam McNally
2015/10/01 01:54:50
This is a bit of a quick and nasty ServiceProvider
|
| - // FIXME: We need to add some tests regarding "high accuracy" mode. |
| - // See https://bugs.webkit.org/show_bug.cgi?id=49438 |
| + if (interfaceName == geolocation::GeolocationService::Name_) |
| + m_geolocationBinding.Bind(pipe.Pass()); |
| + else if (interfaceName == permission::PermissionService::Name_) |
| + m_permissionBinding.Bind(pipe.Pass()); |
| } |
| -GeolocationPosition* GeolocationClientMock::lastPosition() |
| +void GeolocationClientMock::runGeolocationCallbacks() |
| { |
| - return m_lastPosition.get(); |
| -} |
| + if (m_pendingGeolocations.isEmpty() || !m_lastPosition) |
| + return; |
| -void GeolocationClientMock::asyncUpdateController() |
| -{ |
| - if (m_isActive && !m_controllerTimer.isActive()) |
| - m_controllerTimer.startOneShot(0, FROM_HERE); |
| + for (const auto& callback : m_pendingGeolocations) |
| + callback.Run(m_lastPosition.Clone()); |
| + m_pendingGeolocations.clear(); |
| + m_lastPosition = nullptr; |
| } |
| -void GeolocationClientMock::controllerTimerFired(Timer<GeolocationClientMock>* timer) |
| +void GeolocationClientMock::runPermissionCallbacks() |
| { |
| - ASSERT_UNUSED(timer, timer == &m_controllerTimer); |
| + if (m_pendingPermissions.isEmpty() || m_permissionState == permission::STATUS_ASK) |
| + return; |
| - // Make a copy of the set of controllers since it might be modified while iterating. |
| - GeolocationControllers controllers = m_controllers; |
| - if (m_lastPosition.get()) { |
| - ASSERT(!m_hasError); |
| - for (GeolocationControllers::iterator it = controllers.begin(); it != controllers.end(); ++it) |
| - (*it)->positionChanged(m_lastPosition.get()); |
| - } else if (m_hasError) { |
| - for (GeolocationControllers::iterator it = controllers.begin(); it != controllers.end(); ++it) |
| - (*it)->errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, m_errorMessage)); |
| - } |
| -} |
| - |
| -void GeolocationClientMock::clearError() |
| -{ |
| - m_hasError = false; |
| - m_errorMessage = String(); |
| + for (const auto& pendingPermission : m_pendingPermissions) |
| + pendingPermission.Run(m_permissionState); |
| + m_permissionState = permission::STATUS_ASK; |
| + m_pendingPermissions.clear(); |
| } |
| DEFINE_TRACE(GeolocationClientMock) |
| { |
| -#if ENABLE(OILPAN) |
| - visitor->trace(m_controllers); |
| -#endif |
| - visitor->trace(m_lastPosition); |
| + WillBeHeapSupplement<Document>::trace(visitor); |
| visitor->trace(m_pendingPermissions); |
| - GeolocationClient::trace(visitor); |
| + visitor->trace(m_pendingGeolocations); |
| } |
| } // namespace blink |