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 const char kNotConfigured[] = "The device must have a configuration selected."; | |
| 31 | 32 |
| 32 DOMException* convertControlTransferParameters( | 33 DOMException* convertControlTransferParameters( |
| 33 WebUSBDevice::TransferDirection direction, | 34 WebUSBDevice::TransferDirection direction, |
| 34 const USBControlTransferParameters& parameters, | 35 const USBControlTransferParameters& parameters, |
| 35 WebUSBDevice::ControlTransferParameters* webParameters) | 36 WebUSBDevice::ControlTransferParameters* webParameters) |
| 36 { | 37 { |
| 37 webParameters->direction = direction; | 38 webParameters->direction = direction; |
| 38 | 39 |
| 39 if (parameters.requestType() == "standard") | 40 if (parameters.requestType() == "standard") |
| 40 webParameters->type = WebUSBDevice::RequestType::Standard; | 41 webParameters->type = WebUSBDevice::RequestType::Standard; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 m_device->onDeviceOpenedOrClosed(!m_desiredState); | 104 m_device->onDeviceOpenedOrClosed(!m_desiredState); |
| 104 m_resolver->reject(USBError::take(m_resolver, e)); | 105 m_resolver->reject(USBError::take(m_resolver, e)); |
| 105 } | 106 } |
| 106 | 107 |
| 107 private: | 108 private: |
| 108 WeakPersistent<USBDevice> m_device; | 109 WeakPersistent<USBDevice> m_device; |
| 109 Persistent<ScriptPromiseResolver> m_resolver; | 110 Persistent<ScriptPromiseResolver> m_resolver; |
| 110 bool m_desiredState; // true: open, false: closed | 111 bool m_desiredState; // true: open, false: closed |
| 111 }; | 112 }; |
| 112 | 113 |
| 113 class GetConfigurationPromiseAdapter : public WebCallbacks<uint8_t, const WebUSB Error&> { | 114 class SelectConfigurationPromiseAdapter : public WebCallbacks<void, const WebUSB Error&> { |
| 114 public: | 115 public: |
| 115 GetConfigurationPromiseAdapter(USBDevice* device, ScriptPromiseResolver* res olver) : m_device(device), m_resolver(resolver) {} | 116 SelectConfigurationPromiseAdapter(USBDevice* device, ScriptPromiseResolver* resolver, uint8_t configurationValue) |
| 117 : m_device(device) | |
| 118 , m_resolver(resolver) | |
| 119 , m_configurationValue(configurationValue) | |
| 120 { | |
| 121 } | |
| 116 | 122 |
| 117 void onSuccess(uint8_t value) override | 123 void onSuccess() override |
| 118 { | 124 { |
| 119 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex t()->activeDOMObjectsAreStopped()) | 125 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex t()->activeDOMObjectsAreStopped()) |
| 120 return; | 126 return; |
| 121 m_resolver->resolve(USBConfiguration::createFromValue(m_device, value)); | 127 if (m_device) |
| 128 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.
| |
| 129 m_resolver->resolve(); | |
| 122 } | 130 } |
| 123 | 131 |
| 124 void onError(const WebUSBError& e) override | 132 void onError(const WebUSBError& e) override |
| 125 { | 133 { |
| 126 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex t()->activeDOMObjectsAreStopped()) | 134 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex t()->activeDOMObjectsAreStopped()) |
| 127 return; | 135 return; |
| 136 if (m_device) | |
| 137 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.
| |
| 128 m_resolver->reject(USBError::take(m_resolver, e)); | 138 m_resolver->reject(USBError::take(m_resolver, e)); |
| 129 } | 139 } |
| 130 | 140 |
| 131 private: | 141 private: |
| 132 Persistent<USBDevice> m_device; | 142 WeakPersistent<USBDevice> m_device; |
| 133 Persistent<ScriptPromiseResolver> m_resolver; | 143 Persistent<ScriptPromiseResolver> m_resolver; |
| 144 uint8_t m_configurationValue; | |
| 134 }; | 145 }; |
| 135 | 146 |
| 136 class InputTransferResult { | 147 class InputTransferResult { |
| 137 WTF_MAKE_NONCOPYABLE(InputTransferResult); | 148 WTF_MAKE_NONCOPYABLE(InputTransferResult); |
| 138 public: | 149 public: |
| 139 using WebType = OwnPtr<WebUSBTransferInfo>; | 150 using WebType = OwnPtr<WebUSBTransferInfo>; |
| 140 | 151 |
| 141 static USBInTransferResult* take(ScriptPromiseResolver*, PassOwnPtr<WebUSBTr ansferInfo> webTransferInfo) | 152 static USBInTransferResult* take(ScriptPromiseResolver*, PassOwnPtr<WebUSBTr ansferInfo> webTransferInfo) |
| 142 { | 153 { |
| 143 ASSERT(webTransferInfo->status.size() == 1); | 154 ASSERT(webTransferInfo->status.size() == 1); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 { | 244 { |
| 234 return USBDevice::create(device, resolver->getExecutionContext()); | 245 return USBDevice::create(device, resolver->getExecutionContext()); |
| 235 } | 246 } |
| 236 | 247 |
| 237 void USBDevice::onDeviceOpenedOrClosed(bool opened) | 248 void USBDevice::onDeviceOpenedOrClosed(bool opened) |
| 238 { | 249 { |
| 239 m_opened = opened; | 250 m_opened = opened; |
| 240 m_deviceStateChangeInProgress = false; | 251 m_deviceStateChangeInProgress = false; |
| 241 } | 252 } |
| 242 | 253 |
| 254 void USBDevice::onConfigurationSelected(bool success, uint8_t configurationValue ) | |
| 255 { | |
| 256 if (success) | |
| 257 m_activeConfiguration = configurationValue; | |
| 258 m_deviceStateChangeInProgress = false; | |
| 259 } | |
| 260 | |
| 261 USBConfiguration* USBDevice::configuration() const | |
| 262 { | |
| 263 if (m_activeConfiguration != 0) { | |
| 264 for (size_t i = 0; i < info().configurations.size(); ++i) { | |
| 265 if (info().configurations[i].configurationValue == m_activeConfigura tion) | |
| 266 return USBConfiguration::create(this, i); | |
| 267 } | |
| 268 } | |
| 269 return nullptr; | |
| 270 } | |
| 271 | |
| 243 HeapVector<Member<USBConfiguration>> USBDevice::configurations() const | 272 HeapVector<Member<USBConfiguration>> USBDevice::configurations() const |
| 244 { | 273 { |
| 245 HeapVector<Member<USBConfiguration>> configurations; | 274 HeapVector<Member<USBConfiguration>> configurations; |
| 246 for (size_t i = 0; i < info().configurations.size(); ++i) | 275 for (size_t i = 0; i < info().configurations.size(); ++i) |
| 247 configurations.append(USBConfiguration::create(this, i)); | 276 configurations.append(USBConfiguration::create(this, i)); |
| 248 return configurations; | 277 return configurations; |
| 249 } | 278 } |
| 250 | 279 |
| 251 ScriptPromise USBDevice::open(ScriptState* scriptState) | 280 ScriptPromise USBDevice::open(ScriptState* scriptState) |
| 252 { | 281 { |
| 253 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 282 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 254 ScriptPromise promise = resolver->promise(); | 283 ScriptPromise promise = resolver->promise(); |
| 255 if (m_deviceStateChangeInProgress) { | 284 if (m_deviceStateChangeInProgress) { |
| 256 resolver->reject(DOMException::create(InvalidStateError, kOpenCloseInPro gress)); | 285 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); |
| 257 } else if (m_opened) { | 286 } else if (m_opened) { |
| 258 resolver->resolve(); | 287 resolver->resolve(); |
| 259 } else { | 288 } else { |
| 260 m_deviceStateChangeInProgress = true; | 289 m_deviceStateChangeInProgress = true; |
| 261 m_device->open(new OpenClosePromiseAdapter(this, resolver, true /* open */)); | 290 m_device->open(new OpenClosePromiseAdapter(this, resolver, true /* open */)); |
| 262 } | 291 } |
| 263 return promise; | 292 return promise; |
| 264 } | 293 } |
| 265 | 294 |
| 266 ScriptPromise USBDevice::close(ScriptState* scriptState) | 295 ScriptPromise USBDevice::close(ScriptState* scriptState) |
| 267 { | 296 { |
| 268 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 297 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 269 ScriptPromise promise = resolver->promise(); | 298 ScriptPromise promise = resolver->promise(); |
| 270 if (m_deviceStateChangeInProgress) { | 299 if (m_deviceStateChangeInProgress) { |
| 271 resolver->reject(DOMException::create(InvalidStateError, kOpenCloseInPro gress)); | 300 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); |
| 272 } else if (!m_opened) { | 301 } else if (!m_opened) { |
| 273 resolver->resolve(); | 302 resolver->resolve(); |
| 274 } else { | 303 } else { |
| 275 m_deviceStateChangeInProgress = true; | 304 m_deviceStateChangeInProgress = true; |
| 276 m_device->close(new OpenClosePromiseAdapter(this, resolver, false /* clo sed */)); | 305 m_device->close(new OpenClosePromiseAdapter(this, resolver, false /* clo sed */)); |
| 277 } | 306 } |
| 278 return promise; | 307 return promise; |
| 279 } | 308 } |
| 280 | 309 |
| 281 ScriptPromise USBDevice::getConfiguration(ScriptState* scriptState) | 310 ScriptPromise USBDevice::selectConfiguration(ScriptState* scriptState, uint8_t c onfigurationValue) |
| 282 { | 311 { |
| 283 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 312 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 284 ScriptPromise promise = resolver->promise(); | 313 ScriptPromise promise = resolver->promise(); |
| 285 m_device->getConfiguration(new GetConfigurationPromiseAdapter(this, resolver )); | 314 bool foundConfiguration = false; |
| 315 for (size_t i = 0; i < info().configurations.size(); ++i) { | |
| 316 if (info().configurations[i].configurationValue == configurationValue) { | |
| 317 foundConfiguration = true; | |
| 318 break; | |
| 319 } | |
| 320 } | |
| 321 if (!m_opened) { | |
| 322 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | |
| 323 } else if (m_deviceStateChangeInProgress) { | |
| 324 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
|
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.
| |
| 325 } else if (!foundConfiguration) { | |
| 326 resolver->reject(DOMException::create(NotFoundError, "The configuration value provided is not supported by the device.")); | |
| 327 } else if (m_activeConfiguration == configurationValue) { | |
| 328 resolver->resolve(); | |
| 329 } else { | |
| 330 m_deviceStateChangeInProgress = true; | |
| 331 m_device->setConfiguration(configurationValue, new SelectConfigurationPr omiseAdapter(this, resolver, configurationValue)); | |
| 332 } | |
| 286 return promise; | 333 return promise; |
| 287 } | 334 } |
| 288 | 335 |
| 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) | 336 ScriptPromise USBDevice::claimInterface(ScriptState* scriptState, uint8_t interf aceNumber) |
| 301 { | 337 { |
| 302 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 338 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 303 ScriptPromise promise = resolver->promise(); | 339 ScriptPromise promise = resolver->promise(); |
| 304 if (!m_opened) | 340 if (!m_opened) |
| 305 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 341 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 342 else if (m_deviceStateChangeInProgress) | |
| 343 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 344 else if (m_activeConfiguration == 0) | |
| 345 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 306 else | 346 else |
| 307 m_device->claimInterface(interfaceNumber, new CallbackPromiseAdapter<voi d, USBError>(resolver)); | 347 m_device->claimInterface(interfaceNumber, new CallbackPromiseAdapter<voi d, USBError>(resolver)); |
| 308 return promise; | 348 return promise; |
| 309 } | 349 } |
| 310 | 350 |
| 311 ScriptPromise USBDevice::releaseInterface(ScriptState* scriptState, uint8_t inte rfaceNumber) | 351 ScriptPromise USBDevice::releaseInterface(ScriptState* scriptState, uint8_t inte rfaceNumber) |
| 312 { | 352 { |
| 313 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 353 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 314 ScriptPromise promise = resolver->promise(); | 354 ScriptPromise promise = resolver->promise(); |
| 315 if (!m_opened) | 355 if (!m_opened) |
| 316 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 356 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 357 else if (m_deviceStateChangeInProgress) | |
| 358 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 359 else if (m_activeConfiguration == 0) | |
| 360 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 317 else | 361 else |
| 318 m_device->releaseInterface(interfaceNumber, new CallbackPromiseAdapter<v oid, USBError>(resolver)); | 362 m_device->releaseInterface(interfaceNumber, new CallbackPromiseAdapter<v oid, USBError>(resolver)); |
| 319 return promise; | 363 return promise; |
| 320 } | 364 } |
| 321 | 365 |
| 322 ScriptPromise USBDevice::setInterface(ScriptState* scriptState, uint8_t interfac eNumber, uint8_t alternateSetting) | 366 ScriptPromise USBDevice::setInterface(ScriptState* scriptState, uint8_t interfac eNumber, uint8_t alternateSetting) |
| 323 { | 367 { |
| 324 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 368 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 325 ScriptPromise promise = resolver->promise(); | 369 ScriptPromise promise = resolver->promise(); |
| 326 if (!m_opened) | 370 if (!m_opened) |
| 327 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 371 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 372 else if (m_deviceStateChangeInProgress) | |
| 373 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 374 else if (m_activeConfiguration == 0) | |
| 375 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 328 else | 376 else |
| 329 m_device->setInterface(interfaceNumber, alternateSetting, new CallbackPr omiseAdapter<void, USBError>(resolver)); | 377 m_device->setInterface(interfaceNumber, alternateSetting, new CallbackPr omiseAdapter<void, USBError>(resolver)); |
| 330 return promise; | 378 return promise; |
| 331 } | 379 } |
| 332 | 380 |
| 333 ScriptPromise USBDevice::controlTransferIn(ScriptState* scriptState, const USBCo ntrolTransferParameters& setup, unsigned length) | 381 ScriptPromise USBDevice::controlTransferIn(ScriptState* scriptState, const USBCo ntrolTransferParameters& setup, unsigned length) |
| 334 { | 382 { |
| 335 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 383 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 336 ScriptPromise promise = resolver->promise(); | 384 ScriptPromise promise = resolver->promise(); |
| 337 if (!m_opened) { | 385 if (!m_opened) { |
| 338 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 386 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 339 return promise; | 387 } else if (m_deviceStateChangeInProgress) { |
| 388 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 389 } else if (m_activeConfiguration == 0) { | |
| 390 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 391 } else { | |
| 392 WebUSBDevice::ControlTransferParameters parameters; | |
| 393 DOMException* error = convertControlTransferParameters(WebUSBDevice::Tra nsferDirection::In, setup, ¶meters); | |
| 394 if (error) | |
| 395 resolver->reject(error); | |
| 396 else | |
| 397 m_device->controlTransfer(parameters, nullptr, length, 0, new Callba ckPromiseAdapter<InputTransferResult, USBError>(resolver)); | |
| 340 } | 398 } |
| 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; | 399 return promise; |
| 351 } | 400 } |
| 352 | 401 |
| 353 ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC ontrolTransferParameters& setup) | 402 ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC ontrolTransferParameters& setup) |
| 354 { | 403 { |
| 355 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 404 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 356 ScriptPromise promise = resolver->promise(); | 405 ScriptPromise promise = resolver->promise(); |
| 357 if (!m_opened) { | 406 if (!m_opened) { |
| 358 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 407 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 359 return promise; | 408 } else if (m_deviceStateChangeInProgress) { |
| 409 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 410 } else if (m_activeConfiguration == 0) { | |
| 411 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 412 } else { | |
| 413 WebUSBDevice::ControlTransferParameters parameters; | |
| 414 DOMException* error = convertControlTransferParameters(WebUSBDevice::Tra nsferDirection::Out, setup, ¶meters); | |
| 415 if (error) | |
| 416 resolver->reject(error); | |
| 417 else | |
| 418 m_device->controlTransfer(parameters, nullptr, 0, 0, new CallbackPro miseAdapter<OutputTransferResult, USBError>(resolver)); | |
| 360 } | 419 } |
| 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; | 420 return promise; |
| 371 } | 421 } |
| 372 | 422 |
| 373 ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC ontrolTransferParameters& setup, const ArrayBufferOrArrayBufferView& data) | 423 ScriptPromise USBDevice::controlTransferOut(ScriptState* scriptState, const USBC ontrolTransferParameters& setup, const ArrayBufferOrArrayBufferView& data) |
| 374 { | 424 { |
| 375 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 425 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 376 ScriptPromise promise = resolver->promise(); | 426 ScriptPromise promise = resolver->promise(); |
| 377 if (!m_opened) { | 427 if (!m_opened) { |
| 378 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 428 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 379 return promise; | 429 } else if (m_deviceStateChangeInProgress) { |
| 430 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 431 } else if (m_activeConfiguration == 0) { | |
| 432 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 433 } else { | |
| 434 WebUSBDevice::ControlTransferParameters parameters; | |
| 435 DOMException* error = convertControlTransferParameters(WebUSBDevice::Tra nsferDirection::Out, setup, ¶meters); | |
| 436 if (error) { | |
| 437 resolver->reject(error); | |
| 438 } else { | |
| 439 BufferSource buffer(data); | |
| 440 m_device->controlTransfer(parameters, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResult, USBError>(resolver)); | |
| 441 } | |
| 380 } | 442 } |
| 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; | 443 return promise; |
| 392 } | 444 } |
| 393 | 445 |
| 394 ScriptPromise USBDevice::clearHalt(ScriptState* scriptState, uint8_t endpointNum ber) | 446 ScriptPromise USBDevice::clearHalt(ScriptState* scriptState, uint8_t endpointNum ber) |
| 395 { | 447 { |
| 396 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 448 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 397 ScriptPromise promise = resolver->promise(); | 449 ScriptPromise promise = resolver->promise(); |
| 398 if (!m_opened) | 450 if (!m_opened) |
| 399 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 451 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 452 else if (m_deviceStateChangeInProgress) | |
| 453 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 454 else if (m_activeConfiguration == 0) | |
| 455 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 400 else | 456 else |
| 401 m_device->clearHalt(endpointNumber, new CallbackPromiseAdapter<void, USB Error>(resolver)); | 457 m_device->clearHalt(endpointNumber, new CallbackPromiseAdapter<void, USB Error>(resolver)); |
| 402 return promise; | 458 return promise; |
| 403 } | 459 } |
| 404 | 460 |
| 405 ScriptPromise USBDevice::transferIn(ScriptState* scriptState, uint8_t endpointNu mber, unsigned length) | 461 ScriptPromise USBDevice::transferIn(ScriptState* scriptState, uint8_t endpointNu mber, unsigned length) |
| 406 { | 462 { |
| 407 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 463 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 408 ScriptPromise promise = resolver->promise(); | 464 ScriptPromise promise = resolver->promise(); |
| 409 if (!m_opened) | 465 if (!m_opened) |
| 410 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 466 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 467 else if (m_deviceStateChangeInProgress) | |
| 468 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 469 else if (m_activeConfiguration == 0) | |
| 470 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 411 else | 471 else |
| 412 m_device->transfer(WebUSBDevice::TransferDirection::In, endpointNumber, nullptr, length, 0, new CallbackPromiseAdapter<InputTransferResult, USBError>(re solver)); | 472 m_device->transfer(WebUSBDevice::TransferDirection::In, endpointNumber, nullptr, length, 0, new CallbackPromiseAdapter<InputTransferResult, USBError>(re solver)); |
| 413 return promise; | 473 return promise; |
| 414 } | 474 } |
| 415 | 475 |
| 416 ScriptPromise USBDevice::transferOut(ScriptState* scriptState, uint8_t endpointN umber, const ArrayBufferOrArrayBufferView& data) | 476 ScriptPromise USBDevice::transferOut(ScriptState* scriptState, uint8_t endpointN umber, const ArrayBufferOrArrayBufferView& data) |
| 417 { | 477 { |
| 418 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 478 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 419 ScriptPromise promise = resolver->promise(); | 479 ScriptPromise promise = resolver->promise(); |
| 420 if (!m_opened) { | 480 if (!m_opened) { |
| 421 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 481 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 482 } else if (m_deviceStateChangeInProgress) { | |
| 483 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 484 } else if (m_activeConfiguration == 0) { | |
| 485 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 422 } else { | 486 } else { |
| 423 BufferSource buffer(data); | 487 BufferSource buffer(data); |
| 424 m_device->transfer(WebUSBDevice::TransferDirection::Out, endpointNumber, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResul t, USBError>(resolver)); | 488 m_device->transfer(WebUSBDevice::TransferDirection::Out, endpointNumber, buffer.data(), buffer.size(), 0, new CallbackPromiseAdapter<OutputTransferResul t, USBError>(resolver)); |
| 425 } | 489 } |
| 426 return promise; | 490 return promise; |
| 427 } | 491 } |
| 428 | 492 |
| 429 ScriptPromise USBDevice::isochronousTransferIn(ScriptState* scriptState, uint8_t endpointNumber, Vector<unsigned> packetLengths) | 493 ScriptPromise USBDevice::isochronousTransferIn(ScriptState* scriptState, uint8_t endpointNumber, Vector<unsigned> packetLengths) |
| 430 { | 494 { |
| 431 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 495 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 432 ScriptPromise promise = resolver->promise(); | 496 ScriptPromise promise = resolver->promise(); |
| 433 if (!m_opened) | 497 if (!m_opened) |
| 434 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 498 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 499 else if (m_deviceStateChangeInProgress) | |
| 500 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 501 else if (m_activeConfiguration == 0) | |
| 502 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 435 else | 503 else |
| 436 m_device->isochronousTransfer(WebUSBDevice::TransferDirection::In, endpo intNumber, nullptr, 0, packetLengths, 0, new CallbackPromiseAdapter<IsochronousI nputTransferResult, USBError>(resolver)); | 504 m_device->isochronousTransfer(WebUSBDevice::TransferDirection::In, endpo intNumber, nullptr, 0, packetLengths, 0, new CallbackPromiseAdapter<IsochronousI nputTransferResult, USBError>(resolver)); |
| 437 return promise; | 505 return promise; |
| 438 } | 506 } |
| 439 | 507 |
| 440 ScriptPromise USBDevice::isochronousTransferOut(ScriptState* scriptState, uint8_ t endpointNumber, const ArrayBufferOrArrayBufferView& data, Vector<unsigned> pac ketLengths) | 508 ScriptPromise USBDevice::isochronousTransferOut(ScriptState* scriptState, uint8_ t endpointNumber, const ArrayBufferOrArrayBufferView& data, Vector<unsigned> pac ketLengths) |
| 441 { | 509 { |
| 442 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 510 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 443 ScriptPromise promise = resolver->promise(); | 511 ScriptPromise promise = resolver->promise(); |
| 444 if (!m_opened) { | 512 if (!m_opened) { |
| 445 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 513 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 514 } else if (m_deviceStateChangeInProgress) { | |
| 515 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 516 } else if (m_activeConfiguration == 0) { | |
| 517 resolver->reject(DOMException::create(InvalidStateError, kNotConfigured) ); | |
| 446 } else { | 518 } else { |
| 447 BufferSource buffer(data); | 519 BufferSource buffer(data); |
| 448 m_device->isochronousTransfer(WebUSBDevice::TransferDirection::Out, endp ointNumber, buffer.data(), buffer.size(), packetLengths, 0, new CallbackPromiseA dapter<IsochronousOutputTransferResult, USBError>(resolver)); | 520 m_device->isochronousTransfer(WebUSBDevice::TransferDirection::Out, endp ointNumber, buffer.data(), buffer.size(), packetLengths, 0, new CallbackPromiseA dapter<IsochronousOutputTransferResult, USBError>(resolver)); |
| 449 } | 521 } |
| 450 return promise; | 522 return promise; |
| 451 } | 523 } |
| 452 | 524 |
| 453 ScriptPromise USBDevice::reset(ScriptState* scriptState) | 525 ScriptPromise USBDevice::reset(ScriptState* scriptState) |
| 454 { | 526 { |
| 455 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 527 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 456 ScriptPromise promise = resolver->promise(); | 528 ScriptPromise promise = resolver->promise(); |
| 457 if (!m_opened) | 529 if (!m_opened) |
| 458 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; | 530 resolver->reject(DOMException::create(InvalidStateError, kOpenRequired)) ; |
| 531 else if (m_deviceStateChangeInProgress) | |
| 532 resolver->reject(DOMException::create(InvalidStateError, kDeviceStateCha ngeInProgress)); | |
| 459 else | 533 else |
| 460 m_device->reset(new CallbackPromiseAdapter<void, USBError>(resolver)); | 534 m_device->reset(new CallbackPromiseAdapter<void, USBError>(resolver)); |
| 461 return promise; | 535 return promise; |
| 462 } | 536 } |
| 463 | 537 |
| 464 void USBDevice::contextDestroyed() | 538 void USBDevice::contextDestroyed() |
| 465 { | 539 { |
| 466 if (m_opened) | 540 if (m_opened) |
| 467 m_device->close(new WebUSBDeviceCloseCallbacks()); | 541 m_device->close(new WebUSBDeviceCloseCallbacks()); |
| 468 } | 542 } |
| 469 | 543 |
| 470 DEFINE_TRACE(USBDevice) | 544 DEFINE_TRACE(USBDevice) |
| 471 { | 545 { |
| 472 ContextLifecycleObserver::trace(visitor); | 546 ContextLifecycleObserver::trace(visitor); |
| 473 } | 547 } |
| 474 | 548 |
| 475 } // namespace blink | 549 } // namespace blink |
| OLD | NEW |