Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: chrome/browser/extensions/api/usb/usb_api.cc

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

Powered by Google App Engine
This is Rietveld 408576698