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