| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 92 |
| 93 stopListening(); | 93 stopListening(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 static String ToString(Sensor::SensorState state) { | 96 static String ToString(Sensor::SensorState state) { |
| 97 switch (state) { | 97 switch (state) { |
| 98 case Sensor::SensorState::Idle: | 98 case Sensor::SensorState::Idle: |
| 99 return "idle"; | 99 return "idle"; |
| 100 case Sensor::SensorState::Activating: | 100 case Sensor::SensorState::Activating: |
| 101 return "activating"; | 101 return "activating"; |
| 102 case Sensor::SensorState::Active: | 102 case Sensor::SensorState::Activated: |
| 103 return "active"; | 103 return "activated"; |
| 104 case Sensor::SensorState::Errored: | 104 case Sensor::SensorState::Errored: |
| 105 return "errored"; | 105 return "errored"; |
| 106 default: | 106 default: |
| 107 NOTREACHED(); | 107 NOTREACHED(); |
| 108 } | 108 } |
| 109 return "idle"; | 109 return "idle"; |
| 110 } | 110 } |
| 111 | 111 |
| 112 // Getters | 112 // Getters |
| 113 String Sensor::state() const { | 113 String Sensor::state() const { |
| 114 return ToString(m_state); | 114 return ToString(m_state); |
| 115 } | 115 } |
| 116 | 116 |
| 117 SensorReading* Sensor::reading() const { | 117 SensorReading* Sensor::reading() const { |
| 118 if (m_state != Sensor::SensorState::Active) | 118 if (m_state != Sensor::SensorState::Activated) |
| 119 return nullptr; | 119 return nullptr; |
| 120 DCHECK(m_sensorProxy); | 120 DCHECK(m_sensorProxy); |
| 121 return m_sensorProxy->sensorReading(); | 121 return m_sensorProxy->sensorReading(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 DEFINE_TRACE(Sensor) { | 124 DEFINE_TRACE(Sensor) { |
| 125 visitor->trace(m_sensorProxy); | 125 visitor->trace(m_sensorProxy); |
| 126 ActiveScriptWrappable::trace(visitor); | 126 ActiveScriptWrappable::trace(visitor); |
| 127 ContextLifecycleObserver::trace(visitor); | 127 ContextLifecycleObserver::trace(visitor); |
| 128 EventTargetWithInlineData::trace(visitor); | 128 EventTargetWithInlineData::trace(visitor); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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->page(), |
| 168 createSensorReadingFactory()); | 168 createSensorReadingFactory()); |
| 169 } | 169 } |
| 170 } | 170 } |
| 171 | 171 |
| 172 void Sensor::contextDestroyed() { | 172 void Sensor::contextDestroyed() { |
| 173 if (m_state == Sensor::SensorState::Active || | 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() { |
| 186 if (m_state == Sensor::SensorState::Active) { | 186 if (m_state == Sensor::SensorState::Activated) { |
| 187 DCHECK(m_sensorUpdateNotifier); | 187 DCHECK(m_sensorUpdateNotifier); |
| 188 m_sensorUpdateNotifier->onSensorReadingChanged(); | 188 m_sensorUpdateNotifier->onSensorReadingChanged(); |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 | 191 |
| 192 void Sensor::onSensorError(ExceptionCode code, | 192 void Sensor::onSensorError(ExceptionCode code, |
| 193 const String& sanitizedMessage, | 193 const String& sanitizedMessage, |
| 194 const String& unsanitizedMessage) { | 194 const String& unsanitizedMessage) { |
| 195 reportError(code, sanitizedMessage, unsanitizedMessage); | 195 reportError(code, sanitizedMessage, unsanitizedMessage); |
| 196 if (m_sensorUpdateNotifier) | 196 if (m_sensorUpdateNotifier) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 208 return; | 208 return; |
| 209 } | 209 } |
| 210 | 210 |
| 211 DCHECK(m_configuration); | 211 DCHECK(m_configuration); |
| 212 DCHECK(m_sensorProxy); | 212 DCHECK(m_sensorProxy); |
| 213 auto updateCallback = | 213 auto updateCallback = |
| 214 WTF::bind(&Sensor::onSensorUpdateNotification, wrapWeakPersistent(this)); | 214 WTF::bind(&Sensor::onSensorUpdateNotification, wrapWeakPersistent(this)); |
| 215 DCHECK_GT(m_configuration->frequency, 0); | 215 DCHECK_GT(m_configuration->frequency, 0); |
| 216 m_sensorUpdateNotifier = SensorUpdateNotificationStrategy::create( | 216 m_sensorUpdateNotifier = SensorUpdateNotificationStrategy::create( |
| 217 m_configuration->frequency, std::move(updateCallback)); | 217 m_configuration->frequency, std::move(updateCallback)); |
| 218 updateState(Sensor::SensorState::Active); | 218 updateState(Sensor::SensorState::Activated); |
| 219 } | 219 } |
| 220 | 220 |
| 221 void Sensor::startListening() { | 221 void Sensor::startListening() { |
| 222 DCHECK(m_sensorProxy); | 222 DCHECK(m_sensorProxy); |
| 223 updateState(Sensor::SensorState::Activating); | 223 updateState(Sensor::SensorState::Activating); |
| 224 | 224 |
| 225 m_sensorProxy->addObserver(this); | 225 m_sensorProxy->addObserver(this); |
| 226 if (!m_sensorProxy->isInitialized()) { | 226 if (!m_sensorProxy->isInitialized()) { |
| 227 m_sensorProxy->initialize(); | 227 m_sensorProxy->initialize(); |
| 228 return; | 228 return; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 249 m_sensorUpdateNotifier->cancelPendingNotifications(); | 249 m_sensorUpdateNotifier->cancelPendingNotifications(); |
| 250 | 250 |
| 251 if (m_sensorProxy->isInitialized()) { | 251 if (m_sensorProxy->isInitialized()) { |
| 252 DCHECK(m_configuration); | 252 DCHECK(m_configuration); |
| 253 m_sensorProxy->removeConfiguration(m_configuration->Clone()); | 253 m_sensorProxy->removeConfiguration(m_configuration->Clone()); |
| 254 } | 254 } |
| 255 m_sensorProxy->removeObserver(this); | 255 m_sensorProxy->removeObserver(this); |
| 256 } | 256 } |
| 257 | 257 |
| 258 void Sensor::onSensorUpdateNotification() { | 258 void Sensor::onSensorUpdateNotification() { |
| 259 if (m_state != Sensor::SensorState::Active) | 259 if (m_state != Sensor::SensorState::Activated) |
| 260 return; | 260 return; |
| 261 | 261 |
| 262 DCHECK(m_sensorProxy); | 262 DCHECK(m_sensorProxy); |
| 263 DCHECK(m_sensorProxy->isInitialized()); | 263 DCHECK(m_sensorProxy->isInitialized()); |
| 264 DCHECK(m_sensorProxy->sensorReading()); | 264 DCHECK(m_sensorProxy->sensorReading()); |
| 265 | 265 |
| 266 if (getExecutionContext() && | 266 if (getExecutionContext() && |
| 267 m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { | 267 m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
| 268 getExecutionContext()->postTask( | 268 getExecutionContext()->postTask( |
| 269 BLINK_FROM_HERE, | 269 BLINK_FROM_HERE, |
| 270 createSameThreadTask(&Sensor::notifySensorReadingChanged, | 270 createSameThreadTask(&Sensor::notifySensorReadingChanged, |
| 271 wrapWeakPersistent(this))); | 271 wrapWeakPersistent(this))); |
| 272 } | 272 } |
| 273 | 273 |
| 274 m_storedData = m_sensorProxy->sensorReading()->data(); | 274 m_storedData = m_sensorProxy->sensorReading()->data(); |
| 275 } | 275 } |
| 276 | 276 |
| 277 void Sensor::updateState(Sensor::SensorState newState) { | 277 void Sensor::updateState(Sensor::SensorState newState) { |
| 278 if (newState == m_state) | 278 if (newState == m_state) |
| 279 return; | 279 return; |
| 280 | 280 |
| 281 if (newState == SensorState::Active && getExecutionContext()) { | 281 if (newState == SensorState::Activated && getExecutionContext()) { |
| 282 DCHECK_EQ(SensorState::Activating, m_state); | 282 DCHECK_EQ(SensorState::Activating, m_state); |
| 283 getExecutionContext()->postTask( | 283 getExecutionContext()->postTask( |
| 284 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyOnActivate, | 284 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyOnActivate, |
| 285 wrapWeakPersistent(this))); | 285 wrapWeakPersistent(this))); |
| 286 } | 286 } |
| 287 | 287 |
| 288 m_state = newState; | 288 m_state = newState; |
| 289 } | 289 } |
| 290 | 290 |
| 291 void Sensor::reportError(ExceptionCode code, | 291 void Sensor::reportError(ExceptionCode code, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 314 void Sensor::notifyOnActivate() { | 314 void Sensor::notifyOnActivate() { |
| 315 dispatchEvent(Event::create(EventTypeNames::activate)); | 315 dispatchEvent(Event::create(EventTypeNames::activate)); |
| 316 } | 316 } |
| 317 | 317 |
| 318 void Sensor::notifyError(DOMException* error) { | 318 void Sensor::notifyError(DOMException* error) { |
| 319 dispatchEvent( | 319 dispatchEvent( |
| 320 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 320 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
| 321 } | 321 } |
| 322 | 322 |
| 323 } // namespace blink | 323 } // namespace blink |
| OLD | NEW |