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

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

Issue 2870073002: [Sensors] Decouple sensor readings update from rAF (Closed)
Patch Set: [Sensors] Decouple sensor readings update from rAF Created 3 years, 7 months 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 d7f8d9339d343c52bf2cd593a957056829f4fbf7..a4087b399489f35c1bad22660326842d1f49c069 100644
--- a/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp
+++ b/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp
@@ -4,10 +4,9 @@
#include "modules/sensor/SensorProxy.h"
-#include "core/dom/Document.h"
+#include "core/dom/TaskRunnerHelper.h"
#include "core/frame/LocalFrame.h"
#include "modules/sensor/SensorProviderProxy.h"
-#include "modules/sensor/SensorReadingUpdater.h"
#include "platform/mojo/MojoHelper.h"
#include "public/platform/Platform.h"
@@ -24,7 +23,11 @@ SensorProxy::SensorProxy(SensorType sensor_type,
provider_(provider),
client_binding_(this),
state_(SensorProxy::kUninitialized),
- suspended_(false) {}
+ suspended_(false),
+ polling_timer_(TaskRunnerHelper::Get(TaskType::kSensor,
+ provider->GetSupplementable()),
+ this,
+ &SensorProxy::OnPollingTimer) {}
SensorProxy::~SensorProxy() {}
@@ -33,7 +36,6 @@ void SensorProxy::Dispose() {
}
DEFINE_TRACE(SensorProxy) {
- visitor->Trace(reading_updater_);
visitor->Trace(observers_);
visitor->Trace(provider_);
PageVisibilityObserver::Trace(visitor);
@@ -64,10 +66,6 @@ void SensorProxy::Initialize() {
callback);
}
-bool SensorProxy::IsActive() const {
- return IsInitialized() && !suspended_ && !frequencies_used_.IsEmpty();
-}
-
void SensorProxy::AddConfiguration(
SensorConfigurationPtr configuration,
std::unique_ptr<Function<void(bool)>> callback) {
@@ -94,6 +92,7 @@ void SensorProxy::Suspend() {
sensor_->Suspend();
suspended_ = true;
+ UpdatePollingStatus();
}
void SensorProxy::Resume() {
@@ -103,9 +102,7 @@ void SensorProxy::Resume() {
sensor_->Resume();
suspended_ = false;
-
- if (IsActive())
- reading_updater_->Start();
+ UpdatePollingStatus();
}
const SensorConfiguration* SensorProxy::DefaultConfig() const {
@@ -113,10 +110,6 @@ const SensorConfiguration* SensorProxy::DefaultConfig() const {
return default_config_.get();
}
-Document* SensorProxy::GetDocument() const {
- return provider_->GetSupplementable()->GetDocument();
-}
-
void SensorProxy::UpdateSensorReading() {
DCHECK(IsInitialized());
int read_attempts = 0;
@@ -131,27 +124,19 @@ void SensorProxy::UpdateSensorReading() {
if (reading_.timestamp != reading_data.timestamp) {
reading_ = reading_data;
+ double now = WTF::MonotonicallyIncreasingTime();
for (Observer* observer : observers_)
- observer->OnSensorReadingChanged();
+ observer->OnSensorReadingChanged(now);
}
}
-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 = observers_;
- for (Observer* observer : copy)
- observer->NotifySensorChanged(timestamp);
-}
-
void SensorProxy::RaiseError() {
HandleSensorError();
}
void SensorProxy::SensorReadingChanged() {
DCHECK_EQ(ReportingMode::ON_CHANGE, mode_);
- if (IsActive())
- reading_updater_->Start();
+ UpdateSensorReading();
}
void SensorProxy::PageVisibilityChanged() {
@@ -169,6 +154,7 @@ void SensorProxy::HandleSensorError() {
state_ = kUninitialized;
frequencies_used_.clear();
reading_ = device::SensorReading();
+ UpdatePollingStatus();
// The m_sensor.reset() will release all callbacks and its bound parameters,
// therefore, handleSensorError accepts messages by value.
@@ -229,8 +215,6 @@ void SensorProxy::OnSensorCreated(SensorInitParamsPtr params,
sensor_.set_connection_error_handler(
ConvertToBaseCallback(std::move(error_callback)));
- reading_updater_ = SensorReadingUpdater::Create(this, mode_);
-
state_ = kInitialized;
for (Observer* observer : observers_)
@@ -244,8 +228,7 @@ void SensorProxy::OnAddConfigurationCompleted(
if (result) {
frequencies_used_.push_back(frequency);
std::sort(frequencies_used_.begin(), frequencies_used_.end());
- if (IsActive())
- reading_updater_->Start();
+ UpdatePollingStatus();
}
(*callback)(result);
@@ -278,4 +261,22 @@ bool SensorProxy::TryReadFromBuffer(device::SensorReading& result) {
return true;
}
+void SensorProxy::OnPollingTimer(TimerBase*) {
+ UpdateSensorReading();
+}
+
+void SensorProxy::UpdatePollingStatus() {
+ bool start_polling = (mode_ == ReportingMode::CONTINUOUS) &&
+ IsInitialized() && !suspended_ &&
+ !frequencies_used_.IsEmpty();
+ if (start_polling) {
+ // TODO(crbug/721297) : We need to find out an algorithm for resulting
+ // polling frequency.
+ polling_timer_.StartRepeating(1 / frequencies_used_.back(),
+ BLINK_FROM_HERE);
+ } else {
+ polling_timer_.Stop();
+ }
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698