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..1305e6f2c56491b58b51fc3c8b64246f98a29a53 100644 |
--- a/content/renderer/device_sensors/device_motion_event_pump.cc |
+++ b/content/renderer/device_sensors/device_motion_event_pump.cc |
@@ -2,38 +2,294 @@ |
// 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/common/service_names.mojom.h" |
#include "content/public/renderer/render_thread.h" |
+#include "mojo/public/cpp/bindings/interface_request.h" |
+#include "services/device/public/interfaces/constants.mojom.h" |
+#include "services/service_manager/public/cpp/connector.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) {} |
+ : PlatformEventObserver<blink::WebDeviceMotionListener>(thread), |
+ accelerometer_sensor_(this, device::mojom::SensorType::ACCELEROMETER), |
+ linear_acceleration_sensor_( |
+ this, |
+ device::mojom::SensorType::LINEAR_ACCELERATION), |
+ gyroscope_sensor_(this, device::mojom::SensorType::GYROSCOPE), |
+ pump_delay_microseconds_(kDefaultPumpDelayMicroseconds), |
+ num_sensors_tried_(0), |
+ state_(PumpState::STOPPED) {} |
DeviceMotionEventPump::~DeviceMotionEventPump() { |
+ PlatformEventObserver<blink::WebDeviceMotionListener>::StopIfObserving(); |
+} |
+ |
+void DeviceMotionEventPump::Start(blink::WebPlatformEventListener* listener) { |
+ DVLOG(2) << "requested start"; |
+ |
+ if (state_ != PumpState::STOPPED) |
+ return; |
+ |
+ DCHECK(!timer_.IsRunning()); |
+ |
+ PlatformEventObserver<blink::WebDeviceMotionListener>::Start(listener); |
+ state_ = PumpState::PENDING_START; |
+} |
+ |
+void DeviceMotionEventPump::Stop() { |
+ DVLOG(2) << "requested stop"; |
+ |
+ if (state_ == PumpState::STOPPED) |
+ return; |
+ |
+ DCHECK((state_ == PumpState::PENDING_START && !timer_.IsRunning()) || |
+ (state_ == PumpState::RUNNING && timer_.IsRunning())); |
+ |
+ if (timer_.IsRunning()) |
+ timer_.Stop(); |
+ |
+ PlatformEventObserver<blink::WebDeviceMotionListener>::Stop(); |
+ state_ = PumpState::STOPPED; |
+} |
+ |
+void DeviceMotionEventPump::SendStartMessage() { |
+ auto request = mojo::MakeRequest(&sensor_provider_); |
+ |
+ // When running layout tests, those observers should not listen to the |
+ // actual hardware changes. In order to make that happen, don't connect |
+ // the other end of the mojo pipe to anything. |
+ if (RenderThreadImpl::current() && |
+ !RenderThreadImpl::current()->layout_test_mode()) { |
+ RenderThread::Get()->GetConnector()->BindInterface( |
+ device::mojom::kServiceName, std::move(request)); |
+ sensor_provider_.set_connection_error_handler( |
+ base::Bind(&DeviceMotionEventPump::HandleSensorProviderError, |
+ base::Unretained(this))); |
+ GetSensor(&accelerometer_sensor_); |
+ GetSensor(&linear_acceleration_sensor_); |
+ GetSensor(&gyroscope_sensor_); |
+ } |
+} |
+ |
+void DeviceMotionEventPump::SendStopMessage() { |
+ accelerometer_sensor_.HandleSensorError(); |
+ linear_acceleration_sensor_.HandleSensorError(); |
+ gyroscope_sensor_.HandleSensorError(); |
+ num_sensors_tried_ = 0; |
+} |
+ |
+void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) { |
+ blink::WebDeviceMotionData data = |
+ *static_cast<blink::WebDeviceMotionData*>(fake_data); |
+ listener()->DidChangeDeviceMotion(data); |
+} |
+ |
+DeviceMotionEventPump::SensorEntry::SensorEntry( |
+ DeviceMotionEventPump* pump, |
+ device::mojom::SensorType sensor_type) |
+ : event_pump(pump), type(sensor_type), client_binding(this) {} |
+ |
+DeviceMotionEventPump::SensorEntry::~SensorEntry() { |
+ client_binding.Close(); |
+} |
+ |
+void DeviceMotionEventPump::SensorEntry::RaiseError() { |
+ HandleSensorError(); |
+} |
+ |
+void DeviceMotionEventPump::SensorEntry::SensorReadingChanged() { |
+ // Since DeviceMotionEventPump::FireEvent is called in a certain |
+ // frequency, the |shared_buffer| is read frequently, so this |
+ // method doesn't need to be implemented. |
timvolodine
2017/05/24 15:30:16
I guess this method shouldn't be called at all?
juncai
2017/05/26 02:38:53
It depends on the sensor reporting mode:
https://c
timvolodine
2017/05/30 00:53:29
Right, what I meant is that it could have a DCHECK
juncai
2017/05/30 22:26:56
I don't think a DCHECK() or NOTREACHED() can be us
|
+} |
+ |
+void DeviceMotionEventPump::SensorEntry::OnSensorCreated( |
+ device::mojom::SensorInitParamsPtr params, |
+ device::mojom::SensorClientRequest client_request) { |
+ if (!params) { |
+ ++event_pump->num_sensors_tried_; |
+ HandleSensorError(); |
+ if (event_pump->CanStart()) |
+ event_pump->DidStart(); |
+ return; |
+ } |
+ |
+ constexpr size_t kReadBufferSize = sizeof(device::SensorReadingSharedBuffer); |
+ |
+ DCHECK_EQ(0u, params->buffer_offset % kReadBufferSize); |
+ |
+ mode = params->mode; |
+ default_config = params->default_configuration; |
+ |
+ DCHECK(sensor.is_bound()); |
+ client_binding.Bind(std::move(client_request)); |
+ |
+ shared_buffer_handle = std::move(params->memory); |
+ DCHECK(!shared_buffer); |
+ shared_buffer = |
+ shared_buffer_handle->MapAtOffset(kReadBufferSize, params->buffer_offset); |
+ |
+ if (!shared_buffer) { |
+ ++event_pump->num_sensors_tried_; |
+ HandleSensorError(); |
+ if (event_pump->CanStart()) |
+ event_pump->DidStart(); |
+ return; |
+ } |
+ |
+ frequency_limits.first = params->minimum_frequency; |
+ frequency_limits.second = params->maximum_frequency; |
+ |
+ DCHECK_GT(frequency_limits.first, 0.0); |
+ DCHECK_GE(frequency_limits.second, frequency_limits.first); |
+ constexpr double kMaxAllowedFrequency = |
+ device::mojom::SensorConfiguration::kMaxAllowedFrequency; |
+ DCHECK_GE(kMaxAllowedFrequency, frequency_limits.second); |
+ |
+ sensor->AddConfiguration(default_config, |
+ base::Bind(&SensorEntry::OnSensorAddConfiguration, |
+ base::Unretained(this))); |
+} |
+ |
+void DeviceMotionEventPump::SensorEntry::OnSensorAddConfiguration( |
+ bool success) { |
+ ++event_pump->num_sensors_tried_; |
+ if (!success) |
+ HandleSensorError(); |
+ if (event_pump->CanStart()) |
+ event_pump->DidStart(); |
+} |
+ |
+void DeviceMotionEventPump::SensorEntry::HandleSensorError() { |
+ sensor.reset(); |
+ shared_buffer_handle.reset(); |
+ shared_buffer.reset(); |
+ client_binding.Close(); |
+} |
+ |
+bool DeviceMotionEventPump::SensorEntry::UpdateSensorReading() { |
+ int read_attempts = 0; |
+ device::SensorReading reading_data; |
+ while (!TryReadFromBuffer(&reading_data)) { |
+ if (++read_attempts == kMaxReadAttemptsCount) { |
+ HandleSensorError(); |
+ return false; |
+ } |
+ } |
+ |
+ reading = reading_data; |
+ return true; |
+} |
+ |
+bool DeviceMotionEventPump::SensorEntry::TryReadFromBuffer( |
+ device::SensorReading* result) { |
timvolodine
2017/05/24 15:30:16
maybe reader_ i.e. SharedMemorySeqlockReader can b
juncai
2017/05/26 02:38:52
The |shared_buffer| is a mojo::ScopedSharedBufferM
timvolodine
2017/05/30 00:53:29
I see, maybe we could have something similar for t
juncai
2017/05/30 22:26:56
Filed an issue for this for a possible follow-up:
|
+ const device::SensorReadingSharedBuffer* buffer = |
+ static_cast<const device::SensorReadingSharedBuffer*>( |
+ shared_buffer.get()); |
+ const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); |
+ auto version = seqlock.ReadBegin(); |
+ auto reading_data = buffer->reading; |
+ if (seqlock.ReadRetry(version)) |
+ return false; |
+ *result = reading_data; |
+ return true; |
+} |
+ |
+bool DeviceMotionEventPump::SensorEntry::SensorReadingUpdated() { |
+ return sensor && UpdateSensorReading(); |
} |
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); |
+void DeviceMotionEventPump::DidStart() { |
+ DVLOG(2) << "did start sensor event pump"; |
+ |
+ if (state_ != PumpState::PENDING_START) |
+ return; |
+ |
+ DCHECK(!timer_.IsRunning()); |
+ |
+ timer_.Start(FROM_HERE, |
+ base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), |
+ this, &DeviceMotionEventPump::FireEvent); |
+ state_ = PumpState::RUNNING; |
} |
-void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) { |
- device::MotionData data = *static_cast<device::MotionData*>(fake_data); |
+bool DeviceMotionEventPump::CanStart() const { |
+ if (num_sensors_tried_ != 3) |
+ return false; |
- listener()->DidChangeDeviceMotion(data); |
+ // device motion spec does not require all sensors to be available. |
+ return accelerometer_sensor_.sensor || linear_acceleration_sensor_.sensor || |
+ gyroscope_sensor_.sensor; |
+} |
+ |
+bool DeviceMotionEventPump::GetDataFromSharedMemory( |
+ blink::WebDeviceMotionData* data) { |
+ bool has_data = false; |
+ if (accelerometer_sensor_.SensorReadingUpdated()) { |
timvolodine
2017/05/24 15:30:16
nit: SensorReadingUpdated sounds a bit misleading,
juncai
2017/05/26 02:38:52
Done.
|
+ data->acceleration_including_gravity_x = |
+ accelerometer_sensor_.reading.values[0].value(); |
+ data->acceleration_including_gravity_y = |
+ accelerometer_sensor_.reading.values[1].value(); |
+ data->acceleration_including_gravity_z = |
+ accelerometer_sensor_.reading.values[2].value(); |
+ data->has_acceleration_including_gravity_x = true; |
+ data->has_acceleration_including_gravity_y = true; |
+ data->has_acceleration_including_gravity_z = true; |
+ has_data = true; |
+ } |
+ |
+ if (linear_acceleration_sensor_.SensorReadingUpdated()) { |
+ data->acceleration_x = |
+ linear_acceleration_sensor_.reading.values[0].value(); |
+ data->acceleration_y = |
+ linear_acceleration_sensor_.reading.values[1].value(); |
+ data->acceleration_z = |
+ linear_acceleration_sensor_.reading.values[2].value(); |
+ data->has_acceleration_x = true; |
+ data->has_acceleration_y = true; |
+ data->has_acceleration_z = true; |
+ has_data = true; |
+ } |
+ |
+ if (gyroscope_sensor_.SensorReadingUpdated()) { |
+ data->rotation_rate_alpha = gyroscope_sensor_.reading.values[0].value(); |
+ data->rotation_rate_beta = gyroscope_sensor_.reading.values[1].value(); |
+ data->rotation_rate_gamma = gyroscope_sensor_.reading.values[2].value(); |
+ data->has_rotation_rate_alpha = true; |
+ data->has_rotation_rate_beta = true; |
+ data->has_rotation_rate_gamma = true; |
+ has_data = true; |
+ } |
+ |
+ return has_data; |
+} |
+ |
+void DeviceMotionEventPump::GetSensor(SensorEntry* sensor_entry) { |
+ auto request = mojo::MakeRequest(&sensor_entry->sensor); |
+ sensor_provider_->GetSensor(sensor_entry->type, std::move(request), |
+ base::Bind(&SensorEntry::OnSensorCreated, |
+ base::Unretained(sensor_entry))); |
+ sensor_entry->sensor.set_connection_error_handler(base::Bind( |
+ &SensorEntry::HandleSensorError, base::Unretained(sensor_entry))); |
+} |
+ |
+void DeviceMotionEventPump::HandleSensorProviderError() { |
+ sensor_provider_.reset(); |
} |
} // namespace content |