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>} | |
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.
| |
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} | |
Dan Beam
2016/11/09 18:00:32
can you use ? or !
mbrunson
2016/11/10 01:02:13
Done.
| |
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 CustomEvent('deviceadded', { | |
74 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
| |
75 deviceInfo: deviceInfo | |
76 } | |
77 }); | |
78 this.adapterBroker_.dispatchEvent(event); | |
79 }, | |
80 | |
81 /** | |
82 * Fires deviceremoved event. | |
83 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | |
84 */ | |
85 deviceRemoved: function(deviceInfo) { | |
86 var event = new CustomEvent('deviceremoved', { | |
87 detail: { | |
88 deviceInfo: deviceInfo | |
89 } | |
90 }); | |
91 this.adapterBroker_.dispatchEvent(event); | |
92 }, | |
93 | |
94 /** | |
95 * Fires devicechanged event. | |
96 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | |
97 */ | |
98 deviceChanged: function(deviceInfo) { | |
99 var event = new CustomEvent('devicechanged', { | |
100 detail: { | |
101 deviceInfo: deviceInfo | |
102 } | |
103 }); | |
104 this.adapterBroker_.dispatchEvent(event); | |
105 } | |
106 }; | |
107 | |
108 var adapterBroker = null; | |
109 | |
110 /** | |
111 * Initializes an AdapterBroker if one doesn't exist. | |
112 * @return {Promise<AdapterBroker>} resolves with AdapterBroker, | |
113 * rejects if Bluetooth is not supported. | |
114 */ | |
115 function getAdapterBroker() { | |
116 if (adapterBroker) { | |
117 return Promise.resolve(adapterBroker); | |
118 } | |
Dan Beam
2016/11/09 18:00:32
no curlies
mbrunson
2016/11/10 01:02:13
Done.
| |
119 | |
120 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.
| |
121 // Hook up the instance properties. | |
122 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
| |
123 interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype; | |
124 | |
125 var adapterFactory = interfaces.Connection.bindHandleToProxy( | |
126 interfaces.FrameInterfaces.getInterface( | |
127 interfaces.BluetoothAdapter.AdapterFactory.name), | |
128 interfaces.BluetoothAdapter.AdapterFactory); | |
129 | |
130 // Get an Adapter service. | |
131 return adapterFactory.getAdapter(); | |
132 }).then(function(response) { | |
133 if (!response.adapter) { | |
134 throw new Error('Bluetooth Not Supported on this platform.'); | |
135 } | |
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
| |
136 | |
137 var adapter = interfaces.Connection.bindHandleToProxy( | |
138 response.adapter, | |
139 interfaces.BluetoothAdapter.Adapter); | |
140 | |
141 adapterBroker = new AdapterBroker(adapter); | |
142 return adapterBroker; | |
143 }); | |
144 } | |
145 | |
146 return { | |
147 getAdapterBroker: getAdapterBroker, | |
148 }; | |
149 }); | |
OLD | NEW |