mojo: Fix map of booleans for python bindings.
[chromium-blink-merge.git] / device / usb / usb_service_impl.cc
blobe639870cb059ea0560b839c1ae2f5ceeb5b2495e
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/usb/usb_service.h"
7 #include <map>
8 #include <set>
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/stl_util.h"
14 #include "device/usb/usb_context.h"
15 #include "device/usb/usb_device_impl.h"
16 #include "device/usb/usb_error.h"
17 #include "third_party/libusb/src/libusb/libusb.h"
19 namespace device {
21 namespace {
23 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance =
24 LAZY_INSTANCE_INITIALIZER;
26 } // namespace
28 typedef struct libusb_device* PlatformUsbDevice;
29 typedef struct libusb_context* PlatformUsbContext;
31 class UsbServiceImpl : public UsbService,
32 private base::MessageLoop::DestructionObserver {
33 public:
34 explicit UsbServiceImpl(
35 PlatformUsbContext context,
36 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
37 virtual ~UsbServiceImpl();
39 private:
40 // device::UsbService implementation
41 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override;
42 virtual void GetDevices(
43 std::vector<scoped_refptr<UsbDevice> >* devices) override;
45 // base::MessageLoop::DestructionObserver implementation.
46 virtual void WillDestroyCurrentMessageLoop() override;
48 // Enumerate USB devices from OS and update devices_ map.
49 void RefreshDevices();
51 scoped_refptr<UsbContext> context_;
52 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
53 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
55 // TODO(reillyg): Figure out a better solution.
56 uint32 next_unique_id_;
58 // The map from unique IDs to UsbDevices.
59 typedef std::map<uint32, scoped_refptr<UsbDeviceImpl> > DeviceMap;
60 DeviceMap devices_;
62 // The map from PlatformUsbDevices to UsbDevices.
63 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl> >
64 PlatformDeviceMap;
65 PlatformDeviceMap platform_devices_;
67 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl);
70 scoped_refptr<UsbDevice> UsbServiceImpl::GetDeviceById(uint32 unique_id) {
71 DCHECK(CalledOnValidThread());
72 RefreshDevices();
73 DeviceMap::iterator it = devices_.find(unique_id);
74 if (it != devices_.end()) {
75 return it->second;
77 return NULL;
80 void UsbServiceImpl::GetDevices(
81 std::vector<scoped_refptr<UsbDevice> >* devices) {
82 DCHECK(CalledOnValidThread());
83 STLClearObject(devices);
84 RefreshDevices();
86 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
87 devices->push_back(it->second);
91 void UsbServiceImpl::WillDestroyCurrentMessageLoop() {
92 DCHECK(CalledOnValidThread());
93 g_usb_service_instance.Get().reset(NULL);
96 UsbServiceImpl::UsbServiceImpl(
97 PlatformUsbContext context,
98 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
99 : context_(new UsbContext(context)),
100 ui_task_runner_(ui_task_runner),
101 next_unique_id_(0) {
102 base::MessageLoop::current()->AddDestructionObserver(this);
105 UsbServiceImpl::~UsbServiceImpl() {
106 base::MessageLoop::current()->RemoveDestructionObserver(this);
107 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
108 it->second->OnDisconnect();
112 void UsbServiceImpl::RefreshDevices() {
113 DCHECK(CalledOnValidThread());
115 libusb_device** platform_devices = NULL;
116 const ssize_t device_count =
117 libusb_get_device_list(context_->context(), &platform_devices);
118 if (device_count < 0) {
119 VLOG(1) << "Failed to get device list: "
120 << ConvertPlatformUsbErrorToString(device_count);
123 std::set<UsbDevice*> connected_devices;
124 std::vector<PlatformUsbDevice> disconnected_devices;
126 // Populates new devices.
127 for (ssize_t i = 0; i < device_count; ++i) {
128 if (!ContainsKey(platform_devices_, platform_devices[i])) {
129 libusb_device_descriptor descriptor;
130 const int rv =
131 libusb_get_device_descriptor(platform_devices[i], &descriptor);
132 // This test is needed. A valid vendor/produce pair is required.
133 if (rv != LIBUSB_SUCCESS) {
134 VLOG(1) << "Failed to get device descriptor: "
135 << ConvertPlatformUsbErrorToString(rv);
136 continue;
139 uint32 unique_id;
140 do {
141 unique_id = ++next_unique_id_;
142 } while (devices_.find(unique_id) != devices_.end());
144 scoped_refptr<UsbDeviceImpl> new_device(
145 new UsbDeviceImpl(context_,
146 ui_task_runner_,
147 platform_devices[i],
148 descriptor.idVendor,
149 descriptor.idProduct,
150 unique_id));
151 platform_devices_[platform_devices[i]] = new_device;
152 devices_[unique_id] = new_device;
153 connected_devices.insert(new_device.get());
154 } else {
155 connected_devices.insert(platform_devices_[platform_devices[i]].get());
159 // Find disconnected devices.
160 for (PlatformDeviceMap::iterator it = platform_devices_.begin();
161 it != platform_devices_.end();
162 ++it) {
163 if (!ContainsKey(connected_devices, it->second.get())) {
164 disconnected_devices.push_back(it->first);
165 devices_.erase(it->second->unique_id());
166 it->second->OnDisconnect();
170 // Remove disconnected devices from platform_devices_.
171 for (size_t i = 0; i < disconnected_devices.size(); ++i) {
172 // UsbDevice will be destroyed after this. The corresponding
173 // PlatformUsbDevice will be unref'ed during this process.
174 platform_devices_.erase(disconnected_devices[i]);
177 libusb_free_device_list(platform_devices, true);
180 // static
181 UsbService* UsbService::GetInstance(
182 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
183 UsbService* instance = g_usb_service_instance.Get().get();
184 if (!instance) {
185 PlatformUsbContext context = NULL;
187 const int rv = libusb_init(&context);
188 if (rv != LIBUSB_SUCCESS) {
189 VLOG(1) << "Failed to initialize libusb: "
190 << ConvertPlatformUsbErrorToString(rv);
191 return NULL;
193 if (!context)
194 return NULL;
196 instance = new UsbServiceImpl(context, ui_task_runner);
197 g_usb_service_instance.Get().reset(instance);
199 return instance;
202 // static
203 void UsbService::SetInstanceForTest(UsbService* instance) {
204 g_usb_service_instance.Get().reset(instance);
207 } // namespace device