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_device_impl.h" | 5 #include "components/usb_service/usb_device_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/location.h" |
| 11 #include "base/single_thread_task_runner.h" |
9 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/thread_task_runner_handle.h" |
10 #include "components/usb_service/usb_context.h" | 14 #include "components/usb_service/usb_context.h" |
11 #include "components/usb_service/usb_device_handle_impl.h" | 15 #include "components/usb_service/usb_device_handle_impl.h" |
12 #include "components/usb_service/usb_error.h" | 16 #include "components/usb_service/usb_error.h" |
13 #include "components/usb_service/usb_interface_impl.h" | 17 #include "components/usb_service/usb_interface_impl.h" |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "third_party/libusb/src/libusb/libusb.h" | 18 #include "third_party/libusb/src/libusb/libusb.h" |
16 | 19 |
17 #if defined(OS_CHROMEOS) | 20 #if defined(OS_CHROMEOS) |
18 #include "base/sys_info.h" | 21 #include "base/sys_info.h" |
19 #include "chromeos/dbus/dbus_thread_manager.h" | 22 #include "chromeos/dbus/dbus_thread_manager.h" |
20 #include "chromeos/dbus/permission_broker_client.h" | 23 #include "chromeos/dbus/permission_broker_client.h" |
21 #endif // defined(OS_CHROMEOS) | 24 #endif // defined(OS_CHROMEOS) |
22 | 25 |
23 using content::BrowserThread; | |
24 | |
25 namespace { | 26 namespace { |
26 | 27 |
27 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
28 void OnRequestUsbAccessReplied( | 29 void OnRequestUsbAccessReplied( |
| 30 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
29 const base::Callback<void(bool success)>& callback, | 31 const base::Callback<void(bool success)>& callback, |
30 bool success) { | 32 bool success) { |
31 BrowserThread::PostTask( | 33 task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); |
32 BrowserThread::FILE, FROM_HERE, base::Bind(callback, success)); | |
33 } | 34 } |
34 #endif // defined(OS_CHROMEOS) | 35 #endif // defined(OS_CHROMEOS) |
35 | 36 |
36 } // namespace | 37 } // namespace |
37 | 38 |
38 namespace usb_service { | 39 namespace usb_service { |
39 | 40 |
40 UsbDeviceImpl::UsbDeviceImpl(scoped_refptr<UsbContext> context, | 41 UsbDeviceImpl::UsbDeviceImpl( |
41 PlatformUsbDevice platform_device, | 42 scoped_refptr<UsbContext> context, |
42 uint16 vendor_id, | 43 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
43 uint16 product_id, | 44 PlatformUsbDevice platform_device, |
44 uint32 unique_id) | 45 uint16 vendor_id, |
| 46 uint16 product_id, |
| 47 uint32 unique_id) |
45 : UsbDevice(vendor_id, product_id, unique_id), | 48 : UsbDevice(vendor_id, product_id, unique_id), |
46 platform_device_(platform_device), | 49 platform_device_(platform_device), |
47 context_(context) { | 50 context_(context), |
| 51 ui_task_runner_(ui_task_runner) { |
48 CHECK(platform_device) << "platform_device cannot be NULL"; | 52 CHECK(platform_device) << "platform_device cannot be NULL"; |
49 libusb_ref_device(platform_device); | 53 libusb_ref_device(platform_device); |
50 } | 54 } |
51 | 55 |
52 UsbDeviceImpl::~UsbDeviceImpl() { | 56 UsbDeviceImpl::~UsbDeviceImpl() { |
53 DCHECK(thread_checker_.CalledOnValidThread()); | 57 DCHECK(thread_checker_.CalledOnValidThread()); |
54 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); | 58 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); |
55 ++it) { | 59 ++it) { |
56 (*it)->InternalClose(); | 60 (*it)->InternalClose(); |
57 } | 61 } |
(...skipping 12 matching lines...) Expand all Loading... |
70 // use permission broker. | 74 // use permission broker. |
71 if (base::SysInfo::IsRunningOnChromeOS()) { | 75 if (base::SysInfo::IsRunningOnChromeOS()) { |
72 chromeos::PermissionBrokerClient* client = | 76 chromeos::PermissionBrokerClient* client = |
73 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | 77 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
74 DCHECK(client) << "Could not get permission broker client."; | 78 DCHECK(client) << "Could not get permission broker client."; |
75 if (!client) { | 79 if (!client) { |
76 callback.Run(false); | 80 callback.Run(false); |
77 return; | 81 return; |
78 } | 82 } |
79 | 83 |
80 BrowserThread::PostTask( | 84 ui_task_runner_->PostTask( |
81 BrowserThread::UI, | |
82 FROM_HERE, | 85 FROM_HERE, |
83 base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess, | 86 base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess, |
84 base::Unretained(client), | 87 base::Unretained(client), |
85 vendor_id(), | 88 vendor_id(), |
86 product_id(), | 89 product_id(), |
87 interface_id, | 90 interface_id, |
88 base::Bind(&OnRequestUsbAccessReplied, callback))); | 91 base::Bind(&OnRequestUsbAccessReplied, |
| 92 base::ThreadTaskRunnerHandle::Get(), |
| 93 callback))); |
89 } | 94 } |
90 } | 95 } |
91 | 96 |
92 #endif | 97 #endif |
93 | 98 |
94 scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { | 99 scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { |
95 DCHECK(thread_checker_.CalledOnValidThread()); | 100 DCHECK(thread_checker_.CalledOnValidThread()); |
96 PlatformUsbDeviceHandle handle; | 101 PlatformUsbDeviceHandle handle; |
97 const int rv = libusb_open(platform_device_, &handle); | 102 const int rv = libusb_open(platform_device_, &handle); |
98 if (LIBUSB_SUCCESS == rv) { | 103 if (LIBUSB_SUCCESS == rv) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 | 144 |
140 void UsbDeviceImpl::OnDisconnect() { | 145 void UsbDeviceImpl::OnDisconnect() { |
141 DCHECK(thread_checker_.CalledOnValidThread()); | 146 DCHECK(thread_checker_.CalledOnValidThread()); |
142 HandlesVector handles; | 147 HandlesVector handles; |
143 swap(handles, handles_); | 148 swap(handles, handles_); |
144 for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it) | 149 for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it) |
145 (*it)->InternalClose(); | 150 (*it)->InternalClose(); |
146 } | 151 } |
147 | 152 |
148 } // namespace usb_service | 153 } // namespace usb_service |
OLD | NEW |