OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_USB_USB_DEVICE_IMPL_H_ | 5 #ifndef DEVICE_USB_USB_DEVICE_IMPL_H_ |
6 #define DEVICE_USB_USB_DEVICE_IMPL_H_ | 6 #define DEVICE_USB_USB_DEVICE_IMPL_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
13 #include "device/usb/usb_descriptors.h" | 13 #include "device/usb/usb_descriptors.h" |
14 #include "device/usb/usb_device.h" | 14 #include "device/usb/usb_device.h" |
15 | 15 |
16 struct libusb_device; | 16 struct libusb_device; |
17 struct libusb_config_descriptor; | 17 struct libusb_config_descriptor; |
| 18 struct libusb_device_handle; |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 class SingleThreadTaskRunner; | 21 class SequencedTaskRunner; |
21 } | 22 } |
22 | 23 |
23 namespace device { | 24 namespace device { |
24 | 25 |
25 class UsbDeviceHandleImpl; | 26 class UsbDeviceHandleImpl; |
26 class UsbContext; | 27 class UsbContext; |
27 | 28 |
28 typedef libusb_device* PlatformUsbDevice; | 29 typedef struct libusb_device* PlatformUsbDevice; |
29 typedef libusb_config_descriptor* PlatformUsbConfigDescriptor; | 30 typedef struct libusb_config_descriptor* PlatformUsbConfigDescriptor; |
| 31 typedef struct libusb_device_handle* PlatformUsbDeviceHandle; |
30 | 32 |
31 class UsbDeviceImpl : public UsbDevice { | 33 class UsbDeviceImpl : public UsbDevice { |
32 public: | 34 public: |
33 // UsbDevice implementation: | 35 // UsbDevice implementation: |
34 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
35 void RequestUsbAccess( | 37 void RequestUsbAccess( |
36 int interface_id, | 38 int interface_id, |
37 const base::Callback<void(bool success)>& callback) override; | 39 const base::Callback<void(bool success)>& callback) override; |
38 #endif // OS_CHROMEOS | 40 #endif // OS_CHROMEOS |
39 scoped_refptr<UsbDeviceHandle> Open() override; | 41 void Open(const OpenCallback& callback) override; |
40 bool Close(scoped_refptr<UsbDeviceHandle> handle) override; | 42 bool Close(scoped_refptr<UsbDeviceHandle> handle) override; |
41 const UsbConfigDescriptor* GetConfiguration() override; | 43 const UsbConfigDescriptor* GetConfiguration() override; |
42 bool GetManufacturer(base::string16* manufacturer) override; | |
43 bool GetProduct(base::string16* product) override; | |
44 bool GetSerialNumber(base::string16* serial_number) override; | |
45 | 44 |
46 protected: | 45 protected: |
47 friend class UsbServiceImpl; | 46 friend class UsbServiceImpl; |
48 friend class UsbDeviceHandleImpl; | 47 friend class UsbDeviceHandleImpl; |
49 | 48 |
50 // Called by UsbServiceImpl only; | 49 // Called by UsbServiceImpl only; |
51 UsbDeviceImpl(scoped_refptr<UsbContext> context, | 50 UsbDeviceImpl(scoped_refptr<UsbContext> context, |
52 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
53 PlatformUsbDevice platform_device, | 51 PlatformUsbDevice platform_device, |
54 uint16 vendor_id, | 52 uint16 vendor_id, |
55 uint16 product_id, | 53 uint16 product_id, |
56 uint32 unique_id); | 54 uint32 unique_id, |
| 55 const base::string16& manufacturer_string, |
| 56 const base::string16& product_string, |
| 57 const base::string16& serial_number, |
| 58 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
57 | 59 |
58 ~UsbDeviceImpl() override; | 60 ~UsbDeviceImpl() override; |
59 | 61 |
60 // Called only by UsbServiceImpl. | 62 // Called only by UsbServiceImpl. |
| 63 PlatformUsbDevice platform_device() const { return platform_device_; } |
| 64 void set_visited(bool visited) { visited_ = visited; } |
| 65 bool was_visited() const { return visited_; } |
61 void OnDisconnect(); | 66 void OnDisconnect(); |
62 | 67 |
63 // Called by UsbDeviceHandleImpl. | 68 // Called by UsbDeviceHandleImpl. |
64 void RefreshConfiguration(); | 69 void RefreshConfiguration(); |
65 | 70 |
66 private: | 71 private: |
| 72 void OpenOnBlockingThread(const OpenCallback& callback); |
| 73 void Opened(PlatformUsbDeviceHandle platform_handle, |
| 74 const OpenCallback& callback); |
| 75 |
67 base::ThreadChecker thread_checker_; | 76 base::ThreadChecker thread_checker_; |
68 PlatformUsbDevice platform_device_; | 77 PlatformUsbDevice platform_device_; |
| 78 bool visited_ = false; |
69 | 79 |
70 // On Linux these properties are read from sysfs when the device is enumerated | |
71 // to avoid hitting the permission broker on Chrome OS for a real string | |
72 // descriptor request. | |
73 base::string16 manufacturer_; | |
74 base::string16 product_; | |
75 base::string16 serial_number_; | |
76 #if !defined(USE_UDEV) | |
77 // On other platforms the device must be opened in order to cache them. This | |
78 // should be delayed until the strings are needed to avoid poor interactions | |
79 // with other applications. | |
80 void CacheStrings(); | |
81 bool strings_cached_; | |
82 #endif | |
83 #if defined(OS_CHROMEOS) | 80 #if defined(OS_CHROMEOS) |
84 // On Chrome OS save the devnode string for requesting path access from | 81 // On Chrome OS save the devnode string for requesting path access from |
85 // permission broker. | 82 // permission broker. |
86 std::string devnode_; | 83 std::string devnode_; |
87 #endif | 84 #endif |
88 | 85 |
89 // The current device configuration descriptor. May be null if the device is | 86 // The current device configuration descriptor. May be null if the device is |
90 // in an unconfigured state. | 87 // in an unconfigured state. |
91 scoped_ptr<UsbConfigDescriptor> configuration_; | 88 scoped_ptr<UsbConfigDescriptor> configuration_; |
92 | 89 |
93 // Retain the context so that it will not be released before UsbDevice. | 90 // Retain the context so that it will not be released before UsbDevice. |
94 scoped_refptr<UsbContext> context_; | 91 scoped_refptr<UsbContext> context_; |
95 | 92 |
96 // Opened handles. | 93 // Opened handles. |
97 typedef std::vector<scoped_refptr<UsbDeviceHandleImpl> > HandlesVector; | 94 typedef std::vector<scoped_refptr<UsbDeviceHandleImpl> > HandlesVector; |
98 HandlesVector handles_; | 95 HandlesVector handles_; |
99 | 96 |
100 // Reference to the UI thread for permission-broker calls. | 97 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
101 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 98 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
102 | 99 |
103 DISALLOW_COPY_AND_ASSIGN(UsbDeviceImpl); | 100 DISALLOW_COPY_AND_ASSIGN(UsbDeviceImpl); |
104 }; | 101 }; |
105 | 102 |
106 } // namespace device | 103 } // namespace device |
107 | 104 |
108 #endif // DEVICE_USB_USB_DEVICE_IMPL_H_ | 105 #endif // DEVICE_USB_USB_DEVICE_IMPL_H_ |
OLD | NEW |