OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SensorProxy_h |
| 6 #define SensorProxy_h |
| 7 |
| 8 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| 9 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom-blink.h" |
| 10 #include "modules/ModulesExport.h" |
| 11 #include "mojo/public/cpp/bindings/binding.h" |
| 12 #include "platform/Supplementable.h" |
| 13 #include "platform/heap/Handle.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 class SensorProviderProxy; |
| 18 |
| 19 // This class wraps 'Sensor' mojo interface and used by multiple |
| 20 // JS sensor instances (within a single frame). |
| 21 class MODULES_EXPORT SensorProxy final |
| 22 : public GarbageCollectedFinalized<SensorProxy> |
| 23 , public device::mojom::blink::SensorClient { |
| 24 USING_PRE_FINALIZER(SensorProxy, dispose); |
| 25 WTF_MAKE_NONCOPYABLE(SensorProxy); |
| 26 public: |
| 27 class Observer : public GarbageCollectedMixin { |
| 28 public: |
| 29 // Has valid 'Sensor' binding, add(remove)Configuration() |
| 30 // methods can be called. |
| 31 virtual void onSensorInitialized() {} |
| 32 // Platfrom sensort reading has changed (for 'ONCHANGE' reporting mode). |
| 33 virtual void onSensorReadingChanged() {} |
| 34 // An error has occurred. |
| 35 virtual void onSensorError() {} |
| 36 }; |
| 37 |
| 38 ~SensorProxy(); |
| 39 |
| 40 void dispose(); |
| 41 |
| 42 void addObserver(Observer*); |
| 43 void removeObserver(Observer*); |
| 44 |
| 45 void initialize(); |
| 46 |
| 47 bool isInitializing() const { return m_state == Initializing; } |
| 48 bool isInitialized() const { return m_state == Initialized; } |
| 49 |
| 50 void addConfiguration(device::mojom::blink::SensorConfigurationPtr, std::uni
que_ptr<Function<void(bool)>>); |
| 51 void removeConfiguration(device::mojom::blink::SensorConfigurationPtr, std::
unique_ptr<Function<void(bool)>>); |
| 52 |
| 53 void suspendNotification(); |
| 54 void resumeNotification(); |
| 55 |
| 56 device::mojom::blink::SensorType type() const { return m_type; } |
| 57 device::mojom::blink::ReportingMode reportingMode() const { return m_mode; } |
| 58 |
| 59 template <typename Reading> |
| 60 const Reading* reading() const |
| 61 { |
| 62 DCHECK(m_sharedBuffer); |
| 63 static_assert(sizeof(Reading) == device::mojom::blink::SensorReadBuffer:
:kReadBufferSize, "Check reading size"); |
| 64 return static_cast<Reading*>(m_sharedBuffer.get()); |
| 65 } |
| 66 |
| 67 DECLARE_VIRTUAL_TRACE(); |
| 68 |
| 69 private: |
| 70 friend class SensorProviderProxy; |
| 71 SensorProxy(device::mojom::blink::SensorType, SensorProviderProxy*); |
| 72 |
| 73 // device::mojom::blink::SensorClient overrides. |
| 74 void RaiseError() override; |
| 75 void SensorReadingChanged() override; |
| 76 |
| 77 void handleSensorError(); |
| 78 |
| 79 void onSensorCreated(device::mojom::blink::SensorReadBufferPtr, device::mojo
m::blink::SensorClientRequest); |
| 80 |
| 81 device::mojom::blink::SensorType m_type; |
| 82 device::mojom::blink::ReportingMode m_mode; |
| 83 Member<SensorProviderProxy> m_provider; |
| 84 using ObserversSet = HeapHashSet<WeakMember<Observer>>; |
| 85 ObserversSet m_observers; |
| 86 |
| 87 device::mojom::blink::SensorPtr m_sensor; |
| 88 mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding; |
| 89 enum State { |
| 90 Uninitialized, |
| 91 Initializing, |
| 92 Initialized |
| 93 }; |
| 94 State m_state; |
| 95 mojo::ScopedSharedBufferHandle m_sharedBufferHandle; |
| 96 mojo::ScopedSharedBufferMapping m_sharedBuffer; |
| 97 }; |
| 98 |
| 99 } // namespace blink |
| 100 |
| 101 #endif // SensorProxy_h |
OLD | NEW |