| 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 AdapterBroker, served from | 6 * Javascript for AdapterBroker, served from |
| 7 * chrome://bluetooth-internals/. | 7 * chrome://bluetooth-internals/. |
| 8 */ | 8 */ |
| 9 cr.define('adapter_broker', function() { | 9 cr.define('adapter_broker', function() { |
| 10 /** | 10 /** |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 this.adapter_ = adapter; | 21 this.adapter_ = adapter; |
| 22 this.adapterClient_ = new AdapterClient(this); | 22 this.adapterClient_ = new AdapterClient(this); |
| 23 this.setClient(this.adapterClient_); | 23 this.setClient(this.adapterClient_); |
| 24 }; | 24 }; |
| 25 | 25 |
| 26 AdapterBroker.prototype = { | 26 AdapterBroker.prototype = { |
| 27 __proto__: cr.EventTarget.prototype, | 27 __proto__: cr.EventTarget.prototype, |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * Sets client of Adapter service. | 30 * Sets client of Adapter service. |
| 31 * @param {interfaces.BluetoothAdapter.AdapterClient} adapterClient | 31 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient |
| 32 */ | 32 */ |
| 33 setClient: function(adapterClient) { | 33 setClient: function(adapterClient) { |
| 34 this.adapter_.setClient(interfaces.Connection.bindStubDerivedImpl( | 34 this.adapter_.setClient(interfaces.Connection.bindStubDerivedImpl( |
| 35 adapterClient)); | 35 adapterClient)); |
| 36 }, | 36 }, |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * Gets an array of currently detectable devices from the Adapter service. | 39 * Gets an array of currently detectable devices from the Adapter service. |
| 40 * @return {Array<interfaces.BluetoothDevice.DeviceInfo>} | 40 * @return {!Array<!interfaces.BluetoothDevice.DeviceInfo>} |
| 41 */ | 41 */ |
| 42 getDevices: function() { | 42 getDevices: function() { |
| 43 return this.adapter_.getDevices(); | 43 return this.adapter_.getDevices(); |
| 44 }, | 44 }, |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * Gets the current state of the Adapter. | 47 * Gets the current state of the Adapter. |
| 48 * @return {interfaces.BluetoothAdapter.AdapterInfo} | 48 * @return {!interfaces.BluetoothAdapter.AdapterInfo} |
| 49 */ | 49 */ |
| 50 getInfo: function() { | 50 getInfo: function() { |
| 51 return this.adapter_.getInfo(); | 51 return this.adapter_.getInfo(); |
| 52 } | 52 } |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * The implementation of AdapterClient in | 56 * The implementation of AdapterClient in |
| 57 * device/bluetooth/public/interfaces/adapter.mojom. Dispatches events | 57 * device/bluetooth/public/interfaces/adapter.mojom. Dispatches events |
| 58 * through AdapterBroker to notify client objects of changes to the Adapter | 58 * through AdapterBroker to notify client objects of changes to the Adapter |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 } | 102 } |
| 103 }); | 103 }); |
| 104 this.adapterBroker_.dispatchEvent(event); | 104 this.adapterBroker_.dispatchEvent(event); |
| 105 } | 105 } |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 var adapterBroker = null; | 108 var adapterBroker = null; |
| 109 | 109 |
| 110 /** | 110 /** |
| 111 * Initializes an AdapterBroker if one doesn't exist. | 111 * Initializes an AdapterBroker if one doesn't exist. |
| 112 * @return {Promise<AdapterBroker>} resolves with AdapterBroker, | 112 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, |
| 113 * rejects if Bluetooth is not supported. | 113 * rejects if Bluetooth is not supported. |
| 114 */ | 114 */ |
| 115 function getAdapterBroker() { | 115 function getAdapterBroker() { |
| 116 if (adapterBroker) { | 116 if (adapterBroker) return Promise.resolve(adapterBroker); |
| 117 return Promise.resolve(adapterBroker); | |
| 118 } | |
| 119 | 117 |
| 120 return interfaces.importInterfaces().then(function(adapter) { | 118 return interfaces.setupInterfaces().then(function(adapter) { |
| 121 // Hook up the instance properties. | 119 // Hook up the instance properties. |
| 122 AdapterClient.prototype.__proto__ = | 120 AdapterClient.prototype.__proto__ = |
| 123 interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype; | 121 interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype; |
| 124 | 122 |
| 125 var adapterFactory = interfaces.Connection.bindHandleToProxy( | 123 var adapterFactory = interfaces.Connection.bindHandleToProxy( |
| 126 interfaces.FrameInterfaces.getInterface( | 124 interfaces.FrameInterfaces.getInterface( |
| 127 interfaces.BluetoothAdapter.AdapterFactory.name), | 125 interfaces.BluetoothAdapter.AdapterFactory.name), |
| 128 interfaces.BluetoothAdapter.AdapterFactory); | 126 interfaces.BluetoothAdapter.AdapterFactory); |
| 129 | 127 |
| 130 // Get an Adapter service. | 128 // Get an Adapter service. |
| 131 return adapterFactory.getAdapter(); | 129 return adapterFactory.getAdapter(); |
| 132 }).then(function(response) { | 130 }).then(function(response) { |
| 133 if (!response.adapter) { | 131 if (!response.adapter) { |
| 134 throw new Error('Bluetooth Not Supported on this platform.'); | 132 throw new Error('Bluetooth Not Supported on this platform.'); |
| 135 } | 133 } |
| 136 | 134 |
| 137 var adapter = interfaces.Connection.bindHandleToProxy( | 135 var adapter = interfaces.Connection.bindHandleToProxy( |
| 138 response.adapter, | 136 response.adapter, |
| 139 interfaces.BluetoothAdapter.Adapter); | 137 interfaces.BluetoothAdapter.Adapter); |
| 140 | 138 |
| 141 adapterBroker = new AdapterBroker(adapter); | 139 adapterBroker = new AdapterBroker(adapter); |
| 142 return adapterBroker; | 140 return adapterBroker; |
| 143 }); | 141 }); |
| 144 } | 142 } |
| 145 | 143 |
| 146 return { | 144 return { |
| 147 getAdapterBroker: getAdapterBroker, | 145 getAdapterBroker: getAdapterBroker, |
| 148 }; | 146 }; |
| 149 }); | 147 }); |
| OLD | NEW |