Chromium Code Reviews| 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..627fc3fd0c22a7e828a9efbc64caea2e4b70bbbd 100644 |
| --- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| +++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| @@ -7,6 +7,83 @@ |
| * chrome://bluetooth-internals/. |
| */ |
| -document.addEventListener('DOMContentLoaded', function() { |
| - console.log('Welcome to Bluetooth Internals.'); |
| -}); |
| +/** |
| + * The implementation of AdapterClient in |
| + * device/bluetooth/public/interfaces/adapter.mojom. |
| + */ |
| +class AdapterClient { |
| + /** |
| + * 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 removed'); |
| + console.log(device); |
| + } |
| +}; |
| + |
| +(function() { |
| + let adapter = null; |
| + let adapterClient = null; |
| + let adapterClientHandle = null; |
| + |
| + /** |
| + * 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) { |
| + define(moduleNames, function(var_args) { |
| + resolve(Array.prototype.slice.call(arguments, 0)); |
| + }); |
| + }); |
| + } |
| + |
| + /** |
| + * Initializes Mojo proxies for page and Bluetooth services. |
| + * @return {!Promise} |
| + */ |
| + function initializeProxies() { |
| + return importModules([ |
| + 'content/public/renderer/frame_interfaces', |
| + 'device/bluetooth/public/interfaces/adapter.mojom', |
| + 'mojo/public/js/connection', |
| + ]) |
| + .then(([frameInterfaces, bluetooth, connection]) => { |
|
scheib
2016/09/28 03:12:21
The 'bluetooth' returned module will be trouble la
mbrunson
2016/09/28 21:20:34
Yeah. I realized this but left it the same because
|
| + console.log('Loaded modules'); |
| + |
| + // Hook up the instance properties |
| + Object.setPrototypeOf( |
| + AdapterClient.prototype, |
| + bluetooth.AdapterClient.stubClass.prototype); |
| + |
| + adapterClient = new AdapterClient(); |
|
ortuno
2016/09/28 09:44:54
nit: Move this next to where it's used.
mbrunson
2016/09/28 21:20:34
Done.
|
| + |
| + // Create a message pipe and bind one end to client |
| + // implementation |
| + adapterClientHandle = connection.bindStubDerivedImpl(adapterClient); |
| + |
| + adapter = connection.bindHandleToProxy( |
| + frameInterfaces.getInterface(bluetooth.Adapter.name), |
| + bluetooth.Adapter); |
| + |
| + adapter.setClient(adapterClientHandle); |
| + }); |
| + } |
| + |
| + document.addEventListener('DOMContentLoaded', function() { |
| + initializeProxies() |
| + .then(() => adapter.getDevices()) |
| + .then(response => console.log(response.devices)); |
| + }); |
| +})(); |