Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 'use strict'; | 1 'use strict'; |
| 2 | 2 |
| 3 // This polyfill library implements the WebUSB Test API as specified here: | 3 // This polyfill library implements the WebUSB Test API as specified here: |
| 4 // https://wicg.github.io/webusb/test/ | 4 // https://wicg.github.io/webusb/test/ |
| 5 | 5 |
| 6 (() => { | 6 (() => { |
| 7 | 7 |
| 8 // The global mojo object contains the Mojo JS binding modules loaded during | 8 // The global mojo object contains the Mojo JS binding modules loaded during |
| 9 // initialization. | 9 // initialization. |
| 10 let mojo = null; | 10 let mojo = null; |
| 11 | 11 |
| 12 // These variables are logically members of the USBTest class but are defined | 12 // These variables are logically members of the USBTest class but are defined |
| 13 // here to hide them from being visible as fields of navigator.usb.test. | 13 // here to hide them from being visible as fields of navigator.usb.test. |
| 14 let g_initializePromise = null; | 14 let g_initializePromise = null; |
| 15 let g_chooserService = null; | 15 let g_chooserService = null; |
| 16 let g_deviceManager = null; | 16 let g_deviceManager = null; |
| 17 let g_closeListener = null; | |
| 18 let g_nextGuid = 0; | |
| 19 | 17 |
| 20 function fakeDeviceInitToDeviceInfo(guid, init) { | 18 function fakeDeviceInitToDeviceInfo(guid, init) { |
| 21 let deviceInfo = { | 19 let deviceInfo = { |
| 22 guid: guid + "", | 20 guid: guid + "", |
| 23 usb_version_major: init.usbVersionMajor, | 21 usb_version_major: init.usbVersionMajor, |
| 24 usb_version_minor: init.usbVersionMinor, | 22 usb_version_minor: init.usbVersionMinor, |
| 25 usb_version_subminor: init.usbVersionSubminor, | 23 usb_version_subminor: init.usbVersionSubminor, |
| 26 class_code: init.deviceClass, | 24 class_code: init.deviceClass, |
| 27 subclass_code: init.deviceSubclass, | 25 subclass_code: init.deviceSubclass, |
| 28 protocol_code: init.deviceProtocol, | 26 protocol_code: init.deviceProtocol, |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 }); | 340 }); |
| 343 return Promise.resolve({ results: devices }); | 341 return Promise.resolve({ results: devices }); |
| 344 } | 342 } |
| 345 | 343 |
| 346 getDevice(guid, request) { | 344 getDevice(guid, request) { |
| 347 let device = this.devicesByGuid_.get(guid); | 345 let device = this.devicesByGuid_.get(guid); |
| 348 if (device) { | 346 if (device) { |
| 349 let binding = new mojo.bindings.Binding( | 347 let binding = new mojo.bindings.Binding( |
| 350 mojo.device.Device, new FakeDevice(device.info), request); | 348 mojo.device.Device, new FakeDevice(device.info), request); |
| 351 binding.setConnectionErrorHandler(() => { | 349 binding.setConnectionErrorHandler(() => { |
| 352 if (g_closeListener) | 350 if (device.fakeDevice.onclose) |
| 353 g_closeListener(device.fakeDevice); | 351 device.fakeDevice.onclose(); |
| 354 }); | 352 }); |
| 355 device.bindingArray.push(binding); | 353 device.bindingArray.push(binding); |
| 356 } else { | 354 } else { |
| 357 request.close(); | 355 request.close(); |
| 358 } | 356 } |
| 359 } | 357 } |
| 360 | 358 |
| 361 setClient(client) { | 359 setClient(client) { |
| 362 this.client_ = client; | 360 this.client_ = client; |
| 363 } | 361 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 387 result: fakeDeviceInitToDeviceInfo(device.guid, device.info) | 385 result: fakeDeviceInitToDeviceInfo(device.guid, device.info) |
| 388 }); | 386 }); |
| 389 } else { | 387 } else { |
| 390 return Promise.resolve({ result: null }); | 388 return Promise.resolve({ result: null }); |
| 391 } | 389 } |
| 392 } | 390 } |
| 393 } | 391 } |
| 394 | 392 |
| 395 // Unlike FakeDevice this class is exported to callers of USBTest.addFakeDevice. | 393 // Unlike FakeDevice this class is exported to callers of USBTest.addFakeDevice. |
| 396 class FakeUSBDevice { | 394 class FakeUSBDevice { |
| 395 constructor() { | |
| 396 this.onclose = null; | |
|
mcasas
2017/04/21 02:02:35
I think JS initializes all onBla event listeners
Reilly Grant (use Gerrit)
2017/04/24 19:29:15
I need to write an actual polyfill EventTarget imp
| |
| 397 } | |
| 398 | |
| 397 disconnect() { | 399 disconnect() { |
| 398 setTimeout(() => g_deviceManager.removeDevice(this), 0); | 400 setTimeout(() => g_deviceManager.removeDevice(this), 0); |
| 399 } | 401 } |
| 400 } | 402 } |
| 401 | 403 |
| 402 class USBTest { | 404 class USBTest { |
| 403 constructor() {} | 405 constructor() {} |
| 404 | 406 |
| 405 initialize() { | 407 initialize() { |
| 406 if (!g_initializePromise) { | 408 if (!g_initializePromise) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 throw new Error('Call initialize() before addFakeDevice().'); | 480 throw new Error('Call initialize() before addFakeDevice().'); |
| 479 | 481 |
| 480 // |addDevice| and |removeDevice| are called in a setTimeout callback so | 482 // |addDevice| and |removeDevice| are called in a setTimeout callback so |
| 481 // that tests do not rely on the device being immediately available which | 483 // that tests do not rely on the device being immediately available which |
| 482 // may not be true for all implementations of this test API. | 484 // may not be true for all implementations of this test API. |
| 483 let fakeDevice = new FakeUSBDevice(); | 485 let fakeDevice = new FakeUSBDevice(); |
| 484 setTimeout(() => g_deviceManager.addDevice(fakeDevice, deviceInit), 0); | 486 setTimeout(() => g_deviceManager.addDevice(fakeDevice, deviceInit), 0); |
| 485 return fakeDevice; | 487 return fakeDevice; |
| 486 } | 488 } |
| 487 | 489 |
| 488 set ondeviceclose(func) { | |
| 489 g_closeListener = func; | |
| 490 } | |
| 491 | |
| 492 set chosenDevice(fakeDevice) { | 490 set chosenDevice(fakeDevice) { |
| 493 if (!g_chooserService) | 491 if (!g_chooserService) |
| 494 throw new Error('Call initialize() before setting chosenDevice.'); | 492 throw new Error('Call initialize() before setting chosenDevice.'); |
| 495 | 493 |
| 496 g_chooserService.setChosenDevice(fakeDevice); | 494 g_chooserService.setChosenDevice(fakeDevice); |
| 497 } | 495 } |
| 498 | 496 |
| 499 get lastFilters() { | 497 get lastFilters() { |
| 500 if (!g_chooserService) | 498 if (!g_chooserService) |
| 501 throw new Error('Call initialize() before getting lastFilters.'); | 499 throw new Error('Call initialize() before getting lastFilters.'); |
| 502 | 500 |
| 503 return g_chooserService.lastFilters_; | 501 return g_chooserService.lastFilters_; |
| 504 } | 502 } |
| 505 | 503 |
| 506 reset() { | 504 reset() { |
| 507 if (!g_deviceManager || !g_chooserService) | 505 if (!g_deviceManager || !g_chooserService) |
| 508 throw new Error('Call initialize() before reset().'); | 506 throw new Error('Call initialize() before reset().'); |
| 509 | 507 |
| 510 g_deviceManager.removeAllDevices(); | 508 g_deviceManager.removeAllDevices(); |
| 511 g_chooserService.setChosenDevice(null); | 509 g_chooserService.setChosenDevice(null); |
| 512 g_closeListener = null; | |
| 513 } | 510 } |
| 514 } | 511 } |
| 515 | 512 |
| 516 navigator.usb.test = new USBTest(); | 513 navigator.usb.test = new USBTest(); |
| 517 | 514 |
| 518 })(); | 515 })(); |
| OLD | NEW |