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"
12 #include "base/threading/thread_restrictions.h"
14 #if defined(OS_LINUX) && defined(USE_UDEV)
15 #include "device/hid/hid_service_linux.h"
16 #elif defined(OS_MACOSX)
17 #include "device/hid/hid_service_mac.h"
19 #include "device/hid/hid_service_win.h"
26 HidService
* g_service
= NULL
;
30 class HidService::Destroyer
: public base::MessageLoop::DestructionObserver
{
32 explicit Destroyer(HidService
* hid_service
)
33 : hid_service_(hid_service
) {}
34 virtual ~Destroyer() {}
37 // base::MessageLoop::DestructionObserver implementation.
38 virtual void WillDestroyCurrentMessageLoop() OVERRIDE
{
39 base::MessageLoop::current()->RemoveDestructionObserver(this);
45 HidService
* hid_service_
;
48 HidService
* HidService::GetInstance(
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();
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 base::ThreadRestrictions::AssertIOAllowed();
92 DCHECK(thread_checker_
.CalledOnValidThread());
95 void HidService::AddDevice(const HidDeviceInfo
& info
) {
96 DCHECK(thread_checker_
.CalledOnValidThread());
97 if (!ContainsKey(devices_
, info
.device_id
)) {
98 devices_
[info
.device_id
] = info
;
102 void HidService::RemoveDevice(const HidDeviceId
& device_id
) {
103 DCHECK(thread_checker_
.CalledOnValidThread());
104 DeviceMap::iterator it
= devices_
.find(device_id
);
105 if (it
!= devices_
.end())
109 const HidService::DeviceMap
& HidService::GetDevicesNoEnumerate() const {
113 } // namespace device