Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(463)

Side by Side Diff: content/renderer/device_sensors/device_motion_event_pump.h

Issue 2896583005: Reland: Refactor DeviceMotionEventPump to use //device/generic_sensor instead of //device/sensors (Closed)
Patch Set: address comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
timvolodine 2017/05/23 12:33:40 this implementation looks much more complicated no
juncai 2017/05/23 21:33:55 This implementation contains some common code that
timvolodine 2017/05/24 15:30:16 Thanks for the pointer, I'll have a look. Just not
juncai 2017/05/26 02:38:53 Yes, there are plans to do that and the idea is si
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 SensorEntry(DeviceMotionEventPump* pump,
52 device::mojom::SensorType sensor_type);
53 ~SensorEntry() override;
54
55 // device::mojom::SensorClient:
56 void RaiseError() override;
57 void SensorReadingChanged() override;
58
59 // Mojo callback for SensorProvider::GetSensor().
60 void OnSensorCreated(device::mojom::SensorInitParamsPtr params,
61 device::mojom::SensorClientRequest client_request);
62
63 // Mojo callback for Sensor::AddConfiguration().
64 void OnSensorAddConfiguration(bool success);
65
66 void HandleSensorError();
67
68 // Updates sensor reading from shared buffer.
69 bool UpdateSensorReading();
70
71 bool TryReadFromBuffer(device::SensorReading* result);
72
73 bool SensorReadingUpdated();
74
75 DeviceMotionEventPump* event_pump;
timvolodine 2017/05/24 15:30:16 we normally use _ for members, e.g. event_pump_?
juncai 2017/05/26 02:38:53 This is inside a struct and it doesn't need the tr
76 device::mojom::SensorPtr sensor;
77 device::mojom::SensorType type;
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 SensorEntry accelerometer_sensor_;
94 SensorEntry linear_acceleration_sensor_;
95 SensorEntry gyroscope_sensor_;
96 int pump_delay_microseconds_;
97
98 private:
99 // The pump is a tri-state automaton with allowed transitions as follows:
100 // STOPPED -> PENDING_START
101 // PENDING_START -> RUNNING
102 // PENDING_START -> STOPPED
103 // RUNNING -> STOPPED
104 enum class PumpState { STOPPED, RUNNING, PENDING_START };
105
106 bool CanStart() const;
107 bool GetDataFromSharedMemory(blink::WebDeviceMotionData* data);
108 void GetSensor(SensorEntry* sensor_entry);
109 void HandleSensorProviderError();
110
111 mojo::InterfacePtr<device::mojom::SensorProvider> sensor_provider_;
112 // The number of sensors that are tried by calling
113 // SensorProvider::GetSensor() and Sensor::AddConfiguration().
114 int num_sensors_tried_;
115 PumpState state_;
116 base::RepeatingTimer timer_;
41 117
42 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPump); 118 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPump);
43 }; 119 };
44 120
45 } // namespace content 121 } // namespace content
46 122
47 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_ 123 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_MOTION_EVENT_PUMP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698