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 (function() { |
11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; | 11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
12 | 12 |
13 var REMOVED_CSS = 'removed'; | |
14 | |
13 /* | 15 /* |
14 * Data model for a cached device. | 16 * Data model for a cached device. |
15 * @constructor | 17 * @constructor |
16 * @param {!bluetoothDevice.DeviceInfo} info | 18 * @param {!bluetoothDevice.DeviceInfo} info |
17 */ | 19 */ |
18 var Device = function(info) { this.info = info; }; | 20 var Device = function(info) { this.info = info; }; |
19 | 21 |
20 /** | 22 /** |
21 * The implementation of AdapterClient in | 23 * The implementation of AdapterClient in |
22 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the | 24 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the |
23 * client-side collection of devices. | 25 * client-side collection of devices. |
24 * @constructor | 26 * @constructor |
25 */ | 27 */ |
26 var AdapterClient = function() { this.devices_ = new Map(); }; | 28 var AdapterClient = function() { this.devices_ = new Map(); }; |
27 AdapterClient.prototype = { | 29 AdapterClient.prototype = { |
28 /** | 30 /** |
29 * Logs added device to console and caches the device info. | 31 * Caches the device info and updates the device list. |
30 * @param {!bluetoothDevice.DeviceInfo} device | 32 * @param {!bluetoothDevice.DeviceInfo} deviceInfo |
31 */ | 33 */ |
32 deviceAdded: function(deviceInfo) { | 34 deviceAdded: function(deviceInfo) { |
33 console.log('Device added', deviceInfo); | 35 if (this.devices_.has(deviceInfo.address)) { |
34 this.devices_.set(deviceInfo.address, new Device(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); | |
35 }, | 51 }, |
36 | 52 |
37 /** | 53 /** |
38 * Logs removed device to console and removes the cached device. | 54 * Removes the cached device and updates the device list. |
dpapad
2016/10/24 18:12:26
Well, the comment says it "removes the cached devi
mbrunson
2016/10/24 19:27:53
Done.
| |
39 * @param {!bluetoothDevice.DeviceInfo} device | 55 * @param {!bluetoothDevice.DeviceInfo} deviceInfo |
40 */ | 56 */ |
41 deviceRemoved: function(deviceInfo) { | 57 deviceRemoved: function(deviceInfo) { |
42 console.log('Device removed', deviceInfo); | 58 $(deviceInfo.address).classList.add(REMOVED_CSS); |
43 this.devices_.delete(deviceInfo.address); | |
44 }, | 59 }, |
45 | 60 |
46 /** | 61 /** |
47 * Logs changed device info to console and updates the cached device. | 62 * Updates cached device and updates the device list. |
48 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 63 * @param {!bluetoothDevice.DeviceInfo} deviceInfo |
49 */ | 64 */ |
50 deviceChanged: function(deviceInfo) { | 65 deviceChanged: function(deviceInfo) { |
51 console.log(new Date(), deviceInfo); | 66 console.log(new Date(), deviceInfo); |
dpapad
2016/10/24 18:12:26
Now that this page starts having UI, do you still
mbrunson
2016/10/24 19:27:53
I'll remove the logs when I have a part of the UI
| |
52 if (this.devices_.has(deviceInfo.address)) { | 67 |
53 this.devices_.get(deviceInfo.address).info = deviceInfo; | 68 assert(this.devices_.has(deviceInfo.address), 'Device does not exist.'); |
54 } | 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; | |
55 } | 81 } |
56 }; | 82 }; |
57 | 83 |
58 /** | 84 /** |
59 * TODO(crbug.com/652361): Move to shared location. | 85 * TODO(crbug.com/652361): Move to shared location. |
60 * Helper to convert callback-based define() API to a promise-based API. | 86 * Helper to convert callback-based define() API to a promise-based API. |
61 * @param {!Array<string>} moduleNames | 87 * @param {!Array<string>} moduleNames |
62 * @return {!Promise} | 88 * @return {!Promise} |
63 */ | 89 */ |
64 function importModules(moduleNames) { | 90 function importModules(moduleNames) { |
65 return new Promise(function(resolve, reject) { | 91 return new Promise(function(resolve, reject) { |
66 define(moduleNames, function(var_args) { | 92 define(moduleNames, function(var_args) { |
67 resolve(Array.prototype.slice.call(arguments, 0)); | 93 resolve(Array.prototype.slice.call(arguments, 0)); |
68 }); | 94 }); |
69 }); | 95 }); |
70 } | 96 } |
71 | 97 |
72 /** | 98 /** |
73 * Initializes Mojo proxies for page and Bluetooth services. | 99 * Initializes Mojo proxies for page and Bluetooth services. |
74 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth | 100 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth |
75 * is not supported. | 101 * is not supported. |
76 */ | 102 */ |
77 function initializeProxies() { | 103 function initializeProxies() { |
78 return importModules([ | 104 return importModules([ |
79 'content/public/renderer/frame_interfaces', | 105 'content/public/renderer/frame_interfaces', |
80 'device/bluetooth/public/interfaces/adapter.mojom', | 106 'device/bluetooth/public/interfaces/adapter.mojom', |
81 'device/bluetooth/public/interfaces/device.mojom', | 107 'device/bluetooth/public/interfaces/device.mojom', |
82 'mojo/public/js/connection', | 108 'mojo/public/js/connection', |
83 ]).then(function([frameInterfaces, ...modules]) { | 109 ]).then(function([frameInterfaces, ...modules]) { |
84 console.log('Loaded modules'); | |
85 | |
86 // Destructure here to assign global variables. | 110 // Destructure here to assign global variables. |
87 [bluetoothAdapter, bluetoothDevice, connection] = modules; | 111 [bluetoothAdapter, bluetoothDevice, connection] = modules; |
88 | 112 |
89 // Hook up the instance properties. | 113 // Hook up the instance properties. |
90 AdapterClient.prototype.__proto__ = | 114 AdapterClient.prototype.__proto__ = |
91 bluetoothAdapter.AdapterClient.stubClass.prototype; | 115 bluetoothAdapter.AdapterClient.stubClass.prototype; |
92 | 116 |
93 var adapterFactory = connection.bindHandleToProxy( | 117 var adapterFactory = connection.bindHandleToProxy( |
94 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | 118 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
95 bluetoothAdapter.AdapterFactory); | 119 bluetoothAdapter.AdapterFactory); |
(...skipping 14 matching lines...) Expand all Loading... | |
110 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 134 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
111 }); | 135 }); |
112 } | 136 } |
113 | 137 |
114 document.addEventListener('DOMContentLoaded', function() { | 138 document.addEventListener('DOMContentLoaded', function() { |
115 initializeProxies() | 139 initializeProxies() |
116 .then(function() { return adapter.getInfo(); }) | 140 .then(function() { return adapter.getInfo(); }) |
117 .then(function(response) { console.log('adapter', response.info); }) | 141 .then(function(response) { console.log('adapter', response.info); }) |
118 .then(function() { return adapter.getDevices(); }) | 142 .then(function() { return adapter.getDevices(); }) |
119 .then(function(response) { | 143 .then(function(response) { |
120 console.log('devices', response.devices.length); | 144 response.devices.forEach(adapterClient.deviceAdded, adapterClient); |
121 | |
122 response.devices.forEach(function(deviceInfo) { | |
123 adapterClient.deviceAdded(deviceInfo); | |
124 }); | |
125 }) | 145 }) |
126 .catch(function(error) { console.error(error); }); | 146 .catch(function(error) { console.error(error); }); |
127 }); | 147 }); |
128 })(); | 148 })(); |
OLD | NEW |