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