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

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

Issue 2590513002: Revert of [Sensors] Align sensor reading updates and 'onchange' notification with rAF. (Closed)
Patch Set: 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/SensorProxy.cpp
diff --git a/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp b/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp
index 1d3e991f23640e6abc6388090f95fa3dae468bd2..44df7687c304fcb5902f6cfe28994ec46c02be14 100644
--- a/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp
+++ b/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp
@@ -4,11 +4,9 @@
#include "modules/sensor/SensorProxy.h"
-#include "core/dom/Document.h"
#include "core/frame/LocalFrame.h"
#include "modules/sensor/SensorProviderProxy.h"
#include "modules/sensor/SensorReading.h"
-#include "modules/sensor/SensorReadingUpdater.h"
#include "platform/mojo/MojoHelper.h"
#include "public/platform/Platform.h"
@@ -18,18 +16,18 @@
SensorProxy::SensorProxy(SensorType sensorType,
SensorProviderProxy* provider,
- Document* document,
+ Page* page,
std::unique_ptr<SensorReadingFactory> readingFactory)
- : PageVisibilityObserver(document->page()),
+ : PageVisibilityObserver(page),
m_type(sensorType),
m_mode(ReportingMode::CONTINUOUS),
m_provider(provider),
m_clientBinding(this),
m_state(SensorProxy::Uninitialized),
m_suspended(false),
- m_document(document),
m_readingFactory(std::move(readingFactory)),
- m_maximumFrequency(0.0) {}
+ m_maximumFrequency(0.0),
+ m_timer(this, &SensorProxy::onTimerFired) {}
SensorProxy::~SensorProxy() {}
@@ -38,8 +36,6 @@
}
DEFINE_TRACE(SensorProxy) {
- visitor->trace(m_document);
- visitor->trace(m_readingUpdater);
visitor->trace(m_reading);
visitor->trace(m_observers);
visitor->trace(m_provider);
@@ -71,10 +67,6 @@
callback);
}
-bool SensorProxy::isActive() const {
- return isInitialized() && !m_suspended && !m_frequenciesUsed.isEmpty();
-}
-
void SensorProxy::addConfiguration(
SensorConfigurationPtr configuration,
std::unique_ptr<Function<void(bool)>> callback) {
@@ -101,6 +93,12 @@
m_sensor->Suspend();
m_suspended = true;
+
+ if (usesPollingTimer())
+ updatePollingStatus();
+
+ for (Observer* observer : m_observers)
+ observer->onSuspended();
}
void SensorProxy::resume() {
@@ -111,13 +109,17 @@
m_sensor->Resume();
m_suspended = false;
- if (isActive())
- m_readingUpdater->start();
+ if (usesPollingTimer())
+ updatePollingStatus();
}
const SensorConfiguration* SensorProxy::defaultConfig() const {
DCHECK(isInitialized());
return m_defaultConfig.get();
+}
+
+bool SensorProxy::usesPollingTimer() const {
+ return isInitialized() && (m_mode == ReportingMode::CONTINUOUS);
}
void SensorProxy::updateSensorReading() {
@@ -134,14 +136,9 @@
}
m_reading = m_readingFactory->createSensorReading(readingData);
-}
-
-void SensorProxy::notifySensorChanged(double timestamp) {
- // This notification leads to sync 'onchange' event sending, so
- // we must cache m_observers as it can be modified within event handlers.
- auto copy = m_observers;
- for (Observer* observer : copy)
- observer->onSensorReadingChanged(timestamp);
+
+ for (Observer* observer : m_observers)
+ observer->onSensorReadingChanged();
}
void SensorProxy::RaiseError() {
@@ -150,8 +147,7 @@
void SensorProxy::SensorReadingChanged() {
DCHECK_EQ(ReportingMode::ON_CHANGE, m_mode);
- if (isActive())
- m_readingUpdater->start();
+ updateSensorReading();
}
void SensorProxy::pageVisibilityChanged() {
@@ -173,9 +169,12 @@
return;
}
+ if (usesPollingTimer()) { // Stop polling.
+ m_frequenciesUsed.clear();
+ updatePollingStatus();
+ }
+
m_state = Uninitialized;
- m_frequenciesUsed.clear();
-
// The m_sensor.reset() will release all callbacks and its bound parameters,
// therefore, handleSensorError accepts messages by value.
m_sensor.reset();
@@ -229,10 +228,7 @@
m_sensor.set_connection_error_handler(
convertToBaseCallback(std::move(errorCallback)));
- m_readingUpdater = SensorReadingUpdater::create(this, m_mode);
-
m_state = Initialized;
-
for (Observer* observer : m_observers)
observer->onSensorInitialized();
}
@@ -241,11 +237,9 @@
double frequency,
std::unique_ptr<Function<void(bool)>> callback,
bool result) {
- if (result) {
+ if (usesPollingTimer() && result) {
m_frequenciesUsed.append(frequency);
- std::sort(m_frequenciesUsed.begin(), m_frequenciesUsed.end());
- if (isActive())
- m_readingUpdater->start();
+ updatePollingStatus();
}
(*callback)(result);
@@ -256,6 +250,9 @@
if (!result)
DVLOG(1) << "Failure at sensor configuration removal";
+ if (!usesPollingTimer())
+ return;
+
size_t index = m_frequenciesUsed.find(frequency);
if (index == kNotFound) {
// Could happen e.g. if 'handleSensorError' was called before.
@@ -263,6 +260,7 @@
}
m_frequenciesUsed.remove(index);
+ updatePollingStatus();
}
bool SensorProxy::tryReadFromBuffer(device::SensorReading& result) {
@@ -278,4 +276,28 @@
return true;
}
+void SensorProxy::updatePollingStatus() {
+ DCHECK(usesPollingTimer());
+
+ if (m_suspended || m_frequenciesUsed.isEmpty()) {
+ m_timer.stop();
+ return;
+ }
+ // TODO(Mikhail): Consider using sorted queue instead of searching
+ // max element each time.
+ auto it =
+ std::max_element(m_frequenciesUsed.begin(), m_frequenciesUsed.end());
+ DCHECK_GT(*it, 0.0);
+
+ double repeatInterval = 1 / *it;
+ if (!m_timer.isActive() || m_timer.repeatInterval() != repeatInterval) {
+ updateSensorReading();
+ m_timer.startRepeating(repeatInterval, BLINK_FROM_HERE);
+ }
+}
+
+void SensorProxy::onTimerFired(TimerBase*) {
+ updateSensorReading();
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698