Chromium Code Reviews| 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'); | |
|
dpapad
2016/11/04 17:19:59
How about using a CustomEvent instead? Seems more
mbrunson
2016/11/04 22:57:40
Done.
| |
| 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); | |
|
dpapad
2016/11/04 17:19:59
Can we remove this now?
mbrunson
2016/11/04 22:57:40
Done.
| |
| 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 with AdapterBroker, | |
| 106 * rejects if Bluetooth is not supported. | |
| 107 */ | |
| 108 function getAdapterBroker() { | |
| 109 if (adapterBroker) { | |
| 110 return Promise.resolve(adapterBroker); | |
| 111 } | |
| 112 | |
| 113 return interfaces.importInterfaces().then(function(adapter) { | |
| 114 // Hook up the instance properties. | |
| 115 AdapterClient.prototype.__proto__ = | |
| 116 interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype; | |
| 117 | |
| 118 var adapterFactory = interfaces.Connection.bindHandleToProxy( | |
| 119 interfaces.FrameInterfaces.getInterface( | |
| 120 interfaces.BluetoothAdapter.AdapterFactory.name), | |
| 121 interfaces.BluetoothAdapter.AdapterFactory); | |
| 122 | |
| 123 // Get an Adapter service. | |
| 124 return adapterFactory.getAdapter(); | |
| 125 }).then(function(response) { | |
| 126 if (!response.adapter) { | |
| 127 throw new Error('Bluetooth Not Supported on this platform.'); | |
| 128 } | |
| 129 | |
| 130 var adapter = interfaces.Connection.bindHandleToProxy( | |
| 131 response.adapter, | |
| 132 interfaces.BluetoothAdapter.Adapter); | |
| 133 | |
| 134 adapterBroker = new AdapterBroker(adapter); | |
| 135 return adapterBroker; | |
| 136 }); | |
| 137 } | |
| 138 | |
| 139 return { | |
| 140 getAdapterBroker: getAdapterBroker, | |
| 141 }; | |
| 142 }); | |
| OLD | NEW |