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, | |
|
timvolodine
2017/06/07 20:37:36
These methods and everything related to reading ca
juncai
2017/06/07 23:05:07
Done.
| |
| 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 state_(PumpState::STOPPED) {} | |
| 16 | 54 |
| 17 DeviceMotionEventPump::~DeviceMotionEventPump() { | 55 DeviceMotionEventPump::~DeviceMotionEventPump() { |
| 18 } | 56 PlatformEventObserver<blink::WebDeviceMotionListener>::StopIfObserving(); |
| 19 | 57 } |
| 20 void DeviceMotionEventPump::FireEvent() { | 58 |
| 21 DCHECK(listener()); | 59 void DeviceMotionEventPump::Start(blink::WebPlatformEventListener* listener) { |
| 22 device::MotionData data; | 60 DVLOG(2) << "requested start"; |
| 23 if (reader_->GetLatestData(&data) && data.all_available_sensors_are_active) | 61 |
| 24 listener()->DidChangeDeviceMotion(data); | 62 if (state_ != PumpState::STOPPED) |
| 25 } | 63 return; |
| 26 | 64 |
| 27 bool DeviceMotionEventPump::InitializeReader(base::SharedMemoryHandle handle) { | 65 DCHECK(!timer_.IsRunning()); |
| 28 if (!reader_) | 66 |
| 29 reader_.reset(new DeviceMotionSharedMemoryReader()); | 67 state_ = PumpState::PENDING_START; |
| 30 return reader_->Initialize(handle); | 68 PlatformEventObserver<blink::WebDeviceMotionListener>::Start(listener); |
| 69 } | |
| 70 | |
| 71 void DeviceMotionEventPump::Stop() { | |
| 72 DVLOG(2) << "requested stop"; | |
| 73 | |
| 74 if (state_ == PumpState::STOPPED) | |
| 75 return; | |
| 76 | |
| 77 DCHECK((state_ == PumpState::PENDING_START && !timer_.IsRunning()) || | |
| 78 (state_ == PumpState::RUNNING && timer_.IsRunning())); | |
| 79 | |
| 80 if (timer_.IsRunning()) | |
| 81 timer_.Stop(); | |
| 82 | |
| 83 PlatformEventObserver<blink::WebDeviceMotionListener>::Stop(); | |
| 84 state_ = PumpState::STOPPED; | |
| 85 } | |
| 86 | |
| 87 void DeviceMotionEventPump::SendStartMessage() { | |
| 88 auto request = mojo::MakeRequest(&sensor_provider_); | |
| 89 | |
| 90 // When running layout tests, those observers should not listen to the | |
| 91 // actual hardware changes. In order to make that happen, don't connect | |
| 92 // the other end of the mojo pipe to anything. | |
| 93 if (RenderThreadImpl::current() && | |
| 94 !RenderThreadImpl::current()->layout_test_mode()) { | |
|
timvolodine
2017/06/07 20:37:35
nit: maybe just do an early return in case it's in
juncai
2017/06/07 23:05:07
Done.
| |
| 95 if (!accelerometer_.sensor && !linear_acceleration_sensor_.sensor && | |
| 96 !gyroscope_.sensor) { | |
| 97 RenderThread::Get()->GetConnector()->BindInterface( | |
| 98 device::mojom::kServiceName, std::move(request)); | |
| 99 sensor_provider_.set_connection_error_handler( | |
| 100 base::Bind(&DeviceMotionEventPump::HandleSensorProviderError, | |
| 101 base::Unretained(this))); | |
| 102 GetSensor(&accelerometer_); | |
| 103 GetSensor(&linear_acceleration_sensor_); | |
| 104 GetSensor(&gyroscope_); | |
| 105 } else { | |
| 106 if (accelerometer_.sensor) | |
| 107 accelerometer_.sensor->Resume(); | |
| 108 | |
| 109 if (linear_acceleration_sensor_.sensor) | |
| 110 linear_acceleration_sensor_.sensor->Resume(); | |
| 111 | |
| 112 if (gyroscope_.sensor) | |
| 113 gyroscope_.sensor->Resume(); | |
| 114 | |
| 115 DidStart(); | |
| 116 } | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 void DeviceMotionEventPump::SendStopMessage() { | |
| 121 if (accelerometer_.sensor) | |
| 122 accelerometer_.sensor->Suspend(); | |
|
timvolodine
2017/06/07 20:37:35
What happens when we close a window, will that sto
juncai
2017/06/07 23:05:07
When a window is closed, the pipe is closed and th
timvolodine
2017/06/08 20:26:51
I see, so it's optimized for visibility change. Pe
juncai
2017/06/08 23:29:43
Done.
| |
| 123 | |
| 124 if (linear_acceleration_sensor_.sensor) | |
| 125 linear_acceleration_sensor_.sensor->Suspend(); | |
| 126 | |
| 127 if (gyroscope_.sensor) | |
| 128 gyroscope_.sensor->Suspend(); | |
| 31 } | 129 } |
| 32 | 130 |
| 33 void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) { | 131 void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) { |
| 34 device::MotionData data = *static_cast<device::MotionData*>(fake_data); | 132 device::MotionData data = *static_cast<device::MotionData*>(fake_data); |
| 35 | |
| 36 listener()->DidChangeDeviceMotion(data); | 133 listener()->DidChangeDeviceMotion(data); |
| 37 } | 134 } |
| 38 | 135 |
| 136 DeviceMotionEventPump::SensorEntry::SensorEntry( | |
| 137 DeviceMotionEventPump* pump, | |
| 138 device::mojom::SensorType sensor_type) | |
| 139 : event_pump(pump), type(sensor_type), client_binding(this) {} | |
| 140 | |
| 141 DeviceMotionEventPump::SensorEntry::~SensorEntry() {} | |
| 142 | |
| 143 void DeviceMotionEventPump::SensorEntry::RaiseError() { | |
| 144 HandleSensorError(); | |
| 145 } | |
| 146 | |
| 147 void DeviceMotionEventPump::SensorEntry::SensorReadingChanged() { | |
| 148 // Since DeviceMotionEventPump::FireEvent is called in a certain | |
| 149 // frequency, the |shared_buffer| is read frequently, so this | |
| 150 // method doesn't need to be implemented. | |
|
timvolodine
2017/06/07 20:37:36
sorry if I asked this previously, can we put NOTRE
juncai
2017/06/07 23:05:07
I don't think NOREACHED() can be put here.
This q
timvolodine
2017/06/08 20:26:51
This seems strange - we would get unnecessary high
juncai
2017/06/08 23:29:43
Yes, we would get high frequency mojo IPC calls. I
juncai
2017/06/13 23:02:18
Since the CL:
https://codereview.chromium.org/2927
| |
| 151 } | |
| 152 | |
| 153 void DeviceMotionEventPump::SensorEntry::OnSensorCreated( | |
| 154 device::mojom::SensorInitParamsPtr params, | |
| 155 device::mojom::SensorClientRequest client_request) { | |
| 156 if (!params) { | |
| 157 HandleSensorError(); | |
| 158 if (event_pump->CanStart()) | |
| 159 event_pump->DidStart(); | |
| 160 return; | |
| 161 } | |
| 162 | |
| 163 constexpr size_t kReadBufferSize = sizeof(device::SensorReadingSharedBuffer); | |
| 164 | |
| 165 DCHECK_EQ(0u, params->buffer_offset % kReadBufferSize); | |
| 166 | |
| 167 mode = params->mode; | |
| 168 default_config = params->default_configuration; | |
| 169 | |
| 170 DCHECK(sensor.is_bound()); | |
| 171 client_binding.Bind(std::move(client_request)); | |
| 172 | |
| 173 shared_buffer_handle = std::move(params->memory); | |
| 174 DCHECK(!shared_buffer); | |
| 175 shared_buffer = | |
| 176 shared_buffer_handle->MapAtOffset(kReadBufferSize, params->buffer_offset); | |
| 177 | |
| 178 if (!shared_buffer) { | |
| 179 HandleSensorError(); | |
| 180 if (event_pump->CanStart()) | |
| 181 event_pump->DidStart(); | |
| 182 return; | |
| 183 } | |
| 184 | |
| 185 DCHECK_GT(params->minimum_frequency, 0.0); | |
| 186 DCHECK_GE(params->maximum_frequency, params->minimum_frequency); | |
| 187 constexpr double kMaxAllowedFrequency = | |
| 188 device::mojom::SensorConfiguration::kMaxAllowedFrequency; | |
|
timvolodine
2017/06/07 20:37:35
nit: probably no need for constexpr?
juncai
2017/06/07 23:05:07
Done.
timvolodine
2017/06/08 20:26:51
nit:
I actually meant putting device::mojom::Senso
juncai
2017/06/08 23:29:43
I see.
Done.
| |
| 189 DCHECK_GE(kMaxAllowedFrequency, params->maximum_frequency); | |
| 190 | |
| 191 default_config.set_frequency(kDefaultPumpFrequencyHz); | |
| 192 | |
| 193 sensor->AddConfiguration(default_config, | |
| 194 base::Bind(&SensorEntry::OnSensorAddConfiguration, | |
| 195 base::Unretained(this))); | |
| 196 } | |
| 197 | |
| 198 void DeviceMotionEventPump::SensorEntry::OnSensorAddConfiguration( | |
| 199 bool success) { | |
| 200 if (!success) | |
| 201 HandleSensorError(); | |
| 202 if (event_pump->CanStart()) | |
| 203 event_pump->DidStart(); | |
| 204 } | |
| 205 | |
| 206 void DeviceMotionEventPump::SensorEntry::HandleSensorError() { | |
| 207 sensor.reset(); | |
| 208 shared_buffer_handle.reset(); | |
| 209 shared_buffer.reset(); | |
| 210 client_binding.Close(); | |
| 211 } | |
| 212 | |
| 213 bool DeviceMotionEventPump::SensorEntry::SensorReadingCouldBeRead() { | |
| 214 if (!sensor) | |
| 215 return false; | |
| 216 | |
| 217 const device::SensorReadingSharedBuffer* buffer = | |
| 218 static_cast<const device::SensorReadingSharedBuffer*>( | |
| 219 shared_buffer.get()); | |
| 220 if (!UpdateSensorReading(buffer, &reading)) { | |
| 221 HandleSensorError(); | |
| 222 return false; | |
| 223 } | |
| 224 | |
| 225 return true; | |
| 226 } | |
| 227 | |
| 228 void DeviceMotionEventPump::FireEvent() { | |
| 229 device::MotionData data; | |
| 230 data.interval = kDefaultPumpDelayMicroseconds; | |
| 231 | |
| 232 DCHECK(listener()); | |
| 233 | |
| 234 GetDataFromSharedMemory(&data); | |
| 235 listener()->DidChangeDeviceMotion(data); | |
| 236 } | |
| 237 | |
| 238 void DeviceMotionEventPump::DidStart() { | |
| 239 DVLOG(2) << "did start sensor event pump"; | |
| 240 | |
| 241 if (state_ != PumpState::PENDING_START) | |
| 242 return; | |
| 243 | |
| 244 DCHECK(!timer_.IsRunning()); | |
| 245 | |
| 246 timer_.Start(FROM_HERE, | |
| 247 base::TimeDelta::FromMicroseconds(kDefaultPumpDelayMicroseconds), | |
| 248 this, &DeviceMotionEventPump::FireEvent); | |
| 249 state_ = PumpState::RUNNING; | |
| 250 } | |
| 251 | |
| 252 bool DeviceMotionEventPump::CanStart() const { | |
| 253 if (accelerometer_.sensor && !accelerometer_.shared_buffer) | |
| 254 return false; | |
| 255 | |
| 256 if (linear_acceleration_sensor_.sensor && | |
| 257 !linear_acceleration_sensor_.shared_buffer) { | |
| 258 return false; | |
| 259 } | |
| 260 | |
| 261 if (gyroscope_.sensor && !gyroscope_.shared_buffer) | |
| 262 return false; | |
| 263 | |
| 264 return true; | |
| 265 } | |
| 266 | |
| 267 void DeviceMotionEventPump::GetDataFromSharedMemory(device::MotionData* data) { | |
| 268 if (accelerometer_.SensorReadingCouldBeRead()) { | |
| 269 data->acceleration_including_gravity_x = | |
| 270 accelerometer_.reading.values[0].value(); | |
| 271 data->acceleration_including_gravity_y = | |
| 272 accelerometer_.reading.values[1].value(); | |
| 273 data->acceleration_including_gravity_z = | |
| 274 accelerometer_.reading.values[2].value(); | |
| 275 data->has_acceleration_including_gravity_x = true; | |
| 276 data->has_acceleration_including_gravity_y = true; | |
| 277 data->has_acceleration_including_gravity_z = true; | |
| 278 } | |
| 279 | |
| 280 if (linear_acceleration_sensor_.SensorReadingCouldBeRead()) { | |
| 281 data->acceleration_x = | |
| 282 linear_acceleration_sensor_.reading.values[0].value(); | |
| 283 data->acceleration_y = | |
| 284 linear_acceleration_sensor_.reading.values[1].value(); | |
| 285 data->acceleration_z = | |
| 286 linear_acceleration_sensor_.reading.values[2].value(); | |
| 287 data->has_acceleration_x = true; | |
| 288 data->has_acceleration_y = true; | |
| 289 data->has_acceleration_z = true; | |
| 290 } | |
| 291 | |
| 292 if (gyroscope_.SensorReadingCouldBeRead()) { | |
| 293 data->rotation_rate_alpha = gyroscope_.reading.values[0].value(); | |
| 294 data->rotation_rate_beta = gyroscope_.reading.values[1].value(); | |
| 295 data->rotation_rate_gamma = gyroscope_.reading.values[2].value(); | |
| 296 data->has_rotation_rate_alpha = true; | |
| 297 data->has_rotation_rate_beta = true; | |
| 298 data->has_rotation_rate_gamma = true; | |
| 299 } | |
| 300 } | |
| 301 | |
| 302 void DeviceMotionEventPump::GetSensor(SensorEntry* sensor_entry) { | |
| 303 auto request = mojo::MakeRequest(&sensor_entry->sensor); | |
| 304 sensor_provider_->GetSensor(sensor_entry->type, std::move(request), | |
| 305 base::Bind(&SensorEntry::OnSensorCreated, | |
| 306 base::Unretained(sensor_entry))); | |
| 307 sensor_entry->sensor.set_connection_error_handler(base::Bind( | |
| 308 &SensorEntry::HandleSensorError, base::Unretained(sensor_entry))); | |
| 309 } | |
| 310 | |
| 311 void DeviceMotionEventPump::HandleSensorProviderError() { | |
| 312 sensor_provider_.reset(); | |
| 313 } | |
| 314 | |
| 39 } // namespace content | 315 } // namespace content |
| OLD | NEW |