Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / settings / internet_page / network_siminfo.js
blobc3a038222788a227f273b2be0f65dcef56acf0f3
1 // Copyright 2015 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.
5 /**
6 * @fileoverview Polymer element for displaying and modifying a list of cellular
7 * access points.
8 */
9 (function() {
11 /** @enum {string} */
12 var ErrorType = {
13 NONE: 'none',
14 INCORRECT_PIN: 'incorrect-pin',
15 INCORRECT_PUK: 'incorrect-puk',
16 MISMATCHED_PIN: 'mismatched-pin',
17 INVALID_PIN: 'invalid-pin',
18 INVALID_PUK: 'invalid-puk'
21 var PIN_MIN_LENGTH = 4;
22 var PUK_MIN_LENGTH = 8;
24 Polymer({
25 is: 'network-siminfo',
27 properties: {
28 /**
29 * The network state associated with the element.
30 * @type {?CrOnc.NetworkStateProperties}
32 networkState: {
33 type: Object,
34 value: null,
35 observer: 'networkStateChanged_'
38 /** Set to true when a PUK is required to unlock the SIM. */
39 pukRequired: {
40 type: Boolean,
41 value: false,
42 observer: 'pukRequiredChanged_'
45 /**
46 * Set to an ErrorType value after an incorrect PIN or PUK entry.
47 * @type {ErrorType}
49 error: {
50 type: Object,
51 value: ErrorType.NONE
55 sendSimLockEnabled_: false,
57 /** Polymer networkState changed method. */
58 networkStateChanged_: function() {
59 if (!this.networkState || !this.networkState.Cellular)
60 return;
61 var simLockStatus = /** @type {CrOnc.SIMLockStatus|undefined} */(
62 CrOnc.getActiveValue(this.networkState, 'Cellular.SIMLockStatus'));
63 this.pukRequired =
64 !!simLockStatus && simLockStatus.LockType == CrOnc.LockType.PUK;
67 /** Polymer networkState changed method. */
68 pukRequiredChanged_: function() {
69 if (this.$.unlockPukDialog.opened) {
70 if (this.pukRequired)
71 this.$.unlockPuk.focus();
72 else
73 this.$.unlockPukDialog.close();
74 return;
77 if (!this.pukRequired)
78 return;
80 // If the PUK was activated while attempting to enter or change a pin,
81 // close the dialog and open the unlock PUK dialog.
82 var showUnlockPuk = false;
83 if (this.$.enterPinDialog.opened) {
84 this.$.enterPinDialog.close();
85 showUnlockPuk = true;
87 if (this.$.changePinDialog.opened) {
88 this.$.changePinDialog.close();
89 showUnlockPuk = true;
91 if (this.$.unlockPinDialog.opened) {
92 this.$.unlockPinDialog.close();
93 showUnlockPuk = true;
95 if (!showUnlockPuk)
96 return;
98 this.error = ErrorType.NONE;
99 this.$.unlockPukDialog.open();
100 this.$.unlockPuk.focus();
103 /** Polymer networkState changed method. */
104 onSimLockEnabledChange_: function(event) {
105 if (!this.networkState || !this.networkState.Cellular)
106 return;
107 this.sendSimLockEnabled_ = event.target.checked;
108 this.error = ErrorType.NONE;
109 this.$.enterPinDialog.open();
113 * Focuses the correct element when the dialog is opened.
114 * @param {Event} event
115 * @private
117 onEnterPinDialogOpened_: function(event) {
118 this.$.enterPin.value = '';
119 this.$.enterPin.focus();
123 * Sends the PIN value from the Enter PIN dialog.
124 * @param {Event} event
125 * @private
127 sendEnterPin_: function(event) {
128 var guid = this.networkState && this.networkState.GUID;
129 if (!guid)
130 return;
132 var pin = this.$.enterPin.value;
133 if (!this.validatePin_(pin))
134 return;
136 var simState = /** @type {!CrOnc.CellularSimState} */({
137 currentPin: pin,
138 requirePin: this.sendSimLockEnabled_
140 chrome.networkingPrivate.setCellularSimState(guid, simState, function() {
141 if (chrome.runtime.lastError) {
142 this.error = ErrorType.INCORRECT_PIN;
143 } else {
144 this.error = ErrorType.NONE;
145 this.$.enterPinDialog.close();
147 }.bind(this));
151 * Opens the Change PIN dialog.
152 * @param {Event} event
153 * @private
155 onChangePin_: function(event) {
156 if (!this.networkState || !this.networkState.Cellular)
157 return;
158 this.error = ErrorType.NONE;
159 this.$.changePinDialog.open();
163 * Focuses the correct element when the dialog is opened.
164 * @param {Event} event
165 * @private
167 onChangePinDialogOpened_: function(event) {
168 this.$.changePinOld.value = '';
169 this.$.changePinNew1.value = '';
170 this.$.changePinNew2.value = '';
171 this.$.changePinOld.focus();
175 * Sends the old and new PIN values from the Change PIN dialog.
176 * @param {Event} event
177 * @private
179 sendChangePin_: function(event) {
180 var guid = this.networkState && this.networkState.GUID;
181 if (!guid)
182 return;
184 var newPin = this.$.changePinNew1.value;
185 if (!this.validatePin_(newPin, this.$.changePinNew2.value))
186 return;
188 var simState = /** @type {!CrOnc.CellularSimState} */({
189 requirePin: true,
190 currentPin: this.$.changePinOld.value,
191 newPin: newPin
193 chrome.networkingPrivate.setCellularSimState(guid, simState, function() {
194 if (chrome.runtime.lastError) {
195 this.error = ErrorType.INCORRECT_PIN;
196 } else {
197 this.error = ErrorType.NONE;
198 this.$.changePinDialog.close();
200 }.bind(this));
204 * Opens the Unlock PIN dialog.
205 * @param {Event} event
206 * @private
208 unlockPin_: function(event) {
209 this.error = ErrorType.NONE;
210 this.$.unlockPinDialog.open();
214 * Focuses the correct element when the dialog is opened.
215 * @param {Event} event
216 * @private
218 onUnlockPinDialogOpened_: function(event) {
219 this.$.unlockPin.value = '';
220 this.$.unlockPin.focus();
224 * Sends the PIN value from the Unlock PIN dialog.
225 * @param {Event} event
226 * @private
228 sendUnlockPin_: function(event) {
229 var guid = this.networkState && this.networkState.GUID;
230 if (!guid)
231 return;
232 var pin = this.$.unlockPin.value;
233 if (!this.validatePin_(pin))
234 return;
236 chrome.networkingPrivate.unlockCellularSim(guid, pin, '', function() {
237 if (chrome.runtime.lastError) {
238 this.error = ErrorType.INCORRECT_PIN;
239 } else {
240 this.error = ErrorType.NONE;
241 this.$.unlockPinDialog.close();
243 }.bind(this));
247 * Opens the Unlock PUK dialog.
248 * @param {Event} event
249 * @private
251 unlockPuk_: function(event) {
252 this.error = ErrorType.NONE;
253 this.$.unlockPukDialog.open();
257 * Focuses the correct element when the dialog is opened.
258 * @param {Event} event
259 * @private
261 onUnlockPukDialogOpened_: function(event) {
262 this.$.unlockPuk.value = '';
263 this.$.unlockPin1.value = '';
264 this.$.unlockPin2.value = '';
265 this.$.unlockPuk.focus();
269 * Sends the PUK value and new PIN value from the Unblock PUK dialog.
270 * @param {Event} event
271 * @private
273 sendUnlockPuk_: function(event) {
274 var guid = this.networkState && this.networkState.GUID;
275 if (!guid)
276 return;
278 var puk = this.$.unlockPuk.value;
279 if (!this.validatePuk_(puk))
280 return;
281 var pin = this.$.unlockPin1.value;
282 if (!this.validatePin_(pin, this.$.unlockPin2.value))
283 return;
285 chrome.networkingPrivate.unlockCellularSim(guid, pin, puk, function() {
286 if (chrome.runtime.lastError) {
287 this.error = ErrorType.INCORRECT_PUK;
288 } else {
289 this.error = ErrorType.NONE;
290 this.$.unlockSimDialog.close();
292 }.bind(this));
296 * @param {?CrOnc.NetworkStateProperties} state
297 * @return {boolean} True if the Cellular SIM is locked.
298 * @private
300 isSimLocked_: function(state) {
301 return !!state && CrOnc.isSimLocked(state);
305 * @param {?CrOnc.NetworkStateProperties} state
306 * @return {string} The message for the number of retries left.
307 * @private
309 getRetriesLeftMsg_: function(state) {
310 var retriesLeft =
311 this.get('Cellular.SIMLockStatus.RetriesLeft', state) || 0;
312 // TODO(stevenjb): Localize
313 return 'Retries left: ' + retriesLeft.toString();
317 * @param {string} error
318 * @return {boolean} True if an error message should be shown for |error|.
319 * @private
321 showError_: function(error) {
322 return !!error && error != ErrorType.NONE;
326 * @param {string} error
327 * @return {string} The error message to display for |error|.
328 * @private
330 getErrorMsg_: function(error) {
331 // TODO(stevenjb_: Translate
332 if (error == ErrorType.INCORRECT_PIN)
333 return 'Incorrect PIN.';
334 if (error == ErrorType.INCORRECT_PUK)
335 return 'Incorrect PUK.';
336 if (error == ErrorType.MISMATCHED_PIN)
337 return 'PIN values do not match.';
338 if (error == ErrorType.INVALID_PIN)
339 return 'Invalid PIN.';
340 if (error == ErrorType.INVALID_PUK)
341 return 'Invalid PUK.';
342 return '';
346 * Checks whether |pin1| is of the proper length and if opt_pin2 is not
347 * undefined, whether pin1 and opt_pin2 match. On any failure, sets
348 * |this.error| and returns false.
349 * @param {string} pin1
350 * @param {string=} opt_pin2
351 * @return {boolean} True if the pins match and are of minimum length.
352 * @private
354 validatePin_: function(pin1, opt_pin2) {
355 if (pin1.length < PIN_MIN_LENGTH) {
356 this.error = ErrorType.INVALID_PIN;
357 return false;
359 if (opt_pin2 != undefined && pin1 != opt_pin2) {
360 this.error = ErrorType.MISMATCHED_PIN;
361 return false;
363 return true;
367 * Checks whether |puk| is of the proper length. If not, sets |this.error|
368 * and returns false.
369 * @param {string} puk
370 * @return {boolean} True if the puk is of minimum length.
371 * @private
373 validatePuk_: function(puk) {
374 if (puk.length < PUK_MIN_LENGTH) {
375 this.error = ErrorType.INVALID_PUK;
376 return false;
378 return true;
381 })();