| OLD | NEW | 
|---|
|  | (Empty) | 
| 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 <vector> |  | 
| 8 |  | 
| 9 #include "base/lazy_instance.h" |  | 
| 10 #include "base/logging.h" |  | 
| 11 #include "base/memory/scoped_vector.h" |  | 
| 12 #include "base/message_loop/message_loop.h" |  | 
| 13 #include "base/threading/thread_restrictions.h" |  | 
| 14 #include "build/build_config.h" |  | 
| 15 #include "device/hid/hid_device_info.h" |  | 
| 16 |  | 
| 17 #if defined(OS_LINUX) |  | 
| 18 #include "device/hid/hid_service_linux.h" |  | 
| 19 #elif defined(OS_MACOSX) |  | 
| 20 #include "device/hid/hid_service_mac.h" |  | 
| 21 #else |  | 
| 22 #include "device/hid/hid_service_win.h" |  | 
| 23 #endif |  | 
| 24 |  | 
| 25 namespace device { |  | 
| 26 |  | 
| 27 namespace { |  | 
| 28 |  | 
| 29 // The instance will be reset when message loop destroys. |  | 
| 30 base::LazyInstance<scoped_ptr<HidService> >::Leaky g_hid_service_ptr = |  | 
| 31     LAZY_INSTANCE_INITIALIZER; |  | 
| 32 |  | 
| 33 }  // namespace |  | 
| 34 |  | 
| 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) { |  | 
| 52   DCHECK(thread_checker_.CalledOnValidThread()); |  | 
| 53   STLClearObject(devices); |  | 
| 54   for (DeviceMap::iterator it = devices_.begin(); |  | 
| 55       it != devices_.end(); |  | 
| 56       ++it) { |  | 
| 57     devices->push_back(it->second); |  | 
| 58   } |  | 
| 59 } |  | 
| 60 |  | 
| 61 // Fills in the device info struct of the given device_id. |  | 
| 62 bool HidService::GetInfo(std::string device_id, HidDeviceInfo* info) const { |  | 
| 63   DeviceMap::const_iterator it = devices_.find(device_id); |  | 
| 64   if (it == devices_.end()) |  | 
| 65     return false; |  | 
| 66   *info = it->second; |  | 
| 67   return true; |  | 
| 68 } |  | 
| 69 |  | 
| 70 void HidService::AddDevice(HidDeviceInfo info) { |  | 
| 71   if (!ContainsKey(devices_, info.device_id)) { |  | 
| 72     DCHECK(thread_checker_.CalledOnValidThread()); |  | 
| 73     devices_[info.device_id] = info; |  | 
| 74   } |  | 
| 75 } |  | 
| 76 |  | 
| 77 void HidService::RemoveDevice(std::string device_id) { |  | 
| 78   if (ContainsKey(devices_, device_id)) { |  | 
| 79     DCHECK(thread_checker_.CalledOnValidThread()); |  | 
| 80     devices_.erase(device_id); |  | 
| 81   } |  | 
| 82 } |  | 
| 83 |  | 
| 84 HidService* HidService::CreateInstance() { |  | 
| 85 #if defined(OS_LINUX) |  | 
| 86     return new HidServiceLinux(); |  | 
| 87 #elif defined(OS_MACOSX) |  | 
| 88     return new HidServiceMac(); |  | 
| 89 #elif defined(OS_WIN) |  | 
| 90     return new HidServiceWin(); |  | 
| 91 #else |  | 
| 92     return NULL; |  | 
| 93 #endif |  | 
| 94 } |  | 
| 95 |  | 
| 96 HidService* HidService::GetInstance() { |  | 
| 97   if (!g_hid_service_ptr.Get().get()){ |  | 
| 98     scoped_ptr<HidService> service(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(); |  | 
| 104 } |  | 
| 105 |  | 
| 106 }  // namespace device |  | 
| OLD | NEW | 
|---|