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