Revert of Instrumented libraries: update the libnspr4 build script. (patchset #1...
[chromium-blink-merge.git] / device / hid / hid_service.cc
blob2dfdb896e91c3101e516f7e1f92865dff2f020b3
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"
18 #else
19 #include "device/hid/hid_service_win.h"
20 #endif
22 namespace device {
24 namespace {
26 HidService* g_service = NULL;
28 } // namespace
30 class HidService::Destroyer : public base::MessageLoop::DestructionObserver {
31 public:
32 explicit Destroyer(HidService* hid_service)
33 : hid_service_(hid_service) {}
34 virtual ~Destroyer() {}
36 private:
37 // base::MessageLoop::DestructionObserver implementation.
38 virtual void WillDestroyCurrentMessageLoop() OVERRIDE {
39 base::MessageLoop::current()->RemoveDestructionObserver(this);
40 delete hid_service_;
41 delete this;
42 g_service = NULL;
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();
55 #elif defined(OS_WIN)
56 g_service = new HidServiceWin();
57 #endif
58 if (g_service != NULL) {
59 Destroyer* destroyer = new Destroyer(g_service);
60 base::MessageLoop::current()->AddDestructionObserver(destroyer);
63 return g_service;
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();
74 it != devices_.end();
75 ++it) {
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())
85 return false;
86 *info = it->second;
87 return true;
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())
106 devices_.erase(it);
109 const HidService::DeviceMap& HidService::GetDevicesNoEnumerate() const {
110 return devices_;
113 } // namespace device