2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "GeolocationClientMock.h"
34 #if ENABLE(CLIENT_BASED_GEOLOCATION)
36 #include "GeolocationController.h"
37 #include "GeolocationError.h"
38 #include "GeolocationPosition.h"
42 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(PassRefPtr<GeolocationPosition> position)
64 m_lastPosition = position;
66 asyncUpdateController();
69 void GeolocationClientMock::setError(PassRefPtr<GeolocationError> error)
73 asyncUpdateController();
76 void GeolocationClientMock::setPermission(bool allowed)
78 m_permissionState = allowed ? PermissionStateAllowed : PermissionStateDenied;
79 asyncUpdatePermission();
82 int GeolocationClientMock::numberOfPendingPermissionRequests() const
84 return m_pendingPermission.size();
87 void GeolocationClientMock::requestPermission(Geolocation* geolocation)
89 m_pendingPermission.add(geolocation);
90 if (m_permissionState != PermissionStateUnset)
91 asyncUpdatePermission();
94 void GeolocationClientMock::cancelPermissionRequest(Geolocation* geolocation)
96 // Called from Geolocation::disconnectFrame() in response to Frame destruction.
97 m_pendingPermission.remove(geolocation);
98 if (m_pendingPermission.isEmpty() && m_permissionTimer.isActive())
99 m_permissionTimer.stop();
102 void GeolocationClientMock::asyncUpdatePermission()
104 ASSERT(m_permissionState != PermissionStateUnset);
105 if (!m_permissionTimer.isActive())
106 m_permissionTimer.startOneShot(0);
109 void GeolocationClientMock::permissionTimerFired(WebCore::Timer<GeolocationClientMock>* timer)
111 ASSERT_UNUSED(timer, timer == &m_permissionTimer);
112 ASSERT(m_permissionState != PermissionStateUnset);
113 bool allowed = m_permissionState == PermissionStateAllowed;
114 GeolocationSet::iterator end = m_pendingPermission.end();
116 // Once permission has been set (or denied) on a Geolocation object, there can be
117 // no further requests for permission to the mock. Consequently the callbacks
118 // which fire synchronously from Geolocation::setIsAllowed() cannot reentrantly modify
119 // m_pendingPermission.
120 for (GeolocationSet::iterator it = m_pendingPermission.begin(); it != end; ++it)
121 (*it)->setIsAllowed(allowed);
122 m_pendingPermission.clear();
125 void GeolocationClientMock::reset()
129 m_permissionState = PermissionStateUnset;
132 void GeolocationClientMock::geolocationDestroyed()
137 void GeolocationClientMock::startUpdating()
141 asyncUpdateController();
144 void GeolocationClientMock::stopUpdating()
148 m_controllerTimer.stop();
151 void GeolocationClientMock::setEnableHighAccuracy(bool)
153 // FIXME: We need to add some tests regarding "high accuracy" mode.
154 // See https://bugs.webkit.org/show_bug.cgi?id=49438
157 GeolocationPosition* GeolocationClientMock::lastPosition()
159 return m_lastPosition.get();
162 void GeolocationClientMock::asyncUpdateController()
164 ASSERT(m_controller);
165 if (m_isActive && !m_controllerTimer.isActive())
166 m_controllerTimer.startOneShot(0);
169 void GeolocationClientMock::controllerTimerFired(Timer<GeolocationClientMock>* timer)
171 ASSERT_UNUSED(timer, timer == &m_controllerTimer);
172 ASSERT(m_controller);
174 if (m_lastPosition.get())
175 m_controller->positionChanged(m_lastPosition.get());
176 else if (m_lastError.get())
177 m_controller->errorOccurred(m_lastError.get());
182 #endif // ENABLE(CLIENT_BASED_GEOLOCATION)