Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: chrome/browser/usb/usb_service.cc

Issue 236203019: Move UsbService to its own component. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move usb_service component symbols into usb_service namespace Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/usb/usb_service.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/browser/usb/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 "chrome/browser/usb/usb_context.h"
13 #include "chrome/browser/usb/usb_device.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "third_party/libusb/src/libusb/libusb.h"
16
17 namespace {
18
19 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance =
20 LAZY_INSTANCE_INITIALIZER;
21
22 } // namespace
23
24 // static
25 UsbService* UsbService::GetInstance() {
26 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
27 UsbService* instance = g_usb_service_instance.Get().get();
28 if (!instance) {
29 PlatformUsbContext context = NULL;
30 if (libusb_init(&context) != LIBUSB_SUCCESS)
31 return NULL;
32 if (!context)
33 return NULL;
34
35 instance = new UsbService(context);
36 g_usb_service_instance.Get().reset(instance);
37 }
38 return instance;
39 }
40
41 scoped_refptr<UsbDevice> UsbService::GetDeviceById(uint32 unique_id) {
42 DCHECK(CalledOnValidThread());
43 RefreshDevices();
44 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
45 if (it->second->unique_id() == unique_id)
46 return it->second;
47 }
48 return NULL;
49 }
50
51 void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) {
52 DCHECK(CalledOnValidThread());
53 STLClearObject(devices);
54 RefreshDevices();
55
56 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
57 devices->push_back(it->second);
58 }
59 }
60
61 void UsbService::WillDestroyCurrentMessageLoop() {
62 DCHECK(CalledOnValidThread());
63 g_usb_service_instance.Get().reset(NULL);
64 }
65
66 UsbService::UsbService(PlatformUsbContext context)
67 : context_(new UsbContext(context)), next_unique_id_(0) {
68 base::MessageLoop::current()->AddDestructionObserver(this);
69 }
70
71 UsbService::~UsbService() {
72 base::MessageLoop::current()->RemoveDestructionObserver(this);
73 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
74 it->second->OnDisconnect();
75 }
76 }
77
78 void UsbService::RefreshDevices() {
79 DCHECK(CalledOnValidThread());
80
81 libusb_device** platform_devices = NULL;
82 const ssize_t device_count =
83 libusb_get_device_list(context_->context(), &platform_devices);
84
85 std::set<UsbDevice*> connected_devices;
86 std::vector<PlatformUsbDevice> disconnected_devices;
87
88 // Populates new devices.
89 for (ssize_t i = 0; i < device_count; ++i) {
90 if (!ContainsKey(devices_, platform_devices[i])) {
91 libusb_device_descriptor descriptor;
92 // This test is needed. A valid vendor/produce pair is required.
93 if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor))
94 continue;
95 UsbDevice* new_device = new UsbDevice(context_,
96 platform_devices[i],
97 descriptor.idVendor,
98 descriptor.idProduct,
99 ++next_unique_id_);
100 devices_[platform_devices[i]] = new_device;
101 connected_devices.insert(new_device);
102 } else {
103 connected_devices.insert(devices_[platform_devices[i]].get());
104 }
105 }
106
107 // Find disconnected devices.
108 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
109 if (!ContainsKey(connected_devices, it->second)) {
110 disconnected_devices.push_back(it->first);
111 }
112 }
113
114 // Remove disconnected devices from devices_.
115 for (size_t i = 0; i < disconnected_devices.size(); ++i) {
116 // UsbDevice will be destroyed after this. The corresponding
117 // PlatformUsbDevice will be unref'ed during this process.
118 devices_.erase(disconnected_devices[i]);
119 }
120
121 libusb_free_device_list(platform_devices, true);
122 }
OLDNEW
« no previous file with comments | « chrome/browser/usb/usb_service.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698