Chromium Code Reviews| Index: chrome/browser/resources/settings/bluetooth_page/bluetooth_device_dialog.js |
| diff --git a/chrome/browser/resources/settings/bluetooth_page/bluetooth_device_dialog.js b/chrome/browser/resources/settings/bluetooth_page/bluetooth_device_dialog.js |
| index a5ebfd0931c94c64369bf1a227d78cd9e803b70b..76238a36451eab0853b45a47882df9a49bb35483 100644 |
| --- a/chrome/browser/resources/settings/bluetooth_page/bluetooth_device_dialog.js |
| +++ b/chrome/browser/resources/settings/bluetooth_page/bluetooth_device_dialog.js |
| @@ -4,8 +4,6 @@ |
| cr.exportPath('settings'); |
| -(function() { |
| - |
| var PairingEventType = chrome.bluetoothPrivate.PairingEventType; |
| // NOTE(dbeam): even though these behaviors are only used privately, they must |
| @@ -45,7 +43,8 @@ settings.BluetoothAddDeviceBehavior = { |
| }, |
| }, |
| - /** @type {boolean} */ itemWasFocused_: false, |
| + /** @type {boolean} */ |
| + itemWasFocused_: false, |
| /** @private */ |
| adapterStateChanged_: function() { |
| @@ -67,7 +66,9 @@ settings.BluetoothAddDeviceBehavior = { |
| return; |
| } |
| // Otherwise try again. |
| - setTimeout(function() { this.deviceListChanged_(); }.bind(this), 100); |
| + setTimeout(function() { |
| + this.deviceListChanged_(); |
| + }.bind(this), 100); |
| }, |
| /** @private */ |
| @@ -100,20 +101,33 @@ settings.BluetoothPairDeviceBehavior = { |
| properties: { |
| /** |
| * Current Pairing device. |
| - * @type {?chrome.bluetooth.Device|undefined} |
| + * @type {!chrome.bluetooth.Device|undefined} |
| */ |
| pairingDevice: Object, |
| /** |
| * Current Pairing event. |
| - * @type {?chrome.bluetoothPrivate.PairingEvent|undefined} |
| + * @type {?chrome.bluetoothPrivate.PairingEvent} |
| */ |
| - pairingEvent: Object, |
| + pairingEvent_: { |
| + type: Object, |
| + value: null, |
| + }, |
| /** Pincode or passkey value, used to trigger connect enabled changes. */ |
| pinOrPass: String, |
| /** |
| + * Interface for bluetoothPrivate calls. Set in bluetooth-page. |
| + * @type {BluetoothPrivate} |
| + * @private |
| + */ |
| + bluetoothPrivate: { |
| + type: Object, |
| + value: chrome.bluetoothPrivate, |
| + }, |
| + |
| + /** |
| * @const |
| * @type {!Array<number>} |
| */ |
| @@ -125,13 +139,57 @@ settings.BluetoothPairDeviceBehavior = { |
| }, |
| observers: [ |
| - 'pairingChanged_(pairingDevice, pairingEvent)', |
| + 'pairingChanged_(pairingDevice, pairingEvent_)', |
| ], |
| + /** |
| + * Listener for chrome.bluetoothPrivate.onPairing events. |
| + * @type {?function(!chrome.bluetoothPrivate.PairingEvent)} |
| + * @private |
| + */ |
| + bluetoothPrivateOnPairingListener_: null, |
| + |
| + /** Called when the dialog is opened. Starts listening for pairing events. */ |
| + startPairing: function() { |
|
fukino
2017/01/30 08:44:01
Can we make this function as @private?
stevenjb
2017/01/30 19:19:26
This is actually part of a behavior, and the metho
|
| + if (!this.bluetoothPrivateOnPairingListener_) { |
| + this.bluetoothPrivateOnPairingListener_ = |
| + this.onBluetoothPrivateOnPairing_.bind(this); |
| + this.bluetoothPrivate.onPairing.addListener( |
| + this.bluetoothPrivateOnPairingListener_); |
| + } |
| + }, |
| + |
| + /** Called when the dialog is closed. */ |
| + endPairing: function() { |
|
fukino
2017/01/30 08:44:01
Can we make this function as @private?
stevenjb
2017/01/30 19:19:26
Ditto.
|
| + if (this.bluetoothPrivateOnPairingListener_) { |
| + this.bluetoothPrivate.onPairing.removeListener( |
| + this.bluetoothPrivateOnPairingListener_); |
| + this.bluetoothPrivateOnPairingListener_ = null; |
| + } |
| + this.pairingEvent_ = null; |
| + }, |
| + |
| + /** |
| + * Process bluetoothPrivate.onPairing events. |
| + * @param {!chrome.bluetoothPrivate.PairingEvent} event |
| + * @private |
| + */ |
| + onBluetoothPrivateOnPairing_: function(event) { |
| + if (event.device.address != this.pairingDevice.address) |
|
fukino
2017/01/30 08:44:01
I think we should check the existence of |this.pai
stevenjb
2017/01/30 19:19:26
Yeah, there are some new edge cases here that I di
|
| + return; |
| + if (event.pairing == PairingEventType.KEYS_ENTERED && |
| + event.passkey === undefined && this.pairingEvent_) { |
| + // 'keysEntered' event might not include the updated passkey so preserve |
| + // the current one. |
| + event.passkey = this.pairingEvent_.passkey; |
| + } |
| + this.pairingEvent_ = event; |
| + }, |
| + |
| /** @private */ |
| pairingChanged_: function() { |
| // Auto-close the dialog when pairing completes. |
| - if (this.pairingDevice && this.pairingDevice.connected) { |
| + if (this.pairingDevice.connected) { |
| this.close(); |
| return; |
| } |
| @@ -143,13 +201,11 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| getMessage_: function() { |
| - if (!this.pairingDevice) |
| - return ''; |
| var message; |
| - if (!this.pairingEvent) |
| + if (!this.pairingEvent_) |
| message = 'bluetoothStartConnecting'; |
| else |
| - message = this.getEventDesc_(this.pairingEvent.pairing); |
| + message = this.getEventDesc_(this.pairingEvent_.pairing); |
| return this.i18n(message, this.pairingDevice.name); |
| }, |
| @@ -158,8 +214,8 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| showEnterPincode_: function() { |
| - return !!this.pairingEvent && |
| - this.pairingEvent.pairing == PairingEventType.REQUEST_PINCODE; |
| + return !!this.pairingEvent_ && |
| + this.pairingEvent_.pairing == PairingEventType.REQUEST_PINCODE; |
| }, |
| /** |
| @@ -167,8 +223,8 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| showEnterPasskey_: function() { |
| - return !!this.pairingEvent && |
| - this.pairingEvent.pairing == PairingEventType.REQUEST_PASSKEY; |
| + return !!this.pairingEvent_ && |
| + this.pairingEvent_.pairing == PairingEventType.REQUEST_PASSKEY; |
| }, |
| /** |
| @@ -176,9 +232,9 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| showDisplayPassOrPin_: function() { |
| - if (!this.pairingEvent) |
| + if (!this.pairingEvent_) |
| return false; |
| - var pairing = this.pairingEvent.pairing; |
| + var pairing = this.pairingEvent_.pairing; |
| return ( |
| pairing == PairingEventType.DISPLAY_PINCODE || |
| pairing == PairingEventType.DISPLAY_PASSKEY || |
| @@ -191,8 +247,8 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| showAcceptReject_: function() { |
| - return !!this.pairingEvent && |
| - this.pairingEvent.pairing == PairingEventType.CONFIRM_PASSKEY; |
| + return !!this.pairingEvent_ && |
| + this.pairingEvent_.pairing == PairingEventType.CONFIRM_PASSKEY; |
| }, |
| /** |
| @@ -200,9 +256,9 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| showConnect_: function() { |
| - if (!this.pairingEvent) |
| + if (!this.pairingEvent_) |
| return false; |
| - var pairing = this.pairingEvent.pairing; |
| + var pairing = this.pairingEvent_.pairing; |
| return pairing == PairingEventType.REQUEST_PINCODE || |
| pairing == PairingEventType.REQUEST_PASSKEY; |
| }, |
| @@ -215,7 +271,7 @@ settings.BluetoothPairDeviceBehavior = { |
| if (!this.showConnect_()) |
| return false; |
| var inputId = |
| - (this.pairingEvent.pairing == PairingEventType.REQUEST_PINCODE) ? |
| + (this.pairingEvent_.pairing == PairingEventType.REQUEST_PINCODE) ? |
| '#pincode' : |
| '#passkey'; |
| var paperInput = /** @type {!PaperInputElement} */ (this.$$(inputId)); |
| @@ -229,9 +285,9 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| showDismiss_: function() { |
| - return (!!this.paringDevice && this.pairingDevice.paired) || |
| - (!!this.pairingEvent && |
| - this.pairingEvent.pairing == PairingEventType.COMPLETE); |
| + return this.pairingDevice.paired || |
| + (!!this.pairingEvent_ && |
| + this.pairingEvent_.pairing == PairingEventType.COMPLETE); |
| }, |
| /** @private */ |
| @@ -254,15 +310,13 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| sendResponse_: function(response) { |
| - if (!this.pairingDevice) |
| - return; |
| var options = |
| /** @type {!chrome.bluetoothPrivate.SetPairingResponseOptions} */ { |
| device: this.pairingDevice, |
| response: response |
| }; |
| if (response == chrome.bluetoothPrivate.PairingResponse.CONFIRM) { |
| - var pairing = this.pairingEvent.pairing; |
| + var pairing = this.pairingEvent_.pairing; |
| if (pairing == PairingEventType.REQUEST_PINCODE) |
| options.pincode = this.$$('#pincode').value; |
| else if (pairing == PairingEventType.REQUEST_PASSKEY) |
| @@ -292,19 +346,20 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| getPinDigit_: function(index) { |
| - if (!this.pairingEvent) |
| + if (!this.pairingEvent_) |
| return ''; |
| var digit = '0'; |
| - var pairing = this.pairingEvent.pairing; |
| + var pairing = this.pairingEvent_.pairing; |
| if (pairing == PairingEventType.DISPLAY_PINCODE && |
| - this.pairingEvent.pincode && index < this.pairingEvent.pincode.length) { |
| - digit = this.pairingEvent.pincode[index]; |
| + this.pairingEvent_.pincode && |
| + index < this.pairingEvent_.pincode.length) { |
| + digit = this.pairingEvent_.pincode[index]; |
| } else if ( |
| - this.pairingEvent.passkey && |
| + this.pairingEvent_.passkey && |
| (pairing == PairingEventType.DISPLAY_PASSKEY || |
| pairing == PairingEventType.KEYS_ENTERED || |
| pairing == PairingEventType.CONFIRM_PASSKEY)) { |
| - var passkeyString = String(this.pairingEvent.passkey); |
| + var passkeyString = String(this.pairingEvent_.passkey); |
| if (index < passkeyString.length) |
| digit = passkeyString[index]; |
| } |
| @@ -317,21 +372,21 @@ settings.BluetoothPairDeviceBehavior = { |
| * @private |
| */ |
| getPinClass_: function(index) { |
| - if (!this.pairingEvent) |
| + if (!this.pairingEvent_) |
| return ''; |
| - if (this.pairingEvent.pairing == PairingEventType.CONFIRM_PASSKEY) |
| + if (this.pairingEvent_.pairing == PairingEventType.CONFIRM_PASSKEY) |
| return 'confirm'; |
| var cssClass = 'display'; |
| - if (this.pairingEvent.pairing == PairingEventType.DISPLAY_PASSKEY) { |
| + if (this.pairingEvent_.pairing == PairingEventType.DISPLAY_PASSKEY) { |
| if (index == 0) |
| cssClass += ' next'; |
| else |
| cssClass += ' untyped'; |
| } else if ( |
| - this.pairingEvent.pairing == PairingEventType.KEYS_ENTERED && |
| - this.pairingEvent.enteredKey) { |
| - var enteredKey = this.pairingEvent.enteredKey; // 1-7 |
| - var lastKey = this.digits.length; // 6 |
| + this.pairingEvent_.pairing == PairingEventType.KEYS_ENTERED && |
| + this.pairingEvent_.enteredKey) { |
| + var enteredKey = this.pairingEvent_.enteredKey; // 1-7 |
| + var lastKey = this.digits.length; // 6 |
| if ((index == -1 && enteredKey > lastKey) || (index + 1 == enteredKey)) |
| cssClass += ' next'; |
| else if (index > enteredKey) |
| @@ -360,16 +415,18 @@ Polymer({ |
| }, |
| observers: [ |
| - 'dialogUpdated_(dialogId, pairingEvent)', |
| + 'dialogUpdated_(dialogId, pairingEvent_)', |
| ], |
| open: function() { |
| + this.startPairing(); |
| this.pinOrPass = ''; |
| this.getDialog_().showModal(); |
| this.itemWasFocused_ = false; |
| }, |
| close: function() { |
|
fukino
2017/01/30 08:44:01
close() is not called when the dialog is closed by
stevenjb
2017/01/30 19:19:26
Right, thanks. Fixed.
|
| + this.endPairing(); |
| var dialog = this.getDialog_(); |
| if (dialog.open) |
| dialog.close(); |
| @@ -411,5 +468,3 @@ Polymer({ |
| this.sendResponse_(chrome.bluetoothPrivate.PairingResponse.CANCEL); |
| }, |
| }); |
| - |
| -})(); |