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