Chromium Code Reviews| Index: third_party/WebKit/Source/modules/sensor/Sensor.cpp |
| diff --git a/third_party/WebKit/Source/modules/sensor/Sensor.cpp b/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
| index 6c3ecc113b0d9808d4f9b4e197c446d1a978b5bf..7ee6b018a5f7ea4dfe064a0f2a96cb399722bef7 100644 |
| --- a/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
| +++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
| @@ -12,7 +12,6 @@ |
| #include "modules/sensor/SensorErrorEvent.h" |
| #include "modules/sensor/SensorProviderProxy.h" |
| #include "modules/sensor/SensorReading.h" |
| -#include "modules/sensor/SensorUpdateNotificationStrategy.h" |
| using namespace device::mojom::blink; |
| @@ -26,7 +25,8 @@ Sensor::Sensor(ExecutionContext* executionContext, |
| ContextLifecycleObserver(executionContext), |
| m_sensorOptions(sensorOptions), |
| m_type(type), |
| - m_state(Sensor::SensorState::Idle) { |
| + m_state(Sensor::SensorState::Idle), |
| + m_lastUpdateTimestamp(0.0) { |
| // Check secure context. |
| String errorMessage; |
| if (!executionContext->isSecureContext(errorMessage)) { |
| @@ -164,7 +164,7 @@ void Sensor::initSensorProxyIfNeeded() { |
| m_sensorProxy = provider->getSensorProxy(m_type); |
| if (!m_sensorProxy) { |
| - m_sensorProxy = provider->createSensorProxy(m_type, document->page(), |
| + m_sensorProxy = provider->createSensorProxy(m_type, document, |
| createSensorReadingFactory()); |
| } |
| } |
| @@ -182,10 +182,15 @@ void Sensor::onSensorInitialized() { |
| startListening(); |
| } |
| -void Sensor::onSensorReadingChanged() { |
| - if (m_state == Sensor::SensorState::Activated) { |
| - DCHECK(m_sensorUpdateNotifier); |
| - m_sensorUpdateNotifier->onSensorReadingChanged(); |
| +void Sensor::onSensorReadingChanged(double timestamp) { |
| + if (m_state != Sensor::SensorState::Activated) |
| + return; |
| + |
| + DCHECK_GT(m_configuration->frequency, 0.0); |
| + double period = 1 / m_configuration->frequency; |
| + if (timestamp - m_lastUpdateTimestamp >= period) { |
| + m_lastUpdateTimestamp = timestamp; |
| + notifySensorReadingChanged(); |
| } |
| } |
| @@ -193,8 +198,6 @@ void Sensor::onSensorError(ExceptionCode code, |
| const String& sanitizedMessage, |
| const String& unsanitizedMessage) { |
| reportError(code, sanitizedMessage, unsanitizedMessage); |
| - if (m_sensorUpdateNotifier) |
| - m_sensorUpdateNotifier->cancelPendingNotifications(); |
| } |
| void Sensor::onStartRequestCompleted(bool result) { |
| @@ -208,13 +211,6 @@ void Sensor::onStartRequestCompleted(bool result) { |
| return; |
| } |
| - DCHECK(m_configuration); |
| - DCHECK(m_sensorProxy); |
| - auto updateCallback = |
| - WTF::bind(&Sensor::onSensorUpdateNotification, wrapWeakPersistent(this)); |
| - DCHECK_GT(m_configuration->frequency, 0); |
| - m_sensorUpdateNotifier = SensorUpdateNotificationStrategy::create( |
| - m_configuration->frequency, std::move(updateCallback)); |
| updateState(Sensor::SensorState::Activated); |
| } |
| @@ -245,9 +241,6 @@ void Sensor::stopListening() { |
| DCHECK(m_sensorProxy); |
| updateState(Sensor::SensorState::Idle); |
| - if (m_sensorUpdateNotifier) |
| - m_sensorUpdateNotifier->cancelPendingNotifications(); |
| - |
| if (m_sensorProxy->isInitialized()) { |
| DCHECK(m_configuration); |
| m_sensorProxy->removeConfiguration(m_configuration->Clone()); |
| @@ -255,25 +248,6 @@ void Sensor::stopListening() { |
| m_sensorProxy->removeObserver(this); |
| } |
| -void Sensor::onSensorUpdateNotification() { |
| - if (m_state != Sensor::SensorState::Activated) |
| - return; |
| - |
| - DCHECK(m_sensorProxy); |
| - DCHECK(m_sensorProxy->isInitialized()); |
| - DCHECK(m_sensorProxy->sensorReading()); |
| - |
| - if (getExecutionContext() && |
| - m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
| - getExecutionContext()->postTask( |
| - BLINK_FROM_HERE, |
| - createSameThreadTask(&Sensor::notifySensorReadingChanged, |
| - wrapWeakPersistent(this))); |
| - } |
| - |
| - m_storedData = m_sensorProxy->sensorReading()->data(); |
| -} |
| - |
| void Sensor::updateState(Sensor::SensorState newState) { |
| if (newState == m_state) |
| return; |
| @@ -302,13 +276,14 @@ void Sensor::reportError(ExceptionCode code, |
| } |
| } |
| -void Sensor::onSuspended() { |
| - if (m_sensorUpdateNotifier) |
| - m_sensorUpdateNotifier->cancelPendingNotifications(); |
| -} |
| - |
| void Sensor::notifySensorReadingChanged() { |
| - dispatchEvent(Event::create(EventTypeNames::change)); |
| + DCHECK(m_sensorProxy); |
| + DCHECK(m_sensorProxy->sensorReading()); |
| + |
| + if (m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
| + dispatchEvent(Event::create(EventTypeNames::change)); |
|
shalamov
2016/12/16 17:10:48
should it be reversed?
m_storedData = m_sensorPro
Reilly Grant (use Gerrit)
2016/12/16 22:09:37
If this data were used by Sensor::reading() then i
Mikhail
2016/12/19 07:07:56
Done.
Mikhail
2016/12/19 07:07:56
Done.
|
| + m_storedData = m_sensorProxy->sensorReading()->data(); |
| + } |
| } |
| void Sensor::notifyOnActivate() { |