| 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 "core/timing/DOMWindowPerformance.h" |
| 13 #include "core/timing/Performance.h" |
| 12 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 14 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| 13 #include "modules/sensor/SensorErrorEvent.h" | 15 #include "modules/sensor/SensorErrorEvent.h" |
| 14 #include "modules/sensor/SensorProviderProxy.h" | 16 #include "modules/sensor/SensorProviderProxy.h" |
| 15 #include "modules/sensor/SensorReading.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 : ContextLifecycleObserver(executionContext), | 26 : ContextLifecycleObserver(executionContext), |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 NOTREACHED(); | 108 NOTREACHED(); |
| 108 } | 109 } |
| 109 return "idle"; | 110 return "idle"; |
| 110 } | 111 } |
| 111 | 112 |
| 112 // Getters | 113 // Getters |
| 113 String Sensor::state() const { | 114 String Sensor::state() const { |
| 114 return ToString(m_state); | 115 return ToString(m_state); |
| 115 } | 116 } |
| 116 | 117 |
| 117 SensorReading* Sensor::reading() const { | 118 DOMHighResTimeStamp Sensor::timestamp(ScriptState* scriptState, |
| 118 if (m_state != Sensor::SensorState::Activated) | 119 bool& isNull) const { |
| 119 return nullptr; | 120 if (!canReturnReadings()) { |
| 121 isNull = true; |
| 122 return 0.0; |
| 123 } |
| 124 |
| 125 LocalDOMWindow* window = scriptState->domWindow(); |
| 126 if (!window) { |
| 127 isNull = true; |
| 128 return 0.0; |
| 129 } |
| 130 |
| 131 Performance* performance = DOMWindowPerformance::performance(*window); |
| 132 DCHECK(performance); |
| 120 DCHECK(m_sensorProxy); | 133 DCHECK(m_sensorProxy); |
| 121 return m_sensorProxy->sensorReading(); | 134 isNull = false; |
| 135 |
| 136 return performance->monotonicTimeToDOMHighResTimeStamp( |
| 137 m_sensorProxy->reading().timestamp); |
| 122 } | 138 } |
| 123 | 139 |
| 124 DEFINE_TRACE(Sensor) { | 140 DEFINE_TRACE(Sensor) { |
| 125 visitor->trace(m_sensorProxy); | 141 visitor->trace(m_sensorProxy); |
| 126 ActiveScriptWrappable::trace(visitor); | 142 ActiveScriptWrappable::trace(visitor); |
| 127 ContextLifecycleObserver::trace(visitor); | 143 ContextLifecycleObserver::trace(visitor); |
| 128 EventTargetWithInlineData::trace(visitor); | 144 EventTargetWithInlineData::trace(visitor); |
| 129 } | 145 } |
| 130 | 146 |
| 131 bool Sensor::hasPendingActivity() const { | 147 bool Sensor::hasPendingActivity() const { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 145 ? m_sensorOptions.frequency() | 161 ? m_sensorOptions.frequency() |
| 146 : defaultFrequency; | 162 : defaultFrequency; |
| 147 | 163 |
| 148 if (frequency > maximumFrequency) | 164 if (frequency > maximumFrequency) |
| 149 frequency = maximumFrequency; | 165 frequency = maximumFrequency; |
| 150 | 166 |
| 151 result->frequency = frequency; | 167 result->frequency = frequency; |
| 152 return result; | 168 return result; |
| 153 } | 169 } |
| 154 | 170 |
| 171 double Sensor::readingValue(int index, bool& isNull) const { |
| 172 if (!canReturnReadings()) { |
| 173 isNull = true; |
| 174 return 0.0; |
| 175 } |
| 176 DCHECK(m_sensorProxy); |
| 177 DCHECK(index >= 0 && index < device::SensorReading::kValuesCount); |
| 178 return m_sensorProxy->reading().values[index]; |
| 179 } |
| 180 |
| 155 void Sensor::initSensorProxyIfNeeded() { | 181 void Sensor::initSensorProxyIfNeeded() { |
| 156 if (m_sensorProxy) | 182 if (m_sensorProxy) |
| 157 return; | 183 return; |
| 158 | 184 |
| 159 Document* document = toDocument(getExecutionContext()); | 185 Document* document = toDocument(getExecutionContext()); |
| 160 if (!document || !document->frame()) | 186 if (!document || !document->frame()) |
| 161 return; | 187 return; |
| 162 | 188 |
| 163 auto provider = SensorProviderProxy::from(document->frame()); | 189 auto provider = SensorProviderProxy::from(document->frame()); |
| 164 m_sensorProxy = provider->getSensorProxy(m_type); | 190 m_sensorProxy = provider->getSensorProxy(m_type); |
| 165 | 191 |
| 166 if (!m_sensorProxy) { | 192 if (!m_sensorProxy) |
| 167 m_sensorProxy = provider->createSensorProxy(m_type, document->page(), | 193 m_sensorProxy = provider->createSensorProxy(m_type, document->page()); |
| 168 createSensorReadingFactory()); | |
| 169 } | |
| 170 } | 194 } |
| 171 | 195 |
| 172 void Sensor::contextDestroyed(ExecutionContext*) { | 196 void Sensor::contextDestroyed(ExecutionContext*) { |
| 173 if (m_state == Sensor::SensorState::Activated || | 197 if (m_state == Sensor::SensorState::Activated || |
| 174 m_state == Sensor::SensorState::Activating) | 198 m_state == Sensor::SensorState::Activating) |
| 175 stopListening(); | 199 stopListening(); |
| 176 } | 200 } |
| 177 | 201 |
| 178 void Sensor::onSensorInitialized() { | 202 void Sensor::onSensorInitialized() { |
| 179 if (m_state != Sensor::SensorState::Activating) | 203 if (m_state != Sensor::SensorState::Activating) |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 DOMException::create(code, sanitizedMessage, unsanitizedMessage); | 300 DOMException::create(code, sanitizedMessage, unsanitizedMessage); |
| 277 getExecutionContext()->postTask( | 301 getExecutionContext()->postTask( |
| 278 TaskType::Sensor, BLINK_FROM_HERE, | 302 TaskType::Sensor, BLINK_FROM_HERE, |
| 279 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), | 303 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), |
| 280 wrapPersistent(error))); | 304 wrapPersistent(error))); |
| 281 } | 305 } |
| 282 } | 306 } |
| 283 | 307 |
| 284 void Sensor::notifySensorReadingChanged() { | 308 void Sensor::notifySensorReadingChanged() { |
| 285 DCHECK(m_sensorProxy); | 309 DCHECK(m_sensorProxy); |
| 286 DCHECK(m_sensorProxy->sensorReading()); | |
| 287 | 310 |
| 288 if (m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { | 311 if (m_sensorProxy->reading().timestamp != m_storedData.timestamp) { |
| 289 m_storedData = m_sensorProxy->sensorReading()->data(); | 312 m_storedData = m_sensorProxy->reading(); |
| 290 dispatchEvent(Event::create(EventTypeNames::change)); | 313 dispatchEvent(Event::create(EventTypeNames::change)); |
| 291 } | 314 } |
| 292 } | 315 } |
| 293 | 316 |
| 294 void Sensor::notifyOnActivate() { | 317 void Sensor::notifyOnActivate() { |
| 295 dispatchEvent(Event::create(EventTypeNames::activate)); | 318 dispatchEvent(Event::create(EventTypeNames::activate)); |
| 296 } | 319 } |
| 297 | 320 |
| 298 void Sensor::notifyError(DOMException* error) { | 321 void Sensor::notifyError(DOMException* error) { |
| 299 dispatchEvent( | 322 dispatchEvent( |
| 300 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 323 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
| 301 } | 324 } |
| 302 | 325 |
| 326 bool Sensor::canReturnReadings() const { |
| 327 if (m_state != Sensor::SensorState::Activated) |
| 328 return false; |
| 329 DCHECK(m_sensorProxy); |
| 330 return m_sensorProxy->reading().timestamp != 0.0; |
| 331 } |
| 332 |
| 303 } // namespace blink | 333 } // namespace blink |
| OLD | NEW |