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