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 /** | |
11 * The implementation of AdapterClient in | |
12 * device/bluetooth/public/interfaces/adapter.mojom. | |
13 */ | |
14 var AdapterClient = function() {}; | |
15 AdapterClient.prototype = { | |
16 /** | |
17 * Prints added device to console. | |
18 * @param {!bluetoothDevice.DeviceInfo} device | |
19 */ | |
20 deviceAdded: function(device) { console.log('Device added', device); }, | |
21 | |
22 /** | |
23 * Prints removed device to console. | |
24 * @param {!bluetoothDevice.DeviceInfo} device | |
25 */ | |
26 deviceRemoved: function(device) { console.log('Device removed', device); } | |
27 }; | |
28 | |
29 (function() { | 10 (function() { |
30 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; | 11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
31 | 12 |
13 // Dictionary for address->Device cache. | |
14 var devices = {}; | |
dpapad
2016/10/20 17:52:03
Can you use a proper Javascript Map instance inste
mbrunson
2016/10/20 20:09:04
Done.
| |
15 | |
16 // Data model for a cached device. | |
17 function Device() {}; | |
dpapad
2016/10/20 17:52:03
Why is this a class with a prototype? It has no me
mbrunson
2016/10/20 18:15:36
I'm using it as a simple data container right now.
dpapad
2016/10/20 18:36:21
Yes, data fields should be in the constructor. Wei
mbrunson
2016/10/20 20:09:04
Ah ok. I forgot about the shallow copy for Object
| |
18 Device.prototype = { info: null }; | |
19 | |
20 /** | |
21 * The implementation of AdapterClient in | |
22 * device/bluetooth/public/interfaces/adapter.mojom. | |
23 */ | |
24 var AdapterClient = function() {}; | |
25 AdapterClient.prototype = { | |
26 /** | |
27 * Logs added device to console and caches the device info. | |
28 * @param {!bluetoothDevice.DeviceInfo} device | |
29 */ | |
30 deviceAdded: function(deviceInfo) { | |
31 console.log('Device added', deviceInfo); | |
32 devices[deviceInfo.address] = new Device(); | |
dpapad
2016/10/20 17:52:03
Again, don't fully understand what you are trying
mbrunson
2016/10/20 18:15:36
The Device instance is just a data container to gr
dpapad
2016/10/20 18:36:21
If you want to go the class route instead of a sim
mbrunson
2016/10/20 20:09:04
I'll stick with the class route. Done.
| |
33 devices[deviceInfo.address].info = deviceInfo; | |
34 }, | |
35 | |
36 /** | |
37 * Logs removed device to console and removes the cached device. | |
38 * @param {!bluetoothDevice.DeviceInfo} device | |
39 */ | |
40 deviceRemoved: function(deviceInfo) { | |
41 console.log('Device removed', deviceInfo); | |
42 delete devices[deviceInfo.address]; | |
43 }, | |
44 | |
45 /** | |
46 * Logs changed device info to console and updates the cached device. | |
47 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | |
48 */ | |
49 deviceChanged: function(deviceInfo) { | |
50 console.log(new Date(), deviceInfo); | |
51 devices[deviceInfo.address].info = deviceInfo; | |
52 } | |
53 }; | |
54 | |
32 /** | 55 /** |
33 * TODO(crbug.com/652361): Move to shared location. | 56 * TODO(crbug.com/652361): Move to shared location. |
34 * Helper to convert callback-based define() API to a promise-based API. | 57 * Helper to convert callback-based define() API to a promise-based API. |
35 * @param {!Array<string>} moduleNames | 58 * @param {!Array<string>} moduleNames |
36 * @return {!Promise} | 59 * @return {!Promise} |
37 */ | 60 */ |
38 function importModules(moduleNames) { | 61 function importModules(moduleNames) { |
39 return new Promise(function(resolve, reject) { | 62 return new Promise(function(resolve, reject) { |
40 define(moduleNames, function(var_args) { | 63 define(moduleNames, function(var_args) { |
41 resolve(Array.prototype.slice.call(arguments, 0)); | 64 resolve(Array.prototype.slice.call(arguments, 0)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 adapter = connection.bindHandleToProxy(response.adapter, | 101 adapter = connection.bindHandleToProxy(response.adapter, |
79 bluetoothAdapter.Adapter); | 102 bluetoothAdapter.Adapter); |
80 | 103 |
81 // Create a message pipe and bind one end to client | 104 // Create a message pipe and bind one end to client |
82 // implementation and the other to the Adapter service. | 105 // implementation and the other to the Adapter service. |
83 adapterClient = new AdapterClient(); | 106 adapterClient = new AdapterClient(); |
84 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 107 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
85 }); | 108 }); |
86 } | 109 } |
87 | 110 |
88 /** | |
89 * Prints device info from the device service. | |
90 * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to | |
91 * get the information. | |
92 * @return {!Promise} resolves if device service is retrieved, rejects | |
93 * otherwise. | |
94 */ | |
95 function logDevice(deviceInfo) { | |
96 return adapter.getDevice(deviceInfo.address).then(function(response) { | |
97 var deviceHandle = response.device; | |
98 if (!deviceHandle) { | |
99 throw new Error(deviceInfo.name_for_display + ' cannot be found.'); | |
100 } | |
101 | |
102 var device = connection.bindHandleToProxy( | |
103 deviceHandle, bluetoothDevice.Device); | |
104 return device.getInfo(); | |
105 }).then(function(response) { | |
106 console.log(deviceInfo.name_for_display, response.info); | |
107 }); | |
108 } | |
109 | |
110 document.addEventListener('DOMContentLoaded', function() { | 111 document.addEventListener('DOMContentLoaded', function() { |
111 initializeProxies() | 112 initializeProxies() |
112 .then(function() { return adapter.getInfo(); }) | 113 .then(function() { return adapter.getInfo(); }) |
113 .then(function(response) { console.log('adapter', response.info); }) | 114 .then(function(response) { console.log('adapter', response.info); }) |
114 .then(function() { return adapter.getDevices(); }) | 115 .then(function() { return adapter.getDevices(); }) |
115 .then(function(response) { | 116 .then(function(response) { |
116 var devices = response.devices; | 117 console.log('devices', response.devices.length); |
117 console.log('devices', devices.length); | |
118 | 118 |
119 return Promise.all(devices.map(logDevice)); | 119 response.devices.forEach(function(deviceInfo) { |
120 devices[deviceInfo.address] = new Device(); | |
121 devices[deviceInfo.address].info = deviceInfo; | |
122 console.log(deviceInfo.name_for_display, deviceInfo); | |
123 }); | |
120 }) | 124 }) |
121 .catch(function(error) { console.error(error); }); | 125 .catch(function(error) { console.error(error); }); |
122 }); | 126 }); |
123 })(); | 127 })(); |
OLD | NEW |