Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: third_party/WebKit/Source/modules/sensor/SensorProxy.h

Issue 2551223003: [Sensors] Align sensor reading updates and 'onchange' notification with rAF. (Closed)
Patch Set: Comments from Reilly Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
shalamov 2016/12/16 17:10:48 I think this can be removed now.
Mikhail 2016/12/19 07:07:56 Done.
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.
47 virtual void onSuspended() {}
48 }; 52 };
49 53
50 ~SensorProxy(); 54 ~SensorProxy();
51 55
52 void dispose(); 56 void dispose();
53 57
54 void addObserver(Observer*); 58 void addObserver(Observer*);
55 void removeObserver(Observer*); 59 void removeObserver(Observer*);
56 60
57 void initialize(); 61 void initialize();
58 62
59 bool isInitializing() const { return m_state == Initializing; } 63 bool isInitializing() const { return m_state == Initializing; }
60 bool isInitialized() const { return m_state == Initialized; } 64 bool isInitialized() const { return m_state == Initialized; }
61 65
66 // Is watching new reading data (initialized, not suspended and has
67 // configurations added).
68 bool isActive() const;
69
62 void addConfiguration(device::mojom::blink::SensorConfigurationPtr, 70 void addConfiguration(device::mojom::blink::SensorConfigurationPtr,
63 std::unique_ptr<Function<void(bool)>>); 71 std::unique_ptr<Function<void(bool)>>);
64 72
65 void removeConfiguration(device::mojom::blink::SensorConfigurationPtr); 73 void removeConfiguration(device::mojom::blink::SensorConfigurationPtr);
66 74
67 void suspend(); 75 void suspend();
68 void resume(); 76 void resume();
69 77
70 device::mojom::blink::SensorType type() const { return m_type; } 78 device::mojom::blink::SensorType type() const { return m_type; }
71 device::mojom::blink::ReportingMode reportingMode() const { return m_mode; } 79 device::mojom::blink::ReportingMode reportingMode() const { return m_mode; }
72 80
73 // The |SensorReading| instance which is shared between sensor instances 81 // The |SensorReading| instance which is shared between sensor instances
74 // of the same type. 82 // of the same type.
75 // Note: the returned value is reset after updateSensorReading() call. 83 // Note: the returned value is reset after updateSensorReading() call.
76 SensorReading* sensorReading() const { return m_reading; } 84 SensorReading* sensorReading() const { return m_reading; }
77 85
78 const device::mojom::blink::SensorConfiguration* defaultConfig() const; 86 const device::mojom::blink::SensorConfiguration* defaultConfig() const;
79 87
80 double maximumFrequency() const { return m_maximumFrequency; } 88 double maximumFrequency() const { return m_maximumFrequency; }
81 89
90 Document* document() const { return m_document; }
91 const WTF::Vector<double>& frequenciesUsed() const {
92 return m_frequenciesUsed;
93 }
94
82 DECLARE_VIRTUAL_TRACE(); 95 DECLARE_VIRTUAL_TRACE();
83 96
84 private: 97 private:
85 friend class SensorProviderProxy; 98 friend class SensorProviderProxy;
99 friend class SensorReadingUpdaterContinuous;
100 friend class SensorReadingUpdaterOnChange;
86 SensorProxy(device::mojom::blink::SensorType, 101 SensorProxy(device::mojom::blink::SensorType,
87 SensorProviderProxy*, 102 SensorProviderProxy*,
88 Page*, 103 Document*,
89 std::unique_ptr<SensorReadingFactory>); 104 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 105
94 // Updates sensor reading from shared buffer. 106 // Updates sensor reading from shared buffer.
95 void updateSensorReading(); 107 void updateSensorReading();
108 void notifySensorChanged(double timestamp);
96 109
97 // device::mojom::blink::SensorClient overrides. 110 // device::mojom::blink::SensorClient overrides.
98 void RaiseError() override; 111 void RaiseError() override;
99 void SensorReadingChanged() override; 112 void SensorReadingChanged() override;
100 113
101 // PageVisibilityObserver overrides. 114 // PageVisibilityObserver overrides.
102 void pageVisibilityChanged() override; 115 void pageVisibilityChanged() override;
103 116
104 // Generic handler for a fatal error. 117 // Generic handler for a fatal error.
105 // String parameters are intentionally copied. 118 // String parameters are intentionally copied.
106 void handleSensorError(ExceptionCode = UnknownError, 119 void handleSensorError(ExceptionCode = UnknownError,
107 String sanitizedMessage = String(), 120 String sanitizedMessage = String(),
108 String unsanitizedMessage = String()); 121 String unsanitizedMessage = String());
109 // mojo call callbacks. 122 // mojo call callbacks.
110 void onSensorCreated(device::mojom::blink::SensorInitParamsPtr, 123 void onSensorCreated(device::mojom::blink::SensorInitParamsPtr,
111 device::mojom::blink::SensorClientRequest); 124 device::mojom::blink::SensorClientRequest);
112 void onAddConfigurationCompleted( 125 void onAddConfigurationCompleted(
113 double frequency, 126 double frequency,
114 std::unique_ptr<Function<void(bool)>> callback, 127 std::unique_ptr<Function<void(bool)>> callback,
115 bool result); 128 bool result);
116 void onRemoveConfigurationCompleted(double frequency, bool result); 129 void onRemoveConfigurationCompleted(double frequency, bool result);
117 130
118 bool tryReadFromBuffer(device::SensorReading& result); 131 bool tryReadFromBuffer(device::SensorReading& result);
119 void updatePollingStatus(); 132 void onAnimationFrame(double timestamp);
120 void onTimerFired(TimerBase*);
121 133
122 device::mojom::blink::SensorType m_type; 134 device::mojom::blink::SensorType m_type;
123 device::mojom::blink::ReportingMode m_mode; 135 device::mojom::blink::ReportingMode m_mode;
124 Member<SensorProviderProxy> m_provider; 136 Member<SensorProviderProxy> m_provider;
125 using ObserversSet = HeapHashSet<WeakMember<Observer>>; 137 using ObserversSet = HeapHashSet<WeakMember<Observer>>;
126 ObserversSet m_observers; 138 ObserversSet m_observers;
127 139
128 device::mojom::blink::SensorPtr m_sensor; 140 device::mojom::blink::SensorPtr m_sensor;
129 device::mojom::blink::SensorConfigurationPtr m_defaultConfig; 141 device::mojom::blink::SensorConfigurationPtr m_defaultConfig;
130 mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding; 142 mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding;
131 143
132 enum State { Uninitialized, Initializing, Initialized }; 144 enum State { Uninitialized, Initializing, Initialized };
133 State m_state; 145 State m_state;
134 mojo::ScopedSharedBufferHandle m_sharedBufferHandle; 146 mojo::ScopedSharedBufferHandle m_sharedBufferHandle;
135 mojo::ScopedSharedBufferMapping m_sharedBuffer; 147 mojo::ScopedSharedBufferMapping m_sharedBuffer;
136 bool m_suspended; 148 bool m_suspended;
149 Member<Document> m_document;
137 Member<SensorReading> m_reading; 150 Member<SensorReading> m_reading;
138 std::unique_ptr<SensorReadingFactory> m_readingFactory; 151 std::unique_ptr<SensorReadingFactory> m_readingFactory;
139 double m_maximumFrequency; 152 double m_maximumFrequency;
140 153
141 // Used for continious reporting mode. 154 Member<SensorReadingUpdater> m_readingUpdater;
142 Timer<SensorProxy> m_timer;
143 WTF::Vector<double> m_frequenciesUsed; 155 WTF::Vector<double> m_frequenciesUsed;
156 double m_lastRafTimestamp;
144 157
145 using ReadingBuffer = device::SensorReadingSharedBuffer; 158 using ReadingBuffer = device::SensorReadingSharedBuffer;
146 static_assert( 159 static_assert(
147 sizeof(ReadingBuffer) == 160 sizeof(ReadingBuffer) ==
148 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, 161 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests,
149 "Check reading buffer size for tests"); 162 "Check reading buffer size for tests");
150 }; 163 };
151 164
152 } // namespace blink 165 } // namespace blink
153 166
154 #endif // SensorProxy_h 167 #endif // SensorProxy_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698