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 #ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_H_
6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/threading/thread_checker.h"
14 #include "components/usb_service/usb_interface.h"
15 #include "components/usb_service/usb_service_export.h"
19 namespace usb_service
{
21 class UsbDeviceHandle
;
24 typedef libusb_device
* PlatformUsbDevice
;
26 // A UsbDevice object represents a detected USB device, providing basic
27 // information about it. For further manipulation of the device, a
28 // UsbDeviceHandle must be created from Open() method.
29 class USB_SERVICE_EXPORT UsbDevice
30 : public base::RefCountedThreadSafe
<UsbDevice
> {
32 // Accessors to basic information.
33 PlatformUsbDevice
platform_device() const { return platform_device_
; }
34 uint16
vendor_id() const { return vendor_id_
; }
35 uint16
product_id() const { return product_id_
; }
36 uint32
unique_id() const { return unique_id_
; }
38 #if defined(OS_CHROMEOS)
39 // On ChromeOS, if an interface of a claimed device is not claimed, the
40 // permission broker can change the owner of the device so that the unclaimed
41 // interfaces can be used. If this argument is missing, permission broker will
42 // not be used and this method fails if the device is claimed.
43 virtual void RequestUsbAcess(
45 const base::Callback
<void(bool success
)>& callback
);
48 // Creates a UsbDeviceHandle for further manipulation.
49 // Blocking method. Must be called on FILE thread.
50 virtual scoped_refptr
<UsbDeviceHandle
> Open();
52 // Explicitly closes a device handle. This method will be automatically called
53 // by the destructor of a UsbDeviceHandle as well.
54 // Closing a closed handle is a safe
55 // Blocking method. Must be called on FILE thread.
56 virtual bool Close(scoped_refptr
<UsbDeviceHandle
> handle
);
58 // Lists the interfaces provided by the device and fills the given
59 // UsbConfigDescriptor.
60 // Blocking method. Must be called on FILE thread.
61 virtual scoped_refptr
<UsbConfigDescriptor
> ListInterfaces();
64 friend class UsbService
;
65 friend class base::RefCountedThreadSafe
<UsbDevice
>;
67 // Called by UsbService only;
68 UsbDevice(scoped_refptr
<UsbContext
> context
,
69 PlatformUsbDevice platform_device
,
74 // Constructor called in test only.
78 // Called only be UsbService.
79 virtual void OnDisconnect();
82 PlatformUsbDevice platform_device_
;
87 // Retain the context so that it will not be released before UsbDevice.
88 scoped_refptr
<UsbContext
> context_
;
91 typedef std::vector
<scoped_refptr
<UsbDeviceHandle
> > HandlesVector
;
92 HandlesVector handles_
;
94 base::ThreadChecker thread_checker_
;
96 DISALLOW_COPY_AND_ASSIGN(UsbDevice
);
99 } // namespace usb_service
101 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_H_