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()
44 , m_controllerTimer(this, &GeolocationClientMock::controllerTimerFired)
45 , m_permissionTimer(this, &GeolocationClientMock::permissionTimerFired)
47 , m_permissionState(PermissionStateUnset)
51 GeolocationClientMock::~GeolocationClientMock()
56 void GeolocationClientMock::setController(GeolocationController *controller)
58 ASSERT(controller && !m_controller);
59 m_controller = controller;
62 void GeolocationClientMock::setPosition(PassRefPtrWillBeRawPtr<GeolocationPosition> position)
64 m_lastPosition = position;
66 asyncUpdateController();
69 void GeolocationClientMock::setPositionUnavailableError(const String& errorMessage)
72 m_errorMessage = errorMessage;
73 m_lastPosition = nullptr;
74 asyncUpdateController();
77 void GeolocationClientMock::setPermission(bool allowed)
79 m_permissionState = allowed ? PermissionStateAllowed : PermissionStateDenied;
80 asyncUpdatePermission();
83 int GeolocationClientMock::numberOfPendingPermissionRequests() const
85 return m_pendingPermissions.size();
88 void GeolocationClientMock::requestPermission(Geolocation* geolocation)
90 m_pendingPermissions.add(geolocation);
91 if (m_permissionState != PermissionStateUnset)
92 asyncUpdatePermission();
95 void GeolocationClientMock::cancelPermissionRequest(Geolocation* geolocation)
97 // Called from Geolocation::disconnectFrame() in response to LocalFrame destruction.
98 m_pendingPermissions.remove(geolocation);
99 if (m_pendingPermissions.isEmpty() && m_permissionTimer.isActive())
100 m_permissionTimer.stop();
103 void GeolocationClientMock::asyncUpdatePermission()
105 ASSERT(m_permissionState != PermissionStateUnset);
106 if (!m_permissionTimer.isActive())
107 m_permissionTimer.startOneShot(0, FROM_HERE);
110 void GeolocationClientMock::permissionTimerFired(Timer<GeolocationClientMock>* timer)
112 ASSERT_UNUSED(timer, timer == &m_permissionTimer);
113 ASSERT(m_permissionState != PermissionStateUnset);
114 bool allowed = m_permissionState == PermissionStateAllowed;
115 GeolocationSet::iterator end = m_pendingPermissions.end();
117 // Once permission has been set (or denied) on a Geolocation object, there can be
118 // no further requests for permission to the mock. Consequently the callbacks
119 // which fire synchronously from Geolocation::setIsAllowed() cannot reentrantly modify
120 // m_pendingPermissions.
121 for (GeolocationSet::iterator it = m_pendingPermissions.begin(); it != end; ++it)
122 (*it)->setIsAllowed(allowed);
123 m_pendingPermissions.clear();
126 void GeolocationClientMock::geolocationDestroyed()
131 void GeolocationClientMock::startUpdating()
135 asyncUpdateController();
138 void GeolocationClientMock::stopUpdating()
142 m_controllerTimer.stop();
145 void GeolocationClientMock::setEnableHighAccuracy(bool)
147 // FIXME: We need to add some tests regarding "high accuracy" mode.
148 // See https://bugs.webkit.org/show_bug.cgi?id=49438
151 GeolocationPosition* GeolocationClientMock::lastPosition()
153 return m_lastPosition.get();
156 void GeolocationClientMock::asyncUpdateController()
158 ASSERT(m_controller);
159 if (m_isActive && !m_controllerTimer.isActive())
160 m_controllerTimer.startOneShot(0, FROM_HERE);
163 void GeolocationClientMock::controllerTimerFired(Timer<GeolocationClientMock>* timer)
165 ASSERT_UNUSED(timer, timer == &m_controllerTimer);
166 ASSERT(m_controller);
168 if (m_lastPosition.get()) {
170 m_controller->positionChanged(m_lastPosition.get());
171 } else if (m_hasError) {
172 m_controller->errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, m_errorMessage).get());
176 void GeolocationClientMock::clearError()
179 m_errorMessage = String();