| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SensorProxy_h | 5 #ifndef SensorProxy_h |
| 6 #define SensorProxy_h | 6 #define SensorProxy_h |
| 7 | 7 |
| 8 #include "core/dom/ExceptionCode.h" | 8 #include "core/dom/ExceptionCode.h" |
| 9 #include "core/page/PageVisibilityObserver.h" | 9 #include "core/page/PageVisibilityObserver.h" |
| 10 #include "device/generic_sensor/public/cpp/sensor_reading.h" | 10 #include "device/generic_sensor/public/cpp/sensor_reading.h" |
| 11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| 12 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom-blink.h" | 12 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom-blink.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
| 14 #include "platform/Supplementable.h" | 14 #include "platform/Supplementable.h" |
| 15 #include "platform/Timer.h" |
| 15 #include "platform/heap/Handle.h" | 16 #include "platform/heap/Handle.h" |
| 16 #include "platform/wtf/Vector.h" | 17 #include "platform/wtf/Vector.h" |
| 17 | 18 |
| 18 namespace blink { | 19 namespace blink { |
| 19 | 20 |
| 20 class SensorProviderProxy; | 21 class SensorProviderProxy; |
| 21 class SensorReading; | 22 class SensorReading; |
| 22 class SensorReadingUpdater; | |
| 23 | 23 |
| 24 // This class wraps 'Sensor' mojo interface and used by multiple | 24 // This class wraps 'Sensor' mojo interface and used by multiple |
| 25 // JS sensor instances of the same type (within a single frame). | 25 // JS sensor instances of the same type (within a single frame). |
| 26 class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>, | 26 class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>, |
| 27 public device::mojom::blink::SensorClient, | 27 public device::mojom::blink::SensorClient, |
| 28 public PageVisibilityObserver { | 28 public PageVisibilityObserver { |
| 29 USING_GARBAGE_COLLECTED_MIXIN(SensorProxy); | 29 USING_GARBAGE_COLLECTED_MIXIN(SensorProxy); |
| 30 USING_PRE_FINALIZER(SensorProxy, Dispose); | 30 USING_PRE_FINALIZER(SensorProxy, Dispose); |
| 31 WTF_MAKE_NONCOPYABLE(SensorProxy); | 31 WTF_MAKE_NONCOPYABLE(SensorProxy); |
| 32 | 32 |
| 33 public: | 33 public: |
| 34 class Observer : public GarbageCollectedMixin { | 34 class Observer : public GarbageCollectedMixin { |
| 35 public: | 35 public: |
| 36 // Has valid 'Sensor' binding, {add, remove}Configuration() | 36 // Has valid 'Sensor' binding, {add, remove}Configuration() |
| 37 // methods can be called. | 37 // methods can be called. |
| 38 virtual void OnSensorInitialized() {} | 38 virtual void OnSensorInitialized() {} |
| 39 // Platfrom sensor reading has changed. | 39 // Observer should update its cached reading and send 'onchange' |
| 40 virtual void OnSensorReadingChanged() {} | 40 // event if needed. |
| 41 // Observer should send 'onchange' event if needed. | |
| 42 // The 'notifySensorChanged' calls are in sync with rAF. | |
| 43 // Currently, we decide whether to send 'onchange' event based on the | |
| 44 // time elapsed from the previous notification. | |
| 45 // TODO: Reconsider this after https://github.com/w3c/sensors/issues/152 | |
| 46 // is resolved. | |
| 47 // |timestamp| Reference timestamp in seconds of the moment when | 41 // |timestamp| Reference timestamp in seconds of the moment when |
| 48 // sensor reading was updated from the buffer. | 42 // sensor reading was updated from the buffer. |
| 49 // Note: |timestamp| values are only used to calculate elapsed time | 43 // Note: |timestamp| values are only used to calculate elapsed time |
| 50 // between shared buffer readings. These values *do not* correspond | 44 // between shared buffer readings. These values *do not* correspond |
| 51 // to sensor reading timestamps which are obtained on platform side. | 45 // to sensor reading timestamps which are obtained on platform side. |
| 52 virtual void NotifySensorChanged(double timestamp) {} | 46 virtual void OnSensorReadingChanged(double timestamp) {} |
| 53 // An error has occurred. | 47 // An error has occurred. |
| 54 virtual void OnSensorError(ExceptionCode, | 48 virtual void OnSensorError(ExceptionCode, |
| 55 const String& sanitized_message, | 49 const String& sanitized_message, |
| 56 const String& unsanitized_message) {} | 50 const String& unsanitized_message) {} |
| 57 }; | 51 }; |
| 58 | 52 |
| 59 ~SensorProxy(); | 53 ~SensorProxy(); |
| 60 | 54 |
| 61 void Dispose(); | 55 void Dispose(); |
| 62 | 56 |
| 63 void AddObserver(Observer*); | 57 void AddObserver(Observer*); |
| 64 void RemoveObserver(Observer*); | 58 void RemoveObserver(Observer*); |
| 65 | 59 |
| 66 void Initialize(); | 60 void Initialize(); |
| 67 | 61 |
| 68 bool IsInitializing() const { return state_ == kInitializing; } | 62 bool IsInitializing() const { return state_ == kInitializing; } |
| 69 bool IsInitialized() const { return state_ == kInitialized; } | 63 bool IsInitialized() const { return state_ == kInitialized; } |
| 70 | 64 |
| 71 // Is watching new reading data (initialized, not suspended and has | |
| 72 // configurations added). | |
| 73 bool IsActive() const; | |
| 74 | |
| 75 void AddConfiguration(device::mojom::blink::SensorConfigurationPtr, | 65 void AddConfiguration(device::mojom::blink::SensorConfigurationPtr, |
| 76 std::unique_ptr<Function<void(bool)>>); | 66 std::unique_ptr<Function<void(bool)>>); |
| 77 | 67 |
| 78 void RemoveConfiguration(device::mojom::blink::SensorConfigurationPtr); | 68 void RemoveConfiguration(device::mojom::blink::SensorConfigurationPtr); |
| 79 | 69 |
| 80 void Suspend(); | 70 void Suspend(); |
| 81 void Resume(); | 71 void Resume(); |
| 82 | 72 |
| 83 device::mojom::blink::SensorType GetType() const { return type_; } | 73 device::mojom::blink::SensorType type() const { return type_; } |
| 84 device::mojom::blink::ReportingMode GetReportingMode() const { return mode_; } | |
| 85 | 74 |
| 86 // Note: the returned value is reset after updateSensorReading() call. | 75 // Note: the returned value is reset after updateSensorReading() call. |
| 87 const device::SensorReading& Reading() const { return reading_; } | 76 const device::SensorReading& reading() const { return reading_; } |
| 88 | 77 |
| 89 const device::mojom::blink::SensorConfiguration* DefaultConfig() const; | 78 const device::mojom::blink::SensorConfiguration* DefaultConfig() const; |
| 90 | 79 |
| 91 const std::pair<double, double>& FrequencyLimits() const { | 80 const std::pair<double, double>& FrequencyLimits() const { |
| 92 return frequency_limits_; | 81 return frequency_limits_; |
| 93 } | 82 } |
| 94 | 83 |
| 95 Document* GetDocument() const; | |
| 96 const WTF::Vector<double>& FrequenciesUsed() const { | |
| 97 return frequencies_used_; | |
| 98 } | |
| 99 | |
| 100 DECLARE_VIRTUAL_TRACE(); | 84 DECLARE_VIRTUAL_TRACE(); |
| 101 | 85 |
| 102 private: | 86 private: |
| 103 friend class SensorProviderProxy; | 87 friend class SensorProviderProxy; |
| 104 friend class SensorReadingUpdaterContinuous; | 88 friend class SensorReadingUpdaterContinuous; |
| 105 friend class SensorReadingUpdaterOnChange; | 89 friend class SensorReadingUpdaterOnChange; |
| 106 SensorProxy(device::mojom::blink::SensorType, SensorProviderProxy*, Page*); | 90 SensorProxy(device::mojom::blink::SensorType, SensorProviderProxy*, Page*); |
| 107 | 91 |
| 108 // Updates sensor reading from shared buffer. | 92 // Updates sensor reading from shared buffer. |
| 109 void UpdateSensorReading(); | 93 void UpdateSensorReading(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 122 // mojo call callbacks. | 106 // mojo call callbacks. |
| 123 void OnSensorCreated(device::mojom::blink::SensorInitParamsPtr, | 107 void OnSensorCreated(device::mojom::blink::SensorInitParamsPtr, |
| 124 device::mojom::blink::SensorClientRequest); | 108 device::mojom::blink::SensorClientRequest); |
| 125 void OnAddConfigurationCompleted( | 109 void OnAddConfigurationCompleted( |
| 126 double frequency, | 110 double frequency, |
| 127 std::unique_ptr<Function<void(bool)>> callback, | 111 std::unique_ptr<Function<void(bool)>> callback, |
| 128 bool result); | 112 bool result); |
| 129 void OnRemoveConfigurationCompleted(double frequency, bool result); | 113 void OnRemoveConfigurationCompleted(double frequency, bool result); |
| 130 | 114 |
| 131 bool TryReadFromBuffer(device::SensorReading& result); | 115 bool TryReadFromBuffer(device::SensorReading& result); |
| 132 void OnAnimationFrame(double timestamp); | 116 |
| 117 void OnPollingTimer(TimerBase*); |
| 118 // Starts polling timer if needed (continuous reporting, initialized, not |
| 119 // suspended and has configurations added). |
| 120 void UpdatePollingStatus(); |
| 133 | 121 |
| 134 device::mojom::blink::SensorType type_; | 122 device::mojom::blink::SensorType type_; |
| 135 device::mojom::blink::ReportingMode mode_; | 123 device::mojom::blink::ReportingMode mode_; |
| 136 Member<SensorProviderProxy> provider_; | 124 Member<SensorProviderProxy> provider_; |
| 137 using ObserversSet = HeapHashSet<WeakMember<Observer>>; | 125 using ObserversSet = HeapHashSet<WeakMember<Observer>>; |
| 138 ObserversSet observers_; | 126 ObserversSet observers_; |
| 139 | 127 |
| 140 device::mojom::blink::SensorPtr sensor_; | 128 device::mojom::blink::SensorPtr sensor_; |
| 141 device::mojom::blink::SensorConfigurationPtr default_config_; | 129 device::mojom::blink::SensorConfigurationPtr default_config_; |
| 142 mojo::Binding<device::mojom::blink::SensorClient> client_binding_; | 130 mojo::Binding<device::mojom::blink::SensorClient> client_binding_; |
| 143 | 131 |
| 144 enum State { kUninitialized, kInitializing, kInitialized }; | 132 enum State { kUninitialized, kInitializing, kInitialized }; |
| 145 State state_; | 133 State state_; |
| 146 mojo::ScopedSharedBufferHandle shared_buffer_handle_; | 134 mojo::ScopedSharedBufferHandle shared_buffer_handle_; |
| 147 mojo::ScopedSharedBufferMapping shared_buffer_; | 135 mojo::ScopedSharedBufferMapping shared_buffer_; |
| 148 bool suspended_; | 136 bool suspended_; |
| 149 device::SensorReading reading_; | 137 device::SensorReading reading_; |
| 150 std::pair<double, double> frequency_limits_; | 138 std::pair<double, double> frequency_limits_; |
| 151 | 139 |
| 152 Member<SensorReadingUpdater> reading_updater_; | |
| 153 WTF::Vector<double> frequencies_used_; | 140 WTF::Vector<double> frequencies_used_; |
| 154 double last_raf_timestamp_; | 141 TaskRunnerTimer<SensorProxy> polling_timer_; |
| 155 | 142 |
| 156 using ReadingBuffer = device::SensorReadingSharedBuffer; | 143 using ReadingBuffer = device::SensorReadingSharedBuffer; |
| 157 static_assert( | 144 static_assert( |
| 158 sizeof(ReadingBuffer) == | 145 sizeof(ReadingBuffer) == |
| 159 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, | 146 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, |
| 160 "Check reading buffer size for tests"); | 147 "Check reading buffer size for tests"); |
| 161 }; | 148 }; |
| 162 | 149 |
| 163 } // namespace blink | 150 } // namespace blink |
| 164 | 151 |
| 165 #endif // SensorProxy_h | 152 #endif // SensorProxy_h |
| OLD | NEW |