| 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/TaskRunnerHelper.h" | 9 #include "core/dom/TaskRunnerHelper.h" |
| 10 #include "core/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
| 11 #include "core/timing/DOMWindowPerformance.h" | 11 #include "core/timing/DOMWindowPerformance.h" |
| 12 #include "core/timing/Performance.h" | 12 #include "core/timing/Performance.h" |
| 13 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 13 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| 14 #include "modules/sensor/SensorErrorEvent.h" | 14 #include "modules/sensor/SensorErrorEvent.h" |
| 15 #include "modules/sensor/SensorProviderProxy.h" | 15 #include "modules/sensor/SensorProviderProxy.h" |
| 16 | 16 |
| 17 using namespace device::mojom::blink; | 17 using namespace device::mojom::blink; |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 21 Sensor::Sensor(ExecutionContext* executionContext, | 21 Sensor::Sensor(ExecutionContext* executionContext, |
| 22 const SensorOptions& sensorOptions, | 22 const SensorOptions& sensorOptions, |
| 23 ExceptionState& exceptionState, | 23 ExceptionState& exceptionState, |
| 24 SensorType type) | 24 SensorType type) |
| 25 : ContextLifecycleObserver(executionContext), | 25 : ContextLifecycleObserver(executionContext), |
| 26 m_sensorOptions(sensorOptions), | 26 m_sensorOptions(sensorOptions), |
| 27 m_type(type), | 27 m_type(type), |
| 28 m_state(Sensor::SensorState::Unconnected), | 28 m_state(SensorState::Idle), |
| 29 m_lastUpdateTimestamp(0.0) { | 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() || |
| (...skipping 11 matching lines...) Expand all Loading... |
| 50 ConsoleMessage* consoleMessage = ConsoleMessage::create( | 50 ConsoleMessage* consoleMessage = ConsoleMessage::create( |
| 51 JSMessageSource, InfoMessageLevel, "Frequency is limited to 60 Hz."); | 51 JSMessageSource, InfoMessageLevel, "Frequency is limited to 60 Hz."); |
| 52 executionContext->addConsoleMessage(consoleMessage); | 52 executionContext->addConsoleMessage(consoleMessage); |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 Sensor::~Sensor() = default; | 57 Sensor::~Sensor() = default; |
| 58 | 58 |
| 59 void Sensor::start() { | 59 void Sensor::start() { |
| 60 if (m_state != Sensor::SensorState::Unconnected && | 60 if (m_state != Sensor::SensorState::Idle) |
| 61 m_state != Sensor::SensorState::Idle && | |
| 62 m_state != Sensor::SensorState::Errored) | |
| 63 return; | 61 return; |
| 64 | 62 |
| 65 initSensorProxyIfNeeded(); | 63 initSensorProxyIfNeeded(); |
| 66 if (!m_sensorProxy) { | 64 if (!m_sensorProxy) { |
| 67 reportError(InvalidStateError, | 65 reportError(InvalidStateError, |
| 68 "The Sensor is no longer associated to a frame."); | 66 "The Sensor is no longer associated to a frame."); |
| 69 return; | 67 return; |
| 70 } | 68 } |
| 71 | 69 |
| 72 m_lastUpdateTimestamp = WTF::monotonicallyIncreasingTime(); | 70 m_lastUpdateTimestamp = WTF::monotonicallyIncreasingTime(); |
| 73 startListening(); | 71 startListening(); |
| 74 } | 72 } |
| 75 | 73 |
| 76 void Sensor::stop() { | 74 void Sensor::stop() { |
| 77 if (m_state == Sensor::SensorState::Unconnected || | 75 if (m_state == Sensor::SensorState::Idle) |
| 78 m_state == Sensor::SensorState::Idle || | |
| 79 m_state == Sensor::SensorState::Errored) | |
| 80 return; | 76 return; |
| 81 | 77 |
| 82 stopListening(); | 78 stopListening(); |
| 83 } | 79 } |
| 84 | 80 |
| 85 static String ToString(Sensor::SensorState state) { | |
| 86 switch (state) { | |
| 87 case Sensor::SensorState::Unconnected: | |
| 88 return "unconnected"; | |
| 89 case Sensor::SensorState::Activating: | |
| 90 return "activating"; | |
| 91 case Sensor::SensorState::Activated: | |
| 92 return "activated"; | |
| 93 case Sensor::SensorState::Idle: | |
| 94 return "idle"; | |
| 95 case Sensor::SensorState::Errored: | |
| 96 return "errored"; | |
| 97 default: | |
| 98 NOTREACHED(); | |
| 99 } | |
| 100 return "idle"; | |
| 101 } | |
| 102 | |
| 103 // Getters | 81 // Getters |
| 104 String Sensor::state() const { | 82 bool Sensor::activated() const { |
| 105 return ToString(m_state); | 83 return m_state == SensorState::Activated; |
| 106 } | 84 } |
| 107 | 85 |
| 108 DOMHighResTimeStamp Sensor::timestamp(ScriptState* scriptState, | 86 DOMHighResTimeStamp Sensor::timestamp(ScriptState* scriptState, |
| 109 bool& isNull) const { | 87 bool& isNull) const { |
| 110 if (!canReturnReadings()) { | 88 if (!canReturnReadings()) { |
| 111 isNull = true; | 89 isNull = true; |
| 112 return 0.0; | 90 return 0.0; |
| 113 } | 91 } |
| 114 | 92 |
| 115 LocalDOMWindow* window = scriptState->domWindow(); | 93 LocalDOMWindow* window = scriptState->domWindow(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 128 } | 106 } |
| 129 | 107 |
| 130 DEFINE_TRACE(Sensor) { | 108 DEFINE_TRACE(Sensor) { |
| 131 visitor->trace(m_sensorProxy); | 109 visitor->trace(m_sensorProxy); |
| 132 ActiveScriptWrappable::trace(visitor); | 110 ActiveScriptWrappable::trace(visitor); |
| 133 ContextLifecycleObserver::trace(visitor); | 111 ContextLifecycleObserver::trace(visitor); |
| 134 EventTargetWithInlineData::trace(visitor); | 112 EventTargetWithInlineData::trace(visitor); |
| 135 } | 113 } |
| 136 | 114 |
| 137 bool Sensor::hasPendingActivity() const { | 115 bool Sensor::hasPendingActivity() const { |
| 138 if (m_state == Sensor::SensorState::Unconnected || | 116 if (m_state == Sensor::SensorState::Idle) |
| 139 m_state == Sensor::SensorState::Idle || | |
| 140 m_state == Sensor::SensorState::Errored) | |
| 141 return false; | 117 return false; |
| 142 return getExecutionContext() && hasEventListeners(); | 118 return getExecutionContext() && hasEventListeners(); |
| 143 } | 119 } |
| 144 | 120 |
| 145 auto Sensor::createSensorConfig() -> SensorConfigurationPtr { | 121 auto Sensor::createSensorConfig() -> SensorConfigurationPtr { |
| 146 auto result = SensorConfiguration::New(); | 122 auto result = SensorConfiguration::New(); |
| 147 | 123 |
| 148 double defaultFrequency = m_sensorProxy->defaultConfig()->frequency; | 124 double defaultFrequency = m_sensorProxy->defaultConfig()->frequency; |
| 149 double minimumFrequency = m_sensorProxy->frequencyLimits().first; | 125 double minimumFrequency = m_sensorProxy->frequencyLimits().first; |
| 150 double maximumFrequency = m_sensorProxy->frequencyLimits().second; | 126 double maximumFrequency = m_sensorProxy->frequencyLimits().second; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 } | 190 } |
| 215 } | 191 } |
| 216 | 192 |
| 217 void Sensor::onSensorError(ExceptionCode code, | 193 void Sensor::onSensorError(ExceptionCode code, |
| 218 const String& sanitizedMessage, | 194 const String& sanitizedMessage, |
| 219 const String& unsanitizedMessage) { | 195 const String& unsanitizedMessage) { |
| 220 reportError(code, sanitizedMessage, unsanitizedMessage); | 196 reportError(code, sanitizedMessage, unsanitizedMessage); |
| 221 } | 197 } |
| 222 | 198 |
| 223 void Sensor::onStartRequestCompleted(bool result) { | 199 void Sensor::onStartRequestCompleted(bool result) { |
| 224 if (m_state != Sensor::SensorState::Activating) | 200 if (m_state != SensorState::Activating) |
| 225 return; | 201 return; |
| 226 | 202 |
| 227 if (!result) { | 203 if (!result) { |
| 228 reportError( | 204 reportError(NotReadableError, "start() call has failed."); |
| 229 OperationError, | |
| 230 "start() call has failed possibly due to inappropriate options."); | |
| 231 return; | 205 return; |
| 232 } | 206 } |
| 233 | 207 |
| 234 updateState(Sensor::SensorState::Activated); | 208 updateState(Sensor::SensorState::Activated); |
| 235 } | 209 } |
| 236 | 210 |
| 237 void Sensor::startListening() { | 211 void Sensor::startListening() { |
| 238 DCHECK(m_sensorProxy); | 212 DCHECK(m_sensorProxy); |
| 239 updateState(Sensor::SensorState::Activating); | 213 updateState(Sensor::SensorState::Activating); |
| 240 | 214 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 ->postTask(BLINK_FROM_HERE, WTF::bind(&Sensor::notifyOnActivate, | 257 ->postTask(BLINK_FROM_HERE, WTF::bind(&Sensor::notifyOnActivate, |
| 284 wrapWeakPersistent(this))); | 258 wrapWeakPersistent(this))); |
| 285 } | 259 } |
| 286 | 260 |
| 287 m_state = newState; | 261 m_state = newState; |
| 288 } | 262 } |
| 289 | 263 |
| 290 void Sensor::reportError(ExceptionCode code, | 264 void Sensor::reportError(ExceptionCode code, |
| 291 const String& sanitizedMessage, | 265 const String& sanitizedMessage, |
| 292 const String& unsanitizedMessage) { | 266 const String& unsanitizedMessage) { |
| 293 updateState(Sensor::SensorState::Errored); | 267 updateState(SensorState::Idle); |
| 294 if (getExecutionContext()) { | 268 if (getExecutionContext()) { |
| 295 auto error = | 269 auto error = |
| 296 DOMException::create(code, sanitizedMessage, unsanitizedMessage); | 270 DOMException::create(code, sanitizedMessage, unsanitizedMessage); |
| 297 TaskRunnerHelper::get(TaskType::Sensor, getExecutionContext()) | 271 TaskRunnerHelper::get(TaskType::Sensor, getExecutionContext()) |
| 298 ->postTask(BLINK_FROM_HERE, | 272 ->postTask(BLINK_FROM_HERE, |
| 299 WTF::bind(&Sensor::notifyError, wrapWeakPersistent(this), | 273 WTF::bind(&Sensor::notifyError, wrapWeakPersistent(this), |
| 300 wrapPersistent(error))); | 274 wrapPersistent(error))); |
| 301 } | 275 } |
| 302 } | 276 } |
| 303 | 277 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 320 } | 294 } |
| 321 | 295 |
| 322 bool Sensor::canReturnReadings() const { | 296 bool Sensor::canReturnReadings() const { |
| 323 if (!isActivated()) | 297 if (!isActivated()) |
| 324 return false; | 298 return false; |
| 325 DCHECK(m_sensorProxy); | 299 DCHECK(m_sensorProxy); |
| 326 return m_sensorProxy->reading().timestamp != 0.0; | 300 return m_sensorProxy->reading().timestamp != 0.0; |
| 327 } | 301 } |
| 328 | 302 |
| 329 } // namespace blink | 303 } // namespace blink |
| OLD | NEW |