Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Fix naming, merge upstream Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 cr.define('bluetooth_internals', function() { 10 cr.define('bluetooth_internals', function() {
11
12 var deviceAddressToProxy = new Map();
dpapad 2016/11/10 19:05:02 Can you annotate this with the appropriate K,V typ
mbrunson 2016/11/10 22:02:22 Done.
13
11 function initializeViews() { 14 function initializeViews() {
12 var adapterBroker = null; 15 var adapterBroker = null;
13 adapter_broker.getAdapterBroker() 16 adapter_broker.getAdapterBroker()
14 .then(function(broker) { adapterBroker = broker; }) 17 .then(function(broker) { adapterBroker = broker; })
15 .then(function() { return adapterBroker.getInfo(); }) 18 .then(function() { return adapterBroker.getInfo(); })
16 .then(function(response) { console.log('adapter', response.info); }) 19 .then(function(response) { console.log('adapter', response.info); })
17 .then(function() { return adapterBroker.getDevices(); }) 20 .then(function() { return adapterBroker.getDevices(); })
18 .then(function(response) { 21 .then(function(response) {
19 // Hook up device collection events. 22 // Hook up device collection events.
20 var devices = new device_collection.DeviceCollection([]); 23 var devices = new device_collection.DeviceCollection([]);
21 adapterBroker.addEventListener('deviceadded', function(event) { 24 adapterBroker.addEventListener('deviceadded', function(event) {
22 devices.addOrUpdate(event.detail.deviceInfo); 25 devices.addOrUpdate(event.detail.deviceInfo);
23 }); 26 });
24 adapterBroker.addEventListener('devicechanged', function(event) { 27 adapterBroker.addEventListener('devicechanged', function(event) {
25 devices.addOrUpdate(event.detail.deviceInfo); 28 devices.addOrUpdate(event.detail.deviceInfo);
26 }); 29 });
27 adapterBroker.addEventListener('deviceremoved', function(event) { 30 adapterBroker.addEventListener('deviceremoved', function(event) {
28 devices.remove(event.detail.deviceInfo); 31 devices.remove(event.detail.deviceInfo);
29 }); 32 });
30 33
31 response.devices.forEach(devices.addOrUpdate, 34 response.devices.forEach(devices.addOrUpdate,
32 devices /* this */); 35 devices /* this */);
33 36
34 var deviceTable = new device_table.DeviceTable(); 37 var deviceTable = new device_table.DeviceTable();
38
39 deviceTable.addEventListener('inspectpressed', function(event) {
40 // TODO(crbug.com/663470): Move connection logic to DeviceDetailsView
41 // when it's added in chrome://bluetooth-internals.
42 var address = event.detail.address;
43 var proxy = deviceAddressToProxy.get(address);
44
45 if (proxy) {
dpapad 2016/11/10 19:05:02 Nit: if (proxy) { ... return; } // No need fo
mbrunson 2016/11/10 22:02:22 Done.
46 // Device is already connected, so disconnect.
47 proxy.disconnect();
48 deviceAddressToProxy.delete(address);
49 devices.updateConnectionStatus(address, 'disconnected');
dpapad 2016/11/10 19:05:02 Please explain where those string literals come fr
mbrunson 2016/11/10 22:02:22 Done.
50 } else {
51 devices.updateConnectionStatus(address, 'connecting');
52 adapterBroker.connectToDevice(address).then(function(deviceProxy) {
53 if (!devices.getByAddress(address)) {
54 // Device no longer in list, so drop the connection.
55 deviceProxy.disconnect();
56 return;
57 }
58
59 deviceAddressToProxy.set(address, deviceProxy);
60 devices.updateConnectionStatus(address, 'connected');
61
62 // Fetch services asynchronously.
63 deviceProxy.getServices().then(function(response) {
64 var deviceInfo = devices.getByAddress(address);
65 deviceInfo.services = response.services;
66 devices.addOrUpdate(deviceInfo);
67 });
68 }).catch(function(error) {
69 devices.updateConnectionStatus(address, 'disconnected', error);
70 });
71 }
72 });
73
35 deviceTable.setDevices(devices); 74 deviceTable.setDevices(devices);
36 document.body.appendChild(deviceTable); 75 document.body.appendChild(deviceTable);
37 }) 76 })
38 .catch(function(error) { console.error(error); }); 77 .catch(function(error) { console.error(error); });
39 } 78 }
40 79
41 return { 80 return {
42 initializeViews: initializeViews 81 initializeViews: initializeViews
43 }; 82 };
44 83
45 }); 84 });
46 85
47 document.addEventListener( 86 document.addEventListener(
48 'DOMContentLoaded', bluetooth_internals.initializeViews); 87 'DOMContentLoaded', bluetooth_internals.initializeViews);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698