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 60551e7edd45e22c1090aaf8f1f05beec2d6fa4a..2f654adc90e6abf98e6bf3c973a7e9b87c7ed4ce 100644 |
| --- a/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
| +++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
| @@ -10,9 +10,9 @@ |
| #include "core/inspector/ConsoleMessage.h" |
| #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| #include "modules/sensor/SensorErrorEvent.h" |
| -#include "modules/sensor/SensorPollingStrategy.h" |
| #include "modules/sensor/SensorProviderProxy.h" |
| #include "modules/sensor/SensorReading.h" |
| +#include "modules/sensor/SensorUpdateNotificationStrategy.h" |
| using namespace device::mojom::blink; |
| @@ -24,8 +24,6 @@ Sensor::Sensor(ScriptState* scriptState, |
| SensorType type) |
| : ActiveScriptWrappable(this), |
| ContextLifecycleObserver(scriptState->getExecutionContext()), |
| - PageVisibilityObserver( |
| - toDocument(scriptState->getExecutionContext())->page()), |
| m_sensorOptions(sensorOptions), |
| m_type(type), |
| m_state(Sensor::SensorState::Idle) { |
| @@ -127,7 +125,6 @@ DEFINE_TRACE(Sensor) { |
| visitor->trace(m_sensorProxy); |
| ActiveScriptWrappable::trace(visitor); |
| ContextLifecycleObserver::trace(visitor); |
| - PageVisibilityObserver::trace(visitor); |
| EventTargetWithInlineData::trace(visitor); |
| } |
| @@ -167,8 +164,10 @@ void Sensor::initSensorProxyIfNeeded() { |
| m_sensorProxy = provider->getSensor(m_type); |
| if (!m_sensorProxy) { |
| + Page* page = toDocument(getExecutionContext())->page(); |
|
haraken
2016/11/16 04:31:47
toDocument(getExecutionContext()) => document
Mikhail
2016/11/17 17:07:35
Done.
|
| + DCHECK(page); |
| m_sensorProxy = |
| - provider->createSensor(m_type, createSensorReadingFactory()); |
| + provider->createSensor(m_type, page, createSensorReadingFactory()); |
| } |
| } |
| @@ -186,8 +185,8 @@ void Sensor::onSensorInitialized() { |
| } |
| void Sensor::onSensorReadingChanged() { |
| - if (m_polling) |
| - m_polling->onSensorReadingChanged(); |
| + if (m_sensorUpdateNotifier && m_state == Sensor::SensorState::Active) |
| + m_sensorUpdateNotifier->onSensorReadingChanged(); |
| } |
| void Sensor::onSensorError(ExceptionCode code, |
| @@ -209,38 +208,14 @@ void Sensor::onStartRequestCompleted(bool result) { |
| DCHECK(m_configuration); |
| DCHECK(m_sensorProxy); |
| - auto pollCallback = WTF::bind(&Sensor::pollForData, wrapWeakPersistent(this)); |
| + auto updateCallback = |
| + WTF::bind(&Sensor::onSensorUpdateNotification, wrapWeakPersistent(this)); |
| DCHECK_GT(m_configuration->frequency, 0); |
| - m_polling = SensorPollingStrategy::create(1 / m_configuration->frequency, |
| - std::move(pollCallback), |
| - m_sensorProxy->reportingMode()); |
| + m_sensorUpdateNotifier = SensorUpdateNotificationStrategy::create( |
| + m_configuration->frequency, std::move(updateCallback)); |
| updateState(Sensor::SensorState::Active); |
| } |
| -void Sensor::onStopRequestCompleted(bool result) { |
| - if (m_state == Sensor::SensorState::Idle) |
| - return; |
| - |
| - if (!result) |
| - reportError(OperationError); |
| - |
| - DCHECK(m_sensorProxy); |
| - m_sensorProxy->removeObserver(this); |
| -} |
| - |
| -void Sensor::pageVisibilityChanged() { |
| - updatePollingStatus(); |
| - |
| - if (!m_sensorProxy || !m_sensorProxy->isInitialized()) |
| - return; |
| - |
| - if (page()->visibilityState() != PageVisibilityStateVisible) { |
| - m_sensorProxy->suspend(); |
| - } else { |
| - m_sensorProxy->resume(); |
| - } |
| -} |
| - |
| void Sensor::startListening() { |
| DCHECK(m_sensorProxy); |
| updateState(Sensor::SensorState::Activating); |
| @@ -269,28 +244,20 @@ void Sensor::stopListening() { |
| updateState(Sensor::SensorState::Idle); |
| if (m_sensorProxy->isInitialized()) { |
| - auto callback = |
| - WTF::bind(&Sensor::onStopRequestCompleted, wrapWeakPersistent(this)); |
| DCHECK(m_configuration); |
| - m_sensorProxy->removeConfiguration(m_configuration->Clone(), |
| - std::move(callback)); |
| - } else { |
| - m_sensorProxy->removeObserver(this); |
| + m_sensorProxy->removeConfiguration(m_configuration->Clone()); |
| } |
| + m_sensorProxy->removeObserver(this); |
| } |
| -void Sensor::pollForData() { |
| - if (m_state != Sensor::SensorState::Active) { |
| - DCHECK(m_polling); |
| - m_polling->stopPolling(); |
| +void Sensor::onSensorUpdateNotification() { |
| + if (m_state != Sensor::SensorState::Active) |
| return; |
| - } |
| DCHECK(m_sensorProxy); |
| DCHECK(m_sensorProxy->isInitialized()); |
| - m_sensorProxy->updateSensorReading(); |
| - |
| DCHECK(m_sensorProxy->sensorReading()); |
| + |
| if (getExecutionContext() && |
| m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
| getExecutionContext()->postTask( |
| @@ -314,7 +281,6 @@ void Sensor::updateState(Sensor::SensorState newState) { |
| } |
| m_state = newState; |
| - updatePollingStatus(); |
| } |
| void Sensor::reportError(ExceptionCode code, |
| @@ -331,16 +297,9 @@ void Sensor::reportError(ExceptionCode code, |
| } |
| } |
| -void Sensor::updatePollingStatus() { |
| - if (!m_polling) |
| - return; |
| - |
| - if (m_state != Sensor::SensorState::Active || |
| - page()->visibilityState() != PageVisibilityStateVisible) { |
| - m_polling->stopPolling(); |
| - } else { |
| - m_polling->startPolling(); |
| - } |
| +void Sensor::onSuspended() { |
| + if (m_sensorUpdateNotifier) |
| + m_sensorUpdateNotifier->cancelPendingNotifications(); |
| } |
| void Sensor::notifySensorReadingChanged() { |