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_SERVICE_H_ | 5 #ifndef DEVICE_USB_USB_SERVICE_H_ |
6 #define DEVICE_USB_USB_SERVICE_H_ | 6 #define DEVICE_USB_USB_SERVICE_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/bind_helpers.h" |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
13 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class SingleThreadTaskRunner; | 17 class SequencedTaskRunner; |
17 } | 18 } |
18 | 19 |
19 namespace device { | 20 namespace device { |
20 | 21 |
21 class UsbDevice; | 22 class UsbDevice; |
22 | 23 |
23 // The USB service handles creating and managing an event handler thread that is | 24 // The USB service handles creating and managing an event handler thread that is |
24 // used to manage and dispatch USB events. It is also responsible for device | 25 // used to manage and dispatch USB events. It is also responsible for device |
25 // discovery on the system, which allows it to re-use device handles to prevent | 26 // discovery on the system, which allows it to re-use device handles to prevent |
26 // competition for the same USB device. | 27 // competition for the same USB device. |
27 // | |
28 // All functions on this object must be called from a thread with a | |
29 // MessageLoopForIO (for example, BrowserThread::FILE). | |
30 class UsbService : public base::NonThreadSafe { | 28 class UsbService : public base::NonThreadSafe { |
31 public: | 29 public: |
| 30 using GetDevicesCallback = |
| 31 base::Callback<void(const std::vector<scoped_refptr<UsbDevice>>&)>; |
| 32 |
32 class Observer { | 33 class Observer { |
33 public: | 34 public: |
34 // These events are delivered from the thread on which the UsbService object | 35 // These events are delivered from the thread on which the UsbService object |
35 // was created. | 36 // was created. |
36 virtual void OnDeviceAdded(scoped_refptr<UsbDevice> device); | 37 virtual void OnDeviceAdded(scoped_refptr<UsbDevice> device); |
37 virtual void OnDeviceRemoved(scoped_refptr<UsbDevice> device); | 38 virtual void OnDeviceRemoved(scoped_refptr<UsbDevice> device); |
38 // For observers that need to process device removal after others have run. | 39 // For observers that need to process device removal after others have run. |
39 // Should not depend on any other service's knowledge of connected devices. | 40 // Should not depend on any other service's knowledge of connected devices. |
40 virtual void OnDeviceRemovedCleanup(scoped_refptr<UsbDevice> device); | 41 virtual void OnDeviceRemovedCleanup(scoped_refptr<UsbDevice> device); |
41 }; | 42 }; |
42 | 43 |
43 // The UI task runner reference is used to talk to the PermissionBrokerClient | 44 // The file task runner reference is used for blocking I/O operations. |
44 // on ChromeOS (UI thread). Returns NULL when initialization fails. | 45 // Returns NULL when initialization fails. |
45 static UsbService* GetInstance( | 46 static UsbService* GetInstance( |
46 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); | 47 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
47 | |
48 static void SetInstanceForTest(UsbService* instance); | |
49 | 48 |
50 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) = 0; | 49 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) = 0; |
51 | 50 |
52 // Get all of the devices attached to the system, inserting them into | 51 // Enumerates available devices. |
53 // |devices|. Clears |devices| before use. The result will be sorted by id | 52 virtual void GetDevices(const GetDevicesCallback& callback) = 0; |
54 // in increasing order. | |
55 virtual void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) = 0; | |
56 | 53 |
57 void AddObserver(Observer* observer); | 54 void AddObserver(Observer* observer); |
58 void RemoveObserver(Observer* observer); | 55 void RemoveObserver(Observer* observer); |
59 | 56 |
60 protected: | 57 protected: |
61 UsbService(); | 58 UsbService(); |
62 virtual ~UsbService(); | 59 virtual ~UsbService(); |
63 | 60 |
64 void NotifyDeviceAdded(scoped_refptr<UsbDevice> device); | 61 void NotifyDeviceAdded(scoped_refptr<UsbDevice> device); |
65 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device); | 62 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device); |
66 | 63 |
67 ObserverList<Observer, true> observer_list_; | 64 ObserverList<Observer, true> observer_list_; |
68 | 65 |
69 private: | 66 private: |
70 class Destroyer; | 67 friend void base::DeletePointer<UsbService>(UsbService* service); |
71 | 68 |
72 DISALLOW_COPY_AND_ASSIGN(UsbService); | 69 DISALLOW_COPY_AND_ASSIGN(UsbService); |
73 }; | 70 }; |
74 | 71 |
75 } // namespace device | 72 } // namespace device |
76 | 73 |
77 #endif // DEVICE_USB_USB_SERVICE_H_ | 74 #endif // DEVICE_USB_USB_SERVICE_H_ |
OLD | NEW |