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