| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "device/usb/webusb_descriptors.h" | 5 #include "device/usb/webusb_descriptors.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 // These constants are defined by the WebUSB specification: | 38 // These constants are defined by the WebUSB specification: |
| 39 // http://wicg.github.io/webusb/ | 39 // http://wicg.github.io/webusb/ |
| 40 const uint8_t kGetAllowedOriginsRequest = 0x01; | 40 const uint8_t kGetAllowedOriginsRequest = 0x01; |
| 41 const uint8_t kGetUrlRequest = 0x02; | 41 const uint8_t kGetUrlRequest = 0x02; |
| 42 | 42 |
| 43 const uint8_t kWebUsbCapabilityUUID[16] = { | 43 const uint8_t kWebUsbCapabilityUUID[16] = { |
| 44 // Little-endian encoding of {3408b638-09a9-47a0-8bfd-a0768815b665}. | 44 // Little-endian encoding of {3408b638-09a9-47a0-8bfd-a0768815b665}. |
| 45 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, | 45 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, |
| 46 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65}; | 46 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65}; |
| 47 | 47 |
| 48 const int kControlTransferTimeout = 60000; // 1 minute | 48 const int kControlTransferTimeoutMs = 2000; // 2 seconds |
| 49 | 49 |
| 50 using ReadWebUsbDescriptorsCallback = | 50 using ReadWebUsbDescriptorsCallback = |
| 51 base::Callback<void(std::unique_ptr<WebUsbAllowedOrigins> allowed_origins, | 51 base::Callback<void(std::unique_ptr<WebUsbAllowedOrigins> allowed_origins, |
| 52 const GURL& landing_page)>; | 52 const GURL& landing_page)>; |
| 53 | 53 |
| 54 using ReadWebUsbAllowedOriginsCallback = | 54 using ReadWebUsbAllowedOriginsCallback = |
| 55 base::Callback<void(std::unique_ptr<WebUsbAllowedOrigins> allowed_origins)>; | 55 base::Callback<void(std::unique_ptr<WebUsbAllowedOrigins> allowed_origins)>; |
| 56 | 56 |
| 57 // Parses a WebUSB Function Subset Header: | 57 // Parses a WebUSB Function Subset Header: |
| 58 // http://wicg.github.io/webusb/#dfn-function-subset-header | 58 // http://wicg.github.io/webusb/#dfn-function-subset-header |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 // |url_map| and then runs |callback|. | 211 // |url_map| and then runs |callback|. |
| 212 void ReadUrlDescriptor(scoped_refptr<UsbDeviceHandle> device_handle, | 212 void ReadUrlDescriptor(scoped_refptr<UsbDeviceHandle> device_handle, |
| 213 uint8_t vendor_code, | 213 uint8_t vendor_code, |
| 214 std::map<uint8_t, GURL>* url_map, | 214 std::map<uint8_t, GURL>* url_map, |
| 215 uint8_t index, | 215 uint8_t index, |
| 216 const base::Closure& callback) { | 216 const base::Closure& callback) { |
| 217 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(255); | 217 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(255); |
| 218 device_handle->ControlTransfer( | 218 device_handle->ControlTransfer( |
| 219 UsbTransferDirection::INBOUND, UsbControlTransferType::VENDOR, | 219 UsbTransferDirection::INBOUND, UsbControlTransferType::VENDOR, |
| 220 UsbControlTransferRecipient::DEVICE, vendor_code, index, kGetUrlRequest, | 220 UsbControlTransferRecipient::DEVICE, vendor_code, index, kGetUrlRequest, |
| 221 buffer, buffer->size(), kControlTransferTimeout, | 221 buffer, buffer->size(), kControlTransferTimeoutMs, |
| 222 base::Bind(&OnReadUrlDescriptor, url_map, index, callback)); | 222 base::Bind(&OnReadUrlDescriptor, url_map, index, callback)); |
| 223 } | 223 } |
| 224 | 224 |
| 225 // Reads URL descriptors from the device so that it can fill |allowed_origins| | 225 // Reads URL descriptors from the device so that it can fill |allowed_origins| |
| 226 // with the GURLs matching the indicies already collected. | 226 // with the GURLs matching the indicies already collected. |
| 227 void ReadUrlDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, | 227 void ReadUrlDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, |
| 228 uint8_t vendor_code, | 228 uint8_t vendor_code, |
| 229 uint8_t landing_page_id, | 229 uint8_t landing_page_id, |
| 230 const ReadWebUsbDescriptorsCallback& callback, | 230 const ReadWebUsbDescriptorsCallback& callback, |
| 231 std::unique_ptr<WebUsbAllowedOrigins> allowed_origins) { | 231 std::unique_ptr<WebUsbAllowedOrigins> allowed_origins) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 return; | 294 return; |
| 295 } | 295 } |
| 296 | 296 |
| 297 const uint8_t* data = reinterpret_cast<uint8_t*>(buffer->data()); | 297 const uint8_t* data = reinterpret_cast<uint8_t*>(buffer->data()); |
| 298 uint16_t new_length = data[2] | (data[3] << 8); | 298 uint16_t new_length = data[2] | (data[3] << 8); |
| 299 scoped_refptr<IOBufferWithSize> new_buffer = new IOBufferWithSize(new_length); | 299 scoped_refptr<IOBufferWithSize> new_buffer = new IOBufferWithSize(new_length); |
| 300 device_handle->ControlTransfer( | 300 device_handle->ControlTransfer( |
| 301 UsbTransferDirection::INBOUND, UsbControlTransferType::VENDOR, | 301 UsbTransferDirection::INBOUND, UsbControlTransferType::VENDOR, |
| 302 UsbControlTransferRecipient::DEVICE, vendor_code, 0, | 302 UsbControlTransferRecipient::DEVICE, vendor_code, 0, |
| 303 kGetAllowedOriginsRequest, new_buffer, new_buffer->size(), | 303 kGetAllowedOriginsRequest, new_buffer, new_buffer->size(), |
| 304 kControlTransferTimeout, | 304 kControlTransferTimeoutMs, |
| 305 base::Bind(&OnReadWebUsbAllowedOrigins, callback)); | 305 base::Bind(&OnReadWebUsbAllowedOrigins, callback)); |
| 306 } | 306 } |
| 307 | 307 |
| 308 void ReadWebUsbAllowedOrigins( | 308 void ReadWebUsbAllowedOrigins( |
| 309 scoped_refptr<UsbDeviceHandle> device_handle, | 309 scoped_refptr<UsbDeviceHandle> device_handle, |
| 310 uint8_t vendor_code, | 310 uint8_t vendor_code, |
| 311 const ReadWebUsbAllowedOriginsCallback& callback) { | 311 const ReadWebUsbAllowedOriginsCallback& callback) { |
| 312 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(4); | 312 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(4); |
| 313 device_handle->ControlTransfer( | 313 device_handle->ControlTransfer( |
| 314 UsbTransferDirection::INBOUND, UsbControlTransferType::VENDOR, | 314 UsbTransferDirection::INBOUND, UsbControlTransferType::VENDOR, |
| 315 UsbControlTransferRecipient::DEVICE, vendor_code, 0, | 315 UsbControlTransferRecipient::DEVICE, vendor_code, 0, |
| 316 kGetAllowedOriginsRequest, buffer, buffer->size(), | 316 kGetAllowedOriginsRequest, buffer, buffer->size(), |
| 317 kControlTransferTimeout, | 317 kControlTransferTimeoutMs, |
| 318 base::Bind(&OnReadWebUsbAllowedOriginsHeader, device_handle, callback, | 318 base::Bind(&OnReadWebUsbAllowedOriginsHeader, device_handle, callback, |
| 319 vendor_code)); | 319 vendor_code)); |
| 320 } | 320 } |
| 321 | 321 |
| 322 void OnReadBosDescriptor(scoped_refptr<UsbDeviceHandle> device_handle, | 322 void OnReadBosDescriptor(scoped_refptr<UsbDeviceHandle> device_handle, |
| 323 const ReadWebUsbDescriptorsCallback& callback, | 323 const ReadWebUsbDescriptorsCallback& callback, |
| 324 UsbTransferStatus status, | 324 UsbTransferStatus status, |
| 325 scoped_refptr<net::IOBuffer> buffer, | 325 scoped_refptr<net::IOBuffer> buffer, |
| 326 size_t length) { | 326 size_t length) { |
| 327 if (status != UsbTransferStatus::COMPLETED) { | 327 if (status != UsbTransferStatus::COMPLETED) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 354 return; | 354 return; |
| 355 } | 355 } |
| 356 | 356 |
| 357 const uint8_t* data = reinterpret_cast<uint8_t*>(buffer->data()); | 357 const uint8_t* data = reinterpret_cast<uint8_t*>(buffer->data()); |
| 358 uint16_t new_length = data[2] | (data[3] << 8); | 358 uint16_t new_length = data[2] | (data[3] << 8); |
| 359 scoped_refptr<IOBufferWithSize> new_buffer = new IOBufferWithSize(new_length); | 359 scoped_refptr<IOBufferWithSize> new_buffer = new IOBufferWithSize(new_length); |
| 360 device_handle->ControlTransfer( | 360 device_handle->ControlTransfer( |
| 361 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, | 361 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, |
| 362 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, | 362 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, |
| 363 kBosDescriptorType << 8, 0, new_buffer, new_buffer->size(), | 363 kBosDescriptorType << 8, 0, new_buffer, new_buffer->size(), |
| 364 kControlTransferTimeout, | 364 kControlTransferTimeoutMs, |
| 365 base::Bind(&OnReadBosDescriptor, device_handle, callback)); | 365 base::Bind(&OnReadBosDescriptor, device_handle, callback)); |
| 366 } | 366 } |
| 367 | 367 |
| 368 } // namespace | 368 } // namespace |
| 369 | 369 |
| 370 WebUsbFunctionSubset::WebUsbFunctionSubset() : first_interface(0) {} | 370 WebUsbFunctionSubset::WebUsbFunctionSubset() : first_interface(0) {} |
| 371 | 371 |
| 372 WebUsbFunctionSubset::WebUsbFunctionSubset(const WebUsbFunctionSubset& other) = | 372 WebUsbFunctionSubset::WebUsbFunctionSubset(const WebUsbFunctionSubset& other) = |
| 373 default; | 373 default; |
| 374 | 374 |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 return true; | 567 return true; |
| 568 } | 568 } |
| 569 | 569 |
| 570 void ReadWebUsbDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, | 570 void ReadWebUsbDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, |
| 571 const ReadWebUsbDescriptorsCallback& callback) { | 571 const ReadWebUsbDescriptorsCallback& callback) { |
| 572 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(5); | 572 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(5); |
| 573 device_handle->ControlTransfer( | 573 device_handle->ControlTransfer( |
| 574 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, | 574 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, |
| 575 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, | 575 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, |
| 576 kBosDescriptorType << 8, 0, buffer, buffer->size(), | 576 kBosDescriptorType << 8, 0, buffer, buffer->size(), |
| 577 kControlTransferTimeout, | 577 kControlTransferTimeoutMs, |
| 578 base::Bind(&OnReadBosDescriptorHeader, device_handle, callback)); | 578 base::Bind(&OnReadBosDescriptorHeader, device_handle, callback)); |
| 579 } | 579 } |
| 580 | 580 |
| 581 bool FindInWebUsbAllowedOrigins( | 581 bool FindInWebUsbAllowedOrigins( |
| 582 const device::WebUsbAllowedOrigins* allowed_origins, | 582 const device::WebUsbAllowedOrigins* allowed_origins, |
| 583 const GURL& origin, | 583 const GURL& origin, |
| 584 base::Optional<uint8_t> config_value, | 584 base::Optional<uint8_t> config_value, |
| 585 base::Optional<uint8_t> first_interface) { | 585 base::Optional<uint8_t> first_interface) { |
| 586 if (!allowed_origins) | 586 if (!allowed_origins) |
| 587 return false; | 587 return false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 602 | 602 |
| 603 if (base::ContainsValue(function.origins, origin)) | 603 if (base::ContainsValue(function.origins, origin)) |
| 604 return true; | 604 return true; |
| 605 } | 605 } |
| 606 } | 606 } |
| 607 | 607 |
| 608 return false; | 608 return false; |
| 609 } | 609 } |
| 610 | 610 |
| 611 } // namespace device | 611 } // namespace device |
| OLD | NEW |