| Index: content/renderer/device_sensors/device_motion_event_pump.cc
|
| diff --git a/content/renderer/device_sensors/device_motion_event_pump.cc b/content/renderer/device_sensors/device_motion_event_pump.cc
|
| index 94d7f3f7b1e5d81f6c5aaef1c2f218348f22cf4d..92cb1e2d57bb533a34cf8aa706017565ef4b4ed8 100644
|
| --- a/content/renderer/device_sensors/device_motion_event_pump.cc
|
| +++ b/content/renderer/device_sensors/device_motion_event_pump.cc
|
| @@ -2,38 +2,111 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "device_motion_event_pump.h"
|
| +#include "content/renderer/device_sensors/device_motion_event_pump.h"
|
|
|
| +#include "base/memory/ptr_util.h"
|
| #include "content/public/renderer/render_thread.h"
|
| +#include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceMotionData.h"
|
| #include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceMotionListener.h"
|
|
|
| namespace content {
|
|
|
| DeviceMotionEventPump::DeviceMotionEventPump(RenderThread* thread)
|
| - : DeviceSensorMojoClientMixin<
|
| - DeviceSensorEventPump<blink::WebDeviceMotionListener>,
|
| - device::mojom::MotionSensor>(thread) {}
|
| + : DeviceSensorEventPump<blink::WebDeviceMotionListener>(thread) {}
|
|
|
| -DeviceMotionEventPump::~DeviceMotionEventPump() {
|
| +DeviceMotionEventPump::~DeviceMotionEventPump() {}
|
| +
|
| +void DeviceMotionEventPump::SendStartMessage() {
|
| + sensors_.push_back(base::MakeUnique<SensorEntry>(this));
|
| + sensors_.push_back(base::MakeUnique<SensorEntry>(this));
|
| + sensors_.push_back(base::MakeUnique<SensorEntry>(this));
|
| + GetSensor(device::mojom::SensorType::ACCELEROMETER, sensors_[0].get());
|
| + GetSensor(device::mojom::SensorType::LINEAR_ACCELERATION, sensors_[1].get());
|
| + GetSensor(device::mojom::SensorType::GYROSCOPE, sensors_[2].get());
|
| +}
|
| +
|
| +void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) {
|
| + blink::WebDeviceMotionData data =
|
| + *static_cast<blink::WebDeviceMotionData*>(fake_data);
|
| + listener()->DidChangeDeviceMotion(data);
|
| }
|
|
|
| void DeviceMotionEventPump::FireEvent() {
|
| + blink::WebDeviceMotionData data;
|
| + data.interval = kDefaultPumpDelayMicroseconds;
|
| +
|
| DCHECK(listener());
|
| - device::MotionData data;
|
| - if (reader_->GetLatestData(&data) && data.all_available_sensors_are_active)
|
| +
|
| + if (GetDataFromSharedMemory(&data))
|
| listener()->DidChangeDeviceMotion(data);
|
| }
|
|
|
| -bool DeviceMotionEventPump::InitializeReader(base::SharedMemoryHandle handle) {
|
| - if (!reader_)
|
| - reader_.reset(new DeviceMotionSharedMemoryReader());
|
| - return reader_->Initialize(handle);
|
| +bool DeviceMotionEventPump::CanStart() const {
|
| + DCHECK_EQ(3u, sensors_.size());
|
| +
|
| + // device motion spec does not require all sensors to be available.
|
| + return (static_cast<size_t>(num_sensors_tried_) == sensors_.size()) &&
|
| + AllAvailableSensorsAreActive();
|
| }
|
|
|
| -void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) {
|
| - device::MotionData data = *static_cast<device::MotionData*>(fake_data);
|
| +bool DeviceMotionEventPump::AllAvailableSensorsAreActive() const {
|
| + int num_active_sensors = 0;
|
| + for (const auto& sensor : sensors_) {
|
| + if (sensor->active)
|
| + ++num_active_sensors;
|
| + }
|
|
|
| - listener()->DidChangeDeviceMotion(data);
|
| + return num_available_sensors_ == num_active_sensors;
|
| +}
|
| +
|
| +bool DeviceMotionEventPump::GetDataFromSharedMemory(
|
| + blink::WebDeviceMotionData* data) {
|
| + if (!AllAvailableSensorsAreActive())
|
| + return false;
|
| +
|
| + bool has_data = false;
|
| + for (auto& sensor : sensors_) {
|
| + if (!sensor->SensorReadingUpdated())
|
| + continue;
|
| +
|
| + double d0 = sensor->reading.values[0].value();
|
| + double d1 = sensor->reading.values[1].value();
|
| + double d2 = sensor->reading.values[2].value();
|
| + switch (sensor->type) {
|
| + case device::mojom::SensorType::ACCELEROMETER:
|
| + data->acceleration_including_gravity_x = d0;
|
| + data->acceleration_including_gravity_y = d1;
|
| + data->acceleration_including_gravity_z = d2;
|
| + data->has_acceleration_including_gravity_x = true;
|
| + data->has_acceleration_including_gravity_y = true;
|
| + data->has_acceleration_including_gravity_z = true;
|
| + has_data = true;
|
| + break;
|
| + case device::mojom::SensorType::LINEAR_ACCELERATION:
|
| + data->acceleration_x = d0;
|
| + data->acceleration_y = d1;
|
| + data->acceleration_z = d2;
|
| + data->has_acceleration_x = true;
|
| + data->has_acceleration_y = true;
|
| + data->has_acceleration_z = true;
|
| + has_data = true;
|
| + break;
|
| + case device::mojom::SensorType::GYROSCOPE:
|
| + data->rotation_rate_alpha = d0;
|
| + data->rotation_rate_beta = d1;
|
| + data->rotation_rate_gamma = d2;
|
| + data->has_rotation_rate_alpha = true;
|
| + data->has_rotation_rate_beta = true;
|
| + data->has_rotation_rate_gamma = true;
|
| + has_data = true;
|
| + break;
|
| + default:
|
| + NOTREACHED();
|
| + break;
|
| + }
|
| + }
|
| +
|
| + return has_data;
|
| }
|
|
|
| } // namespace content
|
|
|