Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / device / hid / hid_service.cc
blob2d0569bef069d2ad3759bfade98e573a2b3a3752
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)
14 #include "device/hid/hid_service_linux.h"
15 #elif defined(OS_MACOSX)
16 #include "device/hid/hid_service_mac.h"
17 #else
18 #include "device/hid/hid_service_win.h"
19 #endif
21 namespace device {
23 namespace {
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;
29 } // namespace
31 void HidService::GetDevices(std::vector<HidDeviceInfo>* devices) {
32 DCHECK(thread_checker_.CalledOnValidThread());
33 STLClearObject(devices);
34 for (DeviceMap::iterator it = devices_.begin();
35 it != devices_.end();
36 ++it) {
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())
46 return false;
47 *info = it->second;
48 return true;
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() {
68 #if defined(OS_LINUX)
69 return new HidServiceLinux();
70 #elif defined(OS_MACOSX)
71 return new HidServiceMac();
72 #elif defined(OS_WIN)
73 return new HidServiceWin();
74 #else
75 return NULL;
76 #endif
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())
90 devices_.erase(it);
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();
99 } // namespace device