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 "device/generic_sensor/platform_sensor_provider_linux.h" | 5 #include "device/generic_sensor/platform_sensor_provider_linux.h" |
6 | 6 |
7 #include "base/memory/singleton.h" | 7 #include "base/memory/singleton.h" |
8 #include "base/task_runner_util.h" | |
9 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
10 #include "device/generic_sensor/linux/platform_sensor_utils_linux.h" | |
11 #include "device/generic_sensor/linux/sensor_data_linux.h" | |
12 #include "device/generic_sensor/platform_sensor_linux.h" | 9 #include "device/generic_sensor/platform_sensor_linux.h" |
10 #include "device/generic_sensor/platform_sensor_manager_linux.h" | |
13 | 11 |
14 namespace device { | 12 namespace device { |
15 | 13 |
16 // static | 14 // static |
17 PlatformSensorProviderLinux* PlatformSensorProviderLinux::GetInstance() { | 15 PlatformSensorProviderLinux* PlatformSensorProviderLinux::GetInstance() { |
18 return base::Singleton< | 16 return base::Singleton< |
19 PlatformSensorProviderLinux, | 17 PlatformSensorProviderLinux, |
20 base::LeakySingletonTraits<PlatformSensorProviderLinux>>::get(); | 18 base::LeakySingletonTraits<PlatformSensorProviderLinux>>::get(); |
21 } | 19 } |
22 | 20 |
23 PlatformSensorProviderLinux::PlatformSensorProviderLinux() = default; | 21 PlatformSensorProviderLinux::PlatformSensorProviderLinux() = default; |
24 | 22 |
25 PlatformSensorProviderLinux::~PlatformSensorProviderLinux() = default; | 23 PlatformSensorProviderLinux::~PlatformSensorProviderLinux() = default; |
26 | 24 |
27 void PlatformSensorProviderLinux::CreateSensorInternal( | 25 void PlatformSensorProviderLinux::CreateSensorInternal( |
28 mojom::SensorType type, | 26 mojom::SensorType type, |
29 mojo::ScopedSharedBufferMapping mapping, | 27 mojo::ScopedSharedBufferMapping mapping, |
30 const CreateSensorCallback& callback) { | 28 const CreateSensorCallback& callback) { |
31 SensorDataLinux data; | 29 DCHECK(sensor_device_manager_); |
32 if (!InitSensorData(type, &data)) { | |
33 callback.Run(nullptr); | |
34 return; | |
35 } | |
36 | 30 |
37 if (!polling_thread_) | 31 SensorDeviceManager::GetSensorDeviceCallback cb = base::Bind( |
38 polling_thread_.reset(new base::Thread("Sensor polling thread")); | 32 &PlatformSensorProviderLinux::SensorDeviceFound, base::Unretained(this), |
33 type, base::Passed(&mapping), callback); | |
39 | 34 |
40 if (!polling_thread_->IsRunning()) { | 35 sensor_device_manager_->GetSensorDevice(type, cb); |
41 if (!polling_thread_->StartWithOptions( | |
42 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) { | |
43 callback.Run(nullptr); | |
44 return; | |
45 } | |
46 polling_thread_task_runner_ = polling_thread_->task_runner(); | |
47 } | |
48 | |
49 base::PostTaskAndReplyWithResult( | |
50 polling_thread_task_runner_.get(), FROM_HERE, | |
51 base::Bind(SensorReader::Create, data), | |
52 base::Bind(&PlatformSensorProviderLinux::SensorReaderFound, | |
53 base::Unretained(this), type, base::Passed(&mapping), callback, | |
54 data)); | |
55 } | 36 } |
56 | 37 |
57 void PlatformSensorProviderLinux::SensorReaderFound( | 38 void PlatformSensorProviderLinux::SensorDeviceFound( |
58 mojom::SensorType type, | 39 mojom::SensorType type, |
59 mojo::ScopedSharedBufferMapping mapping, | 40 mojo::ScopedSharedBufferMapping mapping, |
60 const PlatformSensorProviderBase::CreateSensorCallback& callback, | 41 const PlatformSensorProviderBase::CreateSensorCallback& callback, |
61 const SensorDataLinux& data, | 42 SensorDeviceLinux* sensor_device) { |
62 std::unique_ptr<SensorReader> sensor_reader) { | |
63 DCHECK(CalledOnValidThread()); | 43 DCHECK(CalledOnValidThread()); |
64 | 44 |
65 if (!sensor_reader) { | 45 if (!sensor_device) { |
66 // If there are no sensors, stop polling thread. | 46 // If there are no sensors, stop polling thread. |
67 if (!HasSensors()) | 47 if (!HasSensors()) |
68 AllSensorsRemoved(); | 48 AllSensorsRemoved(); |
69 callback.Run(nullptr); | 49 callback.Run(nullptr); |
70 return; | 50 return; |
71 } | 51 } |
72 | 52 |
73 callback.Run(new PlatformSensorLinux(type, std::move(mapping), this, data, | 53 if (!StartPollingThread()) { |
74 std::move(sensor_reader), | 54 callback.Run(nullptr); |
75 polling_thread_task_runner_)); | 55 return; |
56 } | |
57 | |
58 callback.Run(new PlatformSensorLinux(type, std::move(mapping), this, | |
59 sensor_device, | |
60 polling_thread_->task_runner())); | |
76 } | 61 } |
77 | 62 |
78 void PlatformSensorProviderLinux::SetFileTaskRunner( | 63 void PlatformSensorProviderLinux::SetFileTaskRunner( |
79 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { | 64 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { |
80 DCHECK(CalledOnValidThread()); | 65 DCHECK(CalledOnValidThread()); |
81 if (!file_task_runner_) | 66 file_task_runner_ = file_task_runner; |
Mikhail
2016/11/30 12:29:36
runner should not be reset in runtime, otherwise s
maksims (do not use this acc)
2016/12/05 13:06:59
Done.
| |
82 file_task_runner_ = file_task_runner; | 67 InitializeSensorDeviceManager(); |
68 } | |
69 | |
70 void PlatformSensorProviderLinux::SetSensorDeviceServiceForTesting( | |
71 std::unique_ptr<SensorDeviceService> device) { | |
72 DCHECK(sensor_device_manager_); | |
73 sensor_device_manager_->SetSensorDeviceServiceForTesting(std::move(device)); | |
83 } | 74 } |
84 | 75 |
85 void PlatformSensorProviderLinux::AllSensorsRemoved() { | 76 void PlatformSensorProviderLinux::AllSensorsRemoved() { |
86 DCHECK(CalledOnValidThread()); | 77 DCHECK(CalledOnValidThread()); |
87 DCHECK(file_task_runner_); | 78 DCHECK(file_task_runner_); |
79 sensor_device_manager_->Shutdown(); | |
88 // When there are no sensors left, the polling thread must be stopped. | 80 // When there are no sensors left, the polling thread must be stopped. |
89 // Stop() can only be called on a different thread that allows io. | 81 // Stop() can only be called on a different thread that allows I/O. |
90 // Thus, browser's file thread is used for this purpose. | 82 // Thus, browser's file thread is used for this purpose. |
91 file_task_runner_->PostTask( | 83 file_task_runner_->PostTask( |
92 FROM_HERE, base::Bind(&PlatformSensorProviderLinux::StopPollingThread, | 84 FROM_HERE, base::Bind(&PlatformSensorProviderLinux::StopPollingThread, |
93 base::Unretained(this))); | 85 base::Unretained(this))); |
94 } | 86 } |
95 | 87 |
88 bool PlatformSensorProviderLinux::StartPollingThread() { | |
89 if (!polling_thread_) | |
90 polling_thread_.reset(new base::Thread("Sensor polling thread")); | |
91 | |
92 if (!polling_thread_->IsRunning()) { | |
93 return polling_thread_->StartWithOptions( | |
94 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | |
95 } | |
96 return true; | |
97 } | |
98 | |
96 void PlatformSensorProviderLinux::StopPollingThread() { | 99 void PlatformSensorProviderLinux::StopPollingThread() { |
97 DCHECK(file_task_runner_); | 100 DCHECK(file_task_runner_); |
98 DCHECK(file_task_runner_->BelongsToCurrentThread()); | 101 DCHECK(file_task_runner_->BelongsToCurrentThread()); |
99 polling_thread_->Stop(); | 102 if (polling_thread_ && polling_thread_->IsRunning()) |
103 polling_thread_->Stop(); | |
104 } | |
105 | |
106 void PlatformSensorProviderLinux::InitializeSensorDeviceManager() { | |
107 if (!sensor_device_manager_) | |
108 sensor_device_manager_.reset(new SensorDeviceManager()); | |
109 sensor_device_manager_->SetBlockingTaskRunner(file_task_runner_); | |
100 } | 110 } |
101 | 111 |
102 } // namespace device | 112 } // namespace device |
OLD | NEW |