| 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/FrameRequestCallback.h" |
| 10 #include "core/inspector/ConsoleMessage.h" | 11 #include "core/inspector/ConsoleMessage.h" |
| 11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 12 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| 12 #include "modules/sensor/SensorErrorEvent.h" | 13 #include "modules/sensor/SensorErrorEvent.h" |
| 13 #include "modules/sensor/SensorProviderProxy.h" | 14 #include "modules/sensor/SensorProviderProxy.h" |
| 14 #include "modules/sensor/SensorReading.h" | 15 #include "modules/sensor/SensorReading.h" |
| 15 #include "modules/sensor/SensorUpdateNotificationStrategy.h" | 16 #include "modules/sensor/SensorUpdateNotifier.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 : ActiveScriptWrappable(this), | 26 : ActiveScriptWrappable(this), |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 return; | 158 return; |
| 158 | 159 |
| 159 Document* document = toDocument(getExecutionContext()); | 160 Document* document = toDocument(getExecutionContext()); |
| 160 if (!document || !document->frame()) | 161 if (!document || !document->frame()) |
| 161 return; | 162 return; |
| 162 | 163 |
| 163 auto provider = SensorProviderProxy::from(document->frame()); | 164 auto provider = SensorProviderProxy::from(document->frame()); |
| 164 m_sensorProxy = provider->getSensorProxy(m_type); | 165 m_sensorProxy = provider->getSensorProxy(m_type); |
| 165 | 166 |
| 166 if (!m_sensorProxy) { | 167 if (!m_sensorProxy) { |
| 167 m_sensorProxy = provider->createSensorProxy(m_type, document->page(), | 168 m_sensorProxy = provider->createSensorProxy(m_type, document, |
| 168 createSensorReadingFactory()); | 169 createSensorReadingFactory()); |
| 169 } | 170 } |
| 170 } | 171 } |
| 171 | 172 |
| 172 void Sensor::contextDestroyed() { | 173 void Sensor::contextDestroyed() { |
| 173 if (m_state == Sensor::SensorState::Activated || | 174 if (m_state == Sensor::SensorState::Activated || |
| 174 m_state == Sensor::SensorState::Activating) | 175 m_state == Sensor::SensorState::Activating) |
| 175 stopListening(); | 176 stopListening(); |
| 176 } | 177 } |
| 177 | 178 |
| 178 void Sensor::onSensorInitialized() { | 179 void Sensor::onSensorInitialized() { |
| 179 if (m_state != Sensor::SensorState::Activating) | 180 if (m_state != Sensor::SensorState::Activating) |
| 180 return; | 181 return; |
| 181 | 182 |
| 182 startListening(); | 183 startListening(); |
| 183 } | 184 } |
| 184 | 185 |
| 185 void Sensor::onSensorReadingChanged() { | 186 void Sensor::onSensorReadingChanged(double timestamp) { |
| 186 if (m_state == Sensor::SensorState::Activated) { | 187 if (m_state == Sensor::SensorState::Activated) { |
| 187 DCHECK(m_sensorUpdateNotifier); | 188 DCHECK(m_sensorUpdateNotifier); |
| 188 m_sensorUpdateNotifier->onSensorReadingChanged(); | 189 m_sensorUpdateNotifier->onSensorReadingChanged(timestamp); |
| 189 } | 190 } |
| 190 } | 191 } |
| 191 | 192 |
| 192 void Sensor::onSensorError(ExceptionCode code, | 193 void Sensor::onSensorError(ExceptionCode code, |
| 193 const String& sanitizedMessage, | 194 const String& sanitizedMessage, |
| 194 const String& unsanitizedMessage) { | 195 const String& unsanitizedMessage) { |
| 195 reportError(code, sanitizedMessage, unsanitizedMessage); | 196 reportError(code, sanitizedMessage, unsanitizedMessage); |
| 196 if (m_sensorUpdateNotifier) | 197 if (m_sensorUpdateNotifier) |
| 197 m_sensorUpdateNotifier->cancelPendingNotifications(); | 198 m_sensorUpdateNotifier->cancelPendingNotifications(); |
| 198 } | 199 } |
| 199 | 200 |
| 200 void Sensor::onStartRequestCompleted(bool result) { | 201 void Sensor::onStartRequestCompleted(bool result) { |
| 201 if (m_state != Sensor::SensorState::Activating) | 202 if (m_state != Sensor::SensorState::Activating) |
| 202 return; | 203 return; |
| 203 | 204 |
| 204 if (!result) { | 205 if (!result) { |
| 205 reportError( | 206 reportError( |
| 206 OperationError, | 207 OperationError, |
| 207 "start() call has failed possibly due to inappropriate options."); | 208 "start() call has failed possibly due to inappropriate options."); |
| 208 return; | 209 return; |
| 209 } | 210 } |
| 210 | 211 |
| 211 DCHECK(m_configuration); | 212 DCHECK(m_configuration); |
| 212 DCHECK(m_sensorProxy); | 213 DCHECK(m_sensorProxy); |
| 213 auto updateCallback = | 214 auto updateCallback = |
| 214 WTF::bind(&Sensor::onSensorUpdateNotification, wrapWeakPersistent(this)); | 215 WTF::bind(&Sensor::notifySensorReadingChanged, wrapWeakPersistent(this)); |
| 215 DCHECK_GT(m_configuration->frequency, 0); | 216 DCHECK_GT(m_configuration->frequency, 0); |
| 216 m_sensorUpdateNotifier = SensorUpdateNotificationStrategy::create( | 217 m_sensorUpdateNotifier = SensorUpdateNotifier::create( |
| 217 m_configuration->frequency, std::move(updateCallback)); | 218 m_configuration->frequency, std::move(updateCallback), |
| 219 m_sensorProxy->reportingMode()); |
| 218 updateState(Sensor::SensorState::Activated); | 220 updateState(Sensor::SensorState::Activated); |
| 219 } | 221 } |
| 220 | 222 |
| 221 void Sensor::startListening() { | 223 void Sensor::startListening() { |
| 222 DCHECK(m_sensorProxy); | 224 DCHECK(m_sensorProxy); |
| 223 updateState(Sensor::SensorState::Activating); | 225 updateState(Sensor::SensorState::Activating); |
| 224 | 226 |
| 225 m_sensorProxy->addObserver(this); | 227 m_sensorProxy->addObserver(this); |
| 226 if (!m_sensorProxy->isInitialized()) { | 228 if (!m_sensorProxy->isInitialized()) { |
| 227 m_sensorProxy->initialize(); | 229 m_sensorProxy->initialize(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 248 if (m_sensorUpdateNotifier) | 250 if (m_sensorUpdateNotifier) |
| 249 m_sensorUpdateNotifier->cancelPendingNotifications(); | 251 m_sensorUpdateNotifier->cancelPendingNotifications(); |
| 250 | 252 |
| 251 if (m_sensorProxy->isInitialized()) { | 253 if (m_sensorProxy->isInitialized()) { |
| 252 DCHECK(m_configuration); | 254 DCHECK(m_configuration); |
| 253 m_sensorProxy->removeConfiguration(m_configuration->Clone()); | 255 m_sensorProxy->removeConfiguration(m_configuration->Clone()); |
| 254 } | 256 } |
| 255 m_sensorProxy->removeObserver(this); | 257 m_sensorProxy->removeObserver(this); |
| 256 } | 258 } |
| 257 | 259 |
| 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) { | 260 void Sensor::updateState(Sensor::SensorState newState) { |
| 278 if (newState == m_state) | 261 if (newState == m_state) |
| 279 return; | 262 return; |
| 280 | 263 |
| 281 if (newState == SensorState::Activated && getExecutionContext()) { | 264 if (newState == SensorState::Activated && getExecutionContext()) { |
| 282 DCHECK_EQ(SensorState::Activating, m_state); | 265 DCHECK_EQ(SensorState::Activating, m_state); |
| 283 getExecutionContext()->postTask( | 266 getExecutionContext()->postTask( |
| 284 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyOnActivate, | 267 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyOnActivate, |
| 285 wrapWeakPersistent(this))); | 268 wrapWeakPersistent(this))); |
| 286 } | 269 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 301 wrapPersistent(error))); | 284 wrapPersistent(error))); |
| 302 } | 285 } |
| 303 } | 286 } |
| 304 | 287 |
| 305 void Sensor::onSuspended() { | 288 void Sensor::onSuspended() { |
| 306 if (m_sensorUpdateNotifier) | 289 if (m_sensorUpdateNotifier) |
| 307 m_sensorUpdateNotifier->cancelPendingNotifications(); | 290 m_sensorUpdateNotifier->cancelPendingNotifications(); |
| 308 } | 291 } |
| 309 | 292 |
| 310 void Sensor::notifySensorReadingChanged() { | 293 void Sensor::notifySensorReadingChanged() { |
| 311 dispatchEvent(Event::create(EventTypeNames::change)); | 294 if (m_state != Sensor::SensorState::Activated) |
| 295 return; |
| 296 |
| 297 DCHECK(m_sensorProxy); |
| 298 DCHECK(m_sensorProxy->sensorReading()); |
| 299 |
| 300 if (m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
| 301 dispatchEvent(Event::create(EventTypeNames::change)); |
| 302 m_storedData = m_sensorProxy->sensorReading()->data(); |
| 303 } |
| 312 } | 304 } |
| 313 | 305 |
| 314 void Sensor::notifyOnActivate() { | 306 void Sensor::notifyOnActivate() { |
| 315 dispatchEvent(Event::create(EventTypeNames::activate)); | 307 dispatchEvent(Event::create(EventTypeNames::activate)); |
| 316 } | 308 } |
| 317 | 309 |
| 318 void Sensor::notifyError(DOMException* error) { | 310 void Sensor::notifyError(DOMException* error) { |
| 319 dispatchEvent( | 311 dispatchEvent( |
| 320 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 312 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
| 321 } | 313 } |
| 322 | 314 |
| 323 } // namespace blink | 315 } // namespace blink |
| OLD | NEW |