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

Side by Side Diff: components/usb_service/usb_service_impl.cc

Issue 258783002: Extracted UsbService as interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed chromeos issue. Created 6 years, 7 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 | « components/usb_service/usb_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/usb_service/usb_service.h" 5 #include "components/usb_service/usb_service.h"
6 6
7 #include <map>
7 #include <set> 8 #include <set>
8 #include <vector>
9 9
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
11 #include "base/stl_util.h" 12 #include "base/stl_util.h"
12 #include "components/usb_service/usb_context.h" 13 #include "components/usb_service/usb_context.h"
13 #include "components/usb_service/usb_device.h" 14 #include "components/usb_service/usb_device.h"
14 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
15 #include "third_party/libusb/src/libusb/libusb.h" 16 #include "third_party/libusb/src/libusb/libusb.h"
16 17
17 namespace usb_service { 18 namespace usb_service {
18 19
19 namespace { 20 namespace {
20 21
21 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance = 22 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance =
22 LAZY_INSTANCE_INITIALIZER; 23 LAZY_INSTANCE_INITIALIZER;
23 24
24 } // namespace 25 } // namespace
25 26
26 // static 27 typedef struct libusb_device* PlatformUsbDevice;
27 UsbService* UsbService::GetInstance() { 28 typedef struct libusb_context* PlatformUsbContext;
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 29
37 instance = new UsbService(context); 30 class UsbServiceImpl
38 g_usb_service_instance.Get().reset(instance); 31 : public UsbService,
39 } 32 private base::MessageLoop::DestructionObserver {
40 return instance; 33 public:
41 } 34 explicit UsbServiceImpl(PlatformUsbContext context);
35 virtual ~UsbServiceImpl();
42 36
43 scoped_refptr<UsbDevice> UsbService::GetDeviceById(uint32 unique_id) { 37 private:
38 // usb_service::UsbService implementation
39 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE;
40 virtual void GetDevices(
41 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE;
42
43 // base::MessageLoop::DestructionObserver implementation.
44 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
45
46 // Enumerate USB devices from OS and Update devices_ map.
47 void RefreshDevices();
48
49 scoped_refptr<UsbContext> context_;
50
51 // TODO(ikarienator): Figure out a better solution.
52 uint32 next_unique_id_;
53
54 // The map from PlatformUsbDevices to UsbDevices.
55 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap;
56 DeviceMap devices_;
57
58 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl);
59 };
60
61 scoped_refptr<UsbDevice> UsbServiceImpl::GetDeviceById(uint32 unique_id) {
44 DCHECK(CalledOnValidThread()); 62 DCHECK(CalledOnValidThread());
45 RefreshDevices(); 63 RefreshDevices();
46 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { 64 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
47 if (it->second->unique_id() == unique_id) 65 if (it->second->unique_id() == unique_id)
48 return it->second; 66 return it->second;
49 } 67 }
50 return NULL; 68 return NULL;
51 } 69 }
52 70
53 void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) { 71 void UsbServiceImpl::GetDevices(
72 std::vector<scoped_refptr<UsbDevice> >* devices) {
54 DCHECK(CalledOnValidThread()); 73 DCHECK(CalledOnValidThread());
55 STLClearObject(devices); 74 STLClearObject(devices);
56 RefreshDevices(); 75 RefreshDevices();
57 76
58 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { 77 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
59 devices->push_back(it->second); 78 devices->push_back(it->second);
60 } 79 }
61 } 80 }
62 81
63 void UsbService::WillDestroyCurrentMessageLoop() { 82 void UsbServiceImpl::WillDestroyCurrentMessageLoop() {
64 DCHECK(CalledOnValidThread()); 83 DCHECK(CalledOnValidThread());
65 g_usb_service_instance.Get().reset(NULL); 84 g_usb_service_instance.Get().reset(NULL);
66 } 85 }
67 86
68 UsbService::UsbService(PlatformUsbContext context) 87 UsbServiceImpl::UsbServiceImpl(PlatformUsbContext context)
69 : context_(new UsbContext(context)), next_unique_id_(0) { 88 : context_(new UsbContext(context)), next_unique_id_(0) {
70 base::MessageLoop::current()->AddDestructionObserver(this); 89 base::MessageLoop::current()->AddDestructionObserver(this);
71 } 90 }
72 91
73 UsbService::~UsbService() { 92 UsbServiceImpl::~UsbServiceImpl() {
74 base::MessageLoop::current()->RemoveDestructionObserver(this); 93 base::MessageLoop::current()->RemoveDestructionObserver(this);
75 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { 94 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
76 it->second->OnDisconnect(); 95 it->second->OnDisconnect();
77 } 96 }
78 } 97 }
79 98
80 void UsbService::RefreshDevices() { 99 void UsbServiceImpl::RefreshDevices() {
81 DCHECK(CalledOnValidThread()); 100 DCHECK(CalledOnValidThread());
82 101
83 libusb_device** platform_devices = NULL; 102 libusb_device** platform_devices = NULL;
84 const ssize_t device_count = 103 const ssize_t device_count =
85 libusb_get_device_list(context_->context(), &platform_devices); 104 libusb_get_device_list(context_->context(), &platform_devices);
86 105
87 std::set<UsbDevice*> connected_devices; 106 std::set<UsbDevice*> connected_devices;
88 std::vector<PlatformUsbDevice> disconnected_devices; 107 std::vector<PlatformUsbDevice> disconnected_devices;
89 108
90 // Populates new devices. 109 // Populates new devices.
(...skipping 25 matching lines...) Expand all
116 // Remove disconnected devices from devices_. 135 // Remove disconnected devices from devices_.
117 for (size_t i = 0; i < disconnected_devices.size(); ++i) { 136 for (size_t i = 0; i < disconnected_devices.size(); ++i) {
118 // UsbDevice will be destroyed after this. The corresponding 137 // UsbDevice will be destroyed after this. The corresponding
119 // PlatformUsbDevice will be unref'ed during this process. 138 // PlatformUsbDevice will be unref'ed during this process.
120 devices_.erase(disconnected_devices[i]); 139 devices_.erase(disconnected_devices[i]);
121 } 140 }
122 141
123 libusb_free_device_list(platform_devices, true); 142 libusb_free_device_list(platform_devices, true);
124 } 143 }
125 144
145 // static
146 UsbService* UsbService::GetInstance() {
147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
148 UsbService* instance = g_usb_service_instance.Get().get();
149 if (!instance) {
150 PlatformUsbContext context = NULL;
151 if (libusb_init(&context) != LIBUSB_SUCCESS)
152 return NULL;
153 if (!context)
154 return NULL;
155
156 instance = new UsbServiceImpl(context);
157 g_usb_service_instance.Get().reset(instance);
158 }
159 return instance;
160 }
161
162 // static
163 void UsbService::SetInstanceForTest(UsbService* instance) {
164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
165 g_usb_service_instance.Get().reset(instance);
166 }
167
126 } // namespace usb_service 168 } // namespace usb_service
OLDNEW
« no previous file with comments | « components/usb_service/usb_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698