Disable firewall check. It takes signifficant time, need to be on FILE thread.
[chromium-blink-merge.git] / components / usb_service / usb_service.cc
blob89b884f360991368f9556901afc2046f667f7329
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 "components/usb_service/usb_service.h"
7 #include <set>
8 #include <vector>
10 #include "base/lazy_instance.h"
11 #include "base/stl_util.h"
12 #include "components/usb_service/usb_context.h"
13 #include "components/usb_service/usb_device.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "third_party/libusb/src/libusb/libusb.h"
17 namespace usb_service {
19 namespace {
21 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance =
22 LAZY_INSTANCE_INITIALIZER;
24 } // namespace
26 // static
27 UsbService* UsbService::GetInstance() {
28 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
29 UsbService* instance = g_usb_service_instance.Get().get();
30 if (!instance) {
31 PlatformUsbContext context = NULL;
32 if (libusb_init(&context) != LIBUSB_SUCCESS)
33 return NULL;
34 if (!context)
35 return NULL;
37 instance = new UsbService(context);
38 g_usb_service_instance.Get().reset(instance);
40 return instance;
43 scoped_refptr<UsbDevice> UsbService::GetDeviceById(uint32 unique_id) {
44 DCHECK(CalledOnValidThread());
45 RefreshDevices();
46 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
47 if (it->second->unique_id() == unique_id)
48 return it->second;
50 return NULL;
53 void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) {
54 DCHECK(CalledOnValidThread());
55 STLClearObject(devices);
56 RefreshDevices();
58 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
59 devices->push_back(it->second);
63 void UsbService::WillDestroyCurrentMessageLoop() {
64 DCHECK(CalledOnValidThread());
65 g_usb_service_instance.Get().reset(NULL);
68 UsbService::UsbService(PlatformUsbContext context)
69 : context_(new UsbContext(context)), next_unique_id_(0) {
70 base::MessageLoop::current()->AddDestructionObserver(this);
73 UsbService::~UsbService() {
74 base::MessageLoop::current()->RemoveDestructionObserver(this);
75 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
76 it->second->OnDisconnect();
80 void UsbService::RefreshDevices() {
81 DCHECK(CalledOnValidThread());
83 libusb_device** platform_devices = NULL;
84 const ssize_t device_count =
85 libusb_get_device_list(context_->context(), &platform_devices);
87 std::set<UsbDevice*> connected_devices;
88 std::vector<PlatformUsbDevice> disconnected_devices;
90 // Populates new devices.
91 for (ssize_t i = 0; i < device_count; ++i) {
92 if (!ContainsKey(devices_, platform_devices[i])) {
93 libusb_device_descriptor descriptor;
94 // This test is needed. A valid vendor/produce pair is required.
95 if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor))
96 continue;
97 UsbDevice* new_device = new UsbDevice(context_,
98 platform_devices[i],
99 descriptor.idVendor,
100 descriptor.idProduct,
101 ++next_unique_id_);
102 devices_[platform_devices[i]] = new_device;
103 connected_devices.insert(new_device);
104 } else {
105 connected_devices.insert(devices_[platform_devices[i]].get());
109 // Find disconnected devices.
110 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
111 if (!ContainsKey(connected_devices, it->second)) {
112 disconnected_devices.push_back(it->first);
116 // Remove disconnected devices from devices_.
117 for (size_t i = 0; i < disconnected_devices.size(); ++i) {
118 // UsbDevice will be destroyed after this. The corresponding
119 // PlatformUsbDevice will be unref'ed during this process.
120 devices_.erase(disconnected_devices[i]);
123 libusb_free_device_list(platform_devices, true);
126 } // namespace usb_service