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

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

Issue 558373002: Add chrome.usb.getConfiguration and expose extra descriptors. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased to tip of tree. Created 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/browser/api/usb/usb_api.h" 5 #include "extensions/browser/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 "base/message_loop/message_loop_proxy.h"
12 #include "device/core/device_client.h" 12 #include "device/core/device_client.h"
13 #include "device/usb/usb_device_handle.h" 13 #include "device/usb/usb_device_handle.h"
14 #include "device/usb/usb_service.h" 14 #include "device/usb/usb_service.h"
15 #include "extensions/browser/api/usb/usb_device_resource.h" 15 #include "extensions/browser/api/usb/usb_device_resource.h"
16 #include "extensions/browser/extension_system.h" 16 #include "extensions/browser/extension_system.h"
17 #include "extensions/common/api/usb.h" 17 #include "extensions/common/api/usb.h"
18 #include "extensions/common/permissions/permissions_data.h" 18 #include "extensions/common/permissions/permissions_data.h"
19 #include "extensions/common/permissions/usb_device_permission.h" 19 #include "extensions/common/permissions/usb_device_permission.h"
20 20
21 namespace usb = extensions::core_api::usb; 21 namespace usb = extensions::core_api::usb;
22 namespace BulkTransfer = usb::BulkTransfer; 22 namespace BulkTransfer = usb::BulkTransfer;
23 namespace ClaimInterface = usb::ClaimInterface; 23 namespace ClaimInterface = usb::ClaimInterface;
24 namespace CloseDevice = usb::CloseDevice; 24 namespace CloseDevice = usb::CloseDevice;
25 namespace ControlTransfer = usb::ControlTransfer; 25 namespace ControlTransfer = usb::ControlTransfer;
26 namespace FindDevices = usb::FindDevices; 26 namespace FindDevices = usb::FindDevices;
27 namespace GetDevices = usb::GetDevices; 27 namespace GetDevices = usb::GetDevices;
28 namespace InterruptTransfer = usb::InterruptTransfer; 28 namespace InterruptTransfer = usb::InterruptTransfer;
29 namespace IsochronousTransfer = usb::IsochronousTransfer; 29 namespace IsochronousTransfer = usb::IsochronousTransfer;
30 namespace GetConfiguration = usb::GetConfiguration;
30 namespace ListInterfaces = usb::ListInterfaces; 31 namespace ListInterfaces = usb::ListInterfaces;
31 namespace OpenDevice = usb::OpenDevice; 32 namespace OpenDevice = usb::OpenDevice;
32 namespace ReleaseInterface = usb::ReleaseInterface; 33 namespace ReleaseInterface = usb::ReleaseInterface;
33 namespace RequestAccess = usb::RequestAccess; 34 namespace RequestAccess = usb::RequestAccess;
34 namespace ResetDevice = usb::ResetDevice; 35 namespace ResetDevice = usb::ResetDevice;
35 namespace SetInterfaceAlternateSetting = usb::SetInterfaceAlternateSetting; 36 namespace SetInterfaceAlternateSetting = usb::SetInterfaceAlternateSetting;
36 37
37 using content::BrowserThread; 38 using content::BrowserThread;
38 using std::string; 39 using usb::ConfigDescriptor;
39 using std::vector;
40 using usb::ControlTransferInfo; 40 using usb::ControlTransferInfo;
41 using usb::ConnectionHandle; 41 using usb::ConnectionHandle;
42 using usb::Device; 42 using usb::Device;
43 using usb::Direction; 43 using usb::Direction;
44 using usb::EndpointDescriptor; 44 using usb::EndpointDescriptor;
45 using usb::GenericTransferInfo; 45 using usb::GenericTransferInfo;
46 using usb::InterfaceDescriptor; 46 using usb::InterfaceDescriptor;
47 using usb::IsochronousTransferInfo; 47 using usb::IsochronousTransferInfo;
48 using usb::Recipient; 48 using usb::Recipient;
49 using usb::RequestType; 49 using usb::RequestType;
(...skipping 27 matching lines...) Expand all
77 const char kErrorCancelled[] = "Transfer was cancelled."; 77 const char kErrorCancelled[] = "Transfer was cancelled.";
78 const char kErrorDisconnect[] = "Device disconnected."; 78 const char kErrorDisconnect[] = "Device disconnected.";
79 const char kErrorGeneric[] = "Transfer failed."; 79 const char kErrorGeneric[] = "Transfer failed.";
80 #if !defined(OS_CHROMEOS) 80 #if !defined(OS_CHROMEOS)
81 const char kErrorNotSupported[] = "Not supported on this platform."; 81 const char kErrorNotSupported[] = "Not supported on this platform.";
82 #endif 82 #endif
83 const char kErrorOverflow[] = "Inbound transfer overflow."; 83 const char kErrorOverflow[] = "Inbound transfer overflow.";
84 const char kErrorStalled[] = "Transfer stalled."; 84 const char kErrorStalled[] = "Transfer stalled.";
85 const char kErrorTimeout[] = "Transfer timed out."; 85 const char kErrorTimeout[] = "Transfer timed out.";
86 const char kErrorTransferLength[] = "Transfer length is insufficient."; 86 const char kErrorTransferLength[] = "Transfer length is insufficient.";
87
88 const char kErrorCannotListInterfaces[] = "Error listing interfaces.";
89 const char kErrorCannotClaimInterface[] = "Error claiming interface."; 87 const char kErrorCannotClaimInterface[] = "Error claiming interface.";
90 const char kErrorCannotReleaseInterface[] = "Error releasing interface."; 88 const char kErrorCannotReleaseInterface[] = "Error releasing interface.";
91 const char kErrorCannotSetInterfaceAlternateSetting[] = 89 const char kErrorCannotSetInterfaceAlternateSetting[] =
92 "Error setting alternate interface setting."; 90 "Error setting alternate interface setting.";
93 const char kErrorConvertDirection[] = "Invalid transfer direction."; 91 const char kErrorConvertDirection[] = "Invalid transfer direction.";
94 const char kErrorConvertRecipient[] = "Invalid transfer recipient."; 92 const char kErrorConvertRecipient[] = "Invalid transfer recipient.";
95 const char kErrorConvertRequestType[] = "Invalid request type."; 93 const char kErrorConvertRequestType[] = "Invalid request type.";
96 const char kErrorConvertSynchronizationType[] = "Invalid synchronization type";
97 const char kErrorConvertTransferType[] = "Invalid endpoint type.";
98 const char kErrorConvertUsageType[] = "Invalid usage type.";
99 const char kErrorMalformedParameters[] = "Error parsing parameters."; 94 const char kErrorMalformedParameters[] = "Error parsing parameters.";
100 const char kErrorNoDevice[] = "No such device."; 95 const char kErrorNoDevice[] = "No such device.";
101 const char kErrorPermissionDenied[] = "Permission to access device was denied"; 96 const char kErrorPermissionDenied[] = "Permission to access device was denied";
102 const char kErrorInvalidTransferLength[] = 97 const char kErrorInvalidTransferLength[] =
103 "Transfer length must be a positive number less than 104,857,600."; 98 "Transfer length must be a positive number less than 104,857,600.";
104 const char kErrorInvalidNumberOfPackets[] = 99 const char kErrorInvalidNumberOfPackets[] =
105 "Number of packets must be a positive number less than 4,194,304."; 100 "Number of packets must be a positive number less than 4,194,304.";
106 const char kErrorInvalidPacketLength[] = 101 const char kErrorInvalidPacketLength[] =
107 "Packet length must be a positive number less than 65,536."; 102 "Packet length must be a positive number less than 65,536.";
108 const char kErrorResetDevice[] = 103 const char kErrorResetDevice[] =
109 "Error resetting the device. The device has been closed."; 104 "Error resetting the device. The device has been closed.";
110 105
111 const size_t kMaxTransferLength = 100 * 1024 * 1024; 106 const size_t kMaxTransferLength = 100 * 1024 * 1024;
112 const int kMaxPackets = 4 * 1024 * 1024; 107 const int kMaxPackets = 4 * 1024 * 1024;
113 const int kMaxPacketLength = 64 * 1024; 108 const int kMaxPacketLength = 64 * 1024;
114 109
115 bool ConvertDirectionToApi(const UsbEndpointDirection& input, 110 bool ConvertDirectionFromApi(const Direction& input,
116 Direction* output) { 111 UsbEndpointDirection* output) {
117 switch (input) {
118 case device::USB_DIRECTION_INBOUND:
119 *output = usb::DIRECTION_IN;
120 return true;
121 case device::USB_DIRECTION_OUTBOUND:
122 *output = usb::DIRECTION_OUT;
123 return true;
124 default:
125 NOTREACHED();
126 return false;
127 }
128 }
129
130 bool ConvertSynchronizationTypeToApi(const UsbSynchronizationType& input,
131 usb::SynchronizationType* output) {
132 switch (input) {
133 case device::USB_SYNCHRONIZATION_NONE:
134 *output = usb::SYNCHRONIZATION_TYPE_NONE;
135 return true;
136 case device::USB_SYNCHRONIZATION_ASYNCHRONOUS:
137 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS;
138 return true;
139 case device::USB_SYNCHRONIZATION_ADAPTIVE:
140 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE;
141 return true;
142 case device::USB_SYNCHRONIZATION_SYNCHRONOUS:
143 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS;
144 return true;
145 default:
146 NOTREACHED();
147 return false;
148 }
149 }
150
151 bool ConvertTransferTypeToApi(const UsbTransferType& input,
152 usb::TransferType* output) {
153 switch (input) {
154 case device::USB_TRANSFER_CONTROL:
155 *output = usb::TRANSFER_TYPE_CONTROL;
156 return true;
157 case device::USB_TRANSFER_INTERRUPT:
158 *output = usb::TRANSFER_TYPE_INTERRUPT;
159 return true;
160 case device::USB_TRANSFER_ISOCHRONOUS:
161 *output = usb::TRANSFER_TYPE_ISOCHRONOUS;
162 return true;
163 case device::USB_TRANSFER_BULK:
164 *output = usb::TRANSFER_TYPE_BULK;
165 return true;
166 default:
167 NOTREACHED();
168 return false;
169 }
170 }
171
172 bool ConvertUsageTypeToApi(const UsbUsageType& input, usb::UsageType* output) {
173 switch (input) {
174 case device::USB_USAGE_DATA:
175 *output = usb::USAGE_TYPE_DATA;
176 return true;
177 case device::USB_USAGE_FEEDBACK:
178 *output = usb::USAGE_TYPE_FEEDBACK;
179 return true;
180 case device::USB_USAGE_EXPLICIT_FEEDBACK:
181 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK;
182 return true;
183 default:
184 NOTREACHED();
185 return false;
186 }
187 }
188
189 bool ConvertDirection(const Direction& input, UsbEndpointDirection* output) {
190 switch (input) { 112 switch (input) {
191 case usb::DIRECTION_IN: 113 case usb::DIRECTION_IN:
192 *output = device::USB_DIRECTION_INBOUND; 114 *output = device::USB_DIRECTION_INBOUND;
193 return true; 115 return true;
194 case usb::DIRECTION_OUT: 116 case usb::DIRECTION_OUT:
195 *output = device::USB_DIRECTION_OUTBOUND; 117 *output = device::USB_DIRECTION_OUTBOUND;
196 return true; 118 return true;
197 default: 119 default:
198 NOTREACHED(); 120 NOTREACHED();
199 return false; 121 return false;
200 } 122 }
201 } 123 }
202 124
203 bool ConvertRequestType(const RequestType& input, 125 bool ConvertRequestTypeFromApi(const RequestType& input,
204 UsbDeviceHandle::TransferRequestType* output) { 126 UsbDeviceHandle::TransferRequestType* output) {
205 switch (input) { 127 switch (input) {
206 case usb::REQUEST_TYPE_STANDARD: 128 case usb::REQUEST_TYPE_STANDARD:
207 *output = UsbDeviceHandle::STANDARD; 129 *output = UsbDeviceHandle::STANDARD;
208 return true; 130 return true;
209 case usb::REQUEST_TYPE_CLASS: 131 case usb::REQUEST_TYPE_CLASS:
210 *output = UsbDeviceHandle::CLASS; 132 *output = UsbDeviceHandle::CLASS;
211 return true; 133 return true;
212 case usb::REQUEST_TYPE_VENDOR: 134 case usb::REQUEST_TYPE_VENDOR:
213 *output = UsbDeviceHandle::VENDOR; 135 *output = UsbDeviceHandle::VENDOR;
214 return true; 136 return true;
215 case usb::REQUEST_TYPE_RESERVED: 137 case usb::REQUEST_TYPE_RESERVED:
216 *output = UsbDeviceHandle::RESERVED; 138 *output = UsbDeviceHandle::RESERVED;
217 return true; 139 return true;
218 default: 140 default:
219 NOTREACHED(); 141 NOTREACHED();
220 return false; 142 return false;
221 } 143 }
222 } 144 }
223 145
224 bool ConvertRecipient(const Recipient& input, 146 bool ConvertRecipientFromApi(const Recipient& input,
225 UsbDeviceHandle::TransferRecipient* output) { 147 UsbDeviceHandle::TransferRecipient* output) {
226 switch (input) { 148 switch (input) {
227 case usb::RECIPIENT_DEVICE: 149 case usb::RECIPIENT_DEVICE:
228 *output = UsbDeviceHandle::DEVICE; 150 *output = UsbDeviceHandle::DEVICE;
229 return true; 151 return true;
230 case usb::RECIPIENT_INTERFACE: 152 case usb::RECIPIENT_INTERFACE:
231 *output = UsbDeviceHandle::INTERFACE; 153 *output = UsbDeviceHandle::INTERFACE;
232 return true; 154 return true;
233 case usb::RECIPIENT_ENDPOINT: 155 case usb::RECIPIENT_ENDPOINT:
234 *output = UsbDeviceHandle::ENDPOINT; 156 *output = UsbDeviceHandle::ENDPOINT;
235 return true; 157 return true;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 } else if (direction == device::USB_DIRECTION_OUTBOUND) { 201 } else if (direction == device::USB_DIRECTION_OUTBOUND) {
280 if (input.data.get() && size <= input.data->size()) { 202 if (input.data.get() && size <= input.data->size()) {
281 memcpy(buffer->data(), input.data->data(), size); 203 memcpy(buffer->data(), input.data->data(), size);
282 return buffer; 204 return buffer;
283 } 205 }
284 } 206 }
285 NOTREACHED(); 207 NOTREACHED();
286 return NULL; 208 return NULL;
287 } 209 }
288 210
289 const char* ConvertTransferStatusToErrorString(const UsbTransferStatus status) { 211 const char* ConvertTransferStatusToApi(const UsbTransferStatus status) {
290 switch (status) { 212 switch (status) {
291 case device::USB_TRANSFER_COMPLETED: 213 case device::USB_TRANSFER_COMPLETED:
292 return ""; 214 return "";
293 case device::USB_TRANSFER_ERROR: 215 case device::USB_TRANSFER_ERROR:
294 return kErrorGeneric; 216 return kErrorGeneric;
295 case device::USB_TRANSFER_TIMEOUT: 217 case device::USB_TRANSFER_TIMEOUT:
296 return kErrorTimeout; 218 return kErrorTimeout;
297 case device::USB_TRANSFER_CANCELLED: 219 case device::USB_TRANSFER_CANCELLED:
298 return kErrorCancelled; 220 return kErrorCancelled;
299 case device::USB_TRANSFER_STALLED: 221 case device::USB_TRANSFER_STALLED:
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 } 295 }
374 296
375 base::Value* PopulateDevice(UsbDevice* device) { 297 base::Value* PopulateDevice(UsbDevice* device) {
376 Device result; 298 Device result;
377 result.device = device->unique_id(); 299 result.device = device->unique_id();
378 result.vendor_id = device->vendor_id(); 300 result.vendor_id = device->vendor_id();
379 result.product_id = device->product_id(); 301 result.product_id = device->product_id();
380 return result.ToValue().release(); 302 return result.ToValue().release();
381 } 303 }
382 304
383 base::Value* PopulateInterfaceDescriptor( 305 TransferType ConvertTransferTypeToApi(const UsbTransferType& input) {
384 int interface_number, 306 switch (input) {
385 int alternate_setting, 307 case device::USB_TRANSFER_CONTROL:
386 int interface_class, 308 return usb::TRANSFER_TYPE_CONTROL;
387 int interface_subclass, 309 case device::USB_TRANSFER_INTERRUPT:
388 int interface_protocol, 310 return usb::TRANSFER_TYPE_INTERRUPT;
389 std::vector<linked_ptr<EndpointDescriptor> >* endpoints) { 311 case device::USB_TRANSFER_ISOCHRONOUS:
390 InterfaceDescriptor descriptor; 312 return usb::TRANSFER_TYPE_ISOCHRONOUS;
391 descriptor.interface_number = interface_number; 313 case device::USB_TRANSFER_BULK:
392 descriptor.alternate_setting = alternate_setting; 314 return usb::TRANSFER_TYPE_BULK;
393 descriptor.interface_class = interface_class; 315 default:
394 descriptor.interface_subclass = interface_subclass; 316 NOTREACHED();
395 descriptor.interface_protocol = interface_protocol; 317 return usb::TRANSFER_TYPE_NONE;
396 descriptor.endpoints = *endpoints; 318 }
397 return descriptor.ToValue().release(); 319 }
320
321 Direction ConvertDirectionToApi(const UsbEndpointDirection& input) {
322 switch (input) {
323 case device::USB_DIRECTION_INBOUND:
324 return usb::DIRECTION_IN;
325 case device::USB_DIRECTION_OUTBOUND:
326 return usb::DIRECTION_OUT;
327 default:
328 NOTREACHED();
329 return usb::DIRECTION_NONE;
330 }
331 }
332
333 SynchronizationType ConvertSynchronizationTypeToApi(
334 const UsbSynchronizationType& input) {
335 switch (input) {
336 case device::USB_SYNCHRONIZATION_NONE:
337 return usb::SYNCHRONIZATION_TYPE_NONE;
338 case device::USB_SYNCHRONIZATION_ASYNCHRONOUS:
339 return usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS;
340 case device::USB_SYNCHRONIZATION_ADAPTIVE:
341 return usb::SYNCHRONIZATION_TYPE_ADAPTIVE;
342 case device::USB_SYNCHRONIZATION_SYNCHRONOUS:
343 return usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS;
344 default:
345 NOTREACHED();
346 return usb::SYNCHRONIZATION_TYPE_NONE;
347 }
348 }
349
350 UsageType ConvertUsageTypeToApi(const UsbUsageType& input) {
351 switch (input) {
352 case device::USB_USAGE_DATA:
353 return usb::USAGE_TYPE_DATA;
354 case device::USB_USAGE_FEEDBACK:
355 return usb::USAGE_TYPE_FEEDBACK;
356 case device::USB_USAGE_EXPLICIT_FEEDBACK:
357 return usb::USAGE_TYPE_EXPLICITFEEDBACK;
358 default:
359 NOTREACHED();
360 return usb::USAGE_TYPE_NONE;
361 }
362 }
363
364 void ConvertEndpointDescriptor(const UsbEndpointDescriptor& input,
365 EndpointDescriptor* output) {
366 output->address = input.address;
367 output->type = ConvertTransferTypeToApi(input.transfer_type);
368 output->direction = ConvertDirectionToApi(input.direction);
369 output->maximum_packet_size = input.maximum_packet_size;
370 output->synchronization =
371 ConvertSynchronizationTypeToApi(input.synchronization_type);
372 output->usage = ConvertUsageTypeToApi(input.usage_type);
373 output->polling_interval.reset(new int(input.polling_interval));
374 if (input.extra_data.size() > 0) {
375 output->extra_data =
376 std::string(reinterpret_cast<const char*>(&input.extra_data[0]),
377 input.extra_data.size());
378 }
379 }
380
381 void ConvertInterfaceDescriptor(const UsbInterfaceDescriptor& input,
382 InterfaceDescriptor* output) {
383 output->interface_number = input.interface_number;
384 output->alternate_setting = input.alternate_setting;
385 output->interface_class = input.interface_class;
386 output->interface_subclass = input.interface_subclass;
387 output->interface_protocol = input.interface_protocol;
388 for (UsbEndpointDescriptor::Iterator endpointIt = input.endpoints.begin();
389 endpointIt != input.endpoints.end();
390 ++endpointIt) {
391 linked_ptr<EndpointDescriptor> endpoint(new EndpointDescriptor);
392 ConvertEndpointDescriptor(*endpointIt, endpoint.get());
393 output->endpoints.push_back(endpoint);
394 }
395 if (input.extra_data.size() > 0) {
396 output->extra_data =
397 std::string(reinterpret_cast<const char*>(&input.extra_data[0]),
398 input.extra_data.size());
399 }
400 }
401
402 void ConvertConfigDescriptor(const UsbConfigDescriptor& input,
403 ConfigDescriptor* output) {
404 output->configuration_value = input.configuration_value;
405 output->self_powered = input.self_powered;
406 output->remote_wakeup = input.remote_wakeup;
407 output->max_power = input.maximum_power;
408 for (UsbInterfaceDescriptor::Iterator interfaceIt = input.interfaces.begin();
409 interfaceIt != input.interfaces.end();
410 ++interfaceIt) {
411 linked_ptr<InterfaceDescriptor> interface(new InterfaceDescriptor);
412 ConvertInterfaceDescriptor(*interfaceIt, interface.get());
413 output->interfaces.push_back(interface);
414 }
415 if (input.extra_data.size() > 0) {
416 output->extra_data =
417 std::string(reinterpret_cast<const char*>(&input.extra_data[0]),
418 input.extra_data.size());
419 }
398 } 420 }
399 421
400 } // namespace 422 } // namespace
401 423
402 namespace extensions { 424 namespace extensions {
403 425
404 UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(NULL) { 426 UsbAsyncApiFunction::UsbAsyncApiFunction() : manager_(NULL) {
405 } 427 }
406 428
407 UsbAsyncApiFunction::~UsbAsyncApiFunction() { 429 UsbAsyncApiFunction::~UsbAsyncApiFunction() {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() { 531 UsbAsyncApiTransferFunction::UsbAsyncApiTransferFunction() {
510 } 532 }
511 533
512 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() { 534 UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() {
513 } 535 }
514 536
515 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, 537 void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status,
516 scoped_refptr<net::IOBuffer> data, 538 scoped_refptr<net::IOBuffer> data,
517 size_t length) { 539 size_t length) {
518 if (status != device::USB_TRANSFER_COMPLETED) 540 if (status != device::USB_TRANSFER_COMPLETED)
519 SetError(ConvertTransferStatusToErrorString(status)); 541 SetError(ConvertTransferStatusToApi(status));
520 542
521 SetResult(CreateTransferInfo(status, data, length)); 543 SetResult(CreateTransferInfo(status, data, length));
522 AsyncWorkCompleted(); 544 AsyncWorkCompleted();
523 } 545 }
524 546
525 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( 547 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely(
526 const Direction& input, 548 const Direction& input,
527 UsbEndpointDirection* output) { 549 UsbEndpointDirection* output) {
528 const bool converted = ConvertDirection(input, output); 550 const bool converted = ConvertDirectionFromApi(input, output);
529 if (!converted) 551 if (!converted)
530 SetError(kErrorConvertDirection); 552 SetError(kErrorConvertDirection);
531 return converted; 553 return converted;
532 } 554 }
533 555
534 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely( 556 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely(
535 const RequestType& input, 557 const RequestType& input,
536 UsbDeviceHandle::TransferRequestType* output) { 558 UsbDeviceHandle::TransferRequestType* output) {
537 const bool converted = ConvertRequestType(input, output); 559 const bool converted = ConvertRequestTypeFromApi(input, output);
538 if (!converted) 560 if (!converted)
539 SetError(kErrorConvertRequestType); 561 SetError(kErrorConvertRequestType);
540 return converted; 562 return converted;
541 } 563 }
542 564
543 bool UsbAsyncApiTransferFunction::ConvertRecipientSafely( 565 bool UsbAsyncApiTransferFunction::ConvertRecipientSafely(
544 const Recipient& input, 566 const Recipient& input,
545 UsbDeviceHandle::TransferRecipient* output) { 567 UsbDeviceHandle::TransferRecipient* output) {
546 const bool converted = ConvertRecipient(input, output); 568 const bool converted = ConvertRecipientFromApi(input, output);
547 if (!converted) 569 if (!converted)
548 SetError(kErrorConvertRecipient); 570 SetError(kErrorConvertRecipient);
549 return converted; 571 return converted;
550 } 572 }
551 573
552 UsbFindDevicesFunction::UsbFindDevicesFunction() { 574 UsbFindDevicesFunction::UsbFindDevicesFunction() {
553 } 575 }
554 576
555 UsbFindDevicesFunction::~UsbFindDevicesFunction() { 577 UsbFindDevicesFunction::~UsbFindDevicesFunction() {
556 } 578 }
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 return; 757 return;
736 } 758 }
737 759
738 SetResult(PopulateConnectionHandle( 760 SetResult(PopulateConnectionHandle(
739 manager_->Add(new UsbDeviceResource(extension_->id(), handle_)), 761 manager_->Add(new UsbDeviceResource(extension_->id(), handle_)),
740 handle_->GetDevice()->vendor_id(), 762 handle_->GetDevice()->vendor_id(),
741 handle_->GetDevice()->product_id())); 763 handle_->GetDevice()->product_id()));
742 AsyncWorkCompleted(); 764 AsyncWorkCompleted();
743 } 765 }
744 766
767 UsbGetConfigurationFunction::UsbGetConfigurationFunction() {
768 }
769
770 UsbGetConfigurationFunction::~UsbGetConfigurationFunction() {
771 }
772
773 bool UsbGetConfigurationFunction::Prepare() {
774 parameters_ = GetConfiguration::Params::Create(*args_);
775 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
776 return true;
777 }
778
779 void UsbGetConfigurationFunction::AsyncWorkStart() {
780 scoped_refptr<UsbDeviceHandle> device_handle =
781 GetDeviceHandleOrCompleteWithError(parameters_->handle);
782 if (!device_handle.get()) {
783 return;
784 }
785
786 ConfigDescriptor config;
787 ConvertConfigDescriptor(device_handle->GetDevice()->GetConfiguration(),
788 &config);
789
790 SetResult(config.ToValue().release());
791 AsyncWorkCompleted();
792 }
793
745 UsbListInterfacesFunction::UsbListInterfacesFunction() { 794 UsbListInterfacesFunction::UsbListInterfacesFunction() {
746 } 795 }
747 796
748 UsbListInterfacesFunction::~UsbListInterfacesFunction() { 797 UsbListInterfacesFunction::~UsbListInterfacesFunction() {
749 } 798 }
750 799
751 bool UsbListInterfacesFunction::Prepare() { 800 bool UsbListInterfacesFunction::Prepare() {
752 parameters_ = ListInterfaces::Params::Create(*args_); 801 parameters_ = ListInterfaces::Params::Create(*args_);
753 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 802 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
754 return true; 803 return true;
755 } 804 }
756 805
757 void UsbListInterfacesFunction::AsyncWorkStart() { 806 void UsbListInterfacesFunction::AsyncWorkStart() {
758 scoped_refptr<UsbDeviceHandle> device_handle = 807 scoped_refptr<UsbDeviceHandle> device_handle =
759 GetDeviceHandleOrCompleteWithError(parameters_->handle); 808 GetDeviceHandleOrCompleteWithError(parameters_->handle);
760 if (!device_handle.get()) 809 if (!device_handle.get()) {
761 return; 810 return;
811 }
762 812
763 const UsbConfigDescriptor& config = 813 ConfigDescriptor config;
764 device_handle->GetDevice()->GetConfiguration(); 814 ConvertConfigDescriptor(device_handle->GetDevice()->GetConfiguration(),
815 &config);
765 816
766 scoped_ptr<base::ListValue> result(new base::ListValue()); 817 scoped_ptr<base::ListValue> result(new base::ListValue);
767 818 for (size_t i = 0; i < config.interfaces.size(); ++i) {
768 for (UsbInterfaceDescriptor::Iterator interfaceIt = config.interfaces.begin(); 819 result->Append(config.interfaces[i]->ToValue().release());
769 interfaceIt != config.interfaces.end();
770 ++interfaceIt) {
771 std::vector<linked_ptr<EndpointDescriptor> > endpoints;
772
773 for (UsbEndpointDescriptor::Iterator endpointIt =
774 interfaceIt->endpoints.begin();
775 endpointIt != interfaceIt->endpoints.end();
776 ++endpointIt) {
777 linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor());
778
779 TransferType type;
780 Direction direction;
781 SynchronizationType synchronization;
782 UsageType usage;
783
784 if (!ConvertTransferTypeSafely(endpointIt->transfer_type, &type) ||
785 !ConvertDirectionSafely(endpointIt->direction, &direction) ||
786 !ConvertSynchronizationTypeSafely(endpointIt->synchronization_type,
787 &synchronization) ||
788 !ConvertUsageTypeSafely(endpointIt->usage_type, &usage)) {
789 SetError(kErrorCannotListInterfaces);
790 AsyncWorkCompleted();
791 return;
792 }
793
794 endpoint_desc->address = endpointIt->address;
795 endpoint_desc->type = type;
796 endpoint_desc->direction = direction;
797 endpoint_desc->maximum_packet_size = endpointIt->maximum_packet_size;
798 endpoint_desc->synchronization = synchronization;
799 endpoint_desc->usage = usage;
800 endpoint_desc->polling_interval.reset(
801 new int(endpointIt->polling_interval));
802
803 endpoints.push_back(endpoint_desc);
804 }
805
806 result->Append(PopulateInterfaceDescriptor(interfaceIt->interface_number,
807 interfaceIt->alternate_setting,
808 interfaceIt->interface_class,
809 interfaceIt->interface_subclass,
810 interfaceIt->interface_protocol,
811 &endpoints));
812 } 820 }
813 821
814 SetResult(result.release()); 822 SetResult(result.release());
815 AsyncWorkCompleted(); 823 AsyncWorkCompleted();
816 } 824 }
817 825
818 bool UsbListInterfacesFunction::ConvertDirectionSafely(
819 const UsbEndpointDirection& input,
820 usb::Direction* output) {
821 const bool converted = ConvertDirectionToApi(input, output);
822 if (!converted)
823 SetError(kErrorConvertDirection);
824 return converted;
825 }
826
827 bool UsbListInterfacesFunction::ConvertSynchronizationTypeSafely(
828 const UsbSynchronizationType& input,
829 usb::SynchronizationType* output) {
830 const bool converted = ConvertSynchronizationTypeToApi(input, output);
831 if (!converted)
832 SetError(kErrorConvertSynchronizationType);
833 return converted;
834 }
835
836 bool UsbListInterfacesFunction::ConvertTransferTypeSafely(
837 const UsbTransferType& input,
838 usb::TransferType* output) {
839 const bool converted = ConvertTransferTypeToApi(input, output);
840 if (!converted)
841 SetError(kErrorConvertTransferType);
842 return converted;
843 }
844
845 bool UsbListInterfacesFunction::ConvertUsageTypeSafely(
846 const UsbUsageType& input,
847 usb::UsageType* output) {
848 const bool converted = ConvertUsageTypeToApi(input, output);
849 if (!converted)
850 SetError(kErrorConvertUsageType);
851 return converted;
852 }
853
854 UsbCloseDeviceFunction::UsbCloseDeviceFunction() { 826 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {
855 } 827 }
856 828
857 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() { 829 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {
858 } 830 }
859 831
860 bool UsbCloseDeviceFunction::Prepare() { 832 bool UsbCloseDeviceFunction::Prepare() {
861 parameters_ = CloseDevice::Params::Create(*args_); 833 parameters_ = CloseDevice::Params::Create(*args_);
862 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 834 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
863 return true; 835 return true;
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
1198 SetResult(new base::FundamentalValue(false)); 1170 SetResult(new base::FundamentalValue(false));
1199 CompleteWithError(kErrorResetDevice); 1171 CompleteWithError(kErrorResetDevice);
1200 return; 1172 return;
1201 } 1173 }
1202 1174
1203 SetResult(new base::FundamentalValue(true)); 1175 SetResult(new base::FundamentalValue(true));
1204 AsyncWorkCompleted(); 1176 AsyncWorkCompleted();
1205 } 1177 }
1206 1178
1207 } // namespace extensions 1179 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/usb/usb_api.h ('k') | extensions/browser/extension_function_histogram_value.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698