OLD | NEW |
| (Empty) |
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 "core/platform/mock/GeolocationClientMock.h" | |
34 | |
35 #include "modules/geolocation/GeolocationController.h" | |
36 #include "modules/geolocation/GeolocationError.h" | |
37 #include "modules/geolocation/GeolocationPosition.h" | |
38 | |
39 namespace WebCore { | |
40 | |
41 GeolocationClientMock::GeolocationClientMock() | |
42 : m_controller(0) | |
43 , m_hasError(false) | |
44 , m_controllerTimer(this, &GeolocationClientMock::controllerTimerFired) | |
45 , m_permissionTimer(this, &GeolocationClientMock::permissionTimerFired) | |
46 , m_isActive(false) | |
47 , m_permissionState(PermissionStateUnset) | |
48 { | |
49 } | |
50 | |
51 GeolocationClientMock::~GeolocationClientMock() | |
52 { | |
53 ASSERT(!m_isActive); | |
54 } | |
55 | |
56 void GeolocationClientMock::setController(GeolocationController *controller) | |
57 { | |
58 ASSERT(controller && !m_controller); | |
59 m_controller = controller; | |
60 } | |
61 | |
62 void GeolocationClientMock::setPosition(PassRefPtr<GeolocationPosition> position
) | |
63 { | |
64 m_lastPosition = position; | |
65 clearError(); | |
66 asyncUpdateController(); | |
67 } | |
68 | |
69 void GeolocationClientMock::setPositionUnavailableError(const String& errorMessa
ge) | |
70 { | |
71 m_hasError = true; | |
72 m_errorMessage = errorMessage; | |
73 m_lastPosition = nullptr; | |
74 asyncUpdateController(); | |
75 } | |
76 | |
77 void GeolocationClientMock::setPermission(bool allowed) | |
78 { | |
79 m_permissionState = allowed ? PermissionStateAllowed : PermissionStateDenied
; | |
80 asyncUpdatePermission(); | |
81 } | |
82 | |
83 int GeolocationClientMock::numberOfPendingPermissionRequests() const | |
84 { | |
85 return m_pendingPermission.size(); | |
86 } | |
87 | |
88 void GeolocationClientMock::requestPermission(Geolocation* geolocation) | |
89 { | |
90 m_pendingPermission.add(geolocation); | |
91 if (m_permissionState != PermissionStateUnset) | |
92 asyncUpdatePermission(); | |
93 } | |
94 | |
95 void GeolocationClientMock::cancelPermissionRequest(Geolocation* geolocation) | |
96 { | |
97 // Called from Geolocation::disconnectFrame() in response to Frame destructi
on. | |
98 m_pendingPermission.remove(geolocation); | |
99 if (m_pendingPermission.isEmpty() && m_permissionTimer.isActive()) | |
100 m_permissionTimer.stop(); | |
101 } | |
102 | |
103 void GeolocationClientMock::asyncUpdatePermission() | |
104 { | |
105 ASSERT(m_permissionState != PermissionStateUnset); | |
106 if (!m_permissionTimer.isActive()) | |
107 m_permissionTimer.startOneShot(0); | |
108 } | |
109 | |
110 void GeolocationClientMock::permissionTimerFired(WebCore::Timer<GeolocationClien
tMock>* timer) | |
111 { | |
112 ASSERT_UNUSED(timer, timer == &m_permissionTimer); | |
113 ASSERT(m_permissionState != PermissionStateUnset); | |
114 bool allowed = m_permissionState == PermissionStateAllowed; | |
115 GeolocationSet::iterator end = m_pendingPermission.end(); | |
116 | |
117 // Once permission has been set (or denied) on a Geolocation object, there c
an be | |
118 // no further requests for permission to the mock. Consequently the callback
s | |
119 // which fire synchronously from Geolocation::setIsAllowed() cannot reentran
tly modify | |
120 // m_pendingPermission. | |
121 for (GeolocationSet::iterator it = m_pendingPermission.begin(); it != end; +
+it) | |
122 (*it)->setIsAllowed(allowed); | |
123 m_pendingPermission.clear(); | |
124 } | |
125 | |
126 void GeolocationClientMock::reset() | |
127 { | |
128 m_lastPosition = 0; | |
129 clearError(); | |
130 m_permissionState = PermissionStateUnset; | |
131 } | |
132 | |
133 void GeolocationClientMock::geolocationDestroyed() | |
134 { | |
135 ASSERT(!m_isActive); | |
136 } | |
137 | |
138 void GeolocationClientMock::startUpdating() | |
139 { | |
140 ASSERT(!m_isActive); | |
141 m_isActive = true; | |
142 asyncUpdateController(); | |
143 } | |
144 | |
145 void GeolocationClientMock::stopUpdating() | |
146 { | |
147 ASSERT(m_isActive); | |
148 m_isActive = false; | |
149 m_controllerTimer.stop(); | |
150 } | |
151 | |
152 void GeolocationClientMock::setEnableHighAccuracy(bool) | |
153 { | |
154 // FIXME: We need to add some tests regarding "high accuracy" mode. | |
155 // See https://bugs.webkit.org/show_bug.cgi?id=49438 | |
156 } | |
157 | |
158 GeolocationPosition* GeolocationClientMock::lastPosition() | |
159 { | |
160 return m_lastPosition.get(); | |
161 } | |
162 | |
163 void GeolocationClientMock::asyncUpdateController() | |
164 { | |
165 ASSERT(m_controller); | |
166 if (m_isActive && !m_controllerTimer.isActive()) | |
167 m_controllerTimer.startOneShot(0); | |
168 } | |
169 | |
170 void GeolocationClientMock::controllerTimerFired(Timer<GeolocationClientMock>* t
imer) | |
171 { | |
172 ASSERT_UNUSED(timer, timer == &m_controllerTimer); | |
173 ASSERT(m_controller); | |
174 | |
175 if (m_lastPosition.get()) { | |
176 ASSERT(!m_hasError); | |
177 m_controller->positionChanged(m_lastPosition.get()); | |
178 } else if (m_hasError) { | |
179 RefPtr<GeolocationError> geolocatioError = GeolocationError::create(Geol
ocationError::PositionUnavailable, m_errorMessage); | |
180 m_controller->errorOccurred(geolocatioError.get()); | |
181 } | |
182 } | |
183 | |
184 void GeolocationClientMock::clearError() | |
185 { | |
186 m_hasError = false; | |
187 m_errorMessage = String(); | |
188 } | |
189 | |
190 } // WebCore | |
OLD | NEW |