| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Javascript for usb_internals.html, served from chrome://usb-internals/. |
| 7 */ |
| 8 |
| 9 (function() { |
| 10 // Connection to the UsbInternalsPageHandler instance running in the browser |
| 11 // process. |
| 12 let pageHandler = null; |
| 13 |
| 14 function refreshDeviceList() { |
| 15 pageHandler.getTestDevices().then(function(response) { |
| 16 let tableBody = $('test-device-list'); |
| 17 tableBody.innerHTML = ''; |
| 18 for (let device of response.devices) { |
| 19 let row = document.createElement('tr'); |
| 20 let name = document.createElement('td'); |
| 21 let serialNumber = document.createElement('td'); |
| 22 let landingPage = document.createElement('td'); |
| 23 let allowedOrigin = document.createElement('td'); |
| 24 let remove = document.createElement('td'); |
| 25 let removeButton = document.createElement('button'); |
| 26 name.textContent = device.name; |
| 27 serialNumber.textContent = device.serial_number; |
| 28 landingPage.textContent = device.landing_page.url; |
| 29 allowedOrigin.textContent = device.allowed_origin.scheme + '://' + |
| 30 device.allowed_origin.host + ':' + device.allowed_origin.port; |
| 31 removeButton.addEventListener('click', function() { |
| 32 pageHandler.removeDeviceForTesting(device.guid) |
| 33 .then(refreshDeviceList); |
| 34 }); |
| 35 removeButton.textContent = 'Remove'; |
| 36 row.appendChild(name); |
| 37 row.appendChild(serialNumber); |
| 38 row.appendChild(landingPage); |
| 39 row.appendChild(allowedOrigin); |
| 40 remove.appendChild(removeButton); |
| 41 row.appendChild(remove); |
| 42 tableBody.appendChild(row); |
| 43 } |
| 44 }); |
| 45 } |
| 46 |
| 47 function addTestDevice(event) { |
| 48 pageHandler.addDeviceForTesting( |
| 49 $('test-device-name').value, |
| 50 $('test-device-serial').value, |
| 51 $('test-device-landing-page').value, |
| 52 $('test-device-allowed-origin').value).then(function(response) { |
| 53 if (response.success) |
| 54 refreshDeviceList(); |
| 55 $('add-test-device-result').textContent = response.message; |
| 56 $('add-test-device-result').className = |
| 57 response.success ? 'action-success' : 'action-failure'; |
| 58 }); |
| 59 event.preventDefault(); |
| 60 } |
| 61 |
| 62 /** |
| 63 * Helper to convert callback-based define() API to a promise-based API. |
| 64 * @param {!Array<string>} moduleNames |
| 65 * @return {!Promise} |
| 66 */ |
| 67 function importModules(moduleNames) { |
| 68 return new Promise(function(resolve, reject) { |
| 69 define(moduleNames, function(var_args) { |
| 70 resolve(Array.prototype.slice.call(arguments, 0)); |
| 71 }); |
| 72 }); |
| 73 } |
| 74 |
| 75 function initializeProxies() { |
| 76 return importModules([ |
| 77 'mojo/public/js/connection', |
| 78 'chrome/browser/ui/webui/usb_internals/usb_internals.mojom', |
| 79 'content/public/renderer/frame_interfaces', |
| 80 ]).then(function(modules) { |
| 81 let connection = modules[0]; |
| 82 let mojom = modules[1]; |
| 83 let frameInterfaces = modules[2]; |
| 84 |
| 85 pageHandler = connection.bindHandleToProxy( |
| 86 frameInterfaces.getInterface(mojom.UsbInternalsPageHandler.name), |
| 87 mojom.UsbInternalsPageHandler); |
| 88 }); |
| 89 } |
| 90 |
| 91 document.addEventListener('DOMContentLoaded', function() { |
| 92 initializeProxies().then(function() { |
| 93 $('add-test-device-form').addEventListener('submit', addTestDevice); |
| 94 refreshDeviceList(); |
| 95 }); |
| 96 }); |
| 97 })(); |
| OLD | NEW |