1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/hid/hid_service.h"
#include "base/at_exit.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "components/device_event_log/device_event_log.h"
#if defined(OS_LINUX) && defined(USE_UDEV)
#include "device/hid/hid_service_linux.h"
#elif defined(OS_MACOSX)
#include "device/hid/hid_service_mac.h"
#elif defined(OS_WIN)
#include "device/hid/hid_service_win.h"
#endif
namespace device {
namespace {
HidService* g_service;
}
void HidService::Observer::OnDeviceAdded(
scoped_refptr<HidDeviceInfo> device_info) {
}
void HidService::Observer::OnDeviceRemoved(
scoped_refptr<HidDeviceInfo> device_info) {
}
void HidService::Observer::OnDeviceRemovedCleanup(
scoped_refptr<HidDeviceInfo> device_info) {
}
HidService* HidService::GetInstance(
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) {
if (g_service == NULL) {
#if defined(OS_LINUX) && defined(USE_UDEV)
g_service = new HidServiceLinux(file_task_runner);
#elif defined(OS_MACOSX)
g_service = new HidServiceMac(file_task_runner);
#elif defined(OS_WIN)
g_service = new HidServiceWin(file_task_runner);
#endif
if (g_service != nullptr) {
base::AtExitManager::RegisterTask(base::Bind(
&base::DeletePointer<HidService>, base::Unretained(g_service)));
}
}
return g_service;
}
void HidService::SetInstanceForTest(HidService* instance) {
DCHECK(!g_service);
g_service = instance;
base::AtExitManager::RegisterTask(base::Bind(&base::DeletePointer<HidService>,
base::Unretained(g_service)));
}
void HidService::GetDevices(const GetDevicesCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (enumeration_ready_) {
std::vector<scoped_refptr<HidDeviceInfo>> devices;
for (const auto& map_entry : devices_) {
devices.push_back(map_entry.second);
}
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, devices));
} else {
pending_enumerations_.push_back(callback);
}
}
void HidService::AddObserver(HidService::Observer* observer) {
observer_list_.AddObserver(observer);
}
void HidService::RemoveObserver(HidService::Observer* observer) {
observer_list_.RemoveObserver(observer);
}
// Fills in the device info struct of the given device_id.
scoped_refptr<HidDeviceInfo> HidService::GetDeviceInfo(
const HidDeviceId& device_id) const {
DCHECK(thread_checker_.CalledOnValidThread());
DeviceMap::const_iterator it = devices_.find(device_id);
if (it == devices_.end()) {
return nullptr;
}
return it->second;
}
HidService::HidService() : enumeration_ready_(false) {
}
HidService::~HidService() {
DCHECK(thread_checker_.CalledOnValidThread());
}
void HidService::AddDevice(scoped_refptr<HidDeviceInfo> device_info) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!ContainsKey(devices_, device_info->device_id())) {
devices_[device_info->device_id()] = device_info;
HID_LOG(USER) << "HID device "
<< (enumeration_ready_ ? "added" : "detected")
<< ": vendorId=" << device_info->vendor_id()
<< ", productId=" << device_info->product_id() << ", name='"
<< device_info->product_name() << "', serial='"
<< device_info->serial_number() << "', deviceId='"
<< device_info->device_id() << "'";
if (enumeration_ready_) {
FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device_info));
}
}
}
void HidService::RemoveDevice(const HidDeviceId& device_id) {
DCHECK(thread_checker_.CalledOnValidThread());
DeviceMap::iterator it = devices_.find(device_id);
if (it != devices_.end()) {
HID_LOG(USER) << "HID device removed: deviceId='" << device_id << "'";
scoped_refptr<HidDeviceInfo> device = it->second;
if (enumeration_ready_) {
FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device));
}
devices_.erase(it);
if (enumeration_ready_) {
FOR_EACH_OBSERVER(Observer, observer_list_,
OnDeviceRemovedCleanup(device));
}
}
}
void HidService::FirstEnumerationComplete() {
enumeration_ready_ = true;
if (!pending_enumerations_.empty()) {
std::vector<scoped_refptr<HidDeviceInfo>> devices;
for (const auto& map_entry : devices_) {
devices.push_back(map_entry.second);
}
for (const GetDevicesCallback& callback : pending_enumerations_) {
callback.Run(devices);
}
pending_enumerations_.clear();
}
}
} // namespace device
|