Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: content/browser/device_sensors/sensor_manager_android.cc

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

Powered by Google App Engine
This is Rietveld 408576698