Chromium Code Reviews| 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.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 } | |
| 25 | |
| 19 static base::LazyInstance<ProfileKeyedAPIFactory< | 26 static base::LazyInstance<ProfileKeyedAPIFactory< |
| 20 ApiResourceManager<UsbDeviceResource> > > | 27 ApiResourceManager<UsbDeviceResource> > > |
| 21 g_factory = LAZY_INSTANCE_INITIALIZER; | 28 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 22 | 29 |
| 23 // static | 30 // static |
| 24 template <> | 31 template <> |
| 25 ProfileKeyedAPIFactory<ApiResourceManager<UsbDeviceResource> >* | 32 ProfileKeyedAPIFactory<ApiResourceManager<UsbDeviceResource> >* |
| 26 ApiResourceManager<UsbDeviceResource>::GetFactoryInstance() { | 33 ApiResourceManager<UsbDeviceResource>::GetFactoryInstance() { |
| 27 return &g_factory.Get(); | 34 return &g_factory.Get(); |
| 28 } | 35 } |
| 29 | 36 |
| 30 UsbDeviceResource::UsbDeviceResource(const std::string& owner_extension_id, | 37 UsbDeviceResource::UsbDeviceResource(const std::string& owner_extension_id, |
| 31 scoped_refptr<UsbDevice> device) | 38 scoped_refptr<UsbDevice> device) |
| 32 : ApiResource(owner_extension_id), device_(device) {} | 39 : ApiResource(owner_extension_id), device_(device) {} |
| 33 | 40 |
| 34 UsbDeviceResource::~UsbDeviceResource() {} | 41 UsbDeviceResource::~UsbDeviceResource() { |
| 42 Close(base::Bind(EmptyCallback)); | |
| 43 } | |
| 44 | |
| 45 void UsbDeviceResource::Close(const base::Callback<void()>& callback) { | |
| 46 scoped_refptr<UsbDevice> handle; | |
| 47 handle.swap(device_); | |
| 48 if (!handle.get()) { | |
| 49 callback.Run(); | |
| 50 return; | |
| 51 } | |
| 52 content::BrowserThread::PostTask( | |
| 53 content::BrowserThread::FILE, | |
| 54 FROM_HERE, | |
| 55 base::Bind(&UsbDevice::Close, handle, callback)); | |
| 56 } | |
| 57 | |
| 58 void UsbDeviceResource::ListInterfaces(UsbConfigDescriptor* config, | |
|
pfeldman
2013/07/22 18:23:13
Unfortunately, all your clients would need to have
| |
| 59 const UsbInterfaceCallback& callback) { | |
| 60 if (!device_.get()) { | |
| 61 callback.Run(false); | |
| 62 return; | |
| 63 } | |
| 64 content::BrowserThread::PostTask( | |
| 65 content::BrowserThread::FILE, FROM_HERE, | |
| 66 base::Bind(&UsbDevice::ListInterfaces, | |
| 67 device_, | |
| 68 make_scoped_refptr(config), | |
| 69 callback)); | |
| 70 } | |
| 71 | |
| 72 void UsbDeviceResource::ClaimInterface(const int interface_number, | |
| 73 const UsbInterfaceCallback& callback) { | |
| 74 if (!device_.get()) { | |
| 75 callback.Run(false); | |
| 76 return; | |
| 77 } | |
| 78 content::BrowserThread::PostTask( | |
| 79 content::BrowserThread::FILE, | |
| 80 FROM_HERE, | |
| 81 base::Bind(&UsbDevice::ClaimInterface, | |
| 82 device_, | |
| 83 interface_number, | |
| 84 callback)); | |
| 85 } | |
| 86 | |
| 87 void UsbDeviceResource::ReleaseInterface(const int interface_number, | |
| 88 const UsbInterfaceCallback& callback) { | |
| 89 if (!device_.get()) { | |
| 90 callback.Run(false); | |
| 91 return; | |
| 92 } | |
| 93 content::BrowserThread::PostTask( | |
| 94 content::BrowserThread::FILE, | |
| 95 FROM_HERE, | |
| 96 base::Bind(&UsbDevice::ReleaseInterface, | |
| 97 device_, | |
| 98 interface_number, | |
| 99 callback)); | |
| 100 } | |
| 101 | |
| 102 void UsbDeviceResource::SetInterfaceAlternateSetting( | |
| 103 const int interface_number, | |
| 104 const int alternate_setting, | |
| 105 const UsbInterfaceCallback& callback) { | |
| 106 if (!device_.get()) { | |
| 107 callback.Run(false); | |
| 108 return; | |
| 109 } | |
| 110 content::BrowserThread::PostTask( | |
| 111 content::BrowserThread::FILE, | |
| 112 FROM_HERE, | |
| 113 base::Bind(&UsbDevice::SetInterfaceAlternateSetting, | |
| 114 device_, | |
| 115 interface_number, | |
| 116 alternate_setting, | |
| 117 callback)); | |
| 118 } | |
| 119 | |
| 120 void UsbDeviceResource::ControlTransfer( | |
| 121 const UsbEndpointDirection direction, | |
| 122 const UsbDevice::TransferRequestType request_type, | |
| 123 const UsbDevice::TransferRecipient recipient, | |
| 124 const uint8 request, | |
| 125 const uint16 value, | |
| 126 const uint16 index, | |
| 127 net::IOBuffer* buffer, | |
| 128 const size_t length, | |
| 129 const unsigned int timeout, | |
| 130 const UsbTransferCallback& callback) { | |
| 131 if (!device_.get()) { | |
| 132 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | |
| 133 return; | |
| 134 } | |
| 135 device_->ControlTransfer(direction, | |
| 136 request_type, | |
| 137 recipient, | |
| 138 request, | |
| 139 value, | |
| 140 index, | |
| 141 buffer, | |
| 142 length, | |
| 143 timeout, | |
| 144 callback); | |
| 145 } | |
| 146 | |
| 147 void UsbDeviceResource::BulkTransfer(const UsbEndpointDirection direction, | |
| 148 const uint8 endpoint, | |
| 149 net::IOBuffer* buffer, | |
| 150 const size_t length, | |
| 151 const unsigned int timeout, | |
| 152 const UsbTransferCallback& callback) { | |
| 153 if (!device_.get()) { | |
| 154 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | |
| 155 return; | |
| 156 } | |
| 157 device_->BulkTransfer(direction, endpoint, buffer, length, timeout, callback); | |
| 158 } | |
| 159 | |
| 160 void UsbDeviceResource::InterruptTransfer(const UsbEndpointDirection direction, | |
| 161 const uint8 endpoint, | |
| 162 net::IOBuffer* buffer, | |
| 163 const size_t length, | |
| 164 const unsigned int timeout, | |
| 165 const UsbTransferCallback& callback) { | |
| 166 if (!device_.get()) { | |
| 167 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | |
| 168 return; | |
| 169 } | |
| 170 device_->InterruptTransfer(direction, endpoint, buffer, length, timeout, | |
| 171 callback); | |
| 172 } | |
| 173 | |
| 174 void UsbDeviceResource::IsochronousTransfer( | |
| 175 const UsbEndpointDirection direction, | |
| 176 const uint8 endpoint, | |
| 177 net::IOBuffer* buffer, | |
| 178 const size_t length, | |
| 179 const unsigned int packets, | |
| 180 const unsigned int packet_length, | |
| 181 const unsigned int timeout, | |
| 182 const UsbTransferCallback& callback) { | |
| 183 if (!device_.get()) { | |
| 184 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | |
| 185 return; | |
| 186 } | |
| 187 device_->IsochronousTransfer(direction, | |
| 188 endpoint, | |
| 189 buffer, | |
| 190 length, | |
| 191 packets, | |
| 192 packet_length, | |
| 193 timeout, | |
| 194 callback); | |
| 195 } | |
| 196 | |
| 197 void UsbDeviceResource::ResetDevice(const UsbResetDeviceCallback& callback) { | |
| 198 if (!device_.get()) { | |
| 199 callback.Run(false); | |
| 200 return; | |
| 201 } | |
| 202 content::BrowserThread::PostTask( | |
| 203 content::BrowserThread::FILE, | |
| 204 FROM_HERE, | |
| 205 base::Bind(&UsbDevice::ResetDevice, device_, callback)); | |
| 206 } | |
| 207 | |
| 35 | 208 |
| 36 } // namespace extensions | 209 } // namespace extensions |
| OLD | NEW |