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. 4 5 #include "device/hid/hid_service.h" 6 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" 12 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" 17 #else 18 #include "device/hid/hid_service_win.h" 19 #endif 20 21 namespace device { 22 23 namespace { 24 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; 28 29 } // namespace 30 GetDevices(std::vector<HidDeviceInfo> * devices)31void 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); 38 } 39 } 40 41 // Fills in the device info struct of the given device_id. GetDeviceInfo(const HidDeviceId & device_id,HidDeviceInfo * info) const42bool 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; 49 } 50 WillDestroyCurrentMessageLoop()51void HidService::WillDestroyCurrentMessageLoop() { 52 DCHECK(thread_checker_.CalledOnValidThread()); 53 g_hid_service_ptr.Get().reset(NULL); 54 } 55 HidService()56HidService::HidService() { 57 base::ThreadRestrictions::AssertIOAllowed(); 58 DCHECK(thread_checker_.CalledOnValidThread()); 59 base::MessageLoop::current()->AddDestructionObserver(this); 60 } 61 ~HidService()62HidService::~HidService() { 63 DCHECK(thread_checker_.CalledOnValidThread()); 64 base::MessageLoop::current()->RemoveDestructionObserver(this); 65 } 66 CreateInstance()67HidService* HidService::CreateInstance() { 68 #if defined(OS_LINUX) && defined(USE_UDEV) 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 77 } 78 AddDevice(const HidDeviceInfo & info)79void HidService::AddDevice(const HidDeviceInfo& info) { 80 DCHECK(thread_checker_.CalledOnValidThread()); 81 if (!ContainsKey(devices_, info.device_id)) { 82 devices_[info.device_id] = info; 83 } 84 } 85 RemoveDevice(const HidDeviceId & device_id)86void 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); 91 } 92 GetInstance()93HidService* 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(); 97 } 98 99 } // namespace device 100