Chromium Code Reviews| 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 /** | 10 /** |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 console.log(device); | 31 console.log(device); |
| 32 } | 32 } |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 (function() { | 35 (function() { |
| 36 var adapter = null; | 36 var adapter = null; |
| 37 var adapterClient = null; | 37 var adapterClient = null; |
| 38 var adapterClientHandle = null; | 38 var adapterClientHandle = null; |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * Logs basic information retrieved from the adapter. | |
| 42 */ | |
| 43 function logAdapterInfo() { | |
| 44 console.log('Getting adapter info'); | |
| 45 | |
| 46 adapter.getAddress().then(function(response) { | |
|
scheib
2016/10/01 00:26:49
Perhaps we should have an AdapterInfo struct and f
mbrunson
2016/10/03 17:40:10
I agree.
| |
| 47 console.log('Address: ' + response.address); | |
| 48 }); | |
| 49 | |
| 50 adapter.getName().then(function(response) { | |
| 51 console.log('Name: ' + response.name); | |
| 52 }); | |
| 53 | |
| 54 adapter.isPowered().then(function(response) { | |
| 55 console.log('Is Powered: ' + response.powered); | |
| 56 }); | |
| 57 | |
| 58 adapter.isPresent().then(function(response) { | |
| 59 console.log('Is Present: ' + response.present); | |
| 60 }); | |
| 61 | |
| 62 adapter.getDevices().then(function(response) { | |
| 63 console.log('Device Count: ' + response.devices.length); | |
| 64 }); | |
| 65 } | |
| 66 | |
| 67 /** | |
| 41 * Helper to convert callback-based define() API to a promise-based API. | 68 * Helper to convert callback-based define() API to a promise-based API. |
| 42 * @param {!Array<string>} moduleNames | 69 * @param {!Array<string>} moduleNames |
| 43 * @return {!Promise} | 70 * @return {!Promise} |
| 44 */ | 71 */ |
| 45 function importModules(moduleNames) { | 72 function importModules(moduleNames) { |
| 46 return new Promise(function(resolve, reject) { | 73 return new Promise(function(resolve, reject) { |
| 47 define(moduleNames, function(var_args) { | 74 define(moduleNames, function(var_args) { |
| 48 resolve(Array.prototype.slice.call(arguments, 0)); | 75 resolve(Array.prototype.slice.call(arguments, 0)); |
| 49 }); | 76 }); |
| 50 }); | 77 }); |
| 51 } | 78 } |
| 52 | 79 |
| 53 /** | 80 /** |
| 54 * Initializes Mojo proxies for page and Bluetooth services. | 81 * Initializes Mojo proxies for page and Bluetooth services. |
| 55 * @return {!Promise} | 82 * @return {!Promise} |
| 56 */ | 83 */ |
| 57 function initializeProxies() { | 84 function initializeProxies() { |
| 58 return importModules([ | 85 return importModules([ |
| 59 'content/public/renderer/frame_interfaces', | 86 'content/public/renderer/frame_interfaces', |
| 60 'device/bluetooth/public/interfaces/adapter.mojom', | 87 'device/bluetooth/public/interfaces/adapter.mojom', |
| 61 'mojo/public/js/connection', | 88 'mojo/public/js/connection', |
| 62 ]) | 89 ]) |
| 63 .then(function(modules) { | 90 .then(function(modules) { |
| 64 var frameInterfaces = modules[0]; | 91 var frameInterfaces = modules[0]; |
| 65 var bluetoothAdapter = modules[1]; | 92 var bluetoothAdapter = modules[1]; |
| 66 var connection = modules[2]; | 93 var connection = modules[2]; |
| 67 | 94 |
| 68 console.log('Loaded modules'); | 95 console.log('Loaded modules'); |
| 69 | 96 |
| 70 // Hook up the instance properties | 97 // Hook up the instance properties. |
| 71 AdapterClient.prototype.__proto__ = | 98 AdapterClient.prototype.__proto__ = |
| 72 bluetoothAdapter.AdapterClient.stubClass.prototype; | 99 bluetoothAdapter.AdapterClient.stubClass.prototype; |
| 73 | 100 |
| 74 // Create a message pipe and bind one end to client | 101 // Create a message pipe and bind one end to client |
| 75 // implementation | 102 // implementation. |
| 76 adapterClient = new AdapterClient(); | 103 adapterClient = new AdapterClient(); |
| 77 adapterClientHandle = connection.bindStubDerivedImpl(adapterClient); | 104 adapterClientHandle = connection.bindStubDerivedImpl(adapterClient); |
| 78 | 105 |
| 79 adapter = connection.bindHandleToProxy( | 106 adapter = connection.bindHandleToProxy( |
| 80 frameInterfaces.getInterface(bluetoothAdapter.Adapter.name), | 107 frameInterfaces.getInterface(bluetoothAdapter.Adapter.name), |
| 81 bluetoothAdapter.Adapter); | 108 bluetoothAdapter.Adapter); |
| 82 | 109 |
| 83 adapter.setClient(adapterClientHandle); | 110 adapter.setClient(adapterClientHandle); |
| 84 }); | 111 }); |
| 85 } | 112 } |
| 86 | 113 |
| 87 document.addEventListener('DOMContentLoaded', function() { | 114 document.addEventListener('DOMContentLoaded', function() { |
| 88 initializeProxies() | 115 initializeProxies().then(function() { logAdapterInfo(); }); |
| 89 .then(function() { return adapter.getDevices();}) | |
| 90 .then(function(response) {console.log(response.devices);}); | |
| 91 }); | 116 }); |
| 92 })(); | 117 })(); |
| OLD | NEW |