OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_ | 5 #ifndef CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_ |
6 #define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_ | 6 #define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | |
10 #include <vector> | |
11 | 9 |
12 #include "base/bind.h" | |
13 #include "base/bind_helpers.h" | |
14 #include "base/macros.h" | 10 #include "base/macros.h" |
15 #include "base/time/time.h" | 11 #include "content/renderer/device_sensors/device_sensor_event_pump.h" |
16 #include "base/timer/timer.h" | 12 #include "content/renderer/shared_memory_seqlock_reader.h" |
17 #include "content/public/renderer/platform_event_observer.h" | |
18 #include "content/renderer/render_thread_impl.h" | |
19 #include "device/generic_sensor/public/cpp/sensor_reading.h" | |
20 #include "device/generic_sensor/public/interfaces/sensor.mojom.h" | |
21 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" | |
22 #include "device/sensors/public/cpp/motion_data.h" | 13 #include "device/sensors/public/cpp/motion_data.h" |
23 #include "mojo/public/cpp/bindings/binding.h" | 14 #include "device/sensors/public/interfaces/motion.mojom.h" |
24 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eMotionListener.h" | 15 |
| 16 namespace blink { |
| 17 class WebDeviceMotionListener; |
| 18 } |
25 | 19 |
26 namespace content { | 20 namespace content { |
27 | 21 |
| 22 typedef SharedMemorySeqLockReader<device::MotionData> |
| 23 DeviceMotionSharedMemoryReader; |
| 24 |
28 class CONTENT_EXPORT DeviceMotionEventPump | 25 class CONTENT_EXPORT DeviceMotionEventPump |
29 : NON_EXPORTED_BASE( | 26 : public DeviceSensorMojoClientMixin< |
30 public PlatformEventObserver<blink::WebDeviceMotionListener>) { | 27 DeviceSensorEventPump<blink::WebDeviceMotionListener>, |
| 28 device::mojom::MotionSensor> { |
31 public: | 29 public: |
32 explicit DeviceMotionEventPump(RenderThread* thread); | 30 explicit DeviceMotionEventPump(RenderThread* thread); |
33 ~DeviceMotionEventPump() override; | 31 ~DeviceMotionEventPump() override; |
34 | 32 |
35 // PlatformEventObserver: | 33 // PlatformEventObserver. |
36 void Start(blink::WebPlatformEventListener* listener) override; | |
37 void Stop() override; | |
38 void SendStartMessage() override; | |
39 void SendStopMessage() override; | |
40 void SendFakeDataForTesting(void* fake_data) override; | 34 void SendFakeDataForTesting(void* fake_data) override; |
41 | 35 |
42 protected: | 36 protected: |
43 // Default rate for firing events. | 37 void FireEvent() override; |
44 static constexpr int kDefaultPumpFrequencyHz = 60; | 38 bool InitializeReader(base::SharedMemoryHandle handle) override; |
45 static constexpr int kDefaultPumpDelayMicroseconds = | |
46 base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz; | |
47 | 39 |
48 struct CONTENT_EXPORT SensorEntry : public device::mojom::SensorClient { | 40 std::unique_ptr<DeviceMotionSharedMemoryReader> reader_; |
49 SensorEntry(DeviceMotionEventPump* pump, | |
50 device::mojom::SensorType sensor_type); | |
51 ~SensorEntry() override; | |
52 | |
53 // device::mojom::SensorClient: | |
54 void RaiseError() override; | |
55 void SensorReadingChanged() override; | |
56 | |
57 // Mojo callback for SensorProvider::GetSensor(). | |
58 void OnSensorCreated(device::mojom::SensorInitParamsPtr params, | |
59 device::mojom::SensorClientRequest client_request); | |
60 | |
61 // Mojo callback for Sensor::AddConfiguration(). | |
62 void OnSensorAddConfiguration(bool success); | |
63 | |
64 void HandleSensorError(); | |
65 | |
66 bool SensorReadingCouldBeRead(); | |
67 | |
68 DeviceMotionEventPump* event_pump; | |
69 device::mojom::SensorPtr sensor; | |
70 device::mojom::SensorType type; | |
71 device::mojom::ReportingMode mode; | |
72 device::PlatformSensorConfiguration default_config; | |
73 mojo::ScopedSharedBufferHandle shared_buffer_handle; | |
74 mojo::ScopedSharedBufferMapping shared_buffer; | |
75 device::SensorReading reading; | |
76 mojo::Binding<device::mojom::SensorClient> client_binding; | |
77 }; | |
78 | |
79 friend struct SensorEntry; | |
80 | |
81 virtual void FireEvent(); | |
82 | |
83 void DidStart(); | |
84 | |
85 SensorEntry accelerometer_; | |
86 SensorEntry linear_acceleration_sensor_; | |
87 SensorEntry gyroscope_; | |
88 | |
89 private: | |
90 // TODO(juncai): refactor DeviceMotionEventPump to use DeviceSensorEventPump | |
91 // when refactoring DeviceOrientation. | |
92 // | |
93 // The pump is a tri-state automaton with allowed transitions as follows: | |
94 // STOPPED -> PENDING_START | |
95 // PENDING_START -> RUNNING | |
96 // PENDING_START -> STOPPED | |
97 // RUNNING -> STOPPED | |
98 enum class PumpState { STOPPED, RUNNING, PENDING_START }; | |
99 | |
100 bool CanStart() const; | |
101 void GetDataFromSharedMemory(device::MotionData* data); | |
102 void GetSensor(SensorEntry* sensor_entry); | |
103 void HandleSensorProviderError(); | |
104 | |
105 mojo::InterfacePtr<device::mojom::SensorProvider> sensor_provider_; | |
106 PumpState state_; | |
107 base::RepeatingTimer timer_; | |
108 | 41 |
109 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPump); | 42 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPump); |
110 }; | 43 }; |
111 | 44 |
112 } // namespace content | 45 } // namespace content |
113 | 46 |
114 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_ | 47 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_ |
OLD | NEW |