| 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/Timer.h" |
| 16 #include "platform/heap/Handle.h" | 16 #include "platform/heap/Handle.h" |
| 17 #include "wtf/Vector.h" | 17 #include "wtf/Vector.h" |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 21 class SensorProviderProxy; | 21 class SensorProviderProxy; |
| 22 class SensorReading; | 22 class SensorReading; |
| 23 class SensorReadingFactory; | 23 class SensorReadingFactory; |
| 24 class SensorReadingUpdater; |
| 24 | 25 |
| 25 // This class wraps 'Sensor' mojo interface and used by multiple | 26 // This class wraps 'Sensor' mojo interface and used by multiple |
| 26 // JS sensor instances of the same type (within a single frame). | 27 // JS sensor instances of the same type (within a single frame). |
| 27 class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>, | 28 class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>, |
| 28 public device::mojom::blink::SensorClient, | 29 public device::mojom::blink::SensorClient, |
| 29 public PageVisibilityObserver { | 30 public PageVisibilityObserver { |
| 30 USING_GARBAGE_COLLECTED_MIXIN(SensorProxy); | 31 USING_GARBAGE_COLLECTED_MIXIN(SensorProxy); |
| 31 USING_PRE_FINALIZER(SensorProxy, dispose); | 32 USING_PRE_FINALIZER(SensorProxy, dispose); |
| 32 WTF_MAKE_NONCOPYABLE(SensorProxy); | 33 WTF_MAKE_NONCOPYABLE(SensorProxy); |
| 33 | 34 |
| 34 public: | 35 public: |
| 35 class Observer : public GarbageCollectedMixin { | 36 class Observer : public GarbageCollectedMixin { |
| 36 public: | 37 public: |
| 37 // Has valid 'Sensor' binding, {add, remove}Configuration() | 38 // Has valid 'Sensor' binding, {add, remove}Configuration() |
| 38 // methods can be called. | 39 // methods can be called. |
| 39 virtual void onSensorInitialized() {} | 40 virtual void onSensorInitialized() {} |
| 40 // Platfrom sensort reading has changed. | 41 // Platfrom sensort reading has changed. |
| 41 virtual void onSensorReadingChanged() {} | 42 // |timestamp| Reference timestamp in seconds of the moment when |
| 43 // sensor reading was updated from the buffer. |
| 44 // Note: |timestamp| values are only used to calculate elapsed time |
| 45 // between shared buffer readings. These values *do not* correspond |
| 46 // to sensor reading timestamps which are obtained on platform side. |
| 47 virtual void onSensorReadingChanged(double timestamp) {} |
| 42 // An error has occurred. | 48 // An error has occurred. |
| 43 virtual void onSensorError(ExceptionCode, | 49 virtual void onSensorError(ExceptionCode, |
| 44 const String& sanitizedMessage, | 50 const String& sanitizedMessage, |
| 45 const String& unsanitizedMessage) {} | 51 const String& unsanitizedMessage) {} |
| 46 // Sensor reading change notification is suspended. | 52 // Sensor reading change notification is suspended. |
| 47 virtual void onSuspended() {} | 53 virtual void onSuspended() {} |
| 48 }; | 54 }; |
| 49 | 55 |
| 50 ~SensorProxy(); | 56 ~SensorProxy(); |
| 51 | 57 |
| 52 void dispose(); | 58 void dispose(); |
| 53 | 59 |
| 54 void addObserver(Observer*); | 60 void addObserver(Observer*); |
| 55 void removeObserver(Observer*); | 61 void removeObserver(Observer*); |
| 56 | 62 |
| 57 void initialize(); | 63 void initialize(); |
| 58 | 64 |
| 59 bool isInitializing() const { return m_state == Initializing; } | 65 bool isInitializing() const { return m_state == Initializing; } |
| 60 bool isInitialized() const { return m_state == Initialized; } | 66 bool isInitialized() const { return m_state == Initialized; } |
| 61 | 67 |
| 68 // Is watching new reading data (initialized, not suspended and has |
| 69 // configurations added). |
| 70 bool isActive() const; |
| 71 |
| 62 void addConfiguration(device::mojom::blink::SensorConfigurationPtr, | 72 void addConfiguration(device::mojom::blink::SensorConfigurationPtr, |
| 63 std::unique_ptr<Function<void(bool)>>); | 73 std::unique_ptr<Function<void(bool)>>); |
| 64 | 74 |
| 65 void removeConfiguration(device::mojom::blink::SensorConfigurationPtr); | 75 void removeConfiguration(device::mojom::blink::SensorConfigurationPtr); |
| 66 | 76 |
| 67 void suspend(); | 77 void suspend(); |
| 68 void resume(); | 78 void resume(); |
| 69 | 79 |
| 70 device::mojom::blink::SensorType type() const { return m_type; } | 80 device::mojom::blink::SensorType type() const { return m_type; } |
| 71 device::mojom::blink::ReportingMode reportingMode() const { return m_mode; } | 81 device::mojom::blink::ReportingMode reportingMode() const { return m_mode; } |
| 72 | 82 |
| 73 // The |SensorReading| instance which is shared between sensor instances | 83 // The |SensorReading| instance which is shared between sensor instances |
| 74 // of the same type. | 84 // of the same type. |
| 75 // Note: the returned value is reset after updateSensorReading() call. | 85 // Note: the returned value is reset after updateSensorReading() call. |
| 76 SensorReading* sensorReading() const { return m_reading; } | 86 SensorReading* sensorReading() const { return m_reading; } |
| 77 | 87 |
| 78 const device::mojom::blink::SensorConfiguration* defaultConfig() const; | 88 const device::mojom::blink::SensorConfiguration* defaultConfig() const; |
| 79 | 89 |
| 80 double maximumFrequency() const { return m_maximumFrequency; } | 90 double maximumFrequency() const { return m_maximumFrequency; } |
| 81 | 91 |
| 92 Document* document() const { return m_document; } |
| 93 const WTF::Vector<double>& frequenciesUsed() const { |
| 94 return m_frequenciesUsed; |
| 95 } |
| 96 |
| 82 DECLARE_VIRTUAL_TRACE(); | 97 DECLARE_VIRTUAL_TRACE(); |
| 83 | 98 |
| 84 private: | 99 private: |
| 85 friend class SensorProviderProxy; | 100 friend class SensorProviderProxy; |
| 101 friend class SensorReadingUpdaterContinuous; |
| 102 friend class SensorReadingUpdaterOnChange; |
| 86 SensorProxy(device::mojom::blink::SensorType, | 103 SensorProxy(device::mojom::blink::SensorType, |
| 87 SensorProviderProxy*, | 104 SensorProviderProxy*, |
| 88 Page*, | 105 Document*, |
| 89 std::unique_ptr<SensorReadingFactory>); | 106 std::unique_ptr<SensorReadingFactory>); |
| 90 // Returns true if this instance is using polling timer to | |
| 91 // periodically fetch reading data from shared buffer. | |
| 92 bool usesPollingTimer() const; | |
| 93 | 107 |
| 94 // Updates sensor reading from shared buffer. | 108 // Updates sensor reading from shared buffer. |
| 95 void updateSensorReading(); | 109 void updateSensorReading(); |
| 110 void notifySensorChanged(double timestamp); |
| 96 | 111 |
| 97 // device::mojom::blink::SensorClient overrides. | 112 // device::mojom::blink::SensorClient overrides. |
| 98 void RaiseError() override; | 113 void RaiseError() override; |
| 99 void SensorReadingChanged() override; | 114 void SensorReadingChanged() override; |
| 100 | 115 |
| 101 // PageVisibilityObserver overrides. | 116 // PageVisibilityObserver overrides. |
| 102 void pageVisibilityChanged() override; | 117 void pageVisibilityChanged() override; |
| 103 | 118 |
| 104 // Generic handler for a fatal error. | 119 // Generic handler for a fatal error. |
| 105 // String parameters are intentionally copied. | 120 // String parameters are intentionally copied. |
| 106 void handleSensorError(ExceptionCode = UnknownError, | 121 void handleSensorError(ExceptionCode = UnknownError, |
| 107 String sanitizedMessage = String(), | 122 String sanitizedMessage = String(), |
| 108 String unsanitizedMessage = String()); | 123 String unsanitizedMessage = String()); |
| 109 // mojo call callbacks. | 124 // mojo call callbacks. |
| 110 void onSensorCreated(device::mojom::blink::SensorInitParamsPtr, | 125 void onSensorCreated(device::mojom::blink::SensorInitParamsPtr, |
| 111 device::mojom::blink::SensorClientRequest); | 126 device::mojom::blink::SensorClientRequest); |
| 112 void onAddConfigurationCompleted( | 127 void onAddConfigurationCompleted( |
| 113 double frequency, | 128 double frequency, |
| 114 std::unique_ptr<Function<void(bool)>> callback, | 129 std::unique_ptr<Function<void(bool)>> callback, |
| 115 bool result); | 130 bool result); |
| 116 void onRemoveConfigurationCompleted(double frequency, bool result); | 131 void onRemoveConfigurationCompleted(double frequency, bool result); |
| 117 | 132 |
| 118 bool tryReadFromBuffer(device::SensorReading& result); | 133 bool tryReadFromBuffer(device::SensorReading& result); |
| 119 void updatePollingStatus(); | 134 void onAnimationFrame(double timestamp); |
| 120 void onTimerFired(TimerBase*); | |
| 121 | 135 |
| 122 device::mojom::blink::SensorType m_type; | 136 device::mojom::blink::SensorType m_type; |
| 123 device::mojom::blink::ReportingMode m_mode; | 137 device::mojom::blink::ReportingMode m_mode; |
| 124 Member<SensorProviderProxy> m_provider; | 138 Member<SensorProviderProxy> m_provider; |
| 125 using ObserversSet = HeapHashSet<WeakMember<Observer>>; | 139 using ObserversSet = HeapHashSet<WeakMember<Observer>>; |
| 126 ObserversSet m_observers; | 140 ObserversSet m_observers; |
| 127 | 141 |
| 128 device::mojom::blink::SensorPtr m_sensor; | 142 device::mojom::blink::SensorPtr m_sensor; |
| 129 device::mojom::blink::SensorConfigurationPtr m_defaultConfig; | 143 device::mojom::blink::SensorConfigurationPtr m_defaultConfig; |
| 130 mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding; | 144 mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding; |
| 131 | 145 |
| 132 enum State { Uninitialized, Initializing, Initialized }; | 146 enum State { Uninitialized, Initializing, Initialized }; |
| 133 State m_state; | 147 State m_state; |
| 134 mojo::ScopedSharedBufferHandle m_sharedBufferHandle; | 148 mojo::ScopedSharedBufferHandle m_sharedBufferHandle; |
| 135 mojo::ScopedSharedBufferMapping m_sharedBuffer; | 149 mojo::ScopedSharedBufferMapping m_sharedBuffer; |
| 136 bool m_suspended; | 150 bool m_suspended; |
| 151 Member<Document> m_document; |
| 137 Member<SensorReading> m_reading; | 152 Member<SensorReading> m_reading; |
| 138 std::unique_ptr<SensorReadingFactory> m_readingFactory; | 153 std::unique_ptr<SensorReadingFactory> m_readingFactory; |
| 139 double m_maximumFrequency; | 154 double m_maximumFrequency; |
| 140 | 155 |
| 141 // Used for continious reporting mode. | 156 Member<SensorReadingUpdater> m_readingUpdater; |
| 142 Timer<SensorProxy> m_timer; | |
| 143 WTF::Vector<double> m_frequenciesUsed; | 157 WTF::Vector<double> m_frequenciesUsed; |
| 158 double m_lastRafTimestamp; |
| 144 | 159 |
| 145 using ReadingBuffer = device::SensorReadingSharedBuffer; | 160 using ReadingBuffer = device::SensorReadingSharedBuffer; |
| 146 static_assert( | 161 static_assert( |
| 147 sizeof(ReadingBuffer) == | 162 sizeof(ReadingBuffer) == |
| 148 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, | 163 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, |
| 149 "Check reading buffer size for tests"); | 164 "Check reading buffer size for tests"); |
| 150 }; | 165 }; |
| 151 | 166 |
| 152 } // namespace blink | 167 } // namespace blink |
| 153 | 168 |
| 154 #endif // SensorProxy_h | 169 #endif // SensorProxy_h |
| OLD | NEW |