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" | 13 #include "base/callback.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 template <class T> class DeleteHelper; | 19 template <class T> class DeleteHelper; |
20 | 20 |
21 } // namespace base | 21 } // namespace base |
22 | 22 |
| 23 struct InitUsbContextTraits; |
23 template <typename T> struct DefaultSingletonTraits; | 24 template <typename T> struct DefaultSingletonTraits; |
24 | 25 |
25 typedef struct libusb_device* PlatformUsbDevice; | 26 typedef struct libusb_device* PlatformUsbDevice; |
| 27 typedef struct libusb_context* PlatformUsbContext; |
26 | 28 |
27 class UsbContext; | 29 class UsbContext; |
28 class UsbDevice; | 30 class UsbDevice; |
29 | 31 |
30 // The USB service handles creating and managing an event handler thread that is | 32 // The USB service handles creating and managing an event handler thread that is |
31 // used to manage and dispatch USB events. It is also responsible for device | 33 // used to manage and dispatch USB events. It is also responsible for device |
32 // discovery on the system, which allows it to re-use device handles to prevent | 34 // discovery on the system, which allows it to re-use device handles to prevent |
33 // competition for the same USB device. | 35 // competition for the same USB device. |
34 class UsbService { | 36 class UsbService { |
35 public: | 37 public: |
36 typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > | 38 typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > |
37 ScopedDeviceVector; | 39 ScopedDeviceVector; |
38 | 40 |
39 // Must be called on FILE thread. | 41 // Must be called on FILE thread. |
| 42 // Returns NULL when failed to initialized. |
40 static UsbService* GetInstance(); | 43 static UsbService* GetInstance(); |
41 | 44 |
42 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id); | 45 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id); |
43 | 46 |
44 // Get all of the devices attached to the system, inserting them into | 47 // Get all of the devices attached to the system, inserting them into |
45 // |devices|. Clears |devices| before use. The result will be sorted by id | 48 // |devices|. Clears |devices| before use. The result will be sorted by id |
46 // in increasing order. Must be called on FILE thread. | 49 // in increasing order. Must be called on FILE thread. |
47 void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices); | 50 void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices); |
48 | 51 |
49 private: | 52 private: |
| 53 friend struct InitUsbContextTraits; |
50 friend struct DefaultSingletonTraits<UsbService>; | 54 friend struct DefaultSingletonTraits<UsbService>; |
51 friend class base::DeleteHelper<UsbService>; | 55 friend class base::DeleteHelper<UsbService>; |
52 | 56 |
53 UsbService(); | 57 explicit UsbService(PlatformUsbContext context); |
54 virtual ~UsbService(); | 58 virtual ~UsbService(); |
55 | 59 |
56 // Return true if |device|'s vendor and product identifiers match |vendor_id| | 60 // Return true if |device|'s vendor and product identifiers match |vendor_id| |
57 // and |product_id|. | 61 // and |product_id|. |
58 static bool DeviceMatches(scoped_refptr<UsbDevice> device, | 62 static bool DeviceMatches(scoped_refptr<UsbDevice> device, |
59 const uint16 vendor_id, | 63 const uint16 vendor_id, |
60 const uint16 product_id); | 64 const uint16 product_id); |
61 | 65 |
62 // Enumerate USB devices from OS and Update devices_ map. | 66 // Enumerate USB devices from OS and Update devices_ map. |
63 void RefreshDevices(); | 67 void RefreshDevices(); |
64 | 68 |
65 scoped_refptr<UsbContext> context_; | 69 scoped_refptr<UsbContext> context_; |
66 | 70 |
67 // TODO(ikarienator): Figure out a better solution. | 71 // TODO(ikarienator): Figure out a better solution. |
68 uint32 next_unique_id_; | 72 uint32 next_unique_id_; |
69 | 73 |
70 // The map from PlatformUsbDevices to UsbDevices. | 74 // The map from PlatformUsbDevices to UsbDevices. |
71 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap; | 75 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap; |
72 DeviceMap devices_; | 76 DeviceMap devices_; |
73 | 77 |
74 DISALLOW_COPY_AND_ASSIGN(UsbService); | 78 DISALLOW_COPY_AND_ASSIGN(UsbService); |
75 }; | 79 }; |
76 | 80 |
77 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_ | 81 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_ |
OLD | NEW |