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 15 matching lines...) Expand all Loading... |
26 * Prints removed device to console. | 26 * Prints removed device to console. |
27 * @param {!Object} device the device that was removed | 27 * @param {!Object} device the device that was removed |
28 */ | 28 */ |
29 deviceRemoved: function(device) { | 29 deviceRemoved: function(device) { |
30 console.log('Device removed'); | 30 console.log('Device removed'); |
31 console.log(device); | 31 console.log(device); |
32 } | 32 } |
33 }; | 33 }; |
34 | 34 |
35 (function() { | 35 (function() { |
| 36 var adapterFactory = null; |
36 var adapter = null; | 37 var adapter = null; |
| 38 var adapterHandle = null; |
37 var adapterClient = null; | 39 var adapterClient = null; |
38 var adapterClientHandle = null; | 40 var adapterClientHandle = null; |
39 | 41 |
40 /** | 42 /** |
41 * TODO: Move to shared location. See crbug.com/652361. | 43 * TODO: Move to shared location. See crbug.com/652361. |
42 * Helper to convert callback-based define() API to a promise-based API. | 44 * Helper to convert callback-based define() API to a promise-based API. |
43 * @param {!Array<string>} moduleNames | 45 * @param {!Array<string>} moduleNames |
44 * @return {!Promise} | 46 * @return {!Promise} |
45 */ | 47 */ |
46 function importModules(moduleNames) { | 48 function importModules(moduleNames) { |
47 return new Promise(function(resolve, reject) { | 49 return new Promise(function(resolve, reject) { |
48 define(moduleNames, function(var_args) { | 50 define(moduleNames, function(var_args) { |
49 resolve(Array.prototype.slice.call(arguments, 0)); | 51 resolve(Array.prototype.slice.call(arguments, 0)); |
50 }); | 52 }); |
51 }); | 53 }); |
52 } | 54 } |
53 | 55 |
54 /** | 56 /** |
55 * Initializes Mojo proxies for page and Bluetooth services. | 57 * Initializes Mojo proxies for page and Bluetooth services. |
56 * @return {!Promise} | 58 * @return {!Promise} resolves if adapter is acquired |
57 */ | 59 */ |
58 function initializeProxies() { | 60 function initializeProxies() { |
59 return importModules([ | 61 return importModules([ |
60 'content/public/renderer/frame_interfaces', | 62 'content/public/renderer/frame_interfaces', |
61 'device/bluetooth/public/interfaces/adapter.mojom', | 63 'device/bluetooth/public/interfaces/adapter.mojom', |
62 'mojo/public/js/connection', | 64 'mojo/public/js/connection', |
63 ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { | 65 ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { |
64 console.log('Loaded modules'); | 66 console.log('Loaded modules'); |
65 | 67 |
66 // Hook up the instance properties. | 68 // Hook up the instance properties. |
67 AdapterClient.prototype.__proto__ = | 69 AdapterClient.prototype.__proto__ = |
68 bluetoothAdapter.AdapterClient.stubClass.prototype; | 70 bluetoothAdapter.AdapterClient.stubClass.prototype; |
69 | 71 |
70 // Create a message pipe and bind one end to client | 72 // Create a message pipe and bind one end to client |
71 // implementation. | 73 // implementation. |
72 adapterClient = new AdapterClient(); | 74 adapterClient = new AdapterClient(); |
73 adapterClientHandle = connection.bindStubDerivedImpl(adapterClient); | 75 adapterClientHandle = connection.bindStubDerivedImpl(adapterClient); |
74 | 76 |
75 adapter = connection.bindHandleToProxy( | 77 adapterFactory = connection.bindHandleToProxy( |
76 frameInterfaces.getInterface(bluetoothAdapter.Adapter.name), | 78 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
77 bluetoothAdapter.Adapter); | 79 bluetoothAdapter.AdapterFactory); |
78 | 80 |
79 adapter.setClient(adapterClientHandle); | 81 // Create a message pipe for the Adapter service. |
| 82 return new Promise(function(resolve) { |
| 83 adapterHandle = connection.bindProxy(resolve, bluetoothAdapter.Adapter); |
| 84 }).then(function(proxy) { |
| 85 adapter = proxy; |
| 86 return adapterFactory.getAdapter(adapterHandle, adapterClientHandle); |
| 87 }).then(function(response) { |
| 88 if (!response.success) { |
| 89 throw new Error('Failed to acquire adapter.'); |
| 90 } |
| 91 }); |
80 }); | 92 }); |
81 } | 93 } |
82 | 94 |
83 document.addEventListener('DOMContentLoaded', function() { | 95 document.addEventListener('DOMContentLoaded', function() { |
84 initializeProxies() | 96 initializeProxies() |
85 .then(function() { return adapter.getDevices(); }) | 97 .then(function() { return adapter.getDevices(); }) |
86 .then(function(response) { console.log(response.devices); }); | 98 .then(function(response) { console.log(response.devices); }) |
| 99 .catch(function(error) { console.error(error); }); |
87 }); | 100 }); |
88 })(); | 101 })(); |
OLD | NEW |