Chromium Code Reviews| 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/USBDevice.h" | 5 #include "modules/webusb/USBDevice.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 "bindings/core/v8/ToV8.h" | 10 #include "bindings/core/v8/ToV8.h" |
| 11 #include "core/dom/DOMArrayBuffer.h" | 11 #include "core/dom/DOMArrayBuffer.h" |
| 12 #include "core/dom/DOMArrayBufferView.h" | 12 #include "core/dom/DOMArrayBufferView.h" |
| 13 #include "core/dom/DOMException.h" | 13 #include "core/dom/DOMException.h" |
| 14 #include "core/dom/ExceptionCode.h" | 14 #include "core/dom/ExceptionCode.h" |
| 15 #include "modules/webusb/USBConfiguration.h" | 15 #include "modules/webusb/USBConfiguration.h" |
| 16 #include "modules/webusb/USBControlTransferParameters.h" | 16 #include "modules/webusb/USBControlTransferParameters.h" |
| 17 #include "modules/webusb/USBError.h" | 17 #include "modules/webusb/USBError.h" |
| 18 #include "modules/webusb/USBInTransferResult.h" | 18 #include "modules/webusb/USBInTransferResult.h" |
| 19 #include "modules/webusb/USBIsochronousInTransferResult.h" | 19 #include "modules/webusb/USBIsochronousInTransferResult.h" |
| 20 #include "modules/webusb/USBIsochronousOutTransferResult.h" | 20 #include "modules/webusb/USBIsochronousOutTransferResult.h" |
| 21 #include "modules/webusb/USBOutTransferResult.h" | 21 #include "modules/webusb/USBOutTransferResult.h" |
| 22 #include "public/platform/modules/webusb/WebUSBTransferInfo.h" | 22 #include "public/platform/modules/webusb/WebUSBTransferInfo.h" |
| 23 #include "wtf/Assertions.h" | 23 #include "wtf/Assertions.h" |
| 24 | 24 |
| 25 namespace blink { | 25 namespace blink { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const char kOpenCloseInProgress[] = "An open() or close() task is in progress."; | 29 const char kDeviceStateChangeInProgress[] = "An operation that changes the devic e state is in progress."; |
| 30 const char kOpenRequired[] = "The device must be open()ed first."; | 30 const char kOpenRequired[] = "The device must be opened first."; |
| 31 | 31 |
| 32 DOMException* convertControlTransferParameters( | 32 DOMException* convertControlTransferParameters( |
| 33 WebUSBDevice::TransferDirection direction, | 33 WebUSBDevice::TransferDirection direction, |
| 34 const USBControlTransferParameters& parameters, | 34 const USBControlTransferParameters& parameters, |
| 35 WebUSBDevice::ControlTransferParameters* webParameters) | 35 WebUSBDevice::ControlTransferParameters* webParameters) |
| 36 { | 36 { |
| 37 webParameters->direction = direction; | 37 webParameters->direction = direction; |
| 38 | 38 |
| 39 if (parameters.requestType() == "standard") | 39 if (parameters.requestType() == "standard") |
| 40 webParameters->type = WebUSBDevice::RequestType::Standard; | 40 webParameters->type = WebUSBDevice::RequestType::Standard; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 m_device->onDeviceOpenedOrClosed(!m_desiredState); | 103 m_device->onDeviceOpenedOrClosed(!m_desiredState); |
| 104 m_resolver->reject(USBError::take(m_resolver, e)); | 104 m_resolver->reject(USBError::take(m_resolver, e)); |
| 105 } | 105 } |
| 106 | 106 |
| 107 private: | 107 private: |
| 108 WeakPersistent<USBDevice> m_device; | 108 WeakPersistent<USBDevice> m_device; |
| 109 Persistent<ScriptPromiseResolver> m_resolver; | 109 Persistent<ScriptPromiseResolver> m_resolver; |
| 110 bool m_desiredState; // true: open, false: closed | 110 bool m_desiredState; // true: open, false: closed |
| 111 }; | 111 }; |
| 112 | 112 |
| 113 class GetConfigurationPromiseAdapter : public WebCallbacks<uint8_t, const WebUSB Error&> { | 113 class SelectConfigurationPromiseAdapter : public WebCallbacks<void, const WebUSB Error&> { |
| 114 public: | 114 public: |
| 115 GetConfigurationPromiseAdapter(USBDevice* device, ScriptPromiseResolver* res olver) : m_device(device), m_resolver(resolver) {} | 115 SelectConfigurationPromiseAdapter(USBDevice* device, ScriptPromiseResolver* resolver, int configurationIndex) |
| 116 : m_device(device) | |
| 117 , m_resolver(resolver) | |
| 118 , m_configurationIndex(configurationIndex) | |
| 119 { | |
| 120 } | |
| 116 | 121 |
| 117 void onSuccess(uint8_t value) override | 122 void onSuccess() override |
| 118 { | 123 { |
| 119 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex t()->activeDOMObjectsAreStopped()) | 124 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex t()->activeDOMObjectsAreStopped()) |
| 120 return; | 125 return; |
| 121 m_resolver->resolve(USBConfiguration::createFromValue(m_device, value)); | 126 if (m_device) |
| 127 m_device->onConfigurationSelected(true /* success */, m_configuratio nIndex); | |
| 128 m_resolver->resolve(); | |
| 122 } | 129 } |
| 123 | 130 |
| 124 void onError(const WebUSBError& e) override | 131 void onError(const WebUSBError& e) override |
| 125 { | 132 { |
| 126 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex t()->activeDOMObjectsAreStopped()) | 133 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex t()->activeDOMObjectsAreStopped()) |
| 127 return; | 134 return; |
| 135 if (m_device) | |
| 136 m_device->onConfigurationSelected(false /* failure */, m_configurati onIndex); | |
| 128 m_resolver->reject(USBError::take(m_resolver, e)); | 137 m_resolver->reject(USBError::take(m_resolver, e)); |
| 129 } | 138 } |
| 130 | 139 |
| 131 private: | 140 private: |
| 132 Persistent<USBDevice> m_device; | 141 WeakPersistent<USBDevice> m_device; |
| 133 Persistent<ScriptPromiseResolver> m_resolver; | 142 Persistent<ScriptPromiseResolver> m_resolver; |
| 143 int m_configurationIndex; | |
| 134 }; | 144 }; |
| 135 | 145 |
| 136 class InputTransferResult { | 146 class InputTransferResult { |
| 137 WTF_MAKE_NONCOPYABLE(InputTransferResult); | 147 WTF_MAKE_NONCOPYABLE(InputTransferResult); |
| 138 public: | 148 public: |
| 139 using WebType = OwnPtr<WebUSBTransferInfo>; | 149 using WebType = OwnPtr<WebUSBTransferInfo>; |
| 140 | 150 |
| 141 static USBInTransferResult* take(ScriptPromiseResolver*, PassOwnPtr<WebUSBTr ansferInfo> webTransferInfo) | 151 static USBInTransferResult* take(ScriptPromiseResolver*, PassOwnPtr<WebUSBTr ansferInfo> webTransferInfo) |
| 142 { | 152 { |
| 143 ASSERT(webTransferInfo->status.size() == 1); | 153 ASSERT(webTransferInfo->status.size() == 1); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 }; | 237 }; |
| 228 | 238 |
| 229 } // namespace | 239 } // namespace |
| 230 | 240 |
| 231 // static | 241 // static |
| 232 USBDevice* USBDevice::take(ScriptPromiseResolver* resolver, PassOwnPtr<WebUSBDev ice> device) | 242 USBDevice* USBDevice::take(ScriptPromiseResolver* resolver, PassOwnPtr<WebUSBDev ice> device) |
| 233 { | 243 { |
| 234 return USBDevice::create(device, resolver->getExecutionContext()); | 244 return USBDevice::create(device, resolver->getExecutionContext()); |
| 235 } | 245 } |
| 236 | 246 |
| 247 USBDevice::USBDevice(PassOwnPtr<WebUSBDevice> device, ExecutionContext* context) | |
| 248 : ContextLifecycleObserver(context) | |
| 249 , m_device(device) | |
| 250 , m_opened(false) | |
| 251 , m_deviceStateChangeInProgress(false) | |
| 252 { | |
| 253 m_configurationIndex = findConfigurationIndex(info().activeConfiguration); | |
| 254 } | |
| 255 | |
| 237 void USBDevice::onDeviceOpenedOrClosed(bool opened) | 256 void USBDevice::onDeviceOpenedOrClosed(bool opened) |
| 238 { | 257 { |
| 239 m_opened = opened; | 258 m_opened = opened; |
| 240 m_deviceStateChangeInProgress = false; | 259 m_deviceStateChangeInProgress = false; |
| 241 } | 260 } |
| 242 | 261 |
| 262 void USBDevice::onConfigurationSelected(bool success, int configurationIndex) | |
| 263 { | |
| 264 if (success) | |
| 265 m_configurationIndex = configurationIndex; | |
| 266 m_deviceStateChangeInProgress = false; | |
| 267 } | |
| 268 | |
| 269 USBConfiguration* USBDevice::configuration() const | |
| 270 { | |
| 271 if (m_configurationIndex != -1) | |
| 272 return USBConfiguration::create(this, m_configurationIndex); | |
| 273 return nullptr; | |
| 274 } | |
| 275 | |
| 243 HeapVector<Member<USBConfiguration>> USBDevice::configurations() const | 276 HeapVector<Member<USBConfiguration>> USBDevice::configurations() const |
| 244 { | 277 { |
| 245 HeapVector<Member<USBConfiguration>> configurations; | 278 HeapVector<Member<USBConfiguration>> configurations; |
| 246 for (size_t i = 0; i < info().configurations.size(); ++i) | 279 size_t numConfigurations = info().configurations.size(); |
| 280 for (size_t i = 0; i < numConfigurations; ++i) | |
| 247 configurations.append(USBConfiguration::create(this, i)); | 281 configurations.append(USBConfiguration::create(this, i)); |
| 248 return configurations; | 282 return configurations; |
| 249 } | 283 } |
| 250 | 284 |
| 251 ScriptPromise USBDevice::open(ScriptState* scriptState) | 285 ScriptPromise USBDevice::open(ScriptState* scriptState) |
| 252 { | 286 { |
| 253 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 287 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 254 ScriptPromise promise = resolver->promise(); | 288 ScriptPromise promise = resolver->promise(); |
| 255 if (m_deviceStateChangeInProgress) { | 289 if (m_deviceStateChangeInProgress) { |
| 256 resolver->reject(DOMException::create(InvalidStateError, kOpenCloseInPro gress)); | 290 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); |
| 257 } else if (m_opened) { | 291 } else if (m_opened) { |
| 258 resolver->resolve(); | 292 resolver->resolve(); |
| 259 } else { | 293 } else { |
| 260 m_deviceStateChangeInProgress = true; | 294 m_deviceStateChangeInProgress = true; |
| 261 m_device->open(new OpenClosePromiseAdapter(this, resolver, true /* open */)); | 295 m_device->open(new OpenClosePromiseAdapter(this, resolver, true /* open */)); |
| 262 } | 296 } |
| 263 return promise; | 297 return promise; |
| 264 } | 298 } |
| 265 | 299 |
| 266 ScriptPromise USBDevice::close(ScriptState* scriptState) | 300 ScriptPromise USBDevice::close(ScriptState* scriptState) |
| 267 { | 301 { |
| 268 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 302 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 269 ScriptPromise promise = resolver->promise(); | 303 ScriptPromise promise = resolver->promise(); |
| 270 if (m_deviceStateChangeInProgress) { | 304 if (m_deviceStateChangeInProgress) { |
| 271 resolver->reject(DOMException::create(InvalidStateError, kOpenCloseInPro gress)); | 305 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); |
| 272 } else if (!m_opened) { | 306 } else if (!m_opened) { |
| 273 resolver->resolve(); | 307 resolver->resolve(); |
| 274 } else { | 308 } else { |
| 275 m_deviceStateChangeInProgress = true; | 309 m_deviceStateChangeInProgress = true; |
| 276 m_device->close(new OpenClosePromiseAdapter(this, resolver, false /* clo sed */)); | 310 m_device->close(new OpenClosePromiseAdapter(this, resolver, false /* clo sed */)); |
| 277 } | 311 } |
| 278 return promise; | 312 return promise; |
| 279 } | 313 } |
| 280 | 314 |
| 281 ScriptPromise USBDevice::getConfiguration(ScriptState* scriptState) | 315 ScriptPromise USBDevice::selectConfiguration(ScriptState* scriptState, uint8_t c onfigurationValue) |
| 282 { | 316 { |
| 283 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 317 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 284 ScriptPromise promise = resolver->promise(); | 318 ScriptPromise promise = resolver->promise(); |
| 285 m_device->getConfiguration(new GetConfigurationPromiseAdapter(this, resolver )); | 319 if (!m_opened) { |
| 320 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 321 } else if (m_deviceStateChangeInProgress) { | |
| 322 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 323 } else { | |
| 324 int configurationIndex = findConfigurationIndex(configurationValue); | |
| 325 if (configurationIndex == -1) { | |
| 326 resolver->reject(DOMException::create(NotFoundError, "The configurat ion value provided is not supported by the device.")); | |
| 327 } else if (m_configurationIndex == configurationIndex) { | |
| 328 resolver->resolve(); | |
| 329 } else { | |
| 330 m_deviceStateChangeInProgress = true; | |
| 331 m_device->setConfiguration(configurationValue, new SelectConfigurati onPromiseAdapter(this, resolver, configurationIndex)); | |
| 332 } | |
| 333 } | |
| 286 return promise; | 334 return promise; |
| 287 } | 335 } |
| 288 | 336 |
| 289 ScriptPromise USBDevice::setConfiguration(ScriptState* scriptState, uint8_t conf igurationValue) | |
| 290 { | |
| 291 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
| 292 ScriptPromise promise = resolver->promise(); | |
| 293 if (!m_opened) | |
| 294 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 295 else | |
| 296 m_device->setConfiguration(configurationValue, new CallbackPromiseAdapte r<void, USBError>(resolver)); | |
| 297 return promise; | |
| 298 } | |
| 299 | |
| 300 ScriptPromise USBDevice::claimInterface(ScriptState* scriptState, uint8_t interf aceNumber) | 337 ScriptPromise USBDevice::claimInterface(ScriptState* scriptState, uint8_t interf aceNumber) |
| 301 { | 338 { |
| 302 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 339 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 303 ScriptPromise promise = resolver->promise(); | 340 ScriptPromise promise = resolver->promise(); |
| 304 if (!m_opened) | 341 if (ensureDeviceConfigured(resolver)) |
| 305 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 306 else | |
| 307 m_device->claimInterface(interfaceNumber, new CallbackPromiseAdapter<voi d, USBError>(resolver)); | 342 m_device->claimInterface(interfaceNumber, new CallbackPromiseAdapter<voi d, USBError>(resolver)); |
| 308 return promise; | 343 return promise; |
| 309 } | 344 } |
| 310 | 345 |
| 311 ScriptPromise USBDevice::releaseInterface(ScriptState* scriptState, uint8_t inte rfaceNumber) | 346 ScriptPromise USBDevice::releaseInterface(ScriptState* scriptState, uint8_t inte rfaceNumber) |
| 312 { | 347 { |
| 313 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 348 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 314 ScriptPromise promise = resolver->promise(); | 349 ScriptPromise promise = resolver->promise(); |
| 315 if (!m_opened) | 350 if (ensureDeviceConfigured(resolver)) |
| 316 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 317 else | |
| 318 m_device->releaseInterface(interfaceNumber, new CallbackPromiseAdapter<v oid, USBError>(resolver)); | 351 m_device->releaseInterface(interfaceNumber, new CallbackPromiseAdapter<v oid, USBError>(resolver)); |
| 319 return promise; | 352 return promise; |
| 320 } | 353 } |
| 321 | 354 |
| 322 ScriptPromise USBDevice::setInterface(ScriptState* scriptState, uint8_t interfac eNumber, uint8_t alternateSetting) | 355 ScriptPromise USBDevice::setInterface(ScriptState* scriptState, uint8_t interfac eNumber, uint8_t alternateSetting) |
| 323 { | 356 { |
| 324 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 357 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 325 ScriptPromise promise = resolver->promise(); | 358 ScriptPromise promise = resolver->promise(); |
| 326 if (!m_opened) | 359 if (ensureDeviceConfigured(resolver)) |
| 327 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 328 else | |
| 329 m_device->setInterface(interfaceNumber, alternateSetting, new CallbackPr omiseAdapter<void, USBError>(resolver)); | 360 m_device->setInterface(interfaceNumber, alternateSetting, new CallbackPr omiseAdapter<void, USBError>(resolver)); |
| 330 return promise; | 361 return promise; |
| 331 } | 362 } |
| 332 | 363 |
| 333 ScriptPromise USBDevice::controlTransferIn(ScriptState* scriptState, const USBCo ntrolTransferParameters& setup, unsigned length) | 364 ScriptPromise USBDevice::controlTransferIn(ScriptState* scriptState, const USBCo ntrolTransferParameters& setup, unsigned length) |
| 334 { | 365 { |
| 335 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 366 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 336 ScriptPromise promise = resolver->promise(); | 367 ScriptPromise promise = resolver->promise(); |
| 337 if (!m_opened) { | 368 if (ensureDeviceConfigured(resolver)) { |
| 338 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 369 WebUSBDevice::ControlTransferParameters parameters; |
| 339 return promise; | 370 DOMException* error = convertControlTransferParameters(WebUSBDevice::Tra nsferDirection::In, setup, ¶meters); |
| 371 if (error) | |
| 372 resolver->reject(error); | |
| 373 else | |
| 374 m_device->controlTransfer(parameters, nullptr, length, 0, new Callba ckPromiseAdapter<InputTransferResult, USBError>(resolver)); | |
| 340 } | 375 } |
| 341 | |
| 342 WebUSBDevice::ControlTransferParameters parameters; | |
| 343 DOMException* error = convertControlTransferParameters(WebUSBDevice::Transfe rDirection::In, setup, ¶meters); | |
| 344 if (error) { | |
| 345 resolver->reject(error); | |
| 346 return promise; | |
| 347 } | |
| 348 | |
| 349 m_device->controlTransfer(parameters, nullptr, length, 0, new CallbackPromis eAdapter<InputTransferResult, USBError>(resolver)); | |
| 350 return promise; | 376 return promise; |
| 351 } | 377 } |
| 352 | 378 |
| 353 ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC ontrolTransferParameters& setup) | 379 ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC ontrolTransferParameters& setup) |
| 354 { | 380 { |
| 355 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 381 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 356 ScriptPromise promise = resolver->promise(); | 382 ScriptPromise promise = resolver->promise(); |
| 357 if (!m_opened) { | 383 if (ensureDeviceConfigured(resolver)) { |
| 358 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 384 WebUSBDevice::ControlTransferParameters parameters; |
| 359 return promise; | 385 DOMException* error = convertControlTransferParameters(WebUSBDevice::Tra nsferDirection::Out, setup, ¶meters); |
| 386 if (error) | |
| 387 resolver->reject(error); | |
| 388 else | |
| 389 m_device->controlTransfer(parameters, nullptr, 0, 0, new CallbackPro miseAdapter<OutputTransferResult, USBError>(resolver)); | |
| 360 } | 390 } |
| 361 | |
| 362 WebUSBDevice::ControlTransferParameters parameters; | |
| 363 DOMException* error = convertControlTransferParameters(WebUSBDevice::Transfe rDirection::Out, setup, ¶meters); | |
| 364 if (error) { | |
| 365 resolver->reject(error); | |
| 366 return promise; | |
| 367 } | |
| 368 | |
| 369 m_device->controlTransfer(parameters, nullptr, 0, 0, new CallbackPromiseAdap ter<OutputTransferResult, USBError>(resolver)); | |
| 370 return promise; | 391 return promise; |
| 371 } | 392 } |
| 372 | 393 |
| 373 ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC ontrolTransferParameters& setup, const ArrayBufferOrArrayBufferView& data) | 394 ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC ontrolTransferParameters& setup, const ArrayBufferOrArrayBufferView& data) |
| 374 { | 395 { |
| 375 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 396 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 376 ScriptPromise promise = resolver->promise(); | 397 ScriptPromise promise = resolver->promise(); |
| 377 if (!m_opened) { | 398 if (ensureDeviceConfigured(resolver)) { |
| 378 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 399 WebUSBDevice::ControlTransferParameters parameters; |
| 379 return promise; | 400 DOMException* error = convertControlTransferParameters(WebUSBDevice::Tra nsferDirection::Out, setup, ¶meters); |
| 401 if (error) { | |
| 402 resolver->reject(error); | |
| 403 } else { | |
| 404 BufferSource buffer(data); | |
| 405 m_device->controlTransfer(parameters, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResult, USBError>(resolver)); | |
| 406 } | |
| 380 } | 407 } |
| 381 | |
| 382 WebUSBDevice::ControlTransferParameters parameters; | |
| 383 DOMException* error = convertControlTransferParameters(WebUSBDevice::Transfe rDirection::Out, setup, ¶meters); | |
| 384 if (error) { | |
| 385 resolver->reject(error); | |
| 386 return promise; | |
| 387 } | |
| 388 | |
| 389 BufferSource buffer(data); | |
| 390 m_device->controlTransfer(parameters, buffer.data(), buffer.size(), 0, new C allbackPromiseAdapter<OutputTransferResult, USBError>(resolver)); | |
| 391 return promise; | 408 return promise; |
| 392 } | 409 } |
| 393 | 410 |
| 394 ScriptPromise USBDevice::clearHalt(ScriptState* scriptState, uint8_t endpointNum ber) | 411 ScriptPromise USBDevice::clearHalt(ScriptState* scriptState, uint8_t endpointNum ber) |
| 395 { | 412 { |
| 396 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 413 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 397 ScriptPromise promise = resolver->promise(); | 414 ScriptPromise promise = resolver->promise(); |
| 398 if (!m_opened) | 415 if (ensureDeviceConfigured(resolver)) |
| 399 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 400 else | |
| 401 m_device->clearHalt(endpointNumber, new CallbackPromiseAdapter<void, USB Error>(resolver)); | 416 m_device->clearHalt(endpointNumber, new CallbackPromiseAdapter<void, USB Error>(resolver)); |
| 402 return promise; | 417 return promise; |
| 403 } | 418 } |
| 404 | 419 |
| 405 ScriptPromise USBDevice::transferIn(ScriptState* scriptState, uint8_t endpointNu mber, unsigned length) | 420 ScriptPromise USBDevice::transferIn(ScriptState* scriptState, uint8_t endpointNu mber, unsigned length) |
| 406 { | 421 { |
| 407 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 422 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 408 ScriptPromise promise = resolver->promise(); | 423 ScriptPromise promise = resolver->promise(); |
| 409 if (!m_opened) | 424 if (ensureDeviceConfigured(resolver)) |
| 410 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 411 else | |
| 412 m_device->transfer(WebUSBDevice::TransferDirection::In, endpointNumber, nullptr, length, 0, new CallbackPromiseAdapter<InputTransferResult, USBError>(re solver)); | 425 m_device->transfer(WebUSBDevice::TransferDirection::In, endpointNumber, nullptr, length, 0, new CallbackPromiseAdapter<InputTransferResult, USBError>(re solver)); |
| 413 return promise; | 426 return promise; |
| 414 } | 427 } |
| 415 | 428 |
| 416 ScriptPromise USBDevice::transferOut(ScriptState* scriptState, uint8_t endpointN umber, const ArrayBufferOrArrayBufferView& data) | 429 ScriptPromise USBDevice::transferOut(ScriptState* scriptState, uint8_t endpointN umber, const ArrayBufferOrArrayBufferView& data) |
| 417 { | 430 { |
| 418 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 431 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 419 ScriptPromise promise = resolver->promise(); | 432 ScriptPromise promise = resolver->promise(); |
| 420 if (!m_opened) { | 433 if (ensureDeviceConfigured(resolver)) { |
| 421 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 422 } else { | |
| 423 BufferSource buffer(data); | 434 BufferSource buffer(data); |
| 424 m_device->transfer(WebUSBDevice::TransferDirection::Out, endpointNumber, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResul t, USBError>(resolver)); | 435 m_device->transfer(WebUSBDevice::TransferDirection::Out, endpointNumber, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResul t, USBError>(resolver)); |
| 425 } | 436 } |
| 426 return promise; | 437 return promise; |
| 427 } | 438 } |
| 428 | 439 |
| 429 ScriptPromise USBDevice::isochronousTransferIn(ScriptState* scriptState, uint8_t endpointNumber, Vector<unsigned> packetLengths) | 440 ScriptPromise USBDevice::isochronousTransferIn(ScriptState* scriptState, uint8_t endpointNumber, Vector<unsigned> packetLengths) |
| 430 { | 441 { |
| 431 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 442 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 432 ScriptPromise promise = resolver->promise(); | 443 ScriptPromise promise = resolver->promise(); |
| 433 if (!m_opened) | 444 if (ensureDeviceConfigured(resolver)) |
| 434 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 435 else | |
| 436 m_device->isochronousTransfer(WebUSBDevice::TransferDirection::In, endpo intNumber, nullptr, 0, packetLengths, 0, new CallbackPromiseAdapter<IsochronousI nputTransferResult, USBError>(resolver)); | 445 m_device->isochronousTransfer(WebUSBDevice::TransferDirection::In, endpo intNumber, nullptr, 0, packetLengths, 0, new CallbackPromiseAdapter<IsochronousI nputTransferResult, USBError>(resolver)); |
| 437 return promise; | 446 return promise; |
| 438 } | 447 } |
| 439 | 448 |
| 440 ScriptPromise USBDevice::isochronousTransferOut(ScriptState* scriptState, uint8_ t endpointNumber, const ArrayBufferOrArrayBufferView& data, Vector<unsigned> pac ketLengths) | 449 ScriptPromise USBDevice::isochronousTransferOut(ScriptState* scriptState, uint8_ t endpointNumber, const ArrayBufferOrArrayBufferView& data, Vector<unsigned> pac ketLengths) |
| 441 { | 450 { |
| 442 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 451 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 443 ScriptPromise promise = resolver->promise(); | 452 ScriptPromise promise = resolver->promise(); |
| 444 if (!m_opened) { | 453 if (ensureDeviceConfigured(resolver)) { |
| 445 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 446 } else { | |
| 447 BufferSource buffer(data); | 454 BufferSource buffer(data); |
| 448 m_device->isochronousTransfer(WebUSBDevice::TransferDirection::Out, endp ointNumber, buffer.data(), buffer.size(), packetLengths, 0, new CallbackPromiseA dapter<IsochronousOutputTransferResult, USBError>(resolver)); | 455 m_device->isochronousTransfer(WebUSBDevice::TransferDirection::Out, endp ointNumber, buffer.data(), buffer.size(), packetLengths, 0, new CallbackPromiseA dapter<IsochronousOutputTransferResult, USBError>(resolver)); |
| 449 } | 456 } |
| 450 return promise; | 457 return promise; |
| 451 } | 458 } |
| 452 | 459 |
| 453 ScriptPromise USBDevice::reset(ScriptState* scriptState) | 460 ScriptPromise USBDevice::reset(ScriptState* scriptState) |
| 454 { | 461 { |
| 455 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 462 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 456 ScriptPromise promise = resolver->promise(); | 463 ScriptPromise promise = resolver->promise(); |
| 457 if (!m_opened) | 464 if (!m_opened) |
| 458 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 465 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 466 else if (m_deviceStateChangeInProgress) | |
| 467 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 459 else | 468 else |
| 460 m_device->reset(new CallbackPromiseAdapter<void, USBError>(resolver)); | 469 m_device->reset(new CallbackPromiseAdapter<void, USBError>(resolver)); |
| 461 return promise; | 470 return promise; |
| 462 } | 471 } |
| 463 | 472 |
| 464 void USBDevice::contextDestroyed() | 473 void USBDevice::contextDestroyed() |
| 465 { | 474 { |
| 466 if (m_opened) | 475 if (m_opened) |
| 467 m_device->close(new WebUSBDeviceCloseCallbacks()); | 476 m_device->close(new WebUSBDeviceCloseCallbacks()); |
| 468 } | 477 } |
| 469 | 478 |
| 470 DEFINE_TRACE(USBDevice) | 479 DEFINE_TRACE(USBDevice) |
| 471 { | 480 { |
| 472 ContextLifecycleObserver::trace(visitor); | 481 ContextLifecycleObserver::trace(visitor); |
| 473 } | 482 } |
| 474 | 483 |
| 484 int USBDevice::findConfigurationIndex(uint8_t configurationValue) const | |
| 485 { | |
| 486 const auto& configurations = info().configurations; | |
| 487 for (size_t i = 0; i < configurations.size(); ++i) { | |
| 488 if (configurations[i].configurationValue == configurationValue) | |
| 489 return i; | |
| 490 } | |
| 491 return -1; | |
| 492 } | |
| 493 | |
| 494 bool USBDevice::ensureDeviceConfigured(ScriptPromiseResolver* resolver) const | |
| 495 { | |
| 496 if (!m_opened) { | |
| 497 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 498 } else if (m_deviceStateChangeInProgress) { | |
| 499 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 500 } else if (m_configurationIndex == -1) { | |
| 501 resolver->reject(DOMException::create(InvalidStateError, "The device mus t have a configuration selected.")); | |
|
juncai
2016/03/14 16:47:45
nit: there was a const char kNotConfigured[] for t
Reilly Grant (use Gerrit)
2016/03/14 17:20:36
In this most recent patchset the string is now onl
juncai
2016/03/14 17:22:45
I see. Thanks!
| |
| 502 } else { | |
| 503 return true; | |
| 504 } | |
| 505 return false; | |
| 506 } | |
| 507 | |
| 475 } // namespace blink | 508 } // namespace blink |
| OLD | NEW |