| 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 92 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 |