| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/device_sensor_host.h" | 5 #include "content/browser/device_sensors/device_sensor_host.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "build/build_config.h" |
| 7 #include "content/browser/device_sensors/device_sensor_service.h" | 9 #include "content/browser/device_sensors/device_sensor_service.h" |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 #include "mojo/public/cpp/bindings/strong_binding.h" | 10 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 10 | 11 |
| 12 #if defined(OS_ANDROID) |
| 13 #include "content/browser/device_sensors/sensor_manager_android.h" |
| 14 #endif |
| 15 |
| 11 namespace content { | 16 namespace content { |
| 12 | 17 |
| 13 template <typename MojoInterface, ConsumerType consumer_type> | 18 template <typename MojoInterface, ConsumerType consumer_type> |
| 14 void DeviceSensorHost<MojoInterface, consumer_type>::Create( | 19 void DeviceSensorHost<MojoInterface, consumer_type>::Create( |
| 20 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 15 mojo::InterfaceRequest<MojoInterface> request) { | 21 mojo::InterfaceRequest<MojoInterface> request) { |
| 16 mojo::MakeStrongBinding( | 22 mojo::MakeStrongBinding( |
| 17 base::WrapUnique(new DeviceSensorHost<MojoInterface, consumer_type>), | 23 base::WrapUnique( |
| 24 new DeviceSensorHost<MojoInterface, consumer_type>(ui_task_runner)), |
| 18 std::move(request)); | 25 std::move(request)); |
| 19 } | 26 } |
| 20 | 27 |
| 21 template <typename MojoInterface, ConsumerType consumer_type> | 28 template <typename MojoInterface, ConsumerType consumer_type> |
| 22 DeviceSensorHost<MojoInterface, consumer_type>::DeviceSensorHost() | 29 DeviceSensorHost<MojoInterface, consumer_type>::DeviceSensorHost( |
| 23 : is_started_(false) { | 30 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) |
| 24 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 31 : is_started_(false), ui_task_runner_(ui_task_runner) { |
| 32 DCHECK(base::MessageLoopForIO::IsCurrent()); |
| 25 } | 33 } |
| 26 | 34 |
| 27 template <typename MojoInterface, ConsumerType consumer_type> | 35 template <typename MojoInterface, ConsumerType consumer_type> |
| 28 DeviceSensorHost<MojoInterface, consumer_type>::~DeviceSensorHost() { | 36 DeviceSensorHost<MojoInterface, consumer_type>::~DeviceSensorHost() { |
| 29 DCHECK(thread_checker_.CalledOnValidThread()); | 37 DCHECK(thread_checker_.CalledOnValidThread()); |
| 30 if (is_started_) | 38 if (is_started_) |
| 31 DeviceSensorService::GetInstance()->RemoveConsumer(consumer_type); | 39 DeviceSensorService::GetInstance()->RemoveConsumer(consumer_type); |
| 32 } | 40 } |
| 33 | 41 |
| 34 template <typename MojoInterface, ConsumerType consumer_type> | 42 template <typename MojoInterface, ConsumerType consumer_type> |
| 35 void DeviceSensorHost<MojoInterface, consumer_type>::DeviceSensorHost:: | 43 void DeviceSensorHost<MojoInterface, consumer_type>::DeviceSensorHost:: |
| 36 StartPolling(const typename MojoInterface::StartPollingCallback& callback) { | 44 StartPolling(const typename MojoInterface::StartPollingCallback& callback) { |
| 37 DCHECK(thread_checker_.CalledOnValidThread()); | 45 DCHECK(thread_checker_.CalledOnValidThread()); |
| 38 DCHECK(!is_started_); | 46 DCHECK(!is_started_); |
| 39 if (is_started_) | 47 if (is_started_) |
| 40 return; | 48 return; |
| 41 is_started_ = true; | 49 is_started_ = true; |
| 50 #if defined(OS_ANDROID) |
| 51 SensorManagerAndroid::GetInstance()->SetUITaskRunner(ui_task_runner_); |
| 52 #endif |
| 42 DeviceSensorService::GetInstance()->AddConsumer(consumer_type); | 53 DeviceSensorService::GetInstance()->AddConsumer(consumer_type); |
| 43 callback.Run( | 54 callback.Run( |
| 44 DeviceSensorService::GetInstance()->GetSharedMemoryHandle(consumer_type)); | 55 DeviceSensorService::GetInstance()->GetSharedMemoryHandle(consumer_type)); |
| 45 } | 56 } |
| 46 | 57 |
| 47 template <typename MojoInterface, ConsumerType consumer_type> | 58 template <typename MojoInterface, ConsumerType consumer_type> |
| 48 void DeviceSensorHost<MojoInterface, | 59 void DeviceSensorHost<MojoInterface, |
| 49 consumer_type>::DeviceSensorHost::StopPolling() { | 60 consumer_type>::DeviceSensorHost::StopPolling() { |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); | 61 DCHECK(thread_checker_.CalledOnValidThread()); |
| 51 DCHECK(is_started_); | 62 DCHECK(is_started_); |
| 52 if (!is_started_) | 63 if (!is_started_) |
| 53 return; | 64 return; |
| 54 is_started_ = false; | 65 is_started_ = false; |
| 55 DeviceSensorService::GetInstance()->RemoveConsumer(consumer_type); | 66 DeviceSensorService::GetInstance()->RemoveConsumer(consumer_type); |
| 56 } | 67 } |
| 57 | 68 |
| 58 template class DeviceSensorHost<device::mojom::LightSensor, | 69 template class DeviceSensorHost<device::mojom::LightSensor, |
| 59 CONSUMER_TYPE_LIGHT>; | 70 CONSUMER_TYPE_LIGHT>; |
| 60 template class DeviceSensorHost<device::mojom::MotionSensor, | 71 template class DeviceSensorHost<device::mojom::MotionSensor, |
| 61 CONSUMER_TYPE_MOTION>; | 72 CONSUMER_TYPE_MOTION>; |
| 62 template class DeviceSensorHost<device::mojom::OrientationSensor, | 73 template class DeviceSensorHost<device::mojom::OrientationSensor, |
| 63 CONSUMER_TYPE_ORIENTATION>; | 74 CONSUMER_TYPE_ORIENTATION>; |
| 64 template class DeviceSensorHost<device::mojom::OrientationAbsoluteSensor, | 75 template class DeviceSensorHost<device::mojom::OrientationAbsoluteSensor, |
| 65 CONSUMER_TYPE_ORIENTATION_ABSOLUTE>; | 76 CONSUMER_TYPE_ORIENTATION_ABSOLUTE>; |
| 66 | 77 |
| 67 } // namespace content | 78 } // namespace content |
| OLD | NEW |