Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Unified Diff: third_party/WebKit/Source/modules/sensor/Sensor.cpp

Issue 2551223003: [Sensors] Align sensor reading updates and 'onchange' notification with rAF. (Closed)
Patch Set: rebased Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 62e2a5c58a230680035c70d4d71ec14da5aa64cb..5f83ae85ca72937f9f2039bbe37cc05db03aed89 100644
--- a/third_party/WebKit/Source/modules/sensor/Sensor.cpp
+++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
@@ -13,7 +13,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(
- TaskType::Sensor, 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;
@@ -303,13 +277,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)) {
+ m_storedData = m_sensorProxy->sensorReading()->data();
+ dispatchEvent(Event::create(EventTypeNames::change));
+ }
}
void Sensor::notifyOnActivate() {

Powered by Google App Engine
This is Rietveld 408576698