| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 return response.device; | 48 return response.device; |
| 49 }); | 49 }); |
| 50 }, | 50 }, |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Sets client of Adapter service. | 53 * Sets client of Adapter service. |
| 54 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient | 54 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient |
| 55 */ | 55 */ |
| 56 setClient: function(adapterClient) { | 56 setClient: function(adapterClient) { |
| 57 adapterClient.binding = new interfaces.Bindings.Binding( | 57 adapterClient.binding = new interfaces.Bindings.Binding( |
| 58 interfaces.BluetoothAdapter.AdapterClient, | 58 interfaces.BluetoothAdapter.AdapterClient, adapterClient); |
| 59 adapterClient); | |
| 60 | 59 |
| 61 this.adapter_.setClient( | 60 this.adapter_.setClient( |
| 62 adapterClient.binding.createInterfacePtrAndBind()); | 61 adapterClient.binding.createInterfacePtrAndBind()); |
| 63 }, | 62 }, |
| 64 | 63 |
| 65 /** | 64 /** |
| 66 * Gets an array of currently detectable devices from the Adapter service. | 65 * Gets an array of currently detectable devices from the Adapter service. |
| 67 * @return {!Array<!interfaces.BluetoothDevice.DeviceInfo>} | 66 * @return {!Array<!interfaces.BluetoothDevice.DeviceInfo>} |
| 68 */ | 67 */ |
| 69 getDevices: function() { | 68 getDevices: function() { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 90 var AdapterClient = function(adapterBroker) { | 89 var AdapterClient = function(adapterBroker) { |
| 91 this.adapterBroker_ = adapterBroker; | 90 this.adapterBroker_ = adapterBroker; |
| 92 }; | 91 }; |
| 93 | 92 |
| 94 AdapterClient.prototype = { | 93 AdapterClient.prototype = { |
| 95 /** | 94 /** |
| 96 * Fires deviceadded event. | 95 * Fires deviceadded event. |
| 97 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | 96 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 98 */ | 97 */ |
| 99 deviceAdded: function(deviceInfo) { | 98 deviceAdded: function(deviceInfo) { |
| 100 var event = new CustomEvent('deviceadded', { | 99 var event = |
| 101 detail: { | 100 new CustomEvent('deviceadded', {detail: {deviceInfo: deviceInfo}}); |
| 102 deviceInfo: deviceInfo | |
| 103 } | |
| 104 }); | |
| 105 this.adapterBroker_.dispatchEvent(event); | 101 this.adapterBroker_.dispatchEvent(event); |
| 106 }, | 102 }, |
| 107 | 103 |
| 108 /** | 104 /** |
| 109 * Fires deviceremoved event. | 105 * Fires deviceremoved event. |
| 110 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | 106 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 111 */ | 107 */ |
| 112 deviceRemoved: function(deviceInfo) { | 108 deviceRemoved: function(deviceInfo) { |
| 113 var event = new CustomEvent('deviceremoved', { | 109 var event = |
| 114 detail: { | 110 new CustomEvent('deviceremoved', {detail: {deviceInfo: deviceInfo}}); |
| 115 deviceInfo: deviceInfo | |
| 116 } | |
| 117 }); | |
| 118 this.adapterBroker_.dispatchEvent(event); | 111 this.adapterBroker_.dispatchEvent(event); |
| 119 }, | 112 }, |
| 120 | 113 |
| 121 /** | 114 /** |
| 122 * Fires devicechanged event. | 115 * Fires devicechanged event. |
| 123 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | 116 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 124 */ | 117 */ |
| 125 deviceChanged: function(deviceInfo) { | 118 deviceChanged: function(deviceInfo) { |
| 126 var event = new CustomEvent('devicechanged', { | 119 var event = |
| 127 detail: { | 120 new CustomEvent('devicechanged', {detail: {deviceInfo: deviceInfo}}); |
| 128 deviceInfo: deviceInfo | |
| 129 } | |
| 130 }); | |
| 131 this.adapterBroker_.dispatchEvent(event); | 121 this.adapterBroker_.dispatchEvent(event); |
| 132 } | 122 } |
| 133 }; | 123 }; |
| 134 | 124 |
| 135 var adapterBroker = null; | 125 var adapterBroker = null; |
| 136 | 126 |
| 137 /** | 127 /** |
| 138 * Initializes an AdapterBroker if one doesn't exist. | 128 * Initializes an AdapterBroker if one doesn't exist. |
| 139 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, | 129 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, |
| 140 * rejects if Bluetooth is not supported. | 130 * rejects if Bluetooth is not supported. |
| 141 */ | 131 */ |
| 142 function getAdapterBroker() { | 132 function getAdapterBroker() { |
| 143 if (adapterBroker) return Promise.resolve(adapterBroker); | 133 if (adapterBroker) |
| 134 return Promise.resolve(adapterBroker); |
| 144 | 135 |
| 145 return interfaces.setupInterfaces().then(function(adapter) { | 136 return interfaces.setupInterfaces() |
| 146 var adapterFactory = new interfaces.BluetoothAdapter.AdapterFactoryPtr( | 137 .then(function(adapter) { |
| 147 interfaces.FrameInterfaces.getInterface( | 138 var adapterFactory = |
| 148 interfaces.BluetoothAdapter.AdapterFactory.name)); | 139 new interfaces.BluetoothAdapter.AdapterFactoryPtr( |
| 140 interfaces.FrameInterfaces.getInterface( |
| 141 interfaces.BluetoothAdapter.AdapterFactory.name)); |
| 149 | 142 |
| 150 // Get an Adapter service. | 143 // Get an Adapter service. |
| 151 return adapterFactory.getAdapter(); | 144 return adapterFactory.getAdapter(); |
| 152 }).then(function(response) { | 145 }) |
| 153 if (!response.adapter.ptr.isBound()) { | 146 .then(function(response) { |
| 154 throw new Error('Bluetooth Not Supported on this platform.'); | 147 if (!response.adapter.ptr.isBound()) { |
| 155 } | 148 throw new Error('Bluetooth Not Supported on this platform.'); |
| 149 } |
| 156 | 150 |
| 157 adapterBroker = new AdapterBroker(response.adapter); | 151 adapterBroker = new AdapterBroker(response.adapter); |
| 158 return adapterBroker; | 152 return adapterBroker; |
| 159 }); | 153 }); |
| 160 } | 154 } |
| 161 | 155 |
| 162 return { | 156 return { |
| 163 getAdapterBroker: getAdapterBroker, | 157 getAdapterBroker: getAdapterBroker, |
| 164 }; | 158 }; |
| 165 }); | 159 }); |
| OLD | NEW |