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
3 // found in the LICENSE file.
5 #include "components/usb_service/usb_device_impl.h"
9 #include "base/stl_util.h"
10 #include "components/usb_service/usb_context.h"
11 #include "components/usb_service/usb_device_handle_impl.h"
12 #include "components/usb_service/usb_interface_impl.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "third_party/libusb/src/libusb/libusb.h"
16 #if defined(OS_CHROMEOS)
17 #include "base/sys_info.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/dbus/permission_broker_client.h"
20 #endif // defined(OS_CHROMEOS)
22 using content::BrowserThread
;
26 #if defined(OS_CHROMEOS)
27 void OnRequestUsbAccessReplied(
28 const base::Callback
<void(bool success
)>& callback
,
30 BrowserThread::PostTask(
31 BrowserThread::FILE, FROM_HERE
, base::Bind(callback
, success
));
33 #endif // defined(OS_CHROMEOS)
37 namespace usb_service
{
39 UsbDeviceImpl::UsbDeviceImpl(scoped_refptr
<UsbContext
> context
,
40 PlatformUsbDevice platform_device
,
44 : UsbDevice(vendor_id
, product_id
, unique_id
),
45 platform_device_(platform_device
),
47 CHECK(platform_device
) << "platform_device cannot be NULL";
48 libusb_ref_device(platform_device
);
51 UsbDeviceImpl::~UsbDeviceImpl() {
52 DCHECK(thread_checker_
.CalledOnValidThread());
53 for (HandlesVector::iterator it
= handles_
.begin(); it
!= handles_
.end();
55 (*it
)->InternalClose();
57 STLClearObject(&handles_
);
58 libusb_unref_device(platform_device_
);
61 #if defined(OS_CHROMEOS)
63 void UsbDeviceImpl::RequestUsbAcess(
65 const base::Callback
<void(bool success
)>& callback
) {
66 DCHECK(thread_checker_
.CalledOnValidThread());
68 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to
69 // use permission broker.
70 if (base::SysInfo::IsRunningOnChromeOS()) {
71 chromeos::PermissionBrokerClient
* client
=
72 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
73 DCHECK(client
) << "Could not get permission broker client.";
79 BrowserThread::PostTask(
82 base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess
,
83 base::Unretained(client
),
87 base::Bind(&OnRequestUsbAccessReplied
, callback
)));
93 scoped_refptr
<UsbDeviceHandle
> UsbDeviceImpl::Open() {
94 DCHECK(thread_checker_
.CalledOnValidThread());
95 PlatformUsbDeviceHandle handle
;
96 int rv
= libusb_open(platform_device_
, &handle
);
97 if (LIBUSB_SUCCESS
== rv
) {
98 scoped_refptr
<UsbConfigDescriptor
> interfaces
= ListInterfaces();
101 scoped_refptr
<UsbDeviceHandleImpl
> device_handle
=
102 new UsbDeviceHandleImpl(context_
, this, handle
, interfaces
);
103 handles_
.push_back(device_handle
);
104 return device_handle
;
109 bool UsbDeviceImpl::Close(scoped_refptr
<UsbDeviceHandle
> handle
) {
110 DCHECK(thread_checker_
.CalledOnValidThread());
112 for (HandlesVector::iterator it
= handles_
.begin(); it
!= handles_
.end();
115 (*it
)->InternalClose();
123 scoped_refptr
<UsbConfigDescriptor
> UsbDeviceImpl::ListInterfaces() {
124 DCHECK(thread_checker_
.CalledOnValidThread());
126 PlatformUsbConfigDescriptor platform_config
;
127 const int list_result
=
128 libusb_get_active_config_descriptor(platform_device_
, &platform_config
);
129 if (list_result
== 0)
130 return new UsbConfigDescriptorImpl(platform_config
);
135 void UsbDeviceImpl::OnDisconnect() {
136 DCHECK(thread_checker_
.CalledOnValidThread());
137 HandlesVector handles
;
138 swap(handles
, handles_
);
139 for (HandlesVector::iterator it
= handles
.begin(); it
!= handles
.end(); ++it
)
140 (*it
)->InternalClose();
143 } // namespace usb_service