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_HID_HID_SERVICE_H_ | 5 #ifndef DEVICE_HID_HID_SERVICE_H_ |
6 #define DEVICE_HID_HID_SERVICE_H_ | 6 #define DEVICE_HID_HID_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
17 #include "device/hid/hid_device_info.h" | 17 #include "device/hid/hid_device_info.h" |
18 | 18 |
19 namespace device { | 19 namespace device { |
20 | 20 |
21 class HidConnection; | 21 class HidConnection; |
22 | 22 |
23 // The HidService keeps track of human interface devices connected to the | 23 // The HidService keeps track of human interface devices connected to the |
24 // system. Call HidService::GetInstance to get the singleton instance. | 24 // system. Call HidService::GetInstance to get the singleton instance. |
25 class HidService { | 25 class HidService { |
26 public: | 26 public: |
27 class Observer { | 27 class Observer { |
28 public: | 28 public: |
29 virtual void OnDeviceAdded(const HidDeviceInfo& info) {} | 29 virtual void OnDeviceAdded(scoped_refptr<HidDeviceInfo> info); |
30 virtual void OnDeviceRemoved(const HidDeviceInfo& info) {} | 30 virtual void OnDeviceRemoved(scoped_refptr<HidDeviceInfo> info); |
31 }; | 31 }; |
32 | 32 |
33 typedef base::Callback<void(const std::vector<HidDeviceInfo>&)> | 33 typedef base::Callback<void(const std::vector<scoped_refptr<HidDeviceInfo>>&)> |
34 GetDevicesCallback; | 34 GetDevicesCallback; |
35 typedef base::Callback<void(scoped_refptr<HidConnection> connection)> | 35 typedef base::Callback<void(scoped_refptr<HidConnection> connection)> |
36 ConnectCallback; | 36 ConnectCallback; |
37 | 37 |
38 // Gets a pointer to the HidService singleton. This function should be called | 38 // Gets a pointer to the HidService singleton. This function should be called |
39 // on a thread with a MessageLoopForUI and be passed the task runner for a | 39 // on a thread with a MessageLoopForUI and be passed the task runner for a |
40 // thread with a MessageLoopForIO. | 40 // thread with a MessageLoopForIO. |
41 static HidService* GetInstance( | 41 static HidService* GetInstance( |
42 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | 42 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); |
43 | 43 |
44 static void SetInstanceForTest(HidService* instance); | 44 static void SetInstanceForTest(HidService* instance); |
45 | 45 |
46 // Enumerates available devices. The provided callback will always be posted | 46 // Enumerates available devices. The provided callback will always be posted |
47 // to the calling thread's task runner. | 47 // to the calling thread's task runner. |
48 virtual void GetDevices(const GetDevicesCallback& callback); | 48 virtual void GetDevices(const GetDevicesCallback& callback); |
49 | 49 |
50 void AddObserver(Observer* observer); | 50 void AddObserver(Observer* observer); |
51 void RemoveObserver(Observer* observer); | 51 void RemoveObserver(Observer* observer); |
52 | 52 |
53 // Fills in a DeviceInfo struct with info for the given device_id. | 53 // Fills in a DeviceInfo struct with info for the given device_id. |
54 // Returns |true| if successful or |false| if |device_id| is invalid. | 54 // Returns |nullptr| if |device_id| is invalid. |
55 bool GetDeviceInfo(const HidDeviceId& device_id, HidDeviceInfo* info) const; | 55 scoped_refptr<HidDeviceInfo> GetDeviceInfo( |
| 56 const HidDeviceId& device_id) const; |
56 | 57 |
57 // Opens a connection to a device. The callback will be run with null on | 58 // Opens a connection to a device. The callback will be run with null on |
58 // failure. | 59 // failure. |
59 virtual void Connect(const HidDeviceId& device_id, | 60 virtual void Connect(const HidDeviceId& device_id, |
60 const ConnectCallback& callback) = 0; | 61 const ConnectCallback& callback) = 0; |
61 | 62 |
62 protected: | 63 protected: |
63 friend void base::DeletePointer<HidService>(HidService* service); | 64 friend void base::DeletePointer<HidService>(HidService* service); |
64 friend class HidConnectionTest; | 65 friend class HidConnectionTest; |
65 | 66 |
66 typedef std::map<HidDeviceId, HidDeviceInfo> DeviceMap; | 67 typedef std::map<HidDeviceId, scoped_refptr<HidDeviceInfo>> DeviceMap; |
67 | 68 |
68 HidService(); | 69 HidService(); |
69 virtual ~HidService(); | 70 virtual ~HidService(); |
70 | 71 |
71 void AddDevice(const HidDeviceInfo& info); | 72 void AddDevice(scoped_refptr<HidDeviceInfo> info); |
72 void RemoveDevice(const HidDeviceId& device_id); | 73 void RemoveDevice(const HidDeviceId& device_id); |
73 void FirstEnumerationComplete(); | 74 void FirstEnumerationComplete(); |
74 | 75 |
75 const DeviceMap& devices() const { return devices_; } | 76 const DeviceMap& devices() const { return devices_; } |
76 | 77 |
77 base::ThreadChecker thread_checker_; | 78 base::ThreadChecker thread_checker_; |
78 | 79 |
79 private: | 80 private: |
80 DeviceMap devices_; | 81 DeviceMap devices_; |
81 bool enumeration_ready_; | 82 bool enumeration_ready_; |
82 std::vector<GetDevicesCallback> pending_enumerations_; | 83 std::vector<GetDevicesCallback> pending_enumerations_; |
83 ObserverList<Observer> observer_list_; | 84 ObserverList<Observer> observer_list_; |
84 | 85 |
85 DISALLOW_COPY_AND_ASSIGN(HidService); | 86 DISALLOW_COPY_AND_ASSIGN(HidService); |
86 }; | 87 }; |
87 | 88 |
88 } // namespace device | 89 } // namespace device |
89 | 90 |
90 #endif // DEVICE_HID_HID_SERVICE_H_ | 91 #endif // DEVICE_HID_HID_SERVICE_H_ |
OLD | NEW |