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" | |
17 | 16 |
18 using namespace device::mojom::blink; | 17 using namespace device::mojom::blink; |
19 | 18 |
20 namespace blink { | 19 namespace blink { |
21 | 20 |
22 Sensor::Sensor(ExecutionContext* executionContext, | 21 Sensor::Sensor(ExecutionContext* executionContext, |
23 const SensorOptions& sensorOptions, | 22 const SensorOptions& sensorOptions, |
24 ExceptionState& exceptionState, | 23 ExceptionState& exceptionState, |
25 SensorType type) | 24 SensorType type) |
26 : ContextLifecycleObserver(executionContext), | 25 : ContextLifecycleObserver(executionContext), |
27 m_sensorOptions(sensorOptions), | 26 m_sensorOptions(sensorOptions), |
28 m_type(type), | 27 m_type(type), |
29 m_state(Sensor::SensorState::Idle) { | 28 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->page(), | 167 m_sensorProxy = provider->createSensorProxy(m_type, document, |
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() { | 185 void Sensor::onSensorReadingChanged(double timestamp) { |
186 if (m_state == Sensor::SensorState::Activated) { | 186 if (m_state != Sensor::SensorState::Activated) |
187 DCHECK(m_sensorUpdateNotifier); | 187 return; |
188 m_sensorUpdateNotifier->onSensorReadingChanged(); | 188 |
| 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(); |
189 } | 194 } |
190 } | 195 } |
191 | 196 |
192 void Sensor::onSensorError(ExceptionCode code, | 197 void Sensor::onSensorError(ExceptionCode code, |
193 const String& sanitizedMessage, | 198 const String& sanitizedMessage, |
194 const String& unsanitizedMessage) { | 199 const String& unsanitizedMessage) { |
195 reportError(code, sanitizedMessage, unsanitizedMessage); | 200 reportError(code, sanitizedMessage, unsanitizedMessage); |
196 if (m_sensorUpdateNotifier) | |
197 m_sensorUpdateNotifier->cancelPendingNotifications(); | |
198 } | 201 } |
199 | 202 |
200 void Sensor::onStartRequestCompleted(bool result) { | 203 void Sensor::onStartRequestCompleted(bool result) { |
201 if (m_state != Sensor::SensorState::Activating) | 204 if (m_state != Sensor::SensorState::Activating) |
202 return; | 205 return; |
203 | 206 |
204 if (!result) { | 207 if (!result) { |
205 reportError( | 208 reportError( |
206 OperationError, | 209 OperationError, |
207 "start() call has failed possibly due to inappropriate options."); | 210 "start() call has failed possibly due to inappropriate options."); |
208 return; | 211 return; |
209 } | 212 } |
210 | 213 |
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)); | |
218 updateState(Sensor::SensorState::Activated); | 214 updateState(Sensor::SensorState::Activated); |
219 } | 215 } |
220 | 216 |
221 void Sensor::startListening() { | 217 void Sensor::startListening() { |
222 DCHECK(m_sensorProxy); | 218 DCHECK(m_sensorProxy); |
223 updateState(Sensor::SensorState::Activating); | 219 updateState(Sensor::SensorState::Activating); |
224 | 220 |
225 m_sensorProxy->addObserver(this); | 221 m_sensorProxy->addObserver(this); |
226 if (!m_sensorProxy->isInitialized()) { | 222 if (!m_sensorProxy->isInitialized()) { |
227 m_sensorProxy->initialize(); | 223 m_sensorProxy->initialize(); |
(...skipping 10 matching lines...) Expand all Loading... |
238 auto startCallback = | 234 auto startCallback = |
239 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); | 235 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); |
240 m_sensorProxy->addConfiguration(m_configuration->Clone(), | 236 m_sensorProxy->addConfiguration(m_configuration->Clone(), |
241 std::move(startCallback)); | 237 std::move(startCallback)); |
242 } | 238 } |
243 | 239 |
244 void Sensor::stopListening() { | 240 void Sensor::stopListening() { |
245 DCHECK(m_sensorProxy); | 241 DCHECK(m_sensorProxy); |
246 updateState(Sensor::SensorState::Idle); | 242 updateState(Sensor::SensorState::Idle); |
247 | 243 |
248 if (m_sensorUpdateNotifier) | |
249 m_sensorUpdateNotifier->cancelPendingNotifications(); | |
250 | |
251 if (m_sensorProxy->isInitialized()) { | 244 if (m_sensorProxy->isInitialized()) { |
252 DCHECK(m_configuration); | 245 DCHECK(m_configuration); |
253 m_sensorProxy->removeConfiguration(m_configuration->Clone()); | 246 m_sensorProxy->removeConfiguration(m_configuration->Clone()); |
254 } | 247 } |
255 m_sensorProxy->removeObserver(this); | 248 m_sensorProxy->removeObserver(this); |
256 } | 249 } |
257 | 250 |
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 | |
277 void Sensor::updateState(Sensor::SensorState newState) { | 251 void Sensor::updateState(Sensor::SensorState newState) { |
278 if (newState == m_state) | 252 if (newState == m_state) |
279 return; | 253 return; |
280 | 254 |
281 if (newState == SensorState::Activated && getExecutionContext()) { | 255 if (newState == SensorState::Activated && getExecutionContext()) { |
282 DCHECK_EQ(SensorState::Activating, m_state); | 256 DCHECK_EQ(SensorState::Activating, m_state); |
283 getExecutionContext()->postTask( | 257 getExecutionContext()->postTask( |
284 TaskType::Sensor, BLINK_FROM_HERE, | 258 TaskType::Sensor, BLINK_FROM_HERE, |
285 createSameThreadTask(&Sensor::notifyOnActivate, | 259 createSameThreadTask(&Sensor::notifyOnActivate, |
286 wrapWeakPersistent(this))); | 260 wrapWeakPersistent(this))); |
287 } | 261 } |
288 | 262 |
289 m_state = newState; | 263 m_state = newState; |
290 } | 264 } |
291 | 265 |
292 void Sensor::reportError(ExceptionCode code, | 266 void Sensor::reportError(ExceptionCode code, |
293 const String& sanitizedMessage, | 267 const String& sanitizedMessage, |
294 const String& unsanitizedMessage) { | 268 const String& unsanitizedMessage) { |
295 updateState(Sensor::SensorState::Errored); | 269 updateState(Sensor::SensorState::Errored); |
296 if (getExecutionContext()) { | 270 if (getExecutionContext()) { |
297 auto error = | 271 auto error = |
298 DOMException::create(code, sanitizedMessage, unsanitizedMessage); | 272 DOMException::create(code, sanitizedMessage, unsanitizedMessage); |
299 getExecutionContext()->postTask( | 273 getExecutionContext()->postTask( |
300 TaskType::Sensor, BLINK_FROM_HERE, | 274 TaskType::Sensor, BLINK_FROM_HERE, |
301 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), | 275 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), |
302 wrapPersistent(error))); | 276 wrapPersistent(error))); |
303 } | 277 } |
304 } | 278 } |
305 | 279 |
306 void Sensor::onSuspended() { | 280 void Sensor::notifySensorReadingChanged() { |
307 if (m_sensorUpdateNotifier) | 281 DCHECK(m_sensorProxy); |
308 m_sensorUpdateNotifier->cancelPendingNotifications(); | 282 DCHECK(m_sensorProxy->sensorReading()); |
309 } | |
310 | 283 |
311 void Sensor::notifySensorReadingChanged() { | 284 if (m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
312 dispatchEvent(Event::create(EventTypeNames::change)); | 285 m_storedData = m_sensorProxy->sensorReading()->data(); |
| 286 dispatchEvent(Event::create(EventTypeNames::change)); |
| 287 } |
313 } | 288 } |
314 | 289 |
315 void Sensor::notifyOnActivate() { | 290 void Sensor::notifyOnActivate() { |
316 dispatchEvent(Event::create(EventTypeNames::activate)); | 291 dispatchEvent(Event::create(EventTypeNames::activate)); |
317 } | 292 } |
318 | 293 |
319 void Sensor::notifyError(DOMException* error) { | 294 void Sensor::notifyError(DOMException* error) { |
320 dispatchEvent( | 295 dispatchEvent( |
321 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 296 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
322 } | 297 } |
323 | 298 |
324 } // namespace blink | 299 } // namespace blink |
OLD | NEW |