Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: chrome/browser/resources/bluetooth_internals/adapter_broker.js

Issue 2564113003: bluetooth: Add basic scanning to chrome://bluetooth-internals. (Closed)
Patch Set: Update comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * Javascript for AdapterBroker, served from 6 * Javascript for AdapterBroker, served from
7 * chrome://bluetooth-internals/. 7 * chrome://bluetooth-internals/.
8 */ 8 */
9 cr.define('adapter_broker', function() { 9 cr.define('adapter_broker', function() {
10 /** @typedef {interfaces.BluetoothAdapter.Adapter.ptrClass} */
11 var AdapterPtr;
12 /** @typedef {interfaces.BluetoothDevice.Device.ptrClass} */
13 var DevicePtr;
14 /** @typedef {interfaces.BluetoothAdapter.DiscoverySession.ptrClass} */
15 var DiscoverySessionPtr;
16
10 /** 17 /**
11 * The proxy class of an adapter and router of adapter events. 18 * The proxy class of an adapter and router of adapter events.
12 * Exposes an EventTarget interface that allows other object to subscribe to 19 * Exposes an EventTarget interface that allows other object to subscribe to
13 * to specific AdapterClient events. 20 * to specific AdapterClient events.
14 * Provides proxy access to Adapter functions. Converts parameters to Mojo 21 * Provides proxy access to Adapter functions. Converts parameters to Mojo
15 * handles and back when necessary. 22 * handles and back when necessary.
16 * @constructor 23 * @constructor
17 * @extends {cr.EventTarget} 24 * @extends {cr.EventTarget}
18 * @param {!interfaces.BluetoothAdapter.AdapterPtr} adapter 25 * @param {!AdapterPtr} adapter
19 */ 26 */
20 var AdapterBroker = function(adapter) { 27 var AdapterBroker = function(adapter) {
21 this.adapter_ = adapter; 28 this.adapter_ = adapter;
22 this.adapterClient_ = new AdapterClient(this); 29 this.adapterClient_ = new AdapterClient(this);
23 this.setClient(this.adapterClient_); 30 this.setClient(this.adapterClient_);
24 }; 31 };
25 32
26 AdapterBroker.prototype = { 33 AdapterBroker.prototype = {
27 __proto__: cr.EventTarget.prototype, 34 __proto__: cr.EventTarget.prototype,
28 35
29 /** 36 /**
30 * Creates a GATT connection to the device with |address|. 37 * Creates a GATT connection to the device with |address|.
31 * @param {string} address 38 * @param {string} address
32 * @return {!Promise<!interfaces.BluetoothDevice.DevicePtr>} 39 * @return {!Promise<!DevicePtr>}
33 */ 40 */
34 connectToDevice: function(address) { 41 connectToDevice: function(address) {
35 return this.adapter_.connectToDevice(address).then(function(response) { 42 return this.adapter_.connectToDevice(address).then(function(response) {
36 if (response.result != 43 if (response.result !=
37 interfaces.BluetoothAdapter.ConnectResult.SUCCESS) { 44 interfaces.BluetoothAdapter.ConnectResult.SUCCESS) {
38 // TODO(crbug.com/663394): Replace with more descriptive error 45 // TODO(crbug.com/663394): Replace with more descriptive error
39 // messages. 46 // messages.
40 var ConnectResult = interfaces.BluetoothAdapter.ConnectResult; 47 var ConnectResult = interfaces.BluetoothAdapter.ConnectResult;
41 var errorString = Object.keys(ConnectResult).find(function(key) { 48 var errorString = Object.keys(ConnectResult).find(function(key) {
42 return ConnectResult[key] === response.result; 49 return ConnectResult[key] === response.result;
43 }); 50 });
44 51
45 throw new Error(errorString); 52 throw new Error(errorString);
46 } 53 }
47 54
48 return response.device; 55 return response.device;
49 }); 56 });
50 }, 57 },
51 58
52 /** 59 /**
60 * Gets an array of currently detectable devices from the Adapter service.
61 * @return {!Array<!interfaces.BluetoothDevice.DeviceInfo>}
62 */
63 getDevices: function() {
64 return this.adapter_.getDevices();
65 },
66
67 /**
68 * Gets the current state of the Adapter.
69 * @return {!interfaces.BluetoothAdapter.AdapterInfo}
70 */
71 getInfo: function() {
72 return this.adapter_.getInfo();
73 },
74
75 /**
53 * Sets client of Adapter service. 76 * Sets client of Adapter service.
54 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient 77 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient
55 */ 78 */
56 setClient: function(adapterClient) { 79 setClient: function(adapterClient) {
57 adapterClient.binding = new interfaces.Bindings.Binding( 80 adapterClient.binding = new interfaces.Bindings.Binding(
58 interfaces.BluetoothAdapter.AdapterClient, 81 interfaces.BluetoothAdapter.AdapterClient,
59 adapterClient); 82 adapterClient);
60 83
61 this.adapter_.setClient( 84 this.adapter_.setClient(
62 adapterClient.binding.createInterfacePtrAndBind()); 85 adapterClient.binding.createInterfacePtrAndBind());
63 }, 86 },
64 87
65 /** 88 /**
66 * Gets an array of currently detectable devices from the Adapter service. 89 * Requests the adapter to start a new discovery session.
67 * @return {!Array<!interfaces.BluetoothDevice.DeviceInfo>} 90 * @return {!Promise<!DiscoverySessionPtr>}
68 */ 91 */
69 getDevices: function() { 92 startDiscoverySession: function() {
70 return this.adapter_.getDevices(); 93 return this.adapter_.startDiscoverySession().then(function(response) {
94 if (!response.session.ptr.isBound()) {
95 throw new Error('Discovery session failed to start');
96 }
97
98 return response.session;
99 });
71 }, 100 },
72
73 /**
74 * Gets the current state of the Adapter.
75 * @return {!interfaces.BluetoothAdapter.AdapterInfo}
76 */
77 getInfo: function() {
78 return this.adapter_.getInfo();
79 }
80 }; 101 };
81 102
82 /** 103 /**
83 * The implementation of AdapterClient in 104 * The implementation of AdapterClient in
84 * device/bluetooth/public/interfaces/adapter.mojom. Dispatches events 105 * device/bluetooth/public/interfaces/adapter.mojom. Dispatches events
85 * through AdapterBroker to notify client objects of changes to the Adapter 106 * through AdapterBroker to notify client objects of changes to the Adapter
86 * service. 107 * service.
87 * @constructor 108 * @constructor
88 * @param {!AdapterBroker} adapterBroker Broker to dispatch events through. 109 * @param {!AdapterBroker} adapterBroker Broker to dispatch events through.
89 */ 110 */
90 var AdapterClient = function(adapterBroker) { 111 var AdapterClient = function(adapterBroker) {
91 this.adapterBroker_ = adapterBroker; 112 this.adapterBroker_ = adapterBroker;
92 }; 113 };
93 114
94 AdapterClient.prototype = { 115 AdapterClient.prototype = {
95 /** 116 /**
117 * Fires adapterchanged event.
118 * @param {boolean} discovering
119 */
120 discoveringChanged: function(discovering) {
121 var event = new CustomEvent('adapterchanged', {
dpapad 2017/01/05 23:30:33 Is this the only case where an 'adapterchanged' ev
mbrunson 2017/01/06 01:18:56 There are a batch of these change events concernin
122 detail: {
123 property: 'discovering',
124 value: discovering,
125 }
126 });
127 this.adapterBroker_.dispatchEvent(event);
128 },
129
130 /**
96 * Fires deviceadded event. 131 * Fires deviceadded event.
97 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo 132 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
98 */ 133 */
99 deviceAdded: function(deviceInfo) { 134 deviceAdded: function(deviceInfo) {
100 var event = new CustomEvent('deviceadded', { 135 var event = new CustomEvent('deviceadded', {
101 detail: { 136 detail: {
102 deviceInfo: deviceInfo 137 deviceInfo: deviceInfo
103 } 138 }
104 }); 139 });
105 this.adapterBroker_.dispatchEvent(event); 140 this.adapterBroker_.dispatchEvent(event);
106 }, 141 },
107 142
108 /** 143 /**
109 * Fires deviceremoved event. 144 * Fires devicechanged event.
110 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo 145 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
111 */ 146 */
112 deviceRemoved: function(deviceInfo) { 147 deviceChanged: function(deviceInfo) {
113 var event = new CustomEvent('deviceremoved', { 148 var event = new CustomEvent('devicechanged', {
dpapad 2017/01/05 23:30:33 Nit (optional): I think is more readable if you se
mbrunson 2017/01/06 01:18:56 If I do this I'd want to do this for all the custo
114 detail: { 149 detail: {
115 deviceInfo: deviceInfo 150 deviceInfo: deviceInfo
116 } 151 }
117 }); 152 });
118 this.adapterBroker_.dispatchEvent(event); 153 this.adapterBroker_.dispatchEvent(event);
119 }, 154 },
120 155
121 /** 156 /**
122 * Fires devicechanged event. 157 * Fires deviceremoved event.
123 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo 158 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
124 */ 159 */
125 deviceChanged: function(deviceInfo) { 160 deviceRemoved: function(deviceInfo) {
126 var event = new CustomEvent('devicechanged', { 161 var event = new CustomEvent('deviceremoved', {
127 detail: { 162 detail: {
128 deviceInfo: deviceInfo 163 deviceInfo: deviceInfo
129 } 164 }
130 }); 165 });
131 this.adapterBroker_.dispatchEvent(event); 166 this.adapterBroker_.dispatchEvent(event);
132 } 167 },
133 }; 168 };
134 169
135 var adapterBroker = null; 170 var adapterBroker = null;
136 171
137 /** 172 /**
138 * Initializes an AdapterBroker if one doesn't exist. 173 * Initializes an AdapterBroker if one doesn't exist.
139 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, 174 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker,
140 * rejects if Bluetooth is not supported. 175 * rejects if Bluetooth is not supported.
141 */ 176 */
142 function getAdapterBroker() { 177 function getAdapterBroker() {
(...skipping 13 matching lines...) Expand all
156 191
157 adapterBroker = new AdapterBroker(response.adapter); 192 adapterBroker = new AdapterBroker(response.adapter);
158 return adapterBroker; 193 return adapterBroker;
159 }); 194 });
160 } 195 }
161 196
162 return { 197 return {
163 getAdapterBroker: getAdapterBroker, 198 getAdapterBroker: getAdapterBroker,
164 }; 199 };
165 }); 200 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698