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 "device/usb/usb_device_impl.h" | 5 #include "device/usb/usb_device_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
16 #include "components/device_event_log/device_event_log.h" | 16 #include "components/device_event_log/device_event_log.h" |
17 #include "device/usb/usb_context.h" | 17 #include "device/usb/usb_context.h" |
18 #include "device/usb/usb_descriptors.h" | 18 #include "device/usb/usb_descriptors.h" |
19 #include "device/usb/usb_device_handle_impl.h" | 19 #include "device/usb/usb_device_handle_impl.h" |
20 #include "device/usb/usb_error.h" | 20 #include "device/usb/usb_error.h" |
21 #include "third_party/libusb/src/libusb/libusb.h" | 21 #include "third_party/libusb/src/libusb/libusb.h" |
22 | 22 |
23 #if defined(OS_CHROMEOS) | 23 #if defined(OS_CHROMEOS) |
24 #include "base/sys_info.h" | |
25 #include "chromeos/dbus/dbus_thread_manager.h" | 24 #include "chromeos/dbus/dbus_thread_manager.h" |
26 #include "chromeos/dbus/permission_broker_client.h" | 25 #include "chromeos/dbus/permission_broker_client.h" |
27 #endif // defined(OS_CHROMEOS) | 26 #endif // defined(OS_CHROMEOS) |
28 | 27 |
29 #if defined(USE_UDEV) | 28 #if defined(USE_UDEV) |
30 #include "device/udev_linux/scoped_udev.h" | 29 #include "device/udev_linux/scoped_udev.h" |
31 #endif // defined(USE_UDEV) | 30 #endif // defined(USE_UDEV) |
32 | 31 |
33 namespace device { | 32 namespace device { |
34 | 33 |
35 namespace { | 34 namespace { |
36 | 35 |
37 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
38 void OnRequestUsbAccessReplied( | 37 |
| 38 void PostResultOnTaskRunner( |
39 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 39 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
40 const base::Callback<void(bool success)>& callback, | 40 const base::Callback<void(bool success)>& callback, |
41 bool success) { | 41 bool success) { |
42 task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); | 42 task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); |
43 } | 43 } |
| 44 |
44 #endif // defined(OS_CHROMEOS) | 45 #endif // defined(OS_CHROMEOS) |
45 | 46 |
46 UsbEndpointDirection GetDirection( | 47 UsbEndpointDirection GetDirection( |
47 const libusb_endpoint_descriptor* descriptor) { | 48 const libusb_endpoint_descriptor* descriptor) { |
48 switch (descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) { | 49 switch (descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) { |
49 case LIBUSB_ENDPOINT_IN: | 50 case LIBUSB_ENDPOINT_IN: |
50 return USB_DIRECTION_INBOUND; | 51 return USB_DIRECTION_INBOUND; |
51 case LIBUSB_ENDPOINT_OUT: | 52 case LIBUSB_ENDPOINT_OUT: |
52 return USB_DIRECTION_OUTBOUND; | 53 return USB_DIRECTION_OUTBOUND; |
53 default: | 54 default: |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 #endif | 173 #endif |
173 } | 174 } |
174 | 175 |
175 UsbDeviceImpl::~UsbDeviceImpl() { | 176 UsbDeviceImpl::~UsbDeviceImpl() { |
176 // The destructor must be safe to call from any thread. | 177 // The destructor must be safe to call from any thread. |
177 libusb_unref_device(platform_device_); | 178 libusb_unref_device(platform_device_); |
178 } | 179 } |
179 | 180 |
180 #if defined(OS_CHROMEOS) | 181 #if defined(OS_CHROMEOS) |
181 | 182 |
182 void UsbDeviceImpl::RequestUsbAccess( | 183 void UsbDeviceImpl::CheckUsbAccess(const ResultCallback& callback) { |
183 int interface_id, | |
184 const base::Callback<void(bool success)>& callback) { | |
185 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
186 | 185 |
187 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to | 186 chromeos::PermissionBrokerClient* client = |
188 // use permission broker. | 187 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
189 if (base::SysInfo::IsRunningOnChromeOS()) { | 188 DCHECK(client) << "Could not get permission broker client."; |
190 chromeos::PermissionBrokerClient* client = | |
191 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | |
192 DCHECK(client) << "Could not get permission broker client."; | |
193 if (!client) { | |
194 callback.Run(false); | |
195 return; | |
196 } | |
197 | 189 |
198 ui_task_runner_->PostTask( | 190 ui_task_runner_->PostTask( |
199 FROM_HERE, | 191 FROM_HERE, |
200 base::Bind(&chromeos::PermissionBrokerClient::RequestPathAccess, | 192 base::Bind(&chromeos::PermissionBrokerClient::CheckPathAccess, |
201 base::Unretained(client), | 193 base::Unretained(client), devnode_, |
202 devnode_, | 194 base::Bind(&PostResultOnTaskRunner, |
203 interface_id, | 195 base::ThreadTaskRunnerHandle::Get(), callback))); |
204 base::Bind(&OnRequestUsbAccessReplied, | 196 } |
205 base::ThreadTaskRunnerHandle::Get(), | 197 |
206 callback))); | 198 void UsbDeviceImpl::RequestUsbAccess(int interface_id, |
207 } else { | 199 const ResultCallback& callback) { |
208 // Not really running on Chrome OS, declare success. | 200 DCHECK(thread_checker_.CalledOnValidThread()); |
209 callback.Run(true); | 201 |
210 } | 202 chromeos::PermissionBrokerClient* client = |
| 203 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 204 DCHECK(client) << "Could not get permission broker client."; |
| 205 |
| 206 ui_task_runner_->PostTask( |
| 207 FROM_HERE, |
| 208 base::Bind(&chromeos::PermissionBrokerClient::RequestPathAccess, |
| 209 base::Unretained(client), devnode_, interface_id, |
| 210 base::Bind(&PostResultOnTaskRunner, |
| 211 base::ThreadTaskRunnerHandle::Get(), callback))); |
211 } | 212 } |
212 | 213 |
213 #endif | 214 #endif |
214 | 215 |
215 scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { | 216 scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { |
216 DCHECK(thread_checker_.CalledOnValidThread()); | 217 DCHECK(thread_checker_.CalledOnValidThread()); |
217 PlatformUsbDeviceHandle handle; | 218 PlatformUsbDeviceHandle handle; |
218 const int rv = libusb_open(platform_device_, &handle); | 219 const int rv = libusb_open(platform_device_, &handle); |
219 if (LIBUSB_SUCCESS == rv) { | 220 if (LIBUSB_SUCCESS == rv) { |
220 scoped_refptr<UsbDeviceHandleImpl> device_handle = | 221 scoped_refptr<UsbDeviceHandleImpl> device_handle = |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 } else { | 390 } else { |
390 USB_LOG(EVENT) | 391 USB_LOG(EVENT) |
391 << "Failed to read device descriptor to cache string descriptors: " | 392 << "Failed to read device descriptor to cache string descriptors: " |
392 << ConvertPlatformUsbErrorToString(rv); | 393 << ConvertPlatformUsbErrorToString(rv); |
393 } | 394 } |
394 strings_cached_ = true; | 395 strings_cached_ = true; |
395 } | 396 } |
396 #endif // !defined(USE_UDEV) | 397 #endif // !defined(USE_UDEV) |
397 | 398 |
398 } // namespace device | 399 } // namespace device |
OLD | NEW |