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