| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/hid/hid_service.h" | 5 #include "device/hid/hid_service.h" |
| 6 | 6 |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 8 #include "base/logging.h" |
| 11 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 12 #include "base/message_loop/message_loop.h" | 10 #include "base/stl_util.h" |
| 13 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
| 14 #include "build/build_config.h" | |
| 15 #include "device/hid/hid_device_info.h" | |
| 16 | 12 |
| 17 #if defined(OS_LINUX) | 13 #if defined(OS_LINUX) |
| 18 #include "device/hid/hid_service_linux.h" | 14 #include "device/hid/hid_service_linux.h" |
| 19 #elif defined(OS_MACOSX) | 15 #elif defined(OS_MACOSX) |
| 20 #include "device/hid/hid_service_mac.h" | 16 #include "device/hid/hid_service_mac.h" |
| 21 #else | 17 #else |
| 22 #include "device/hid/hid_service_win.h" | 18 #include "device/hid/hid_service_win.h" |
| 23 #endif | 19 #endif |
| 24 | 20 |
| 25 namespace device { | 21 namespace device { |
| 26 | 22 |
| 27 namespace { | 23 namespace { |
| 28 | 24 |
| 29 // The instance will be reset when message loop destroys. | 25 // The instance will be reset when message loop destroys. |
| 30 base::LazyInstance<scoped_ptr<HidService> >::Leaky g_hid_service_ptr = | 26 base::LazyInstance<scoped_ptr<HidService> >::Leaky g_hid_service_ptr = |
| 31 LAZY_INSTANCE_INITIALIZER; | 27 LAZY_INSTANCE_INITIALIZER; |
| 32 | 28 |
| 33 } // namespace | 29 } // namespace |
| 34 | 30 |
| 35 HidService::HidService() : initialized_(false) { | |
| 36 base::ThreadRestrictions::AssertIOAllowed(); | |
| 37 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 38 base::MessageLoop::current()->AddDestructionObserver(this); | |
| 39 } | |
| 40 | |
| 41 HidService::~HidService() { | |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 43 base::MessageLoop::current()->RemoveDestructionObserver(this); | |
| 44 } | |
| 45 | |
| 46 void HidService::WillDestroyCurrentMessageLoop() { | |
| 47 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 48 g_hid_service_ptr.Get().reset(NULL); | |
| 49 } | |
| 50 | |
| 51 void HidService::GetDevices(std::vector<HidDeviceInfo>* devices) { | 31 void HidService::GetDevices(std::vector<HidDeviceInfo>* devices) { |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | 32 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 STLClearObject(devices); | 33 STLClearObject(devices); |
| 54 for (DeviceMap::iterator it = devices_.begin(); | 34 for (DeviceMap::iterator it = devices_.begin(); |
| 55 it != devices_.end(); | 35 it != devices_.end(); |
| 56 ++it) { | 36 ++it) { |
| 57 devices->push_back(it->second); | 37 devices->push_back(it->second); |
| 58 } | 38 } |
| 59 } | 39 } |
| 60 | 40 |
| 61 // Fills in the device info struct of the given device_id. | 41 // Fills in the device info struct of the given device_id. |
| 62 bool HidService::GetInfo(std::string device_id, HidDeviceInfo* info) const { | 42 bool HidService::GetDeviceInfo(const HidDeviceId& device_id, |
| 43 HidDeviceInfo* info) const { |
| 63 DeviceMap::const_iterator it = devices_.find(device_id); | 44 DeviceMap::const_iterator it = devices_.find(device_id); |
| 64 if (it == devices_.end()) | 45 if (it == devices_.end()) |
| 65 return false; | 46 return false; |
| 66 *info = it->second; | 47 *info = it->second; |
| 67 return true; | 48 return true; |
| 68 } | 49 } |
| 69 | 50 |
| 70 void HidService::AddDevice(HidDeviceInfo info) { | 51 void HidService::WillDestroyCurrentMessageLoop() { |
| 71 if (!ContainsKey(devices_, info.device_id)) { | 52 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); | 53 g_hid_service_ptr.Get().reset(NULL); |
| 73 devices_[info.device_id] = info; | |
| 74 } | |
| 75 } | 54 } |
| 76 | 55 |
| 77 void HidService::RemoveDevice(std::string device_id) { | 56 HidService::HidService() { |
| 78 if (ContainsKey(devices_, device_id)) { | 57 base::ThreadRestrictions::AssertIOAllowed(); |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); | 58 DCHECK(thread_checker_.CalledOnValidThread()); |
| 80 devices_.erase(device_id); | 59 base::MessageLoop::current()->AddDestructionObserver(this); |
| 81 } | 60 } |
| 61 |
| 62 HidService::~HidService() { |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 82 } | 65 } |
| 83 | 66 |
| 84 HidService* HidService::CreateInstance() { | 67 HidService* HidService::CreateInstance() { |
| 85 #if defined(OS_LINUX) | 68 #if defined(OS_LINUX) |
| 86 return new HidServiceLinux(); | 69 return new HidServiceLinux(); |
| 87 #elif defined(OS_MACOSX) | 70 #elif defined(OS_MACOSX) |
| 88 return new HidServiceMac(); | 71 return new HidServiceMac(); |
| 89 #elif defined(OS_WIN) | 72 #elif defined(OS_WIN) |
| 90 return new HidServiceWin(); | 73 return new HidServiceWin(); |
| 91 #else | 74 #else |
| 92 return NULL; | 75 return NULL; |
| 93 #endif | 76 #endif |
| 94 } | 77 } |
| 95 | 78 |
| 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; |
| 83 } |
| 84 } |
| 85 |
| 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); |
| 91 } |
| 92 |
| 96 HidService* HidService::GetInstance() { | 93 HidService* HidService::GetInstance() { |
| 97 if (!g_hid_service_ptr.Get().get()){ | 94 if (!g_hid_service_ptr.Get().get()) |
| 98 scoped_ptr<HidService> service(CreateInstance()); | 95 g_hid_service_ptr.Get().reset(CreateInstance()); |
| 99 | |
| 100 if (service && service->initialized()) | |
| 101 g_hid_service_ptr.Get().reset(service.release()); | |
| 102 } | |
| 103 return g_hid_service_ptr.Get().get(); | 96 return g_hid_service_ptr.Get().get(); |
| 104 } | 97 } |
| 105 | 98 |
| 106 } // namespace device | 99 } // namespace device |
| OLD | NEW |