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 "device/generic_sensor/public/cpp/sensor_reading.h" | 9 #include "device/generic_sensor/public/cpp/sensor_reading.h" |
10 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 10 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
11 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom-blink.h" | 11 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom-blink.h" |
12 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
13 #include "platform/Supplementable.h" | 13 #include "platform/Supplementable.h" |
14 #include "platform/heap/Handle.h" | 14 #include "platform/heap/Handle.h" |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 | 17 |
18 class SensorProviderProxy; | 18 class SensorProviderProxy; |
| 19 class SensorReading; |
| 20 class SensorReadingFactory; |
19 | 21 |
20 // This class wraps 'Sensor' mojo interface and used by multiple | 22 // This class wraps 'Sensor' mojo interface and used by multiple |
21 // JS sensor instances of the same type (within a single frame). | 23 // JS sensor instances of the same type (within a single frame). |
22 class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>, | 24 class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>, |
23 public device::mojom::blink::SensorClient { | 25 public device::mojom::blink::SensorClient { |
24 USING_PRE_FINALIZER(SensorProxy, dispose); | 26 USING_PRE_FINALIZER(SensorProxy, dispose); |
25 WTF_MAKE_NONCOPYABLE(SensorProxy); | 27 WTF_MAKE_NONCOPYABLE(SensorProxy); |
26 | 28 |
27 public: | 29 public: |
28 class Observer : public GarbageCollectedMixin { | 30 class Observer : public GarbageCollectedMixin { |
(...skipping 25 matching lines...) Expand all Loading... |
54 std::unique_ptr<Function<void(bool)>>); | 56 std::unique_ptr<Function<void(bool)>>); |
55 void removeConfiguration(device::mojom::blink::SensorConfigurationPtr, | 57 void removeConfiguration(device::mojom::blink::SensorConfigurationPtr, |
56 std::unique_ptr<Function<void(bool)>>); | 58 std::unique_ptr<Function<void(bool)>>); |
57 | 59 |
58 void suspend(); | 60 void suspend(); |
59 void resume(); | 61 void resume(); |
60 | 62 |
61 device::mojom::blink::SensorType type() const { return m_type; } | 63 device::mojom::blink::SensorType type() const { return m_type; } |
62 device::mojom::blink::ReportingMode reportingMode() const { return m_mode; } | 64 device::mojom::blink::ReportingMode reportingMode() const { return m_mode; } |
63 | 65 |
64 using Reading = device::SensorReading; | 66 // The |SensorReading| instance which is shared between sensor instances |
65 | 67 // of the same type. |
66 const Reading& reading() const { return m_reading; } | 68 // Note: the returned value is reset after updateSensorReading() call. |
| 69 SensorReading* sensorReading() const { return m_reading; } |
67 | 70 |
68 const device::mojom::blink::SensorConfiguration* defaultConfig() const; | 71 const device::mojom::blink::SensorConfiguration* defaultConfig() const; |
69 | 72 |
70 // Updates internal reading from shared buffer. | 73 // Updates sensor reading from shared buffer. |
71 void updateInternalReading(); | 74 void updateSensorReading(); |
72 | 75 |
73 DECLARE_VIRTUAL_TRACE(); | 76 DECLARE_VIRTUAL_TRACE(); |
74 | 77 |
75 private: | 78 private: |
76 friend class SensorProviderProxy; | 79 friend class SensorProviderProxy; |
77 SensorProxy(device::mojom::blink::SensorType, SensorProviderProxy*); | 80 SensorProxy(device::mojom::blink::SensorType, |
| 81 SensorProviderProxy*, |
| 82 std::unique_ptr<SensorReadingFactory>); |
78 | 83 |
79 // device::mojom::blink::SensorClient overrides. | 84 // device::mojom::blink::SensorClient overrides. |
80 void RaiseError() override; | 85 void RaiseError() override; |
81 void SensorReadingChanged() override; | 86 void SensorReadingChanged() override; |
82 | 87 |
83 // Generic handler for a fatal error. | 88 // Generic handler for a fatal error. |
84 // String parameters are intentionally copied. | 89 // String parameters are intentionally copied. |
85 void handleSensorError(ExceptionCode = UnknownError, | 90 void handleSensorError(ExceptionCode = UnknownError, |
86 String sanitizedMessage = String(), | 91 String sanitizedMessage = String(), |
87 String unsanitizedMessage = String()); | 92 String unsanitizedMessage = String()); |
88 | 93 |
89 void onSensorCreated(device::mojom::blink::SensorInitParamsPtr, | 94 void onSensorCreated(device::mojom::blink::SensorInitParamsPtr, |
90 device::mojom::blink::SensorClientRequest); | 95 device::mojom::blink::SensorClientRequest); |
91 | 96 |
92 bool tryReadFromBuffer(); | 97 bool tryReadFromBuffer(device::SensorReading& result); |
93 | 98 |
94 device::mojom::blink::SensorType m_type; | 99 device::mojom::blink::SensorType m_type; |
95 device::mojom::blink::ReportingMode m_mode; | 100 device::mojom::blink::ReportingMode m_mode; |
96 Member<SensorProviderProxy> m_provider; | 101 Member<SensorProviderProxy> m_provider; |
97 using ObserversSet = HeapHashSet<WeakMember<Observer>>; | 102 using ObserversSet = HeapHashSet<WeakMember<Observer>>; |
98 ObserversSet m_observers; | 103 ObserversSet m_observers; |
99 | 104 |
100 device::mojom::blink::SensorPtr m_sensor; | 105 device::mojom::blink::SensorPtr m_sensor; |
101 device::mojom::blink::SensorConfigurationPtr m_defaultConfig; | 106 device::mojom::blink::SensorConfigurationPtr m_defaultConfig; |
102 mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding; | 107 mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding; |
103 | 108 |
104 enum State { Uninitialized, Initializing, Initialized }; | 109 enum State { Uninitialized, Initializing, Initialized }; |
105 State m_state; | 110 State m_state; |
106 mojo::ScopedSharedBufferHandle m_sharedBufferHandle; | 111 mojo::ScopedSharedBufferHandle m_sharedBufferHandle; |
107 mojo::ScopedSharedBufferMapping m_sharedBuffer; | 112 mojo::ScopedSharedBufferMapping m_sharedBuffer; |
108 Reading m_reading; | |
109 bool m_suspended; | 113 bool m_suspended; |
| 114 Member<SensorReading> m_reading; |
| 115 std::unique_ptr<SensorReadingFactory> m_readingFactory; |
| 116 |
110 using ReadingBuffer = device::SensorReadingSharedBuffer; | 117 using ReadingBuffer = device::SensorReadingSharedBuffer; |
111 static_assert( | 118 static_assert( |
112 sizeof(ReadingBuffer) == | 119 sizeof(ReadingBuffer) == |
113 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, | 120 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, |
114 "Check reading buffer size for tests"); | 121 "Check reading buffer size for tests"); |
115 }; | 122 }; |
116 | 123 |
117 } // namespace blink | 124 } // namespace blink |
118 | 125 |
119 #endif // SensorProxy_h | 126 #endif // SensorProxy_h |
OLD | NEW |