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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// 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_linux.h"
#include <fcntl.h>
#include <limits>
#include <string>
#include "base/bind.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/scoped_observer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread_restrictions.h"
#include "components/device_event_log/device_event_log.h"
#include "device/hid/device_monitor_linux.h"
#include "device/hid/hid_connection_linux.h"
#include "device/hid/hid_device_info_linux.h"
#include "device/udev_linux/scoped_udev.h"
#include "net/base/net_util.h"
#if defined(OS_CHROMEOS)
#include "base/sys_info.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/permission_broker_client.h"
#endif // defined(OS_CHROMEOS)
namespace device {
namespace {
const char kHidrawSubsystem[] = "hidraw";
const char kHIDID[] = "HID_ID";
const char kHIDName[] = "HID_NAME";
const char kHIDUnique[] = "HID_UNIQ";
const char kSysfsReportDescriptorKey[] = "report_descriptor";
} // namespace
struct HidServiceLinux::ConnectParams {
ConnectParams(scoped_refptr<HidDeviceInfoLinux> device_info,
const ConnectCallback& callback,
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner)
: device_info(device_info),
callback(callback),
task_runner(task_runner),
file_task_runner(file_task_runner) {}
~ConnectParams() {}
scoped_refptr<HidDeviceInfoLinux> device_info;
ConnectCallback callback;
scoped_refptr<base::SingleThreadTaskRunner> task_runner;
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner;
base::File device_file;
};
class HidServiceLinux::FileThreadHelper
: public DeviceMonitorLinux::Observer,
public base::MessageLoop::DestructionObserver {
public:
FileThreadHelper(base::WeakPtr<HidServiceLinux> service,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: observer_(this), service_(service), task_runner_(task_runner) {
DeviceMonitorLinux* monitor = DeviceMonitorLinux::GetInstance();
observer_.Add(monitor);
monitor->Enumerate(
base::Bind(&FileThreadHelper::OnDeviceAdded, base::Unretained(this)));
task_runner->PostTask(
FROM_HERE,
base::Bind(&HidServiceLinux::FirstEnumerationComplete, service_));
}
~FileThreadHelper() override {
DCHECK(thread_checker_.CalledOnValidThread());
}
private:
// DeviceMonitorLinux::Observer:
void OnDeviceAdded(udev_device* device) override {
DCHECK(thread_checker_.CalledOnValidThread());
const char* device_path = udev_device_get_syspath(device);
if (!device_path) {
return;
}
HidDeviceId device_id = device_path;
const char* subsystem = udev_device_get_subsystem(device);
if (!subsystem || strcmp(subsystem, kHidrawSubsystem) != 0) {
return;
}
const char* str_property = udev_device_get_devnode(device);
if (!str_property) {
return;
}
std::string device_node = str_property;
udev_device* parent = udev_device_get_parent(device);
if (!parent) {
return;
}
const char* hid_id = udev_device_get_property_value(parent, kHIDID);
if (!hid_id) {
return;
}
std::vector<std::string> parts = base::SplitString(
hid_id, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (parts.size() != 3) {
return;
}
uint32_t int_property = 0;
if (!HexStringToUInt(base::StringPiece(parts[1]), &int_property) ||
int_property > std::numeric_limits<uint16_t>::max()) {
return;
}
uint16_t vendor_id = int_property;
if (!HexStringToUInt(base::StringPiece(parts[2]), &int_property) ||
int_property > std::numeric_limits<uint16_t>::max()) {
return;
}
uint16_t product_id = int_property;
std::string serial_number;
str_property = udev_device_get_property_value(parent, kHIDUnique);
if (str_property != NULL) {
serial_number = str_property;
}
std::string product_name;
str_property = udev_device_get_property_value(parent, kHIDName);
if (str_property != NULL) {
product_name = str_property;
}
const char* parent_sysfs_path = udev_device_get_syspath(parent);
if (!parent_sysfs_path) {
return;
}
base::FilePath report_descriptor_path =
base::FilePath(parent_sysfs_path).Append(kSysfsReportDescriptorKey);
std::string report_descriptor_str;
if (!base::ReadFileToString(report_descriptor_path,
&report_descriptor_str)) {
return;
}
scoped_refptr<HidDeviceInfo> device_info(new HidDeviceInfoLinux(
device_id, device_node, vendor_id, product_id, product_name,
serial_number,
kHIDBusTypeUSB, // TODO(reillyg): Detect Bluetooth. crbug.com/443335
std::vector<uint8>(report_descriptor_str.begin(),
report_descriptor_str.end())));
task_runner_->PostTask(FROM_HERE, base::Bind(&HidServiceLinux::AddDevice,
service_, device_info));
}
void OnDeviceRemoved(udev_device* device) override {
DCHECK(thread_checker_.CalledOnValidThread());
const char* device_path = udev_device_get_syspath(device);
if (device_path) {
task_runner_->PostTask(
FROM_HERE, base::Bind(&HidServiceLinux::RemoveDevice, service_,
std::string(device_path)));
}
}
// base::MessageLoop::DestructionObserver:
void WillDestroyCurrentMessageLoop() override {
DCHECK(thread_checker_.CalledOnValidThread());
base::MessageLoop::current()->RemoveDestructionObserver(this);
delete this;
}
base::ThreadChecker thread_checker_;
ScopedObserver<DeviceMonitorLinux, DeviceMonitorLinux::Observer> observer_;
// This weak pointer is only valid when checked on this task runner.
base::WeakPtr<HidServiceLinux> service_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
};
HidServiceLinux::HidServiceLinux(
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner)
: file_task_runner_(file_task_runner), weak_factory_(this) {
task_runner_ = base::ThreadTaskRunnerHandle::Get();
// The device watcher is passed a weak pointer back to this service so that it
// can be cleaned up after the service is destroyed however this weak pointer
// must be constructed on the this thread where it will be checked.
file_task_runner_->PostTask(
FROM_HERE, base::Bind(&HidServiceLinux::StartHelper,
weak_factory_.GetWeakPtr(), task_runner_));
}
// static
void HidServiceLinux::StartHelper(
base::WeakPtr<HidServiceLinux> weak_ptr,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
// Helper is a message loop destruction observer and will delete itself when
// this thread's message loop is destroyed.
new FileThreadHelper(weak_ptr, task_runner);
}
void HidServiceLinux::Connect(const HidDeviceId& device_id,
const ConnectCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
const auto& map_entry = devices().find(device_id);
if (map_entry == devices().end()) {
task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
return;
}
scoped_refptr<HidDeviceInfoLinux> device_info =
static_cast<HidDeviceInfoLinux*>(map_entry->second.get());
scoped_ptr<ConnectParams> params(new ConnectParams(
device_info, callback, task_runner_, file_task_runner_));
#if defined(OS_CHROMEOS)
chromeos::PermissionBrokerClient* client =
chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
DCHECK(client) << "Could not get permission broker client.";
client->OpenPath(
device_info->device_node(),
base::Bind(&HidServiceLinux::OnPathOpened, base::Passed(¶ms)));
#else
file_task_runner_->PostTask(FROM_HERE,
base::Bind(&HidServiceLinux::OpenOnBlockingThread,
base::Passed(¶ms)));
#endif // defined(OS_CHROMEOS)
}
HidServiceLinux::~HidServiceLinux() {
file_task_runner_->DeleteSoon(FROM_HERE, helper_.release());
}
#if defined(OS_CHROMEOS)
// static
void HidServiceLinux::OnPathOpened(scoped_ptr<ConnectParams> params,
dbus::FileDescriptor fd) {
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner =
params->file_task_runner;
file_task_runner->PostTask(
FROM_HERE, base::Bind(&HidServiceLinux::ValidateFdOnBlockingThread,
base::Passed(¶ms), base::Passed(&fd)));
}
// static
void HidServiceLinux::ValidateFdOnBlockingThread(
scoped_ptr<ConnectParams> params,
dbus::FileDescriptor fd) {
base::ThreadRestrictions::AssertIOAllowed();
fd.CheckValidity();
if (fd.is_valid()) {
params->device_file = base::File(fd.TakeValue());
FinishOpen(params.Pass());
} else {
HID_LOG(EVENT) << "Permission broker denied access to '"
<< params->device_info->device_node() << "'.";
params->task_runner->PostTask(FROM_HERE,
base::Bind(params->callback, nullptr));
}
}
#else
// static
void HidServiceLinux::OpenOnBlockingThread(scoped_ptr<ConnectParams> params) {
base::ThreadRestrictions::AssertIOAllowed();
scoped_refptr<base::SingleThreadTaskRunner> task_runner = params->task_runner;
base::FilePath device_path(params->device_info->device_node());
base::File& device_file = params->device_file;
int flags =
base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE;
device_file.Initialize(device_path, flags);
if (!device_file.IsValid()) {
base::File::Error file_error = device_file.error_details();
if (file_error == base::File::FILE_ERROR_ACCESS_DENIED) {
HID_LOG(EVENT)
<< "Access denied opening device read-write, trying read-only.";
flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
device_file.Initialize(device_path, flags);
}
}
if (!device_file.IsValid()) {
HID_LOG(EVENT) << "Failed to open '" << params->device_info->device_node()
<< "': "
<< base::File::ErrorToString(device_file.error_details());
task_runner->PostTask(FROM_HERE, base::Bind(params->callback, nullptr));
return;
}
FinishOpen(params.Pass());
}
#endif // defined(OS_CHROMEOS)
// static
void HidServiceLinux::FinishOpen(scoped_ptr<ConnectParams> params) {
base::ThreadRestrictions::AssertIOAllowed();
scoped_refptr<base::SingleThreadTaskRunner> task_runner = params->task_runner;
int result = net::SetNonBlocking(params->device_file.GetPlatformFile());
if (result == -1) {
HID_PLOG(ERROR) << "Failed to set the non-blocking flag on the device fd";
task_runner->PostTask(FROM_HERE, base::Bind(params->callback, nullptr));
return;
}
task_runner->PostTask(
FROM_HERE,
base::Bind(&HidServiceLinux::CreateConnection, base::Passed(¶ms)));
}
// static
void HidServiceLinux::CreateConnection(scoped_ptr<ConnectParams> params) {
DCHECK(params->device_file.IsValid());
params->callback.Run(make_scoped_refptr(
new HidConnectionLinux(params->device_info, params->device_file.Pass(),
params->file_task_runner)));
}
} // namespace device
|