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

Side by Side Diff: chrome/browser/resources/settings/bluetooth_page/bluetooth_subpage.js

Issue 2724533006: MD Settings: Bluetooth: Clean up observers and discovery logic (Closed)
Patch Set: Created 3 years, 9 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
« no previous file with comments | « chrome/browser/resources/settings/bluetooth_page/bluetooth_subpage.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 * @fileoverview 6 * @fileoverview
7 * 'settings-bluetooth-subpage' is the settings subpage for managing bluetooth 7 * 'settings-bluetooth-subpage' is the settings subpage for managing bluetooth
8 * properties and devices. 8 * properties and devices.
9 */ 9 */
10 10
11 // NOTE(dbeam): even though this behavior is only used privately, it must 11 // NOTE(dbeam): even though this behavior is only used privately, it must
12 // be globally accessible for Closure's --polymer_pass to compile happily. 12 // be globally accessible for Closure's --polymer_pass to compile happily.
13 13
14 Polymer({ 14 Polymer({
15 is: 'settings-bluetooth-subpage', 15 is: 'settings-bluetooth-subpage',
16 16
17 behaviors: [ 17 behaviors: [
18 I18nBehavior, 18 I18nBehavior,
19 CrScrollableBehavior, 19 CrScrollableBehavior,
20 settings.RouteObserverBehavior, 20 settings.RouteObserverBehavior,
21 ], 21 ],
22 22
23 properties: { 23 properties: {
24 /** Reflects the bluetooth-page property. */ 24 /** Reflects the bluetooth-page property. */
25 bluetoothEnabled: { 25 bluetoothEnabled: {
26 type: Boolean, 26 type: Boolean,
27 observer: 'bluetoothEnabledChanged_',
28 notify: true, 27 notify: true,
29 }, 28 },
30 29
31 /** 30 /**
32 * The bluetooth adapter state, cached by bluetooth-page. 31 * The bluetooth adapter state, cached by bluetooth-page.
33 * @type {!chrome.bluetooth.AdapterState|undefined} 32 * @type {!chrome.bluetooth.AdapterState|undefined}
34 */ 33 */
35 adapterState: Object, 34 adapterState: Object,
36 35
37 /** Informs bluetooth-page whether to show the spinner in the header. */ 36 /** Informs bluetooth-page whether to show the spinner in the header. */
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 }, 140 },
142 }, 141 },
143 142
144 observers: [ 143 observers: [
145 'adapterStateChanged_(adapterState.*)', 144 'adapterStateChanged_(adapterState.*)',
146 'deviceListChanged_(deviceList_.*)', 145 'deviceListChanged_(deviceList_.*)',
147 ], 146 ],
148 147
149 /** 148 /**
150 * Listener for chrome.bluetooth.onBluetoothDeviceAdded/Changed events. 149 * Listener for chrome.bluetooth.onBluetoothDeviceAdded/Changed events.
151 * @type {function(!chrome.bluetooth.Device)|undefined} 150 * @type {?function(!chrome.bluetooth.Device)}
152 * @private 151 * @private
153 */ 152 */
154 bluetoothDeviceUpdatedListener_: undefined, 153 bluetoothDeviceUpdatedListener_: null,
155 154
156 /** 155 /**
157 * Listener for chrome.bluetooth.onBluetoothDeviceRemoved events. 156 * Listener for chrome.bluetooth.onBluetoothDeviceRemoved events.
158 * @type {function(!chrome.bluetooth.Device)|undefined} 157 * @type {?function(!chrome.bluetooth.Device)}
159 * @private 158 * @private
160 */ 159 */
161 bluetoothDeviceRemovedListener_: undefined, 160 bluetoothDeviceRemovedListener_: null,
162 161
163 /** @override */ 162 /** @override */
164 attached: function() { 163 attached: function() {
165 this.bluetoothDeviceUpdatedListener_ = 164 this.bluetoothDeviceUpdatedListener_ =
165 this.bluetoothDeviceUpdatedListener_ ||
166 this.onBluetoothDeviceUpdated_.bind(this); 166 this.onBluetoothDeviceUpdated_.bind(this);
167 this.bluetooth.onDeviceAdded.addListener( 167 this.bluetooth.onDeviceAdded.addListener(
168 this.bluetoothDeviceUpdatedListener_); 168 this.bluetoothDeviceUpdatedListener_);
169 this.bluetooth.onDeviceChanged.addListener( 169 this.bluetooth.onDeviceChanged.addListener(
170 this.bluetoothDeviceUpdatedListener_); 170 this.bluetoothDeviceUpdatedListener_);
171 171
172 this.bluetoothDeviceRemovedListener_ = 172 this.bluetoothDeviceRemovedListener_ =
173 this.bluetoothDeviceRemovedListener_ ||
173 this.onBluetoothDeviceRemoved_.bind(this); 174 this.onBluetoothDeviceRemoved_.bind(this);
174 this.bluetooth.onDeviceRemoved.addListener( 175 this.bluetooth.onDeviceRemoved.addListener(
175 this.bluetoothDeviceRemovedListener_); 176 this.bluetoothDeviceRemovedListener_);
176 }, 177 },
177 178
178 /** @override */ 179 /** @override */
179 detached: function() { 180 detached: function() {
180 if (this.bluetoothAdapterStateChangedListener_) { 181 this.bluetooth.onDeviceAdded.removeListener(
181 this.bluetooth.onAdapterStateChanged.removeListener( 182 assert(this.bluetoothDeviceUpdatedListener_));
182 this.bluetoothAdapterStateChangedListener_); 183 this.bluetooth.onDeviceChanged.removeListener(
183 } 184 assert(this.bluetoothDeviceUpdatedListener_));
184 if (this.bluetoothDeviceUpdatedListener_) { 185 this.bluetooth.onDeviceRemoved.removeListener(
185 this.bluetooth.onDeviceAdded.removeListener( 186 assert(this.bluetoothDeviceRemovedListener_));
186 this.bluetoothDeviceUpdatedListener_);
187 this.bluetooth.onDeviceChanged.removeListener(
188 this.bluetoothDeviceUpdatedListener_);
189 }
190 }, 187 },
191 188
192 /** 189 /**
193 * settings.RouteObserverBehavior 190 * settings.RouteObserverBehavior
194 * @param {!settings.Route} route 191 * @param {!settings.Route} route
195 * @protected 192 * @protected
196 */ 193 */
197 currentRouteChanged: function(route) { 194 currentRouteChanged: function(route) {
198 if (route == settings.Route.BLUETOOTH_DEVICES) { 195 this.updateDiscovery_();
199 if (this.bluetoothEnabled)
200 this.startDiscovery_();
201 } else {
202 this.stopDiscovery_();
203 }
204 }, 196 },
205 197
206 /** @private */ 198 /** @private */
207 computeShowSpinner_: function() { 199 computeShowSpinner_: function() {
208 return !this.dialogId_ && this.get('adapterState.discovering'); 200 return !this.dialogId_ && this.get('adapterState.discovering');
209 }, 201 },
210 202
211 /** @private */ 203 /** @private */
212 adapterStateChanged_: function() { 204 adapterStateChanged_: function() {
205 this.updateDiscovery_();
213 this.updateDeviceList_(); 206 this.updateDeviceList_();
214 }, 207 },
215 208
216 /** @private */ 209 /** @private */
217 bluetoothEnabledChanged_: function() {
218 if (settings.getCurrentRoute() == settings.Route.BLUETOOTH_DEVICES &&
219 this.bluetoothEnabled) {
220 this.startDiscovery_();
221 } else {
222 this.stopDiscovery_();
223 }
224 },
225
226 /** @private */
227 deviceListChanged_: function() { 210 deviceListChanged_: function() {
228 this.saveScroll(this.$.pairedDevices); 211 this.saveScroll(this.$.pairedDevices);
229 this.saveScroll(this.$.unpairedDevices); 212 this.saveScroll(this.$.unpairedDevices);
230 this.pairedDeviceList_ = this.deviceList_.filter(function(device) { 213 this.pairedDeviceList_ = this.deviceList_.filter(function(device) {
231 return !!device.paired || !!device.connecting; 214 return !!device.paired || !!device.connecting;
232 }); 215 });
233 this.unpairedDeviceList_ = this.deviceList_.filter(function(device) { 216 this.unpairedDeviceList_ = this.deviceList_.filter(function(device) {
234 return !device.paired; 217 return !device.paired;
235 }); 218 });
236 this.updateScrollableContents(); 219 this.updateScrollableContents();
237 this.restoreScroll(this.$.unpairedDevices); 220 this.restoreScroll(this.$.unpairedDevices);
238 this.restoreScroll(this.$.pairedDevices); 221 this.restoreScroll(this.$.pairedDevices);
239 }, 222 },
240 223
241 /** @private */ 224 /** @private */
242 selectedPairedItemChanged_: function() { 225 selectedPairedItemChanged_: function() {
243 if (this.selectedPairedItem_) 226 if (this.selectedPairedItem_)
244 this.connectDevice_(this.selectedPairedItem_); 227 this.connectDevice_(this.selectedPairedItem_);
245 }, 228 },
246 229
247 /** @private */ 230 /** @private */
248 selectedUnpairedItemChanged_: function() { 231 selectedUnpairedItemChanged_: function() {
249 if (this.selectedUnpairedItem_) 232 if (this.selectedUnpairedItem_)
250 this.connectDevice_(this.selectedUnpairedItem_); 233 this.connectDevice_(this.selectedUnpairedItem_);
251 }, 234 },
252 235
236 /** @private */
237 updateDiscovery_: function() {
238 if (!this.adapterState || !this.adapterState.powered)
239 return;
240 if (settings.getCurrentRoute() == settings.Route.BLUETOOTH_DEVICES)
241 this.startDiscovery_();
242 else
243 this.stopDiscovery_();
244 },
245
253 /** 246 /**
254 * If bluetooth is enabled, request the complete list of devices and update 247 * If bluetooth is enabled, request the complete list of devices and update
255 * this.deviceList_. 248 * this.deviceList_.
256 * @private 249 * @private
257 */ 250 */
258 updateDeviceList_: function() { 251 updateDeviceList_: function() {
259 if (!this.bluetoothEnabled) { 252 if (!this.bluetoothEnabled) {
260 this.deviceList_ = []; 253 this.deviceList_ = [];
261 return; 254 return;
262 } 255 }
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 Polymer.dom.flush(); 460 Polymer.dom.flush();
468 this.$.deviceDialog.open(); 461 this.$.deviceDialog.open();
469 }, 462 },
470 463
471 /** @private */ 464 /** @private */
472 onDialogClosed_: function() { 465 onDialogClosed_: function() {
473 this.dialogId_ = ''; 466 this.dialogId_ = '';
474 this.pairingDevice_ = undefined; 467 this.pairingDevice_ = undefined;
475 }, 468 },
476 }); 469 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/settings/bluetooth_page/bluetooth_subpage.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698