| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/usb/usb_device_resource.h" | 5 #include "chrome/browser/extensions/api/usb/usb_device_resource.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 #include "chrome/browser/extensions/api/api_resource.h" | 13 #include "chrome/browser/extensions/api/api_resource.h" |
| 14 #include "chrome/browser/usb/usb_device.h" | 14 #include "chrome/browser/usb/usb_device_handle.h" |
| 15 #include "chrome/common/extensions/api/usb.h" | 15 #include "chrome/common/extensions/api/usb.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 16 | 17 |
| 17 namespace extensions { | 18 namespace extensions { |
| 18 | 19 |
| 20 namespace { |
| 21 |
| 22 void EmptyCallback(void) {} |
| 23 |
| 24 } // namepsace |
| 25 |
| 19 UsbDeviceResource::UsbDeviceResource(const std::string& owner_extension_id, | 26 UsbDeviceResource::UsbDeviceResource(const std::string& owner_extension_id, |
| 20 scoped_refptr<UsbDevice> device) | 27 scoped_refptr<UsbDeviceHandle> device) |
| 21 : ApiResource(owner_extension_id), device_(device) {} | 28 : ApiResource(owner_extension_id), device_(device) {} |
| 22 | 29 |
| 23 UsbDeviceResource::~UsbDeviceResource() {} | 30 UsbDeviceResource::~UsbDeviceResource() { Close(base::Bind(EmptyCallback)); } |
| 31 |
| 32 void UsbDeviceResource::Close(const base::Callback<void()>& callback) { |
| 33 scoped_refptr<UsbDeviceHandle> handle; |
| 34 handle.swap(device_); |
| 35 if (!handle.get()) { |
| 36 callback.Run(); |
| 37 return; |
| 38 } |
| 39 content::BrowserThread::PostTask( |
| 40 content::BrowserThread::FILE, FROM_HERE, |
| 41 base::Bind(&UsbDeviceHandle::Close, handle, callback)); |
| 42 } |
| 43 |
| 44 void UsbDeviceResource::ListInterfaces(UsbConfigDescriptor* config, |
| 45 const UsbInterfaceCallback& callback) { |
| 46 if (!device_.get()) { |
| 47 callback.Run(false); |
| 48 } |
| 49 content::BrowserThread::PostTask( |
| 50 content::BrowserThread::FILE, FROM_HERE, |
| 51 base::Bind(&UsbDeviceHandle::ListInterfaces, device_, |
| 52 make_scoped_refptr(config), callback)); |
| 53 } |
| 54 |
| 55 void UsbDeviceResource::ClaimInterface(const int interface_number, |
| 56 const UsbInterfaceCallback& callback) { |
| 57 if (!device_.get()) { |
| 58 callback.Run(false); |
| 59 } |
| 60 content::BrowserThread::PostTask( |
| 61 content::BrowserThread::FILE, FROM_HERE, |
| 62 base::Bind(&UsbDeviceHandle::ClaimInterface, device_, interface_number, |
| 63 callback)); |
| 64 } |
| 65 |
| 66 void UsbDeviceResource::ReleaseInterface(const int interface_number, |
| 67 const UsbInterfaceCallback& callback) { |
| 68 if (!device_.get()) { |
| 69 callback.Run(false); |
| 70 } |
| 71 content::BrowserThread::PostTask( |
| 72 content::BrowserThread::FILE, FROM_HERE, |
| 73 base::Bind(&UsbDeviceHandle::ReleaseInterface, device_, interface_number, |
| 74 callback)); |
| 75 } |
| 76 |
| 77 void UsbDeviceResource::SetInterfaceAlternateSetting( |
| 78 const int interface_number, const int alternate_setting, |
| 79 const UsbInterfaceCallback& callback) { |
| 80 if (!device_.get()) { |
| 81 callback.Run(false); |
| 82 } |
| 83 content::BrowserThread::PostTask( |
| 84 content::BrowserThread::FILE, FROM_HERE, |
| 85 base::Bind(&UsbDeviceHandle::SetInterfaceAlternateSetting, device_, |
| 86 interface_number, alternate_setting, callback)); |
| 87 } |
| 88 |
| 89 void UsbDeviceResource::ControlTransfer( |
| 90 const UsbEndpointDirection direction, |
| 91 const UsbDeviceHandle::TransferRequestType request_type, |
| 92 const UsbDeviceHandle::TransferRecipient recipient, const uint8 request, |
| 93 const uint16 value, const uint16 index, net::IOBuffer* buffer, |
| 94 const size_t length, const unsigned int timeout, |
| 95 const UsbTransferCallback& callback) { |
| 96 if (!device_.get()) { |
| 97 callback.Run(USB_TRANSFER_DISCONNECT, |
| 98 scoped_refptr<net::IOBuffer>(), 0); |
| 99 } |
| 100 device_->ControlTransfer(direction, request_type, recipient, request, value, |
| 101 index, buffer, length, timeout, callback); |
| 102 } |
| 103 |
| 104 void UsbDeviceResource::BulkTransfer(const UsbEndpointDirection direction, |
| 105 const uint8 endpoint, |
| 106 net::IOBuffer* buffer, const size_t length, |
| 107 const unsigned int timeout, |
| 108 const UsbTransferCallback& callback) { |
| 109 if (!device_.get()) { |
| 110 callback.Run(USB_TRANSFER_DISCONNECT, |
| 111 scoped_refptr<net::IOBuffer>(), 0); |
| 112 } |
| 113 device_->BulkTransfer(direction, endpoint, buffer, length, timeout, callback); |
| 114 } |
| 115 |
| 116 void UsbDeviceResource::InterruptTransfer(const UsbEndpointDirection direction, |
| 117 const uint8 endpoint, |
| 118 net::IOBuffer* buffer, |
| 119 const size_t length, |
| 120 const unsigned int timeout, |
| 121 const UsbTransferCallback& callback) { |
| 122 if (!device_.get()) { |
| 123 callback.Run(USB_TRANSFER_DISCONNECT, |
| 124 scoped_refptr<net::IOBuffer>(), 0); |
| 125 } |
| 126 device_->InterruptTransfer(direction, endpoint, buffer, length, timeout, |
| 127 callback); |
| 128 } |
| 129 |
| 130 void UsbDeviceResource::IsochronousTransfer( |
| 131 const UsbEndpointDirection direction, const uint8 endpoint, |
| 132 net::IOBuffer* buffer, const size_t length, const unsigned int packets, |
| 133 const unsigned int packet_length, const unsigned int timeout, |
| 134 const UsbTransferCallback& callback) { |
| 135 if (!device_.get()) { |
| 136 callback.Run(USB_TRANSFER_DISCONNECT, |
| 137 scoped_refptr<net::IOBuffer>(), 0); |
| 138 } |
| 139 device_->IsochronousTransfer(direction, endpoint, buffer, length, packets, |
| 140 packet_length, timeout, callback); |
| 141 } |
| 142 |
| 143 void UsbDeviceResource::ResetDevice( |
| 144 const base::Callback<void(bool)>& callback) { |
| 145 if (!device_.get()) { |
| 146 callback.Run(false); |
| 147 } |
| 148 content::BrowserThread::PostTask( |
| 149 content::BrowserThread::FILE, FROM_HERE, |
| 150 base::Bind(&UsbDeviceHandle::ResetDevice, device_, callback)); |
| 151 } |
| 24 | 152 |
| 25 } // namespace extensions | 153 } // namespace extensions |
| OLD | NEW |