Allow chrome.systemPrivate to be used by apps.
[chromium-blink-merge.git] / components / usb_service / usb_service_impl.cc
blob88aa98f676d5cfe7b5b05b6a5c33228e941132fe
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 <map>
8 #include <set>
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/stl_util.h"
13 #include "components/usb_service/usb_context.h"
14 #include "components/usb_service/usb_device_impl.h"
15 #include "components/usb_service/usb_error.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "third_party/libusb/src/libusb/libusb.h"
19 namespace usb_service {
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
32 : public UsbService,
33 private base::MessageLoop::DestructionObserver {
34 public:
35 explicit UsbServiceImpl(PlatformUsbContext context);
36 virtual ~UsbServiceImpl();
38 private:
39 // usb_service::UsbService implementation
40 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE;
41 virtual void GetDevices(
42 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE;
44 // base::MessageLoop::DestructionObserver implementation.
45 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
47 // Enumerate USB devices from OS and Update devices_ map.
48 void RefreshDevices();
50 scoped_refptr<UsbContext> context_;
52 // TODO(ikarienator): Figure out a better solution.
53 uint32 next_unique_id_;
55 // The map from PlatformUsbDevices to UsbDevices.
56 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl> > DeviceMap;
57 DeviceMap devices_;
59 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl);
62 scoped_refptr<UsbDevice> UsbServiceImpl::GetDeviceById(uint32 unique_id) {
63 DCHECK(CalledOnValidThread());
64 RefreshDevices();
65 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
66 if (it->second->unique_id() == unique_id)
67 return it->second;
69 return NULL;
72 void UsbServiceImpl::GetDevices(
73 std::vector<scoped_refptr<UsbDevice> >* devices) {
74 DCHECK(CalledOnValidThread());
75 STLClearObject(devices);
76 RefreshDevices();
78 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
79 devices->push_back(it->second);
83 void UsbServiceImpl::WillDestroyCurrentMessageLoop() {
84 DCHECK(CalledOnValidThread());
85 g_usb_service_instance.Get().reset(NULL);
88 UsbServiceImpl::UsbServiceImpl(PlatformUsbContext context)
89 : context_(new UsbContext(context)), next_unique_id_(0) {
90 base::MessageLoop::current()->AddDestructionObserver(this);
93 UsbServiceImpl::~UsbServiceImpl() {
94 base::MessageLoop::current()->RemoveDestructionObserver(this);
95 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
96 it->second->OnDisconnect();
100 void UsbServiceImpl::RefreshDevices() {
101 DCHECK(CalledOnValidThread());
103 libusb_device** platform_devices = NULL;
104 const ssize_t device_count =
105 libusb_get_device_list(context_->context(), &platform_devices);
106 if (device_count < 0) {
107 VLOG(1) << "Failed to get device list: "
108 << ConvertErrorToString(device_count);
111 std::set<UsbDevice*> connected_devices;
112 std::vector<PlatformUsbDevice> disconnected_devices;
114 // Populates new devices.
115 for (ssize_t i = 0; i < device_count; ++i) {
116 if (!ContainsKey(devices_, platform_devices[i])) {
117 libusb_device_descriptor descriptor;
118 const int rv =
119 libusb_get_device_descriptor(platform_devices[i], &descriptor);
120 // This test is needed. A valid vendor/produce pair is required.
121 if (rv != LIBUSB_SUCCESS) {
122 VLOG(1) << "Failed to get device descriptor: "
123 << ConvertErrorToString(rv);
124 continue;
126 UsbDeviceImpl* new_device = new UsbDeviceImpl(context_,
127 platform_devices[i],
128 descriptor.idVendor,
129 descriptor.idProduct,
130 ++next_unique_id_);
131 devices_[platform_devices[i]] = new_device;
132 connected_devices.insert(new_device);
133 } else {
134 connected_devices.insert(devices_[platform_devices[i]].get());
138 // Find disconnected devices.
139 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
140 if (!ContainsKey(connected_devices, it->second)) {
141 disconnected_devices.push_back(it->first);
145 // Remove disconnected devices from devices_.
146 for (size_t i = 0; i < disconnected_devices.size(); ++i) {
147 // UsbDevice will be destroyed after this. The corresponding
148 // PlatformUsbDevice will be unref'ed during this process.
149 devices_.erase(disconnected_devices[i]);
152 libusb_free_device_list(platform_devices, true);
155 // static
156 UsbService* UsbService::GetInstance() {
157 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
158 UsbService* instance = g_usb_service_instance.Get().get();
159 if (!instance) {
160 PlatformUsbContext context = NULL;
162 const int rv = libusb_init(&context);
163 if (rv != LIBUSB_SUCCESS) {
164 VLOG(1) << "Failed to initialize libusb: " << ConvertErrorToString(rv);
165 return NULL;
167 if (!context)
168 return NULL;
170 instance = new UsbServiceImpl(context);
171 g_usb_service_instance.Get().reset(instance);
173 return instance;
176 // static
177 void UsbService::SetInstanceForTest(UsbService* instance) {
178 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
179 g_usb_service_instance.Get().reset(instance);
182 } // namespace usb_service