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/message_loop/message_loop.h"
11 #include "base/stl_util.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"
25 HidService
* g_service
= NULL
;
29 class HidService::Destroyer
: public base::MessageLoop::DestructionObserver
{
31 explicit Destroyer(HidService
* hid_service
)
32 : hid_service_(hid_service
) {}
33 ~Destroyer() override
{}
36 // base::MessageLoop::DestructionObserver implementation.
37 void WillDestroyCurrentMessageLoop() override
{
38 base::MessageLoop::current()->RemoveDestructionObserver(this);
44 HidService
* hid_service_
;
47 HidService
* HidService::GetInstance(
48 scoped_refptr
<base::SingleThreadTaskRunner
> file_task_runner
,
49 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
) {
50 if (g_service
== NULL
) {
51 #if defined(OS_LINUX) && defined(USE_UDEV)
52 g_service
= new HidServiceLinux(ui_task_runner
);
53 #elif defined(OS_MACOSX)
54 g_service
= new HidServiceMac(file_task_runner
);
56 g_service
= new HidServiceWin();
58 if (g_service
!= NULL
) {
59 Destroyer
* destroyer
= new Destroyer(g_service
);
60 base::MessageLoop::current()->AddDestructionObserver(destroyer
);
66 HidService::~HidService() {
67 DCHECK(thread_checker_
.CalledOnValidThread());
70 void HidService::GetDevices(std::vector
<HidDeviceInfo
>* devices
) {
71 DCHECK(thread_checker_
.CalledOnValidThread());
72 STLClearObject(devices
);
73 for (DeviceMap::iterator it
= devices_
.begin();
76 devices
->push_back(it
->second
);
80 // Fills in the device info struct of the given device_id.
81 bool HidService::GetDeviceInfo(const HidDeviceId
& device_id
,
82 HidDeviceInfo
* info
) const {
83 DeviceMap::const_iterator it
= devices_
.find(device_id
);
84 if (it
== devices_
.end())
90 HidService::HidService() {
91 DCHECK(thread_checker_
.CalledOnValidThread());
94 void HidService::AddDevice(const HidDeviceInfo
& info
) {
95 DCHECK(thread_checker_
.CalledOnValidThread());
96 if (!ContainsKey(devices_
, info
.device_id
)) {
97 devices_
[info
.device_id
] = info
;
101 void HidService::RemoveDevice(const HidDeviceId
& device_id
) {
102 DCHECK(thread_checker_
.CalledOnValidThread());
103 DeviceMap::iterator it
= devices_
.find(device_id
);
104 if (it
!= devices_
.end())
108 } // namespace device