| 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 "config.h" | 5 #include "config.h" |
| 6 #include "modules/webusb/USB.h" | 6 #include "modules/webusb/USB.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/Document.h" | 12 #include "core/dom/Document.h" |
| 13 #include "core/dom/ExceptionCode.h" | 13 #include "core/dom/ExceptionCode.h" |
| 14 #include "modules/webusb/USBDevice.h" | 14 #include "modules/webusb/USBDevice.h" |
| 15 #include "modules/webusb/USBDeviceEnumerationOptions.h" | |
| 16 #include "modules/webusb/USBDeviceFilter.h" | 15 #include "modules/webusb/USBDeviceFilter.h" |
| 16 #include "modules/webusb/USBDeviceRequestOptions.h" |
| 17 #include "modules/webusb/USBError.h" | 17 #include "modules/webusb/USBError.h" |
| 18 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" |
| 19 #include "public/platform/WebVector.h" | 19 #include "public/platform/WebVector.h" |
| 20 #include "public/platform/modules/webusb/WebUSBClient.h" | 20 #include "public/platform/modules/webusb/WebUSBClient.h" |
| 21 #include "public/platform/modules/webusb/WebUSBDeviceEnumerationOptions.h" | |
| 22 #include "public/platform/modules/webusb/WebUSBDeviceFilter.h" | 21 #include "public/platform/modules/webusb/WebUSBDeviceFilter.h" |
| 22 #include "public/platform/modules/webusb/WebUSBDeviceRequestOptions.h" |
| 23 #include "public/platform/modules/webusb/WebUSBError.h" | 23 #include "public/platform/modules/webusb/WebUSBError.h" |
| 24 | 24 |
| 25 namespace blink { | 25 namespace blink { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 void convertDeviceEnumerationOptions(const USBDeviceEnumerationOptions& options,
WebUSBDeviceEnumerationOptions* webOptions) | 28 void convertDeviceFilter(const USBDeviceFilter& filter, WebUSBDeviceFilter* webF
ilter) |
| 29 { |
| 30 webFilter->hasVendorID = filter.hasVendorId(); |
| 31 if (filter.hasVendorId()) |
| 32 webFilter->vendorID = filter.vendorId(); |
| 33 webFilter->hasProductID = filter.hasProductId(); |
| 34 if (filter.hasProductId()) |
| 35 webFilter->productID = filter.productId(); |
| 36 webFilter->hasClassCode = filter.hasClassCode(); |
| 37 if (filter.hasClassCode()) |
| 38 webFilter->classCode = filter.classCode(); |
| 39 webFilter->hasSubclassCode = filter.hasSubclassCode(); |
| 40 if (filter.hasSubclassCode()) |
| 41 webFilter->subclassCode = filter.subclassCode(); |
| 42 webFilter->hasProtocolCode = filter.hasProtocolCode(); |
| 43 if (filter.hasProtocolCode()) |
| 44 webFilter->protocolCode = filter.protocolCode(); |
| 45 } |
| 46 |
| 47 void convertDeviceRequestOptions(const USBDeviceRequestOptions& options, WebUSBD
eviceRequestOptions* webOptions) |
| 29 { | 48 { |
| 30 ASSERT(options.hasFilters()); | 49 ASSERT(options.hasFilters()); |
| 31 webOptions->filters = WebVector<WebUSBDeviceFilter>(options.filters().size()
); | 50 webOptions->filters = WebVector<WebUSBDeviceFilter>(options.filters().size()
); |
| 32 for (size_t i = 0; i < options.filters().size(); ++i) { | 51 for (size_t i = 0; i < options.filters().size(); ++i) { |
| 33 const USBDeviceFilter& filter = options.filters()[i]; | 52 convertDeviceFilter(options.filters()[i], &webOptions->filters[i]); |
| 34 WebUSBDeviceFilter& webFilter = webOptions->filters[i]; | |
| 35 webFilter.hasVendorID = filter.hasVendorId(); | |
| 36 if (filter.hasVendorId()) | |
| 37 webFilter.vendorID = filter.vendorId(); | |
| 38 webFilter.hasProductID = filter.hasProductId(); | |
| 39 if (filter.hasProductId()) | |
| 40 webFilter.productID = filter.productId(); | |
| 41 webFilter.hasClassCode = filter.hasClassCode(); | |
| 42 if (filter.hasClassCode()) | |
| 43 webFilter.classCode = filter.classCode(); | |
| 44 webFilter.hasSubclassCode = filter.hasSubclassCode(); | |
| 45 if (filter.hasSubclassCode()) | |
| 46 webFilter.subclassCode = filter.subclassCode(); | |
| 47 webFilter.hasProtocolCode = filter.hasProtocolCode(); | |
| 48 if (filter.hasProtocolCode()) | |
| 49 webFilter.protocolCode = filter.protocolCode(); | |
| 50 } | 53 } |
| 51 } | 54 } |
| 52 | 55 |
| 53 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the | 56 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the |
| 54 // getDevices() promise with a HeapVector owning USBDevices. | 57 // getDevices() promise with a HeapVector owning USBDevices. |
| 55 class DeviceArray { | 58 class DeviceArray { |
| 56 WTF_MAKE_NONCOPYABLE(DeviceArray); | 59 WTF_MAKE_NONCOPYABLE(DeviceArray); |
| 57 public: | 60 public: |
| 58 using WebType = OwnPtr<WebVector<WebUSBDevice*>>; | 61 using WebType = OwnPtr<WebVector<WebUSBDevice*>>; |
| 59 | 62 |
| 60 static HeapVector<Member<USBDevice>> take(ScriptPromiseResolver*, PassOwnPtr
<WebVector<WebUSBDevice*>> webDevices) | 63 static HeapVector<Member<USBDevice>> take(ScriptPromiseResolver*, PassOwnPtr
<WebVector<WebUSBDevice*>> webDevices) |
| 61 { | 64 { |
| 62 HeapVector<Member<USBDevice>> devices; | 65 HeapVector<Member<USBDevice>> devices; |
| 63 for (const auto webDevice : *webDevices) | 66 for (const auto webDevice : *webDevices) |
| 64 devices.append(USBDevice::create(adoptPtr(webDevice))); | 67 devices.append(USBDevice::create(adoptPtr(webDevice))); |
| 65 return devices; | 68 return devices; |
| 66 } | 69 } |
| 67 | 70 |
| 68 private: | 71 private: |
| 69 DeviceArray() = delete; | 72 DeviceArray() = delete; |
| 70 }; | 73 }; |
| 71 | 74 |
| 72 } // namespace | 75 } // namespace |
| 73 | 76 |
| 74 USB::USB(LocalFrame& frame) | 77 USB::USB(LocalFrame& frame) |
| 75 : m_controller(USBController::from(frame)) | 78 : m_controller(USBController::from(frame)) |
| 76 { | 79 { |
| 77 } | 80 } |
| 78 | 81 |
| 79 ScriptPromise USB::getDevices(ScriptState* scriptState, const USBDeviceEnumerati
onOptions& options) | 82 ScriptPromise USB::getDevices(ScriptState* scriptState) |
| 80 { | 83 { |
| 81 WebUSBClient* client = m_controller->client(); | 84 WebUSBClient* client = m_controller->client(); |
| 82 if (!client) | 85 if (!client) |
| 83 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | 86 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 84 | 87 |
| 85 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 88 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 86 ScriptPromise promise = resolver->promise(); | 89 ScriptPromise promise = resolver->promise(); |
| 90 client->getDevices(new CallbackPromiseAdapter<DeviceArray, USBError>(resolve
r)); |
| 91 |
| 92 return promise; |
| 93 } |
| 94 |
| 95 ScriptPromise USB::requestDevice(ScriptState* scriptState, const USBDeviceReques
tOptions& options) |
| 96 { |
| 97 WebUSBClient* client = m_controller->client(); |
| 98 if (!client) |
| 99 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 100 |
| 101 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 102 ScriptPromise promise = resolver->promise(); |
| 87 | 103 |
| 88 WebUSBDeviceEnumerationOptions webOptions; | 104 WebUSBDeviceRequestOptions webOptions; |
| 89 convertDeviceEnumerationOptions(options, &webOptions); | 105 convertDeviceRequestOptions(options, &webOptions); |
| 90 client->getDevices(webOptions, new CallbackPromiseAdapter<DeviceArray, USBEr
ror>(resolver)); | 106 client->requestDevice(webOptions, new CallbackPromiseAdapter<USBDevice, USBE
rror>(resolver)); |
| 107 |
| 91 return promise; | 108 return promise; |
| 92 } | 109 } |
| 93 | 110 |
| 94 DEFINE_TRACE(USB) | 111 DEFINE_TRACE(USB) |
| 95 { | 112 { |
| 96 visitor->trace(m_controller); | 113 visitor->trace(m_controller); |
| 97 } | 114 } |
| 98 | 115 |
| 99 } // namespace blink | 116 } // namespace blink |
| OLD | NEW |