 Chromium Code Reviews
 Chromium Code Reviews Issue 2870073002:
  [Sensors] Decouple sensor readings update from rAF  (Closed)
    
  
    Issue 2870073002:
  [Sensors] Decouple sensor readings update from rAF  (Closed) 
  | 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..993de9f3df41e513e281fef8bc62358a66448209 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; | 
| + auto now = WTF::MonotonicallyIncreasingTime(); | 
| 
Reilly Grant (use Gerrit)
2017/05/11 18:28:12
nit: use double here as the type of the return val
 
Mikhail
2017/05/12 08:42:13
Done.
 | 
| 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(Mikhail) : We need to find out an algorithm for resulting | 
| + // polling frequency. | 
| 
Reilly Grant (use Gerrit)
2017/05/10 14:53:28
Please create an issue for this.
 
Mikhail
2017/05/11 10:46:20
Done https://bugs.chromium.org/p/chromium/issues/d
 
Reilly Grant (use Gerrit)
2017/05/11 18:28:12
Please update the comment with the issue number. T
 
Mikhail
2017/05/12 08:42:13
Done.
 | 
| + polling_timer_.StartRepeating(1 / frequencies_used_.back(), | 
| + BLINK_FROM_HERE); | 
| + } else { | 
| + polling_timer_.Stop(); | 
| + } | 
| +} | 
| + | 
| } // namespace blink |