OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "device/sensors/device_sensor_host.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "device/sensors/device_sensor_export.h" | |
12 #include "device/sensors/device_sensor_service.h" | |
13 #include "mojo/public/cpp/bindings/strong_binding.h" | |
14 | |
15 namespace device { | |
16 | |
17 template <typename MojoInterface, ConsumerType consumer_type> | |
18 void DeviceSensorHost<MojoInterface, consumer_type>::Create( | |
19 mojo::InterfaceRequest<MojoInterface> request) { | |
20 mojo::MakeStrongBinding( | |
21 base::WrapUnique(new DeviceSensorHost<MojoInterface, consumer_type>), | |
22 std::move(request)); | |
23 } | |
24 | |
25 template <typename MojoInterface, ConsumerType consumer_type> | |
26 DeviceSensorHost<MojoInterface, consumer_type>::DeviceSensorHost() | |
27 : is_started_(false) { | |
28 #if defined(OS_ANDROID) | |
29 DCHECK(base::MessageLoopForUI::IsCurrent()); | |
30 #else | |
31 DCHECK(base::MessageLoopForIO::IsCurrent()); | |
32 #endif | |
33 } | |
34 | |
35 template <typename MojoInterface, ConsumerType consumer_type> | |
36 DeviceSensorHost<MojoInterface, consumer_type>::~DeviceSensorHost() { | |
37 DCHECK(thread_checker_.CalledOnValidThread()); | |
38 if (is_started_) | |
39 DeviceSensorService::GetInstance()->RemoveConsumer(consumer_type); | |
40 } | |
41 | |
42 template <typename MojoInterface, ConsumerType consumer_type> | |
43 void DeviceSensorHost<MojoInterface, consumer_type>::DeviceSensorHost:: | |
44 StartPolling(typename MojoInterface::StartPollingCallback callback) { | |
45 DCHECK(thread_checker_.CalledOnValidThread()); | |
46 DCHECK(!is_started_); | |
47 if (is_started_) | |
48 return; | |
49 is_started_ = true; | |
50 DeviceSensorService::GetInstance()->AddConsumer(consumer_type); | |
51 std::move(callback).Run( | |
52 DeviceSensorService::GetInstance()->GetSharedMemoryHandle(consumer_type)); | |
53 } | |
54 | |
55 template <typename MojoInterface, ConsumerType consumer_type> | |
56 void DeviceSensorHost<MojoInterface, | |
57 consumer_type>::DeviceSensorHost::StopPolling() { | |
58 DCHECK(thread_checker_.CalledOnValidThread()); | |
59 DCHECK(is_started_); | |
60 if (!is_started_) | |
61 return; | |
62 is_started_ = false; | |
63 DeviceSensorService::GetInstance()->RemoveConsumer(consumer_type); | |
64 } | |
65 | |
66 template class DEVICE_SENSOR_EXPORT | |
67 DeviceSensorHost<device::mojom::MotionSensor, CONSUMER_TYPE_MOTION>; | |
68 template class DEVICE_SENSOR_EXPORT | |
69 DeviceSensorHost<device::mojom::OrientationSensor, | |
70 CONSUMER_TYPE_ORIENTATION>; | |
71 template class DEVICE_SENSOR_EXPORT | |
72 DeviceSensorHost<device::mojom::OrientationAbsoluteSensor, | |
73 CONSUMER_TYPE_ORIENTATION_ABSOLUTE>; | |
74 | |
75 } // namespace device | |
OLD | NEW |