| OLD | NEW | 
|---|
|  | (Empty) | 
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |  | 
| 2 // Use of this source code is governed by a BSD-style license that can be |  | 
| 3 // found in the LICENSE file. |  | 
| 4 |  | 
| 5 #include "modules/sensor/SensorReadingUpdater.h" |  | 
| 6 |  | 
| 7 #include "core/dom/Document.h" |  | 
| 8 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |  | 
| 9 #include "modules/sensor/SensorProxy.h" |  | 
| 10 #include "platform/wtf/CurrentTime.h" |  | 
| 11 |  | 
| 12 using device::mojom::blink::ReportingMode; |  | 
| 13 |  | 
| 14 namespace blink { |  | 
| 15 |  | 
| 16 SensorReadingUpdater::SensorReadingUpdater(SensorProxy* sensor_proxy) |  | 
| 17     : sensor_proxy_(sensor_proxy), |  | 
| 18       document_(sensor_proxy_->GetDocument()), |  | 
| 19       has_pending_animation_frame_task_(false) {} |  | 
| 20 |  | 
| 21 void SensorReadingUpdater::EnqueueAnimationFrameTask() { |  | 
| 22   if (!document_ || document_->IsDetached()) { |  | 
| 23     // If the document has detached the scheduled callbacks |  | 
| 24     // will never be called. |  | 
| 25     has_pending_animation_frame_task_ = false; |  | 
| 26     document_ = sensor_proxy_->GetDocument(); |  | 
| 27     if (!document_ || document_->IsDetached()) |  | 
| 28       return; |  | 
| 29   } |  | 
| 30 |  | 
| 31   if (has_pending_animation_frame_task_) |  | 
| 32     return; |  | 
| 33 |  | 
| 34   auto callback = WTF::Bind(&SensorReadingUpdater::OnAnimationFrame, |  | 
| 35                             WrapWeakPersistent(this)); |  | 
| 36   document_->EnqueueAnimationFrameTask(std::move(callback)); |  | 
| 37   has_pending_animation_frame_task_ = true; |  | 
| 38 } |  | 
| 39 |  | 
| 40 void SensorReadingUpdater::Start() { |  | 
| 41   EnqueueAnimationFrameTask(); |  | 
| 42 } |  | 
| 43 |  | 
| 44 void SensorReadingUpdater::OnAnimationFrame() { |  | 
| 45   has_pending_animation_frame_task_ = false; |  | 
| 46   OnAnimationFrameInternal(); |  | 
| 47 } |  | 
| 48 |  | 
| 49 DEFINE_TRACE(SensorReadingUpdater) { |  | 
| 50   visitor->Trace(document_); |  | 
| 51   visitor->Trace(sensor_proxy_); |  | 
| 52 } |  | 
| 53 |  | 
| 54 class SensorReadingUpdaterContinuous : public SensorReadingUpdater { |  | 
| 55  public: |  | 
| 56   explicit SensorReadingUpdaterContinuous(SensorProxy* sensor_proxy) |  | 
| 57       : SensorReadingUpdater(sensor_proxy) {} |  | 
| 58 |  | 
| 59   DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::Trace(visitor); } |  | 
| 60 |  | 
| 61  protected: |  | 
| 62   void OnAnimationFrameInternal() override { |  | 
| 63     if (!sensor_proxy_->IsActive()) |  | 
| 64       return; |  | 
| 65 |  | 
| 66     sensor_proxy_->UpdateSensorReading(); |  | 
| 67     sensor_proxy_->NotifySensorChanged(WTF::MonotonicallyIncreasingTime()); |  | 
| 68     EnqueueAnimationFrameTask(); |  | 
| 69   } |  | 
| 70 }; |  | 
| 71 |  | 
| 72 // New data is fetched from shared buffer only once after 'start()' |  | 
| 73 // call. Further, notification is send until every client is updated |  | 
| 74 // (i.e. until longest notification period elapses) rAF stops after that. |  | 
| 75 class SensorReadingUpdaterOnChange : public SensorReadingUpdater { |  | 
| 76  public: |  | 
| 77   explicit SensorReadingUpdaterOnChange(SensorProxy* sensor_proxy) |  | 
| 78       : SensorReadingUpdater(sensor_proxy), |  | 
| 79         new_data_arrived_time_(0.0), |  | 
| 80         new_data_arrived_(false) {} |  | 
| 81 |  | 
| 82   DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::Trace(visitor); } |  | 
| 83 |  | 
| 84   void Start() override { |  | 
| 85     new_data_arrived_ = true; |  | 
| 86     SensorReadingUpdater::Start(); |  | 
| 87   } |  | 
| 88 |  | 
| 89  protected: |  | 
| 90   void OnAnimationFrameInternal() override { |  | 
| 91     if (!sensor_proxy_->IsActive()) |  | 
| 92       return; |  | 
| 93 |  | 
| 94     double timestamp = WTF::MonotonicallyIncreasingTime(); |  | 
| 95 |  | 
| 96     if (new_data_arrived_) { |  | 
| 97       new_data_arrived_ = false; |  | 
| 98       sensor_proxy_->UpdateSensorReading(); |  | 
| 99       new_data_arrived_time_ = timestamp; |  | 
| 100     } |  | 
| 101     sensor_proxy_->NotifySensorChanged(timestamp); |  | 
| 102 |  | 
| 103     DCHECK_GT(sensor_proxy_->FrequenciesUsed().front(), 0.0); |  | 
| 104     double longest_notification_period = |  | 
| 105         1 / sensor_proxy_->FrequenciesUsed().front(); |  | 
| 106 |  | 
| 107     if (timestamp - new_data_arrived_time_ <= longest_notification_period) |  | 
| 108       EnqueueAnimationFrameTask(); |  | 
| 109   } |  | 
| 110 |  | 
| 111  private: |  | 
| 112   double new_data_arrived_time_; |  | 
| 113   bool new_data_arrived_; |  | 
| 114 }; |  | 
| 115 |  | 
| 116 // static |  | 
| 117 SensorReadingUpdater* SensorReadingUpdater::Create(SensorProxy* proxy, |  | 
| 118                                                    ReportingMode mode) { |  | 
| 119   if (mode == ReportingMode::CONTINUOUS) |  | 
| 120     return new SensorReadingUpdaterContinuous(proxy); |  | 
| 121   return new SensorReadingUpdaterOnChange(proxy); |  | 
| 122 } |  | 
| 123 |  | 
| 124 }  // namespace blink |  | 
| OLD | NEW | 
|---|