| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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 void HidService::DeviceAdd(HidDeviceInfo info) { |
| 62 if (!ContainsKey(devices_, info.device_id)) { |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 devices_[info.device_id] = info; |
| 65 } |
| 66 } |
| 67 |
| 68 void HidService::DeviceRemove(std::string device_id) { |
| 69 if (ContainsKey(devices_, device_id)) { |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 71 devices_.erase(device_id); |
| 72 } |
| 73 } |
| 74 |
| 75 HidService* HidService::CreateInstance() { |
| 76 #if defined(OS_LINUX) |
| 77 return new HidServiceLinux(); |
| 78 #elif defined(OS_MACOSX) |
| 79 return new HidServiceMac(); |
| 80 #elif defined(OS_WIN) |
| 81 return new HidServiceWin(); |
| 82 #else |
| 83 return NULL; |
| 84 #endif |
| 85 } |
| 86 |
| 87 HidService* HidService::GetInstance() { |
| 88 if (!g_hid_service_ptr.Get().get()){ |
| 89 if (base::MessageLoop::current()->type() != base::MessageLoop::TYPE_IO) { |
| 90 LOG(ERROR) << "HidService must be obtained on IO message loop"; |
| 91 return NULL; |
| 92 } |
| 93 |
| 94 scoped_ptr<HidService> service(CreateInstance()); |
| 95 |
| 96 if (service && service->initialized()) |
| 97 g_hid_service_ptr.Get().reset(service.release()); |
| 98 } |
| 99 return g_hid_service_ptr.Get().get(); |
| 100 } |
| 101 |
| 102 } // namespace device |
| OLD | NEW |