| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/usb/usb_device.h" | 5 #include "components/usb_service/usb_device.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "chrome/browser/usb/usb_context.h" | 10 #include "components/usb_service/usb_context.h" |
| 11 #include "chrome/browser/usb/usb_device_handle.h" | 11 #include "components/usb_service/usb_device_handle.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "third_party/libusb/src/libusb/libusb.h" | 13 #include "third_party/libusb/src/libusb/libusb.h" |
| 14 | 14 |
| 15 #if defined(OS_CHROMEOS) | 15 #if defined(OS_CHROMEOS) |
| 16 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" | 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/dbus/permission_broker_client.h" | 18 #include "chromeos/dbus/permission_broker_client.h" |
| 19 #endif // defined(OS_CHROMEOS) | 19 #endif // defined(OS_CHROMEOS) |
| 20 | 20 |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 #if defined(OS_CHROMEOS) | 25 #if defined(OS_CHROMEOS) |
| 26 void OnRequestUsbAccessReplied( | 26 void OnRequestUsbAccessReplied( |
| 27 const base::Callback<void(bool success)>& callback, | 27 const base::Callback<void(bool success)>& callback, |
| 28 bool success) { | 28 bool success) { |
| 29 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 29 BrowserThread::PostTask( |
| 30 base::Bind(callback, success)); | 30 BrowserThread::FILE, FROM_HERE, base::Bind(callback, success)); |
| 31 } | 31 } |
| 32 #endif // defined(OS_CHROMEOS) | 32 #endif // defined(OS_CHROMEOS) |
| 33 | 33 |
| 34 } // namespace | 34 } // namespace |
| 35 | 35 |
| 36 UsbDevice::UsbDevice( | 36 namespace usb_service { |
| 37 scoped_refptr<UsbContext> context, | 37 |
| 38 PlatformUsbDevice platform_device, | 38 UsbDevice::UsbDevice(scoped_refptr<UsbContext> context, |
| 39 uint16 vendor_id, | 39 PlatformUsbDevice platform_device, |
| 40 uint16 product_id, | 40 uint16 vendor_id, |
| 41 uint32 unique_id) | 41 uint16 product_id, |
| 42 uint32 unique_id) |
| 42 : platform_device_(platform_device), | 43 : platform_device_(platform_device), |
| 43 vendor_id_(vendor_id), | 44 vendor_id_(vendor_id), |
| 44 product_id_(product_id), | 45 product_id_(product_id), |
| 45 unique_id_(unique_id), | 46 unique_id_(unique_id), |
| 46 context_(context) { | 47 context_(context) { |
| 47 CHECK(platform_device) << "platform_device cannot be NULL"; | 48 CHECK(platform_device) << "platform_device cannot be NULL"; |
| 48 libusb_ref_device(platform_device); | 49 libusb_ref_device(platform_device); |
| 49 } | 50 } |
| 50 | 51 |
| 51 UsbDevice::UsbDevice() | 52 UsbDevice::UsbDevice() |
| 52 : platform_device_(NULL), | 53 : platform_device_(NULL), |
| 53 vendor_id_(0), | 54 vendor_id_(0), |
| 54 product_id_(0), | 55 product_id_(0), |
| 55 unique_id_(0), | 56 unique_id_(0), |
| 56 context_(NULL) { | 57 context_(NULL) { |
| 57 } | 58 } |
| 58 | 59 |
| 59 UsbDevice::~UsbDevice() { | 60 UsbDevice::~UsbDevice() { |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | 61 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 for (HandlesVector::iterator it = handles_.begin(); | 62 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); |
| 62 it != handles_.end(); | 63 ++it) { |
| 63 ++it) { | |
| 64 (*it)->InternalClose(); | 64 (*it)->InternalClose(); |
| 65 } | 65 } |
| 66 STLClearObject(&handles_); | 66 STLClearObject(&handles_); |
| 67 libusb_unref_device(platform_device_); | 67 libusb_unref_device(platform_device_); |
| 68 } | 68 } |
| 69 | 69 |
| 70 #if defined(OS_CHROMEOS) | 70 #if defined(OS_CHROMEOS) |
| 71 | 71 |
| 72 void UsbDevice::RequestUsbAcess( | 72 void UsbDevice::RequestUsbAcess( |
| 73 int interface_id, | 73 int interface_id, |
| 74 const base::Callback<void(bool success)>& callback) { | 74 const base::Callback<void(bool success)>& callback) { |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); | 75 DCHECK(thread_checker_.CalledOnValidThread()); |
| 76 | 76 |
| 77 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to | 77 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to |
| 78 // use permission broker. | 78 // use permission broker. |
| 79 if (base::SysInfo::IsRunningOnChromeOS()) { | 79 if (base::SysInfo::IsRunningOnChromeOS()) { |
| 80 chromeos::PermissionBrokerClient* client = | 80 chromeos::PermissionBrokerClient* client = |
| 81 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | 81 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 82 DCHECK(client) << "Could not get permission broker client."; | 82 DCHECK(client) << "Could not get permission broker client."; |
| 83 if (!client) { | 83 if (!client) { |
| 84 callback.Run(false); | 84 callback.Run(false); |
| 85 return; | 85 return; |
| 86 } | 86 } |
| 87 | 87 |
| 88 BrowserThread::PostTask( | 88 BrowserThread::PostTask( |
| 89 BrowserThread::UI, FROM_HERE, | 89 BrowserThread::UI, |
| 90 FROM_HERE, |
| 90 base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess, | 91 base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess, |
| 91 base::Unretained(client), | 92 base::Unretained(client), |
| 92 this->vendor_id_, | 93 this->vendor_id_, |
| 93 this->product_id_, | 94 this->product_id_, |
| 94 interface_id, | 95 interface_id, |
| 95 base::Bind(&OnRequestUsbAccessReplied, callback))); | 96 base::Bind(&OnRequestUsbAccessReplied, callback))); |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 | 99 |
| 99 #endif | 100 #endif |
| (...skipping 10 matching lines...) Expand all Loading... |
| 110 new UsbDeviceHandle(context_, this, handle, interfaces); | 111 new UsbDeviceHandle(context_, this, handle, interfaces); |
| 111 handles_.push_back(device_handle); | 112 handles_.push_back(device_handle); |
| 112 return device_handle; | 113 return device_handle; |
| 113 } | 114 } |
| 114 return NULL; | 115 return NULL; |
| 115 } | 116 } |
| 116 | 117 |
| 117 bool UsbDevice::Close(scoped_refptr<UsbDeviceHandle> handle) { | 118 bool UsbDevice::Close(scoped_refptr<UsbDeviceHandle> handle) { |
| 118 DCHECK(thread_checker_.CalledOnValidThread()); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
| 119 | 120 |
| 120 for (HandlesVector::iterator it = handles_.begin(); | 121 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); |
| 121 it != handles_.end(); | 122 ++it) { |
| 122 ++it) { | |
| 123 if (*it == handle) { | 123 if (*it == handle) { |
| 124 (*it)->InternalClose(); | 124 (*it)->InternalClose(); |
| 125 handles_.erase(it); | 125 handles_.erase(it); |
| 126 return true; | 126 return true; |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 return false; | 129 return false; |
| 130 } | 130 } |
| 131 | 131 |
| 132 scoped_refptr<UsbConfigDescriptor> UsbDevice::ListInterfaces() { | 132 scoped_refptr<UsbConfigDescriptor> UsbDevice::ListInterfaces() { |
| 133 DCHECK(thread_checker_.CalledOnValidThread()); | 133 DCHECK(thread_checker_.CalledOnValidThread()); |
| 134 | 134 |
| 135 PlatformUsbConfigDescriptor platform_config; | 135 PlatformUsbConfigDescriptor platform_config; |
| 136 const int list_result = | 136 const int list_result = |
| 137 libusb_get_active_config_descriptor(platform_device_, &platform_config); | 137 libusb_get_active_config_descriptor(platform_device_, &platform_config); |
| 138 if (list_result == 0) | 138 if (list_result == 0) |
| 139 return new UsbConfigDescriptor(platform_config); | 139 return new UsbConfigDescriptor(platform_config); |
| 140 | 140 |
| 141 return NULL; | 141 return NULL; |
| 142 } | 142 } |
| 143 | 143 |
| 144 void UsbDevice::OnDisconnect() { | 144 void UsbDevice::OnDisconnect() { |
| 145 DCHECK(thread_checker_.CalledOnValidThread()); | 145 DCHECK(thread_checker_.CalledOnValidThread()); |
| 146 HandlesVector handles; | 146 HandlesVector handles; |
| 147 swap(handles, handles_); | 147 swap(handles, handles_); |
| 148 for (std::vector<scoped_refptr<UsbDeviceHandle> >::iterator it = | 148 for (std::vector<scoped_refptr<UsbDeviceHandle> >::iterator it = |
| 149 handles.begin(); | 149 handles.begin(); |
| 150 it != handles.end(); | 150 it != handles.end(); |
| 151 ++it) { | 151 ++it) { |
| 152 (*it)->InternalClose(); | 152 (*it)->InternalClose(); |
| 153 } | 153 } |
| 154 } | 154 } |
| 155 |
| 156 } // namespace usb_service |
| OLD | NEW |