2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2012 Apple Inc. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "GeolocationClientMock.h"
35 #include "modules/geolocation/GeolocationController.h"
36 #include "modules/geolocation/GeolocationError.h"
37 #include "modules/geolocation/GeolocationPosition.h"
41 GeolocationClientMock::GeolocationClientMock()
43 , m_controllerTimer(this, &GeolocationClientMock::controllerTimerFired)
44 , m_permissionTimer(this, &GeolocationClientMock::permissionTimerFired)
46 , m_permissionState(PermissionStateUnset)
50 GeolocationClientMock::~GeolocationClientMock()
55 void GeolocationClientMock::setPosition(GeolocationPosition* position)
57 m_lastPosition = position;
59 asyncUpdateController();
62 void GeolocationClientMock::setPositionUnavailableError(const String& errorMessage)
65 m_errorMessage = errorMessage;
66 m_lastPosition = nullptr;
67 asyncUpdateController();
70 void GeolocationClientMock::setPermission(bool allowed)
72 m_permissionState = allowed ? PermissionStateAllowed : PermissionStateDenied;
73 asyncUpdatePermission();
76 int GeolocationClientMock::numberOfPendingPermissionRequests() const
78 return m_pendingPermissions.size();
81 void GeolocationClientMock::requestPermission(Geolocation* geolocation)
83 m_pendingPermissions.add(geolocation);
84 if (m_permissionState != PermissionStateUnset)
85 asyncUpdatePermission();
88 void GeolocationClientMock::cancelPermissionRequest(Geolocation* geolocation)
90 // Called from Geolocation::disconnectFrame() in response to LocalFrame destruction.
91 m_pendingPermissions.remove(geolocation);
92 if (m_pendingPermissions.isEmpty() && m_permissionTimer.isActive())
93 m_permissionTimer.stop();
96 void GeolocationClientMock::controllerForTestAdded(GeolocationController* controller)
98 m_controllers.add(controller);
101 void GeolocationClientMock::controllerForTestRemoved(GeolocationController* controller)
103 m_controllers.remove(controller);
106 void GeolocationClientMock::asyncUpdatePermission()
108 ASSERT(m_permissionState != PermissionStateUnset);
109 if (!m_permissionTimer.isActive())
110 m_permissionTimer.startOneShot(0, FROM_HERE);
113 void GeolocationClientMock::permissionTimerFired(Timer<GeolocationClientMock>* timer)
115 ASSERT_UNUSED(timer, timer == &m_permissionTimer);
116 ASSERT(m_permissionState != PermissionStateUnset);
117 bool allowed = m_permissionState == PermissionStateAllowed;
118 GeolocationSet::iterator end = m_pendingPermissions.end();
120 // Once permission has been set (or denied) on a Geolocation object, there can be
121 // no further requests for permission to the mock. Consequently the callbacks
122 // which fire synchronously from Geolocation::setIsAllowed() cannot reentrantly modify
123 // m_pendingPermissions.
124 for (GeolocationSet::iterator it = m_pendingPermissions.begin(); it != end; ++it)
125 (*it)->setIsAllowed(allowed);
126 m_pendingPermissions.clear();
129 void GeolocationClientMock::geolocationDestroyed()
134 void GeolocationClientMock::startUpdating()
138 asyncUpdateController();
141 void GeolocationClientMock::stopUpdating()
145 m_controllerTimer.stop();
148 void GeolocationClientMock::setEnableHighAccuracy(bool)
150 // FIXME: We need to add some tests regarding "high accuracy" mode.
151 // See https://bugs.webkit.org/show_bug.cgi?id=49438
154 GeolocationPosition* GeolocationClientMock::lastPosition()
156 return m_lastPosition.get();
159 void GeolocationClientMock::asyncUpdateController()
161 if (m_isActive && !m_controllerTimer.isActive())
162 m_controllerTimer.startOneShot(0, FROM_HERE);
165 void GeolocationClientMock::controllerTimerFired(Timer<GeolocationClientMock>* timer)
167 ASSERT_UNUSED(timer, timer == &m_controllerTimer);
169 // Make a copy of the set of controllers since it might be modified while iterating.
170 GeolocationControllers controllers = m_controllers;
171 if (m_lastPosition.get()) {
173 for (GeolocationControllers::iterator it = controllers.begin(); it != controllers.end(); ++it)
174 (*it)->positionChanged(m_lastPosition.get());
175 } else if (m_hasError) {
176 for (GeolocationControllers::iterator it = controllers.begin(); it != controllers.end(); ++it)
177 (*it)->errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, m_errorMessage));
181 void GeolocationClientMock::clearError()
184 m_errorMessage = String();