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

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

Issue 12471013: Add chrome.usb.listInterfaces API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase of PS10 Created 7 years, 7 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 | Annotate | Revision Log
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 "chrome/browser/extensions/api/usb/usb_device_resource.h" 11 #include "chrome/browser/extensions/api/usb/usb_device_resource.h"
12 #include "chrome/browser/extensions/extension_system.h" 12 #include "chrome/browser/extensions/extension_system.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/usb/usb_service.h" 14 #include "chrome/browser/usb/usb_service.h"
15 #include "chrome/browser/usb/usb_service_factory.h" 15 #include "chrome/browser/usb/usb_service_factory.h"
16 #include "chrome/common/extensions/api/usb.h" 16 #include "chrome/common/extensions/api/usb.h"
17 #include "chrome/common/extensions/permissions/permissions_data.h" 17 #include "chrome/common/extensions/permissions/permissions_data.h"
18 #include "chrome/common/extensions/permissions/usb_device_permission.h" 18 #include "chrome/common/extensions/permissions/usb_device_permission.h"
19 19
20 namespace BulkTransfer = extensions::api::usb::BulkTransfer; 20 namespace BulkTransfer = extensions::api::usb::BulkTransfer;
21 namespace ClaimInterface = extensions::api::usb::ClaimInterface; 21 namespace ClaimInterface = extensions::api::usb::ClaimInterface;
22 namespace ListInterfaces = extensions::api::usb::ListInterfaces;
22 namespace CloseDevice = extensions::api::usb::CloseDevice; 23 namespace CloseDevice = extensions::api::usb::CloseDevice;
23 namespace ControlTransfer = extensions::api::usb::ControlTransfer; 24 namespace ControlTransfer = extensions::api::usb::ControlTransfer;
24 namespace FindDevices = extensions::api::usb::FindDevices; 25 namespace FindDevices = extensions::api::usb::FindDevices;
25 namespace InterruptTransfer = extensions::api::usb::InterruptTransfer; 26 namespace InterruptTransfer = extensions::api::usb::InterruptTransfer;
26 namespace IsochronousTransfer = extensions::api::usb::IsochronousTransfer; 27 namespace IsochronousTransfer = extensions::api::usb::IsochronousTransfer;
27 namespace ReleaseInterface = extensions::api::usb::ReleaseInterface; 28 namespace ReleaseInterface = extensions::api::usb::ReleaseInterface;
28 namespace ResetDevice = extensions::api::usb::ResetDevice; 29 namespace ResetDevice = extensions::api::usb::ResetDevice;
29 namespace SetInterfaceAlternateSetting = 30 namespace SetInterfaceAlternateSetting =
30 extensions::api::usb::SetInterfaceAlternateSetting; 31 extensions::api::usb::SetInterfaceAlternateSetting;
31 namespace usb = extensions::api::usb; 32 namespace usb = extensions::api::usb;
32 33
33 using std::string; 34 using std::string;
34 using std::vector; 35 using std::vector;
35 using usb::ControlTransferInfo; 36 using usb::ControlTransferInfo;
36 using usb::Device; 37 using usb::Device;
37 using usb::Direction; 38 using usb::Direction;
39 using usb::EndpointDescriptor;
38 using usb::GenericTransferInfo; 40 using usb::GenericTransferInfo;
41 using usb::InterfaceDescriptor;
39 using usb::IsochronousTransferInfo; 42 using usb::IsochronousTransferInfo;
40 using usb::Recipient; 43 using usb::Recipient;
41 using usb::RequestType; 44 using usb::RequestType;
45 using usb::SynchronizationType;
46 using usb::TransferType;
47 using usb::UsageType;
42 48
43 namespace { 49 namespace {
44 50
45 static const char* kDataKey = "data"; 51 static const char* kDataKey = "data";
46 static const char* kResultCodeKey = "resultCode"; 52 static const char* kResultCodeKey = "resultCode";
47 53
48 static const char* kErrorCancelled = "Transfer was cancelled."; 54 static const char* kErrorCancelled = "Transfer was cancelled.";
49 static const char* kErrorDisconnect = "Device disconnected."; 55 static const char* kErrorDisconnect = "Device disconnected.";
50 static const char* kErrorGeneric = "Transfer failed."; 56 static const char* kErrorGeneric = "Transfer failed.";
51 static const char* kErrorOverflow = "Inbound transfer overflow."; 57 static const char* kErrorOverflow = "Inbound transfer overflow.";
52 static const char* kErrorStalled = "Transfer stalled."; 58 static const char* kErrorStalled = "Transfer stalled.";
53 static const char* kErrorTimeout = "Transfer timed out."; 59 static const char* kErrorTimeout = "Transfer timed out.";
54 static const char* kErrorTransferLength = "Transfer length is insufficient."; 60 static const char* kErrorTransferLength = "Transfer length is insufficient.";
55 61
62 static const char* kErrorCannotListInterfaces = "Error listing interfaces.";
56 static const char* kErrorCannotClaimInterface = "Error claiming interface."; 63 static const char* kErrorCannotClaimInterface = "Error claiming interface.";
57 static const char* kErrorCannotReleaseInterface = "Error releasing interface."; 64 static const char* kErrorCannotReleaseInterface = "Error releasing interface.";
58 static const char* kErrorCannotSetInterfaceAlternateSetting = 65 static const char* kErrorCannotSetInterfaceAlternateSetting =
59 "Error setting alternate interface setting."; 66 "Error setting alternate interface setting.";
60 static const char* kErrorConvertDirection = "Invalid transfer direction."; 67 static const char* kErrorConvertDirection = "Invalid transfer direction.";
61 static const char* kErrorConvertRecipient = "Invalid transfer recipient."; 68 static const char* kErrorConvertRecipient = "Invalid transfer recipient.";
62 static const char* kErrorConvertRequestType = "Invalid request type."; 69 static const char* kErrorConvertRequestType = "Invalid request type.";
70 static const char* kErrorConvertSynchronizationType =
71 "Invalid synchronization type";
72 static const char* kErrorConvertTransferType = "Invalid endpoint type.";
73 static const char* kErrorConvertUsageType = "Invalid usage type.";
63 static const char* kErrorMalformedParameters = "Error parsing parameters."; 74 static const char* kErrorMalformedParameters = "Error parsing parameters.";
64 static const char* kErrorNoDevice = "No such device."; 75 static const char* kErrorNoDevice = "No such device.";
65 static const char* kErrorPermissionDenied = 76 static const char* kErrorPermissionDenied =
66 "Permission to access device was denied"; 77 "Permission to access device was denied";
67 static const char* kErrorInvalidTransferLength = "Transfer length must be a " 78 static const char* kErrorInvalidTransferLength = "Transfer length must be a "
68 "positive number less than 104,857,600."; 79 "positive number less than 104,857,600.";
69 static const char* kErrorInvalidNumberOfPackets = "Number of packets must be a " 80 static const char* kErrorInvalidNumberOfPackets = "Number of packets must be a "
70 "positive number less than 4,194,304."; 81 "positive number less than 4,194,304.";
71 static const char* kErrorInvalidPacketLength = "Packet length must be a " 82 static const char* kErrorInvalidPacketLength = "Packet length must be a "
72 "positive number less than 65,536."; 83 "positive number less than 65,536.";
73 static const char* kErrorResetDevice = 84 static const char* kErrorResetDevice =
74 "Error resetting the device. The device has been closed."; 85 "Error resetting the device. The device has been closed.";
75 86
76 static const size_t kMaxTransferLength = 100 * 1024 * 1024; 87 static const size_t kMaxTransferLength = 100 * 1024 * 1024;
77 static const int kMaxPackets = 4 * 1024 * 1024; 88 static const int kMaxPackets = 4 * 1024 * 1024;
78 static const int kMaxPacketLength = 64 * 1024; 89 static const int kMaxPacketLength = 64 * 1024;
79 90
80 static UsbDevice* device_for_test_ = NULL; 91 static UsbDevice* device_for_test_ = NULL;
81 92
93 static bool ConvertDirectionToApi(const UsbEndpointDirection& input,
94 Direction* output) {
95 switch (input) {
96 case USB_DIRECTION_INBOUND:
97 *output = usb::DIRECTION_IN;
98 return true;
99 case USB_DIRECTION_OUTBOUND:
100 *output = usb::DIRECTION_OUT;
101 return true;
102 default:
103 NOTREACHED();
104 return false;
105 }
106 }
107
108 static bool ConvertSynchronizationTypeToApi(
109 const UsbSynchronizationType& input,
110 extensions::api::usb::SynchronizationType* output) {
111 switch (input) {
112 case USB_SYNCHRONIZATION_NONE:
113 *output = usb::SYNCHRONIZATION_TYPE_NONE;
114 return true;
115 case USB_SYNCHRONIZATION_ASYNCHRONOUS:
116 *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS;
117 return true;
118 case USB_SYNCHRONIZATION_ADAPTIVE:
119 *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE;
120 return true;
121 case USB_SYNCHRONIZATION_SYNCHRONOUS:
122 *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS;
123 return true;
124 default:
125 NOTREACHED();
126 return false;
127 }
128 }
129
130 static bool ConvertTransferTypeToApi(
131 const UsbTransferType& input,
132 extensions::api::usb::TransferType* output) {
133 switch (input) {
134 case USB_TRANSFER_CONTROL:
135 *output = usb::TRANSFER_TYPE_CONTROL;
136 return true;
137 case USB_TRANSFER_INTERRUPT:
138 *output = usb::TRANSFER_TYPE_INTERRUPT;
139 return true;
140 case USB_TRANSFER_ISOCHRONOUS:
141 *output = usb::TRANSFER_TYPE_ISOCHRONOUS;
142 return true;
143 case USB_TRANSFER_BULK:
144 *output = usb::TRANSFER_TYPE_BULK;
145 return true;
146 default:
147 NOTREACHED();
148 return false;
149 }
150 }
151
152 static bool ConvertUsageTypeToApi(const UsbUsageType& input,
153 extensions::api::usb::UsageType* output) {
154 switch (input) {
155 case USB_USAGE_DATA:
156 *output = usb::USAGE_TYPE_DATA;
157 return true;
158 case USB_USAGE_FEEDBACK:
159 *output = usb::USAGE_TYPE_FEEDBACK;
160 return true;
161 case USB_USAGE_EXPLICIT_FEEDBACK:
162 *output = usb::USAGE_TYPE_EXPLICITFEEDBACK;
163 return true;
164 default:
165 NOTREACHED();
166 return false;
167 }
168 }
169
82 static bool ConvertDirection(const Direction& input, 170 static bool ConvertDirection(const Direction& input,
83 UsbDevice::TransferDirection* output) { 171 UsbEndpointDirection* output) {
84 switch (input) { 172 switch (input) {
85 case usb::DIRECTION_IN: 173 case usb::DIRECTION_IN:
86 *output = UsbDevice::INBOUND; 174 *output = USB_DIRECTION_INBOUND;
87 return true; 175 return true;
88 case usb::DIRECTION_OUT: 176 case usb::DIRECTION_OUT:
89 *output = UsbDevice::OUTBOUND; 177 *output = USB_DIRECTION_OUTBOUND;
90 return true; 178 return true;
91 default: 179 default:
180 NOTREACHED();
92 return false; 181 return false;
93 } 182 }
94 NOTREACHED();
95 return false;
96 } 183 }
97 184
98 static bool ConvertRequestType(const RequestType& input, 185 static bool ConvertRequestType(const RequestType& input,
99 UsbDevice::TransferRequestType* output) { 186 UsbDevice::TransferRequestType* output) {
100 switch (input) { 187 switch (input) {
101 case usb::REQUEST_TYPE_STANDARD: 188 case usb::REQUEST_TYPE_STANDARD:
102 *output = UsbDevice::STANDARD; 189 *output = UsbDevice::STANDARD;
103 return true; 190 return true;
104 case usb::REQUEST_TYPE_CLASS: 191 case usb::REQUEST_TYPE_CLASS:
105 *output = UsbDevice::CLASS; 192 *output = UsbDevice::CLASS;
106 return true; 193 return true;
107 case usb::REQUEST_TYPE_VENDOR: 194 case usb::REQUEST_TYPE_VENDOR:
108 *output = UsbDevice::VENDOR; 195 *output = UsbDevice::VENDOR;
109 return true; 196 return true;
110 case usb::REQUEST_TYPE_RESERVED: 197 case usb::REQUEST_TYPE_RESERVED:
111 *output = UsbDevice::RESERVED; 198 *output = UsbDevice::RESERVED;
112 return true; 199 return true;
113 default: 200 default:
201 NOTREACHED();
114 return false; 202 return false;
115 } 203 }
116 NOTREACHED();
117 return false;
118 } 204 }
119 205
120 static bool ConvertRecipient(const Recipient& input, 206 static bool ConvertRecipient(const Recipient& input,
121 UsbDevice::TransferRecipient* output) { 207 UsbDevice::TransferRecipient* output) {
122 switch (input) { 208 switch (input) {
123 case usb::RECIPIENT_DEVICE: 209 case usb::RECIPIENT_DEVICE:
124 *output = UsbDevice::DEVICE; 210 *output = UsbDevice::DEVICE;
125 return true; 211 return true;
126 case usb::RECIPIENT_INTERFACE: 212 case usb::RECIPIENT_INTERFACE:
127 *output = UsbDevice::INTERFACE; 213 *output = UsbDevice::INTERFACE;
128 return true; 214 return true;
129 case usb::RECIPIENT_ENDPOINT: 215 case usb::RECIPIENT_ENDPOINT:
130 *output = UsbDevice::ENDPOINT; 216 *output = UsbDevice::ENDPOINT;
131 return true; 217 return true;
132 case usb::RECIPIENT_OTHER: 218 case usb::RECIPIENT_OTHER:
133 *output = UsbDevice::OTHER; 219 *output = UsbDevice::OTHER;
134 return true; 220 return true;
135 default: 221 default:
222 NOTREACHED();
136 return false; 223 return false;
137 } 224 }
138 NOTREACHED();
139 return false;
140 } 225 }
141 226
142 template<class T> 227 template<class T>
143 static bool GetTransferSize(const T& input, size_t* output) { 228 static bool GetTransferSize(const T& input, size_t* output) {
144 if (input.direction == usb::DIRECTION_IN) { 229 if (input.direction == usb::DIRECTION_IN) {
145 const int* length = input.length.get(); 230 const int* length = input.length.get();
146 if (length && *length >= 0 && 231 if (length && *length >= 0 &&
147 static_cast<size_t>(*length) < kMaxTransferLength) { 232 static_cast<size_t>(*length) < kMaxTransferLength) {
148 *output = *length; 233 *output = *length;
149 return true; 234 return true;
150 } 235 }
151 } else if (input.direction == usb::DIRECTION_OUT) { 236 } else if (input.direction == usb::DIRECTION_OUT) {
152 if (input.data.get()) { 237 if (input.data.get()) {
153 *output = input.data->size(); 238 *output = input.data->size();
154 return true; 239 return true;
155 } 240 }
156 } 241 }
157 return false; 242 return false;
158 } 243 }
159 244
160 template<class T> 245 template<class T>
161 static scoped_refptr<net::IOBuffer> CreateBufferForTransfer( 246 static scoped_refptr<net::IOBuffer> CreateBufferForTransfer(
162 const T& input, UsbDevice::TransferDirection direction, size_t size) { 247 const T& input, UsbEndpointDirection direction, size_t size) {
163 248
164 if (size >= kMaxTransferLength) 249 if (size >= kMaxTransferLength)
165 return NULL; 250 return NULL;
166 251
167 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This 252 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This
168 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer 253 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer
169 // cannot represent a zero-length buffer, while an URB can. 254 // cannot represent a zero-length buffer, while an URB can.
170 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max( 255 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max(
171 static_cast<size_t>(1), size)); 256 static_cast<size_t>(1), size));
172 257
173 if (direction == UsbDevice::INBOUND) { 258 if (direction == USB_DIRECTION_INBOUND) {
174 return buffer; 259 return buffer;
175 } else if (direction == UsbDevice::OUTBOUND) { 260 } else if (direction == USB_DIRECTION_OUTBOUND) {
176 if (input.data.get() && size <= input.data->size()) { 261 if (input.data.get() && size <= input.data->size()) {
177 memcpy(buffer->data(), input.data->data(), size); 262 memcpy(buffer->data(), input.data->data(), size);
178 return buffer; 263 return buffer;
179 } 264 }
180 } 265 }
181 NOTREACHED(); 266 NOTREACHED();
182 return NULL; 267 return NULL;
183 } 268 }
184 269
185 static const char* ConvertTransferStatusToErrorString( 270 static const char* ConvertTransferStatusToErrorString(
186 const UsbTransferStatus status) { 271 const UsbTransferStatus status) {
187 switch (status) { 272 switch (status) {
188 case USB_TRANSFER_COMPLETED: 273 case USB_TRANSFER_COMPLETED:
189 return ""; 274 return "";
190 case USB_TRANSFER_ERROR: 275 case USB_TRANSFER_ERROR:
191 return kErrorGeneric; 276 return kErrorGeneric;
192 case USB_TRANSFER_TIMEOUT: 277 case USB_TRANSFER_TIMEOUT:
193 return kErrorTimeout; 278 return kErrorTimeout;
194 case USB_TRANSFER_CANCELLED: 279 case USB_TRANSFER_CANCELLED:
195 return kErrorCancelled; 280 return kErrorCancelled;
196 case USB_TRANSFER_STALLED: 281 case USB_TRANSFER_STALLED:
197 return kErrorStalled; 282 return kErrorStalled;
198 case USB_TRANSFER_DISCONNECT: 283 case USB_TRANSFER_DISCONNECT:
199 return kErrorDisconnect; 284 return kErrorDisconnect;
200 case USB_TRANSFER_OVERFLOW: 285 case USB_TRANSFER_OVERFLOW:
201 return kErrorOverflow; 286 return kErrorOverflow;
202 case USB_TRANSFER_LENGTH_SHORT: 287 case USB_TRANSFER_LENGTH_SHORT:
203 return kErrorTransferLength; 288 return kErrorTransferLength;
289 default:
290 NOTREACHED();
291 return "";
204 } 292 }
205
206 NOTREACHED();
207 return "";
208 } 293 }
209 294
210 static base::DictionaryValue* CreateTransferInfo( 295 static base::DictionaryValue* CreateTransferInfo(
211 UsbTransferStatus status, 296 UsbTransferStatus status,
212 scoped_refptr<net::IOBuffer> data, 297 scoped_refptr<net::IOBuffer> data,
213 size_t length) { 298 size_t length) {
214 base::DictionaryValue* result = new base::DictionaryValue(); 299 base::DictionaryValue* result = new base::DictionaryValue();
215 result->SetInteger(kResultCodeKey, status); 300 result->SetInteger(kResultCodeKey, status);
216 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer(data->data(), 301 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer(data->data(),
217 length)); 302 length));
218 return result; 303 return result;
219 } 304 }
220 305
221 static base::Value* PopulateDevice(int handle, int vendor_id, int product_id) { 306 static base::Value* PopulateDevice(int handle, int vendor_id, int product_id) {
222 Device device; 307 Device device;
223 device.handle = handle; 308 device.handle = handle;
224 device.vendor_id = vendor_id; 309 device.vendor_id = vendor_id;
225 device.product_id = product_id; 310 device.product_id = product_id;
226 return device.ToValue().release(); 311 return device.ToValue().release();
227 } 312 }
228 313
314 static base::Value* PopulateInterfaceDescriptor(int interface_number,
315 int alternate_setting, int interface_class, int interface_subclass,
316 int interface_protocol,
317 std::vector<linked_ptr<EndpointDescriptor> >* endpoints) {
318 InterfaceDescriptor descriptor;
319 descriptor.interface_number = interface_number;
320 descriptor.alternate_setting = alternate_setting;
321 descriptor.interface_class = interface_class;
322 descriptor.interface_subclass = interface_subclass;
323 descriptor.interface_protocol = interface_subclass;
324 descriptor.endpoints = *endpoints;
325 return descriptor.ToValue().release();
326 }
327
229 } // namespace 328 } // namespace
230 329
231 namespace extensions { 330 namespace extensions {
232 331
233 UsbAsyncApiFunction::UsbAsyncApiFunction() 332 UsbAsyncApiFunction::UsbAsyncApiFunction()
234 : manager_(NULL) { 333 : manager_(NULL) {
235 } 334 }
236 335
237 UsbAsyncApiFunction::~UsbAsyncApiFunction() { 336 UsbAsyncApiFunction::~UsbAsyncApiFunction() {
238 } 337 }
(...skipping 29 matching lines...) Expand all
268 scoped_refptr<net::IOBuffer> data, 367 scoped_refptr<net::IOBuffer> data,
269 size_t length) { 368 size_t length) {
270 if (status != USB_TRANSFER_COMPLETED) 369 if (status != USB_TRANSFER_COMPLETED)
271 SetError(ConvertTransferStatusToErrorString(status)); 370 SetError(ConvertTransferStatusToErrorString(status));
272 371
273 SetResult(CreateTransferInfo(status, data, length)); 372 SetResult(CreateTransferInfo(status, data, length));
274 AsyncWorkCompleted(); 373 AsyncWorkCompleted();
275 } 374 }
276 375
277 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely( 376 bool UsbAsyncApiTransferFunction::ConvertDirectionSafely(
278 const Direction& input, UsbDevice::TransferDirection* output) { 377 const Direction& input, UsbEndpointDirection* output) {
279 const bool converted = ConvertDirection(input, output); 378 const bool converted = ConvertDirection(input, output);
280 if (!converted) 379 if (!converted)
281 SetError(kErrorConvertDirection); 380 SetError(kErrorConvertDirection);
282 return converted; 381 return converted;
283 } 382 }
284 383
285 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely( 384 bool UsbAsyncApiTransferFunction::ConvertRequestTypeSafely(
286 const RequestType& input, UsbDevice::TransferRequestType* output) { 385 const RequestType& input, UsbDevice::TransferRequestType* output) {
287 const bool converted = ConvertRequestType(input, output); 386 const bool converted = ConvertRequestType(input, output);
288 if (!converted) 387 if (!converted)
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 Device js_device; 457 Device js_device;
359 result_->Append(PopulateDevice(manager_->Add(resource), 458 result_->Append(PopulateDevice(manager_->Add(resource),
360 parameters_->options.vendor_id, 459 parameters_->options.vendor_id,
361 parameters_->options.product_id)); 460 parameters_->options.product_id));
362 } 461 }
363 462
364 SetResult(result_.release()); 463 SetResult(result_.release());
365 AsyncWorkCompleted(); 464 AsyncWorkCompleted();
366 } 465 }
367 466
467 UsbListInterfacesFunction::UsbListInterfacesFunction() {}
468
469 UsbListInterfacesFunction::~UsbListInterfacesFunction() {}
470
471 bool UsbListInterfacesFunction::Prepare() {
472 parameters_ = ListInterfaces::Params::Create(*args_);
473 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
474 return true;
475 }
476
477 void UsbListInterfacesFunction::AsyncWorkStart() {
478 UsbDeviceResource* const resource = GetUsbDeviceResource(
479 parameters_->device.handle);
480 if (!resource) {
481 CompleteWithError(kErrorNoDevice);
482 return;
483 }
484
485 config_ = new UsbConfigDescriptor();
486 resource->device()->ListInterfaces(config_, base::Bind(
487 &UsbListInterfacesFunction::OnCompleted, this));
488 }
489
490 void UsbListInterfacesFunction::OnCompleted(bool success) {
491 if (!success) {
492 SetError(kErrorCannotListInterfaces);
493 AsyncWorkCompleted();
494 return;
495 }
496
497 result_.reset(new base::ListValue());
498
499 for (size_t i = 0, numInterfaces = config_->GetNumInterfaces();
500 i < numInterfaces; ++i) {
501 scoped_refptr<const UsbInterface> usbInterface(config_->GetInterface(i));
502 for (size_t j = 0, numDescriptors = usbInterface->GetNumAltSettings();
503 j < numDescriptors; ++j) {
504 scoped_refptr<const UsbInterfaceDescriptor> descriptor
505 = usbInterface->GetAltSetting(j);
506 std::vector<linked_ptr<EndpointDescriptor> > endpoints;
507 for (size_t k = 0, numEndpoints = descriptor->GetNumEndpoints();
508 k < numEndpoints; k++) {
509 scoped_refptr<const UsbEndpointDescriptor> endpoint
510 = descriptor->GetEndpoint(k);
511 linked_ptr<EndpointDescriptor> endpoint_desc(new EndpointDescriptor());
512
513 TransferType type;
514 Direction direction;
515 SynchronizationType synchronization;
516 UsageType usage;
517
518 if (!ConvertTransferTypeSafely(endpoint->GetTransferType(), &type) ||
519 !ConvertDirectionSafely(endpoint->GetDirection(), &direction) ||
520 !ConvertSynchronizationTypeSafely(
521 endpoint->GetSynchronizationType(), &synchronization) ||
522 !ConvertUsageTypeSafely(endpoint->GetUsageType(), &usage)) {
523 SetError(kErrorCannotListInterfaces);
524 AsyncWorkCompleted();
525 return;
526 }
527
528 endpoint_desc->address = endpoint->GetAddress();
529 endpoint_desc->type = type;
530 endpoint_desc->direction = direction;
531 endpoint_desc->maximum_packet_size = endpoint->GetMaximumPacketSize();
532 endpoint_desc->synchronization = synchronization;
533 endpoint_desc->usage = usage;
534
535 int* polling_interval = new int;
536 endpoint_desc->polling_interval.reset(polling_interval);
537 *polling_interval = endpoint->GetPollingInterval();
538
539 endpoints.push_back(endpoint_desc);
540 }
541
542 result_->Append(PopulateInterfaceDescriptor(
543 descriptor->GetInterfaceNumber(),
544 descriptor->GetAlternateSetting(),
545 descriptor->GetInterfaceClass(),
546 descriptor->GetInterfaceSubclass(),
547 descriptor->GetInterfaceProtocol(),
548 &endpoints));
549 }
550 }
551
552 SetResult(result_.release());
553 AsyncWorkCompleted();
554 }
555
556 bool UsbListInterfacesFunction::ConvertDirectionSafely(
557 const UsbEndpointDirection& input,
558 extensions::api::usb::Direction* output) {
559 const bool converted = ConvertDirectionToApi(input, output);
560 if (!converted)
561 SetError(kErrorConvertDirection);
562 return converted;
563 }
564
565 bool UsbListInterfacesFunction::ConvertSynchronizationTypeSafely(
566 const UsbSynchronizationType& input,
567 extensions::api::usb::SynchronizationType* output) {
568 const bool converted = ConvertSynchronizationTypeToApi(input, output);
569 if (!converted)
570 SetError(kErrorConvertSynchronizationType);
571 return converted;
572 }
573
574 bool UsbListInterfacesFunction::ConvertTransferTypeSafely(
575 const UsbTransferType& input,
576 extensions::api::usb::TransferType* output) {
577 const bool converted = ConvertTransferTypeToApi(input, output);
578 if (!converted)
579 SetError(kErrorConvertTransferType);
580 return converted;
581 }
582
583 bool UsbListInterfacesFunction::ConvertUsageTypeSafely(
584 const UsbUsageType& input,
585 extensions::api::usb::UsageType* output) {
586 const bool converted = ConvertUsageTypeToApi(input, output);
587 if (!converted)
588 SetError(kErrorConvertUsageType);
589 return converted;
590 }
591
368 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {} 592 UsbCloseDeviceFunction::UsbCloseDeviceFunction() {}
369 593
370 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {} 594 UsbCloseDeviceFunction::~UsbCloseDeviceFunction() {}
371 595
372 bool UsbCloseDeviceFunction::Prepare() { 596 bool UsbCloseDeviceFunction::Prepare() {
373 parameters_ = CloseDevice::Params::Create(*args_); 597 parameters_ = CloseDevice::Params::Create(*args_);
374 EXTENSION_FUNCTION_VALIDATE(parameters_.get()); 598 EXTENSION_FUNCTION_VALIDATE(parameters_.get());
375 return true; 599 return true;
376 } 600 }
377 601
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 void UsbControlTransferFunction::AsyncWorkStart() { 717 void UsbControlTransferFunction::AsyncWorkStart() {
494 UsbDeviceResource* const resource = GetUsbDeviceResource( 718 UsbDeviceResource* const resource = GetUsbDeviceResource(
495 parameters_->device.handle); 719 parameters_->device.handle);
496 if (!resource) { 720 if (!resource) {
497 CompleteWithError(kErrorNoDevice); 721 CompleteWithError(kErrorNoDevice);
498 return; 722 return;
499 } 723 }
500 724
501 const ControlTransferInfo& transfer = parameters_->transfer_info; 725 const ControlTransferInfo& transfer = parameters_->transfer_info;
502 726
503 UsbDevice::TransferDirection direction; 727 UsbEndpointDirection direction;
504 UsbDevice::TransferRequestType request_type; 728 UsbDevice::TransferRequestType request_type;
505 UsbDevice::TransferRecipient recipient; 729 UsbDevice::TransferRecipient recipient;
506 size_t size = 0; 730 size_t size = 0;
507 731
508 if (!ConvertDirectionSafely(transfer.direction, &direction) || 732 if (!ConvertDirectionSafely(transfer.direction, &direction) ||
509 !ConvertRequestTypeSafely(transfer.request_type, &request_type) || 733 !ConvertRequestTypeSafely(transfer.request_type, &request_type) ||
510 !ConvertRecipientSafely(transfer.recipient, &recipient)) { 734 !ConvertRecipientSafely(transfer.recipient, &recipient)) {
511 AsyncWorkCompleted(); 735 AsyncWorkCompleted();
512 return; 736 return;
513 } 737 }
(...skipping 28 matching lines...) Expand all
542 void UsbBulkTransferFunction::AsyncWorkStart() { 766 void UsbBulkTransferFunction::AsyncWorkStart() {
543 UsbDeviceResource* const resource = GetUsbDeviceResource( 767 UsbDeviceResource* const resource = GetUsbDeviceResource(
544 parameters_->device.handle); 768 parameters_->device.handle);
545 if (!resource) { 769 if (!resource) {
546 CompleteWithError(kErrorNoDevice); 770 CompleteWithError(kErrorNoDevice);
547 return; 771 return;
548 } 772 }
549 773
550 const GenericTransferInfo& transfer = parameters_->transfer_info; 774 const GenericTransferInfo& transfer = parameters_->transfer_info;
551 775
552 UsbDevice::TransferDirection direction; 776 UsbEndpointDirection direction;
553 size_t size = 0; 777 size_t size = 0;
554 778
555 if (!ConvertDirectionSafely(transfer.direction, &direction)) { 779 if (!ConvertDirectionSafely(transfer.direction, &direction)) {
556 AsyncWorkCompleted(); 780 AsyncWorkCompleted();
557 return; 781 return;
558 } 782 }
559 783
560 if (!GetTransferSize(transfer, &size)) { 784 if (!GetTransferSize(transfer, &size)) {
561 CompleteWithError(kErrorInvalidTransferLength); 785 CompleteWithError(kErrorInvalidTransferLength);
562 return; 786 return;
(...skipping 23 matching lines...) Expand all
586 void UsbInterruptTransferFunction::AsyncWorkStart() { 810 void UsbInterruptTransferFunction::AsyncWorkStart() {
587 UsbDeviceResource* const resource = GetUsbDeviceResource( 811 UsbDeviceResource* const resource = GetUsbDeviceResource(
588 parameters_->device.handle); 812 parameters_->device.handle);
589 if (!resource) { 813 if (!resource) {
590 CompleteWithError(kErrorNoDevice); 814 CompleteWithError(kErrorNoDevice);
591 return; 815 return;
592 } 816 }
593 817
594 const GenericTransferInfo& transfer = parameters_->transfer_info; 818 const GenericTransferInfo& transfer = parameters_->transfer_info;
595 819
596 UsbDevice::TransferDirection direction; 820 UsbEndpointDirection direction;
597 size_t size = 0; 821 size_t size = 0;
598 822
599 if (!ConvertDirectionSafely(transfer.direction, &direction)) { 823 if (!ConvertDirectionSafely(transfer.direction, &direction)) {
600 AsyncWorkCompleted(); 824 AsyncWorkCompleted();
601 return; 825 return;
602 } 826 }
603 827
604 if (!GetTransferSize(transfer, &size)) { 828 if (!GetTransferSize(transfer, &size)) {
605 CompleteWithError(kErrorInvalidTransferLength); 829 CompleteWithError(kErrorInvalidTransferLength);
606 return; 830 return;
(...skipping 25 matching lines...) Expand all
632 parameters_->device.handle); 856 parameters_->device.handle);
633 if (!resource) { 857 if (!resource) {
634 CompleteWithError(kErrorNoDevice); 858 CompleteWithError(kErrorNoDevice);
635 return; 859 return;
636 } 860 }
637 861
638 const IsochronousTransferInfo& transfer = parameters_->transfer_info; 862 const IsochronousTransferInfo& transfer = parameters_->transfer_info;
639 const GenericTransferInfo& generic_transfer = transfer.transfer_info; 863 const GenericTransferInfo& generic_transfer = transfer.transfer_info;
640 864
641 size_t size = 0; 865 size_t size = 0;
642 UsbDevice::TransferDirection direction; 866 UsbEndpointDirection direction;
643 867
644 if (!ConvertDirectionSafely(generic_transfer.direction, &direction)) { 868 if (!ConvertDirectionSafely(generic_transfer.direction, &direction)) {
645 AsyncWorkCompleted(); 869 AsyncWorkCompleted();
646 return; 870 return;
647 } 871 }
648 if (!GetTransferSize(generic_transfer, &size)) { 872 if (!GetTransferSize(generic_transfer, &size)) {
649 CompleteWithError(kErrorInvalidTransferLength); 873 CompleteWithError(kErrorInvalidTransferLength);
650 return; 874 return;
651 } 875 }
652 if (transfer.packets < 0 || transfer.packets >= kMaxPackets) { 876 if (transfer.packets < 0 || transfer.packets >= kMaxPackets) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 } 959 }
736 960
737 void UsbResetDeviceFunction::OnError() { 961 void UsbResetDeviceFunction::OnError() {
738 RemoveUsbDeviceResource(parameters_->device.handle); 962 RemoveUsbDeviceResource(parameters_->device.handle);
739 SetError(kErrorResetDevice); 963 SetError(kErrorResetDevice);
740 SetResult(Value::CreateBooleanValue(false)); 964 SetResult(Value::CreateBooleanValue(false));
741 AsyncWorkCompleted(); 965 AsyncWorkCompleted();
742 } 966 }
743 967
744 } // namespace extensions 968 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/usb/usb_api.h ('k') | chrome/browser/extensions/api/usb/usb_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698