| OLD | NEW |
| 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 <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/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "components/usb_service/usb_context.h" | 13 #include "components/usb_service/usb_context.h" |
| 14 #include "components/usb_service/usb_device_impl.h" | 14 #include "components/usb_service/usb_device_impl.h" |
| 15 #include "components/usb_service/usb_error.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 #include "third_party/libusb/src/libusb/libusb.h" | 17 #include "third_party/libusb/src/libusb/libusb.h" |
| 17 | 18 |
| 18 namespace usb_service { | 19 namespace usb_service { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance = | 23 base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance = |
| 23 LAZY_INSTANCE_INITIALIZER; | 24 LAZY_INSTANCE_INITIALIZER; |
| 24 | 25 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 it->second->OnDisconnect(); | 96 it->second->OnDisconnect(); |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 | 99 |
| 99 void UsbServiceImpl::RefreshDevices() { | 100 void UsbServiceImpl::RefreshDevices() { |
| 100 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
| 101 | 102 |
| 102 libusb_device** platform_devices = NULL; | 103 libusb_device** platform_devices = NULL; |
| 103 const ssize_t device_count = | 104 const ssize_t device_count = |
| 104 libusb_get_device_list(context_->context(), &platform_devices); | 105 libusb_get_device_list(context_->context(), &platform_devices); |
| 106 if (device_count < 0) { |
| 107 LOG(ERROR) << "Failed to get device list: " << |
| 108 ConvertErrorToString(device_count); |
| 109 } |
| 105 | 110 |
| 106 std::set<UsbDevice*> connected_devices; | 111 std::set<UsbDevice*> connected_devices; |
| 107 std::vector<PlatformUsbDevice> disconnected_devices; | 112 std::vector<PlatformUsbDevice> disconnected_devices; |
| 108 | 113 |
| 109 // Populates new devices. | 114 // Populates new devices. |
| 110 for (ssize_t i = 0; i < device_count; ++i) { | 115 for (ssize_t i = 0; i < device_count; ++i) { |
| 111 if (!ContainsKey(devices_, platform_devices[i])) { | 116 if (!ContainsKey(devices_, platform_devices[i])) { |
| 112 libusb_device_descriptor descriptor; | 117 libusb_device_descriptor descriptor; |
| 118 const int rv = |
| 119 libusb_get_device_descriptor(platform_devices[i], &descriptor); |
| 113 // This test is needed. A valid vendor/produce pair is required. | 120 // This test is needed. A valid vendor/produce pair is required. |
| 114 if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor)) | 121 if (rv != LIBUSB_SUCCESS) { |
| 122 LOG(WARNING) << "Failed to get device descriptor: " |
| 123 << ConvertErrorToString(rv); |
| 115 continue; | 124 continue; |
| 125 } |
| 116 UsbDeviceImpl* new_device = new UsbDeviceImpl(context_, | 126 UsbDeviceImpl* new_device = new UsbDeviceImpl(context_, |
| 117 platform_devices[i], | 127 platform_devices[i], |
| 118 descriptor.idVendor, | 128 descriptor.idVendor, |
| 119 descriptor.idProduct, | 129 descriptor.idProduct, |
| 120 ++next_unique_id_); | 130 ++next_unique_id_); |
| 121 devices_[platform_devices[i]] = new_device; | 131 devices_[platform_devices[i]] = new_device; |
| 122 connected_devices.insert(new_device); | 132 connected_devices.insert(new_device); |
| 123 } else { | 133 } else { |
| 124 connected_devices.insert(devices_[platform_devices[i]].get()); | 134 connected_devices.insert(devices_[platform_devices[i]].get()); |
| 125 } | 135 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 141 | 151 |
| 142 libusb_free_device_list(platform_devices, true); | 152 libusb_free_device_list(platform_devices, true); |
| 143 } | 153 } |
| 144 | 154 |
| 145 // static | 155 // static |
| 146 UsbService* UsbService::GetInstance() { | 156 UsbService* UsbService::GetInstance() { |
| 147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 157 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 148 UsbService* instance = g_usb_service_instance.Get().get(); | 158 UsbService* instance = g_usb_service_instance.Get().get(); |
| 149 if (!instance) { | 159 if (!instance) { |
| 150 PlatformUsbContext context = NULL; | 160 PlatformUsbContext context = NULL; |
| 151 if (libusb_init(&context) != LIBUSB_SUCCESS) | 161 |
| 162 const int rv = libusb_init(&context); |
| 163 if (rv != LIBUSB_SUCCESS) { |
| 164 LOG(ERROR) << "Failed to initialize libusb: " << ConvertErrorToString(rv); |
| 152 return NULL; | 165 return NULL; |
| 166 } |
| 153 if (!context) | 167 if (!context) |
| 154 return NULL; | 168 return NULL; |
| 155 | 169 |
| 156 instance = new UsbServiceImpl(context); | 170 instance = new UsbServiceImpl(context); |
| 157 g_usb_service_instance.Get().reset(instance); | 171 g_usb_service_instance.Get().reset(instance); |
| 158 } | 172 } |
| 159 return instance; | 173 return instance; |
| 160 } | 174 } |
| 161 | 175 |
| 162 // static | 176 // static |
| 163 void UsbService::SetInstanceForTest(UsbService* instance) { | 177 void UsbService::SetInstanceForTest(UsbService* instance) { |
| 164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 178 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 165 g_usb_service_instance.Get().reset(instance); | 179 g_usb_service_instance.Get().reset(instance); |
| 166 } | 180 } |
| 167 | 181 |
| 168 } // namespace usb_service | 182 } // namespace usb_service |
| OLD | NEW |