1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2012 Apple Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
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
14 * distribution.
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.
18 *
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.
30 */
31
32 #include "config.h"
33 #include "GeolocationClientMock.h"
34
35 #include "modules/geolocation/GeolocationController.h"
36 #include "modules/geolocation/GeolocationError.h"
37 #include "modules/geolocation/GeolocationPosition.h"
38
39 namespace blink {
40
GeolocationClientMock()41 GeolocationClientMock::GeolocationClientMock()
42 : m_hasError(false)
43 , m_controllerTimer(this, &GeolocationClientMock::controllerTimerFired)
44 , m_permissionTimer(this, &GeolocationClientMock::permissionTimerFired)
45 , m_isActive(false)
46 , m_permissionState(PermissionStateUnset)
47 {
48 }
49
~GeolocationClientMock()50 GeolocationClientMock::~GeolocationClientMock()
51 {
52 ASSERT(!m_isActive);
53 }
54
setPosition(GeolocationPosition * position)55 void GeolocationClientMock::setPosition(GeolocationPosition* position)
56 {
57 m_lastPosition = position;
58 clearError();
59 asyncUpdateController();
60 }
61
setPositionUnavailableError(const String & errorMessage)62 void GeolocationClientMock::setPositionUnavailableError(const String& errorMessage)
63 {
64 m_hasError = true;
65 m_errorMessage = errorMessage;
66 m_lastPosition = nullptr;
67 asyncUpdateController();
68 }
69
setPermission(bool allowed)70 void GeolocationClientMock::setPermission(bool allowed)
71 {
72 m_permissionState = allowed ? PermissionStateAllowed : PermissionStateDenied;
73 asyncUpdatePermission();
74 }
75
numberOfPendingPermissionRequests() const76 int GeolocationClientMock::numberOfPendingPermissionRequests() const
77 {
78 return m_pendingPermissions.size();
79 }
80
requestPermission(Geolocation * geolocation)81 void GeolocationClientMock::requestPermission(Geolocation* geolocation)
82 {
83 m_pendingPermissions.add(geolocation);
84 if (m_permissionState != PermissionStateUnset)
85 asyncUpdatePermission();
86 }
87
cancelPermissionRequest(Geolocation * geolocation)88 void GeolocationClientMock::cancelPermissionRequest(Geolocation* geolocation)
89 {
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();
94 }
95
controllerForTestAdded(GeolocationController * controller)96 void GeolocationClientMock::controllerForTestAdded(GeolocationController* controller)
97 {
98 m_controllers.add(controller);
99 }
100
controllerForTestRemoved(GeolocationController * controller)101 void GeolocationClientMock::controllerForTestRemoved(GeolocationController* controller)
102 {
103 m_controllers.remove(controller);
104 }
105
asyncUpdatePermission()106 void GeolocationClientMock::asyncUpdatePermission()
107 {
108 ASSERT(m_permissionState != PermissionStateUnset);
109 if (!m_permissionTimer.isActive())
110 m_permissionTimer.startOneShot(0, FROM_HERE);
111 }
112
permissionTimerFired(Timer<GeolocationClientMock> * timer)113 void GeolocationClientMock::permissionTimerFired(Timer<GeolocationClientMock>* timer)
114 {
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();
119
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();
127 }
128
startUpdating()129 void GeolocationClientMock::startUpdating()
130 {
131 ASSERT(!m_isActive);
132 m_isActive = true;
133 asyncUpdateController();
134 }
135
stopUpdating()136 void GeolocationClientMock::stopUpdating()
137 {
138 ASSERT(m_isActive);
139 m_isActive = false;
140 m_controllerTimer.stop();
141 }
142
setEnableHighAccuracy(bool)143 void GeolocationClientMock::setEnableHighAccuracy(bool)
144 {
145 // FIXME: We need to add some tests regarding "high accuracy" mode.
146 // See https://bugs.webkit.org/show_bug.cgi?id=49438
147 }
148
lastPosition()149 GeolocationPosition* GeolocationClientMock::lastPosition()
150 {
151 return m_lastPosition.get();
152 }
153
asyncUpdateController()154 void GeolocationClientMock::asyncUpdateController()
155 {
156 if (m_isActive && !m_controllerTimer.isActive())
157 m_controllerTimer.startOneShot(0, FROM_HERE);
158 }
159
controllerTimerFired(Timer<GeolocationClientMock> * timer)160 void GeolocationClientMock::controllerTimerFired(Timer<GeolocationClientMock>* timer)
161 {
162 ASSERT_UNUSED(timer, timer == &m_controllerTimer);
163
164 // Make a copy of the set of controllers since it might be modified while iterating.
165 GeolocationControllers controllers = m_controllers;
166 if (m_lastPosition.get()) {
167 ASSERT(!m_hasError);
168 for (GeolocationControllers::iterator it = controllers.begin(); it != controllers.end(); ++it)
169 (*it)->positionChanged(m_lastPosition.get());
170 } else if (m_hasError) {
171 for (GeolocationControllers::iterator it = controllers.begin(); it != controllers.end(); ++it)
172 (*it)->errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, m_errorMessage));
173 }
174 }
175
clearError()176 void GeolocationClientMock::clearError()
177 {
178 m_hasError = false;
179 m_errorMessage = String();
180 }
181
trace(Visitor * visitor)182 void GeolocationClientMock::trace(Visitor* visitor)
183 {
184 #if ENABLE(OILPAN)
185 visitor->trace(m_controllers);
186 #endif
187 visitor->trace(m_lastPosition);
188 visitor->trace(m_pendingPermissions);
189 GeolocationClient::trace(visitor);
190 }
191
192 } // namespace blink
193