| 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..683a81e84f5cff0be610d8bdac4a79b3f7d323e5
|
| --- /dev/null
|
| +++ b/chrome/browser/resources/bluetooth_internals/adapter_broker.js
|
| @@ -0,0 +1,126 @@
|
| +// 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>}
|
| + */
|
| + getDevices: function() {
|
| + return this.adapter_.getDevices();
|
| + },
|
| +
|
| + /**
|
| + * Gets the current state of the Adapter.
|
| + * @return {interfaces.BluetoothAdapter.AdapterInfo}
|
| + */
|
| + 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 Event('deviceadded');
|
| + event.deviceInfo = deviceInfo;
|
| + this.adapterBroker_.dispatchEvent(event);
|
| + },
|
| +
|
| + /**
|
| + * Fires deviceremoved event.
|
| + * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
|
| + */
|
| + deviceRemoved: function(deviceInfo) {
|
| + var event = new Event('deviceremoved');
|
| + event.deviceInfo = deviceInfo;
|
| + this.adapterBroker_.dispatchEvent(event);
|
| + },
|
| +
|
| + /**
|
| + * Fires devicechanged event.
|
| + * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
|
| + */
|
| + deviceChanged: function(deviceInfo) {
|
| + console.log(new Date(), deviceInfo);
|
| +
|
| + var event = new Event('devicechanged');
|
| + event.deviceInfo = deviceInfo;
|
| + this.adapterBroker_.dispatchEvent(event);
|
| + }
|
| + };
|
| +
|
| + var adapterBroker = null;
|
| +
|
| + /**
|
| + * Initializes an AdapterBroker if one doesn't exist.
|
| + * @return {Promise<AdapterBroker>} resolves if Mojo interfaces are
|
| + * initialized, rejects if Bluetooth is not supported.
|
| + */
|
| + function getAdapterBroker() {
|
| + if (adapterBroker) {
|
| + return Promise.resolve(adapterBroker);
|
| + }
|
| +
|
| + return interfaces.initialize().then(function(adapter) {
|
| + // Hook up the instance properties.
|
| + AdapterClient.prototype.__proto__ =
|
| + interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype;
|
| +
|
| + adapterBroker = new AdapterBroker(adapter);
|
| + return adapterBroker;
|
| + });
|
| + }
|
| +
|
| + return {
|
| + getAdapterBroker: getAdapterBroker,
|
| + };
|
| +});
|
|
|