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 "chrome/browser/usb/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 "chrome/browser/usb/usb_context.h" |
11 #include "chrome/browser/usb/usb_device_handle.h" | 11 #include "chrome/browser/usb/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) |
| 16 #include "base/chromeos/chromeos_version.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/dbus/permission_broker_client.h" |
| 19 #endif // defined(OS_CHROMEOS) |
| 20 |
15 using content::BrowserThread; | 21 using content::BrowserThread; |
16 | 22 |
17 UsbDevice::UsbDevice( | 23 UsbDevice::UsbDevice( |
18 scoped_refptr<UsbContext> context, | 24 scoped_refptr<UsbContext> context, |
19 PlatformUsbDevice platform_device, | 25 PlatformUsbDevice platform_device, |
20 uint16 vendor_id, | 26 uint16 vendor_id, |
21 uint16 product_id) | 27 uint16 product_id, |
| 28 uint32 unique_id) |
22 : platform_device_(platform_device), | 29 : platform_device_(platform_device), |
23 vendor_id_(vendor_id), | 30 vendor_id_(vendor_id), |
24 product_id_(product_id), | 31 product_id_(product_id), |
| 32 unique_id_(unique_id), |
25 context_(context) { | 33 context_(context) { |
26 CHECK(platform_device) << "platform_device cannot be NULL"; | 34 CHECK(platform_device) << "platform_device cannot be NULL"; |
27 libusb_ref_device(platform_device); | 35 libusb_ref_device(platform_device); |
28 } | 36 } |
29 | 37 |
30 UsbDevice::UsbDevice() | 38 UsbDevice::UsbDevice() |
31 : platform_device_(NULL), | 39 : platform_device_(NULL), |
32 vendor_id_(0), | 40 vendor_id_(0), |
33 product_id_(0), | 41 product_id_(0), |
| 42 unique_id_(0), |
34 context_(NULL) { | 43 context_(NULL) { |
35 } | 44 } |
36 | 45 |
37 UsbDevice::~UsbDevice() { | 46 UsbDevice::~UsbDevice() { |
38 DCHECK(thread_checker_.CalledOnValidThread()); | 47 DCHECK(thread_checker_.CalledOnValidThread()); |
39 for (HandlesVector::iterator it = handles_.begin(); | 48 for (HandlesVector::iterator it = handles_.begin(); |
40 it != handles_.end(); | 49 it != handles_.end(); |
41 ++it) { | 50 ++it) { |
42 (*it)->InternalClose(); | 51 (*it)->InternalClose(); |
43 } | 52 } |
44 STLClearObject(&handles_); | 53 STLClearObject(&handles_); |
45 libusb_unref_device(platform_device_); | 54 libusb_unref_device(platform_device_); |
46 } | 55 } |
47 | 56 |
| 57 #if defined(OS_CHROMEOS) |
| 58 void UsbDevice::RequestUsbAcess( |
| 59 int interface_id, |
| 60 const base::Callback<void(bool success)>& callback) { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 |
| 63 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to |
| 64 // use permission broker. |
| 65 if (base::chromeos::IsRunningOnChromeOS()) { |
| 66 chromeos::PermissionBrokerClient* client = |
| 67 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 68 DCHECK(client) << "Could not get permission broker client."; |
| 69 if (!client) { |
| 70 callback.Run(false); |
| 71 return; |
| 72 } |
| 73 |
| 74 BrowserThread::PostTask( |
| 75 BrowserThread::UI, FROM_HERE, |
| 76 base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess, |
| 77 base::Unretained(client), |
| 78 this->vendor_id_, |
| 79 this->product_id_, |
| 80 interface_id, |
| 81 base::Bind(&UsbDevice::OnRequestUsbAccessReplied, |
| 82 base::Unretained(this), |
| 83 callback))); |
| 84 } |
| 85 } |
| 86 |
| 87 void UsbDevice::OnRequestUsbAccessReplied( |
| 88 const base::Callback<void(bool success)>& callback, |
| 89 bool success) { |
| 90 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 91 base::Bind(callback, success)); |
| 92 } |
| 93 |
| 94 #endif |
| 95 |
48 scoped_refptr<UsbDeviceHandle> UsbDevice::Open() { | 96 scoped_refptr<UsbDeviceHandle> UsbDevice::Open() { |
49 DCHECK(thread_checker_.CalledOnValidThread()); | 97 DCHECK(thread_checker_.CalledOnValidThread()); |
50 PlatformUsbDeviceHandle handle; | 98 PlatformUsbDeviceHandle handle; |
51 int rv = libusb_open(platform_device_, &handle); | 99 int rv = libusb_open(platform_device_, &handle); |
52 if (LIBUSB_SUCCESS == rv) { | 100 if (LIBUSB_SUCCESS == rv) { |
53 scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces(); | 101 scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces(); |
54 if (!interfaces) | 102 if (!interfaces) |
55 return NULL; | 103 return NULL; |
56 scoped_refptr<UsbDeviceHandle> device_handle = | 104 scoped_refptr<UsbDeviceHandle> device_handle = |
57 new UsbDeviceHandle(context_, this, handle, interfaces); | 105 new UsbDeviceHandle(context_, this, handle, interfaces); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 DCHECK(thread_checker_.CalledOnValidThread()); | 140 DCHECK(thread_checker_.CalledOnValidThread()); |
93 HandlesVector handles; | 141 HandlesVector handles; |
94 swap(handles, handles_); | 142 swap(handles, handles_); |
95 for (std::vector<scoped_refptr<UsbDeviceHandle> >::iterator it = | 143 for (std::vector<scoped_refptr<UsbDeviceHandle> >::iterator it = |
96 handles.begin(); | 144 handles.begin(); |
97 it != handles.end(); | 145 it != handles.end(); |
98 ++it) { | 146 ++it) { |
99 (*it)->InternalClose(); | 147 (*it)->InternalClose(); |
100 } | 148 } |
101 } | 149 } |
OLD | NEW |