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

Side by Side Diff: device/usb/usb_device_impl.cc

Issue 1897483003: Revert of Replace libusb in the Linux/Chrome OS USB I/O path. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months 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
« no previous file with comments | « device/usb/usb_device_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "device/usb/usb_device_impl.h" 5 #include "device/usb/usb_device_impl.h"
6 6
7 #include <fcntl.h>
8 #include <stddef.h> 7 #include <stddef.h>
9 8
10 #include <algorithm> 9 #include <algorithm>
11 10
12 #include "base/bind.h" 11 #include "base/bind.h"
13 #include "base/location.h" 12 #include "base/location.h"
14 #include "base/posix/eintr_wrapper.h"
15 #include "base/sequenced_task_runner.h" 13 #include "base/sequenced_task_runner.h"
16 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
17 #include "base/stl_util.h" 15 #include "base/stl_util.h"
18 #include "base/thread_task_runner_handle.h" 16 #include "base/thread_task_runner_handle.h"
19 #include "build/build_config.h" 17 #include "build/build_config.h"
20 #include "components/device_event_log/device_event_log.h" 18 #include "components/device_event_log/device_event_log.h"
21 #include "device/usb/usb_context.h" 19 #include "device/usb/usb_context.h"
22 #include "device/usb/usb_descriptors.h" 20 #include "device/usb/usb_descriptors.h"
21 #include "device/usb/usb_device_handle_impl.h"
23 #include "device/usb/usb_error.h" 22 #include "device/usb/usb_error.h"
24 #include "third_party/libusb/src/libusb/libusb.h" 23 #include "third_party/libusb/src/libusb/libusb.h"
25 24
26 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_LINUX)
27 #include "device/usb/usb_device_handle_usbfs.h"
28 #else
29 #include "device/usb/usb_device_handle_impl.h"
30 #endif
31
32 #if defined(OS_CHROMEOS) 25 #if defined(OS_CHROMEOS)
33 #include "chromeos/dbus/dbus_thread_manager.h" 26 #include "chromeos/dbus/dbus_thread_manager.h"
34 #include "chromeos/dbus/permission_broker_client.h" 27 #include "chromeos/dbus/permission_broker_client.h"
35 #include "dbus/file_descriptor.h" // nogncheck 28 #include "dbus/file_descriptor.h" // nogncheck
36 #endif // defined(OS_CHROMEOS) 29 #endif // defined(OS_CHROMEOS)
37 30
38 namespace device { 31 namespace device {
39 32
40 namespace { 33 namespace {
41 34
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 device_path_, 194 device_path_,
202 base::Bind(&UsbDeviceImpl::OnOpenRequestComplete, this, callback), 195 base::Bind(&UsbDeviceImpl::OnOpenRequestComplete, this, callback),
203 base::Bind(&UsbDeviceImpl::OnOpenRequestError, this, callback)); 196 base::Bind(&UsbDeviceImpl::OnOpenRequestError, this, callback));
204 #else 197 #else
205 blocking_task_runner_->PostTask( 198 blocking_task_runner_->PostTask(
206 FROM_HERE, 199 FROM_HERE,
207 base::Bind(&UsbDeviceImpl::OpenOnBlockingThread, this, callback)); 200 base::Bind(&UsbDeviceImpl::OpenOnBlockingThread, this, callback));
208 #endif // defined(OS_CHROMEOS) 201 #endif // defined(OS_CHROMEOS)
209 } 202 }
210 203
211 void UsbDeviceImpl::HandleClosed(UsbDeviceHandle* handle) { 204 void UsbDeviceImpl::HandleClosed(scoped_refptr<UsbDeviceHandle> handle) {
212 DCHECK(thread_checker_.CalledOnValidThread()); 205 DCHECK(thread_checker_.CalledOnValidThread());
213 handles_.remove(handle); 206 handles_.remove(handle.get());
214 } 207 }
215 208
216 const UsbConfigDescriptor* UsbDeviceImpl::GetActiveConfiguration() const { 209 const UsbConfigDescriptor* UsbDeviceImpl::GetActiveConfiguration() const {
217 DCHECK(thread_checker_.CalledOnValidThread()); 210 DCHECK(thread_checker_.CalledOnValidThread());
218 return active_configuration_; 211 return active_configuration_;
219 } 212 }
220 213
221 void UsbDeviceImpl::OnDisconnect() { 214 void UsbDeviceImpl::OnDisconnect() {
222 DCHECK(thread_checker_.CalledOnValidThread()); 215 DCHECK(thread_checker_.CalledOnValidThread());
223 // Swap out the handle list as HandleClosed() will try to modify it. 216 // Swap out the handle list as HandleClosed() will try to modify it.
(...skipping 26 matching lines...) Expand all
250 ConvertConfigDescriptor(platform_config, &config_descriptor); 243 ConvertConfigDescriptor(platform_config, &config_descriptor);
251 configurations_.push_back(config_descriptor); 244 configurations_.push_back(config_descriptor);
252 libusb_free_config_descriptor(platform_config); 245 libusb_free_config_descriptor(platform_config);
253 } 246 }
254 } else { 247 } else {
255 USB_LOG(EVENT) << "Failed to get device descriptor: " 248 USB_LOG(EVENT) << "Failed to get device descriptor: "
256 << ConvertPlatformUsbErrorToString(rv); 249 << ConvertPlatformUsbErrorToString(rv);
257 } 250 }
258 } 251 }
259 252
260 void UsbDeviceImpl::ActiveConfigurationChanged(int configuration_value) {
261 for (const auto& config : configurations_) {
262 if (config.configuration_value == configuration_value) {
263 active_configuration_ = &config;
264 return;
265 }
266 }
267 }
268
269 void UsbDeviceImpl::RefreshActiveConfiguration() { 253 void UsbDeviceImpl::RefreshActiveConfiguration() {
270 active_configuration_ = nullptr; 254 active_configuration_ = nullptr;
271 libusb_config_descriptor* platform_config; 255 libusb_config_descriptor* platform_config;
272 int rv = 256 int rv =
273 libusb_get_active_config_descriptor(platform_device_, &platform_config); 257 libusb_get_active_config_descriptor(platform_device_, &platform_config);
274 if (rv != LIBUSB_SUCCESS) { 258 if (rv != LIBUSB_SUCCESS) {
275 USB_LOG(EVENT) << "Failed to get config descriptor: " 259 USB_LOG(EVENT) << "Failed to get config descriptor: "
276 << ConvertPlatformUsbErrorToString(rv); 260 << ConvertPlatformUsbErrorToString(rv);
277 return; 261 return;
278 } 262 }
279 263
280 ActiveConfigurationChanged(platform_config->bConfigurationValue); 264 for (const auto& config : configurations_) {
265 if (config.configuration_value == platform_config->bConfigurationValue) {
266 active_configuration_ = &config;
267 break;
268 }
269 }
270
281 libusb_free_config_descriptor(platform_config); 271 libusb_free_config_descriptor(platform_config);
282 } 272 }
283 273
284 #if defined(OS_CHROMEOS) 274 #if defined(OS_CHROMEOS)
285 275
286 void UsbDeviceImpl::OnOpenRequestComplete(const OpenCallback& callback, 276 void UsbDeviceImpl::OnOpenRequestComplete(const OpenCallback& callback,
287 dbus::FileDescriptor fd) { 277 dbus::FileDescriptor fd) {
288 blocking_task_runner_->PostTask( 278 blocking_task_runner_->PostTask(
289 FROM_HERE, base::Bind(&UsbDeviceImpl::OpenOnBlockingThreadWithFd, this, 279 FROM_HERE, base::Bind(&UsbDeviceImpl::OpenOnBlockingThreadWithFd, this,
290 base::Passed(&fd), callback)); 280 base::Passed(&fd), callback));
291 } 281 }
292 282
293 void UsbDeviceImpl::OnOpenRequestError(const OpenCallback& callback, 283 void UsbDeviceImpl::OnOpenRequestError(const OpenCallback& callback,
294 const std::string& error_name, 284 const std::string& error_name,
295 const std::string& error_message) { 285 const std::string& error_message) {
296 USB_LOG(EVENT) << "Permission broker failed to open the device: " 286 USB_LOG(EVENT) << "Permission broker failed to open the device: "
297 << error_name << ": " << error_message; 287 << error_name << ": " << error_message;
298 callback.Run(nullptr); 288 callback.Run(nullptr);
299 } 289 }
300 290
301 void UsbDeviceImpl::OpenOnBlockingThreadWithFd(dbus::FileDescriptor fd, 291 void UsbDeviceImpl::OpenOnBlockingThreadWithFd(dbus::FileDescriptor fd,
302 const OpenCallback& callback) { 292 const OpenCallback& callback) {
303 fd.CheckValidity(); 293 fd.CheckValidity();
304 if (fd.is_valid()) { 294 DCHECK(fd.is_valid());
305 base::ScopedFD scoped_fd(fd.TakeValue()); 295
306 task_runner_->PostTask(FROM_HERE, 296 PlatformUsbDeviceHandle handle;
307 base::Bind(&UsbDeviceImpl::Opened, this, 297 const int rv = libusb_open_fd(platform_device_, fd.TakeValue(), &handle);
308 base::Passed(&scoped_fd), callback)); 298 if (LIBUSB_SUCCESS == rv) {
299 task_runner_->PostTask(
300 FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback));
309 } else { 301 } else {
310 USB_LOG(EVENT) << "Did not get valid device handle from permission broker."; 302 USB_LOG(EVENT) << "Failed to open device: "
303 << ConvertPlatformUsbErrorToString(rv);
311 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); 304 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
312 } 305 }
313 } 306 }
314 307
315 #else 308 #endif // defined(OS_CHROMEOS)
316 #if defined(OS_LINUX)
317
318 void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) {
319 base::ScopedFD fd(HANDLE_EINTR(open(device_path_.c_str(), O_RDWR)));
320 if (fd.is_valid()) {
321 task_runner_->PostTask(FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this,
322 base::Passed(&fd), callback));
323 } else {
324 USB_PLOG(EVENT) << "Failed to open device";
325 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
326 }
327 }
328
329 #else
330 309
331 void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) { 310 void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) {
332 PlatformUsbDeviceHandle handle; 311 PlatformUsbDeviceHandle handle;
333 const int rv = libusb_open(platform_device_, &handle); 312 const int rv = libusb_open(platform_device_, &handle);
334 if (LIBUSB_SUCCESS == rv) { 313 if (LIBUSB_SUCCESS == rv) {
335 task_runner_->PostTask( 314 task_runner_->PostTask(
336 FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback)); 315 FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback));
337 } else { 316 } else {
338 USB_LOG(EVENT) << "Failed to open device: " 317 USB_LOG(EVENT) << "Failed to open device: "
339 << ConvertPlatformUsbErrorToString(rv); 318 << ConvertPlatformUsbErrorToString(rv);
319 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
340 } 320 }
341 } 321 }
342 322
343 #endif // defined(OS_LINUX)
344 #endif // defined(OS_CHROMEOS)
345
346 #if defined(OS_LINUX)
347
348 void UsbDeviceImpl::Opened(base::ScopedFD fd, const OpenCallback& callback) {
349 DCHECK(thread_checker_.CalledOnValidThread());
350 scoped_refptr<UsbDeviceHandle> device_handle =
351 new UsbDeviceHandleUsbfs(this, std::move(fd), blocking_task_runner_);
352 handles_.push_back(device_handle.get());
353 callback.Run(device_handle);
354 }
355
356 #else
357
358 void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle, 323 void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle,
359 const OpenCallback& callback) { 324 const OpenCallback& callback) {
360 DCHECK(thread_checker_.CalledOnValidThread()); 325 DCHECK(thread_checker_.CalledOnValidThread());
361 scoped_refptr<UsbDeviceHandle> device_handle = new UsbDeviceHandleImpl( 326 scoped_refptr<UsbDeviceHandleImpl> device_handle = new UsbDeviceHandleImpl(
362 context_, this, platform_handle, blocking_task_runner_); 327 context_, this, platform_handle, blocking_task_runner_);
363 handles_.push_back(device_handle.get()); 328 handles_.push_back(device_handle.get());
364 callback.Run(device_handle); 329 callback.Run(device_handle);
365 } 330 }
366 331
367 #endif // defined(OS_LINUX)
368
369 } // namespace device 332 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/usb_device_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698