Chromium Code Reviews| Index: third_party/WebKit/Source/modules/webusb/USBDevice.cpp |
| diff --git a/third_party/WebKit/Source/modules/webusb/USBDevice.cpp b/third_party/WebKit/Source/modules/webusb/USBDevice.cpp |
| index e35f6d47a7c9cd4a12f333b43c02287ab3319116..9c998c3090a221c70b2649806629e4fe91fd8f5c 100644 |
| --- a/third_party/WebKit/Source/modules/webusb/USBDevice.cpp |
| +++ b/third_party/WebKit/Source/modules/webusb/USBDevice.cpp |
| @@ -27,6 +27,8 @@ namespace blink { |
| namespace { |
| const char kDeviceStateChangeInProgress[] = "An operation that changes the device state is in progress."; |
| +const char kInterfaceNotFound[] = "The interface number provided is not supported by the device in its current configuration."; |
| +const char kInterfaceStateChangeInProgress[] = "An operation that changes interface state is in progress."; |
| const char kOpenRequired[] = "The device must be opened first."; |
| DOMException* convertControlTransferParameters( |
| @@ -45,6 +47,8 @@ DOMException* convertControlTransferParameters( |
| else |
| return DOMException::create(TypeMismatchError, "The control transfer requestType parameter is invalid."); |
| + // TODO(reillyg): Check for interface and endpoint availability if that is |
| + // the control transfer target. |
| if (parameters.recipient() == "device") |
| webParameters->recipient = WebUSBDevice::RequestRecipient::Device; |
| else if (parameters.recipient() == "interface") |
| @@ -90,8 +94,7 @@ public: |
| { |
| if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| return; |
| - if (m_device) |
| - m_device->onDeviceOpenedOrClosed(m_desiredState); |
| + m_device->onDeviceOpenedOrClosed(m_desiredState); |
| m_resolver->resolve(); |
| } |
| @@ -99,13 +102,12 @@ public: |
| { |
| if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| return; |
| - if (m_device) |
| - m_device->onDeviceOpenedOrClosed(!m_desiredState); |
| + m_device->onDeviceOpenedOrClosed(!m_desiredState); |
| m_resolver->reject(USBError::take(m_resolver, e)); |
| } |
| private: |
| - WeakPersistent<USBDevice> m_device; |
| + Persistent<USBDevice> m_device; |
| Persistent<ScriptPromiseResolver> m_resolver; |
| bool m_desiredState; // true: open, false: closed |
| }; |
| @@ -123,8 +125,7 @@ public: |
| { |
| if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| return; |
| - if (m_device) |
| - m_device->onConfigurationSelected(true /* success */, m_configurationIndex); |
| + m_device->onConfigurationSelected(true /* success */, m_configurationIndex); |
| m_resolver->resolve(); |
| } |
| @@ -132,17 +133,49 @@ public: |
| { |
| if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| return; |
| - if (m_device) |
| - m_device->onConfigurationSelected(false /* failure */, m_configurationIndex); |
| + m_device->onConfigurationSelected(false /* failure */, m_configurationIndex); |
| m_resolver->reject(USBError::take(m_resolver, e)); |
| } |
| private: |
| - WeakPersistent<USBDevice> m_device; |
| + Persistent<USBDevice> m_device; |
| Persistent<ScriptPromiseResolver> m_resolver; |
| int m_configurationIndex; |
| }; |
| +class ClaimInterfacePromiseAdapter : public WebCallbacks<void, const WebUSBError&> { |
|
juncai
2016/03/17 01:21:11
Is class ClaimInterfacePromiseAdapter used anywher
juncai
2016/03/17 05:59:34
ah, please ignore this comment. I just realized th
Reilly Grant (use Gerrit)
2016/03/17 17:07:01
It is used in claimInterface and releaseInterface.
|
| +public: |
| + ClaimInterfacePromiseAdapter(USBDevice* device, ScriptPromiseResolver* resolver, int interfaceIndex, bool desiredState) |
| + : m_device(device) |
| + , m_resolver(resolver) |
| + , m_interfaceIndex(interfaceIndex) |
| + , m_desiredState(desiredState) |
| + { |
| + } |
| + |
| + void onSuccess() override |
| + { |
| + if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| + return; |
| + m_device->onInterfaceClaimedOrUnclaimed(m_desiredState, m_interfaceIndex); |
| + m_resolver->resolve(); |
| + } |
| + |
| + void onError(const WebUSBError& e) override |
| + { |
| + if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| + return; |
| + m_device->onInterfaceClaimedOrUnclaimed(!m_desiredState, m_interfaceIndex); |
| + m_resolver->reject(USBError::take(m_resolver, e)); |
| + } |
| + |
| +private: |
| + Persistent<USBDevice> m_device; |
| + Persistent<ScriptPromiseResolver> m_resolver; |
| + int m_interfaceIndex; |
| + bool m_desiredState; // true: claimed, false: unclaimed |
| +}; |
| + |
| class InputTransferResult { |
| WTF_MAKE_NONCOPYABLE(InputTransferResult); |
| public: |
| @@ -249,8 +282,11 @@ USBDevice::USBDevice(PassOwnPtr<WebUSBDevice> device, ExecutionContext* context) |
| , m_device(device) |
| , m_opened(false) |
| , m_deviceStateChangeInProgress(false) |
| + , m_configurationIndex(-1) |
| { |
| - m_configurationIndex = findConfigurationIndex(info().activeConfiguration); |
| + int configurationIndex = findConfigurationIndex(info().activeConfiguration); |
| + if (configurationIndex != -1) |
| + onConfigurationSelected(true, configurationIndex); |
|
juncai
2016/03/17 01:21:11
nit: add comment for true parameter.
Reilly Grant (use Gerrit)
2016/03/17 17:07:01
Done.
|
| } |
| void USBDevice::onDeviceOpenedOrClosed(bool opened) |
| @@ -261,11 +297,31 @@ void USBDevice::onDeviceOpenedOrClosed(bool opened) |
| void USBDevice::onConfigurationSelected(bool success, int configurationIndex) |
| { |
| - if (success) |
| + if (success) { |
| m_configurationIndex = configurationIndex; |
| + size_t numInterfaces = info().configurations[m_configurationIndex].interfaces.size(); |
| + m_claimedInterfaces.clearAll(); |
| + m_claimedInterfaces.resize(numInterfaces); |
| + m_interfaceStateChangeInProgress.clearAll(); |
| + m_interfaceStateChangeInProgress.resize(numInterfaces); |
| + } |
| m_deviceStateChangeInProgress = false; |
| } |
| +void USBDevice::onInterfaceClaimedOrUnclaimed(bool claimed, int interfaceIndex) |
| +{ |
| + if (claimed) |
| + m_claimedInterfaces.set(interfaceIndex); |
| + else |
| + m_claimedInterfaces.clear(interfaceIndex); |
| + m_interfaceStateChangeInProgress.clear(interfaceIndex); |
| +} |
| + |
| +bool USBDevice::isInterfaceClaimed(size_t configurationIndex, size_t interfaceIndex) const |
| +{ |
| + return m_configurationIndex != -1 && static_cast<size_t>(m_configurationIndex) == configurationIndex && m_claimedInterfaces.get(interfaceIndex); |
| +} |
| + |
| USBConfiguration* USBDevice::configuration() const |
| { |
| if (m_configurationIndex != -1) |
| @@ -288,6 +344,8 @@ ScriptPromise USBDevice::open(ScriptState* scriptState) |
| ScriptPromise promise = resolver->promise(); |
| if (m_deviceStateChangeInProgress) { |
| resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + } else if (anyInterfaceChangeInProgress()) { |
| + resolver->reject(DOMException::create(InvalidStateError, kInterfaceStateChangeInProgress)); |
|
juncai
2016/03/17 01:21:11
USBDevice::open, USBDevice::close and USBDevice::s
Reilly Grant (use Gerrit)
2016/03/17 17:07:01
Done.
|
| } else if (m_opened) { |
| resolver->resolve(); |
| } else { |
| @@ -303,6 +361,8 @@ ScriptPromise USBDevice::close(ScriptState* scriptState) |
| ScriptPromise promise = resolver->promise(); |
| if (m_deviceStateChangeInProgress) { |
| resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + } else if (anyInterfaceChangeInProgress()) { |
| + resolver->reject(DOMException::create(InvalidStateError, kInterfaceStateChangeInProgress)); |
| } else if (!m_opened) { |
| resolver->resolve(); |
| } else { |
| @@ -320,6 +380,8 @@ ScriptPromise USBDevice::selectConfiguration(ScriptState* scriptState, uint8_t c |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| } else if (m_deviceStateChangeInProgress) { |
| resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + } else if (anyInterfaceChangeInProgress()) { |
| + resolver->reject(DOMException::create(InvalidStateError, kInterfaceStateChangeInProgress)); |
| } else { |
| int configurationIndex = findConfigurationIndex(configurationValue); |
| if (configurationIndex == -1) { |
| @@ -338,8 +400,19 @@ ScriptPromise USBDevice::claimInterface(ScriptState* scriptState, uint8_t interf |
| { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - if (ensureDeviceConfigured(resolver)) |
| - m_device->claimInterface(interfaceNumber, new CallbackPromiseAdapter<void, USBError>(resolver)); |
| + if (ensureDeviceConfigured(resolver)) { |
| + int interfaceIndex = findInterfaceIndex(interfaceNumber); |
| + if (interfaceIndex == -1) { |
| + resolver->reject(DOMException::create(NotFoundError, kInterfaceNotFound)); |
| + } else if (m_interfaceStateChangeInProgress.get(interfaceIndex)) { |
|
juncai
2016/03/17 01:21:11
Use static_cast<size_t>(interfaceIndex) since: get
Reilly Grant (use Gerrit)
2016/03/17 17:07:01
Done.
|
| + resolver->reject(DOMException::create(InvalidStateError, kInterfaceStateChangeInProgress)); |
| + } else if (m_claimedInterfaces.get(interfaceIndex)) { |
| + resolver->resolve(); |
| + } else { |
| + m_interfaceStateChangeInProgress.set(interfaceIndex); |
| + m_device->claimInterface(interfaceNumber, new ClaimInterfacePromiseAdapter(this, resolver, interfaceIndex, true)); |
| + } |
| + } |
| return promise; |
| } |
| @@ -347,8 +420,19 @@ ScriptPromise USBDevice::releaseInterface(ScriptState* scriptState, uint8_t inte |
| { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - if (ensureDeviceConfigured(resolver)) |
| - m_device->releaseInterface(interfaceNumber, new CallbackPromiseAdapter<void, USBError>(resolver)); |
| + if (ensureDeviceConfigured(resolver)) { |
| + int interfaceIndex = findInterfaceIndex(interfaceNumber); |
| + if (interfaceIndex == -1) { |
| + resolver->reject(DOMException::create(NotFoundError, "The interface number provided is not supported by the device in its current configuration.")); |
| + } else if (m_interfaceStateChangeInProgress.get(interfaceIndex)) { |
| + resolver->reject(DOMException::create(InvalidStateError, kInterfaceStateChangeInProgress)); |
| + } else if (!m_claimedInterfaces.get(interfaceIndex)) { |
| + resolver->resolve(); |
| + } else { |
| + m_interfaceStateChangeInProgress.set(interfaceIndex); |
| + m_device->releaseInterface(interfaceNumber, new ClaimInterfacePromiseAdapter(this, resolver, interfaceIndex, false)); |
| + } |
| + } |
| return promise; |
| } |
| @@ -356,7 +440,7 @@ ScriptPromise USBDevice::setInterface(ScriptState* scriptState, uint8_t interfac |
| { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - if (ensureDeviceConfigured(resolver)) |
| + if (ensureInterfaceClaimed(interfaceNumber, resolver)) |
| m_device->setInterface(interfaceNumber, alternateSetting, new CallbackPromiseAdapter<void, USBError>(resolver)); |
| return promise; |
| } |
| @@ -408,11 +492,11 @@ ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC |
| return promise; |
| } |
| -ScriptPromise USBDevice::clearHalt(ScriptState* scriptState, uint8_t endpointNumber) |
| +ScriptPromise USBDevice::clearHalt(ScriptState* scriptState, String direction, uint8_t endpointNumber) |
| { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - if (ensureDeviceConfigured(resolver)) |
| + if (ensureEndpointAvailable(direction == "in", endpointNumber, resolver)) |
| m_device->clearHalt(endpointNumber, new CallbackPromiseAdapter<void, USBError>(resolver)); |
| return promise; |
| } |
| @@ -421,7 +505,7 @@ ScriptPromise USBDevice::transferIn(ScriptState* scriptState, uint8_t endpointNu |
| { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - if (ensureDeviceConfigured(resolver)) |
| + if (ensureEndpointAvailable(true, endpointNumber, resolver)) |
| m_device->transfer(WebUSBDevice::TransferDirection::In, endpointNumber, nullptr, length, 0, new CallbackPromiseAdapter<InputTransferResult, USBError>(resolver)); |
| return promise; |
| } |
| @@ -430,7 +514,7 @@ ScriptPromise USBDevice::transferOut(ScriptState* scriptState, uint8_t endpointN |
| { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - if (ensureDeviceConfigured(resolver)) { |
| + if (ensureEndpointAvailable(false, endpointNumber, resolver)) { |
| BufferSource buffer(data); |
| m_device->transfer(WebUSBDevice::TransferDirection::Out, endpointNumber, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResult, USBError>(resolver)); |
| } |
| @@ -441,7 +525,7 @@ ScriptPromise USBDevice::isochronousTransferIn(ScriptState* scriptState, uint8_t |
| { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - if (ensureDeviceConfigured(resolver)) |
| + if (ensureEndpointAvailable(true, endpointNumber, resolver)) |
| m_device->isochronousTransfer(WebUSBDevice::TransferDirection::In, endpointNumber, nullptr, 0, packetLengths, 0, new CallbackPromiseAdapter<IsochronousInputTransferResult, USBError>(resolver)); |
| return promise; |
| } |
| @@ -450,7 +534,7 @@ ScriptPromise USBDevice::isochronousTransferOut(ScriptState* scriptState, uint8_ |
| { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - if (ensureDeviceConfigured(resolver)) { |
| + if (ensureEndpointAvailable(false, endpointNumber, resolver)) { |
| BufferSource buffer(data); |
| m_device->isochronousTransfer(WebUSBDevice::TransferDirection::Out, endpointNumber, buffer.data(), buffer.size(), packetLengths, 0, new CallbackPromiseAdapter<IsochronousOutputTransferResult, USBError>(resolver)); |
| } |
| @@ -491,6 +575,17 @@ int USBDevice::findConfigurationIndex(uint8_t configurationValue) const |
| return -1; |
| } |
| +int USBDevice::findInterfaceIndex(uint8_t interfaceNumber) const |
|
juncai
2016/03/17 01:21:11
This function may need to return size_t.
Reilly Grant (use Gerrit)
2016/03/17 17:07:01
It can't return -1 if it returns size_t.
|
| +{ |
| + ASSERT(m_configurationIndex != -1); |
| + const auto& interfaces = info().configurations[m_configurationIndex].interfaces; |
| + for (size_t i = 0; i < interfaces.size(); ++i) { |
| + if (interfaces[i].interfaceNumber == interfaceNumber) |
| + return i; |
| + } |
| + return -1; |
| +} |
| + |
| bool USBDevice::ensureDeviceConfigured(ScriptPromiseResolver* resolver) const |
| { |
| if (!m_opened) { |
| @@ -505,4 +600,37 @@ bool USBDevice::ensureDeviceConfigured(ScriptPromiseResolver* resolver) const |
| return false; |
| } |
| +bool USBDevice::ensureInterfaceClaimed(uint8_t interfaceNumber, ScriptPromiseResolver* resolver) const |
| +{ |
| + if (!ensureDeviceConfigured(resolver)) |
| + return false; |
| + int interfaceIndex = findInterfaceIndex(interfaceNumber); |
| + if (interfaceIndex == -1) { |
| + resolver->reject(DOMException::create(NotFoundError, kInterfaceNotFound)); |
| + } else if (m_interfaceStateChangeInProgress.get(interfaceIndex)) { |
| + resolver->reject(DOMException::create(InvalidStateError, kInterfaceStateChangeInProgress)); |
| + } else if (!m_claimedInterfaces.get(interfaceIndex)) { |
| + resolver->reject(DOMException::create(InvalidStateError, "The specified interface has not been claimed.")); |
| + } else { |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +bool USBDevice::ensureEndpointAvailable(bool inTransfer, uint8_t endpointNumber, ScriptPromiseResolver* resolver) const |
| +{ |
| + // TODO(reillyg): Check endpoint availability once Blink is tracking which |
| + // alternate interfaces are selected. |
| + return ensureDeviceConfigured(resolver); |
| +} |
| + |
| +bool USBDevice::anyInterfaceChangeInProgress() const |
| +{ |
| + for (size_t i = 0; i < m_interfaceStateChangeInProgress.size(); ++i) { |
| + if (m_interfaceStateChangeInProgress.quickGet(i)) |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| } // namespace blink |