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