ozone: Disable menu tests
[chromium-blink-merge.git] / components / usb_service / usb_service_impl.cc
bloba77749e7d4da3a245481aacbeb296bfe4e98ecaf
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 UsbService* instance = g_usb_service_instance.Get().get();
158 if (!instance) {
159 PlatformUsbContext context = NULL;
161 const int rv = libusb_init(&context);
162 if (rv != LIBUSB_SUCCESS) {
163 VLOG(1) << "Failed to initialize libusb: " << ConvertErrorToString(rv);
164 return NULL;
166 if (!context)
167 return NULL;
169 instance = new UsbServiceImpl(context);
170 g_usb_service_instance.Get().reset(instance);
172 return instance;
175 // static
176 void UsbService::SetInstanceForTest(UsbService* instance) {
177 g_usb_service_instance.Get().reset(instance);
180 } // namespace usb_service