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_IMPL_H_
6 #define DEVICE_USB_USB_DEVICE_IMPL_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/threading/thread_checker.h"
13 #include "device/usb/usb_descriptors.h"
14 #include "device/usb/usb_device.h"
15 #include "device/usb/webusb_descriptors.h"
18 struct libusb_config_descriptor
;
19 struct libusb_device_handle
;
22 class SequencedTaskRunner
;
31 class UsbDeviceHandleImpl
;
34 typedef struct libusb_device
* PlatformUsbDevice
;
35 typedef struct libusb_config_descriptor
* PlatformUsbConfigDescriptor
;
36 typedef struct libusb_device_handle
* PlatformUsbDeviceHandle
;
38 class UsbDeviceImpl
: public UsbDevice
{
40 // UsbDevice implementation:
41 #if defined(OS_CHROMEOS)
42 void CheckUsbAccess(const ResultCallback
& callback
) override
;
44 void Open(const OpenCallback
& callback
) override
;
45 bool Close(scoped_refptr
<UsbDeviceHandle
> handle
) override
;
46 const UsbConfigDescriptor
* GetActiveConfiguration() override
;
48 // These functions are used during enumeration only. The values must not
49 // change during the object's lifetime.
50 void set_manufacturer_string(const base::string16
& value
) {
51 manufacturer_string_
= value
;
53 void set_product_string(const base::string16
& value
) {
54 product_string_
= value
;
56 void set_serial_number(const base::string16
& value
) {
57 serial_number_
= value
;
59 void set_device_path(const std::string
& value
) { device_path_
= value
; }
60 void set_webusb_allowed_origins(scoped_ptr
<WebUsbDescriptorSet
> descriptors
) {
61 webusb_allowed_origins_
= descriptors
.Pass();
63 void set_webusb_landing_page(const GURL
& url
) { webusb_landing_page_
= url
; }
65 PlatformUsbDevice
platform_device() const { return platform_device_
; }
68 friend class UsbServiceImpl
;
69 friend class UsbDeviceHandleImpl
;
71 // Called by UsbServiceImpl only;
72 UsbDeviceImpl(scoped_refptr
<UsbContext
> context
,
73 PlatformUsbDevice platform_device
,
76 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
78 ~UsbDeviceImpl() override
;
80 // Called only by UsbServiceImpl.
81 void set_visited(bool visited
) { visited_
= visited
; }
82 bool was_visited() const { return visited_
; }
84 void ReadAllConfigurations();
86 // Called by UsbDeviceHandleImpl.
87 void RefreshActiveConfiguration();
90 void GetAllConfigurations();
91 #if defined(OS_CHROMEOS)
92 void OnOpenRequestComplete(const OpenCallback
& callback
,
93 dbus::FileDescriptor fd
);
94 void OpenOnBlockingThreadWithFd(dbus::FileDescriptor fd
,
95 const OpenCallback
& callback
);
97 void OpenOnBlockingThread(const OpenCallback
& callback
);
98 void Opened(PlatformUsbDeviceHandle platform_handle
,
99 const OpenCallback
& callback
);
101 base::ThreadChecker thread_checker_
;
102 PlatformUsbDevice platform_device_
;
103 bool visited_
= false;
105 // On Chrome OS device path is necessary to request access from the permission
107 std::string device_path_
;
109 // The current device configuration descriptor. May be null if the device is
110 // in an unconfigured state; if not null, it is a pointer to one of the
111 // items at UsbDevice::configurations_.
112 const UsbConfigDescriptor
* active_configuration_
= nullptr;
114 // Retain the context so that it will not be released before UsbDevice.
115 scoped_refptr
<UsbContext
> context_
;
118 typedef std::vector
<scoped_refptr
<UsbDeviceHandleImpl
> > HandlesVector
;
119 HandlesVector handles_
;
121 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
122 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
124 DISALLOW_COPY_AND_ASSIGN(UsbDeviceImpl
);
127 } // namespace device
129 #endif // DEVICE_USB_USB_DEVICE_IMPL_H_