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 "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/observer_list.h" |
11 | 12 |
12 namespace device { | 13 namespace device { |
13 | 14 |
14 class UsbDeviceHandle; | 15 class UsbDeviceHandle; |
15 struct UsbConfigDescriptor; | 16 struct UsbConfigDescriptor; |
16 | 17 |
17 // A UsbDevice object represents a detected USB device, providing basic | 18 // A UsbDevice object represents a detected USB device, providing basic |
18 // information about it. For further manipulation of the device, a | 19 // information about it. For further manipulation of the device, a |
19 // UsbDeviceHandle must be created from Open() method. | 20 // UsbDeviceHandle must be created from Open() method. |
20 class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> { | 21 class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> { |
21 public: | 22 public: |
| 23 class Observer { |
| 24 public: |
| 25 virtual void OnDisconnect(scoped_refptr<UsbDevice> device) = 0; |
| 26 }; |
| 27 |
22 // Accessors to basic information. | 28 // Accessors to basic information. |
23 uint16 vendor_id() const { return vendor_id_; } | 29 uint16 vendor_id() const { return vendor_id_; } |
24 uint16 product_id() const { return product_id_; } | 30 uint16 product_id() const { return product_id_; } |
25 uint32 unique_id() const { return unique_id_; } | 31 uint32 unique_id() const { return unique_id_; } |
26 | 32 |
27 #if defined(OS_CHROMEOS) | 33 #if defined(OS_CHROMEOS) |
28 // On ChromeOS, if an interface of a claimed device is not claimed, the | 34 // On ChromeOS, if an interface of a claimed device is not claimed, the |
29 // permission broker can change the owner of the device so that the unclaimed | 35 // permission broker can change the owner of the device so that the unclaimed |
30 // interfaces can be used. If this argument is missing, permission broker will | 36 // interfaces can be used. If this argument is missing, permission broker will |
31 // not be used and this method fails if the device is claimed. | 37 // not be used and this method fails if the device is claimed. |
32 virtual void RequestUsbAccess( | 38 virtual void RequestUsbAccess( |
33 int interface_id, | 39 int interface_id, |
34 const base::Callback<void(bool success)>& callback) = 0; | 40 const base::Callback<void(bool success)>& callback) = 0; |
35 #endif // OS_CHROMEOS | 41 #endif // OS_CHROMEOS |
36 | 42 |
37 // Creates a UsbDeviceHandle for further manipulation. | 43 // Creates a UsbDeviceHandle for further manipulation. |
38 // Blocking method. Must be called on FILE thread. | 44 // Blocking method. Must be called on FILE thread. |
39 virtual scoped_refptr<UsbDeviceHandle> Open() = 0; | 45 virtual scoped_refptr<UsbDeviceHandle> Open() = 0; |
40 | 46 |
41 // Explicitly closes a device handle. This method will be automatically called | 47 // Explicitly closes a device handle. This method will be automatically called |
42 // by the destructor of a UsbDeviceHandle as well. | 48 // by the destructor of a UsbDeviceHandle as well. |
43 // Closing a closed handle is a safe | 49 // Closing a closed handle is a safe |
44 // Blocking method. Must be called on FILE thread. | 50 // Blocking method. Must be called on FILE thread. |
45 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) = 0; | 51 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) = 0; |
46 | 52 |
47 // Gets the UsbConfigDescriptor for the active device configuration. | 53 // Gets the UsbConfigDescriptor for the active device configuration. |
48 // Blocking method. Must be called on FILE thread. | 54 // Blocking method. Must be called on FILE thread. |
49 virtual const UsbConfigDescriptor& GetConfiguration() = 0; | 55 virtual const UsbConfigDescriptor& GetConfiguration() = 0; |
50 | 56 |
| 57 void AddObserver(Observer* obs) { observer_list_.AddObserver(obs); } |
| 58 void RemoveObserver(Observer* obs) { observer_list_.RemoveObserver(obs); } |
| 59 |
51 protected: | 60 protected: |
52 UsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) | 61 UsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id); |
53 : vendor_id_(vendor_id), product_id_(product_id), unique_id_(unique_id) {} | 62 virtual ~UsbDevice(); |
54 | 63 |
55 virtual ~UsbDevice() {} | 64 void NotifyDisconnect(); |
56 | 65 |
57 private: | 66 private: |
58 friend class base::RefCountedThreadSafe<UsbDevice>; | 67 friend class base::RefCountedThreadSafe<UsbDevice>; |
59 | 68 |
60 const uint16 vendor_id_; | 69 const uint16 vendor_id_; |
61 const uint16 product_id_; | 70 const uint16 product_id_; |
62 const uint32 unique_id_; | 71 const uint32 unique_id_; |
63 | 72 |
| 73 ObserverList<Observer> observer_list_; |
| 74 |
64 DISALLOW_COPY_AND_ASSIGN(UsbDevice); | 75 DISALLOW_COPY_AND_ASSIGN(UsbDevice); |
65 }; | 76 }; |
66 | 77 |
67 } // namespace device | 78 } // namespace device |
68 | 79 |
69 #endif // DEVICE_USB_USB_DEVICE_H_ | 80 #endif // DEVICE_USB_USB_DEVICE_H_ |
OLD | NEW |