OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/sensor/Sensor.h" | 5 #include "modules/sensor/Sensor.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/ExceptionCode.h" | 8 #include "core/dom/ExceptionCode.h" |
9 #include "core/dom/ExecutionContextTask.h" | 9 #include "core/dom/ExecutionContextTask.h" |
10 #include "core/dom/TaskRunnerHelper.h" | 10 #include "core/dom/TaskRunnerHelper.h" |
11 #include "core/inspector/ConsoleMessage.h" | 11 #include "core/inspector/ConsoleMessage.h" |
12 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 12 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
13 #include "modules/sensor/SensorErrorEvent.h" | 13 #include "modules/sensor/SensorErrorEvent.h" |
14 #include "modules/sensor/SensorProviderProxy.h" | 14 #include "modules/sensor/SensorProviderProxy.h" |
15 #include "modules/sensor/SensorReading.h" | 15 #include "modules/sensor/SensorReading.h" |
| 16 #include "modules/sensor/SensorUpdateNotificationStrategy.h" |
16 | 17 |
17 using namespace device::mojom::blink; | 18 using namespace device::mojom::blink; |
18 | 19 |
19 namespace blink { | 20 namespace blink { |
20 | 21 |
21 Sensor::Sensor(ExecutionContext* executionContext, | 22 Sensor::Sensor(ExecutionContext* executionContext, |
22 const SensorOptions& sensorOptions, | 23 const SensorOptions& sensorOptions, |
23 ExceptionState& exceptionState, | 24 ExceptionState& exceptionState, |
24 SensorType type) | 25 SensorType type) |
25 : ContextLifecycleObserver(executionContext), | 26 : ContextLifecycleObserver(executionContext), |
26 m_sensorOptions(sensorOptions), | 27 m_sensorOptions(sensorOptions), |
27 m_type(type), | 28 m_type(type), |
28 m_state(Sensor::SensorState::Idle), | 29 m_state(Sensor::SensorState::Idle) { |
29 m_lastUpdateTimestamp(0.0) { | |
30 // Check secure context. | 30 // Check secure context. |
31 String errorMessage; | 31 String errorMessage; |
32 if (!executionContext->isSecureContext(errorMessage)) { | 32 if (!executionContext->isSecureContext(errorMessage)) { |
33 exceptionState.throwDOMException(SecurityError, errorMessage); | 33 exceptionState.throwDOMException(SecurityError, errorMessage); |
34 return; | 34 return; |
35 } | 35 } |
36 | 36 |
37 // Check top-level browsing context. | 37 // Check top-level browsing context. |
38 if (!toDocument(executionContext)->domWindow()->frame() || | 38 if (!toDocument(executionContext)->domWindow()->frame() || |
39 !toDocument(executionContext)->frame()->isMainFrame()) { | 39 !toDocument(executionContext)->frame()->isMainFrame()) { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 return; | 157 return; |
158 | 158 |
159 Document* document = toDocument(getExecutionContext()); | 159 Document* document = toDocument(getExecutionContext()); |
160 if (!document || !document->frame()) | 160 if (!document || !document->frame()) |
161 return; | 161 return; |
162 | 162 |
163 auto provider = SensorProviderProxy::from(document->frame()); | 163 auto provider = SensorProviderProxy::from(document->frame()); |
164 m_sensorProxy = provider->getSensorProxy(m_type); | 164 m_sensorProxy = provider->getSensorProxy(m_type); |
165 | 165 |
166 if (!m_sensorProxy) { | 166 if (!m_sensorProxy) { |
167 m_sensorProxy = provider->createSensorProxy(m_type, document, | 167 m_sensorProxy = provider->createSensorProxy(m_type, document->page(), |
168 createSensorReadingFactory()); | 168 createSensorReadingFactory()); |
169 } | 169 } |
170 } | 170 } |
171 | 171 |
172 void Sensor::contextDestroyed() { | 172 void Sensor::contextDestroyed() { |
173 if (m_state == Sensor::SensorState::Activated || | 173 if (m_state == Sensor::SensorState::Activated || |
174 m_state == Sensor::SensorState::Activating) | 174 m_state == Sensor::SensorState::Activating) |
175 stopListening(); | 175 stopListening(); |
176 } | 176 } |
177 | 177 |
178 void Sensor::onSensorInitialized() { | 178 void Sensor::onSensorInitialized() { |
179 if (m_state != Sensor::SensorState::Activating) | 179 if (m_state != Sensor::SensorState::Activating) |
180 return; | 180 return; |
181 | 181 |
182 startListening(); | 182 startListening(); |
183 } | 183 } |
184 | 184 |
185 void Sensor::onSensorReadingChanged(double timestamp) { | 185 void Sensor::onSensorReadingChanged() { |
186 if (m_state != Sensor::SensorState::Activated) | 186 if (m_state == Sensor::SensorState::Activated) { |
187 return; | 187 DCHECK(m_sensorUpdateNotifier); |
188 | 188 m_sensorUpdateNotifier->onSensorReadingChanged(); |
189 DCHECK_GT(m_configuration->frequency, 0.0); | |
190 double period = 1 / m_configuration->frequency; | |
191 if (timestamp - m_lastUpdateTimestamp >= period) { | |
192 m_lastUpdateTimestamp = timestamp; | |
193 notifySensorReadingChanged(); | |
194 } | 189 } |
195 } | 190 } |
196 | 191 |
197 void Sensor::onSensorError(ExceptionCode code, | 192 void Sensor::onSensorError(ExceptionCode code, |
198 const String& sanitizedMessage, | 193 const String& sanitizedMessage, |
199 const String& unsanitizedMessage) { | 194 const String& unsanitizedMessage) { |
200 reportError(code, sanitizedMessage, unsanitizedMessage); | 195 reportError(code, sanitizedMessage, unsanitizedMessage); |
| 196 if (m_sensorUpdateNotifier) |
| 197 m_sensorUpdateNotifier->cancelPendingNotifications(); |
201 } | 198 } |
202 | 199 |
203 void Sensor::onStartRequestCompleted(bool result) { | 200 void Sensor::onStartRequestCompleted(bool result) { |
204 if (m_state != Sensor::SensorState::Activating) | 201 if (m_state != Sensor::SensorState::Activating) |
205 return; | 202 return; |
206 | 203 |
207 if (!result) { | 204 if (!result) { |
208 reportError( | 205 reportError( |
209 OperationError, | 206 OperationError, |
210 "start() call has failed possibly due to inappropriate options."); | 207 "start() call has failed possibly due to inappropriate options."); |
211 return; | 208 return; |
212 } | 209 } |
213 | 210 |
| 211 DCHECK(m_configuration); |
| 212 DCHECK(m_sensorProxy); |
| 213 auto updateCallback = |
| 214 WTF::bind(&Sensor::onSensorUpdateNotification, wrapWeakPersistent(this)); |
| 215 DCHECK_GT(m_configuration->frequency, 0); |
| 216 m_sensorUpdateNotifier = SensorUpdateNotificationStrategy::create( |
| 217 m_configuration->frequency, std::move(updateCallback)); |
214 updateState(Sensor::SensorState::Activated); | 218 updateState(Sensor::SensorState::Activated); |
215 } | 219 } |
216 | 220 |
217 void Sensor::startListening() { | 221 void Sensor::startListening() { |
218 DCHECK(m_sensorProxy); | 222 DCHECK(m_sensorProxy); |
219 updateState(Sensor::SensorState::Activating); | 223 updateState(Sensor::SensorState::Activating); |
220 | 224 |
221 m_sensorProxy->addObserver(this); | 225 m_sensorProxy->addObserver(this); |
222 if (!m_sensorProxy->isInitialized()) { | 226 if (!m_sensorProxy->isInitialized()) { |
223 m_sensorProxy->initialize(); | 227 m_sensorProxy->initialize(); |
(...skipping 10 matching lines...) Expand all Loading... |
234 auto startCallback = | 238 auto startCallback = |
235 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); | 239 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); |
236 m_sensorProxy->addConfiguration(m_configuration->Clone(), | 240 m_sensorProxy->addConfiguration(m_configuration->Clone(), |
237 std::move(startCallback)); | 241 std::move(startCallback)); |
238 } | 242 } |
239 | 243 |
240 void Sensor::stopListening() { | 244 void Sensor::stopListening() { |
241 DCHECK(m_sensorProxy); | 245 DCHECK(m_sensorProxy); |
242 updateState(Sensor::SensorState::Idle); | 246 updateState(Sensor::SensorState::Idle); |
243 | 247 |
| 248 if (m_sensorUpdateNotifier) |
| 249 m_sensorUpdateNotifier->cancelPendingNotifications(); |
| 250 |
244 if (m_sensorProxy->isInitialized()) { | 251 if (m_sensorProxy->isInitialized()) { |
245 DCHECK(m_configuration); | 252 DCHECK(m_configuration); |
246 m_sensorProxy->removeConfiguration(m_configuration->Clone()); | 253 m_sensorProxy->removeConfiguration(m_configuration->Clone()); |
247 } | 254 } |
248 m_sensorProxy->removeObserver(this); | 255 m_sensorProxy->removeObserver(this); |
249 } | 256 } |
250 | 257 |
| 258 void Sensor::onSensorUpdateNotification() { |
| 259 if (m_state != Sensor::SensorState::Activated) |
| 260 return; |
| 261 |
| 262 DCHECK(m_sensorProxy); |
| 263 DCHECK(m_sensorProxy->isInitialized()); |
| 264 DCHECK(m_sensorProxy->sensorReading()); |
| 265 |
| 266 if (getExecutionContext() && |
| 267 m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
| 268 getExecutionContext()->postTask( |
| 269 TaskType::Sensor, BLINK_FROM_HERE, |
| 270 createSameThreadTask(&Sensor::notifySensorReadingChanged, |
| 271 wrapWeakPersistent(this))); |
| 272 } |
| 273 |
| 274 m_storedData = m_sensorProxy->sensorReading()->data(); |
| 275 } |
| 276 |
251 void Sensor::updateState(Sensor::SensorState newState) { | 277 void Sensor::updateState(Sensor::SensorState newState) { |
252 if (newState == m_state) | 278 if (newState == m_state) |
253 return; | 279 return; |
254 | 280 |
255 if (newState == SensorState::Activated && getExecutionContext()) { | 281 if (newState == SensorState::Activated && getExecutionContext()) { |
256 DCHECK_EQ(SensorState::Activating, m_state); | 282 DCHECK_EQ(SensorState::Activating, m_state); |
257 getExecutionContext()->postTask( | 283 getExecutionContext()->postTask( |
258 TaskType::Sensor, BLINK_FROM_HERE, | 284 TaskType::Sensor, BLINK_FROM_HERE, |
259 createSameThreadTask(&Sensor::notifyOnActivate, | 285 createSameThreadTask(&Sensor::notifyOnActivate, |
260 wrapWeakPersistent(this))); | 286 wrapWeakPersistent(this))); |
261 } | 287 } |
262 | 288 |
263 m_state = newState; | 289 m_state = newState; |
264 } | 290 } |
265 | 291 |
266 void Sensor::reportError(ExceptionCode code, | 292 void Sensor::reportError(ExceptionCode code, |
267 const String& sanitizedMessage, | 293 const String& sanitizedMessage, |
268 const String& unsanitizedMessage) { | 294 const String& unsanitizedMessage) { |
269 updateState(Sensor::SensorState::Errored); | 295 updateState(Sensor::SensorState::Errored); |
270 if (getExecutionContext()) { | 296 if (getExecutionContext()) { |
271 auto error = | 297 auto error = |
272 DOMException::create(code, sanitizedMessage, unsanitizedMessage); | 298 DOMException::create(code, sanitizedMessage, unsanitizedMessage); |
273 getExecutionContext()->postTask( | 299 getExecutionContext()->postTask( |
274 TaskType::Sensor, BLINK_FROM_HERE, | 300 TaskType::Sensor, BLINK_FROM_HERE, |
275 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), | 301 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), |
276 wrapPersistent(error))); | 302 wrapPersistent(error))); |
277 } | 303 } |
278 } | 304 } |
279 | 305 |
| 306 void Sensor::onSuspended() { |
| 307 if (m_sensorUpdateNotifier) |
| 308 m_sensorUpdateNotifier->cancelPendingNotifications(); |
| 309 } |
| 310 |
280 void Sensor::notifySensorReadingChanged() { | 311 void Sensor::notifySensorReadingChanged() { |
281 DCHECK(m_sensorProxy); | 312 dispatchEvent(Event::create(EventTypeNames::change)); |
282 DCHECK(m_sensorProxy->sensorReading()); | |
283 | |
284 if (m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { | |
285 m_storedData = m_sensorProxy->sensorReading()->data(); | |
286 dispatchEvent(Event::create(EventTypeNames::change)); | |
287 } | |
288 } | 313 } |
289 | 314 |
290 void Sensor::notifyOnActivate() { | 315 void Sensor::notifyOnActivate() { |
291 dispatchEvent(Event::create(EventTypeNames::activate)); | 316 dispatchEvent(Event::create(EventTypeNames::activate)); |
292 } | 317 } |
293 | 318 |
294 void Sensor::notifyError(DOMException* error) { | 319 void Sensor::notifyError(DOMException* error) { |
295 dispatchEvent( | 320 dispatchEvent( |
296 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 321 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
297 } | 322 } |
298 | 323 |
299 } // namespace blink | 324 } // namespace blink |
OLD | NEW |