Chromium Code Reviews| Index: chrome/browser/resources/bluetooth_internals/adapter_broker.js |
| diff --git a/chrome/browser/resources/bluetooth_internals/adapter_broker.js b/chrome/browser/resources/bluetooth_internals/adapter_broker.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c17c89fe2a4806ce63a0155d4f5fba024ef10c33 |
| --- /dev/null |
| +++ b/chrome/browser/resources/bluetooth_internals/adapter_broker.js |
| @@ -0,0 +1,149 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * Javascript for AdapterBroker, served from |
| + * chrome://bluetooth-internals/. |
| + */ |
| +cr.define('adapter_broker', function() { |
| + /** |
| + * The proxy class of an adapter and router of adapter events. |
| + * Exposes an EventTarget interface that allows other object to subscribe to |
| + * to specific AdapterClient events. |
| + * Provides proxy access to Adapter functions. Converts parameters to Mojo |
| + * handles and back when necessary. |
| + * @constructor |
| + * @extends {cr.EventTarget} |
| + * @param {!interfaces.BluetoothAdapter.Adapter.proxyClass} adapter |
| + */ |
| + var AdapterBroker = function(adapter) { |
| + this.adapter_ = adapter; |
| + this.adapterClient_ = new AdapterClient(this); |
| + this.setClient(this.adapterClient_); |
| + }; |
| + |
| + AdapterBroker.prototype = { |
| + __proto__: cr.EventTarget.prototype, |
| + |
| + /** |
| + * Sets client of Adapter service. |
| + * @param {interfaces.BluetoothAdapter.AdapterClient} adapterClient |
| + */ |
| + setClient: function(adapterClient) { |
| + this.adapter_.setClient(interfaces.Connection.bindStubDerivedImpl( |
| + adapterClient)); |
| + }, |
| + |
| + /** |
| + * Gets an array of currently detectable devices from the Adapter service. |
| + * @return {Array<interfaces.BluetoothDevice.DeviceInfo>} |
|
Dan Beam
2016/11/09 18:00:32
can we add more !, i.e. !Array<!interfaces.Bluetoo
mbrunson
2016/11/10 01:02:13
Done.
|
| + */ |
| + getDevices: function() { |
| + return this.adapter_.getDevices(); |
| + }, |
| + |
| + /** |
| + * Gets the current state of the Adapter. |
| + * @return {interfaces.BluetoothAdapter.AdapterInfo} |
|
Dan Beam
2016/11/09 18:00:32
can you use ? or !
mbrunson
2016/11/10 01:02:13
Done.
|
| + */ |
| + getInfo: function() { |
| + return this.adapter_.getInfo(); |
| + } |
| + }; |
| + |
| + /** |
| + * The implementation of AdapterClient in |
| + * device/bluetooth/public/interfaces/adapter.mojom. Dispatches events |
| + * through AdapterBroker to notify client objects of changes to the Adapter |
| + * service. |
| + * @constructor |
| + * @param {!AdapterBroker} adapterBroker Broker to dispatch events through. |
| + */ |
| + var AdapterClient = function(adapterBroker) { |
| + this.adapterBroker_ = adapterBroker; |
| + }; |
| + |
| + AdapterClient.prototype = { |
| + /** |
| + * Fires deviceadded event. |
| + * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| + */ |
| + deviceAdded: function(deviceInfo) { |
| + var event = new CustomEvent('deviceadded', { |
| + detail: { |
|
Dan Beam
2016/11/09 18:00:31
don't you want just new CustomEvent('deviceadded',
mbrunson
2016/11/10 01:02:13
CustomEvent spec [1] requires second parameter dic
|
| + deviceInfo: deviceInfo |
| + } |
| + }); |
| + this.adapterBroker_.dispatchEvent(event); |
| + }, |
| + |
| + /** |
| + * Fires deviceremoved event. |
| + * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| + */ |
| + deviceRemoved: function(deviceInfo) { |
| + var event = new CustomEvent('deviceremoved', { |
| + detail: { |
| + deviceInfo: deviceInfo |
| + } |
| + }); |
| + this.adapterBroker_.dispatchEvent(event); |
| + }, |
| + |
| + /** |
| + * Fires devicechanged event. |
| + * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| + */ |
| + deviceChanged: function(deviceInfo) { |
| + var event = new CustomEvent('devicechanged', { |
| + detail: { |
| + deviceInfo: deviceInfo |
| + } |
| + }); |
| + this.adapterBroker_.dispatchEvent(event); |
| + } |
| + }; |
| + |
| + var adapterBroker = null; |
| + |
| + /** |
| + * Initializes an AdapterBroker if one doesn't exist. |
| + * @return {Promise<AdapterBroker>} resolves with AdapterBroker, |
| + * rejects if Bluetooth is not supported. |
| + */ |
| + function getAdapterBroker() { |
| + if (adapterBroker) { |
| + return Promise.resolve(adapterBroker); |
| + } |
|
Dan Beam
2016/11/09 18:00:32
no curlies
mbrunson
2016/11/10 01:02:13
Done.
|
| + |
| + return interfaces.importInterfaces().then(function(adapter) { |
|
Dan Beam
2016/11/09 18:00:32
can we call this like setupInterfaces()?
mbrunson
2016/11/10 01:02:13
Done.
|
| + // Hook up the instance properties. |
| + AdapterClient.prototype.__proto__ = |
|
Dan Beam
2016/11/09 18:00:32
why is this necessary?
mbrunson
2016/11/10 01:02:13
The Mojo connection libraries [1] require client i
|
| + interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype; |
| + |
| + var adapterFactory = interfaces.Connection.bindHandleToProxy( |
| + interfaces.FrameInterfaces.getInterface( |
| + interfaces.BluetoothAdapter.AdapterFactory.name), |
| + interfaces.BluetoothAdapter.AdapterFactory); |
| + |
| + // Get an Adapter service. |
| + return adapterFactory.getAdapter(); |
| + }).then(function(response) { |
| + if (!response.adapter) { |
| + throw new Error('Bluetooth Not Supported on this platform.'); |
| + } |
|
Dan Beam
2016/11/09 18:00:32
no curlies
mbrunson
2016/11/10 01:02:13
This doesn't fit on one line.
https://engdoc.corp
Dan Beam
2016/11/10 03:22:54
yeah, that doc deals with ES6 (we're using ES5 in
|
| + |
| + var adapter = interfaces.Connection.bindHandleToProxy( |
| + response.adapter, |
| + interfaces.BluetoothAdapter.Adapter); |
| + |
| + adapterBroker = new AdapterBroker(adapter); |
| + return adapterBroker; |
| + }); |
| + } |
| + |
| + return { |
| + getAdapterBroker: getAdapterBroker, |
| + }; |
| +}); |