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 COMPONENTS_USB_SERVICE_USB_SERVICE_H_ | 5 #ifndef COMPONENTS_USB_SERVICE_USB_SERVICE_H_ |
6 #define COMPONENTS_USB_SERVICE_USB_SERVICE_H_ | 6 #define COMPONENTS_USB_SERVICE_USB_SERVICE_H_ |
7 | 7 |
8 #include <map> | |
9 #include <utility> | |
10 #include <vector> | 8 #include <vector> |
11 | 9 |
12 #include "base/basictypes.h" | |
13 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
17 #include "components/usb_service/usb_service_export.h" | 13 #include "components/usb_service/usb_service_export.h" |
18 | 14 |
19 struct libusb_device; | |
20 struct libusb_context; | |
21 | |
22 namespace usb_service { | 15 namespace usb_service { |
23 | 16 |
24 typedef struct libusb_device* PlatformUsbDevice; | |
25 typedef struct libusb_context* PlatformUsbContext; | |
26 | |
27 class UsbContext; | |
28 class UsbDevice; | 17 class UsbDevice; |
29 | 18 |
30 // The USB service handles creating and managing an event handler thread that is | 19 // 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 | 20 // 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 | 21 // discovery on the system, which allows it to re-use device handles to prevent |
33 // competition for the same USB device. | 22 // competition for the same USB device. |
34 class USB_SERVICE_EXPORT UsbService | 23 class USB_SERVICE_EXPORT UsbService : public base::NonThreadSafe { |
35 : public base::MessageLoop::DestructionObserver, | |
36 public base::NonThreadSafe { | |
37 public: | 24 public: |
38 typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > | |
39 ScopedDeviceVector; | |
40 | |
41 // Must be called on FILE thread. | 25 // Must be called on FILE thread. |
42 // Returns NULL when failed to initialized. | 26 // Returns NULL when failed to initialized. |
43 static UsbService* GetInstance(); | 27 static UsbService* GetInstance(); |
44 | 28 |
45 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id); | 29 static void SetInstanceForTest(UsbService* instance); |
| 30 |
| 31 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) = 0; |
46 | 32 |
47 // Get all of the devices attached to the system, inserting them into | 33 // Get all of the devices attached to the system, inserting them into |
48 // |devices|. Clears |devices| before use. The result will be sorted by id | 34 // |devices|. Clears |devices| before use. The result will be sorted by id |
49 // in increasing order. Must be called on FILE thread. | 35 // in increasing order. Must be called on FILE thread. |
50 void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices); | 36 virtual void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) = 0; |
51 | 37 |
52 // base::MessageLoop::DestructionObserver implementation. | 38 protected: |
53 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | |
54 | |
55 private: | |
56 friend struct base::DefaultDeleter<UsbService>; | 39 friend struct base::DefaultDeleter<UsbService>; |
57 | 40 UsbService() {} |
58 explicit UsbService(PlatformUsbContext context); | 41 virtual ~UsbService() {} |
59 virtual ~UsbService(); | |
60 | |
61 // Enumerate USB devices from OS and Update devices_ map. | |
62 void RefreshDevices(); | |
63 | |
64 scoped_refptr<UsbContext> context_; | |
65 | |
66 // TODO(ikarienator): Figure out a better solution. | |
67 uint32 next_unique_id_; | |
68 | |
69 // The map from PlatformUsbDevices to UsbDevices. | |
70 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap; | |
71 DeviceMap devices_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(UsbService); | 42 DISALLOW_COPY_AND_ASSIGN(UsbService); |
74 }; | 43 }; |
75 | 44 |
76 } // namespace usb_service | 45 } // namespace usb_service |
77 | 46 |
78 #endif // COMPONENTS_USB_SERVICE_USB_SERVICE_H_ | 47 #endif // COMPONENTS_USB_SERVICE_USB_SERVICE_H_ |
OLD | NEW |