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 "content/browser/device_sensors/sensor_manager_android.h" | 5 #include "content/browser/device_sensors/sensor_manager_android.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/android/context_utils.h" | 9 #include "base/android/context_utils.h" |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "jni/DeviceSensors_jni.h" | 14 #include "jni/DeviceSensors_jni.h" |
| 16 | 15 |
| 17 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
| 18 using base::android::JavaParamRef; | 17 using base::android::JavaParamRef; |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 void UpdateDeviceOrientationHistogram( | 21 void UpdateDeviceOrientationHistogram( |
| 23 content::SensorManagerAndroid::OrientationSensorType type) { | 22 content::SensorManagerAndroid::OrientationSensorType type) { |
| 24 UMA_HISTOGRAM_ENUMERATION("InertialSensor.DeviceOrientationSensorAndroid", | 23 UMA_HISTOGRAM_ENUMERATION("InertialSensor.DeviceOrientationSensorAndroid", |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 51 namespace content { | 50 namespace content { |
| 52 | 51 |
| 53 SensorManagerAndroid::SensorManagerAndroid() | 52 SensorManagerAndroid::SensorManagerAndroid() |
| 54 : number_active_device_motion_sensors_(0), | 53 : number_active_device_motion_sensors_(0), |
| 55 device_light_buffer_(nullptr), | 54 device_light_buffer_(nullptr), |
| 56 device_motion_buffer_(nullptr), | 55 device_motion_buffer_(nullptr), |
| 57 device_orientation_buffer_(nullptr), | 56 device_orientation_buffer_(nullptr), |
| 58 motion_buffer_initialized_(false), | 57 motion_buffer_initialized_(false), |
| 59 orientation_buffer_initialized_(false), | 58 orientation_buffer_initialized_(false), |
| 60 is_shutdown_(false) { | 59 is_shutdown_(false) { |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 60 DCHECK(thread_checker_.CalledOnValidThread()); |
|
timvolodine
2016/10/31 15:52:46
can we add an explicit UI thread check here? to av
blundell
2016/11/09 16:00:46
Done in GetInstance(), the initial entrypoint.
| |
| 62 memset(received_motion_data_, 0, sizeof(received_motion_data_)); | 61 memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
| 63 device_sensors_.Reset(Java_DeviceSensors_getInstance( | 62 device_sensors_.Reset(Java_DeviceSensors_getInstance( |
| 64 AttachCurrentThread(), base::android::GetApplicationContext())); | 63 AttachCurrentThread(), base::android::GetApplicationContext())); |
| 65 } | 64 } |
| 66 | 65 |
| 67 SensorManagerAndroid::~SensorManagerAndroid() { | 66 SensorManagerAndroid::~SensorManagerAndroid() { |
| 68 } | 67 } |
| 69 | 68 |
| 70 bool SensorManagerAndroid::Register(JNIEnv* env) { | 69 bool SensorManagerAndroid::Register(JNIEnv* env) { |
| 71 return RegisterNativesImpl(env); | 70 return RegisterNativesImpl(env); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 | 200 |
| 202 if (!device_light_buffer_) | 201 if (!device_light_buffer_) |
| 203 return; | 202 return; |
| 204 | 203 |
| 205 device_light_buffer_->seqlock.WriteBegin(); | 204 device_light_buffer_->seqlock.WriteBegin(); |
| 206 device_light_buffer_->data.value = value; | 205 device_light_buffer_->data.value = value; |
| 207 device_light_buffer_->seqlock.WriteEnd(); | 206 device_light_buffer_->seqlock.WriteEnd(); |
| 208 } | 207 } |
| 209 | 208 |
| 210 bool SensorManagerAndroid::Start(ConsumerType consumer_type) { | 209 bool SensorManagerAndroid::Start(ConsumerType consumer_type) { |
| 211 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
| 212 DCHECK(!device_sensors_.is_null()); | 211 DCHECK(!device_sensors_.is_null()); |
| 213 int rate_in_microseconds = (consumer_type == CONSUMER_TYPE_LIGHT) | 212 int rate_in_microseconds = (consumer_type == CONSUMER_TYPE_LIGHT) |
| 214 ? kLightSensorIntervalMicroseconds | 213 ? kLightSensorIntervalMicroseconds |
| 215 : kDeviceSensorIntervalMicroseconds; | 214 : kDeviceSensorIntervalMicroseconds; |
| 216 return Java_DeviceSensors_start( | 215 return Java_DeviceSensors_start( |
| 217 AttachCurrentThread(), device_sensors_, reinterpret_cast<intptr_t>(this), | 216 AttachCurrentThread(), device_sensors_, reinterpret_cast<intptr_t>(this), |
| 218 static_cast<jint>(consumer_type), rate_in_microseconds); | 217 static_cast<jint>(consumer_type), rate_in_microseconds); |
| 219 } | 218 } |
| 220 | 219 |
| 221 void SensorManagerAndroid::Stop(ConsumerType consumer_type) { | 220 void SensorManagerAndroid::Stop(ConsumerType consumer_type) { |
| 222 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 221 DCHECK(thread_checker_.CalledOnValidThread()); |
| 223 DCHECK(!device_sensors_.is_null()); | 222 DCHECK(!device_sensors_.is_null()); |
| 224 Java_DeviceSensors_stop(AttachCurrentThread(), device_sensors_, | 223 Java_DeviceSensors_stop(AttachCurrentThread(), device_sensors_, |
| 225 static_cast<jint>(consumer_type)); | 224 static_cast<jint>(consumer_type)); |
| 226 } | 225 } |
| 227 | 226 |
| 228 int SensorManagerAndroid::GetNumberActiveDeviceMotionSensors() { | 227 int SensorManagerAndroid::GetNumberActiveDeviceMotionSensors() { |
| 229 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 228 DCHECK(thread_checker_.CalledOnValidThread()); |
| 230 DCHECK(!device_sensors_.is_null()); | 229 DCHECK(!device_sensors_.is_null()); |
| 231 return Java_DeviceSensors_getNumberActiveDeviceMotionSensors( | 230 return Java_DeviceSensors_getNumberActiveDeviceMotionSensors( |
| 232 AttachCurrentThread(), device_sensors_); | 231 AttachCurrentThread(), device_sensors_); |
| 233 } | 232 } |
| 234 | 233 |
| 235 SensorManagerAndroid::OrientationSensorType | 234 SensorManagerAndroid::OrientationSensorType |
| 236 SensorManagerAndroid::GetOrientationSensorTypeUsed() { | 235 SensorManagerAndroid::GetOrientationSensorTypeUsed() { |
| 237 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 236 DCHECK(thread_checker_.CalledOnValidThread()); |
| 238 DCHECK(!device_sensors_.is_null()); | 237 DCHECK(!device_sensors_.is_null()); |
| 239 return static_cast<SensorManagerAndroid::OrientationSensorType>( | 238 return static_cast<SensorManagerAndroid::OrientationSensorType>( |
| 240 Java_DeviceSensors_getOrientationSensorTypeUsed(AttachCurrentThread(), | 239 Java_DeviceSensors_getOrientationSensorTypeUsed(AttachCurrentThread(), |
| 241 device_sensors_)); | 240 device_sensors_)); |
| 242 } | 241 } |
| 243 | 242 |
| 244 // ----- Shared memory API methods | 243 // ----- Shared memory API methods |
| 245 | 244 |
| 246 // --- Device Light | 245 // --- Device Light |
| 247 | 246 |
| 248 void SensorManagerAndroid::StartFetchingDeviceLightData( | 247 void SensorManagerAndroid::StartFetchingDeviceLightData( |
| 249 DeviceLightHardwareBuffer* buffer) { | 248 DeviceLightHardwareBuffer* buffer) { |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 249 DCHECK(thread_checker_.CalledOnValidThread()); |
| 251 DCHECK(buffer); | 250 DCHECK(buffer); |
| 252 if (is_shutdown_) | 251 if (is_shutdown_) |
| 253 return; | 252 return; |
| 254 | 253 |
| 255 { | 254 { |
| 256 base::AutoLock autolock(light_buffer_lock_); | 255 base::AutoLock autolock(light_buffer_lock_); |
| 257 device_light_buffer_ = buffer; | 256 device_light_buffer_ = buffer; |
| 258 SetLightBufferValue(-1); | 257 SetLightBufferValue(-1); |
| 259 } | 258 } |
| 260 bool success = Start(CONSUMER_TYPE_LIGHT); | 259 bool success = Start(CONSUMER_TYPE_LIGHT); |
| 261 if (!success) { | 260 if (!success) { |
| 262 base::AutoLock autolock(light_buffer_lock_); | 261 base::AutoLock autolock(light_buffer_lock_); |
| 263 SetLightBufferValue(std::numeric_limits<double>::infinity()); | 262 SetLightBufferValue(std::numeric_limits<double>::infinity()); |
| 264 } | 263 } |
| 265 } | 264 } |
| 266 | 265 |
| 267 void SensorManagerAndroid::StopFetchingDeviceLightData() { | 266 void SensorManagerAndroid::StopFetchingDeviceLightData() { |
| 268 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 267 DCHECK(thread_checker_.CalledOnValidThread()); |
| 269 if (is_shutdown_) | 268 if (is_shutdown_) |
| 270 return; | 269 return; |
| 271 | 270 |
| 272 Stop(CONSUMER_TYPE_LIGHT); | 271 Stop(CONSUMER_TYPE_LIGHT); |
| 273 { | 272 { |
| 274 base::AutoLock autolock(light_buffer_lock_); | 273 base::AutoLock autolock(light_buffer_lock_); |
| 275 if (device_light_buffer_) { | 274 if (device_light_buffer_) { |
| 276 SetLightBufferValue(-1); | 275 SetLightBufferValue(-1); |
| 277 device_light_buffer_ = nullptr; | 276 device_light_buffer_ = nullptr; |
| 278 } | 277 } |
| 279 } | 278 } |
| 280 } | 279 } |
| 281 | 280 |
| 282 void SensorManagerAndroid::SetLightBufferValue(double lux) { | 281 void SensorManagerAndroid::SetLightBufferValue(double lux) { |
| 283 device_light_buffer_->seqlock.WriteBegin(); | 282 device_light_buffer_->seqlock.WriteBegin(); |
| 284 device_light_buffer_->data.value = lux; | 283 device_light_buffer_->data.value = lux; |
| 285 device_light_buffer_->seqlock.WriteEnd(); | 284 device_light_buffer_->seqlock.WriteEnd(); |
| 286 } | 285 } |
| 287 | 286 |
| 288 // --- Device Motion | 287 // --- Device Motion |
| 289 | 288 |
| 290 void SensorManagerAndroid::StartFetchingDeviceMotionData( | 289 void SensorManagerAndroid::StartFetchingDeviceMotionData( |
| 291 DeviceMotionHardwareBuffer* buffer) { | 290 DeviceMotionHardwareBuffer* buffer) { |
| 292 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 291 DCHECK(thread_checker_.CalledOnValidThread()); |
| 293 DCHECK(buffer); | 292 DCHECK(buffer); |
| 294 if (is_shutdown_) | 293 if (is_shutdown_) |
| 295 return; | 294 return; |
| 296 | 295 |
| 297 { | 296 { |
| 298 base::AutoLock autolock(motion_buffer_lock_); | 297 base::AutoLock autolock(motion_buffer_lock_); |
| 299 device_motion_buffer_ = buffer; | 298 device_motion_buffer_ = buffer; |
| 300 ClearInternalMotionBuffers(); | 299 ClearInternalMotionBuffers(); |
| 301 } | 300 } |
| 302 Start(CONSUMER_TYPE_MOTION); | 301 Start(CONSUMER_TYPE_MOTION); |
| 303 | 302 |
| 304 // If no motion data can ever be provided, the number of active device motion | 303 // If no motion data can ever be provided, the number of active device motion |
| 305 // sensors will be zero. In that case flag the shared memory buffer | 304 // sensors will be zero. In that case flag the shared memory buffer |
| 306 // as ready to read, as it will not change anyway. | 305 // as ready to read, as it will not change anyway. |
| 307 number_active_device_motion_sensors_ = GetNumberActiveDeviceMotionSensors(); | 306 number_active_device_motion_sensors_ = GetNumberActiveDeviceMotionSensors(); |
| 308 { | 307 { |
| 309 base::AutoLock autolock(motion_buffer_lock_); | 308 base::AutoLock autolock(motion_buffer_lock_); |
| 310 CheckMotionBufferReadyToRead(); | 309 CheckMotionBufferReadyToRead(); |
| 311 } | 310 } |
| 312 } | 311 } |
| 313 | 312 |
| 314 void SensorManagerAndroid::StopFetchingDeviceMotionData() { | 313 void SensorManagerAndroid::StopFetchingDeviceMotionData() { |
| 315 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 314 DCHECK(thread_checker_.CalledOnValidThread()); |
| 316 if (is_shutdown_) | 315 if (is_shutdown_) |
| 317 return; | 316 return; |
| 318 | 317 |
| 319 Stop(CONSUMER_TYPE_MOTION); | 318 Stop(CONSUMER_TYPE_MOTION); |
| 320 { | 319 { |
| 321 base::AutoLock autolock(motion_buffer_lock_); | 320 base::AutoLock autolock(motion_buffer_lock_); |
| 322 if (device_motion_buffer_) { | 321 if (device_motion_buffer_) { |
| 323 ClearInternalMotionBuffers(); | 322 ClearInternalMotionBuffers(); |
| 324 device_motion_buffer_ = nullptr; | 323 device_motion_buffer_ = nullptr; |
| 325 } | 324 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 void SensorManagerAndroid::ClearInternalMotionBuffers() { | 357 void SensorManagerAndroid::ClearInternalMotionBuffers() { |
| 359 memset(received_motion_data_, 0, sizeof(received_motion_data_)); | 358 memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
| 360 number_active_device_motion_sensors_ = 0; | 359 number_active_device_motion_sensors_ = 0; |
| 361 SetMotionBufferReadyStatus(false); | 360 SetMotionBufferReadyStatus(false); |
| 362 } | 361 } |
| 363 | 362 |
| 364 // --- Device Orientation | 363 // --- Device Orientation |
| 365 | 364 |
| 366 void SensorManagerAndroid::StartFetchingDeviceOrientationData( | 365 void SensorManagerAndroid::StartFetchingDeviceOrientationData( |
| 367 DeviceOrientationHardwareBuffer* buffer) { | 366 DeviceOrientationHardwareBuffer* buffer) { |
| 368 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 367 DCHECK(thread_checker_.CalledOnValidThread()); |
| 369 DCHECK(buffer); | 368 DCHECK(buffer); |
| 370 if (is_shutdown_) | 369 if (is_shutdown_) |
| 371 return; | 370 return; |
| 372 | 371 |
| 373 { | 372 { |
| 374 base::AutoLock autolock(orientation_buffer_lock_); | 373 base::AutoLock autolock(orientation_buffer_lock_); |
| 375 device_orientation_buffer_ = buffer; | 374 device_orientation_buffer_ = buffer; |
| 376 } | 375 } |
| 377 bool success = Start(CONSUMER_TYPE_ORIENTATION); | 376 bool success = Start(CONSUMER_TYPE_ORIENTATION); |
| 378 | 377 |
| 379 { | 378 { |
| 380 base::AutoLock autolock(orientation_buffer_lock_); | 379 base::AutoLock autolock(orientation_buffer_lock_); |
| 381 // If Start() was unsuccessful then set the buffer ready flag to true | 380 // If Start() was unsuccessful then set the buffer ready flag to true |
| 382 // to start firing all-null events. | 381 // to start firing all-null events. |
| 383 SetOrientationBufferStatus(buffer, !success /* ready */, | 382 SetOrientationBufferStatus(buffer, !success /* ready */, |
| 384 false /* absolute */); | 383 false /* absolute */); |
| 385 orientation_buffer_initialized_ = !success; | 384 orientation_buffer_initialized_ = !success; |
| 386 } | 385 } |
| 387 | 386 |
| 388 if (!success) | 387 if (!success) |
| 389 UpdateDeviceOrientationHistogram(NOT_AVAILABLE); | 388 UpdateDeviceOrientationHistogram(NOT_AVAILABLE); |
| 390 } | 389 } |
| 391 | 390 |
| 392 void SensorManagerAndroid::StopFetchingDeviceOrientationData() { | 391 void SensorManagerAndroid::StopFetchingDeviceOrientationData() { |
| 393 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 392 DCHECK(thread_checker_.CalledOnValidThread()); |
| 394 if (is_shutdown_) | 393 if (is_shutdown_) |
| 395 return; | 394 return; |
| 396 | 395 |
| 397 Stop(CONSUMER_TYPE_ORIENTATION); | 396 Stop(CONSUMER_TYPE_ORIENTATION); |
| 398 { | 397 { |
| 399 base::AutoLock autolock(orientation_buffer_lock_); | 398 base::AutoLock autolock(orientation_buffer_lock_); |
| 400 if (device_orientation_buffer_) { | 399 if (device_orientation_buffer_) { |
| 401 SetOrientationBufferStatus(device_orientation_buffer_, false, false); | 400 SetOrientationBufferStatus(device_orientation_buffer_, false, false); |
| 402 orientation_buffer_initialized_ = false; | 401 orientation_buffer_initialized_ = false; |
| 403 device_orientation_buffer_ = nullptr; | 402 device_orientation_buffer_ = nullptr; |
| 404 } | 403 } |
| 405 } | 404 } |
| 406 } | 405 } |
| 407 | 406 |
| 408 void SensorManagerAndroid::StartFetchingDeviceOrientationAbsoluteData( | 407 void SensorManagerAndroid::StartFetchingDeviceOrientationAbsoluteData( |
| 409 DeviceOrientationHardwareBuffer* buffer) { | 408 DeviceOrientationHardwareBuffer* buffer) { |
| 410 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 409 DCHECK(thread_checker_.CalledOnValidThread()); |
| 411 DCHECK(buffer); | 410 DCHECK(buffer); |
| 412 if (is_shutdown_) | 411 if (is_shutdown_) |
| 413 return; | 412 return; |
| 414 | 413 |
| 415 { | 414 { |
| 416 base::AutoLock autolock(orientation_absolute_buffer_lock_); | 415 base::AutoLock autolock(orientation_absolute_buffer_lock_); |
| 417 device_orientation_absolute_buffer_ = buffer; | 416 device_orientation_absolute_buffer_ = buffer; |
| 418 } | 417 } |
| 419 bool success = Start(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); | 418 bool success = Start(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); |
| 420 | 419 |
| 421 { | 420 { |
| 422 base::AutoLock autolock(orientation_absolute_buffer_lock_); | 421 base::AutoLock autolock(orientation_absolute_buffer_lock_); |
| 423 // If Start() was unsuccessful then set the buffer ready flag to true | 422 // If Start() was unsuccessful then set the buffer ready flag to true |
| 424 // to start firing all-null events. | 423 // to start firing all-null events. |
| 425 SetOrientationBufferStatus(buffer, !success /* ready */, | 424 SetOrientationBufferStatus(buffer, !success /* ready */, |
| 426 false /* absolute */); | 425 false /* absolute */); |
| 427 orientation_absolute_buffer_initialized_ = !success; | 426 orientation_absolute_buffer_initialized_ = !success; |
| 428 } | 427 } |
| 429 } | 428 } |
| 430 | 429 |
| 431 void SensorManagerAndroid::StopFetchingDeviceOrientationAbsoluteData() { | 430 void SensorManagerAndroid::StopFetchingDeviceOrientationAbsoluteData() { |
| 432 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 431 DCHECK(thread_checker_.CalledOnValidThread()); |
| 433 if (is_shutdown_) | 432 if (is_shutdown_) |
| 434 return; | 433 return; |
| 435 | 434 |
| 436 Stop(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); | 435 Stop(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); |
| 437 { | 436 { |
| 438 base::AutoLock autolock(orientation_absolute_buffer_lock_); | 437 base::AutoLock autolock(orientation_absolute_buffer_lock_); |
| 439 if (device_orientation_absolute_buffer_) { | 438 if (device_orientation_absolute_buffer_) { |
| 440 SetOrientationBufferStatus(device_orientation_absolute_buffer_, false, | 439 SetOrientationBufferStatus(device_orientation_absolute_buffer_, false, |
| 441 false); | 440 false); |
| 442 orientation_absolute_buffer_initialized_ = false; | 441 orientation_absolute_buffer_initialized_ = false; |
| 443 device_orientation_absolute_buffer_ = nullptr; | 442 device_orientation_absolute_buffer_ = nullptr; |
| 444 } | 443 } |
| 445 } | 444 } |
| 446 } | 445 } |
| 447 | 446 |
| 448 void SensorManagerAndroid::Shutdown() { | 447 void SensorManagerAndroid::Shutdown() { |
| 449 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 448 DCHECK(thread_checker_.CalledOnValidThread()); |
| 450 is_shutdown_ = true; | 449 is_shutdown_ = true; |
| 451 } | 450 } |
| 452 | 451 |
| 453 } // namespace content | 452 } // namespace content |
| OLD | NEW |