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; |
37 var adapterClient = null; | 38 var adapterClient = null; |
38 var adapterClientHandle = null; | |
39 | 39 |
40 /** | 40 /** |
41 * TODO: Move to shared location. See crbug.com/652361. | 41 * TODO: Move to shared location. See crbug.com/652361. |
42 * Helper to convert callback-based define() API to a promise-based API. | 42 * Helper to convert callback-based define() API to a promise-based API. |
43 * @param {!Array<string>} moduleNames | 43 * @param {!Array<string>} moduleNames |
44 * @return {!Promise} | 44 * @return {!Promise} |
45 */ | 45 */ |
46 function importModules(moduleNames) { | 46 function importModules(moduleNames) { |
47 return new Promise(function(resolve, reject) { | 47 return new Promise(function(resolve, reject) { |
48 define(moduleNames, function(var_args) { | 48 define(moduleNames, function(var_args) { |
49 resolve(Array.prototype.slice.call(arguments, 0)); | 49 resolve(Array.prototype.slice.call(arguments, 0)); |
50 }); | 50 }); |
51 }); | 51 }); |
52 } | 52 } |
53 | 53 |
54 /** | 54 /** |
55 * Initializes Mojo proxies for page and Bluetooth services. | 55 * Initializes Mojo proxies for page and Bluetooth services. |
56 * @return {!Promise} | 56 * @return {!Promise} resolves if adapter is acquired |
dpapad
2016/10/06 00:52:45
Nit: Use proper punctuation (per styleguide). Also
mbrunson
2016/10/06 01:13:58
Done.
| |
57 */ | 57 */ |
58 function initializeProxies() { | 58 function initializeProxies() { |
59 return importModules([ | 59 return importModules([ |
60 'content/public/renderer/frame_interfaces', | 60 'content/public/renderer/frame_interfaces', |
61 'device/bluetooth/public/interfaces/adapter.mojom', | 61 'device/bluetooth/public/interfaces/adapter.mojom', |
62 'mojo/public/js/connection', | 62 'mojo/public/js/connection', |
63 ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { | 63 ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { |
64 console.log('Loaded modules'); | 64 console.log('Loaded modules'); |
65 | 65 |
66 // Hook up the instance properties. | 66 // Hook up the instance properties. |
67 AdapterClient.prototype.__proto__ = | 67 AdapterClient.prototype.__proto__ = |
68 bluetoothAdapter.AdapterClient.stubClass.prototype; | 68 bluetoothAdapter.AdapterClient.stubClass.prototype; |
69 | 69 |
70 // Create a message pipe and bind one end to client | 70 adapterFactory = connection.bindHandleToProxy( |
dpapad
2016/10/06 00:52:45
Does this need to be global? It is only used withi
mbrunson
2016/10/06 01:13:58
Done.
| |
71 // implementation. | 71 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
72 adapterClient = new AdapterClient(); | 72 bluetoothAdapter.AdapterFactory); |
73 adapterClientHandle = connection.bindStubDerivedImpl(adapterClient); | |
74 | 73 |
75 adapter = connection.bindHandleToProxy( | 74 // Get an Adapter service. |
76 frameInterfaces.getInterface(bluetoothAdapter.Adapter.name), | 75 return adapterFactory.getAdapter().then(function(response) { |
77 bluetoothAdapter.Adapter); | 76 if (!response.adapter) { |
77 throw new Error('Bluetooth Not Supported on this platform.'); | |
78 } | |
78 | 79 |
79 adapter.setClient(adapterClientHandle); | 80 adapter = connection.bindHandleToProxy(response.adapter, |
81 bluetoothAdapter.Adapter); | |
82 | |
83 // Create a message pipe and bind one end to client | |
84 // implementation and the other to the Adapter service. | |
85 adapterClient = new AdapterClient(); | |
86 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | |
87 }); | |
80 }); | 88 }); |
81 } | 89 } |
82 | 90 |
83 document.addEventListener('DOMContentLoaded', function() { | 91 document.addEventListener('DOMContentLoaded', function() { |
84 initializeProxies() | 92 initializeProxies() |
85 .then(function() { return adapter.getDevices(); }) | 93 .then(function() { return adapter.getDevices(); }) |
86 .then(function(response) { console.log(response.devices); }); | 94 .then(function(response) { console.log(response.devices); }) |
95 .catch(function(error) { console.error(error); }); | |
87 }); | 96 }); |
88 })(); | 97 })(); |
OLD | NEW |