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

Side by Side Diff: device/usb/usb_service_impl.cc

Issue 497363004: Merge components/usb_service into device/usb. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 6 years, 3 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
« no previous file with comments | « device/usb/usb_service.h ('k') | device/usb/usb_service_unittest.cc » ('j') | 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 "device/usb/usb_service.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
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/message_loop/message_loop.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "components/usb_service/usb_context.h" 14 #include "device/usb/usb_context.h"
15 #include "components/usb_service/usb_device_impl.h" 15 #include "device/usb/usb_device_impl.h"
16 #include "components/usb_service/usb_error.h" 16 #include "device/usb/usb_error.h"
17 #include "third_party/libusb/src/libusb/libusb.h" 17 #include "third_party/libusb/src/libusb/libusb.h"
18 18
19 namespace usb_service { 19 namespace device {
20 20
21 namespace { 21 namespace {
22 22
23 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance = 23 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance =
24 LAZY_INSTANCE_INITIALIZER; 24 LAZY_INSTANCE_INITIALIZER;
25 25
26 } // namespace 26 } // namespace
27 27
28 typedef struct libusb_device* PlatformUsbDevice; 28 typedef struct libusb_device* PlatformUsbDevice;
29 typedef struct libusb_context* PlatformUsbContext; 29 typedef struct libusb_context* PlatformUsbContext;
30 30
31 class UsbServiceImpl 31 class UsbServiceImpl : public UsbService,
32 : public UsbService, 32 private base::MessageLoop::DestructionObserver {
33 private base::MessageLoop::DestructionObserver {
34 public: 33 public:
35 explicit UsbServiceImpl( 34 explicit UsbServiceImpl(
36 PlatformUsbContext context, 35 PlatformUsbContext context,
37 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); 36 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
38 virtual ~UsbServiceImpl(); 37 virtual ~UsbServiceImpl();
39 38
40 private: 39 private:
41 // usb_service::UsbService implementation 40 // device::UsbService implementation
42 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE; 41 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE;
43 virtual void GetDevices( 42 virtual void GetDevices(
44 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE; 43 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE;
45 44
46 // base::MessageLoop::DestructionObserver implementation. 45 // base::MessageLoop::DestructionObserver implementation.
47 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; 46 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
48 47
49 // Enumerate USB devices from OS and Update devices_ map. 48 // Enumerate USB devices from OS and Update devices_ map.
50 void RefreshDevices(); 49 void RefreshDevices();
51 50
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 } 105 }
107 106
108 void UsbServiceImpl::RefreshDevices() { 107 void UsbServiceImpl::RefreshDevices() {
109 DCHECK(CalledOnValidThread()); 108 DCHECK(CalledOnValidThread());
110 109
111 libusb_device** platform_devices = NULL; 110 libusb_device** platform_devices = NULL;
112 const ssize_t device_count = 111 const ssize_t device_count =
113 libusb_get_device_list(context_->context(), &platform_devices); 112 libusb_get_device_list(context_->context(), &platform_devices);
114 if (device_count < 0) { 113 if (device_count < 0) {
115 VLOG(1) << "Failed to get device list: " 114 VLOG(1) << "Failed to get device list: "
116 << ConvertErrorToString(device_count); 115 << ConvertPlatformUsbErrorToString(device_count);
117 } 116 }
118 117
119 std::set<UsbDevice*> connected_devices; 118 std::set<UsbDevice*> connected_devices;
120 std::vector<PlatformUsbDevice> disconnected_devices; 119 std::vector<PlatformUsbDevice> disconnected_devices;
121 120
122 // Populates new devices. 121 // Populates new devices.
123 for (ssize_t i = 0; i < device_count; ++i) { 122 for (ssize_t i = 0; i < device_count; ++i) {
124 if (!ContainsKey(devices_, platform_devices[i])) { 123 if (!ContainsKey(devices_, platform_devices[i])) {
125 libusb_device_descriptor descriptor; 124 libusb_device_descriptor descriptor;
126 const int rv = 125 const int rv =
127 libusb_get_device_descriptor(platform_devices[i], &descriptor); 126 libusb_get_device_descriptor(platform_devices[i], &descriptor);
128 // This test is needed. A valid vendor/produce pair is required. 127 // This test is needed. A valid vendor/produce pair is required.
129 if (rv != LIBUSB_SUCCESS) { 128 if (rv != LIBUSB_SUCCESS) {
130 VLOG(1) << "Failed to get device descriptor: " 129 VLOG(1) << "Failed to get device descriptor: "
131 << ConvertErrorToString(rv); 130 << ConvertPlatformUsbErrorToString(rv);
132 continue; 131 continue;
133 } 132 }
134 UsbDeviceImpl* new_device = new UsbDeviceImpl(context_, 133 UsbDeviceImpl* new_device = new UsbDeviceImpl(context_,
135 ui_task_runner_, 134 ui_task_runner_,
136 platform_devices[i], 135 platform_devices[i],
137 descriptor.idVendor, 136 descriptor.idVendor,
138 descriptor.idProduct, 137 descriptor.idProduct,
139 ++next_unique_id_); 138 ++next_unique_id_);
140 devices_[platform_devices[i]] = new_device; 139 devices_[platform_devices[i]] = new_device;
141 connected_devices.insert(new_device); 140 connected_devices.insert(new_device);
(...skipping 21 matching lines...) Expand all
163 162
164 // static 163 // static
165 UsbService* UsbService::GetInstance( 164 UsbService* UsbService::GetInstance(
166 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { 165 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
167 UsbService* instance = g_usb_service_instance.Get().get(); 166 UsbService* instance = g_usb_service_instance.Get().get();
168 if (!instance) { 167 if (!instance) {
169 PlatformUsbContext context = NULL; 168 PlatformUsbContext context = NULL;
170 169
171 const int rv = libusb_init(&context); 170 const int rv = libusb_init(&context);
172 if (rv != LIBUSB_SUCCESS) { 171 if (rv != LIBUSB_SUCCESS) {
173 VLOG(1) << "Failed to initialize libusb: " << ConvertErrorToString(rv); 172 VLOG(1) << "Failed to initialize libusb: "
173 << ConvertPlatformUsbErrorToString(rv);
174 return NULL; 174 return NULL;
175 } 175 }
176 if (!context) 176 if (!context)
177 return NULL; 177 return NULL;
178 178
179 instance = new UsbServiceImpl(context, ui_task_runner); 179 instance = new UsbServiceImpl(context, ui_task_runner);
180 g_usb_service_instance.Get().reset(instance); 180 g_usb_service_instance.Get().reset(instance);
181 } 181 }
182 return instance; 182 return instance;
183 } 183 }
184 184
185 // static 185 // static
186 void UsbService::SetInstanceForTest(UsbService* instance) { 186 void UsbService::SetInstanceForTest(UsbService* instance) {
187 g_usb_service_instance.Get().reset(instance); 187 g_usb_service_instance.Get().reset(instance);
188 } 188 }
189 189
190 } // namespace usb_service 190 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/usb_service.h ('k') | device/usb/usb_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698