| 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" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 Sensor::Sensor(ScriptState* scriptState, | 21 Sensor::Sensor(ScriptState* scriptState, |
| 22 const SensorOptions& sensorOptions, | 22 const SensorOptions& sensorOptions, |
| 23 ExceptionState& exceptionState, | 23 ExceptionState& exceptionState, |
| 24 SensorType type) | 24 SensorType type) |
| 25 : ActiveScriptWrappable(this), | 25 : ActiveScriptWrappable(this), |
| 26 ContextLifecycleObserver(scriptState->getExecutionContext()), | 26 ContextLifecycleObserver(scriptState->getExecutionContext()), |
| 27 PageVisibilityObserver( | 27 PageVisibilityObserver( |
| 28 toDocument(scriptState->getExecutionContext())->page()), | 28 toDocument(scriptState->getExecutionContext())->page()), |
| 29 m_sensorOptions(sensorOptions), | 29 m_sensorOptions(sensorOptions), |
| 30 m_type(type), | 30 m_type(type), |
| 31 m_state(Sensor::SensorState::IDLE) { | 31 m_state(Sensor::SensorState::Idle) { |
| 32 // Check secure context. | 32 // Check secure context. |
| 33 String errorMessage; | 33 String errorMessage; |
| 34 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) { | 34 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) { |
| 35 exceptionState.throwDOMException(SecurityError, errorMessage); | 35 exceptionState.throwDOMException(SecurityError, errorMessage); |
| 36 return; | 36 return; |
| 37 } | 37 } |
| 38 | 38 |
| 39 // Check top-level browsing context. | 39 // Check top-level browsing context. |
| 40 if (!scriptState->domWindow() || !scriptState->domWindow()->frame() || | 40 if (!scriptState->domWindow() || !scriptState->domWindow()->frame() || |
| 41 !scriptState->domWindow()->frame()->isMainFrame()) { | 41 !scriptState->domWindow()->frame()->isMainFrame()) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 57 ConsoleMessage* consoleMessage = ConsoleMessage::create( | 57 ConsoleMessage* consoleMessage = ConsoleMessage::create( |
| 58 JSMessageSource, InfoMessageLevel, "Frequency is limited to 60 Hz."); | 58 JSMessageSource, InfoMessageLevel, "Frequency is limited to 60 Hz."); |
| 59 scriptState->getExecutionContext()->addConsoleMessage(consoleMessage); | 59 scriptState->getExecutionContext()->addConsoleMessage(consoleMessage); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 Sensor::~Sensor() = default; | 64 Sensor::~Sensor() = default; |
| 65 | 65 |
| 66 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) { | 66 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) { |
| 67 if (m_state != Sensor::SensorState::IDLE && | 67 if (m_state != Sensor::SensorState::Idle && |
| 68 m_state != Sensor::SensorState::ERRORED) { | 68 m_state != Sensor::SensorState::Errored) { |
| 69 exceptionState.throwDOMException( | 69 exceptionState.throwDOMException( |
| 70 InvalidStateError, | 70 InvalidStateError, |
| 71 "Cannot start because SensorState is not idle or errored"); | 71 "Cannot start because SensorState is not Idle or errored"); |
| 72 return; | 72 return; |
| 73 } | 73 } |
| 74 | 74 |
| 75 initSensorProxyIfNeeded(); | 75 initSensorProxyIfNeeded(); |
| 76 | 76 |
| 77 if (!m_sensorProxy) { | 77 if (!m_sensorProxy) { |
| 78 exceptionState.throwDOMException( | 78 exceptionState.throwDOMException( |
| 79 InvalidStateError, "The Sensor is no longer associated to a frame."); | 79 InvalidStateError, "The Sensor is no longer associated to a frame."); |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 | 82 |
| 83 startListening(); | 83 startListening(); |
| 84 } | 84 } |
| 85 | 85 |
| 86 void Sensor::stop(ScriptState*, ExceptionState& exceptionState) { | 86 void Sensor::stop(ScriptState*, ExceptionState& exceptionState) { |
| 87 if (m_state == Sensor::SensorState::IDLE || | 87 if (m_state == Sensor::SensorState::Idle || |
| 88 m_state == Sensor::SensorState::ERRORED) { | 88 m_state == Sensor::SensorState::Errored) { |
| 89 exceptionState.throwDOMException( | 89 exceptionState.throwDOMException( |
| 90 InvalidStateError, | 90 InvalidStateError, |
| 91 "Cannot stop because SensorState is either idle or errored"); | 91 "Cannot stop because SensorState is either Idle or errored"); |
| 92 return; | 92 return; |
| 93 } | 93 } |
| 94 | 94 |
| 95 stopListening(); | 95 stopListening(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 static String ToString(Sensor::SensorState state) { | 98 static String ToString(Sensor::SensorState state) { |
| 99 switch (state) { | 99 switch (state) { |
| 100 case Sensor::SensorState::IDLE: | 100 case Sensor::SensorState::Idle: |
| 101 return "idle"; | 101 return "idle"; |
| 102 case Sensor::SensorState::ACTIVATING: | 102 case Sensor::SensorState::Activating: |
| 103 return "activating"; | 103 return "activating"; |
| 104 case Sensor::SensorState::ACTIVE: | 104 case Sensor::SensorState::Active: |
| 105 return "active"; | 105 return "active"; |
| 106 case Sensor::SensorState::ERRORED: | 106 case Sensor::SensorState::Errored: |
| 107 return "errored"; | 107 return "errored"; |
| 108 default: | 108 default: |
| 109 NOTREACHED(); | 109 NOTREACHED(); |
| 110 } | 110 } |
| 111 return "idle"; | 111 return "idle"; |
| 112 } | 112 } |
| 113 | 113 |
| 114 // Getters | 114 // Getters |
| 115 String Sensor::state() const { | 115 String Sensor::state() const { |
| 116 return ToString(m_state); | 116 return ToString(m_state); |
| 117 } | 117 } |
| 118 | 118 |
| 119 SensorReading* Sensor::reading() const { | 119 SensorReading* Sensor::reading() const { |
| 120 if (m_state != Sensor::SensorState::ACTIVE) | 120 if (m_state != Sensor::SensorState::Active) |
| 121 return nullptr; | 121 return nullptr; |
| 122 DCHECK(m_sensorProxy); | 122 DCHECK(m_sensorProxy); |
| 123 return m_sensorProxy->sensorReading(); | 123 return m_sensorProxy->sensorReading(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 DEFINE_TRACE(Sensor) { | 126 DEFINE_TRACE(Sensor) { |
| 127 visitor->trace(m_sensorProxy); | 127 visitor->trace(m_sensorProxy); |
| 128 ActiveScriptWrappable::trace(visitor); | 128 ActiveScriptWrappable::trace(visitor); |
| 129 ContextLifecycleObserver::trace(visitor); | 129 ContextLifecycleObserver::trace(visitor); |
| 130 PageVisibilityObserver::trace(visitor); | 130 PageVisibilityObserver::trace(visitor); |
| 131 EventTargetWithInlineData::trace(visitor); | 131 EventTargetWithInlineData::trace(visitor); |
| 132 } | 132 } |
| 133 | 133 |
| 134 bool Sensor::hasPendingActivity() const { | 134 bool Sensor::hasPendingActivity() const { |
| 135 if (m_state == Sensor::SensorState::IDLE || | 135 if (m_state == Sensor::SensorState::Idle || |
| 136 m_state == Sensor::SensorState::ERRORED) | 136 m_state == Sensor::SensorState::Errored) |
| 137 return false; | 137 return false; |
| 138 return hasEventListeners(); | 138 return hasEventListeners(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 auto Sensor::createSensorConfig() -> SensorConfigurationPtr { | 141 auto Sensor::createSensorConfig() -> SensorConfigurationPtr { |
| 142 auto result = SensorConfiguration::New(); | 142 auto result = SensorConfiguration::New(); |
| 143 | 143 |
| 144 double defaultFrequency = m_sensorProxy->defaultConfig()->frequency; | 144 double defaultFrequency = m_sensorProxy->defaultConfig()->frequency; |
| 145 double maximumFrequency = m_sensorProxy->maximumFrequency(); | 145 double maximumFrequency = m_sensorProxy->maximumFrequency(); |
| 146 | 146 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 166 auto provider = SensorProviderProxy::from(document->frame()); | 166 auto provider = SensorProviderProxy::from(document->frame()); |
| 167 m_sensorProxy = provider->getSensor(m_type); | 167 m_sensorProxy = provider->getSensor(m_type); |
| 168 | 168 |
| 169 if (!m_sensorProxy) { | 169 if (!m_sensorProxy) { |
| 170 m_sensorProxy = | 170 m_sensorProxy = |
| 171 provider->createSensor(m_type, createSensorReadingFactory()); | 171 provider->createSensor(m_type, createSensorReadingFactory()); |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 | 174 |
| 175 void Sensor::contextDestroyed() { | 175 void Sensor::contextDestroyed() { |
| 176 if (m_state == Sensor::SensorState::ACTIVE || | 176 if (m_state == Sensor::SensorState::Active || |
| 177 m_state == Sensor::SensorState::ACTIVATING) | 177 m_state == Sensor::SensorState::Activating) |
| 178 stopListening(); | 178 stopListening(); |
| 179 } | 179 } |
| 180 | 180 |
| 181 void Sensor::onSensorInitialized() { | 181 void Sensor::onSensorInitialized() { |
| 182 if (m_state != Sensor::SensorState::ACTIVATING) | 182 if (m_state != Sensor::SensorState::Activating) |
| 183 return; | 183 return; |
| 184 | 184 |
| 185 startListening(); | 185 startListening(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void Sensor::onSensorReadingChanged() { | 188 void Sensor::onSensorReadingChanged() { |
| 189 if (m_polling) | 189 if (m_polling) |
| 190 m_polling->onSensorReadingChanged(); | 190 m_polling->onSensorReadingChanged(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void Sensor::onSensorError(ExceptionCode code, | 193 void Sensor::onSensorError(ExceptionCode code, |
| 194 const String& sanitizedMessage, | 194 const String& sanitizedMessage, |
| 195 const String& unsanitizedMessage) { | 195 const String& unsanitizedMessage) { |
| 196 reportError(code, sanitizedMessage, unsanitizedMessage); | 196 reportError(code, sanitizedMessage, unsanitizedMessage); |
| 197 } | 197 } |
| 198 | 198 |
| 199 void Sensor::onStartRequestCompleted(bool result) { | 199 void Sensor::onStartRequestCompleted(bool result) { |
| 200 if (m_state != Sensor::SensorState::ACTIVATING) | 200 if (m_state != Sensor::SensorState::Activating) |
| 201 return; | 201 return; |
| 202 | 202 |
| 203 if (!result) { | 203 if (!result) { |
| 204 reportError( | 204 reportError( |
| 205 OperationError, | 205 OperationError, |
| 206 "start() call has failed possibly due to inappropriate options."); | 206 "start() call has failed possibly due to inappropriate options."); |
| 207 return; | 207 return; |
| 208 } | 208 } |
| 209 | 209 |
| 210 DCHECK(m_configuration); | 210 DCHECK(m_configuration); |
| 211 DCHECK(m_sensorProxy); | 211 DCHECK(m_sensorProxy); |
| 212 auto pollCallback = WTF::bind(&Sensor::pollForData, wrapWeakPersistent(this)); | 212 auto pollCallback = WTF::bind(&Sensor::pollForData, wrapWeakPersistent(this)); |
| 213 DCHECK_GT(m_configuration->frequency, 0); | 213 DCHECK_GT(m_configuration->frequency, 0); |
| 214 m_polling = SensorPollingStrategy::create(1 / m_configuration->frequency, | 214 m_polling = SensorPollingStrategy::create(1 / m_configuration->frequency, |
| 215 std::move(pollCallback), | 215 std::move(pollCallback), |
| 216 m_sensorProxy->reportingMode()); | 216 m_sensorProxy->reportingMode()); |
| 217 updateState(Sensor::SensorState::ACTIVE); | 217 updateState(Sensor::SensorState::Active); |
| 218 } | 218 } |
| 219 | 219 |
| 220 void Sensor::onStopRequestCompleted(bool result) { | 220 void Sensor::onStopRequestCompleted(bool result) { |
| 221 if (m_state == Sensor::SensorState::IDLE) | 221 if (m_state == Sensor::SensorState::Idle) |
| 222 return; | 222 return; |
| 223 | 223 |
| 224 if (!result) | 224 if (!result) |
| 225 reportError(OperationError); | 225 reportError(OperationError); |
| 226 | 226 |
| 227 DCHECK(m_sensorProxy); | 227 DCHECK(m_sensorProxy); |
| 228 m_sensorProxy->removeObserver(this); | 228 m_sensorProxy->removeObserver(this); |
| 229 } | 229 } |
| 230 | 230 |
| 231 void Sensor::pageVisibilityChanged() { | 231 void Sensor::pageVisibilityChanged() { |
| 232 updatePollingStatus(); | 232 updatePollingStatus(); |
| 233 | 233 |
| 234 if (!m_sensorProxy || !m_sensorProxy->isInitialized()) | 234 if (!m_sensorProxy || !m_sensorProxy->isInitialized()) |
| 235 return; | 235 return; |
| 236 | 236 |
| 237 if (page()->visibilityState() != PageVisibilityStateVisible) { | 237 if (page()->visibilityState() != PageVisibilityStateVisible) { |
| 238 m_sensorProxy->suspend(); | 238 m_sensorProxy->suspend(); |
| 239 } else { | 239 } else { |
| 240 m_sensorProxy->resume(); | 240 m_sensorProxy->resume(); |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 | 243 |
| 244 void Sensor::startListening() { | 244 void Sensor::startListening() { |
| 245 DCHECK(m_sensorProxy); | 245 DCHECK(m_sensorProxy); |
| 246 updateState(Sensor::SensorState::ACTIVATING); | 246 updateState(Sensor::SensorState::Activating); |
| 247 | 247 |
| 248 m_sensorProxy->addObserver(this); | 248 m_sensorProxy->addObserver(this); |
| 249 if (!m_sensorProxy->isInitialized()) { | 249 if (!m_sensorProxy->isInitialized()) { |
| 250 m_sensorProxy->initialize(); | 250 m_sensorProxy->initialize(); |
| 251 return; | 251 return; |
| 252 } | 252 } |
| 253 | 253 |
| 254 if (!m_configuration) { | 254 if (!m_configuration) { |
| 255 m_configuration = createSensorConfig(); | 255 m_configuration = createSensorConfig(); |
| 256 DCHECK(m_configuration); | 256 DCHECK(m_configuration); |
| 257 DCHECK(m_configuration->frequency > 0 && | 257 DCHECK(m_configuration->frequency > 0 && |
| 258 m_configuration->frequency <= m_sensorProxy->maximumFrequency()); | 258 m_configuration->frequency <= m_sensorProxy->maximumFrequency()); |
| 259 } | 259 } |
| 260 | 260 |
| 261 auto startCallback = | 261 auto startCallback = |
| 262 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); | 262 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); |
| 263 m_sensorProxy->addConfiguration(m_configuration->Clone(), | 263 m_sensorProxy->addConfiguration(m_configuration->Clone(), |
| 264 std::move(startCallback)); | 264 std::move(startCallback)); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void Sensor::stopListening() { | 267 void Sensor::stopListening() { |
| 268 DCHECK(m_sensorProxy); | 268 DCHECK(m_sensorProxy); |
| 269 updateState(Sensor::SensorState::IDLE); | 269 updateState(Sensor::SensorState::Idle); |
| 270 | 270 |
| 271 if (m_sensorProxy->isInitialized()) { | 271 if (m_sensorProxy->isInitialized()) { |
| 272 auto callback = | 272 auto callback = |
| 273 WTF::bind(&Sensor::onStopRequestCompleted, wrapWeakPersistent(this)); | 273 WTF::bind(&Sensor::onStopRequestCompleted, wrapWeakPersistent(this)); |
| 274 DCHECK(m_configuration); | 274 DCHECK(m_configuration); |
| 275 m_sensorProxy->removeConfiguration(m_configuration->Clone(), | 275 m_sensorProxy->removeConfiguration(m_configuration->Clone(), |
| 276 std::move(callback)); | 276 std::move(callback)); |
| 277 } else { | 277 } else { |
| 278 m_sensorProxy->removeObserver(this); | 278 m_sensorProxy->removeObserver(this); |
| 279 } | 279 } |
| 280 } | 280 } |
| 281 | 281 |
| 282 void Sensor::pollForData() { | 282 void Sensor::pollForData() { |
| 283 if (m_state != Sensor::SensorState::ACTIVE) { | 283 if (m_state != Sensor::SensorState::Active) { |
| 284 DCHECK(m_polling); | 284 DCHECK(m_polling); |
| 285 m_polling->stopPolling(); | 285 m_polling->stopPolling(); |
| 286 return; | 286 return; |
| 287 } | 287 } |
| 288 | 288 |
| 289 DCHECK(m_sensorProxy); | 289 DCHECK(m_sensorProxy); |
| 290 DCHECK(m_sensorProxy->isInitialized()); | 290 DCHECK(m_sensorProxy->isInitialized()); |
| 291 m_sensorProxy->updateSensorReading(); | 291 m_sensorProxy->updateSensorReading(); |
| 292 | 292 |
| 293 DCHECK(m_sensorProxy->sensorReading()); | 293 DCHECK(m_sensorProxy->sensorReading()); |
| 294 if (getExecutionContext() && | 294 if (getExecutionContext() && |
| 295 m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { | 295 m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
| 296 getExecutionContext()->postTask( | 296 getExecutionContext()->postTask( |
| 297 BLINK_FROM_HERE, | 297 BLINK_FROM_HERE, |
| 298 createSameThreadTask(&Sensor::notifySensorReadingChanged, | 298 createSameThreadTask(&Sensor::notifySensorReadingChanged, |
| 299 wrapWeakPersistent(this))); | 299 wrapWeakPersistent(this))); |
| 300 } | 300 } |
| 301 | 301 |
| 302 m_storedData = m_sensorProxy->sensorReading()->data(); | 302 m_storedData = m_sensorProxy->sensorReading()->data(); |
| 303 } | 303 } |
| 304 | 304 |
| 305 void Sensor::updateState(Sensor::SensorState newState) { | 305 void Sensor::updateState(Sensor::SensorState newState) { |
| 306 if (newState == m_state) | 306 if (newState == m_state) |
| 307 return; | 307 return; |
| 308 m_state = newState; | 308 |
| 309 if (getExecutionContext()) { | 309 if (newState == SensorState::Active && getExecutionContext()) { |
| 310 DCHECK_EQ(SensorState::Activating, m_state); |
| 310 getExecutionContext()->postTask( | 311 getExecutionContext()->postTask( |
| 311 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyStateChanged, | 312 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyOnActivate, |
| 312 wrapWeakPersistent(this))); | 313 wrapWeakPersistent(this))); |
| 313 } | 314 } |
| 314 | 315 |
| 316 m_state = newState; |
| 315 updatePollingStatus(); | 317 updatePollingStatus(); |
| 316 } | 318 } |
| 317 | 319 |
| 318 void Sensor::reportError(ExceptionCode code, | 320 void Sensor::reportError(ExceptionCode code, |
| 319 const String& sanitizedMessage, | 321 const String& sanitizedMessage, |
| 320 const String& unsanitizedMessage) { | 322 const String& unsanitizedMessage) { |
| 321 updateState(Sensor::SensorState::ERRORED); | 323 updateState(Sensor::SensorState::Errored); |
| 322 if (getExecutionContext()) { | 324 if (getExecutionContext()) { |
| 323 auto error = | 325 auto error = |
| 324 DOMException::create(code, sanitizedMessage, unsanitizedMessage); | 326 DOMException::create(code, sanitizedMessage, unsanitizedMessage); |
| 325 getExecutionContext()->postTask( | 327 getExecutionContext()->postTask( |
| 326 BLINK_FROM_HERE, | 328 BLINK_FROM_HERE, |
| 327 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), | 329 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), |
| 328 wrapPersistent(error))); | 330 wrapPersistent(error))); |
| 329 } | 331 } |
| 330 } | 332 } |
| 331 | 333 |
| 332 void Sensor::updatePollingStatus() { | 334 void Sensor::updatePollingStatus() { |
| 333 if (!m_polling) | 335 if (!m_polling) |
| 334 return; | 336 return; |
| 335 | 337 |
| 336 if (m_state != Sensor::SensorState::ACTIVE || | 338 if (m_state != Sensor::SensorState::Active || |
| 337 page()->visibilityState() != PageVisibilityStateVisible) { | 339 page()->visibilityState() != PageVisibilityStateVisible) { |
| 338 m_polling->stopPolling(); | 340 m_polling->stopPolling(); |
| 339 } else { | 341 } else { |
| 340 m_polling->startPolling(); | 342 m_polling->startPolling(); |
| 341 } | 343 } |
| 342 } | 344 } |
| 343 | 345 |
| 344 void Sensor::notifySensorReadingChanged() { | 346 void Sensor::notifySensorReadingChanged() { |
| 345 dispatchEvent(Event::create(EventTypeNames::change)); | 347 dispatchEvent(Event::create(EventTypeNames::change)); |
| 346 } | 348 } |
| 347 | 349 |
| 348 void Sensor::notifyStateChanged() { | 350 void Sensor::notifyOnActivate() { |
| 349 dispatchEvent(Event::create(EventTypeNames::statechange)); | 351 dispatchEvent(Event::create(EventTypeNames::activate)); |
| 350 } | 352 } |
| 351 | 353 |
| 352 void Sensor::notifyError(DOMException* error) { | 354 void Sensor::notifyError(DOMException* error) { |
| 353 dispatchEvent( | 355 dispatchEvent( |
| 354 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 356 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
| 355 } | 357 } |
| 356 | 358 |
| 357 } // namespace blink | 359 } // namespace blink |
| OLD | NEW |