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_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "device/usb/usb_descriptors.h"
19 class UsbDeviceHandle
;
20 struct WebUsbDescriptorSet
;
22 // A UsbDevice object represents a detected USB device, providing basic
23 // information about it. Methods other than simple property accessors must be
24 // called from the thread on which this object was created. For further
25 // manipulation of the device, a UsbDeviceHandle must be created from Open()
27 class UsbDevice
: public base::RefCountedThreadSafe
<UsbDevice
> {
29 using OpenCallback
= base::Callback
<void(scoped_refptr
<UsbDeviceHandle
>)>;
30 using ResultCallback
= base::Callback
<void(bool success
)>;
32 // A unique identifier which remains stable for the lifetime of this device
33 // object (i.e., until the device is unplugged or the USB service dies.)
34 const std::string
& guid() const { return guid_
; }
36 // Accessors to basic information.
37 uint16
vendor_id() const { return vendor_id_
; }
38 uint16
product_id() const { return product_id_
; }
39 const base::string16
& manufacturer_string() const {
40 return manufacturer_string_
;
42 const base::string16
& product_string() const { return product_string_
; }
43 const base::string16
& serial_number() const { return serial_number_
; }
44 const WebUsbDescriptorSet
* webusb_allowed_origins() const {
45 return webusb_allowed_origins_
.get();
47 const GURL
& webusb_landing_page() const { return webusb_landing_page_
; }
49 // On ChromeOS the permission_broker service is used to change the ownership
50 // of USB device nodes so that Chrome can open them. On other platforms these
51 // functions are no-ops and always return true.
52 virtual void CheckUsbAccess(const ResultCallback
& callback
);
54 // Creates a UsbDeviceHandle for further manipulation.
55 virtual void Open(const OpenCallback
& callback
) = 0;
57 // Explicitly closes a device handle. This method will be automatically called
58 // by the destructor of a UsbDeviceHandle as well.
59 virtual bool Close(scoped_refptr
<UsbDeviceHandle
> handle
) = 0;
61 // Gets the UsbConfigDescriptor for the active device configuration or nullptr
62 // if the device is unconfigured.
63 virtual const UsbConfigDescriptor
* GetActiveConfiguration() = 0;
65 // Gets all of the device's UsbConfigDescriptors.
66 const std::vector
<UsbConfigDescriptor
>& configurations() const {
67 return configurations_
;
71 UsbDevice(uint16 vendor_id
,
73 const base::string16
& manufacturer_string
,
74 const base::string16
& product_string
,
75 const base::string16
& serial_number
);
78 // These members must be mutable by subclasses as necessary during device
79 // enumeration. To preserve the thread safety of this object they must remain
80 // constant afterwards.
81 base::string16 manufacturer_string_
;
82 base::string16 product_string_
;
83 base::string16 serial_number_
;
84 scoped_ptr
<WebUsbDescriptorSet
> webusb_allowed_origins_
;
85 GURL webusb_landing_page_
;
87 // All of the device's configuration descriptors.
88 std::vector
<UsbConfigDescriptor
> configurations_
;
91 friend class base::RefCountedThreadSafe
<UsbDevice
>;
93 const std::string guid_
;
94 const uint16 vendor_id_
;
95 const uint16 product_id_
;
97 DISALLOW_COPY_AND_ASSIGN(UsbDevice
);
100 } // namespace device
102 #endif // DEVICE_USB_USB_DEVICE_H_