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

Unified Diff: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js

Issue 2357383002: bluetooth: Add device list retrieval for chrome://bluetooth-internals (Closed)
Patch Set: Created 4 years, 3 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
diff --git a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
index 308779cb50e4ed62fb1168bf63515bbd91d2ec74..0ca90c1ae32d2cff6e3f3b032c2c6d2f7d03dd57 100644
--- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
+++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
@@ -6,7 +6,95 @@
* Javascript for bluetooth_internals.html, served from
* chrome://bluetooth-internals/.
*/
+(function() {
+ let adapterService = null;
-document.addEventListener('DOMContentLoaded', function() {
- console.log('Welcome to Bluetooth Internals.');
-});
+ /**
+ * Retrieves a list of devices from the Bluetooth Adapter service.
+ * @return {!Promise} promise with array of devices
+ */
+ function getDevices() {
+ return adapterService.getDevices(0, 5).then((response) => response.devices);
+ }
+
+ /**
+ * Helper to convert callback-based define() API to a promise-based API.
+ * @param {!Array<string>} moduleNames
+ * @return {!Promise}
+ */
+ function importModules(moduleNames) {
+ return new Promise(function(resolve, reject) {
ortuno 2016/09/22 08:32:53 q: Does this need to be a function? Could it be an
scheib 2016/09/23 20:53:51 These are good suggestions, but ortuno FYI this is
mbrunson 2016/09/24 01:05:46 The define callback has to be a function. I've run
ortuno 2016/09/26 01:57:55 Ah got it. Thanks for the explanation.
+ define(moduleNames, function(var_args) {
ortuno 2016/09/22 08:32:53 var_args seems unused?
mbrunson 2016/09/24 01:05:46 Done.
ortuno 2016/09/26 01:57:55 Did you mean to delete the argument or are you lea
mbrunson 2016/09/28 02:18:21 I'm just going to leave this entire function alone
+ resolve(Array.prototype.slice.call(arguments, 0));
ortuno 2016/09/22 08:32:53 I believe you can use the spread operator here. So
ortuno 2016/09/22 08:32:53 I believe you can use the spread operator here. So
mbrunson 2016/09/24 01:05:46 I believe the promise spec only allows a single va
ortuno 2016/09/26 01:57:55 Right. In ES2015 you can use either the spread ope
+ });
+ });
+ }
+
+ /**
+ * Initializes Mojo proxies for page and Bluetooth services.
+ * @return {!Promise}
+ */
+ function initializeProxies() {
+ return importModules([
+ 'content/public/renderer/frame_interfaces',
+ 'chrome/browser/ui/webui/bluetooth_internals/' +
+ 'bluetooth_internals.mojom',
+ 'device/bluetooth/public/interfaces/bluetooth.mojom',
+ 'mojo/public/js/connection',
+ ])
+ .then((modules) => {
ortuno 2016/09/22 08:32:53 .then(([frameInterfaces, mojom, bluetooth, connect
mbrunson 2016/09/24 01:05:46 In which case this has to be one variable; however
+ let frameInterfaces = modules[0];
+ let mojom = modules[1];
+ let bluetooth = modules[2];
+ let connection = modules[3];
+
+ let pageHandler = connection.bindHandleToProxy(
+ frameInterfaces.getInterface(mojom.InternalsPageHandler.name),
+ mojom.InternalsPageHandler);
+
+ let AdapterClientImpl = function() {};
+
+ // Create adapter client implementation
+ AdapterClientImpl.prototype = {
ortuno 2016/09/22 08:32:53 Is it possible to use ES6 classes? https://cs.chr
mbrunson 2016/09/24 01:05:46 Done.
+ __proto__: bluetooth.AdapterClient.stubClass.prototype,
+ /**
+ * Prints added device to console
+ * @param {!Object} device the device that was added
+ */
+ deviceAdded(device) {
+ console.log('Device added');
+ console.log(device);
+ },
+
+ /**
+ * Prints removed device to console
+ * @param {!Object} device the device that was removed
+ */
+ deviceRemoved(device) {
+ console.log('Device added');
+ console.log(device);
+ }
+ };
+
+ let adapterClient = new AdapterClientImpl();
+
+ // Create adapter service interface request
+ let adapter = connection.bindProxy2(bluetooth.Adapter);
+
+ adapterService = adapter.proxy;
+ let adapterHandle = adapter.handle;
+
+ // Create a message pipe and bind one end to client implementation
+ let clientHandle = connection.bindStubDerivedImpl(adapterClient);
+
+ // Send interface request and other end of client message pipe to C++
+ pageHandler.getAdapterService(adapterHandle, clientHandle);
+ });
+ }
+
+ document.addEventListener('DOMContentLoaded', function() {
+ initializeProxies()
+ .then(() => getDevices())
+ .then((devices) => console.log(devices));
+ });
+})();

Powered by Google App Engine
This is Rietveld 408576698