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"
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 // The instance will be reset when message loop destroys.
26 base::LazyInstance
<scoped_ptr
<HidService
> >::Leaky g_hid_service_ptr
=
27 LAZY_INSTANCE_INITIALIZER
;
31 void HidService::GetDevices(std::vector
<HidDeviceInfo
>* devices
) {
32 DCHECK(thread_checker_
.CalledOnValidThread());
33 STLClearObject(devices
);
34 for (DeviceMap::iterator it
= devices_
.begin();
37 devices
->push_back(it
->second
);
41 // Fills in the device info struct of the given device_id.
42 bool HidService::GetDeviceInfo(const HidDeviceId
& device_id
,
43 HidDeviceInfo
* info
) const {
44 DeviceMap::const_iterator it
= devices_
.find(device_id
);
45 if (it
== devices_
.end())
51 void HidService::WillDestroyCurrentMessageLoop() {
52 DCHECK(thread_checker_
.CalledOnValidThread());
53 g_hid_service_ptr
.Get().reset(NULL
);
56 HidService::HidService() {
57 base::ThreadRestrictions::AssertIOAllowed();
58 DCHECK(thread_checker_
.CalledOnValidThread());
59 base::MessageLoop::current()->AddDestructionObserver(this);
62 HidService::~HidService() {
63 DCHECK(thread_checker_
.CalledOnValidThread());
64 base::MessageLoop::current()->RemoveDestructionObserver(this);
67 HidService
* HidService::CreateInstance() {
69 return new HidServiceLinux();
70 #elif defined(OS_MACOSX)
71 return new HidServiceMac();
73 return new HidServiceWin();
79 void HidService::AddDevice(const HidDeviceInfo
& info
) {
80 DCHECK(thread_checker_
.CalledOnValidThread());
81 if (!ContainsKey(devices_
, info
.device_id
)) {
82 devices_
[info
.device_id
] = info
;
86 void HidService::RemoveDevice(const HidDeviceId
& device_id
) {
87 DCHECK(thread_checker_
.CalledOnValidThread());
88 DeviceMap::iterator it
= devices_
.find(device_id
);
89 if (it
!= devices_
.end())
93 HidService
* HidService::GetInstance() {
94 if (!g_hid_service_ptr
.Get().get())
95 g_hid_service_ptr
.Get().reset(CreateInstance());
96 return g_hid_service_ptr
.Get().get();