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 DEVICE_USB_USB_DEVICE_H_
6 #define DEVICE_USB_USB_DEVICE_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/strings/string16.h"
15 class UsbDeviceHandle
;
16 struct UsbConfigDescriptor
;
18 // A UsbDevice object represents a detected USB device, providing basic
19 // information about it. For further manipulation of the device, a
20 // UsbDeviceHandle must be created from Open() method.
21 class UsbDevice
: public base::RefCountedThreadSafe
<UsbDevice
> {
23 typedef base::Callback
<void(bool success
)> ResultCallback
;
25 // Accessors to basic information.
26 uint16
vendor_id() const { return vendor_id_
; }
27 uint16
product_id() const { return product_id_
; }
28 uint32
unique_id() const { return unique_id_
; }
30 // On ChromeOS the permission_broker service is used to change the ownership
31 // of USB device nodes so that Chrome can open them. On other platforms these
32 // functions are no-ops and always return true.
33 virtual void CheckUsbAccess(const ResultCallback
& callback
);
35 // Like CheckUsbAccess but actually changes the ownership of the device node.
36 virtual void RequestUsbAccess(int interface_id
,
37 const ResultCallback
& callback
);
39 // Creates a UsbDeviceHandle for further manipulation.
40 // Blocking method. Must be called on FILE thread.
41 virtual scoped_refptr
<UsbDeviceHandle
> Open() = 0;
43 // Explicitly closes a device handle. This method will be automatically called
44 // by the destructor of a UsbDeviceHandle as well.
45 // Closing a closed handle is a safe
46 // Blocking method. Must be called on FILE thread.
47 virtual bool Close(scoped_refptr
<UsbDeviceHandle
> handle
) = 0;
49 // Gets the UsbConfigDescriptor for the active device configuration or nullptr
50 // if the device is unconfigured.
51 // Blocking method. Must be called on FILE thread.
52 virtual const UsbConfigDescriptor
* GetConfiguration() = 0;
54 // Gets the manufacturer string of the device, or false and an empty
55 // string. This is a blocking method and must be called on FILE thread.
56 // TODO(reillyg): Make this available from the UI thread. crbug.com/427985
57 virtual bool GetManufacturer(base::string16
* manufacturer
) = 0;
59 // Gets the product string of the device, or returns false and an empty
60 // string. This is a blocking method and must be called on FILE thread.
61 // TODO(reillyg): Make this available from the UI thread. crbug.com/427985
62 virtual bool GetProduct(base::string16
* product
) = 0;
64 // Gets the serial number string of the device, or returns false and an empty
65 // string. This is a blocking method and must be called on FILE thread.
66 // TODO(reillyg): Make this available from the UI thread. crbug.com/427985
67 virtual bool GetSerialNumber(base::string16
* serial
) = 0;
70 UsbDevice(uint16 vendor_id
, uint16 product_id
, uint32 unique_id
);
74 friend class base::RefCountedThreadSafe
<UsbDevice
>;
76 const uint16 vendor_id_
;
77 const uint16 product_id_
;
78 const uint32 unique_id_
;
80 DISALLOW_COPY_AND_ASSIGN(UsbDevice
);
85 #endif // DEVICE_USB_USB_DEVICE_H_