Using pre-existing constants instead of hard-coding
[chromium-blink-merge.git] / components / usb_service / usb_device_impl.cc
blob43c8c66fbd43ad92dcb0cbaa2f6b481315eeded4
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
3 // found in the LICENSE file.
5 #include "components/usb_service/usb_device_impl.h"
7 #include <algorithm>
9 #include "base/stl_util.h"
10 #include "components/usb_service/usb_context.h"
11 #include "components/usb_service/usb_device_handle_impl.h"
12 #include "components/usb_service/usb_interface_impl.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "third_party/libusb/src/libusb/libusb.h"
16 #if defined(OS_CHROMEOS)
17 #include "base/sys_info.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/dbus/permission_broker_client.h"
20 #endif // defined(OS_CHROMEOS)
22 using content::BrowserThread;
24 namespace {
26 #if defined(OS_CHROMEOS)
27 void OnRequestUsbAccessReplied(
28 const base::Callback<void(bool success)>& callback,
29 bool success) {
30 BrowserThread::PostTask(
31 BrowserThread::FILE, FROM_HERE, base::Bind(callback, success));
33 #endif // defined(OS_CHROMEOS)
35 } // namespace
37 namespace usb_service {
39 UsbDeviceImpl::UsbDeviceImpl(scoped_refptr<UsbContext> context,
40 PlatformUsbDevice platform_device,
41 uint16 vendor_id,
42 uint16 product_id,
43 uint32 unique_id)
44 : UsbDevice(vendor_id, product_id, unique_id),
45 platform_device_(platform_device),
46 context_(context) {
47 CHECK(platform_device) << "platform_device cannot be NULL";
48 libusb_ref_device(platform_device);
51 UsbDeviceImpl::~UsbDeviceImpl() {
52 DCHECK(thread_checker_.CalledOnValidThread());
53 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end();
54 ++it) {
55 (*it)->InternalClose();
57 STLClearObject(&handles_);
58 libusb_unref_device(platform_device_);
61 #if defined(OS_CHROMEOS)
63 void UsbDeviceImpl::RequestUsbAcess(
64 int interface_id,
65 const base::Callback<void(bool success)>& callback) {
66 DCHECK(thread_checker_.CalledOnValidThread());
68 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to
69 // use permission broker.
70 if (base::SysInfo::IsRunningOnChromeOS()) {
71 chromeos::PermissionBrokerClient* client =
72 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
73 DCHECK(client) << "Could not get permission broker client.";
74 if (!client) {
75 callback.Run(false);
76 return;
79 BrowserThread::PostTask(
80 BrowserThread::UI,
81 FROM_HERE,
82 base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess,
83 base::Unretained(client),
84 vendor_id(),
85 product_id(),
86 interface_id,
87 base::Bind(&OnRequestUsbAccessReplied, callback)));
91 #endif
93 scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() {
94 DCHECK(thread_checker_.CalledOnValidThread());
95 PlatformUsbDeviceHandle handle;
96 int rv = libusb_open(platform_device_, &handle);
97 if (LIBUSB_SUCCESS == rv) {
98 scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces();
99 if (!interfaces)
100 return NULL;
101 scoped_refptr<UsbDeviceHandleImpl> device_handle =
102 new UsbDeviceHandleImpl(context_, this, handle, interfaces);
103 handles_.push_back(device_handle);
104 return device_handle;
106 return NULL;
109 bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) {
110 DCHECK(thread_checker_.CalledOnValidThread());
112 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end();
113 ++it) {
114 if (*it == handle) {
115 (*it)->InternalClose();
116 handles_.erase(it);
117 return true;
120 return false;
123 scoped_refptr<UsbConfigDescriptor> UsbDeviceImpl::ListInterfaces() {
124 DCHECK(thread_checker_.CalledOnValidThread());
126 PlatformUsbConfigDescriptor platform_config;
127 const int list_result =
128 libusb_get_active_config_descriptor(platform_device_, &platform_config);
129 if (list_result == 0)
130 return new UsbConfigDescriptorImpl(platform_config);
132 return NULL;
135 void UsbDeviceImpl::OnDisconnect() {
136 DCHECK(thread_checker_.CalledOnValidThread());
137 HandlesVector handles;
138 swap(handles, handles_);
139 for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it)
140 (*it)->InternalClose();
143 } // namespace usb_service