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 #include "device/hid/hid_service.h"
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/stl_util.h"
11 #include "base/threading/thread_restrictions.h"
13 #if defined(OS_LINUX) && defined(USE_UDEV)
14 #include "device/hid/hid_service_linux.h"
15 #elif defined(OS_MACOSX)
16 #include "device/hid/hid_service_mac.h"
18 #include "device/hid/hid_service_win.h"
23 HidService
* HidService::Create(
24 scoped_refptr
<base::MessageLoopProxy
> ui_message_loop
) {
25 #if defined(OS_LINUX) && defined(USE_UDEV)
26 return new HidServiceLinux(ui_message_loop
);
27 #elif defined(OS_MACOSX)
28 return new HidServiceMac();
30 return new HidServiceWin();
36 HidService::~HidService() {
37 DCHECK(thread_checker_
.CalledOnValidThread());
40 void HidService::GetDevices(std::vector
<HidDeviceInfo
>* devices
) {
41 DCHECK(thread_checker_
.CalledOnValidThread());
42 STLClearObject(devices
);
43 for (DeviceMap::iterator it
= devices_
.begin();
46 devices
->push_back(it
->second
);
50 // Fills in the device info struct of the given device_id.
51 bool HidService::GetDeviceInfo(const HidDeviceId
& device_id
,
52 HidDeviceInfo
* info
) const {
53 DeviceMap::const_iterator it
= devices_
.find(device_id
);
54 if (it
== devices_
.end())
60 HidService::HidService() {
61 base::ThreadRestrictions::AssertIOAllowed();
62 DCHECK(thread_checker_
.CalledOnValidThread());
65 void HidService::AddDevice(const HidDeviceInfo
& info
) {
66 DCHECK(thread_checker_
.CalledOnValidThread());
67 if (!ContainsKey(devices_
, info
.device_id
)) {
68 devices_
[info
.device_id
] = info
;
72 void HidService::RemoveDevice(const HidDeviceId
& device_id
) {
73 DCHECK(thread_checker_
.CalledOnValidThread());
74 DeviceMap::iterator it
= devices_
.find(device_id
);
75 if (it
!= devices_
.end())
79 const HidService::DeviceMap
& HidService::GetDevicesNoEnumerate() const {