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_H_ | 5 #ifndef DEVICE_USB_USB_DEVICE_H_ |
6 #define DEVICE_USB_USB_DEVICE_H_ | 6 #define DEVICE_USB_USB_DEVICE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/observer_list.h" |
16 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
17 #include "device/usb/usb_descriptors.h" | 18 #include "device/usb/usb_descriptors.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 namespace device { | 21 namespace device { |
21 | 22 |
22 class UsbDeviceHandle; | 23 class UsbDeviceHandle; |
23 struct WebUsbAllowedOrigins; | 24 struct WebUsbAllowedOrigins; |
24 | 25 |
25 // A UsbDevice object represents a detected USB device, providing basic | 26 // A UsbDevice object represents a detected USB device, providing basic |
26 // information about it. Methods other than simple property accessors must be | 27 // information about it. Methods other than simple property accessors must be |
27 // called from the thread on which this object was created. For further | 28 // called from the thread on which this object was created. For further |
28 // manipulation of the device, a UsbDeviceHandle must be created from Open() | 29 // manipulation of the device, a UsbDeviceHandle must be created from Open() |
29 // method. | 30 // method. |
30 class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> { | 31 class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> { |
31 public: | 32 public: |
32 using OpenCallback = base::Callback<void(scoped_refptr<UsbDeviceHandle>)>; | 33 using OpenCallback = base::Callback<void(scoped_refptr<UsbDeviceHandle>)>; |
33 using ResultCallback = base::Callback<void(bool success)>; | 34 using ResultCallback = base::Callback<void(bool success)>; |
34 | 35 |
| 36 // This observer interface should be used by objects that need only be |
| 37 // notified about the removal of a particular device as it is more efficient |
| 38 // than registering a large number of observers with UsbService::AddObserver. |
| 39 class Observer { |
| 40 public: |
| 41 virtual ~Observer(); |
| 42 |
| 43 // This method is called when the UsbService that created this object |
| 44 // detects that the device has been disconnected from the host. |
| 45 virtual void OnDeviceRemoved(scoped_refptr<UsbDevice> device); |
| 46 }; |
| 47 |
35 // A unique identifier which remains stable for the lifetime of this device | 48 // A unique identifier which remains stable for the lifetime of this device |
36 // object (i.e., until the device is unplugged or the USB service dies.) | 49 // object (i.e., until the device is unplugged or the USB service dies.) |
37 const std::string& guid() const { return guid_; } | 50 const std::string& guid() const { return guid_; } |
38 | 51 |
39 // Accessors to basic information. | 52 // Accessors to basic information. |
40 uint16_t vendor_id() const { return vendor_id_; } | 53 uint16_t vendor_id() const { return vendor_id_; } |
41 uint16_t product_id() const { return product_id_; } | 54 uint16_t product_id() const { return product_id_; } |
42 const base::string16& manufacturer_string() const { | 55 const base::string16& manufacturer_string() const { |
43 return manufacturer_string_; | 56 return manufacturer_string_; |
44 } | 57 } |
(...skipping 12 matching lines...) Expand all Loading... |
57 // functions are no-ops and always return true. | 70 // functions are no-ops and always return true. |
58 virtual void CheckUsbAccess(const ResultCallback& callback); | 71 virtual void CheckUsbAccess(const ResultCallback& callback); |
59 | 72 |
60 // Creates a UsbDeviceHandle for further manipulation. | 73 // Creates a UsbDeviceHandle for further manipulation. |
61 virtual void Open(const OpenCallback& callback) = 0; | 74 virtual void Open(const OpenCallback& callback) = 0; |
62 | 75 |
63 // Gets the UsbConfigDescriptor for the active device configuration or nullptr | 76 // Gets the UsbConfigDescriptor for the active device configuration or nullptr |
64 // if the device is unconfigured. | 77 // if the device is unconfigured. |
65 virtual const UsbConfigDescriptor* GetActiveConfiguration() = 0; | 78 virtual const UsbConfigDescriptor* GetActiveConfiguration() = 0; |
66 | 79 |
| 80 void AddObserver(Observer* observer); |
| 81 void RemoveObserver(Observer* observer); |
| 82 |
67 protected: | 83 protected: |
| 84 friend class UsbService; |
| 85 |
68 UsbDevice(uint16_t vendor_id, | 86 UsbDevice(uint16_t vendor_id, |
69 uint16_t product_id, | 87 uint16_t product_id, |
70 const base::string16& manufacturer_string, | 88 const base::string16& manufacturer_string, |
71 const base::string16& product_string, | 89 const base::string16& product_string, |
72 const base::string16& serial_number); | 90 const base::string16& serial_number); |
73 virtual ~UsbDevice(); | 91 virtual ~UsbDevice(); |
74 | 92 |
| 93 void NotifyDeviceRemoved(); |
| 94 |
75 // These members must be mutable by subclasses as necessary during device | 95 // These members must be mutable by subclasses as necessary during device |
76 // enumeration. To preserve the thread safety of this object they must remain | 96 // enumeration. To preserve the thread safety of this object they must remain |
77 // constant afterwards. | 97 // constant afterwards. |
78 base::string16 manufacturer_string_; | 98 base::string16 manufacturer_string_; |
79 base::string16 product_string_; | 99 base::string16 product_string_; |
80 base::string16 serial_number_; | 100 base::string16 serial_number_; |
81 scoped_ptr<WebUsbAllowedOrigins> webusb_allowed_origins_; | 101 scoped_ptr<WebUsbAllowedOrigins> webusb_allowed_origins_; |
82 GURL webusb_landing_page_; | 102 GURL webusb_landing_page_; |
83 | 103 |
84 // All of the device's configuration descriptors. | 104 // All of the device's configuration descriptors. |
85 std::vector<UsbConfigDescriptor> configurations_; | 105 std::vector<UsbConfigDescriptor> configurations_; |
86 | 106 |
87 private: | 107 private: |
88 friend class base::RefCountedThreadSafe<UsbDevice>; | 108 friend class base::RefCountedThreadSafe<UsbDevice>; |
89 | 109 |
90 const std::string guid_; | 110 const std::string guid_; |
91 const uint16_t vendor_id_; | 111 const uint16_t vendor_id_; |
92 const uint16_t product_id_; | 112 const uint16_t product_id_; |
93 | 113 |
| 114 base::ObserverList<Observer, true> observer_list_; |
| 115 |
94 DISALLOW_COPY_AND_ASSIGN(UsbDevice); | 116 DISALLOW_COPY_AND_ASSIGN(UsbDevice); |
95 }; | 117 }; |
96 | 118 |
97 } // namespace device | 119 } // namespace device |
98 | 120 |
99 #endif // DEVICE_USB_USB_DEVICE_H_ | 121 #endif // DEVICE_USB_USB_DEVICE_H_ |
OLD | NEW |