| 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 "modules/webusb/USB.h" | 5 #include "modules/webusb/USB.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 webFilter->hasSubclassCode = filter.hasSubclassCode(); | 42 webFilter->hasSubclassCode = filter.hasSubclassCode(); |
| 43 if (filter.hasSubclassCode()) | 43 if (filter.hasSubclassCode()) |
| 44 webFilter->subclassCode = filter.subclassCode(); | 44 webFilter->subclassCode = filter.subclassCode(); |
| 45 webFilter->hasProtocolCode = filter.hasProtocolCode(); | 45 webFilter->hasProtocolCode = filter.hasProtocolCode(); |
| 46 if (filter.hasProtocolCode()) | 46 if (filter.hasProtocolCode()) |
| 47 webFilter->protocolCode = filter.protocolCode(); | 47 webFilter->protocolCode = filter.protocolCode(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void convertDeviceRequestOptions(const USBDeviceRequestOptions& options, WebUSBD
eviceRequestOptions* webOptions) | 50 void convertDeviceRequestOptions(const USBDeviceRequestOptions& options, WebUSBD
eviceRequestOptions* webOptions) |
| 51 { | 51 { |
| 52 ASSERT(options.hasFilters()); | 52 DCHECK(options.hasFilters()); |
| 53 webOptions->filters = WebVector<WebUSBDeviceFilter>(options.filters().size()
); | 53 webOptions->filters = WebVector<WebUSBDeviceFilter>(options.filters().size()
); |
| 54 for (size_t i = 0; i < options.filters().size(); ++i) { | 54 for (size_t i = 0; i < options.filters().size(); ++i) { |
| 55 convertDeviceFilter(options.filters()[i], &webOptions->filters[i]); | 55 convertDeviceFilter(options.filters()[i], &webOptions->filters[i]); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the | 59 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the |
| 60 // getDevices() promise with a HeapVector owning USBDevices. | 60 // getDevices() promise with a HeapVector owning USBDevices. |
| 61 class DeviceArray { | 61 class DeviceArray { |
| 62 STATIC_ONLY(DeviceArray); | 62 STATIC_ONLY(DeviceArray); |
| 63 public: | 63 public: |
| 64 using WebType = OwnPtr<WebVector<WebUSBDevice*>>; | 64 using WebType = OwnPtr<WebVector<WebUSBDevice*>>; |
| 65 | 65 |
| 66 static HeapVector<Member<USBDevice>> take(ScriptPromiseResolver* resolver, P
assOwnPtr<WebVector<WebUSBDevice*>> webDevices) | 66 static HeapVector<Member<USBDevice>> take(ScriptPromiseResolver* resolver, P
assOwnPtr<WebVector<WebUSBDevice*>> webDevices) |
| 67 { | 67 { |
| 68 HeapVector<Member<USBDevice>> devices; | 68 HeapVector<Member<USBDevice>> devices; |
| 69 for (const auto webDevice : *webDevices) | 69 for (const auto webDevice : *webDevices) |
| 70 devices.append(USBDevice::create(adoptPtr(webDevice), resolver->getE
xecutionContext())); | 70 devices.append(USBDevice::create(adoptPtr(webDevice), resolver->getE
xecutionContext())); |
| 71 return devices; | 71 return devices; |
| 72 } | 72 } |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 } // namespace | 75 } // namespace |
| 76 | 76 |
| 77 USB::USB(LocalFrame& frame) | 77 USB::USB(LocalFrame& frame) |
| 78 : LocalFrameLifecycleObserver(&frame) | 78 : LocalFrameLifecycleObserver(&frame) |
| 79 , m_client(USBController::from(frame).client()) | 79 , m_client(USBController::from(frame).client()) |
| 80 { | 80 { |
| 81 if (m_client) | 81 if (m_client) |
| 82 m_client->setObserver(this); | 82 m_client->addObserver(this); |
| 83 } | 83 } |
| 84 | 84 |
| 85 USB::~USB() | 85 USB::~USB() |
| 86 { | 86 { |
| 87 if (m_client) | 87 if (m_client) |
| 88 m_client->setObserver(nullptr); | 88 m_client->removeObserver(this); |
| 89 } | 89 } |
| 90 | 90 |
| 91 ScriptPromise USB::getDevices(ScriptState* scriptState) | 91 ScriptPromise USB::getDevices(ScriptState* scriptState) |
| 92 { | 92 { |
| 93 if (!m_client) | 93 if (!m_client) |
| 94 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | 94 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 95 | 95 |
| 96 String errorMessage; | 96 String errorMessage; |
| 97 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) | 97 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) |
| 98 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, errorMessage)); | 98 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, errorMessage)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } | 132 } |
| 133 | 133 |
| 134 const AtomicString& USB::interfaceName() const | 134 const AtomicString& USB::interfaceName() const |
| 135 { | 135 { |
| 136 return EventTargetNames::USB; | 136 return EventTargetNames::USB; |
| 137 } | 137 } |
| 138 | 138 |
| 139 void USB::willDetachFrameHost() | 139 void USB::willDetachFrameHost() |
| 140 { | 140 { |
| 141 if (m_client) | 141 if (m_client) |
| 142 m_client->setObserver(nullptr); | 142 m_client->removeObserver(this); |
| 143 m_client = nullptr; | 143 m_client = nullptr; |
| 144 } | 144 } |
| 145 | 145 |
| 146 void USB::onDeviceConnected(WebPassOwnPtr<WebUSBDevice> device) | 146 void USB::onDeviceConnected(WebPassOwnPtr<WebUSBDevice> device) |
| 147 { | 147 { |
| 148 dispatchEvent(USBConnectionEvent::create(EventTypeNames::connect, USBDevice:
:create(device.release(), getExecutionContext()))); | 148 dispatchEvent(USBConnectionEvent::create(EventTypeNames::connect, USBDevice:
:create(device.release(), getExecutionContext()))); |
| 149 } | 149 } |
| 150 | 150 |
| 151 void USB::onDeviceDisconnected(WebPassOwnPtr<WebUSBDevice> device) | 151 void USB::onDeviceDisconnected(WebPassOwnPtr<WebUSBDevice> device) |
| 152 { | 152 { |
| 153 dispatchEvent(USBConnectionEvent::create(EventTypeNames::disconnect, USBDevi
ce::create(device.release(), getExecutionContext()))); | 153 dispatchEvent(USBConnectionEvent::create(EventTypeNames::disconnect, USBDevi
ce::create(device.release(), getExecutionContext()))); |
| 154 } | 154 } |
| 155 | 155 |
| 156 DEFINE_TRACE(USB) | 156 DEFINE_TRACE(USB) |
| 157 { | 157 { |
| 158 RefCountedGarbageCollectedEventTargetWithInlineData<USB>::trace(visitor); | 158 RefCountedGarbageCollectedEventTargetWithInlineData<USB>::trace(visitor); |
| 159 LocalFrameLifecycleObserver::trace(visitor); | 159 LocalFrameLifecycleObserver::trace(visitor); |
| 160 } | 160 } |
| 161 | 161 |
| 162 } // namespace blink | 162 } // namespace blink |
| OLD | NEW |