Chromium Code Reviews| 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/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
| 11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| 12 #include "modules/sensor/SensorErrorEvent.h" | 12 #include "modules/sensor/SensorErrorEvent.h" |
| 13 #include "modules/sensor/SensorProviderProxy.h" | 13 #include "modules/sensor/SensorProviderProxy.h" |
| 14 #include "modules/sensor/SensorReading.h" | 14 #include "modules/sensor/SensorReading.h" |
| 15 #include "modules/sensor/SensorUpdateNotificationStrategy.h" | |
| 16 | 15 |
| 17 using namespace device::mojom::blink; | 16 using namespace device::mojom::blink; |
| 18 | 17 |
| 19 namespace blink { | 18 namespace blink { |
| 20 | 19 |
| 21 Sensor::Sensor(ExecutionContext* executionContext, | 20 Sensor::Sensor(ExecutionContext* executionContext, |
| 22 const SensorOptions& sensorOptions, | 21 const SensorOptions& sensorOptions, |
| 23 ExceptionState& exceptionState, | 22 ExceptionState& exceptionState, |
| 24 SensorType type) | 23 SensorType type) |
| 25 : ActiveScriptWrappable<Sensor>(this), | 24 : ActiveScriptWrappable<Sensor>(this), |
| 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 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 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyOnActivate, | 258 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyOnActivate, |
| 285 wrapWeakPersistent(this))); | 259 wrapWeakPersistent(this))); |
| 286 } | 260 } |
| 287 | 261 |
| 288 m_state = newState; | 262 m_state = newState; |
| 289 } | 263 } |
| 290 | 264 |
| 291 void Sensor::reportError(ExceptionCode code, | 265 void Sensor::reportError(ExceptionCode code, |
| 292 const String& sanitizedMessage, | 266 const String& sanitizedMessage, |
| 293 const String& unsanitizedMessage) { | 267 const String& unsanitizedMessage) { |
| 294 updateState(Sensor::SensorState::Errored); | 268 updateState(Sensor::SensorState::Errored); |
| 295 if (getExecutionContext()) { | 269 if (getExecutionContext()) { |
| 296 auto error = | 270 auto error = |
| 297 DOMException::create(code, sanitizedMessage, unsanitizedMessage); | 271 DOMException::create(code, sanitizedMessage, unsanitizedMessage); |
| 298 getExecutionContext()->postTask( | 272 getExecutionContext()->postTask( |
| 299 BLINK_FROM_HERE, | 273 BLINK_FROM_HERE, |
| 300 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), | 274 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), |
| 301 wrapPersistent(error))); | 275 wrapPersistent(error))); |
| 302 } | 276 } |
| 303 } | 277 } |
| 304 | 278 |
| 305 void Sensor::onSuspended() { | 279 void Sensor::notifySensorReadingChanged() { |
| 306 if (m_sensorUpdateNotifier) | 280 DCHECK(m_sensorProxy); |
| 307 m_sensorUpdateNotifier->cancelPendingNotifications(); | 281 DCHECK(m_sensorProxy->sensorReading()); |
| 308 } | |
| 309 | 282 |
| 310 void Sensor::notifySensorReadingChanged() { | 283 if (m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
| 311 dispatchEvent(Event::create(EventTypeNames::change)); | 284 dispatchEvent(Event::create(EventTypeNames::change)); |
|
shalamov
2016/12/16 17:10:48
should it be reversed?
m_storedData = m_sensorPro
Reilly Grant (use Gerrit)
2016/12/16 22:09:37
If this data were used by Sensor::reading() then i
Mikhail
2016/12/19 07:07:56
Done.
Mikhail
2016/12/19 07:07:56
Done.
| |
| 285 m_storedData = m_sensorProxy->sensorReading()->data(); | |
| 286 } | |
| 312 } | 287 } |
| 313 | 288 |
| 314 void Sensor::notifyOnActivate() { | 289 void Sensor::notifyOnActivate() { |
| 315 dispatchEvent(Event::create(EventTypeNames::activate)); | 290 dispatchEvent(Event::create(EventTypeNames::activate)); |
| 316 } | 291 } |
| 317 | 292 |
| 318 void Sensor::notifyError(DOMException* error) { | 293 void Sensor::notifyError(DOMException* error) { |
| 319 dispatchEvent( | 294 dispatchEvent( |
| 320 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 295 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
| 321 } | 296 } |
| 322 | 297 |
| 323 } // namespace blink | 298 } // namespace blink |
| OLD | NEW |