Chromium Code Reviews| Index: third_party/WebKit/Source/modules/sensor/SensorReadingUpdater.cpp |
| diff --git a/third_party/WebKit/Source/modules/sensor/SensorReadingUpdater.cpp b/third_party/WebKit/Source/modules/sensor/SensorReadingUpdater.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ef0d3c0d49b2099bd3f54f71166e45ed0fcd87de |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/sensor/SensorReadingUpdater.cpp |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "modules/sensor/SensorReadingUpdater.h" |
| + |
| +#include "core/dom/Document.h" |
| +#include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| +#include "modules/sensor/SensorProxy.h" |
| + |
| +namespace blink { |
| + |
| +using ReportingMode = device::mojom::blink::ReportingMode; |
|
Reilly Grant (use Gerrit)
2016/12/16 00:48:55
Move outside of the namespace blink block and chan
|
| + |
| +SensorReadingUpdater::SensorReadingUpdater(SensorProxy* sensorProxy) |
| + : m_sensorProxy(sensorProxy), m_rafCallbackId(0) {} |
| + |
| +void SensorReadingUpdater::start() { |
| + if (!m_rafCallbackId) |
| + m_rafCallbackId = m_sensorProxy->document()->requestAnimationFrame(this); |
| +} |
| + |
| +void SensorReadingUpdater::stop() { |
| + if (m_rafCallbackId) { // Cancel pending request. |
| + m_sensorProxy->document()->cancelAnimationFrame(m_rafCallbackId); |
| + m_rafCallbackId = 0; |
| + } |
| +} |
| + |
| +DEFINE_TRACE(SensorReadingUpdater) { |
| + visitor->trace(m_sensorProxy); |
| + FrameRequestCallback::trace(visitor); |
| +} |
| + |
| +class SensorReadingUpdaterContinuous : public SensorReadingUpdater { |
| + public: |
| + explicit SensorReadingUpdaterContinuous(SensorProxy* sensorProxy) |
| + : SensorReadingUpdater(sensorProxy) {} |
| + |
| + DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::trace(visitor); } |
| + |
| + protected: |
| + void handleEvent(double highResTimeMs) override { |
| + if (!m_sensorProxy->isActive()) |
| + return; |
| + |
| + m_rafCallbackId = m_sensorProxy->document()->requestAnimationFrame(this); |
| + m_sensorProxy->updateSensorReading(); |
| + m_sensorProxy->notifySensorChanged(highResTimeMs * 0.001); |
| + } |
| +}; |
| + |
| +// New data is fetched from shared buffer only once after 'start()' |
| +// call. Further, notification is send until every client is updated |
| +// (i.e. until longest notification period elapses) rAF stops after that. |
| +class SensorReadingUpdaterOnChange : public SensorReadingUpdater { |
| + public: |
| + explicit SensorReadingUpdaterOnChange(SensorProxy* sensorProxy) |
| + : SensorReadingUpdater(sensorProxy), |
| + m_newDataArrivedTime(0.0), |
| + m_newDataArrived(false) {} |
| + |
| + DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::trace(visitor); } |
| + |
| + void start() override { |
| + m_newDataArrived = true; |
| + SensorReadingUpdater::start(); |
| + } |
| + |
| + protected: |
| + void handleEvent(double highResTimeMs) override { |
| + if (!m_sensorProxy->isActive()) |
| + return; |
| + |
| + double timestampSec = highResTimeMs * 0.001; |
| + |
| + if (m_newDataArrived) { |
| + m_newDataArrived = false; |
| + m_sensorProxy->updateSensorReading(); |
| + m_newDataArrivedTime = timestampSec; |
| + m_sensorProxy->notifySensorChanged(timestampSec); |
|
Reilly Grant (use Gerrit)
2016/12/16 00:48:55
Should we be calling notifySensorChanged() twice h
|
| + } |
| + m_sensorProxy->notifySensorChanged(timestampSec); |
| + |
| + DCHECK_GT(m_sensorProxy->frequenciesUsed().front(), 0.0); |
| + double longestNotificationPeriod = |
| + 1 / m_sensorProxy->frequenciesUsed().front(); |
| + bool requestNextFrame = |
| + timestampSec - m_newDataArrivedTime <= longestNotificationPeriod; |
| + m_rafCallbackId = |
| + requestNextFrame |
| + ? m_sensorProxy->document()->requestAnimationFrame(this) |
| + : 0; |
| + } |
| + |
| + private: |
| + double m_newDataArrivedTime; |
| + bool m_newDataArrived; |
| +}; |
| + |
| +// static |
| +SensorReadingUpdater* SensorReadingUpdater::create(SensorProxy* proxy, |
| + ReportingMode mode) { |
| + if (mode == ReportingMode::CONTINUOUS) |
| + return new SensorReadingUpdaterContinuous(proxy); |
| + return new SensorReadingUpdaterOnChange(proxy); |
| +} |
| + |
| +} // namespace blink |