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. Methods other than simple property accessors must be
20 // called from the thread on which this object was created. For further
21 // manipulation of the device, a UsbDeviceHandle must be created from Open()
23 class UsbDevice
: public base::RefCountedThreadSafe
<UsbDevice
> {
25 using OpenCallback
= base::Callback
<void(scoped_refptr
<UsbDeviceHandle
>)>;
26 using ResultCallback
= base::Callback
<void(bool success
)>;
28 // A unique identifier which remains stable for the lifetime of this device
29 // object (i.e., until the device is unplugged or the USB service dies.)
30 const std::string
& guid() const { return guid_
; }
32 // Accessors to basic information.
33 uint16
vendor_id() const { return vendor_id_
; }
34 uint16
product_id() const { return product_id_
; }
35 const base::string16
& manufacturer_string() const {
36 return manufacturer_string_
;
38 const base::string16
& product_string() const { return product_string_
; }
39 const base::string16
& serial_number() const { return serial_number_
; }
41 // On ChromeOS the permission_broker service is used to change the ownership
42 // of USB device nodes so that Chrome can open them. On other platforms these
43 // functions are no-ops and always return true.
44 virtual void CheckUsbAccess(const ResultCallback
& callback
);
46 // Creates a UsbDeviceHandle for further manipulation.
47 virtual void Open(const OpenCallback
& callback
) = 0;
49 // Explicitly closes a device handle. This method will be automatically called
50 // by the destructor of a UsbDeviceHandle as well.
51 virtual bool Close(scoped_refptr
<UsbDeviceHandle
> handle
) = 0;
53 // Gets the UsbConfigDescriptor for the active device configuration or nullptr
54 // if the device is unconfigured.
55 virtual const UsbConfigDescriptor
* GetConfiguration() = 0;
58 UsbDevice(uint16 vendor_id
,
60 const base::string16
& manufacturer_string
,
61 const base::string16
& product_string
,
62 const base::string16
& serial_number
);
65 // These members must be mutable by subclasses as necessary during device
66 // enumeration. To preserve the thread safety of this object they must remain
67 // constant afterwards.
68 base::string16 manufacturer_string_
;
69 base::string16 product_string_
;
70 base::string16 serial_number_
;
73 friend class base::RefCountedThreadSafe
<UsbDevice
>;
75 const std::string guid_
;
76 const uint16 vendor_id_
;
77 const uint16 product_id_
;
79 DISALLOW_COPY_AND_ASSIGN(UsbDevice
);
84 #endif // DEVICE_USB_USB_DEVICE_H_