OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Javascript for AdapterBroker, served from |
| 7 * chrome://bluetooth-internals/. |
| 8 */ |
| 9 cr.define('adapter_broker', function() { |
| 10 /** |
| 11 * The proxy class of an adapter and router of adapter events. |
| 12 * Exposes an EventTarget interface that allows other object to subscribe to |
| 13 * to specific AdapterClient events. |
| 14 * Provides proxy access to Adapter functions. Converts parameters to Mojo |
| 15 * handles and back when necessary. |
| 16 * @constructor |
| 17 * @extends {cr.EventTarget} |
| 18 * @param {!interfaces.BluetoothAdapter.Adapter.proxyClass} adapter |
| 19 */ |
| 20 var AdapterBroker = function(adapter) { |
| 21 this.adapter_ = adapter; |
| 22 this.adapterClient_ = new AdapterClient(this); |
| 23 this.setClient(this.adapterClient_); |
| 24 }; |
| 25 |
| 26 AdapterBroker.prototype = { |
| 27 __proto__: cr.EventTarget.prototype, |
| 28 |
| 29 /** |
| 30 * Sets client of Adapter service. |
| 31 * @param {interfaces.BluetoothAdapter.AdapterClient} adapterClient |
| 32 */ |
| 33 setClient: function(adapterClient) { |
| 34 this.adapter_.setClient(interfaces.Connection.bindStubDerivedImpl( |
| 35 adapterClient)); |
| 36 }, |
| 37 |
| 38 /** |
| 39 * Gets an array of currently detectable devices from the Adapter service. |
| 40 * @return {Array<interfaces.BluetoothDevice.DeviceInfo>} |
| 41 */ |
| 42 getDevices: function() { |
| 43 return this.adapter_.getDevices(); |
| 44 }, |
| 45 |
| 46 /** |
| 47 * Gets the current state of the Adapter. |
| 48 * @return {interfaces.BluetoothAdapter.AdapterInfo} |
| 49 */ |
| 50 getInfo: function() { |
| 51 return this.adapter_.getInfo(); |
| 52 } |
| 53 }; |
| 54 |
| 55 /** |
| 56 * The implementation of AdapterClient in |
| 57 * device/bluetooth/public/interfaces/adapter.mojom. Dispatches events |
| 58 * through AdapterBroker to notify client objects of changes to the Adapter |
| 59 * service. |
| 60 * @constructor |
| 61 * @param {!AdapterBroker} adapterBroker Broker to dispatch events through. |
| 62 */ |
| 63 var AdapterClient = function(adapterBroker) { |
| 64 this.adapterBroker_ = adapterBroker; |
| 65 }; |
| 66 |
| 67 AdapterClient.prototype = { |
| 68 /** |
| 69 * Fires deviceadded event. |
| 70 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 71 */ |
| 72 deviceAdded: function(deviceInfo) { |
| 73 var event = new Event('deviceadded'); |
| 74 event.deviceInfo = deviceInfo; |
| 75 this.adapterBroker_.dispatchEvent(event); |
| 76 }, |
| 77 |
| 78 /** |
| 79 * Fires deviceremoved event. |
| 80 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 81 */ |
| 82 deviceRemoved: function(deviceInfo) { |
| 83 var event = new Event('deviceremoved'); |
| 84 event.deviceInfo = deviceInfo; |
| 85 this.adapterBroker_.dispatchEvent(event); |
| 86 }, |
| 87 |
| 88 /** |
| 89 * Fires devicechanged event. |
| 90 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 91 */ |
| 92 deviceChanged: function(deviceInfo) { |
| 93 console.log(new Date(), deviceInfo); |
| 94 |
| 95 var event = new Event('devicechanged'); |
| 96 event.deviceInfo = deviceInfo; |
| 97 this.adapterBroker_.dispatchEvent(event); |
| 98 } |
| 99 }; |
| 100 |
| 101 var adapterBroker = null; |
| 102 |
| 103 /** |
| 104 * Initializes an AdapterBroker if one doesn't exist. |
| 105 * @return {Promise<AdapterBroker>} resolves if Mojo interfaces are |
| 106 * initialized, rejects if Bluetooth is not supported. |
| 107 */ |
| 108 function getAdapterBroker() { |
| 109 if (adapterBroker) { |
| 110 return Promise.resolve(adapterBroker); |
| 111 } |
| 112 |
| 113 return interfaces.initialize().then(function(adapter) { |
| 114 // Hook up the instance properties. |
| 115 AdapterClient.prototype.__proto__ = |
| 116 interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype; |
| 117 |
| 118 adapterBroker = new AdapterBroker(adapter); |
| 119 return adapterBroker; |
| 120 }); |
| 121 } |
| 122 |
| 123 return { |
| 124 getAdapterBroker: getAdapterBroker, |
| 125 }; |
| 126 }); |
OLD | NEW |