Fix a typo in the MB mixin for official builds.
[chromium-blink-merge.git] / device / hid / hid_service.h
blobc2805c8111b0ba233db8fcb609e1540744460996
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
3 // found in the LICENSE file.
5 #ifndef DEVICE_HID_HID_SERVICE_H_
6 #define DEVICE_HID_HID_SERVICE_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/bind_helpers.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/observer_list.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/threading/thread_checker.h"
17 #include "device/hid/hid_device_info.h"
19 namespace device {
21 class HidConnection;
23 // The HidService keeps track of human interface devices connected to the
24 // system. Call HidService::GetInstance to get the singleton instance.
25 class HidService {
26 public:
27 class Observer {
28 public:
29 virtual void OnDeviceAdded(scoped_refptr<HidDeviceInfo> info);
30 // Notifies all observers that a device is being removed, called before
31 // removing the device from HidService. Observers should not depend on the
32 // order in which they are notified of the OnDeviceRemove event.
33 virtual void OnDeviceRemoved(scoped_refptr<HidDeviceInfo> info);
34 // Notifies all observers again, after having first notified all observers
35 // with OnDeviceRemoved and removed the device from internal structures.
36 // Each observer must not depend on any other observers' awareness of the
37 // device as they could be cleaned up in any order.
38 virtual void OnDeviceRemovedCleanup(scoped_refptr<HidDeviceInfo> info);
41 typedef base::Callback<void(const std::vector<scoped_refptr<HidDeviceInfo>>&)>
42 GetDevicesCallback;
43 typedef base::Callback<void(scoped_refptr<HidConnection> connection)>
44 ConnectCallback;
46 // This function should be called on a thread with a MessageLoopForUI and be
47 // passed the task runner for a thread with a MessageLoopForIO.
48 static scoped_ptr<HidService> Create(
49 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner);
51 virtual ~HidService();
53 // Enumerates available devices. The provided callback will always be posted
54 // to the calling thread's task runner.
55 virtual void GetDevices(const GetDevicesCallback& callback);
57 void AddObserver(Observer* observer);
58 void RemoveObserver(Observer* observer);
60 // Fills in a DeviceInfo struct with info for the given device_id.
61 // Returns |nullptr| if |device_id| is invalid.
62 scoped_refptr<HidDeviceInfo> GetDeviceInfo(
63 const HidDeviceId& device_id) const;
65 // Opens a connection to a device. The callback will be run with null on
66 // failure.
67 virtual void Connect(const HidDeviceId& device_id,
68 const ConnectCallback& callback) = 0;
70 protected:
71 friend class HidConnectionTest;
73 typedef std::map<HidDeviceId, scoped_refptr<HidDeviceInfo>> DeviceMap;
75 HidService();
77 void AddDevice(scoped_refptr<HidDeviceInfo> info);
78 void RemoveDevice(const HidDeviceId& device_id);
79 void FirstEnumerationComplete();
81 const DeviceMap& devices() const { return devices_; }
83 base::ThreadChecker thread_checker_;
85 private:
86 DeviceMap devices_;
87 bool enumeration_ready_;
88 std::vector<GetDevicesCallback> pending_enumerations_;
89 base::ObserverList<Observer, true> observer_list_;
91 DISALLOW_COPY_AND_ASSIGN(HidService);
94 } // namespace device
96 #endif // DEVICE_HID_HID_SERVICE_H_