| 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() { |
| 11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; | 11 function initializeViews() { |
| 12 var adapterBroker = null; |
| 13 adapter_broker.getAdapterBroker() |
| 14 .then(function(broker) { adapterBroker = broker; }) |
| 15 .then(function() { return adapterBroker.getInfo(); }) |
| 16 .then(function(response) { console.log('adapter', response.info); }) |
| 17 .then(function() { return adapterBroker.getDevices(); }) |
| 18 .then(function(response) { |
| 19 // Hook up device collection events. |
| 20 var devices = new device_collection.DeviceCollection([]); |
| 21 adapterBroker.addEventListener('deviceadded', function(event) { |
| 22 devices.addOrUpdate(event.deviceInfo); |
| 23 }); |
| 24 adapterBroker.addEventListener('devicechanged', function(event) { |
| 25 devices.addOrUpdate(event.deviceInfo); |
| 26 }); |
| 27 adapterBroker.addEventListener('deviceremoved', function(event) { |
| 28 devices.remove(event.deviceInfo); |
| 29 }); |
| 12 | 30 |
| 13 var REMOVED_CSS = 'removed'; | 31 response.devices.forEach(devices.addOrUpdate, |
| 32 devices /* this */); |
| 14 | 33 |
| 15 /* | 34 var deviceTable = new device_table.DeviceTable(); |
| 16 * Data model for a cached device. | 35 deviceTable.setDevices(devices); |
| 17 * @constructor | 36 document.body.appendChild(deviceTable); |
| 18 * @param {!bluetoothDevice.DeviceInfo} info | 37 }) |
| 19 */ | 38 .catch(function(error) { console.error(error); }); |
| 20 var Device = function(info) { this.info = info; }; | 39 } |
| 21 | 40 |
| 22 /** | 41 return { |
| 23 * The implementation of AdapterClient in | 42 initializeViews: initializeViews |
| 24 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the | |
| 25 * client-side collection of devices. | |
| 26 * @constructor | |
| 27 */ | |
| 28 var AdapterClient = function() { this.devices_ = new Map(); }; | |
| 29 AdapterClient.prototype = { | |
| 30 /** | |
| 31 * Caches the device info and updates the device list. | |
| 32 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | |
| 33 */ | |
| 34 deviceAdded: function(deviceInfo) { | |
| 35 if (this.devices_.has(deviceInfo.address)) { | |
| 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 }, | |
| 52 | |
| 53 /** | |
| 54 * Marks device as removed. | |
| 55 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | |
| 56 */ | |
| 57 deviceRemoved: function(deviceInfo) { | |
| 58 $(deviceInfo.address).classList.add(REMOVED_CSS); | |
| 59 }, | |
| 60 | |
| 61 /** | |
| 62 * Updates cached device and updates the device list. | |
| 63 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | |
| 64 */ | |
| 65 deviceChanged: function(deviceInfo) { | |
| 66 console.log(new Date(), deviceInfo); | |
| 67 | |
| 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 } | |
| 82 }; | 43 }; |
| 83 | 44 |
| 84 /** | 45 }); |
| 85 * Initializes Mojo proxies for page and Bluetooth services. | |
| 86 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth | |
| 87 * is not supported. | |
| 88 */ | |
| 89 function initializeProxies() { | |
| 90 return importModules([ | |
| 91 'content/public/renderer/frame_interfaces', | |
| 92 'device/bluetooth/public/interfaces/adapter.mojom', | |
| 93 'device/bluetooth/public/interfaces/device.mojom', | |
| 94 'mojo/public/js/connection', | |
| 95 ]).then(function([frameInterfaces, ...modules]) { | |
| 96 // Destructure here to assign global variables. | |
| 97 [bluetoothAdapter, bluetoothDevice, connection] = modules; | |
| 98 | 46 |
| 99 // Hook up the instance properties. | 47 document.addEventListener( |
| 100 AdapterClient.prototype.__proto__ = | 48 'DOMContentLoaded', bluetooth_internals.initializeViews); |
| 101 bluetoothAdapter.AdapterClient.stubClass.prototype; | |
| 102 | |
| 103 var adapterFactory = connection.bindHandleToProxy( | |
| 104 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | |
| 105 bluetoothAdapter.AdapterFactory); | |
| 106 | |
| 107 // Get an Adapter service. | |
| 108 return adapterFactory.getAdapter(); | |
| 109 }).then(function(response) { | |
| 110 if (!response.adapter) { | |
| 111 throw new Error('Bluetooth Not Supported on this platform.'); | |
| 112 } | |
| 113 | |
| 114 adapter = connection.bindHandleToProxy(response.adapter, | |
| 115 bluetoothAdapter.Adapter); | |
| 116 | |
| 117 // Create a message pipe and bind one end to client | |
| 118 // implementation and the other to the Adapter service. | |
| 119 adapterClient = new AdapterClient(); | |
| 120 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | |
| 121 }); | |
| 122 } | |
| 123 | |
| 124 document.addEventListener('DOMContentLoaded', function() { | |
| 125 initializeProxies() | |
| 126 .then(function() { return adapter.getInfo(); }) | |
| 127 .then(function(response) { console.log('adapter', response.info); }) | |
| 128 .then(function() { return adapter.getDevices(); }) | |
| 129 .then(function(response) { | |
| 130 response.devices.forEach(adapterClient.deviceAdded, adapterClient); | |
| 131 }) | |
| 132 .catch(function(error) { console.error(error); }); | |
| 133 }); | |
| 134 })(); | |
| OLD | NEW |