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/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/observer_list.h" |
12 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 class SingleThreadTaskRunner; | 16 class SingleThreadTaskRunner; |
16 } | 17 } |
17 | 18 |
18 namespace device { | 19 namespace device { |
19 | 20 |
20 class UsbDevice; | 21 class UsbDevice; |
21 | 22 |
22 // The USB service handles creating and managing an event handler thread that is | 23 // The USB service handles creating and managing an event handler thread that is |
23 // used to manage and dispatch USB events. It is also responsible for device | 24 // used to manage and dispatch USB events. It is also responsible for device |
24 // discovery on the system, which allows it to re-use device handles to prevent | 25 // discovery on the system, which allows it to re-use device handles to prevent |
25 // competition for the same USB device. | 26 // competition for the same USB device. |
26 class UsbService : public base::NonThreadSafe { | 27 class UsbService : public base::NonThreadSafe { |
27 public: | 28 public: |
| 29 class Observer { |
| 30 public: |
| 31 virtual void OnDeviceAdded(scoped_refptr<UsbDevice> device); |
| 32 virtual void OnDeviceRemoved(scoped_refptr<UsbDevice> device); |
| 33 }; |
| 34 |
28 // Must be called on a thread with a MessageLoopForIO (for example | 35 // Must be called on a thread with a MessageLoopForIO (for example |
29 // BrowserThread::FILE). The UI task runner reference is used to talk to the | 36 // BrowserThread::FILE). The UI task runner reference is used to talk to the |
30 // PermissionBrokerClient on ChromeOS (UI thread). Returns NULL when | 37 // PermissionBrokerClient on ChromeOS (UI thread). Returns NULL when |
31 // initialization fails. | 38 // initialization fails. |
32 static UsbService* GetInstance( | 39 static UsbService* GetInstance( |
33 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); | 40 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); |
34 | 41 |
35 static void SetInstanceForTest(UsbService* instance); | 42 static void SetInstanceForTest(UsbService* instance); |
36 | 43 |
37 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) = 0; | 44 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) = 0; |
38 | 45 |
39 // Get all of the devices attached to the system, inserting them into | 46 // Get all of the devices attached to the system, inserting them into |
40 // |devices|. Clears |devices| before use. The result will be sorted by id | 47 // |devices|. Clears |devices| before use. The result will be sorted by id |
41 // in increasing order. Must be called on FILE thread. | 48 // in increasing order. Must be called on FILE thread. |
42 virtual void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) = 0; | 49 virtual void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) = 0; |
43 | 50 |
| 51 void AddObserver(Observer* observer); |
| 52 void RemoveObserver(Observer* observer); |
| 53 |
44 protected: | 54 protected: |
45 friend struct base::DefaultDeleter<UsbService>; | 55 friend struct base::DefaultDeleter<UsbService>; |
46 UsbService() {} | 56 |
47 virtual ~UsbService() {} | 57 UsbService(); |
| 58 virtual ~UsbService(); |
| 59 |
| 60 void NotifyDeviceAdded(scoped_refptr<UsbDevice> device); |
| 61 void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device); |
| 62 |
| 63 ObserverList<Observer, true> observer_list_; |
| 64 |
48 DISALLOW_COPY_AND_ASSIGN(UsbService); | 65 DISALLOW_COPY_AND_ASSIGN(UsbService); |
49 }; | 66 }; |
50 | 67 |
51 } // namespace device | 68 } // namespace device |
52 | 69 |
53 #endif // DEVICE_USB_USB_SERVICE_H_ | 70 #endif // DEVICE_USB_USB_SERVICE_H_ |
OLD | NEW |