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_api.h" | 5 #include "chrome/browser/extensions/api/usb/usb_api.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" |
11 #include "chrome/browser/extensions/api/usb/usb_device_resource.h" | 12 #include "chrome/browser/extensions/api/usb/usb_device_resource.h" |
12 #include "chrome/browser/extensions/extension_system.h" | 13 #include "chrome/browser/extensions/extension_system.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/usb/usb_device_handle.h" | 15 #include "chrome/browser/usb/usb_device_handle.h" |
15 #include "chrome/browser/usb/usb_service.h" | 16 #include "chrome/browser/usb/usb_service.h" |
16 #include "chrome/common/extensions/api/usb.h" | 17 #include "chrome/common/extensions/api/usb.h" |
17 #include "chrome/common/extensions/permissions/permissions_data.h" | 18 #include "chrome/common/extensions/permissions/permissions_data.h" |
18 #include "chrome/common/extensions/permissions/usb_device_permission.h" | 19 #include "chrome/common/extensions/permissions/usb_device_permission.h" |
19 | 20 |
20 namespace BulkTransfer = extensions::api::usb::BulkTransfer; | |
21 namespace ClaimInterface = extensions::api::usb::ClaimInterface; | |
22 namespace ListInterfaces = extensions::api::usb::ListInterfaces; | |
23 namespace CloseDevice = extensions::api::usb::CloseDevice; | |
24 namespace ControlTransfer = extensions::api::usb::ControlTransfer; | |
25 namespace FindDevices = extensions::api::usb::FindDevices; | |
26 namespace InterruptTransfer = extensions::api::usb::InterruptTransfer; | |
27 namespace IsochronousTransfer = extensions::api::usb::IsochronousTransfer; | |
28 namespace ReleaseInterface = extensions::api::usb::ReleaseInterface; | |
29 namespace ResetDevice = extensions::api::usb::ResetDevice; | |
30 namespace SetInterfaceAlternateSetting = | |
31 extensions::api::usb::SetInterfaceAlternateSetting; | |
32 namespace usb = extensions::api::usb; | 21 namespace usb = extensions::api::usb; |
| 22 namespace BulkTransfer = usb::BulkTransfer; |
| 23 namespace ClaimInterface = usb::ClaimInterface; |
| 24 namespace CloseDevice = usb::CloseDevice; |
| 25 namespace ControlTransfer = usb::ControlTransfer; |
| 26 namespace FindDevices = usb::FindDevices; |
| 27 namespace GetDevices = usb::GetDevices; |
| 28 namespace InterruptTransfer = usb::InterruptTransfer; |
| 29 namespace IsochronousTransfer = usb::IsochronousTransfer; |
| 30 namespace ListInterfaces = usb::ListInterfaces; |
| 31 namespace OpenDevice = usb::OpenDevice; |
| 32 namespace ReleaseInterface = usb::ReleaseInterface; |
| 33 namespace ResetDevice = usb::ResetDevice; |
| 34 namespace SetInterfaceAlternateSetting = usb::SetInterfaceAlternateSetting; |
33 | 35 |
34 using content::BrowserThread; | 36 using content::BrowserThread; |
35 using std::string; | 37 using std::string; |
36 using std::vector; | 38 using std::vector; |
37 using usb::ControlTransferInfo; | 39 using usb::ControlTransferInfo; |
38 using usb::Device; | 40 using usb::Device; |
| 41 using usb::DeviceHandle; |
39 using usb::Direction; | 42 using usb::Direction; |
40 using usb::EndpointDescriptor; | 43 using usb::EndpointDescriptor; |
41 using usb::GenericTransferInfo; | 44 using usb::GenericTransferInfo; |
42 using usb::InterfaceDescriptor; | 45 using usb::InterfaceDescriptor; |
43 using usb::IsochronousTransferInfo; | 46 using usb::IsochronousTransferInfo; |
44 using usb::Recipient; | 47 using usb::Recipient; |
45 using usb::RequestType; | 48 using usb::RequestType; |
46 using usb::SynchronizationType; | 49 using usb::SynchronizationType; |
47 using usb::TransferType; | 50 using usb::TransferType; |
48 using usb::UsageType; | 51 using usb::UsageType; |
49 | 52 |
| 53 typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > ScopedDeviceVector; |
| 54 |
50 namespace { | 55 namespace { |
51 | 56 |
52 static const char kDataKey[] = "data"; | 57 const char kDataKey[] = "data"; |
53 static const char kResultCodeKey[] = "resultCode"; | 58 const char kResultCodeKey[] = "resultCode"; |
54 | 59 |
55 static const char kErrorCancelled[] = "Transfer was cancelled."; | 60 const char kErrorOpen[] = "Failed to open device."; |
56 static const char kErrorDisconnect[] = "Device disconnected."; | 61 const char kErrorCancelled[] = "Transfer was cancelled."; |
57 static const char kErrorGeneric[] = "Transfer failed."; | 62 const char kErrorDisconnect[] = "Device disconnected."; |
58 static const char kErrorOverflow[] = "Inbound transfer overflow."; | 63 const char kErrorGeneric[] = "Transfer failed."; |
59 static const char kErrorStalled[] = "Transfer stalled."; | 64 const char kErrorOverflow[] = "Inbound transfer overflow."; |
60 static const char kErrorTimeout[] = "Transfer timed out."; | 65 const char kErrorStalled[] = "Transfer stalled."; |
61 static const char kErrorTransferLength[] = "Transfer length is insufficient."; | 66 const char kErrorTimeout[] = "Transfer timed out."; |
| 67 const char kErrorTransferLength[] = "Transfer length is insufficient."; |
62 | 68 |
63 static const char kErrorCannotListInterfaces[] = "Error listing interfaces."; | 69 const char kErrorCannotListInterfaces[] = "Error listing interfaces."; |
64 static const char kErrorCannotClaimInterface[] = "Error claiming interface."; | 70 const char kErrorCannotClaimInterface[] = "Error claiming interface."; |
65 static const char kErrorCannotReleaseInterface[] = "Error releasing interface."; | 71 const char kErrorCannotReleaseInterface[] = "Error releasing interface."; |
66 static const char kErrorCannotSetInterfaceAlternateSetting[] = | 72 const char kErrorCannotSetInterfaceAlternateSetting[] = |
67 "Error setting alternate interface setting."; | 73 "Error setting alternate interface setting."; |
68 static const char kErrorConvertDirection[] = "Invalid transfer direction."; | 74 const char kErrorConvertDirection[] = "Invalid transfer direction."; |
69 static const char kErrorConvertRecipient[] = "Invalid transfer recipient."; | 75 const char kErrorConvertRecipient[] = "Invalid transfer recipient."; |
70 static const char kErrorConvertRequestType[] = "Invalid request type."; | 76 const char kErrorConvertRequestType[] = "Invalid request type."; |
71 static const char kErrorConvertSynchronizationType[] = | 77 const char kErrorConvertSynchronizationType[] = "Invalid synchronization type"; |
72 "Invalid synchronization type"; | 78 const char kErrorConvertTransferType[] = "Invalid endpoint type."; |
73 static const char kErrorConvertTransferType[] = "Invalid endpoint type."; | 79 const char kErrorConvertUsageType[] = "Invalid usage type."; |
74 static const char kErrorConvertUsageType[] = "Invalid usage type."; | 80 const char kErrorMalformedParameters[] = "Error parsing parameters."; |
75 static const char kErrorMalformedParameters[] = "Error parsing parameters."; | 81 const char kErrorNoDevice[] = "No such device."; |
76 static const char kErrorNoDevice[] = "No such device."; | 82 const char kErrorPermissionDenied[] = |
77 static const char kErrorPermissionDenied[] = | |
78 "Permission to access device was denied"; | 83 "Permission to access device was denied"; |
79 static const char kErrorInvalidTransferLength[] = "Transfer length must be a " | 84 const char kErrorInvalidTransferLength[] = |
80 "positive number less than 104,857,600."; | 85 "Transfer length must be a positive number less than 104,857,600."; |
81 static const char kErrorInvalidNumberOfPackets[] = "Number of packets must be " | 86 const char kErrorInvalidNumberOfPackets[] = |
82 "a positive number less than 4,194,304."; | 87 "Number of packets must be a positive number less than 4,194,304."; |
83 static const char kErrorInvalidPacketLength[] = "Packet length must be a " | 88 const char kErrorInvalidPacketLength[] = "Packet length must be a " |
84 "positive number less than 65,536."; | 89 "positive number less than 65,536."; |
85 static const char kErrorResetDevice[] = | 90 const char kErrorResetDevice[] = |
86 "Error resetting the device. The device has been closed."; | 91 "Error resetting the device. The device has been closed."; |
87 | 92 |
88 static const size_t kMaxTransferLength = 100 * 1024 * 1024; | 93 const size_t kMaxTransferLength = 100 * 1024 * 1024; |
89 static const int kMaxPackets = 4 * 1024 * 1024; | 94 const int kMaxPackets = 4 * 1024 * 1024; |
90 static const int kMaxPacketLength = 64 * 1024; | 95 const int kMaxPacketLength = 64 * 1024; |
91 | 96 |
92 static UsbDevice* g_device_for_test = NULL; | 97 UsbDevice* g_device_for_test = NULL; |
93 | 98 |
94 static bool ConvertDirectionToApi(const UsbEndpointDirection& input, | 99 bool ConvertDirectionToApi(const UsbEndpointDirection& input, |
95 Direction* output) { | 100 Direction* output) { |
96 switch (input) { | 101 switch (input) { |
97 case USB_DIRECTION_INBOUND: | 102 case USB_DIRECTION_INBOUND: |
98 *output = usb::DIRECTION_IN; | 103 *output = usb::DIRECTION_IN; |
99 return true; | 104 return true; |
100 case USB_DIRECTION_OUTBOUND: | 105 case USB_DIRECTION_OUTBOUND: |
101 *output = usb::DIRECTION_OUT; | 106 *output = usb::DIRECTION_OUT; |
102 return true; | 107 return true; |
103 default: | 108 default: |
104 NOTREACHED(); | 109 NOTREACHED(); |
105 return false; | 110 return false; |
106 } | 111 } |
107 } | 112 } |
108 | 113 |
109 static bool ConvertSynchronizationTypeToApi( | 114 bool ConvertSynchronizationTypeToApi(const UsbSynchronizationType& input, |
110 const UsbSynchronizationType& input, | 115 usb::SynchronizationType* output) { |
111 extensions::api::usb::SynchronizationType* output) { | |
112 switch (input) { | 116 switch (input) { |
113 case USB_SYNCHRONIZATION_NONE: | 117 case USB_SYNCHRONIZATION_NONE: |
114 *output = usb::SYNCHRONIZATION_TYPE_NONE; | 118 *output = usb::SYNCHRONIZATION_TYPE_NONE; |
115 return true; | 119 return true; |
116 case USB_SYNCHRONIZATION_ASYNCHRONOUS: | 120 case USB_SYNCHRONIZATION_ASYNCHRONOUS: |
117 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; | 121 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; |
118 return true; | 122 return true; |
119 case USB_SYNCHRONIZATION_ADAPTIVE: | 123 case USB_SYNCHRONIZATION_ADAPTIVE: |
120 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE; | 124 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE; |
121 return true; | 125 return true; |
122 case USB_SYNCHRONIZATION_SYNCHRONOUS: | 126 case USB_SYNCHRONIZATION_SYNCHRONOUS: |
123 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; | 127 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; |
124 return true; | 128 return true; |
125 default: | 129 default: |
126 NOTREACHED(); | 130 NOTREACHED(); |
127 return false; | 131 return false; |
128 } | 132 } |
129 } | 133 } |
130 | 134 |
131 static bool ConvertTransferTypeToApi( | 135 bool ConvertTransferTypeToApi( |
132 const UsbTransferType& input, | 136 const UsbTransferType& input, |
133 extensions::api::usb::TransferType* output) { | 137 usb::TransferType* output) { |
134 switch (input) { | 138 switch (input) { |
135 case USB_TRANSFER_CONTROL: | 139 case USB_TRANSFER_CONTROL: |
136 *output = usb::TRANSFER_TYPE_CONTROL; | 140 *output = usb::TRANSFER_TYPE_CONTROL; |
137 return true; | 141 return true; |
138 case USB_TRANSFER_INTERRUPT: | 142 case USB_TRANSFER_INTERRUPT: |
139 *output = usb::TRANSFER_TYPE_INTERRUPT; | 143 *output = usb::TRANSFER_TYPE_INTERRUPT; |
140 return true; | 144 return true; |
141 case USB_TRANSFER_ISOCHRONOUS: | 145 case USB_TRANSFER_ISOCHRONOUS: |
142 *output = usb::TRANSFER_TYPE_ISOCHRONOUS; | 146 *output = usb::TRANSFER_TYPE_ISOCHRONOUS; |
143 return true; | 147 return true; |
144 case USB_TRANSFER_BULK: | 148 case USB_TRANSFER_BULK: |
145 *output = usb::TRANSFER_TYPE_BULK; | 149 *output = usb::TRANSFER_TYPE_BULK; |
146 return true; | 150 return true; |
147 default: | 151 default: |
148 NOTREACHED(); | 152 NOTREACHED(); |
149 return false; | 153 return false; |
150 } | 154 } |
151 } | 155 } |
152 | 156 |
153 static bool ConvertUsageTypeToApi(const UsbUsageType& input, | 157 static bool ConvertUsageTypeToApi(const UsbUsageType& input, |
154 extensions::api::usb::UsageType* output) { | 158 usb::UsageType* output) { |
155 switch (input) { | 159 switch (input) { |
156 case USB_USAGE_DATA: | 160 case USB_USAGE_DATA: |
157 *output = usb::USAGE_TYPE_DATA; | 161 *output = usb::USAGE_TYPE_DATA; |
158 return true; | 162 return true; |
159 case USB_USAGE_FEEDBACK: | 163 case USB_USAGE_FEEDBACK: |
160 *output = usb::USAGE_TYPE_FEEDBACK; | 164 *output = usb::USAGE_TYPE_FEEDBACK; |
161 return true; | 165 return true; |
162 case USB_USAGE_EXPLICIT_FEEDBACK: | 166 case USB_USAGE_EXPLICIT_FEEDBACK: |
163 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; | 167 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; |
164 return true; | 168 return true; |
165 default: | 169 default: |
166 NOTREACHED(); | 170 NOTREACHED(); |
167 return false; | 171 return false; |
168 } | 172 } |
169 } | 173 } |
170 | 174 |
171 static bool ConvertDirection(const Direction& input, | 175 bool ConvertDirection(const Direction& input, |
172 UsbEndpointDirection* output) { | 176 UsbEndpointDirection* output) { |
173 switch (input) { | 177 switch (input) { |
174 case usb::DIRECTION_IN: | 178 case usb::DIRECTION_IN: |
175 *output = USB_DIRECTION_INBOUND; | 179 *output = USB_DIRECTION_INBOUND; |
176 return true; | 180 return true; |
177 case usb::DIRECTION_OUT: | 181 case usb::DIRECTION_OUT: |
178 *output = USB_DIRECTION_OUTBOUND; | 182 *output = USB_DIRECTION_OUTBOUND; |
179 return true; | 183 return true; |
180 default: | 184 default: |
181 NOTREACHED(); | 185 NOTREACHED(); |
182 return false; | 186 return false; |
183 } | 187 } |
184 } | 188 } |
185 | 189 |
186 static bool ConvertRequestType(const RequestType& input, | 190 bool ConvertRequestType(const RequestType& input, |
187 UsbDeviceHandle::TransferRequestType* output) { | 191 UsbDeviceHandle::TransferRequestType* output) { |
188 switch (input) { | 192 switch (input) { |
189 case usb::REQUEST_TYPE_STANDARD: | 193 case usb::REQUEST_TYPE_STANDARD: |
190 *output = UsbDeviceHandle::STANDARD; | 194 *output = UsbDeviceHandle::STANDARD; |
191 return true; | 195 return true; |
192 case usb::REQUEST_TYPE_CLASS: | 196 case usb::REQUEST_TYPE_CLASS: |
193 *output = UsbDeviceHandle::CLASS; | 197 *output = UsbDeviceHandle::CLASS; |
194 return true; | 198 return true; |
195 case usb::REQUEST_TYPE_VENDOR: | 199 case usb::REQUEST_TYPE_VENDOR: |
196 *output = UsbDeviceHandle::VENDOR; | 200 *output = UsbDeviceHandle::VENDOR; |
197 return true; | 201 return true; |
198 case usb::REQUEST_TYPE_RESERVED: | 202 case usb::REQUEST_TYPE_RESERVED: |
199 *output = UsbDeviceHandle::RESERVED; | 203 *output = UsbDeviceHandle::RESERVED; |
200 return true; | 204 return true; |
201 default: | 205 default: |
202 NOTREACHED(); | 206 NOTREACHED(); |
203 return false; | 207 return false; |
204 } | 208 } |
205 } | 209 } |
206 | 210 |
207 static bool ConvertRecipient(const Recipient& input, | 211 bool ConvertRecipient(const Recipient& input, |
208 UsbDeviceHandle::TransferRecipient* output) { | 212 UsbDeviceHandle::TransferRecipient* output) { |
209 switch (input) { | 213 switch (input) { |
210 case usb::RECIPIENT_DEVICE: | 214 case usb::RECIPIENT_DEVICE: |
211 *output = UsbDeviceHandle::DEVICE; | 215 *output = UsbDeviceHandle::DEVICE; |
212 return true; | 216 return true; |
213 case usb::RECIPIENT_INTERFACE: | 217 case usb::RECIPIENT_INTERFACE: |
214 *output = UsbDeviceHandle::INTERFACE; | 218 *output = UsbDeviceHandle::INTERFACE; |
215 return true; | 219 return true; |
216 case usb::RECIPIENT_ENDPOINT: | 220 case usb::RECIPIENT_ENDPOINT: |
217 *output = UsbDeviceHandle::ENDPOINT; | 221 *output = UsbDeviceHandle::ENDPOINT; |
218 return true; | 222 return true; |
219 case usb::RECIPIENT_OTHER: | 223 case usb::RECIPIENT_OTHER: |
220 *output = UsbDeviceHandle::OTHER; | 224 *output = UsbDeviceHandle::OTHER; |
221 return true; | 225 return true; |
222 default: | 226 default: |
223 NOTREACHED(); | 227 NOTREACHED(); |
224 return false; | 228 return false; |
225 } | 229 } |
226 } | 230 } |
227 | 231 |
228 template<class T> | 232 template<class T> |
229 static bool GetTransferSize(const T& input, size_t* output) { | 233 bool GetTransferSize(const T& input, size_t* output) { |
230 if (input.direction == usb::DIRECTION_IN) { | 234 if (input.direction == usb::DIRECTION_IN) { |
231 const int* length = input.length.get(); | 235 const int* length = input.length.get(); |
232 if (length && *length >= 0 && | 236 if (length && *length >= 0 && |
233 static_cast<size_t>(*length) < kMaxTransferLength) { | 237 static_cast<size_t>(*length) < kMaxTransferLength) { |
234 *output = *length; | 238 *output = *length; |
235 return true; | 239 return true; |
236 } | 240 } |
237 } else if (input.direction == usb::DIRECTION_OUT) { | 241 } else if (input.direction == usb::DIRECTION_OUT) { |
238 if (input.data.get()) { | 242 if (input.data.get()) { |
239 *output = input.data->size(); | 243 *output = input.data->size(); |
240 return true; | 244 return true; |
241 } | 245 } |
242 } | 246 } |
243 return false; | 247 return false; |
244 } | 248 } |
245 | 249 |
246 template<class T> | 250 template<class T> |
247 static scoped_refptr<net::IOBuffer> CreateBufferForTransfer( | 251 scoped_refptr<net::IOBuffer> CreateBufferForTransfer( |
248 const T& input, UsbEndpointDirection direction, size_t size) { | 252 const T& input, UsbEndpointDirection direction, size_t size) { |
249 | 253 |
250 if (size >= kMaxTransferLength) | 254 if (size >= kMaxTransferLength) |
251 return NULL; | 255 return NULL; |
252 | 256 |
253 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This | 257 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This |
254 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer | 258 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer |
255 // cannot represent a zero-length buffer, while an URB can. | 259 // cannot represent a zero-length buffer, while an URB can. |
256 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max( | 260 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max( |
257 static_cast<size_t>(1), size)); | 261 static_cast<size_t>(1), size)); |
258 | 262 |
259 if (direction == USB_DIRECTION_INBOUND) { | 263 if (direction == USB_DIRECTION_INBOUND) { |
260 return buffer; | 264 return buffer; |
261 } else if (direction == USB_DIRECTION_OUTBOUND) { | 265 } else if (direction == USB_DIRECTION_OUTBOUND) { |
262 if (input.data.get() && size <= input.data->size()) { | 266 if (input.data.get() && size <= input.data->size()) { |
263 memcpy(buffer->data(), input.data->data(), size); | 267 memcpy(buffer->data(), input.data->data(), size); |
264 return buffer; | 268 return buffer; |
265 } | 269 } |
266 } | 270 } |
267 NOTREACHED(); | 271 NOTREACHED(); |
268 return NULL; | 272 return NULL; |
269 } | 273 } |
270 | 274 |
271 static const char* ConvertTransferStatusToErrorString( | 275 const char* ConvertTransferStatusToErrorString(const UsbTransferStatus status) { |
272 const UsbTransferStatus status) { | |
273 switch (status) { | 276 switch (status) { |
274 case USB_TRANSFER_COMPLETED: | 277 case USB_TRANSFER_COMPLETED: |
275 return ""; | 278 return ""; |
276 case USB_TRANSFER_ERROR: | 279 case USB_TRANSFER_ERROR: |
277 return kErrorGeneric; | 280 return kErrorGeneric; |
278 case USB_TRANSFER_TIMEOUT: | 281 case USB_TRANSFER_TIMEOUT: |
279 return kErrorTimeout; | 282 return kErrorTimeout; |
280 case USB_TRANSFER_CANCELLED: | 283 case USB_TRANSFER_CANCELLED: |
281 return kErrorCancelled; | 284 return kErrorCancelled; |
282 case USB_TRANSFER_STALLED: | 285 case USB_TRANSFER_STALLED: |
283 return kErrorStalled; | 286 return kErrorStalled; |
284 case USB_TRANSFER_DISCONNECT: | 287 case USB_TRANSFER_DISCONNECT: |
285 return kErrorDisconnect; | 288 return kErrorDisconnect; |
286 case USB_TRANSFER_OVERFLOW: | 289 case USB_TRANSFER_OVERFLOW: |
287 return kErrorOverflow; | 290 return kErrorOverflow; |
288 case USB_TRANSFER_LENGTH_SHORT: | 291 case USB_TRANSFER_LENGTH_SHORT: |
289 return kErrorTransferLength; | 292 return kErrorTransferLength; |
290 default: | 293 default: |
291 NOTREACHED(); | 294 NOTREACHED(); |
292 return ""; | 295 return ""; |
293 } | 296 } |
294 } | 297 } |
295 | 298 |
296 static base::DictionaryValue* CreateTransferInfo( | 299 base::DictionaryValue* CreateTransferInfo( |
297 UsbTransferStatus status, | 300 UsbTransferStatus status, |
298 scoped_refptr<net::IOBuffer> data, | 301 scoped_refptr<net::IOBuffer> data, |
299 size_t length) { | 302 size_t length) { |
300 base::DictionaryValue* result = new base::DictionaryValue(); | 303 base::DictionaryValue* result = new base::DictionaryValue(); |
301 result->SetInteger(kResultCodeKey, status); | 304 result->SetInteger(kResultCodeKey, status); |
302 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer(data->data(), | 305 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer(data->data(), |
303 length)); | 306 length)); |
304 return result; | 307 return result; |
305 } | 308 } |
306 | 309 |
307 static base::Value* PopulateDevice(int handle, int vendor_id, int product_id) { | 310 base::Value* PopulateDeviceHandle(int handle, int vendor_id, int product_id) { |
308 Device device; | 311 DeviceHandle result; |
309 device.handle = handle; | 312 result.handle = handle; |
310 device.vendor_id = vendor_id; | 313 result.vendor_id.reset(new int(vendor_id)); |
311 device.product_id = product_id; | 314 result.product_id.reset(new int(product_id)); |
312 return device.ToValue().release(); | 315 return result.ToValue().release(); |
313 } | 316 } |
314 | 317 |
315 static base::Value* PopulateInterfaceDescriptor(int interface_number, | 318 base::Value* PopulateDevice(UsbDevice* device) { |
316 int alternate_setting, int interface_class, int interface_subclass, | 319 Device result; |
317 int interface_protocol, | 320 result.device = device->unique_id(); |
318 std::vector<linked_ptr<EndpointDescriptor> >* endpoints) { | 321 result.vendor_id.reset(new int(device->vendor_id())); |
| 322 result.product_id.reset(new int(device->product_id())); |
| 323 return result.ToValue().release(); |
| 324 } |
| 325 |
| 326 base::Value* PopulateInterfaceDescriptor( |
| 327 int interface_number, |
| 328 int alternate_setting, |
| 329 int interface_class, |
| 330 int interface_subclass, |
| 331 int interface_protocol, |
| 332 std::vector<linked_ptr<EndpointDescriptor> >* endpoints) { |
319 InterfaceDescriptor descriptor; | 333 InterfaceDescriptor descriptor; |
320 descriptor.interface_number = interface_number; | 334 descriptor.interface_number = interface_number; |
321 descriptor.alternate_setting = alternate_setting; | 335 descriptor.alternate_setting = alternate_setting; |
322 descriptor.interface_class = interface_class; | 336 descriptor.interface_class = interface_class; |
323 descriptor.interface_subclass = interface_subclass; | 337 descriptor.interface_subclass = interface_subclass; |
324 descriptor.interface_protocol = interface_protocol; | 338 descriptor.interface_protocol = interface_protocol; |
325 descriptor.endpoints = *endpoints; | 339 descriptor.endpoints = *endpoints; |
326 return descriptor.ToValue().release(); | 340 return descriptor.ToValue().release(); |
327 } | 341 } |
328 | 342 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 const bool converted = ConvertRecipient(input, output); | 410 const bool converted = ConvertRecipient(input, output); |
397 if (!converted) | 411 if (!converted) |
398 SetError(kErrorConvertRecipient); | 412 SetError(kErrorConvertRecipient); |
399 return converted; | 413 return converted; |
400 } | 414 } |
401 | 415 |
402 UsbFindDevicesFunction::UsbFindDevicesFunction() {} | 416 UsbFindDevicesFunction::UsbFindDevicesFunction() {} |
403 | 417 |
404 UsbFindDevicesFunction::~UsbFindDevicesFunction() {} | 418 UsbFindDevicesFunction::~UsbFindDevicesFunction() {} |
405 | 419 |
406 void UsbFindDevicesFunction::SetDeviceForTest(UsbDevice* device) { | |
407 g_device_for_test = device; | |
408 } | |
409 | |
410 bool UsbFindDevicesFunction::Prepare() { | 420 bool UsbFindDevicesFunction::Prepare() { |
411 parameters_ = FindDevices::Params::Create(*args_); | 421 parameters_ = FindDevices::Params::Create(*args_); |
412 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 422 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
413 return true; | 423 return true; |
414 } | 424 } |
415 | 425 |
416 void UsbFindDevicesFunction::AsyncWorkStart() { | 426 void UsbFindDevicesFunction::AsyncWorkStart() { |
417 result_.reset(new base::ListValue()); | 427 result_.reset(new base::ListValue()); |
418 | 428 |
419 if (g_device_for_test) { | 429 if (g_device_for_test) { |
420 UsbDeviceResource* const resource = new UsbDeviceResource( | 430 UsbDeviceResource* const resource = new UsbDeviceResource( |
421 extension_->id(), | 431 extension_->id(), |
422 g_device_for_test->Open()); | 432 g_device_for_test->Open()); |
423 | 433 |
424 Device device; | 434 result_->Append(PopulateDeviceHandle(manager_->Add(resource), 0, 0)); |
425 result_->Append(PopulateDevice(manager_->Add(resource), 0, 0)); | |
426 SetResult(result_.release()); | 435 SetResult(result_.release()); |
427 AsyncWorkCompleted(); | 436 AsyncWorkCompleted(); |
428 return; | 437 return; |
429 } | 438 } |
430 | 439 |
431 const uint16_t vendor_id = parameters_->options.vendor_id; | 440 const uint16_t vendor_id = parameters_->options.vendor_id; |
432 const uint16_t product_id = parameters_->options.product_id; | 441 const uint16_t product_id = parameters_->options.product_id; |
433 int interface_id = parameters_->options.interface_id.get() ? | 442 int interface_id = parameters_->options.interface_id.get() ? |
434 *parameters_->options.interface_id.get() : | 443 *parameters_->options.interface_id.get() : |
435 UsbDevicePermissionData::ANY_INTERFACE; | 444 UsbDevicePermissionData::ANY_INTERFACE; |
(...skipping 19 matching lines...) Expand all Loading... |
455 devices->at(i)->Open(); | 464 devices->at(i)->Open(); |
456 if (device_handle) | 465 if (device_handle) |
457 device_handles_.push_back(device_handle); | 466 device_handles_.push_back(device_handle); |
458 } | 467 } |
459 | 468 |
460 for (size_t i = 0; i < device_handles_.size(); ++i) { | 469 for (size_t i = 0; i < device_handles_.size(); ++i) { |
461 UsbDeviceHandle* const device_handle = device_handles_[i].get(); | 470 UsbDeviceHandle* const device_handle = device_handles_[i].get(); |
462 UsbDeviceResource* const resource = | 471 UsbDeviceResource* const resource = |
463 new UsbDeviceResource(extension_->id(), device_handle); | 472 new UsbDeviceResource(extension_->id(), device_handle); |
464 | 473 |
465 result_->Append(PopulateDevice(manager_->Add(resource), | 474 result_->Append(PopulateDeviceHandle(manager_->Add(resource), |
466 parameters_->options.vendor_id, | 475 parameters_->options.vendor_id, |
467 parameters_->options.product_id)); | 476 parameters_->options.product_id)); |
468 } | 477 } |
469 | 478 |
470 SetResult(result_.release()); | 479 SetResult(result_.release()); |
471 AsyncWorkCompleted(); | 480 AsyncWorkCompleted(); |
472 } | 481 } |
473 | 482 |
| 483 UsbGetDevicesFunction::UsbGetDevicesFunction() { |
| 484 } |
| 485 |
| 486 UsbGetDevicesFunction::~UsbGetDevicesFunction() { |
| 487 } |
| 488 |
| 489 void UsbGetDevicesFunction::SetDeviceForTest(UsbDevice* device) { |
| 490 g_device_for_test = device; |
| 491 } |
| 492 |
| 493 bool UsbGetDevicesFunction::Prepare() { |
| 494 parameters_ = GetDevices::Params::Create(*args_); |
| 495 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 496 return true; |
| 497 } |
| 498 |
| 499 void UsbGetDevicesFunction::AsyncWorkStart() { |
| 500 result_.reset(new base::ListValue()); |
| 501 |
| 502 if (g_device_for_test) { |
| 503 result_->Append(PopulateDevice(g_device_for_test)); |
| 504 SetResult(result_.release()); |
| 505 AsyncWorkCompleted(); |
| 506 return; |
| 507 } |
| 508 |
| 509 const uint16_t vendor_id = parameters_->options.vendor_id; |
| 510 const uint16_t product_id = parameters_->options.product_id; |
| 511 int interface_id = parameters_->options.interface_id.get() ? |
| 512 *parameters_->options.interface_id.get() : |
| 513 UsbDevicePermissionData::ANY_INTERFACE; |
| 514 UsbDevicePermission::CheckParam param(vendor_id, product_id, interface_id); |
| 515 if (!PermissionsData::CheckAPIPermissionWithParam( |
| 516 GetExtension(), APIPermission::kUsbDevice, ¶m)) { |
| 517 LOG(WARNING) << "Insufficient permissions to access device."; |
| 518 CompleteWithError(kErrorPermissionDenied); |
| 519 return; |
| 520 } |
| 521 |
| 522 UsbService* service = UsbService::GetInstance(); |
| 523 service->FindDevices( |
| 524 vendor_id, |
| 525 product_id, |
| 526 interface_id, |
| 527 base::Bind(&UsbGetDevicesFunction::EnumerationCompletedFileThread, this)); |
| 528 } |
| 529 |
| 530 void UsbGetDevicesFunction::EnumerationCompletedFileThread( |
| 531 scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > devices) { |
| 532 for (size_t i = 0; i < devices->size(); ++i) { |
| 533 result_->Append(PopulateDevice(devices->at(i).get())); |
| 534 } |
| 535 |
| 536 SetResult(result_.release()); |
| 537 AsyncWorkCompleted(); |
| 538 } |
| 539 |
| 540 UsbOpenDeviceFunction::UsbOpenDeviceFunction() {} |
| 541 |
| 542 UsbOpenDeviceFunction::~UsbOpenDeviceFunction() {} |
| 543 |
| 544 bool UsbOpenDeviceFunction::Prepare() { |
| 545 parameters_ = OpenDevice::Params::Create(*args_); |
| 546 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
| 547 return true; |
| 548 } |
| 549 |
| 550 void UsbOpenDeviceFunction::AsyncWorkStart() { |
| 551 UsbService* service = UsbService::GetInstance(); |
| 552 scoped_refptr<UsbDevice> device; |
| 553 if (g_device_for_test) |
| 554 device = g_device_for_test; |
| 555 else |
| 556 device = service->GetDeviceById(parameters_->device.device); |
| 557 |
| 558 if (!device) { |
| 559 SetError(kErrorNoDevice); |
| 560 AsyncWorkCompleted(); |
| 561 return; |
| 562 } |
| 563 |
| 564 if (parameters_->device.vendor_id && |
| 565 *parameters_->device.vendor_id.get() != device->vendor_id()) { |
| 566 SetError(kErrorOpen); |
| 567 AsyncWorkCompleted(); |
| 568 return; |
| 569 } |
| 570 |
| 571 if (parameters_->device.product_id && |
| 572 *parameters_->device.product_id.get() != device->product_id()) { |
| 573 SetError(kErrorOpen); |
| 574 AsyncWorkCompleted(); |
| 575 return; |
| 576 } |
| 577 |
| 578 handle_ = device->Open(); |
| 579 if (!handle_) { |
| 580 SetError(kErrorOpen); |
| 581 AsyncWorkCompleted(); |
| 582 return; |
| 583 } |
| 584 |
| 585 SetResult(PopulateDeviceHandle( |
| 586 manager_->Add(new UsbDeviceResource(extension_->id(), handle_)), |
| 587 handle_->device()->vendor_id(), |
| 588 handle_->device()->product_id())); |
| 589 AsyncWorkCompleted(); |
| 590 } |
| 591 |
474 UsbListInterfacesFunction::UsbListInterfacesFunction() {} | 592 UsbListInterfacesFunction::UsbListInterfacesFunction() {} |
475 | 593 |
476 UsbListInterfacesFunction::~UsbListInterfacesFunction() {} | 594 UsbListInterfacesFunction::~UsbListInterfacesFunction() {} |
477 | 595 |
478 bool UsbListInterfacesFunction::Prepare() { | 596 bool UsbListInterfacesFunction::Prepare() { |
479 parameters_ = ListInterfaces::Params::Create(*args_); | 597 parameters_ = ListInterfaces::Params::Create(*args_); |
480 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 598 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
481 return true; | 599 return true; |
482 } | 600 } |
483 | 601 |
484 void UsbListInterfacesFunction::AsyncWorkStart() { | 602 void UsbListInterfacesFunction::AsyncWorkStart() { |
485 UsbDeviceResource* const resource = GetUsbDeviceResource( | 603 UsbDeviceResource* const resource = GetUsbDeviceResource( |
486 parameters_->device.handle); | 604 parameters_->handle.handle); |
487 if (!resource) { | 605 if (!resource) { |
488 CompleteWithError(kErrorNoDevice); | 606 CompleteWithError(kErrorNoDevice); |
489 return; | 607 return; |
490 } | 608 } |
491 | 609 |
492 scoped_refptr<UsbConfigDescriptor> config = | 610 scoped_refptr<UsbConfigDescriptor> config = |
493 resource->device()->device()->ListInterfaces(); | 611 resource->device()->device()->ListInterfaces(); |
494 | 612 |
495 if (!config) { | 613 if (!config) { |
496 SetError(kErrorCannotListInterfaces); | 614 SetError(kErrorCannotListInterfaces); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 &endpoints)); | 671 &endpoints)); |
554 } | 672 } |
555 } | 673 } |
556 | 674 |
557 SetResult(result_.release()); | 675 SetResult(result_.release()); |
558 AsyncWorkCompleted(); | 676 AsyncWorkCompleted(); |
559 } | 677 } |
560 | 678 |
561 bool UsbListInterfacesFunction::ConvertDirectionSafely( | 679 bool UsbListInterfacesFunction::ConvertDirectionSafely( |
562 const UsbEndpointDirection& input, | 680 const UsbEndpointDirection& input, |
563 extensions::api::usb::Direction* output) { | 681 usb::Direction* output) { |
564 const bool converted = ConvertDirectionToApi(input, output); | 682 const bool converted = ConvertDirectionToApi(input, output); |
565 if (!converted) | 683 if (!converted) |
566 SetError(kErrorConvertDirection); | 684 SetError(kErrorConvertDirection); |
567 return converted; | 685 return converted; |
568 } | 686 } |
569 | 687 |
570 bool UsbListInterfacesFunction::ConvertSynchronizationTypeSafely( | 688 bool UsbListInterfacesFunction::ConvertSynchronizationTypeSafely( |
571 const UsbSynchronizationType& input, | 689 const UsbSynchronizationType& input, |
572 extensions::api::usb::SynchronizationType* output) { | 690 usb::SynchronizationType* output) { |
573 const bool converted = ConvertSynchronizationTypeToApi(input, output); | 691 const bool converted = ConvertSynchronizationTypeToApi(input, output); |
574 if (!converted) | 692 if (!converted) |
575 SetError(kErrorConvertSynchronizationType); | 693 SetError(kErrorConvertSynchronizationType); |
576 return converted; | 694 return converted; |
577 } | 695 } |
578 | 696 |
579 bool UsbListInterfacesFunction::ConvertTransferTypeSafely( | 697 bool UsbListInterfacesFunction::ConvertTransferTypeSafely( |
580 const UsbTransferType& input, | 698 const UsbTransferType& input, |
581 extensions::api::usb::TransferType* output) { | 699 usb::TransferType* output) { |
582 const bool converted = ConvertTransferTypeToApi(input, output); | 700 const bool converted = ConvertTransferTypeToApi(input, output); |
583 if (!converted) | 701 if (!converted) |
584 SetError(kErrorConvertTransferType); | 702 SetError(kErrorConvertTransferType); |
585 return converted; | 703 return converted; |
586 } | 704 } |
587 | 705 |
588 bool UsbListInterfacesFunction::ConvertUsageTypeSafely( | 706 bool UsbListInterfacesFunction::ConvertUsageTypeSafely( |
589 const UsbUsageType& input, | 707 const UsbUsageType& input, |
590 extensions::api::usb::UsageType* output) { | 708 usb::UsageType* output) { |
591 const bool converted = ConvertUsageTypeToApi(input, output); | 709 const bool converted = ConvertUsageTypeToApi(input, output); |
592 if (!converted) | 710 if (!converted) |
593 SetError(kErrorConvertUsageType); | 711 SetError(kErrorConvertUsageType); |
594 return converted; | 712 return converted; |
595 } | 713 } |
596 | 714 |
597 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {} | 715 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {} |
598 | 716 |
599 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} | 717 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} |
600 | 718 |
601 bool UsbCloseDeviceFunction::Prepare() { | 719 bool UsbCloseDeviceFunction::Prepare() { |
602 parameters_ = CloseDevice::Params::Create(*args_); | 720 parameters_ = CloseDevice::Params::Create(*args_); |
603 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 721 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
604 return true; | 722 return true; |
605 } | 723 } |
606 | 724 |
607 void UsbCloseDeviceFunction::AsyncWorkStart() { | 725 void UsbCloseDeviceFunction::AsyncWorkStart() { |
608 UsbDeviceResource* const resource = GetUsbDeviceResource( | 726 UsbDeviceResource* const resource = GetUsbDeviceResource( |
609 parameters_->device.handle); | 727 parameters_->handle.handle); |
610 if (!resource) { | 728 if (!resource) { |
611 CompleteWithError(kErrorNoDevice); | 729 CompleteWithError(kErrorNoDevice); |
612 return; | 730 return; |
613 } | 731 } |
614 | 732 |
615 resource->device()->Close(); | 733 resource->device()->Close(); |
616 RemoveUsbDeviceResource(parameters_->device.handle); | 734 RemoveUsbDeviceResource(parameters_->handle.handle); |
617 AsyncWorkCompleted(); | 735 AsyncWorkCompleted(); |
618 } | 736 } |
619 | 737 |
620 UsbClaimInterfaceFunction::UsbClaimInterfaceFunction() {} | 738 UsbClaimInterfaceFunction::UsbClaimInterfaceFunction() {} |
621 | 739 |
622 UsbClaimInterfaceFunction::~UsbClaimInterfaceFunction() {} | 740 UsbClaimInterfaceFunction::~UsbClaimInterfaceFunction() {} |
623 | 741 |
624 bool UsbClaimInterfaceFunction::Prepare() { | 742 bool UsbClaimInterfaceFunction::Prepare() { |
625 parameters_ = ClaimInterface::Params::Create(*args_); | 743 parameters_ = ClaimInterface::Params::Create(*args_); |
626 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 744 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
627 return true; | 745 return true; |
628 } | 746 } |
629 | 747 |
630 void UsbClaimInterfaceFunction::AsyncWorkStart() { | 748 void UsbClaimInterfaceFunction::AsyncWorkStart() { |
631 UsbDeviceResource* resource = | 749 UsbDeviceResource* resource = |
632 GetUsbDeviceResource(parameters_->device.handle); | 750 GetUsbDeviceResource(parameters_->handle.handle); |
633 if (!resource) { | 751 if (!resource) { |
634 CompleteWithError(kErrorNoDevice); | 752 CompleteWithError(kErrorNoDevice); |
635 return; | 753 return; |
636 } | 754 } |
637 | 755 |
638 bool success = | 756 bool success = |
639 resource->device()->ClaimInterface(parameters_->interface_number); | 757 resource->device()->ClaimInterface(parameters_->interface_number); |
640 | 758 |
641 if (!success) | 759 if (!success) |
642 SetError(kErrorCannotClaimInterface); | 760 SetError(kErrorCannotClaimInterface); |
643 AsyncWorkCompleted(); | 761 AsyncWorkCompleted(); |
644 } | 762 } |
645 | 763 |
646 UsbReleaseInterfaceFunction::UsbReleaseInterfaceFunction() {} | 764 UsbReleaseInterfaceFunction::UsbReleaseInterfaceFunction() {} |
647 | 765 |
648 UsbReleaseInterfaceFunction::~UsbReleaseInterfaceFunction() {} | 766 UsbReleaseInterfaceFunction::~UsbReleaseInterfaceFunction() {} |
649 | 767 |
650 bool UsbReleaseInterfaceFunction::Prepare() { | 768 bool UsbReleaseInterfaceFunction::Prepare() { |
651 parameters_ = ReleaseInterface::Params::Create(*args_); | 769 parameters_ = ReleaseInterface::Params::Create(*args_); |
652 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 770 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
653 return true; | 771 return true; |
654 } | 772 } |
655 | 773 |
656 void UsbReleaseInterfaceFunction::AsyncWorkStart() { | 774 void UsbReleaseInterfaceFunction::AsyncWorkStart() { |
657 UsbDeviceResource* resource = | 775 UsbDeviceResource* resource = |
658 GetUsbDeviceResource(parameters_->device.handle); | 776 GetUsbDeviceResource(parameters_->handle.handle); |
659 if (!resource) { | 777 if (!resource) { |
660 CompleteWithError(kErrorNoDevice); | 778 CompleteWithError(kErrorNoDevice); |
661 return; | 779 return; |
662 } | 780 } |
663 bool success = | 781 bool success = |
664 resource->device()->ReleaseInterface(parameters_->interface_number); | 782 resource->device()->ReleaseInterface(parameters_->interface_number); |
665 if (!success) | 783 if (!success) |
666 SetError(kErrorCannotReleaseInterface); | 784 SetError(kErrorCannotReleaseInterface); |
667 AsyncWorkCompleted(); | 785 AsyncWorkCompleted(); |
668 } | 786 } |
669 | 787 |
670 UsbSetInterfaceAlternateSettingFunction:: | 788 UsbSetInterfaceAlternateSettingFunction:: |
671 UsbSetInterfaceAlternateSettingFunction() {} | 789 UsbSetInterfaceAlternateSettingFunction() {} |
672 | 790 |
673 UsbSetInterfaceAlternateSettingFunction:: | 791 UsbSetInterfaceAlternateSettingFunction:: |
674 ~UsbSetInterfaceAlternateSettingFunction() {} | 792 ~UsbSetInterfaceAlternateSettingFunction() {} |
675 | 793 |
676 bool UsbSetInterfaceAlternateSettingFunction::Prepare() { | 794 bool UsbSetInterfaceAlternateSettingFunction::Prepare() { |
677 parameters_ = SetInterfaceAlternateSetting::Params::Create(*args_); | 795 parameters_ = SetInterfaceAlternateSetting::Params::Create(*args_); |
678 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 796 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
679 return true; | 797 return true; |
680 } | 798 } |
681 | 799 |
682 void UsbSetInterfaceAlternateSettingFunction::AsyncWorkStart() { | 800 void UsbSetInterfaceAlternateSettingFunction::AsyncWorkStart() { |
683 UsbDeviceResource* resource = | 801 UsbDeviceResource* resource = |
684 GetUsbDeviceResource(parameters_->device.handle); | 802 GetUsbDeviceResource(parameters_->handle.handle); |
685 if (!resource) { | 803 if (!resource) { |
686 CompleteWithError(kErrorNoDevice); | 804 CompleteWithError(kErrorNoDevice); |
687 return; | 805 return; |
688 } | 806 } |
689 | 807 |
690 bool success = resource->device()->SetInterfaceAlternateSetting( | 808 bool success = resource->device()->SetInterfaceAlternateSetting( |
691 parameters_->interface_number, | 809 parameters_->interface_number, |
692 parameters_->alternate_setting); | 810 parameters_->alternate_setting); |
693 if (!success) | 811 if (!success) |
694 SetError(kErrorCannotSetInterfaceAlternateSetting); | 812 SetError(kErrorCannotSetInterfaceAlternateSetting); |
695 | 813 |
696 AsyncWorkCompleted(); | 814 AsyncWorkCompleted(); |
697 } | 815 } |
698 | 816 |
699 UsbControlTransferFunction::UsbControlTransferFunction() {} | 817 UsbControlTransferFunction::UsbControlTransferFunction() {} |
700 | 818 |
701 UsbControlTransferFunction::~UsbControlTransferFunction() {} | 819 UsbControlTransferFunction::~UsbControlTransferFunction() {} |
702 | 820 |
703 bool UsbControlTransferFunction::Prepare() { | 821 bool UsbControlTransferFunction::Prepare() { |
704 parameters_ = ControlTransfer::Params::Create(*args_); | 822 parameters_ = ControlTransfer::Params::Create(*args_); |
705 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 823 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
706 return true; | 824 return true; |
707 } | 825 } |
708 | 826 |
709 void UsbControlTransferFunction::AsyncWorkStart() { | 827 void UsbControlTransferFunction::AsyncWorkStart() { |
710 UsbDeviceResource* const resource = GetUsbDeviceResource( | 828 UsbDeviceResource* const resource = GetUsbDeviceResource( |
711 parameters_->device.handle); | 829 parameters_->handle.handle); |
712 if (!resource) { | 830 if (!resource) { |
713 CompleteWithError(kErrorNoDevice); | 831 CompleteWithError(kErrorNoDevice); |
714 return; | 832 return; |
715 } | 833 } |
716 | 834 |
717 const ControlTransferInfo& transfer = parameters_->transfer_info; | 835 const ControlTransferInfo& transfer = parameters_->transfer_info; |
718 | 836 |
719 UsbEndpointDirection direction; | 837 UsbEndpointDirection direction; |
720 UsbDeviceHandle::TransferRequestType request_type; | 838 UsbDeviceHandle::TransferRequestType request_type; |
721 UsbDeviceHandle::TransferRecipient recipient; | 839 UsbDeviceHandle::TransferRecipient recipient; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 UsbBulkTransferFunction::~UsbBulkTransferFunction() {} | 876 UsbBulkTransferFunction::~UsbBulkTransferFunction() {} |
759 | 877 |
760 bool UsbBulkTransferFunction::Prepare() { | 878 bool UsbBulkTransferFunction::Prepare() { |
761 parameters_ = BulkTransfer::Params::Create(*args_); | 879 parameters_ = BulkTransfer::Params::Create(*args_); |
762 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 880 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
763 return true; | 881 return true; |
764 } | 882 } |
765 | 883 |
766 void UsbBulkTransferFunction::AsyncWorkStart() { | 884 void UsbBulkTransferFunction::AsyncWorkStart() { |
767 UsbDeviceResource* const resource = GetUsbDeviceResource( | 885 UsbDeviceResource* const resource = GetUsbDeviceResource( |
768 parameters_->device.handle); | 886 parameters_->handle.handle); |
769 if (!resource) { | 887 if (!resource) { |
770 CompleteWithError(kErrorNoDevice); | 888 CompleteWithError(kErrorNoDevice); |
771 return; | 889 return; |
772 } | 890 } |
773 | 891 |
774 const GenericTransferInfo& transfer = parameters_->transfer_info; | 892 const GenericTransferInfo& transfer = parameters_->transfer_info; |
775 | 893 |
776 UsbEndpointDirection direction; | 894 UsbEndpointDirection direction; |
777 size_t size = 0; | 895 size_t size = 0; |
778 | 896 |
(...skipping 28 matching lines...) Expand all Loading... |
807 UsbInterruptTransferFunction::~UsbInterruptTransferFunction() {} | 925 UsbInterruptTransferFunction::~UsbInterruptTransferFunction() {} |
808 | 926 |
809 bool UsbInterruptTransferFunction::Prepare() { | 927 bool UsbInterruptTransferFunction::Prepare() { |
810 parameters_ = InterruptTransfer::Params::Create(*args_); | 928 parameters_ = InterruptTransfer::Params::Create(*args_); |
811 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 929 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
812 return true; | 930 return true; |
813 } | 931 } |
814 | 932 |
815 void UsbInterruptTransferFunction::AsyncWorkStart() { | 933 void UsbInterruptTransferFunction::AsyncWorkStart() { |
816 UsbDeviceResource* const resource = GetUsbDeviceResource( | 934 UsbDeviceResource* const resource = GetUsbDeviceResource( |
817 parameters_->device.handle); | 935 parameters_->handle.handle); |
818 if (!resource) { | 936 if (!resource) { |
819 CompleteWithError(kErrorNoDevice); | 937 CompleteWithError(kErrorNoDevice); |
820 return; | 938 return; |
821 } | 939 } |
822 | 940 |
823 const GenericTransferInfo& transfer = parameters_->transfer_info; | 941 const GenericTransferInfo& transfer = parameters_->transfer_info; |
824 | 942 |
825 UsbEndpointDirection direction; | 943 UsbEndpointDirection direction; |
826 size_t size = 0; | 944 size_t size = 0; |
827 | 945 |
(...skipping 28 matching lines...) Expand all Loading... |
856 UsbIsochronousTransferFunction::~UsbIsochronousTransferFunction() {} | 974 UsbIsochronousTransferFunction::~UsbIsochronousTransferFunction() {} |
857 | 975 |
858 bool UsbIsochronousTransferFunction::Prepare() { | 976 bool UsbIsochronousTransferFunction::Prepare() { |
859 parameters_ = IsochronousTransfer::Params::Create(*args_); | 977 parameters_ = IsochronousTransfer::Params::Create(*args_); |
860 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 978 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
861 return true; | 979 return true; |
862 } | 980 } |
863 | 981 |
864 void UsbIsochronousTransferFunction::AsyncWorkStart() { | 982 void UsbIsochronousTransferFunction::AsyncWorkStart() { |
865 UsbDeviceResource* const resource = GetUsbDeviceResource( | 983 UsbDeviceResource* const resource = GetUsbDeviceResource( |
866 parameters_->device.handle); | 984 parameters_->handle.handle); |
867 if (!resource) { | 985 if (!resource) { |
868 CompleteWithError(kErrorNoDevice); | 986 CompleteWithError(kErrorNoDevice); |
869 return; | 987 return; |
870 } | 988 } |
871 | 989 |
872 const IsochronousTransferInfo& transfer = parameters_->transfer_info; | 990 const IsochronousTransferInfo& transfer = parameters_->transfer_info; |
873 const GenericTransferInfo& generic_transfer = transfer.transfer_info; | 991 const GenericTransferInfo& generic_transfer = transfer.transfer_info; |
874 | 992 |
875 size_t size = 0; | 993 size_t size = 0; |
876 UsbEndpointDirection direction; | 994 UsbEndpointDirection direction; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
923 UsbResetDeviceFunction::~UsbResetDeviceFunction() {} | 1041 UsbResetDeviceFunction::~UsbResetDeviceFunction() {} |
924 | 1042 |
925 bool UsbResetDeviceFunction::Prepare() { | 1043 bool UsbResetDeviceFunction::Prepare() { |
926 parameters_ = ResetDevice::Params::Create(*args_); | 1044 parameters_ = ResetDevice::Params::Create(*args_); |
927 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); | 1045 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); |
928 return true; | 1046 return true; |
929 } | 1047 } |
930 | 1048 |
931 void UsbResetDeviceFunction::AsyncWorkStart() { | 1049 void UsbResetDeviceFunction::AsyncWorkStart() { |
932 UsbDeviceResource* const resource = GetUsbDeviceResource( | 1050 UsbDeviceResource* const resource = GetUsbDeviceResource( |
933 parameters_->device.handle); | 1051 parameters_->handle.handle); |
934 if (!resource) { | 1052 if (!resource) { |
935 CompleteWithError(kErrorNoDevice); | 1053 CompleteWithError(kErrorNoDevice); |
936 return; | 1054 return; |
937 } | 1055 } |
938 | 1056 |
939 bool success = resource->device()->ResetDevice(); | 1057 bool success = resource->device()->ResetDevice(); |
940 if (!success) { | 1058 if (!success) { |
941 UsbDeviceResource* const resource = GetUsbDeviceResource( | 1059 UsbDeviceResource* const resource = GetUsbDeviceResource( |
942 parameters_->device.handle); | 1060 parameters_->handle.handle); |
943 if (!resource) { | 1061 if (!resource) { |
944 CompleteWithError(kErrorNoDevice); | 1062 CompleteWithError(kErrorNoDevice); |
945 return; | 1063 return; |
946 } | 1064 } |
947 resource->device()->Close(); | 1065 resource->device()->Close(); |
948 RemoveUsbDeviceResource(parameters_->device.handle); | 1066 RemoveUsbDeviceResource(parameters_->handle.handle); |
949 SetError(kErrorResetDevice); | 1067 SetError(kErrorResetDevice); |
950 SetResult(new base::FundamentalValue(false)); | 1068 SetResult(new base::FundamentalValue(false)); |
951 AsyncWorkCompleted(); | 1069 AsyncWorkCompleted(); |
952 return; | 1070 return; |
953 } | 1071 } |
954 SetResult(new base::FundamentalValue(true)); | 1072 SetResult(new base::FundamentalValue(true)); |
955 AsyncWorkCompleted(); | 1073 AsyncWorkCompleted(); |
956 } | 1074 } |
957 | 1075 |
958 } // namespace extensions | 1076 } // namespace extensions |
OLD | NEW |