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" | 8 #include "base/task_runner_util.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "device/generic_sensor/linux/sensor_data_linux.h" | 10 #include "device/generic_sensor/linux/sensor_data_linux.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 return; | 55 return; |
56 } | 56 } |
57 SensorDeviceFound(type, std::move(mapping), callback, sensor_device); | 57 SensorDeviceFound(type, std::move(mapping), callback, sensor_device); |
58 } | 58 } |
59 | 59 |
60 void PlatformSensorProviderLinux::SensorDeviceFound( | 60 void PlatformSensorProviderLinux::SensorDeviceFound( |
61 mojom::SensorType type, | 61 mojom::SensorType type, |
62 mojo::ScopedSharedBufferMapping mapping, | 62 mojo::ScopedSharedBufferMapping mapping, |
63 const PlatformSensorProviderBase::CreateSensorCallback& callback, | 63 const PlatformSensorProviderBase::CreateSensorCallback& callback, |
64 const SensorInfoLinux* sensor_device) { | 64 const SensorInfoLinux* sensor_device) { |
65 DCHECK(CalledOnValidThread()); | 65 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
66 DCHECK(sensor_device); | 66 DCHECK(sensor_device); |
67 | 67 |
68 if (!StartPollingThread()) { | 68 if (!StartPollingThread()) { |
69 callback.Run(nullptr); | 69 callback.Run(nullptr); |
70 return; | 70 return; |
71 } | 71 } |
72 | 72 |
73 scoped_refptr<PlatformSensorLinux> sensor = | 73 scoped_refptr<PlatformSensorLinux> sensor = |
74 new PlatformSensorLinux(type, std::move(mapping), this, sensor_device, | 74 new PlatformSensorLinux(type, std::move(mapping), this, sensor_device, |
75 polling_thread_->task_runner()); | 75 polling_thread_->task_runner()); |
76 callback.Run(sensor); | 76 callback.Run(sensor); |
77 } | 77 } |
78 | 78 |
79 void PlatformSensorProviderLinux::SetFileTaskRunner( | 79 void PlatformSensorProviderLinux::SetFileTaskRunner( |
80 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { | 80 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { |
81 DCHECK(CalledOnValidThread()); | 81 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
82 if (!file_task_runner_) | 82 if (!file_task_runner_) |
83 file_task_runner_ = file_task_runner; | 83 file_task_runner_ = file_task_runner; |
84 } | 84 } |
85 | 85 |
86 void PlatformSensorProviderLinux::AllSensorsRemoved() { | 86 void PlatformSensorProviderLinux::AllSensorsRemoved() { |
87 DCHECK(CalledOnValidThread()); | 87 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
88 DCHECK(file_task_runner_); | 88 DCHECK(file_task_runner_); |
89 Shutdown(); | 89 Shutdown(); |
90 // When there are no sensors left, the polling thread must be stopped. | 90 // When there are no sensors left, the polling thread must be stopped. |
91 // Stop() can only be called on a different thread that allows I/O. | 91 // Stop() can only be called on a different thread that allows I/O. |
92 // Thus, browser's file thread is used for this purpose. | 92 // Thus, browser's file thread is used for this purpose. |
93 file_task_runner_->PostTask( | 93 file_task_runner_->PostTask( |
94 FROM_HERE, base::Bind(&PlatformSensorProviderLinux::StopPollingThread, | 94 FROM_HERE, base::Bind(&PlatformSensorProviderLinux::StopPollingThread, |
95 base::Unretained(this))); | 95 base::Unretained(this))); |
96 } | 96 } |
97 | 97 |
98 bool PlatformSensorProviderLinux::StartPollingThread() { | 98 bool PlatformSensorProviderLinux::StartPollingThread() { |
99 if (!polling_thread_) | 99 if (!polling_thread_) |
100 polling_thread_.reset(new base::Thread("Sensor polling thread")); | 100 polling_thread_.reset(new base::Thread("Sensor polling thread")); |
101 | 101 |
102 if (!polling_thread_->IsRunning()) { | 102 if (!polling_thread_->IsRunning()) { |
103 return polling_thread_->StartWithOptions( | 103 return polling_thread_->StartWithOptions( |
104 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 104 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
105 } | 105 } |
106 return true; | 106 return true; |
107 } | 107 } |
108 | 108 |
109 void PlatformSensorProviderLinux::StopPollingThread() { | 109 void PlatformSensorProviderLinux::StopPollingThread() { |
110 DCHECK(file_task_runner_); | 110 DCHECK(file_task_runner_); |
111 DCHECK(file_task_runner_->BelongsToCurrentThread()); | 111 DCHECK(file_task_runner_->BelongsToCurrentThread()); |
112 if (polling_thread_ && polling_thread_->IsRunning()) | 112 if (polling_thread_ && polling_thread_->IsRunning()) |
113 polling_thread_->Stop(); | 113 polling_thread_->Stop(); |
114 } | 114 } |
115 | 115 |
116 void PlatformSensorProviderLinux::Shutdown() { | 116 void PlatformSensorProviderLinux::Shutdown() { |
117 DCHECK(CalledOnValidThread()); | 117 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
118 const bool did_post_task = file_task_runner_->DeleteSoon( | 118 const bool did_post_task = file_task_runner_->DeleteSoon( |
119 FROM_HERE, sensor_device_manager_.release()); | 119 FROM_HERE, sensor_device_manager_.release()); |
120 DCHECK(did_post_task); | 120 DCHECK(did_post_task); |
121 sensor_nodes_enumerated_ = false; | 121 sensor_nodes_enumerated_ = false; |
122 sensor_nodes_enumeration_started_ = false; | 122 sensor_nodes_enumeration_started_ = false; |
123 sensor_devices_by_type_.clear(); | 123 sensor_devices_by_type_.clear(); |
124 } | 124 } |
125 | 125 |
126 SensorInfoLinux* PlatformSensorProviderLinux::GetSensorDevice( | 126 SensorInfoLinux* PlatformSensorProviderLinux::GetSensorDevice( |
127 mojom::SensorType type) { | 127 mojom::SensorType type) { |
128 DCHECK(CalledOnValidThread()); | 128 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
129 auto sensor = sensor_devices_by_type_.find(type); | 129 auto sensor = sensor_devices_by_type_.find(type); |
130 if (sensor == sensor_devices_by_type_.end()) | 130 if (sensor == sensor_devices_by_type_.end()) |
131 return nullptr; | 131 return nullptr; |
132 return sensor->second.get(); | 132 return sensor->second.get(); |
133 } | 133 } |
134 | 134 |
135 void PlatformSensorProviderLinux::GetAllSensorDevices() { | 135 void PlatformSensorProviderLinux::GetAllSensorDevices() { |
136 DCHECK(CalledOnValidThread()); | 136 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
137 // TODO(maksims): implement this method once we have discovery API. | 137 // TODO(maksims): implement this method once we have discovery API. |
138 NOTIMPLEMENTED(); | 138 NOTIMPLEMENTED(); |
139 } | 139 } |
140 | 140 |
141 void PlatformSensorProviderLinux::SetSensorDeviceManagerForTesting( | 141 void PlatformSensorProviderLinux::SetSensorDeviceManagerForTesting( |
142 std::unique_ptr<SensorDeviceManager> sensor_device_manager) { | 142 std::unique_ptr<SensorDeviceManager> sensor_device_manager) { |
143 DCHECK(CalledOnValidThread()); | 143 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
144 Shutdown(); | 144 Shutdown(); |
145 sensor_device_manager_ = std::move(sensor_device_manager); | 145 sensor_device_manager_ = std::move(sensor_device_manager); |
146 } | 146 } |
147 | 147 |
148 void PlatformSensorProviderLinux::SetFileTaskRunnerForTesting( | 148 void PlatformSensorProviderLinux::SetFileTaskRunnerForTesting( |
149 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 149 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
150 DCHECK(CalledOnValidThread()); | 150 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
151 file_task_runner_ = std::move(task_runner); | 151 file_task_runner_ = std::move(task_runner); |
152 } | 152 } |
153 | 153 |
154 void PlatformSensorProviderLinux::ProcessStoredRequests() { | 154 void PlatformSensorProviderLinux::ProcessStoredRequests() { |
155 DCHECK(CalledOnValidThread()); | 155 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
156 std::vector<mojom::SensorType> request_types = GetPendingRequestTypes(); | 156 std::vector<mojom::SensorType> request_types = GetPendingRequestTypes(); |
157 if (request_types.empty()) | 157 if (request_types.empty()) |
158 return; | 158 return; |
159 | 159 |
160 for (auto const& type : request_types) { | 160 for (auto const& type : request_types) { |
161 SensorInfoLinux* device = nullptr; | 161 SensorInfoLinux* device = nullptr; |
162 auto device_entry = sensor_devices_by_type_.find(type); | 162 auto device_entry = sensor_devices_by_type_.find(type); |
163 if (device_entry != sensor_devices_by_type_.end()) | 163 if (device_entry != sensor_devices_by_type_.end()) |
164 device = device_entry->second.get(); | 164 device = device_entry->second.get(); |
165 CreateSensorAndNotify(type, device); | 165 CreateSensorAndNotify(type, device); |
166 } | 166 } |
167 } | 167 } |
168 | 168 |
169 void PlatformSensorProviderLinux::CreateSensorAndNotify( | 169 void PlatformSensorProviderLinux::CreateSensorAndNotify( |
170 mojom::SensorType type, | 170 mojom::SensorType type, |
171 SensorInfoLinux* sensor_device) { | 171 SensorInfoLinux* sensor_device) { |
172 DCHECK(CalledOnValidThread()); | 172 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
173 scoped_refptr<PlatformSensorLinux> sensor; | 173 scoped_refptr<PlatformSensorLinux> sensor; |
174 mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type); | 174 mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type); |
175 if (sensor_device && mapping && StartPollingThread()) { | 175 if (sensor_device && mapping && StartPollingThread()) { |
176 sensor = | 176 sensor = |
177 new PlatformSensorLinux(type, std::move(mapping), this, sensor_device, | 177 new PlatformSensorLinux(type, std::move(mapping), this, sensor_device, |
178 polling_thread_->task_runner()); | 178 polling_thread_->task_runner()); |
179 } | 179 } |
180 NotifySensorCreated(type, sensor); | 180 NotifySensorCreated(type, sensor); |
181 } | 181 } |
182 | 182 |
183 void PlatformSensorProviderLinux::OnSensorNodesEnumerated() { | 183 void PlatformSensorProviderLinux::OnSensorNodesEnumerated() { |
184 DCHECK(CalledOnValidThread()); | 184 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
185 DCHECK(!sensor_nodes_enumerated_); | 185 DCHECK(!sensor_nodes_enumerated_); |
186 sensor_nodes_enumerated_ = true; | 186 sensor_nodes_enumerated_ = true; |
187 ProcessStoredRequests(); | 187 ProcessStoredRequests(); |
188 } | 188 } |
189 | 189 |
190 void PlatformSensorProviderLinux::OnDeviceAdded( | 190 void PlatformSensorProviderLinux::OnDeviceAdded( |
191 mojom::SensorType type, | 191 mojom::SensorType type, |
192 std::unique_ptr<SensorInfoLinux> sensor_device) { | 192 std::unique_ptr<SensorInfoLinux> sensor_device) { |
193 DCHECK(CalledOnValidThread()); | 193 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
194 // At the moment, we support only one device per type. | 194 // At the moment, we support only one device per type. |
195 if (base::ContainsKey(sensor_devices_by_type_, type)) { | 195 if (base::ContainsKey(sensor_devices_by_type_, type)) { |
196 DVLOG(1) << "Sensor ignored. Type " << type | 196 DVLOG(1) << "Sensor ignored. Type " << type |
197 << ". Node: " << sensor_device->device_node; | 197 << ". Node: " << sensor_device->device_node; |
198 return; | 198 return; |
199 } | 199 } |
200 sensor_devices_by_type_[type] = std::move(sensor_device); | 200 sensor_devices_by_type_[type] = std::move(sensor_device); |
201 } | 201 } |
202 | 202 |
203 void PlatformSensorProviderLinux::OnDeviceRemoved( | 203 void PlatformSensorProviderLinux::OnDeviceRemoved( |
204 mojom::SensorType type, | 204 mojom::SensorType type, |
205 const std::string& device_node) { | 205 const std::string& device_node) { |
206 DCHECK(CalledOnValidThread()); | 206 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
207 auto it = sensor_devices_by_type_.find(type); | 207 auto it = sensor_devices_by_type_.find(type); |
208 if (it != sensor_devices_by_type_.end() && | 208 if (it != sensor_devices_by_type_.end() && |
209 it->second->device_node == device_node) | 209 it->second->device_node == device_node) |
210 sensor_devices_by_type_.erase(it); | 210 sensor_devices_by_type_.erase(it); |
211 } | 211 } |
212 | 212 |
213 } // namespace device | 213 } // namespace device |
OLD | NEW |