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 d4bc001f16c4fc67ca73446b92413b26eed81e82..ed86b89bec431880d2261c52dde92527d05b3d1d 100644 |
| --- a/third_party/WebKit/Source/modules/webusb/USBDevice.cpp |
| +++ b/third_party/WebKit/Source/modules/webusb/USBDevice.cpp |
| @@ -26,8 +26,9 @@ namespace blink { |
| namespace { |
| -const char kOpenCloseInProgress[] = "An open() or close() task is in progress."; |
| -const char kOpenRequired[] = "The device must be open()ed first."; |
| +const char kDeviceStateChangeInProgress[] = "An operation that changes the device state is in progress."; |
| +const char kOpenRequired[] = "The device must be opened first."; |
| +const char kNotConfigured[] = "The device must have a configuration selected."; |
| DOMException* convertControlTransferParameters( |
| WebUSBDevice::TransferDirection direction, |
| @@ -110,27 +111,37 @@ private: |
| bool m_desiredState; // true: open, false: closed |
| }; |
| -class GetConfigurationPromiseAdapter : public WebCallbacks<uint8_t, const WebUSBError&> { |
| +class SelectConfigurationPromiseAdapter : public WebCallbacks<void, const WebUSBError&> { |
| public: |
| - GetConfigurationPromiseAdapter(USBDevice* device, ScriptPromiseResolver* resolver) : m_device(device), m_resolver(resolver) {} |
| + SelectConfigurationPromiseAdapter(USBDevice* device, ScriptPromiseResolver* resolver, uint8_t configurationValue) |
| + : m_device(device) |
| + , m_resolver(resolver) |
| + , m_configurationValue(configurationValue) |
| + { |
| + } |
| - void onSuccess(uint8_t value) override |
| + void onSuccess() override |
| { |
| if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| return; |
| - m_resolver->resolve(USBConfiguration::createFromValue(m_device, value)); |
| + if (m_device) |
| + m_device->onConfigurationSelected(true, m_configurationValue); |
|
juncai
2016/03/10 22:14:04
nit: add comment for the true parameter.
Reilly Grant (use Gerrit)
2016/03/11 00:08:36
Done.
|
| + m_resolver->resolve(); |
| } |
| void onError(const WebUSBError& e) override |
| { |
| if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| return; |
| + if (m_device) |
| + m_device->onConfigurationSelected(false, m_configurationValue); |
|
juncai
2016/03/10 22:14:04
nit: add comment for the false parameter.
Reilly Grant (use Gerrit)
2016/03/11 00:08:36
Done.
|
| m_resolver->reject(USBError::take(m_resolver, e)); |
| } |
| private: |
| - Persistent<USBDevice> m_device; |
| + WeakPersistent<USBDevice> m_device; |
| Persistent<ScriptPromiseResolver> m_resolver; |
| + uint8_t m_configurationValue; |
| }; |
| class InputTransferResult { |
| @@ -240,6 +251,24 @@ void USBDevice::onDeviceOpenedOrClosed(bool opened) |
| m_deviceStateChangeInProgress = false; |
| } |
| +void USBDevice::onConfigurationSelected(bool success, uint8_t configurationValue) |
| +{ |
| + if (success) |
| + m_activeConfiguration = configurationValue; |
| + m_deviceStateChangeInProgress = false; |
| +} |
| + |
| +USBConfiguration* USBDevice::configuration() const |
| +{ |
| + if (m_activeConfiguration != 0) { |
| + for (size_t i = 0; i < info().configurations.size(); ++i) { |
| + if (info().configurations[i].configurationValue == m_activeConfiguration) |
| + return USBConfiguration::create(this, i); |
| + } |
| + } |
| + return nullptr; |
| +} |
| + |
| HeapVector<Member<USBConfiguration>> USBDevice::configurations() const |
| { |
| HeapVector<Member<USBConfiguration>> configurations; |
| @@ -253,7 +282,7 @@ ScriptPromise USBDevice::open(ScriptState* scriptState) |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| if (m_deviceStateChangeInProgress) { |
| - resolver->reject(DOMException::create(InvalidStateError, kOpenCloseInProgress)); |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| } else if (m_opened) { |
| resolver->resolve(); |
| } else { |
| @@ -268,7 +297,7 @@ ScriptPromise USBDevice::close(ScriptState* scriptState) |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| if (m_deviceStateChangeInProgress) { |
| - resolver->reject(DOMException::create(InvalidStateError, kOpenCloseInProgress)); |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| } else if (!m_opened) { |
| resolver->resolve(); |
| } else { |
| @@ -278,22 +307,29 @@ ScriptPromise USBDevice::close(ScriptState* scriptState) |
| return promise; |
| } |
| -ScriptPromise USBDevice::getConfiguration(ScriptState* scriptState) |
| +ScriptPromise USBDevice::selectConfiguration(ScriptState* scriptState, uint8_t configurationValue) |
| { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - m_device->getConfiguration(new GetConfigurationPromiseAdapter(this, resolver)); |
| - return promise; |
| -} |
| - |
| -ScriptPromise USBDevice::setConfiguration(ScriptState* scriptState, uint8_t configurationValue) |
| -{ |
| - ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| - ScriptPromise promise = resolver->promise(); |
| - if (!m_opened) |
| + bool foundConfiguration = false; |
| + for (size_t i = 0; i < info().configurations.size(); ++i) { |
| + if (info().configurations[i].configurationValue == configurationValue) { |
| + foundConfiguration = true; |
| + break; |
| + } |
| + } |
| + if (!m_opened) { |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| - else |
| - m_device->setConfiguration(configurationValue, new CallbackPromiseAdapter<void, USBError>(resolver)); |
| + } else if (m_deviceStateChangeInProgress) { |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
|
juncai
2016/03/10 22:14:04
The above two if conditions don't need the foundCo
Reilly Grant (use Gerrit)
2016/03/11 00:08:36
Done.
|
| + } else if (!foundConfiguration) { |
| + resolver->reject(DOMException::create(NotFoundError, "The configuration value provided is not supported by the device.")); |
| + } else if (m_activeConfiguration == configurationValue) { |
| + resolver->resolve(); |
| + } else { |
| + m_deviceStateChangeInProgress = true; |
| + m_device->setConfiguration(configurationValue, new SelectConfigurationPromiseAdapter(this, resolver, configurationValue)); |
| + } |
| return promise; |
| } |
| @@ -303,6 +339,10 @@ ScriptPromise USBDevice::claimInterface(ScriptState* scriptState, uint8_t interf |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| + else if (m_deviceStateChangeInProgress) |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + else if (m_activeConfiguration == 0) |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| else |
| m_device->claimInterface(interfaceNumber, new CallbackPromiseAdapter<void, USBError>(resolver)); |
| return promise; |
| @@ -314,6 +354,10 @@ ScriptPromise USBDevice::releaseInterface(ScriptState* scriptState, uint8_t inte |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| + else if (m_deviceStateChangeInProgress) |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + else if (m_activeConfiguration == 0) |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| else |
| m_device->releaseInterface(interfaceNumber, new CallbackPromiseAdapter<void, USBError>(resolver)); |
| return promise; |
| @@ -325,6 +369,10 @@ ScriptPromise USBDevice::setInterface(ScriptState* scriptState, uint8_t interfac |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| + else if (m_deviceStateChangeInProgress) |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + else if (m_activeConfiguration == 0) |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| else |
| m_device->setInterface(interfaceNumber, alternateSetting, new CallbackPromiseAdapter<void, USBError>(resolver)); |
| return promise; |
| @@ -336,17 +384,18 @@ ScriptPromise USBDevice::controlTransferIn(ScriptState* scriptState, const USBCo |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) { |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| - return promise; |
| - } |
| - |
| - WebUSBDevice::ControlTransferParameters parameters; |
| - DOMException* error = convertControlTransferParameters(WebUSBDevice::TransferDirection::In, setup, ¶meters); |
| - if (error) { |
| - resolver->reject(error); |
| - return promise; |
| + } else if (m_deviceStateChangeInProgress) { |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + } else if (m_activeConfiguration == 0) { |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| + } else { |
| + WebUSBDevice::ControlTransferParameters parameters; |
| + DOMException* error = convertControlTransferParameters(WebUSBDevice::TransferDirection::In, setup, ¶meters); |
| + if (error) |
| + resolver->reject(error); |
| + else |
| + m_device->controlTransfer(parameters, nullptr, length, 0, new CallbackPromiseAdapter<InputTransferResult, USBError>(resolver)); |
| } |
| - |
| - m_device->controlTransfer(parameters, nullptr, length, 0, new CallbackPromiseAdapter<InputTransferResult, USBError>(resolver)); |
| return promise; |
| } |
| @@ -356,17 +405,18 @@ ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) { |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| - return promise; |
| - } |
| - |
| - WebUSBDevice::ControlTransferParameters parameters; |
| - DOMException* error = convertControlTransferParameters(WebUSBDevice::TransferDirection::Out, setup, ¶meters); |
| - if (error) { |
| - resolver->reject(error); |
| - return promise; |
| + } else if (m_deviceStateChangeInProgress) { |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + } else if (m_activeConfiguration == 0) { |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| + } else { |
| + WebUSBDevice::ControlTransferParameters parameters; |
| + DOMException* error = convertControlTransferParameters(WebUSBDevice::TransferDirection::Out, setup, ¶meters); |
| + if (error) |
| + resolver->reject(error); |
| + else |
| + m_device->controlTransfer(parameters, nullptr, 0, 0, new CallbackPromiseAdapter<OutputTransferResult, USBError>(resolver)); |
| } |
| - |
| - m_device->controlTransfer(parameters, nullptr, 0, 0, new CallbackPromiseAdapter<OutputTransferResult, USBError>(resolver)); |
| return promise; |
| } |
| @@ -376,18 +426,20 @@ ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) { |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| - return promise; |
| - } |
| - |
| - WebUSBDevice::ControlTransferParameters parameters; |
| - DOMException* error = convertControlTransferParameters(WebUSBDevice::TransferDirection::Out, setup, ¶meters); |
| - if (error) { |
| - resolver->reject(error); |
| - return promise; |
| + } else if (m_deviceStateChangeInProgress) { |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + } else if (m_activeConfiguration == 0) { |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| + } else { |
| + WebUSBDevice::ControlTransferParameters parameters; |
| + DOMException* error = convertControlTransferParameters(WebUSBDevice::TransferDirection::Out, setup, ¶meters); |
| + if (error) { |
| + resolver->reject(error); |
| + } else { |
| + BufferSource buffer(data); |
| + m_device->controlTransfer(parameters, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResult, USBError>(resolver)); |
| + } |
| } |
| - |
| - BufferSource buffer(data); |
| - m_device->controlTransfer(parameters, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResult, USBError>(resolver)); |
| return promise; |
| } |
| @@ -397,6 +449,10 @@ ScriptPromise USBDevice::clearHalt(ScriptState* scriptState, uint8_t endpointNum |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| + else if (m_deviceStateChangeInProgress) |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + else if (m_activeConfiguration == 0) |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| else |
| m_device->clearHalt(endpointNumber, new CallbackPromiseAdapter<void, USBError>(resolver)); |
| return promise; |
| @@ -408,6 +464,10 @@ ScriptPromise USBDevice::transferIn(ScriptState* scriptState, uint8_t endpointNu |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| + else if (m_deviceStateChangeInProgress) |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + else if (m_activeConfiguration == 0) |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| else |
| m_device->transfer(WebUSBDevice::TransferDirection::In, endpointNumber, nullptr, length, 0, new CallbackPromiseAdapter<InputTransferResult, USBError>(resolver)); |
| return promise; |
| @@ -419,6 +479,10 @@ ScriptPromise USBDevice::transferOut(ScriptState* scriptState, uint8_t endpointN |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) { |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| + } else if (m_deviceStateChangeInProgress) { |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + } else if (m_activeConfiguration == 0) { |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| } else { |
| BufferSource buffer(data); |
| m_device->transfer(WebUSBDevice::TransferDirection::Out, endpointNumber, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResult, USBError>(resolver)); |
| @@ -432,6 +496,10 @@ ScriptPromise USBDevice::isochronousTransferIn(ScriptState* scriptState, uint8_t |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| + else if (m_deviceStateChangeInProgress) |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + else if (m_activeConfiguration == 0) |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| else |
| m_device->isochronousTransfer(WebUSBDevice::TransferDirection::In, endpointNumber, nullptr, 0, packetLengths, 0, new CallbackPromiseAdapter<IsochronousInputTransferResult, USBError>(resolver)); |
| return promise; |
| @@ -443,6 +511,10 @@ ScriptPromise USBDevice::isochronousTransferOut(ScriptState* scriptState, uint8_ |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) { |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| + } else if (m_deviceStateChangeInProgress) { |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| + } else if (m_activeConfiguration == 0) { |
| + resolver->reject(DOMException::create(InvalidStateError, kNotConfigured)); |
| } else { |
| BufferSource buffer(data); |
| m_device->isochronousTransfer(WebUSBDevice::TransferDirection::Out, endpointNumber, buffer.data(), buffer.size(), packetLengths, 0, new CallbackPromiseAdapter<IsochronousOutputTransferResult, USBError>(resolver)); |
| @@ -456,6 +528,8 @@ ScriptPromise USBDevice::reset(ScriptState* scriptState) |
| ScriptPromise promise = resolver->promise(); |
| if (!m_opened) |
| resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)); |
| + else if (m_deviceStateChangeInProgress) |
| + resolver->reject(DOMException::create(InvalidStateError, kDeviceStateChangeInProgress)); |
| else |
| m_device->reset(new CallbackPromiseAdapter<void, USBError>(resolver)); |
| return promise; |