| 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" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 if (frequency > maximumFrequency) | 156 if (frequency > maximumFrequency) |
| 157 frequency = maximumFrequency; | 157 frequency = maximumFrequency; |
| 158 if (frequency < minimumFrequency) | 158 if (frequency < minimumFrequency) |
| 159 frequency = minimumFrequency; | 159 frequency = minimumFrequency; |
| 160 | 160 |
| 161 result->frequency = frequency; | 161 result->frequency = frequency; |
| 162 return result; | 162 return result; |
| 163 } | 163 } |
| 164 | 164 |
| 165 double Sensor::readingValue(int index, bool& isNull) const { | 165 double Sensor::readingValue(int index, bool& isNull) const { |
| 166 if (!canReturnReadings()) { | 166 isNull = !canReturnReadings(); |
| 167 isNull = true; | 167 return isNull ? 0.0 : readingValueUnchecked(index); |
| 168 return 0.0; | 168 } |
| 169 } | 169 |
| 170 double Sensor::readingValueUnchecked(int index) const { |
| 170 DCHECK(m_sensorProxy); | 171 DCHECK(m_sensorProxy); |
| 171 DCHECK(index >= 0 && index < device::SensorReading::kValuesCount); | 172 DCHECK(index >= 0 && index < device::SensorReading::kValuesCount); |
| 172 return m_sensorProxy->reading().values[index]; | 173 return m_sensorProxy->reading().values[index]; |
| 173 } | 174 } |
| 174 | 175 |
| 175 void Sensor::initSensorProxyIfNeeded() { | 176 void Sensor::initSensorProxyIfNeeded() { |
| 176 if (m_sensorProxy) | 177 if (m_sensorProxy) |
| 177 return; | 178 return; |
| 178 | 179 |
| 179 Document* document = toDocument(getExecutionContext()); | 180 Document* document = toDocument(getExecutionContext()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 193 stopListening(); | 194 stopListening(); |
| 194 } | 195 } |
| 195 | 196 |
| 196 void Sensor::onSensorInitialized() { | 197 void Sensor::onSensorInitialized() { |
| 197 if (m_state != Sensor::SensorState::Activating) | 198 if (m_state != Sensor::SensorState::Activating) |
| 198 return; | 199 return; |
| 199 | 200 |
| 200 startListening(); | 201 startListening(); |
| 201 } | 202 } |
| 202 | 203 |
| 203 void Sensor::onSensorReadingChanged(double timestamp) { | 204 void Sensor::notifySensorChanged(double timestamp) { |
| 204 if (m_state != Sensor::SensorState::Activated) | 205 if (m_state != Sensor::SensorState::Activated) |
| 205 return; | 206 return; |
| 206 | 207 |
| 207 DCHECK_GT(m_configuration->frequency, 0.0); | 208 DCHECK_GT(m_configuration->frequency, 0.0); |
| 208 double period = 1 / m_configuration->frequency; | 209 double period = 1 / m_configuration->frequency; |
| 210 |
| 209 if (timestamp - m_lastUpdateTimestamp >= period) { | 211 if (timestamp - m_lastUpdateTimestamp >= period) { |
| 210 m_lastUpdateTimestamp = timestamp; | 212 m_lastUpdateTimestamp = timestamp; |
| 211 notifySensorReadingChanged(); | 213 notifySensorReadingChanged(); |
| 212 } | 214 } |
| 213 } | 215 } |
| 214 | 216 |
| 215 void Sensor::onSensorError(ExceptionCode code, | 217 void Sensor::onSensorError(ExceptionCode code, |
| 216 const String& sanitizedMessage, | 218 const String& sanitizedMessage, |
| 217 const String& unsanitizedMessage) { | 219 const String& unsanitizedMessage) { |
| 218 reportError(code, sanitizedMessage, unsanitizedMessage); | 220 reportError(code, sanitizedMessage, unsanitizedMessage); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 229 return; | 231 return; |
| 230 } | 232 } |
| 231 | 233 |
| 232 updateState(Sensor::SensorState::Activated); | 234 updateState(Sensor::SensorState::Activated); |
| 233 } | 235 } |
| 234 | 236 |
| 235 void Sensor::startListening() { | 237 void Sensor::startListening() { |
| 236 DCHECK(m_sensorProxy); | 238 DCHECK(m_sensorProxy); |
| 237 updateState(Sensor::SensorState::Activating); | 239 updateState(Sensor::SensorState::Activating); |
| 238 | 240 |
| 239 m_sensorProxy->addObserver(this); | 241 m_sensorProxy->addClient(this); |
| 240 if (!m_sensorProxy->isInitialized()) { | 242 if (!m_sensorProxy->isInitialized()) { |
| 241 m_sensorProxy->initialize(); | 243 m_sensorProxy->initialize(); |
| 242 return; | 244 return; |
| 243 } | 245 } |
| 244 | 246 |
| 245 if (!m_configuration) { | 247 if (!m_configuration) { |
| 246 m_configuration = createSensorConfig(); | 248 m_configuration = createSensorConfig(); |
| 247 DCHECK(m_configuration); | 249 DCHECK(m_configuration); |
| 248 DCHECK(m_configuration->frequency > 0 && | 250 DCHECK(m_configuration->frequency > 0 && |
| 249 m_configuration->frequency <= | 251 m_configuration->frequency <= |
| 250 SensorConfiguration::kMaxAllowedFrequency); | 252 SensorConfiguration::kMaxAllowedFrequency); |
| 251 } | 253 } |
| 252 | 254 |
| 253 auto startCallback = | 255 auto startCallback = |
| 254 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); | 256 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); |
| 255 m_sensorProxy->addConfiguration(m_configuration->Clone(), | 257 m_sensorProxy->addConfiguration(m_configuration->Clone(), |
| 256 std::move(startCallback)); | 258 std::move(startCallback)); |
| 257 } | 259 } |
| 258 | 260 |
| 259 void Sensor::stopListening() { | 261 void Sensor::stopListening() { |
| 260 DCHECK(m_sensorProxy); | 262 DCHECK(m_sensorProxy); |
| 261 updateState(Sensor::SensorState::Idle); | 263 updateState(Sensor::SensorState::Idle); |
| 262 | 264 |
| 263 if (m_sensorProxy->isInitialized()) { | 265 if (m_sensorProxy->isInitialized()) { |
| 264 DCHECK(m_configuration); | 266 DCHECK(m_configuration); |
| 265 m_sensorProxy->removeConfiguration(m_configuration->Clone()); | 267 m_sensorProxy->removeConfiguration(m_configuration->Clone()); |
| 266 } | 268 } |
| 267 m_sensorProxy->removeObserver(this); | 269 m_sensorProxy->removeClient(this); |
| 268 } | 270 } |
| 269 | 271 |
| 270 void Sensor::updateState(Sensor::SensorState newState) { | 272 void Sensor::updateState(Sensor::SensorState newState) { |
| 271 if (newState == m_state) | 273 if (newState == m_state) |
| 272 return; | 274 return; |
| 273 | 275 |
| 274 if (newState == SensorState::Activated && getExecutionContext()) { | 276 if (newState == SensorState::Activated && getExecutionContext()) { |
| 275 DCHECK_EQ(SensorState::Activating, m_state); | 277 DCHECK_EQ(SensorState::Activating, m_state); |
| 276 // The initial value for m_lastUpdateTimestamp is set to current time, | 278 // The initial value for m_lastUpdateTimestamp is set to current time, |
| 277 // so that the first reading update will be notified considering the given | 279 // so that the first reading update will be notified considering the given |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 void Sensor::notifyOnActivate() { | 313 void Sensor::notifyOnActivate() { |
| 312 dispatchEvent(Event::create(EventTypeNames::activate)); | 314 dispatchEvent(Event::create(EventTypeNames::activate)); |
| 313 } | 315 } |
| 314 | 316 |
| 315 void Sensor::notifyError(DOMException* error) { | 317 void Sensor::notifyError(DOMException* error) { |
| 316 dispatchEvent( | 318 dispatchEvent( |
| 317 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 319 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
| 318 } | 320 } |
| 319 | 321 |
| 320 bool Sensor::canReturnReadings() const { | 322 bool Sensor::canReturnReadings() const { |
| 321 if (m_state != Sensor::SensorState::Activated) | 323 if (!isActivated()) |
| 322 return false; | 324 return false; |
| 323 DCHECK(m_sensorProxy); | 325 DCHECK(m_sensorProxy); |
| 324 return m_sensorProxy->reading().timestamp != 0.0; | 326 return m_sensorProxy->reading().timestamp != 0.0; |
| 325 } | 327 } |
| 326 | 328 |
| 327 } // namespace blink | 329 } // namespace blink |
| OLD | NEW |