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> | |
9 | 11 |
12 #include "base/bind.h" | |
13 #include "base/bind_helpers.h" | |
10 #include "base/macros.h" | 14 #include "base/macros.h" |
11 #include "content/renderer/device_sensors/device_sensor_event_pump.h" | 15 #include "base/time/time.h" |
12 #include "content/renderer/shared_memory_seqlock_reader.h" | 16 #include "base/timer/timer.h" |
13 #include "device/sensors/public/cpp/motion_data.h" | 17 #include "content/public/renderer/platform_event_observer.h" |
14 #include "device/sensors/public/interfaces/motion.mojom.h" | 18 #include "content/renderer/render_thread_impl.h" |
15 | 19 #include "device/generic_sensor/public/cpp/sensor_reading.h" |
16 namespace blink { | 20 #include "device/generic_sensor/public/interfaces/sensor.mojom.h" |
17 class WebDeviceMotionListener; | 21 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" |
18 } | 22 #include "mojo/public/cpp/bindings/binding.h" |
23 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eMotionData.h" | |
24 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eMotionListener.h" | |
19 | 25 |
20 namespace content { | 26 namespace content { |
21 | 27 |
22 typedef SharedMemorySeqLockReader<device::MotionData> | |
23 DeviceMotionSharedMemoryReader; | |
24 | |
25 class CONTENT_EXPORT DeviceMotionEventPump | 28 class CONTENT_EXPORT DeviceMotionEventPump |
26 : public DeviceSensorMojoClientMixin< | 29 : NON_EXPORTED_BASE( |
27 DeviceSensorEventPump<blink::WebDeviceMotionListener>, | 30 public PlatformEventObserver<blink::WebDeviceMotionListener>) { |
28 device::mojom::MotionSensor> { | |
29 public: | 31 public: |
30 explicit DeviceMotionEventPump(RenderThread* thread); | 32 explicit DeviceMotionEventPump(RenderThread* thread); |
31 ~DeviceMotionEventPump() override; | 33 ~DeviceMotionEventPump() override; |
32 | 34 |
33 // PlatformEventObserver. | 35 // PlatformEventObserver: |
36 void Start(blink::WebPlatformEventListener* listener) override; | |
37 void Stop() override; | |
38 void SendStartMessage() override; | |
39 void SendStopMessage() override; | |
34 void SendFakeDataForTesting(void* fake_data) override; | 40 void SendFakeDataForTesting(void* fake_data) override; |
35 | 41 |
36 protected: | 42 protected: |
37 void FireEvent() override; | 43 // Default rate for firing events. |
38 bool InitializeReader(base::SharedMemoryHandle handle) override; | 44 static constexpr int kDefaultPumpFrequencyHz = 60; |
45 static constexpr int kDefaultPumpDelayMicroseconds = | |
46 base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz; | |
39 | 47 |
40 std::unique_ptr<DeviceMotionSharedMemoryReader> reader_; | 48 static constexpr int kMaxReadAttemptsCount = 10; |
49 | |
50 struct CONTENT_EXPORT SensorEntry : public device::mojom::SensorClient { | |
51 explicit SensorEntry(DeviceMotionEventPump* pump); | |
52 ~SensorEntry() override; | |
53 | |
54 // device::mojom::SensorClient: | |
55 void RaiseError() override; | |
56 void SensorReadingChanged() override; | |
57 | |
58 // Mojo callback for SensorProvider::GetSensor(). | |
59 void OnSensorCreated(device::mojom::SensorInitParamsPtr params, | |
60 device::mojom::SensorClientRequest client_request); | |
61 | |
62 // Mojo callback for Sensor::AddConfiguration(). | |
63 void OnSensorAddConfiguration(bool success); | |
64 | |
65 void HandleSensorError(); | |
66 | |
67 // Updates sensor reading from shared buffer. | |
68 bool UpdateSensorReading(); | |
69 | |
70 bool TryReadFromBuffer(device::SensorReading* result); | |
71 | |
72 bool SensorReadingUpdated(); | |
73 | |
74 DeviceMotionEventPump* event_pump; | |
75 device::mojom::SensorPtr sensor; | |
76 device::mojom::SensorType type; | |
77 bool active; | |
78 device::mojom::ReportingMode mode; | |
79 device::PlatformSensorConfiguration default_config; | |
80 mojo::ScopedSharedBufferHandle shared_buffer_handle; | |
81 mojo::ScopedSharedBufferMapping shared_buffer; | |
82 device::SensorReading reading; | |
83 std::pair<double, double> frequency_limits; | |
84 mojo::Binding<device::mojom::SensorClient> client_binding; | |
85 }; | |
86 | |
87 friend struct SensorEntry; | |
88 | |
89 virtual void FireEvent(); | |
90 | |
91 void DidStart(); | |
92 | |
93 std::vector<std::unique_ptr<SensorEntry>> sensors_; | |
Reilly Grant (use Gerrit)
2017/05/22 20:29:46
Since this sensor type always uses exactly 3 senso
juncai
2017/05/23 02:30:23
Done.
| |
94 int pump_delay_microseconds_; | |
95 // The number of sensors that are available from a successful | |
96 // SensorProvider::GetSensor() call. | |
97 int num_available_sensors_; | |
98 | |
99 private: | |
100 // The pump is a tri-state automaton with allowed transitions as follows: | |
101 // STOPPED -> PENDING_START | |
102 // PENDING_START -> RUNNING | |
103 // PENDING_START -> STOPPED | |
104 // RUNNING -> STOPPED | |
105 enum class PumpState { STOPPED, RUNNING, PENDING_START }; | |
106 | |
107 bool CanStart() const; | |
108 bool AllAvailableSensorsAreActive() const; | |
109 bool GetDataFromSharedMemory(blink::WebDeviceMotionData* data); | |
110 void GetSensor(device::mojom::SensorType type); | |
111 void HandleSensorProviderError(); | |
112 | |
113 mojo::InterfacePtr<device::mojom::SensorProvider> sensor_provider_; | |
114 // The number of sensors that are tried obtaining by calling | |
115 // SensorProvider::GetSensor(). | |
116 int num_sensors_tried_; | |
117 PumpState state_; | |
118 base::RepeatingTimer timer_; | |
41 | 119 |
42 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPump); | 120 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPump); |
43 }; | 121 }; |
44 | 122 |
45 } // namespace content | 123 } // namespace content |
46 | 124 |
47 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_ | 125 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_ |
OLD | NEW |