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