Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 /** | 5 /** |
| 6 * Javascript for bluetooth_internals.html, served from | 6 * Javascript for bluetooth_internals.html, served from |
| 7 * chrome://bluetooth-internals/. | 7 * chrome://bluetooth-internals/. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 (function() { | 10 cr.define('bluetooth_internals', function() { |
|
scheib
2016/10/31 20:24:36
pardon my ignorance, but isn't part of the point o
mbrunson
2016/10/31 21:46:19
There's three kinds of defines that I've seen used
| |
| 11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; | |
| 12 | |
| 13 var REMOVED_CSS = 'removed'; | |
| 14 | |
| 15 /* | |
| 16 * Data model for a cached device. | |
| 17 * @constructor | |
| 18 * @param {!bluetoothDevice.DeviceInfo} info | |
| 19 */ | |
| 20 var Device = function(info) { this.info = info; }; | |
| 21 | |
| 22 /** | 11 /** |
| 23 * The implementation of AdapterClient in | 12 * The implementation of AdapterClient in |
| 24 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the | 13 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the |
| 25 * client-side collection of devices. | 14 * client-side collection of devices. |
| 26 * @constructor | 15 * @constructor |
| 27 */ | 16 */ |
| 28 var AdapterClient = function() { this.devices_ = new Map(); }; | 17 var AdapterClient = function() { |
| 18 this.devices = new device_collection.DeviceCollection([]); | |
| 19 }; | |
| 29 AdapterClient.prototype = { | 20 AdapterClient.prototype = { |
| 30 /** | 21 /** |
| 31 * Caches the device info and updates the device list. | 22 * Adds a device to the device collection. |
| 32 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 23 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 33 */ | 24 */ |
| 34 deviceAdded: function(deviceInfo) { | 25 deviceAdded: function(deviceInfo) { |
| 35 if (this.devices_.has(deviceInfo.address)) { | 26 this.devices.addOrUpdate(deviceInfo); |
| 36 var deviceElement = $(deviceInfo.address); | |
| 37 deviceElement.classList.remove(REMOVED_CSS); | |
| 38 } else { | |
| 39 this.devices_.set(deviceInfo.address, new Device(deviceInfo)); | |
| 40 | |
| 41 var deviceRowTemplate = $('device-row-template'); | |
| 42 var deviceRow = document.importNode( | |
| 43 deviceRowTemplate.content.children[0], true /* deep */); | |
| 44 deviceRow.id = deviceInfo.address; | |
| 45 | |
| 46 var deviceList = $('device-list'); | |
| 47 deviceList.appendChild(deviceRow); | |
| 48 } | |
| 49 | |
| 50 this.deviceChanged(deviceInfo); | |
| 51 }, | 27 }, |
| 52 | 28 |
| 53 /** | 29 /** |
| 54 * Marks device as removed. | 30 * Marks device as removed. |
| 55 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 31 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 56 */ | 32 */ |
| 57 deviceRemoved: function(deviceInfo) { | 33 deviceRemoved: function(deviceInfo) { |
| 58 $(deviceInfo.address).classList.add(REMOVED_CSS); | 34 this.devices.remove(deviceInfo); |
| 59 }, | 35 }, |
| 60 | 36 |
| 61 /** | 37 /** |
| 62 * Updates cached device and updates the device list. | 38 * Updates the cached device. |
| 63 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 39 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 64 */ | 40 */ |
| 65 deviceChanged: function(deviceInfo) { | 41 deviceChanged: function(deviceInfo) { |
| 66 console.log(new Date(), deviceInfo); | 42 console.log(new Date(), deviceInfo); |
| 67 | 43 this.devices.addOrUpdate(deviceInfo); |
| 68 assert(this.devices_.has(deviceInfo.address), 'Device does not exist.'); | |
| 69 | |
| 70 this.devices_.get(deviceInfo.address).info = deviceInfo; | |
| 71 | |
| 72 var deviceRow = $(deviceInfo.address); | |
| 73 deviceRow.querySelector('.device-name').textContent = | |
| 74 deviceInfo.name_for_display; | |
| 75 deviceRow.querySelector('.device-address').textContent = | |
| 76 deviceInfo.address; | |
| 77 | |
| 78 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || | |
| 79 deviceRow.querySelector('.device-rssi').textContent; | |
| 80 deviceRow.querySelector('.device-rssi').textContent = rssi; | |
| 81 } | 44 } |
| 82 }; | 45 }; |
| 83 | 46 |
| 84 /** | 47 var adapterClient = null; |
| 85 * TODO(crbug.com/652361): Move to shared location. | |
| 86 * Helper to convert callback-based define() API to a promise-based API. | |
| 87 * @param {!Array<string>} moduleNames | |
| 88 * @return {!Promise} | |
| 89 */ | |
| 90 function importModules(moduleNames) { | |
| 91 return new Promise(function(resolve, reject) { | |
| 92 define(moduleNames, function(var_args) { | |
| 93 resolve(Array.prototype.slice.call(arguments, 0)); | |
| 94 }); | |
| 95 }); | |
| 96 } | |
| 97 | 48 |
| 98 /** | 49 function initialize() { |
| 99 * Initializes Mojo proxies for page and Bluetooth services. | 50 interfaces.initialize() |
| 100 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth | 51 .then(function() { |
| 101 * is not supported. | 52 // Hook up the instance properties. |
| 102 */ | 53 AdapterClient.prototype.__proto__ = |
| 103 function initializeProxies() { | 54 interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype; |
| 104 return importModules([ | |
| 105 'content/public/renderer/frame_interfaces', | |
| 106 'device/bluetooth/public/interfaces/adapter.mojom', | |
| 107 'device/bluetooth/public/interfaces/device.mojom', | |
| 108 'mojo/public/js/connection', | |
| 109 ]).then(function([frameInterfaces, ...modules]) { | |
| 110 // Destructure here to assign global variables. | |
| 111 [bluetoothAdapter, bluetoothDevice, connection] = modules; | |
| 112 | 55 |
| 113 // Hook up the instance properties. | 56 adapterClient = new AdapterClient(); |
| 114 AdapterClient.prototype.__proto__ = | 57 interfaces.DefaultAdapter.setClient( |
| 115 bluetoothAdapter.AdapterClient.stubClass.prototype; | 58 interfaces.Connection.bindStubDerivedImpl(adapterClient)); |
| 116 | 59 }) |
| 117 var adapterFactory = connection.bindHandleToProxy( | |
| 118 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | |
| 119 bluetoothAdapter.AdapterFactory); | |
| 120 | |
| 121 // Get an Adapter service. | |
| 122 return adapterFactory.getAdapter(); | |
| 123 }).then(function(response) { | |
| 124 if (!response.adapter) { | |
| 125 throw new Error('Bluetooth Not Supported on this platform.'); | |
| 126 } | |
| 127 | |
| 128 adapter = connection.bindHandleToProxy(response.adapter, | |
| 129 bluetoothAdapter.Adapter); | |
| 130 | |
| 131 // Create a message pipe and bind one end to client | |
| 132 // implementation and the other to the Adapter service. | |
| 133 adapterClient = new AdapterClient(); | |
| 134 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | |
| 135 }); | |
| 136 } | |
| 137 | |
| 138 document.addEventListener('DOMContentLoaded', function() { | |
| 139 initializeProxies() | |
| 140 .then(function() { return adapter.getInfo(); }) | 60 .then(function() { return adapter.getInfo(); }) |
| 141 .then(function(response) { console.log('adapter', response.info); }) | 61 .then(function(response) { console.log('adapter', response.info); }) |
| 142 .then(function() { return adapter.getDevices(); }) | 62 .then(function() { return adapter.getDevices(); }) |
| 143 .then(function(response) { | 63 .then(function(response) { |
| 144 response.devices.forEach(adapterClient.deviceAdded, adapterClient); | 64 response.devices.forEach(adapterClient.deviceAdded, |
| 65 adapterClient /** this */); | |
| 66 | |
| 67 var deviceTable = new device_table.DeviceTable(); | |
| 68 deviceTable.setDevices(adapterClient.devices); | |
| 69 document.body.appendChild(deviceTable); | |
| 145 }) | 70 }) |
| 146 .catch(function(error) { console.error(error); }); | 71 .catch(function(error) { console.error(error); }); |
| 147 }); | 72 } |
| 148 })(); | 73 |
| 74 return { | |
| 75 initialize: initialize | |
| 76 }; | |
| 77 | |
| 78 }); | |
| 79 | |
| 80 document.addEventListener('DOMContentLoaded', bluetooth_internals.initialize); | |
| OLD | NEW |