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 5f83ae85ca72937f9f2039bbe37cc05db03aed89..62e2a5c58a230680035c70d4d71ec14da5aa64cb 100644 |
--- a/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
+++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
@@ -13,6 +13,7 @@ |
#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; |
@@ -25,8 +26,7 @@ |
: ContextLifecycleObserver(executionContext), |
m_sensorOptions(sensorOptions), |
m_type(type), |
- m_state(Sensor::SensorState::Idle), |
- m_lastUpdateTimestamp(0.0) { |
+ m_state(Sensor::SensorState::Idle) { |
// Check secure context. |
String errorMessage; |
if (!executionContext->isSecureContext(errorMessage)) { |
@@ -164,7 +164,7 @@ |
m_sensorProxy = provider->getSensorProxy(m_type); |
if (!m_sensorProxy) { |
- m_sensorProxy = provider->createSensorProxy(m_type, document, |
+ m_sensorProxy = provider->createSensorProxy(m_type, document->page(), |
createSensorReadingFactory()); |
} |
} |
@@ -182,15 +182,10 @@ |
startListening(); |
} |
-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(); |
+void Sensor::onSensorReadingChanged() { |
+ if (m_state == Sensor::SensorState::Activated) { |
+ DCHECK(m_sensorUpdateNotifier); |
+ m_sensorUpdateNotifier->onSensorReadingChanged(); |
} |
} |
@@ -198,6 +193,8 @@ |
const String& sanitizedMessage, |
const String& unsanitizedMessage) { |
reportError(code, sanitizedMessage, unsanitizedMessage); |
+ if (m_sensorUpdateNotifier) |
+ m_sensorUpdateNotifier->cancelPendingNotifications(); |
} |
void Sensor::onStartRequestCompleted(bool result) { |
@@ -211,6 +208,13 @@ |
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); |
} |
@@ -241,11 +245,33 @@ |
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()); |
} |
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( |
+ TaskType::Sensor, BLINK_FROM_HERE, |
+ createSameThreadTask(&Sensor::notifySensorReadingChanged, |
+ wrapWeakPersistent(this))); |
+ } |
+ |
+ m_storedData = m_sensorProxy->sensorReading()->data(); |
} |
void Sensor::updateState(Sensor::SensorState newState) { |
@@ -277,14 +303,13 @@ |
} |
} |
+void Sensor::onSuspended() { |
+ if (m_sensorUpdateNotifier) |
+ m_sensorUpdateNotifier->cancelPendingNotifications(); |
+} |
+ |
void Sensor::notifySensorReadingChanged() { |
- DCHECK(m_sensorProxy); |
- DCHECK(m_sensorProxy->sensorReading()); |
- |
- if (m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
- m_storedData = m_sensorProxy->sensorReading()->data(); |
- dispatchEvent(Event::create(EventTypeNames::change)); |
- } |
+ dispatchEvent(Event::create(EventTypeNames::change)); |
} |
void Sensor::notifyOnActivate() { |