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 #include "device_motion_event_pump.h" | 5 #include "content/renderer/device_sensors/device_motion_event_pump.h" |
6 | 6 |
| 7 #include "base/memory/ptr_util.h" |
7 #include "content/public/renderer/render_thread.h" | 8 #include "content/public/renderer/render_thread.h" |
| 9 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eMotionData.h" |
8 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eMotionListener.h" | 10 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eMotionListener.h" |
9 | 11 |
10 namespace content { | 12 namespace content { |
11 | 13 |
12 DeviceMotionEventPump::DeviceMotionEventPump(RenderThread* thread) | 14 DeviceMotionEventPump::DeviceMotionEventPump(RenderThread* thread) |
13 : DeviceSensorMojoClientMixin< | 15 : DeviceSensorEventPump<blink::WebDeviceMotionListener>(thread) {} |
14 DeviceSensorEventPump<blink::WebDeviceMotionListener>, | |
15 device::mojom::MotionSensor>(thread) {} | |
16 | 16 |
17 DeviceMotionEventPump::~DeviceMotionEventPump() { | 17 DeviceMotionEventPump::~DeviceMotionEventPump() {} |
| 18 |
| 19 void DeviceMotionEventPump::SendStartMessage() { |
| 20 sensors_.push_back(base::MakeUnique<SensorEntry>(this)); |
| 21 sensors_.push_back(base::MakeUnique<SensorEntry>(this)); |
| 22 sensors_.push_back(base::MakeUnique<SensorEntry>(this)); |
| 23 GetSensor(device::mojom::SensorType::ACCELEROMETER, sensors_[0].get()); |
| 24 GetSensor(device::mojom::SensorType::LINEAR_ACCELERATION, sensors_[1].get()); |
| 25 GetSensor(device::mojom::SensorType::GYROSCOPE, sensors_[2].get()); |
| 26 } |
| 27 |
| 28 void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) { |
| 29 blink::WebDeviceMotionData data = |
| 30 *static_cast<blink::WebDeviceMotionData*>(fake_data); |
| 31 listener()->DidChangeDeviceMotion(data); |
18 } | 32 } |
19 | 33 |
20 void DeviceMotionEventPump::FireEvent() { | 34 void DeviceMotionEventPump::FireEvent() { |
| 35 blink::WebDeviceMotionData data; |
| 36 data.interval = kDefaultPumpDelayMicroseconds; |
| 37 |
21 DCHECK(listener()); | 38 DCHECK(listener()); |
22 device::MotionData data; | 39 |
23 if (reader_->GetLatestData(&data) && data.all_available_sensors_are_active) | 40 if (GetDataFromSharedMemory(&data)) |
24 listener()->DidChangeDeviceMotion(data); | 41 listener()->DidChangeDeviceMotion(data); |
25 } | 42 } |
26 | 43 |
27 bool DeviceMotionEventPump::InitializeReader(base::SharedMemoryHandle handle) { | 44 bool DeviceMotionEventPump::CanStart() const { |
28 if (!reader_) | 45 DCHECK_EQ(3u, sensors_.size()); |
29 reader_.reset(new DeviceMotionSharedMemoryReader()); | 46 |
30 return reader_->Initialize(handle); | 47 // device motion spec does not require all sensors to be available. |
| 48 return (static_cast<size_t>(num_sensors_tried_) == sensors_.size()) && |
| 49 AllAvailableSensorsAreActive(); |
31 } | 50 } |
32 | 51 |
33 void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) { | 52 bool DeviceMotionEventPump::AllAvailableSensorsAreActive() const { |
34 device::MotionData data = *static_cast<device::MotionData*>(fake_data); | 53 int num_active_sensors = 0; |
| 54 for (const auto& sensor : sensors_) { |
| 55 if (sensor->active) |
| 56 ++num_active_sensors; |
| 57 } |
35 | 58 |
36 listener()->DidChangeDeviceMotion(data); | 59 return num_available_sensors_ == num_active_sensors; |
| 60 } |
| 61 |
| 62 bool DeviceMotionEventPump::GetDataFromSharedMemory( |
| 63 blink::WebDeviceMotionData* data) { |
| 64 if (!AllAvailableSensorsAreActive()) |
| 65 return false; |
| 66 |
| 67 bool has_data = false; |
| 68 for (auto& sensor : sensors_) { |
| 69 if (!sensor->SensorReadingUpdated()) |
| 70 continue; |
| 71 |
| 72 double d0 = sensor->reading.values[0].value(); |
| 73 double d1 = sensor->reading.values[1].value(); |
| 74 double d2 = sensor->reading.values[2].value(); |
| 75 switch (sensor->type) { |
| 76 case device::mojom::SensorType::ACCELEROMETER: |
| 77 data->acceleration_including_gravity_x = d0; |
| 78 data->acceleration_including_gravity_y = d1; |
| 79 data->acceleration_including_gravity_z = d2; |
| 80 data->has_acceleration_including_gravity_x = true; |
| 81 data->has_acceleration_including_gravity_y = true; |
| 82 data->has_acceleration_including_gravity_z = true; |
| 83 has_data = true; |
| 84 break; |
| 85 case device::mojom::SensorType::LINEAR_ACCELERATION: |
| 86 data->acceleration_x = d0; |
| 87 data->acceleration_y = d1; |
| 88 data->acceleration_z = d2; |
| 89 data->has_acceleration_x = true; |
| 90 data->has_acceleration_y = true; |
| 91 data->has_acceleration_z = true; |
| 92 has_data = true; |
| 93 break; |
| 94 case device::mojom::SensorType::GYROSCOPE: |
| 95 data->rotation_rate_alpha = d0; |
| 96 data->rotation_rate_beta = d1; |
| 97 data->rotation_rate_gamma = d2; |
| 98 data->has_rotation_rate_alpha = true; |
| 99 data->has_rotation_rate_beta = true; |
| 100 data->has_rotation_rate_gamma = true; |
| 101 has_data = true; |
| 102 break; |
| 103 default: |
| 104 NOTREACHED(); |
| 105 break; |
| 106 } |
| 107 } |
| 108 |
| 109 return has_data; |
37 } | 110 } |
38 | 111 |
39 } // namespace content | 112 } // namespace content |
OLD | NEW |