OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/usb_service/usb_service.h" | |
6 | |
7 #include <set> | |
8 #include <vector> | |
9 | |
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" | |
16 | |
17 namespace usb_service { | |
18 | |
19 namespace { | |
20 | |
21 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance = | |
22 LAZY_INSTANCE_INITIALIZER; | |
23 | |
24 } // namespace | |
25 | |
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; | |
36 | |
37 instance = new UsbService(context); | |
38 g_usb_service_instance.Get().reset(instance); | |
39 } | |
40 return instance; | |
41 } | |
42 | |
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; | |
49 } | |
50 return NULL; | |
51 } | |
52 | |
53 void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) { | |
54 DCHECK(CalledOnValidThread()); | |
55 STLClearObject(devices); | |
56 RefreshDevices(); | |
57 | |
58 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { | |
59 devices->push_back(it->second); | |
60 } | |
61 } | |
62 | |
63 void UsbService::WillDestroyCurrentMessageLoop() { | |
64 DCHECK(CalledOnValidThread()); | |
65 g_usb_service_instance.Get().reset(NULL); | |
66 } | |
67 | |
68 UsbService::UsbService(PlatformUsbContext context) | |
69 : context_(new UsbContext(context)), next_unique_id_(0) { | |
70 base::MessageLoop::current()->AddDestructionObserver(this); | |
71 } | |
72 | |
73 UsbService::~UsbService() { | |
74 base::MessageLoop::current()->RemoveDestructionObserver(this); | |
75 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { | |
76 it->second->OnDisconnect(); | |
77 } | |
78 } | |
79 | |
80 void UsbService::RefreshDevices() { | |
81 DCHECK(CalledOnValidThread()); | |
82 | |
83 libusb_device** platform_devices = NULL; | |
84 const ssize_t device_count = | |
85 libusb_get_device_list(context_->context(), &platform_devices); | |
86 | |
87 std::set<UsbDevice*> connected_devices; | |
88 std::vector<PlatformUsbDevice> disconnected_devices; | |
89 | |
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()); | |
106 } | |
107 } | |
108 | |
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); | |
113 } | |
114 } | |
115 | |
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]); | |
121 } | |
122 | |
123 libusb_free_device_list(platform_devices, true); | |
124 } | |
125 | |
126 } // namespace usb_service | |
OLD | NEW |