| 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..1148a549784ad3e81d880f2528c15c0d7412b94f 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);
|
| + }
|
| + return supplement;
|
| +}
|
| +
|
| +GeolocationClientMock::GeolocationClientMock(Document* document)
|
| + : m_document(document)
|
| + , m_permissionState(content::PERMISSION_STATUS_ASK)
|
| + , m_geolocationBinding(this)
|
| + , m_permissionBinding(this)
|
| +{
|
| + m_document->setServiceProvider(this);
|
| }
|
|
|
| GeolocationClientMock::~GeolocationClientMock()
|
| {
|
| - ASSERT(!m_isActive);
|
| + if (m_document->serviceProvider() == this)
|
| + m_document->setServiceProvider(nullptr);
|
| }
|
|
|
| void GeolocationClientMock::setPosition(GeolocationPosition* position)
|
| {
|
| - m_lastPosition = position;
|
| - clearError();
|
| - asyncUpdateController();
|
| + m_lastPosition = content::MojoGeoposition::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 = content::MojoGeoposition::New();
|
| + m_lastPosition->error_code = content::MojoGeoposition::ERROR_CODE_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 ? content::PERMISSION_STATUS_GRANTED : content::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(content::PermissionName permission,
|
| + const mojo::String& origin,
|
| + const HasPermissionCallback& callback)
|
| {
|
| - m_controllers.remove(controller);
|
| + callback.Run(content::PERMISSION_STATUS_ASK);
|
| }
|
|
|
| -void GeolocationClientMock::asyncUpdatePermission()
|
| +void GeolocationClientMock::RequestPermission(content::PermissionName 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<content::PermissionName> 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(content::PermissionName permission,
|
| + const mojo::String& origin,
|
| + const RevokePermissionCallback& callback)
|
| {
|
| - ASSERT(!m_isActive);
|
| - m_isActive = true;
|
| - asyncUpdateController();
|
| }
|
|
|
| -void GeolocationClientMock::stopUpdating()
|
| +void GeolocationClientMock::GetNextPermissionChange(
|
| + content::PermissionName permission,
|
| + const mojo::String& origin,
|
| + content::PermissionStatus 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)
|
| {
|
| - // FIXME: We need to add some tests regarding "high accuracy" mode.
|
| - // See https://bugs.webkit.org/show_bug.cgi?id=49438
|
| + if (interfaceName == content::GeolocationService::Name_)
|
| + m_geolocationBinding.Bind(pipe.Pass());
|
| + else if (interfaceName == content::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 == content::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 = content::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
|
|
|