Chromium Code Reviews| 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" | |
| 8 #include "content/public/common/service_names.mojom.h" | |
| 7 #include "content/public/renderer/render_thread.h" | 9 #include "content/public/renderer/render_thread.h" |
| 10 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 11 #include "services/device/public/interfaces/constants.mojom.h" | |
| 12 #include "services/service_manager/public/cpp/connector.h" | |
| 8 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eMotionListener.h" | 13 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eMotionListener.h" |
| 9 | 14 |
| 15 namespace { | |
| 16 | |
| 17 constexpr int kNumSensors = 3; | |
| 18 | |
| 19 constexpr int kMaxReadAttemptsCount = 10; | |
| 20 | |
| 21 bool TryReadFromBuffer(const device::SensorReadingSharedBuffer* buffer, | |
| 22 device::SensorReading* result) { | |
| 23 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); | |
| 24 auto version = seqlock.ReadBegin(); | |
| 25 auto reading_data = buffer->reading; | |
| 26 if (seqlock.ReadRetry(version)) | |
| 27 return false; | |
| 28 *result = reading_data; | |
| 29 return true; | |
| 30 } | |
| 31 | |
| 32 // Updates sensor reading from shared buffer. | |
| 33 bool UpdateSensorReading(const device::SensorReadingSharedBuffer* buffer, | |
| 34 device::SensorReading* result) { | |
| 35 int read_attempts = 0; | |
| 36 while (!TryReadFromBuffer(buffer, result)) { | |
| 37 if (++read_attempts == kMaxReadAttemptsCount) | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 return true; | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 10 namespace content { | 46 namespace content { |
| 11 | 47 |
| 12 DeviceMotionEventPump::DeviceMotionEventPump(RenderThread* thread) | 48 DeviceMotionEventPump::DeviceMotionEventPump(RenderThread* thread) |
| 13 : DeviceSensorMojoClientMixin< | 49 : PlatformEventObserver<blink::WebDeviceMotionListener>(thread), |
| 14 DeviceSensorEventPump<blink::WebDeviceMotionListener>, | 50 accelerometer_sensor_(this, device::mojom::SensorType::ACCELEROMETER), |
| 15 device::mojom::MotionSensor>(thread) {} | 51 linear_acceleration_sensor_( |
| 52 this, | |
| 53 device::mojom::SensorType::LINEAR_ACCELERATION), | |
| 54 gyroscope_sensor_(this, device::mojom::SensorType::GYROSCOPE), | |
| 55 pump_delay_microseconds_(kDefaultPumpDelayMicroseconds), | |
| 56 num_sensors_tried_(0), | |
| 57 state_(PumpState::STOPPED) {} | |
| 16 | 58 |
| 17 DeviceMotionEventPump::~DeviceMotionEventPump() { | 59 DeviceMotionEventPump::~DeviceMotionEventPump() { |
| 60 PlatformEventObserver<blink::WebDeviceMotionListener>::StopIfObserving(); | |
| 61 } | |
| 62 | |
| 63 void DeviceMotionEventPump::Start(blink::WebPlatformEventListener* listener) { | |
| 64 DVLOG(2) << "requested start"; | |
| 65 | |
| 66 if (state_ != PumpState::STOPPED) | |
| 67 return; | |
| 68 | |
| 69 DCHECK(!timer_.IsRunning()); | |
| 70 | |
| 71 PlatformEventObserver<blink::WebDeviceMotionListener>::Start(listener); | |
| 72 state_ = PumpState::PENDING_START; | |
| 73 } | |
| 74 | |
| 75 void DeviceMotionEventPump::Stop() { | |
| 76 DVLOG(2) << "requested stop"; | |
| 77 | |
| 78 if (state_ == PumpState::STOPPED) | |
| 79 return; | |
| 80 | |
| 81 DCHECK((state_ == PumpState::PENDING_START && !timer_.IsRunning()) || | |
| 82 (state_ == PumpState::RUNNING && timer_.IsRunning())); | |
| 83 | |
| 84 if (timer_.IsRunning()) | |
| 85 timer_.Stop(); | |
| 86 | |
| 87 PlatformEventObserver<blink::WebDeviceMotionListener>::Stop(); | |
| 88 state_ = PumpState::STOPPED; | |
| 89 } | |
| 90 | |
| 91 void DeviceMotionEventPump::SendStartMessage() { | |
| 92 auto request = mojo::MakeRequest(&sensor_provider_); | |
| 93 | |
| 94 // When running layout tests, those observers should not listen to the | |
| 95 // actual hardware changes. In order to make that happen, don't connect | |
| 96 // the other end of the mojo pipe to anything. | |
| 97 if (RenderThreadImpl::current() && | |
| 98 !RenderThreadImpl::current()->layout_test_mode()) { | |
| 99 RenderThread::Get()->GetConnector()->BindInterface( | |
| 100 device::mojom::kServiceName, std::move(request)); | |
| 101 sensor_provider_.set_connection_error_handler( | |
| 102 base::Bind(&DeviceMotionEventPump::HandleSensorProviderError, | |
| 103 base::Unretained(this))); | |
| 104 GetSensor(&accelerometer_sensor_); | |
| 105 GetSensor(&linear_acceleration_sensor_); | |
| 106 GetSensor(&gyroscope_sensor_); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 void DeviceMotionEventPump::SendStopMessage() { | |
| 111 accelerometer_sensor_.HandleSensorError(); | |
| 112 linear_acceleration_sensor_.HandleSensorError(); | |
| 113 gyroscope_sensor_.HandleSensorError(); | |
| 114 num_sensors_tried_ = 0; | |
| 115 } | |
| 116 | |
| 117 void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) { | |
| 118 blink::WebDeviceMotionData data = | |
| 119 *static_cast<blink::WebDeviceMotionData*>(fake_data); | |
| 120 listener()->DidChangeDeviceMotion(data); | |
| 121 } | |
| 122 | |
| 123 DeviceMotionEventPump::SensorEntry::SensorEntry( | |
| 124 DeviceMotionEventPump* pump, | |
| 125 device::mojom::SensorType sensor_type) | |
| 126 : event_pump(pump), type(sensor_type), client_binding(this) {} | |
| 127 | |
| 128 DeviceMotionEventPump::SensorEntry::~SensorEntry() { | |
| 129 client_binding.Close(); | |
| 130 } | |
| 131 | |
| 132 void DeviceMotionEventPump::SensorEntry::RaiseError() { | |
| 133 HandleSensorError(); | |
| 134 } | |
| 135 | |
| 136 void DeviceMotionEventPump::SensorEntry::SensorReadingChanged() { | |
| 137 // Since DeviceMotionEventPump::FireEvent is called in a certain | |
| 138 // frequency, the |shared_buffer| is read frequently, so this | |
| 139 // method doesn't need to be implemented. | |
| 140 } | |
| 141 | |
| 142 void DeviceMotionEventPump::SensorEntry::OnSensorCreated( | |
| 143 device::mojom::SensorInitParamsPtr params, | |
| 144 device::mojom::SensorClientRequest client_request) { | |
| 145 if (!params) { | |
| 146 ++event_pump->num_sensors_tried_; | |
| 147 HandleSensorError(); | |
| 148 if (event_pump->CanStart()) | |
| 149 event_pump->DidStart(); | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 constexpr size_t kReadBufferSize = sizeof(device::SensorReadingSharedBuffer); | |
| 154 | |
| 155 DCHECK_EQ(0u, params->buffer_offset % kReadBufferSize); | |
| 156 | |
| 157 mode = params->mode; | |
| 158 default_config = params->default_configuration; | |
| 159 | |
| 160 DCHECK(sensor.is_bound()); | |
| 161 client_binding.Bind(std::move(client_request)); | |
| 162 | |
| 163 shared_buffer_handle = std::move(params->memory); | |
| 164 DCHECK(!shared_buffer); | |
| 165 shared_buffer = | |
| 166 shared_buffer_handle->MapAtOffset(kReadBufferSize, params->buffer_offset); | |
| 167 | |
| 168 if (!shared_buffer) { | |
| 169 ++event_pump->num_sensors_tried_; | |
| 170 HandleSensorError(); | |
| 171 if (event_pump->CanStart()) | |
| 172 event_pump->DidStart(); | |
| 173 return; | |
| 174 } | |
| 175 | |
| 176 frequency_limits.first = params->minimum_frequency; | |
| 177 frequency_limits.second = params->maximum_frequency; | |
| 178 | |
| 179 DCHECK_GT(frequency_limits.first, 0.0); | |
| 180 DCHECK_GE(frequency_limits.second, frequency_limits.first); | |
| 181 constexpr double kMaxAllowedFrequency = | |
| 182 device::mojom::SensorConfiguration::kMaxAllowedFrequency; | |
| 183 DCHECK_GE(kMaxAllowedFrequency, frequency_limits.second); | |
| 184 | |
| 185 sensor->AddConfiguration(default_config, | |
| 186 base::Bind(&SensorEntry::OnSensorAddConfiguration, | |
| 187 base::Unretained(this))); | |
| 188 } | |
| 189 | |
| 190 void DeviceMotionEventPump::SensorEntry::OnSensorAddConfiguration( | |
| 191 bool success) { | |
| 192 ++event_pump->num_sensors_tried_; | |
| 193 if (!success) | |
| 194 HandleSensorError(); | |
| 195 if (event_pump->CanStart()) | |
| 196 event_pump->DidStart(); | |
| 197 } | |
| 198 | |
| 199 void DeviceMotionEventPump::SensorEntry::HandleSensorError() { | |
| 200 sensor.reset(); | |
| 201 shared_buffer_handle.reset(); | |
| 202 shared_buffer.reset(); | |
| 203 client_binding.Close(); | |
| 204 } | |
| 205 | |
| 206 bool DeviceMotionEventPump::SensorEntry::SensorReadingCouldBeRead() { | |
| 207 if (!sensor) | |
| 208 return false; | |
| 209 | |
| 210 const device::SensorReadingSharedBuffer* buffer = | |
| 211 static_cast<const device::SensorReadingSharedBuffer*>( | |
| 212 shared_buffer.get()); | |
| 213 if (!UpdateSensorReading(buffer, &reading)) { | |
| 214 HandleSensorError(); | |
| 215 return false; | |
| 216 } | |
| 217 | |
| 218 return true; | |
| 18 } | 219 } |
| 19 | 220 |
| 20 void DeviceMotionEventPump::FireEvent() { | 221 void DeviceMotionEventPump::FireEvent() { |
| 222 blink::WebDeviceMotionData data; | |
| 223 data.interval = kDefaultPumpDelayMicroseconds; | |
| 224 | |
| 21 DCHECK(listener()); | 225 DCHECK(listener()); |
| 22 device::MotionData data; | 226 |
| 23 if (reader_->GetLatestData(&data) && data.all_available_sensors_are_active) | 227 GetDataFromSharedMemory(&data); |
| 24 listener()->DidChangeDeviceMotion(data); | |
| 25 } | |
| 26 | |
| 27 bool DeviceMotionEventPump::InitializeReader(base::SharedMemoryHandle handle) { | |
| 28 if (!reader_) | |
| 29 reader_.reset(new DeviceMotionSharedMemoryReader()); | |
| 30 return reader_->Initialize(handle); | |
| 31 } | |
| 32 | |
| 33 void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) { | |
| 34 device::MotionData data = *static_cast<device::MotionData*>(fake_data); | |
| 35 | |
| 36 listener()->DidChangeDeviceMotion(data); | 228 listener()->DidChangeDeviceMotion(data); |
| 37 } | 229 } |
| 38 | 230 |
| 231 void DeviceMotionEventPump::DidStart() { | |
| 232 DVLOG(2) << "did start sensor event pump"; | |
| 233 | |
| 234 if (state_ != PumpState::PENDING_START) | |
| 235 return; | |
| 236 | |
| 237 DCHECK(!timer_.IsRunning()); | |
| 238 | |
| 239 timer_.Start(FROM_HERE, | |
| 240 base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), | |
| 241 this, &DeviceMotionEventPump::FireEvent); | |
| 242 state_ = PumpState::RUNNING; | |
| 243 } | |
| 244 | |
| 245 bool DeviceMotionEventPump::CanStart() const { | |
| 246 return num_sensors_tried_ == kNumSensors; | |
|
Reilly Grant (use Gerrit)
2017/05/27 03:17:51
We can replace num_sensors_tried_ with the followi
juncai
2017/05/30 22:26:57
Done.
| |
| 247 } | |
| 248 | |
| 249 void DeviceMotionEventPump::GetDataFromSharedMemory( | |
| 250 blink::WebDeviceMotionData* data) { | |
| 251 if (accelerometer_sensor_.SensorReadingCouldBeRead()) { | |
| 252 data->acceleration_including_gravity_x = | |
| 253 accelerometer_sensor_.reading.values[0].value(); | |
| 254 data->acceleration_including_gravity_y = | |
| 255 accelerometer_sensor_.reading.values[1].value(); | |
| 256 data->acceleration_including_gravity_z = | |
| 257 accelerometer_sensor_.reading.values[2].value(); | |
| 258 data->has_acceleration_including_gravity_x = true; | |
| 259 data->has_acceleration_including_gravity_y = true; | |
| 260 data->has_acceleration_including_gravity_z = true; | |
| 261 } | |
| 262 | |
| 263 if (linear_acceleration_sensor_.SensorReadingCouldBeRead()) { | |
| 264 data->acceleration_x = | |
| 265 linear_acceleration_sensor_.reading.values[0].value(); | |
| 266 data->acceleration_y = | |
| 267 linear_acceleration_sensor_.reading.values[1].value(); | |
| 268 data->acceleration_z = | |
| 269 linear_acceleration_sensor_.reading.values[2].value(); | |
| 270 data->has_acceleration_x = true; | |
| 271 data->has_acceleration_y = true; | |
| 272 data->has_acceleration_z = true; | |
| 273 } | |
| 274 | |
| 275 if (gyroscope_sensor_.SensorReadingCouldBeRead()) { | |
| 276 data->rotation_rate_alpha = gyroscope_sensor_.reading.values[0].value(); | |
| 277 data->rotation_rate_beta = gyroscope_sensor_.reading.values[1].value(); | |
| 278 data->rotation_rate_gamma = gyroscope_sensor_.reading.values[2].value(); | |
| 279 data->has_rotation_rate_alpha = true; | |
| 280 data->has_rotation_rate_beta = true; | |
| 281 data->has_rotation_rate_gamma = true; | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 void DeviceMotionEventPump::GetSensor(SensorEntry* sensor_entry) { | |
| 286 auto request = mojo::MakeRequest(&sensor_entry->sensor); | |
| 287 sensor_provider_->GetSensor(sensor_entry->type, std::move(request), | |
| 288 base::Bind(&SensorEntry::OnSensorCreated, | |
| 289 base::Unretained(sensor_entry))); | |
| 290 sensor_entry->sensor.set_connection_error_handler(base::Bind( | |
| 291 &SensorEntry::HandleSensorError, base::Unretained(sensor_entry))); | |
| 292 } | |
| 293 | |
| 294 void DeviceMotionEventPump::HandleSensorProviderError() { | |
| 295 sensor_provider_.reset(); | |
| 296 } | |
| 297 | |
| 39 } // namespace content | 298 } // namespace content |
| OLD | NEW |