OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_USB_USB_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_USB_USB_SERVICE_H_ |
6 #define CHROME_BROWSER_USB_USB_SERVICE_H_ | 6 #define CHROME_BROWSER_USB_USB_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/callback.h" |
| 14 #include "base/memory/ref_counted.h" |
13 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
14 #include "chrome/browser/usb/usb_device.h" | |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 | 18 |
18 template <class T> class DeleteHelper; | 19 template <class T> class DeleteHelper; |
19 | 20 |
20 } // namespace base | 21 } // namespace base |
21 | 22 |
22 template <typename T> struct DefaultSingletonTraits; | 23 template <typename T> struct DefaultSingletonTraits; |
| 24 |
| 25 typedef struct libusb_device* PlatformUsbDevice; |
| 26 |
23 class UsbContext; | 27 class UsbContext; |
| 28 class UsbDevice; |
24 | 29 |
25 // The USB service handles creating and managing an event handler thread that is | 30 // The USB service handles creating and managing an event handler thread that is |
26 // used to manage and dispatch USB events. It is also responsbile for device | 31 // used to manage and dispatch USB events. It is also responsible for device |
27 // discovery on the system, which allows it to re-use device handles to prevent | 32 // discovery on the system, which allows it to re-use device handles to prevent |
28 // competition for the same USB device. | 33 // competition for the same USB device. |
29 class UsbService { | 34 class UsbService { |
30 public: | 35 public: |
31 typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > | 36 typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > |
32 ScopedDeviceVector; | 37 ScopedDeviceVector; |
33 | 38 |
34 // Must be called on FILE thread. | 39 // Must be called on FILE thread. |
35 static UsbService* GetInstance(); | 40 static UsbService* GetInstance(); |
36 | 41 |
37 // Find all of the devices attached to the system that are identified by | 42 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id); |
38 // |vendor_id| and |product_id|, inserting them into |devices|. Clears | |
39 // |devices| before use. Calls |callback| once |devices| is populated. | |
40 // The result will be sorted by id in increasing order. Must be called on | |
41 // FILE thread. | |
42 void FindDevices( | |
43 const uint16 vendor_id, | |
44 const uint16 product_id, | |
45 int interface_id, | |
46 const base::Callback<void(ScopedDeviceVector vector)>& callback); | |
47 | 43 |
48 // Get all of the devices attached to the system, inserting them into | 44 // Get all of the devices attached to the system, inserting them into |
49 // |devices|. Clears |devices| before use. The result will be sorted by id | 45 // |devices|. Clears |devices| before use. The result will be sorted by id |
50 // in increasing order. Must be called on FILE thread. | 46 // in increasing order. Must be called on FILE thread. |
51 void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices); | 47 void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices); |
52 | 48 |
53 private: | 49 private: |
54 friend struct DefaultSingletonTraits<UsbService>; | 50 friend struct DefaultSingletonTraits<UsbService>; |
55 friend class base::DeleteHelper<UsbService>; | 51 friend class base::DeleteHelper<UsbService>; |
56 | 52 |
57 UsbService(); | 53 UsbService(); |
58 virtual ~UsbService(); | 54 virtual ~UsbService(); |
59 | 55 |
60 // Return true if |device|'s vendor and product identifiers match |vendor_id| | 56 // Return true if |device|'s vendor and product identifiers match |vendor_id| |
61 // and |product_id|. | 57 // and |product_id|. |
62 static bool DeviceMatches(scoped_refptr<UsbDevice> device, | 58 static bool DeviceMatches(scoped_refptr<UsbDevice> device, |
63 const uint16 vendor_id, | 59 const uint16 vendor_id, |
64 const uint16 product_id); | 60 const uint16 product_id); |
65 | 61 |
66 // This method is called when permission broker replied our request. | |
67 // We will simply relay it to FILE thread. | |
68 void OnRequestUsbAccessReplied( | |
69 const uint16 vendor_id, | |
70 const uint16 product_id, | |
71 const base::Callback<void(ScopedDeviceVector vector)>& callback, | |
72 bool success); | |
73 | |
74 // FindDevicesImpl is called by FindDevices on ChromeOS after the permission | |
75 // broker has signaled that permission has been granted to access the | |
76 // underlying device nodes. On other platforms, it is called directly by | |
77 // FindDevices. | |
78 void FindDevicesImpl( | |
79 const uint16 vendor_id, | |
80 const uint16 product_id, | |
81 const base::Callback<void(ScopedDeviceVector vector)>& callback, | |
82 bool success); | |
83 | |
84 // Enumerate USB devices from OS and Update devices_ map. | 62 // Enumerate USB devices from OS and Update devices_ map. |
85 void RefreshDevices(); | 63 void RefreshDevices(); |
86 | 64 |
87 scoped_refptr<UsbContext> context_; | 65 scoped_refptr<UsbContext> context_; |
88 | 66 |
| 67 uint32 next_unique_id_; |
| 68 |
89 // The map from PlatformUsbDevices to UsbDevices. | 69 // The map from PlatformUsbDevices to UsbDevices. |
90 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap; | 70 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap; |
91 DeviceMap devices_; | 71 DeviceMap devices_; |
92 | 72 |
93 DISALLOW_COPY_AND_ASSIGN(UsbService); | 73 DISALLOW_COPY_AND_ASSIGN(UsbService); |
94 }; | 74 }; |
95 | 75 |
96 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_ | 76 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_ |
OLD | NEW |