Chromium Code Reviews| 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 SensorFrameRequestCallback; | |
| 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 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 76 SensorReading* sensorReading() const { return m_reading; } | 82 SensorReading* sensorReading() const { return m_reading; } |
| 77 | 83 |
| 78 const device::mojom::blink::SensorConfiguration* defaultConfig() const; | 84 const device::mojom::blink::SensorConfiguration* defaultConfig() const; |
| 79 | 85 |
| 80 double maximumFrequency() const { return m_maximumFrequency; } | 86 double maximumFrequency() const { return m_maximumFrequency; } |
| 81 | 87 |
| 82 DECLARE_VIRTUAL_TRACE(); | 88 DECLARE_VIRTUAL_TRACE(); |
| 83 | 89 |
| 84 private: | 90 private: |
| 85 friend class SensorProviderProxy; | 91 friend class SensorProviderProxy; |
| 92 friend class SensorFrameRequestCallback; | |
| 86 SensorProxy(device::mojom::blink::SensorType, | 93 SensorProxy(device::mojom::blink::SensorType, |
| 87 SensorProviderProxy*, | 94 SensorProviderProxy*, |
| 88 Page*, | 95 Document*, |
| 89 std::unique_ptr<SensorReadingFactory>); | 96 std::unique_ptr<SensorReadingFactory>); |
| 90 // Returns true if this instance is using polling timer to | 97 // Returns true if this instance is using rAF to |
| 91 // periodically fetch reading data from shared buffer. | 98 // periodically fetch reading data from shared buffer. |
| 92 bool usesPollingTimer() const; | 99 bool isPollingBuffer() const; |
| 93 | 100 |
| 94 // Updates sensor reading from shared buffer. | 101 // Updates sensor reading from shared buffer. |
| 95 void updateSensorReading(); | 102 void updateSensorReading(double timestamp); |
| 96 | 103 |
| 97 // device::mojom::blink::SensorClient overrides. | 104 // device::mojom::blink::SensorClient overrides. |
| 98 void RaiseError() override; | 105 void RaiseError() override; |
| 99 void SensorReadingChanged() override; | 106 void SensorReadingChanged() override; |
| 100 | 107 |
| 101 // PageVisibilityObserver overrides. | 108 // PageVisibilityObserver overrides. |
| 102 void pageVisibilityChanged() override; | 109 void pageVisibilityChanged() override; |
| 103 | 110 |
| 104 // Generic handler for a fatal error. | 111 // Generic handler for a fatal error. |
| 105 // String parameters are intentionally copied. | 112 // String parameters are intentionally copied. |
| 106 void handleSensorError(ExceptionCode = UnknownError, | 113 void handleSensorError(ExceptionCode = UnknownError, |
| 107 String sanitizedMessage = String(), | 114 String sanitizedMessage = String(), |
| 108 String unsanitizedMessage = String()); | 115 String unsanitizedMessage = String()); |
| 109 // mojo call callbacks. | 116 // mojo call callbacks. |
| 110 void onSensorCreated(device::mojom::blink::SensorInitParamsPtr, | 117 void onSensorCreated(device::mojom::blink::SensorInitParamsPtr, |
| 111 device::mojom::blink::SensorClientRequest); | 118 device::mojom::blink::SensorClientRequest); |
| 112 void onAddConfigurationCompleted( | 119 void onAddConfigurationCompleted( |
| 113 double frequency, | 120 double frequency, |
| 114 std::unique_ptr<Function<void(bool)>> callback, | 121 std::unique_ptr<Function<void(bool)>> callback, |
| 115 bool result); | 122 bool result); |
| 116 void onRemoveConfigurationCompleted(double frequency, bool result); | 123 void onRemoveConfigurationCompleted(double frequency, bool result); |
| 117 | 124 |
| 118 bool tryReadFromBuffer(device::SensorReading& result); | 125 bool tryReadFromBuffer(device::SensorReading& result); |
| 119 void updatePollingStatus(); | 126 void updatePollingStatus(); |
| 120 void onTimerFired(TimerBase*); | 127 void onAnimationFrame(double timestamp); |
| 121 | 128 |
| 122 device::mojom::blink::SensorType m_type; | 129 device::mojom::blink::SensorType m_type; |
| 123 device::mojom::blink::ReportingMode m_mode; | 130 device::mojom::blink::ReportingMode m_mode; |
| 124 Member<SensorProviderProxy> m_provider; | 131 Member<SensorProviderProxy> m_provider; |
| 125 using ObserversSet = HeapHashSet<WeakMember<Observer>>; | 132 using ObserversSet = HeapHashSet<WeakMember<Observer>>; |
| 126 ObserversSet m_observers; | 133 ObserversSet m_observers; |
| 127 | 134 |
| 128 device::mojom::blink::SensorPtr m_sensor; | 135 device::mojom::blink::SensorPtr m_sensor; |
| 129 device::mojom::blink::SensorConfigurationPtr m_defaultConfig; | 136 device::mojom::blink::SensorConfigurationPtr m_defaultConfig; |
| 130 mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding; | 137 mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding; |
| 131 | 138 |
| 132 enum State { Uninitialized, Initializing, Initialized }; | 139 enum State { Uninitialized, Initializing, Initialized }; |
| 133 State m_state; | 140 State m_state; |
| 134 mojo::ScopedSharedBufferHandle m_sharedBufferHandle; | 141 mojo::ScopedSharedBufferHandle m_sharedBufferHandle; |
| 135 mojo::ScopedSharedBufferMapping m_sharedBuffer; | 142 mojo::ScopedSharedBufferMapping m_sharedBuffer; |
| 136 bool m_suspended; | 143 bool m_suspended; |
| 144 WeakMember<Document> m_document; | |
|
haraken
2016/12/06 02:38:30
Is there any reason you want to make it a WeakMemb
| |
| 137 Member<SensorReading> m_reading; | 145 Member<SensorReading> m_reading; |
| 138 std::unique_ptr<SensorReadingFactory> m_readingFactory; | 146 std::unique_ptr<SensorReadingFactory> m_readingFactory; |
| 139 double m_maximumFrequency; | 147 double m_maximumFrequency; |
| 140 | 148 |
| 141 // Used for continious reporting mode. | 149 // Used for continious reporting mode. |
| 142 Timer<SensorProxy> m_timer; | 150 Member<SensorFrameRequestCallback> m_rafCallback; |
| 151 int m_rafCallbackId; | |
| 143 WTF::Vector<double> m_frequenciesUsed; | 152 WTF::Vector<double> m_frequenciesUsed; |
| 153 double m_lastRafTimestamp; | |
| 144 | 154 |
| 145 using ReadingBuffer = device::SensorReadingSharedBuffer; | 155 using ReadingBuffer = device::SensorReadingSharedBuffer; |
| 146 static_assert( | 156 static_assert( |
| 147 sizeof(ReadingBuffer) == | 157 sizeof(ReadingBuffer) == |
| 148 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, | 158 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, |
| 149 "Check reading buffer size for tests"); | 159 "Check reading buffer size for tests"); |
| 150 }; | 160 }; |
| 151 | 161 |
| 152 } // namespace blink | 162 } // namespace blink |
| 153 | 163 |
| 154 #endif // SensorProxy_h | 164 #endif // SensorProxy_h |
| OLD | NEW |