[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / resources / settings / internet_page / network_siminfo.js
blobae566e406718786d131b6c3c3dd8a9abba4cffb0
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 = this.networkState.Cellular.SIMLockStatus;
62 this.pukRequired =
63 simLockStatus && simLockStatus.LockType == CrOnc.LockType.PUK;
66 /** Polymer networkState changed method. */
67 pukRequiredChanged_: function() {
68 if (this.$.unlockPukDialog.opened) {
69 if (this.pukRequired)
70 this.$.unlockPuk.focus();
71 else
72 this.$.unlockPukDialog.close();
73 return;
76 if (!this.pukRequired)
77 return;
79 // If the PUK was activated while attempting to enter or change a pin,
80 // close the dialog and open the unlock PUK dialog.
81 var showUnlockPuk = false;
82 if (this.$.enterPinDialog.opened) {
83 this.$.enterPinDialog.close();
84 showUnlockPuk = true;
86 if (this.$.changePinDialog.opened) {
87 this.$.changePinDialog.close();
88 showUnlockPuk = true;
90 if (this.$.unlockPinDialog.opened) {
91 this.$.unlockPinDialog.close();
92 showUnlockPuk = true;
94 if (!showUnlockPuk)
95 return;
97 this.error = ErrorType.NONE;
98 this.$.unlockPukDialog.open();
99 this.$.unlockPuk.focus();
102 /** Polymer networkState changed method. */
103 onSimLockEnabledChange_: function(event) {
104 if (!this.networkState || !this.networkState.Cellular)
105 return;
106 this.sendSimLockEnabled_ = event.target.checked;
107 this.error = ErrorType.NONE;
108 this.$.enterPinDialog.open();
112 * Focuses the correct element when the dialog is opened.
113 * @param {Event} event
114 * @private
116 onEnterPinDialogOpened_: function(event) {
117 this.$.enterPin.value = '';
118 this.$.enterPin.focus();
122 * Sends the PIN value from the Enter PIN dialog.
123 * @param {Event} event
124 * @private
126 sendEnterPin_: function(event) {
127 var guid = this.networkState && this.networkState.GUID;
128 if (!guid)
129 return;
131 var pin = this.$.enterPin.value;
132 if (!this.validatePin_(pin))
133 return;
135 var /** @type {?CrOnc.CellularSimState} */ simState = {
136 requirePin: this.sendSimLockEnabled_,
137 currentPin: pin
139 chrome.networkingPrivate.setCellularSimState(guid, simState, function() {
140 if (chrome.runtime.lastError) {
141 this.error = ErrorType.INCORRECT_PIN;
142 } else {
143 this.error = ErrorType.NONE;
144 this.$.enterPinDialog.close();
146 }.bind(this));
150 * Opens the Change PIN dialog.
151 * @param {Event} event
152 * @private
154 onChangePin_: function(event) {
155 if (!this.networkState || !this.networkState.Cellular)
156 return;
157 this.error = ErrorType.NONE;
158 this.$.changePinDialog.open();
162 * Focuses the correct element when the dialog is opened.
163 * @param {Event} event
164 * @private
166 onChangePinDialogOpened_: function(event) {
167 this.$.changePinOld.value = '';
168 this.$.changePinNew1.value = '';
169 this.$.changePinNew2.value = '';
170 this.$.changePinOld.focus();
174 * Sends the old and new PIN values from the Change PIN dialog.
175 * @param {Event} event
176 * @private
178 sendChangePin_: function(event) {
179 var guid = this.networkState && this.networkState.GUID;
180 if (!guid)
181 return;
183 var newPin = this.$.changePinNew1.value;
184 if (!this.validatePin_(newPin, this.$.changePinNew2.value))
185 return;
187 var /** @type {?CrOnc.CellularSimState} */ simState = {
188 requirePin: true,
189 currentPin: this.$.changePinOld.value,
190 newPin: newPin
192 chrome.networkingPrivate.setCellularSimState(guid, simState, function() {
193 if (chrome.runtime.lastError) {
194 this.error = ErrorType.INCORRECT_PIN;
195 } else {
196 this.error = ErrorType.NONE;
197 this.$.changePinDialog.close();
199 }.bind(this));
203 * Opens the Unlock PIN dialog.
204 * @param {Event} event
205 * @private
207 unlockPin_: function(event) {
208 this.error = ErrorType.NONE;
209 this.$.unlockPinDialog.open();
213 * Focuses the correct element when the dialog is opened.
214 * @param {Event} event
215 * @private
217 onUnlockPinDialogOpened_: function(event) {
218 this.$.unlockPin.value = '';
219 this.$.unlockPin.focus();
223 * Sends the PIN value from the Unlock PIN dialog.
224 * @param {Event} event
225 * @private
227 sendUnlockPin_: function(event) {
228 var guid = this.networkState && this.networkState.GUID;
229 if (!guid)
230 return;
231 var pin = this.$.unlockPin.value;
232 if (!this.validatePin_(pin))
233 return;
235 chrome.networkingPrivate.unlockCellularSim(guid, pin, '', function() {
236 if (chrome.runtime.lastError) {
237 this.error = ErrorType.INCORRECT_PIN;
238 } else {
239 this.error = ErrorType.NONE;
240 this.$.unlockPinDialog.close();
242 }.bind(this));
246 * Opens the Unlock PUK dialog.
247 * @param {Event} event
248 * @private
250 unlockPuk_: function(event) {
251 this.error = ErrorType.NONE;
252 this.$.unlockPukDialog.open();
256 * Focuses the correct element when the dialog is opened.
257 * @param {Event} event
258 * @private
260 onUnlockPukDialogOpened_: function(event) {
261 this.$.unlockPuk.value = '';
262 this.$.unlockPin1.value = '';
263 this.$.unlockPin2.value = '';
264 this.$.unlockPuk.focus();
268 * Sends the PUK value and new PIN value from the Unblock PUK dialog.
269 * @param {Event} event
270 * @private
272 sendUnlockPuk_: function(event) {
273 var guid = this.networkState && this.networkState.GUID;
274 if (!guid)
275 return;
277 var puk = this.$.unlockPuk.value;
278 if (!this.validatePuk_(puk))
279 return;
280 var pin = this.$.unlockPin1.value;
281 if (!this.validatePin_(pin, this.$.unlockPin2.value))
282 return;
284 chrome.networkingPrivate.unlockCellularSim(guid, pin, puk, function() {
285 if (chrome.runtime.lastError) {
286 this.error = ErrorType.INCORRECT_PUK;
287 } else {
288 this.error = ErrorType.NONE;
289 this.$.unlockSimDialog.close();
291 }.bind(this));
295 * @param {?CrOnc.NetworkStateProperties} state
296 * @return {boolean} True if the Cellular SIM is locked.
297 * @private
299 isSimLocked_: function(state) {
300 return state && CrOnc.isSimLocked(state);
304 * @param {?CrOnc.NetworkStateProperties} state
305 * @return {string} The message for the number of retries left.
306 * @private
308 getRetriesLeftMsg_: function(state) {
309 var retriesLeft =
310 this.get('Cellular.SIMLockStatus.RetriesLeft', state) || 0;
311 // TODO(stevenjb): Localize
312 return 'Retries left: ' + retriesLeft.toString();
316 * @param {string} error
317 * @return {boolean} True if an error message should be shown for |error|.
318 * @private
320 showError_: function(error) {
321 return error && error != ErrorType.NONE;
325 * @param {string} error
326 * @return {string} The error message to display for |error|.
327 * @private
329 getErrorMsg_: function(error) {
330 // TODO(stevenjb_: Translate
331 if (error == ErrorType.INCORRECT_PIN)
332 return 'Incorrect PIN.';
333 if (error == ErrorType.INCORRECT_PUK)
334 return 'Incorrect PUK.';
335 if (error == ErrorType.MISMATCHED_PIN)
336 return 'PIN values do not match.';
337 if (error == ErrorType.INVALID_PIN)
338 return 'Invalid PIN.';
339 if (error == ErrorType.INVALID_PUK)
340 return 'Invalid PUK.';
341 return '';
345 * Checks whether |pin1| is of the proper length and if pin2 is not
346 * undefined, whether pin1 and pin2 match. On any failure, sets |this.error|
347 * and returns false.
348 * @param {string} pin1
349 * @param {string|undefined} pin2
350 * @return {boolean} True if the pins match and are of minimum length.
351 * @private
353 validatePin_: function(pin1, pin2) {
354 if (pin1.length < PIN_MIN_LENGTH) {
355 this.error = ErrorType.INVALID_PIN;
356 return false;
358 if (pin2 != undefined && pin1 != pin2) {
359 this.error = ErrorType.MISMATCHED_PIN;
360 return false;
362 return true;
366 * Checks whether |puk| is of the proper length. If not, sets |this.error|
367 * and returns false.
368 * @param {string} puk
369 * @return {boolean} True if the puk is of minimum length.
370 * @private
372 validatePuk_: function(puk) {
373 if (puk.length < PUK_MIN_LENGTH) {
374 this.error = ErrorType.INVALID_PUK;
375 return false;
377 return true;
380 })();