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 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
12 | 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 /** | 13 /** |
23 * The implementation of AdapterClient in | 14 * The implementation of AdapterClient in |
24 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the | 15 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the |
25 * client-side collection of devices. | 16 * client-side collection of devices. |
26 * @constructor | 17 * @constructor |
27 */ | 18 */ |
28 var AdapterClient = function() { this.devices_ = new Map(); }; | 19 var AdapterClient = function() { |
| 20 this.devices = new device.DeviceCollection([]); |
| 21 }; |
29 AdapterClient.prototype = { | 22 AdapterClient.prototype = { |
30 /** | 23 /** |
31 * Caches the device info and updates the device list. | 24 * Creates a new Device, stores it for display, and forces an update in the |
| 25 * |devices| collection. If device is known, it's unmarked as removed. |
32 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 26 * @param {!bluetoothDevice.DeviceInfo} deviceInfo |
33 */ | 27 */ |
34 deviceAdded: function(deviceInfo) { | 28 deviceAdded: function(deviceInfo) { |
35 if (this.devices_.has(deviceInfo.address)) { | 29 var knownDevice = this.devices.getByAddress(deviceInfo.address); |
36 var deviceElement = $(deviceInfo.address); | 30 if (knownDevice) { |
37 deviceElement.classList.remove(REMOVED_CSS); | 31 knownDevice.removed = false; |
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 } | 32 } |
49 | 33 |
50 this.deviceChanged(deviceInfo); | 34 this.devices.update(new device.Device(deviceInfo)); |
51 }, | 35 }, |
52 | 36 |
53 /** | 37 /** |
54 * Marks device as removed. | 38 * Marks device as removed. |
55 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 39 * @param {!bluetoothDevice.DeviceInfo} deviceInfo |
56 */ | 40 */ |
57 deviceRemoved: function(deviceInfo) { | 41 deviceRemoved: function(deviceInfo) { |
58 $(deviceInfo.address).classList.add(REMOVED_CSS); | 42 var device = this.devices.getByAddress(deviceInfo.address); |
| 43 device.removed = true; |
| 44 this.devices.update(device); |
59 }, | 45 }, |
60 | 46 |
61 /** | 47 /** |
62 * Updates cached device and updates the device list. | 48 * Updates the cached device. |
63 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 49 * @param {!bluetoothDevice.DeviceInfo} deviceInfo |
64 */ | 50 */ |
65 deviceChanged: function(deviceInfo) { | 51 deviceChanged: function(deviceInfo) { |
66 console.log(new Date(), deviceInfo); | 52 console.log(new Date(), deviceInfo); |
| 53 assert(this.devices.getByAddress(deviceInfo.address), |
| 54 'Device does not exist.'); |
67 | 55 |
68 assert(this.devices_.has(deviceInfo.address), 'Device does not exist.'); | 56 var device = this.devices.getByAddress(deviceInfo.address); |
69 | 57 |
70 this.devices_.get(deviceInfo.address).info = deviceInfo; | 58 // Update rssi if it's valid |
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) || | 59 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || |
79 deviceRow.querySelector('.device-rssi').textContent; | 60 (device.info.rssi && device.info.rssi.value); |
80 deviceRow.querySelector('.device-rssi').textContent = rssi; | 61 device.info = deviceInfo; |
| 62 device.info.rssi = { value: rssi }; |
| 63 this.devices.update(device); |
81 } | 64 } |
82 }; | 65 }; |
83 | 66 |
84 /** | 67 /** |
85 * TODO(crbug.com/652361): Move to shared location. | 68 * TODO(crbug.com/652361): Move to shared location. |
86 * Helper to convert callback-based define() API to a promise-based API. | 69 * Helper to convert callback-based define() API to a promise-based API. |
87 * @param {!Array<string>} moduleNames | 70 * @param {!Array<string>} moduleNames |
88 * @return {!Promise} | 71 * @return {!Promise} |
89 */ | 72 */ |
90 function importModules(moduleNames) { | 73 function importModules(moduleNames) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 adapter = connection.bindHandleToProxy(response.adapter, | 111 adapter = connection.bindHandleToProxy(response.adapter, |
129 bluetoothAdapter.Adapter); | 112 bluetoothAdapter.Adapter); |
130 | 113 |
131 // Create a message pipe and bind one end to client | 114 // Create a message pipe and bind one end to client |
132 // implementation and the other to the Adapter service. | 115 // implementation and the other to the Adapter service. |
133 adapterClient = new AdapterClient(); | 116 adapterClient = new AdapterClient(); |
134 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 117 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
135 }); | 118 }); |
136 } | 119 } |
137 | 120 |
138 document.addEventListener('DOMContentLoaded', function() { | 121 function initialize() { |
139 initializeProxies() | 122 initializeProxies() |
140 .then(function() { return adapter.getInfo(); }) | 123 .then(function() { return adapter.getInfo(); }) |
141 .then(function(response) { console.log('adapter', response.info); }) | 124 .then(function(response) { console.log('adapter', response.info); }) |
142 .then(function() { return adapter.getDevices(); }) | 125 .then(function() { return adapter.getDevices(); }) |
143 .then(function(response) { | 126 .then(function(response) { |
144 response.devices.forEach(adapterClient.deviceAdded, adapterClient); | 127 response.devices.forEach(adapterClient.deviceAdded, |
| 128 adapterClient /** this */); |
| 129 |
| 130 var deviceTable = new device.DeviceTable(); |
| 131 deviceTable.setDevices(adapterClient.devices); |
| 132 document.body.appendChild(deviceTable); |
145 }) | 133 }) |
146 .catch(function(error) { console.error(error); }); | 134 .catch(function(error) { console.error(error); }); |
147 }); | 135 } |
148 })(); | 136 |
| 137 return { |
| 138 initialize: initialize |
| 139 }; |
| 140 |
| 141 }); |
| 142 |
| 143 document.addEventListener('DOMContentLoaded', bluetooth_internals.initialize); |
OLD | NEW |