• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2011 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.
4
5cr.define('mobile', function() {
6
7  function SimUnlock() {
8  }
9
10  cr.addSingletonGetter(SimUnlock);
11
12  // State of the dialog.
13  SimUnlock.SIM_UNLOCK_LOADING           = -1;
14  SimUnlock.SIM_ABSENT_NOT_LOCKED        =  0,
15  SimUnlock.SIM_NOT_LOCKED_ASK_PIN       =  1;
16  SimUnlock.SIM_NOT_LOCKED_CHANGE_PIN    =  2;
17  SimUnlock.SIM_LOCKED_PIN               =  3;
18  SimUnlock.SIM_LOCKED_NO_PIN_TRIES_LEFT =  4;
19  SimUnlock.SIM_LOCKED_PUK               =  5;
20  SimUnlock.SIM_LOCKED_NO_PUK_TRIES_LEFT =  6;
21  SimUnlock.SIM_DISABLED                 =  7;
22
23  // Mode of the dialog.
24  SimUnlock.SIM_DIALOG_UNLOCK       = 0;
25  SimUnlock.SIM_DIALOG_CHANGE_PIN   = 1;
26  SimUnlock.SIM_DIALOG_SET_LOCK_ON  = 2;
27  SimUnlock.SIM_DIALOG_SET_LOCK_OFF = 3;
28
29  // Error codes.
30  SimUnlock.ERROR_PIN = 'incorrectPin';
31  SimUnlock.ERROR_PUK = 'incorrectPuk';
32  SimUnlock.ERROR_OK = 'ok';
33
34  SimUnlock.localStrings_ = new LocalStrings();
35
36  SimUnlock.prototype = {
37    initialized_: false,
38    mode_: SimUnlock.SIM_DIALOG_UNLOCK,
39    pukValue_: '',
40    state_: SimUnlock.SIM_UNLOCK_LOADING,
41
42    changeState_: function(simInfo) {
43      var newState = simInfo.state;
44      var error = simInfo.error;
45      var tries = simInfo.tries;
46      var pinMessage;
47      this.hideAll_();
48      switch(newState) {
49        case SimUnlock.SIM_UNLOCK_LOADING:
50          break;
51        case SimUnlock.SIM_ABSENT_NOT_LOCKED:
52          SimUnlock.close();
53          break;
54        case SimUnlock.SIM_LOCKED_PIN:
55          if (error == SimUnlock.ERROR_OK) {
56            pinMessage = SimUnlock.localStrings_.getStringF(
57                'enterPinTriesMessage', tries);
58            $('pin-error-msg').classList.remove('error');
59          } else if (error == SimUnlock.ERROR_PIN) {
60              pinMessage = SimUnlock.localStrings_.getStringF(
61                  'incorrectPinTriesMessage', tries);
62            $('pin-error-msg').classList.add('error');
63          }
64          $('pin-error-msg').textContent = pinMessage;
65          $('pin-input').value = '';
66          SimUnlock.enablePinDialog(true);
67          $('locked-pin-overlay').hidden = false;
68          $('pin-input').focus();
69          break;
70        case SimUnlock.SIM_NOT_LOCKED_ASK_PIN:
71          if (error == SimUnlock.ERROR_OK) {
72            pinMessage = SimUnlock.localStrings_.getString('enterPinMessage');
73            $('pin-error-msg').classList.remove('error');
74          } else if (error == SimUnlock.ERROR_PIN) {
75              pinMessage = SimUnlock.localStrings_.getStringF(
76                  'incorrectPinTriesMessage', tries);
77            $('pin-error-msg').classList.add('error');
78          }
79          $('pin-error-msg').textContent = pinMessage;
80          $('pin-input').value = '';
81          SimUnlock.enablePinDialog(true);
82          $('locked-pin-overlay').hidden = false;
83          $('pin-input').focus();
84          break;
85        case SimUnlock.SIM_NOT_LOCKED_CHANGE_PIN:
86          SimUnlock.prepareChoosePinDialog(true);
87          if (error == SimUnlock.ERROR_OK) {
88            pinMessage = SimUnlock.localStrings_.getString('changePinMessage');
89            $('choose-pin-msg').classList.remove('error');
90          } else if (error == SimUnlock.ERROR_PIN) {
91              pinMessage = SimUnlock.localStrings_.getStringF(
92                  'incorrectPinTriesMessage', tries);
93            $('choose-pin-msg').classList.add('error');
94          }
95          $('choose-pin-msg').textContent = pinMessage;
96          $('old-pin-input').value = '';
97          $('new-pin-input').value = '';
98          $('retype-new-pin-input').value = '';
99          $('choose-pin-overlay').hidden = false;
100          $('old-pin-input').focus();
101          SimUnlock.enableChoosePinDialog(true);
102          break;
103        case SimUnlock.SIM_LOCKED_NO_PIN_TRIES_LEFT:
104          $('locked-pin-no-tries-overlay').hidden = false;
105          break;
106        case SimUnlock.SIM_LOCKED_PUK:
107          $('puk-input').value = '';
108          if (tries && tries >= 0) {
109            var pukMessage = SimUnlock.localStrings_.getStringF(
110                'enterPukWarning', tries);
111            $('puk-warning-msg').textContent = pukMessage;
112          }
113          $('locked-puk-overlay').hidden = false;
114          $('puk-input').focus();
115          break;
116        case SimUnlock.SIM_LOCKED_NO_PUK_TRIES_LEFT:
117          $('locked-puk-no-tries-overlay').hidden = false;
118          break;
119        case SimUnlock.SimUnlock.SIM_DISABLED:
120          $('sim-disabled-overlay').hidden = false;
121          break;
122      }
123      this.state_ = newState;
124    },
125
126    hideAll_: function() {
127      $('locked-pin-overlay').hidden = true;
128      $('locked-pin-no-tries-overlay').hidden = true;
129      $('locked-puk-overlay').hidden = true;
130      $('choose-pin-overlay').hidden = true;
131      $('locked-puk-no-tries-overlay').hidden = true;
132      $('sim-disabled-overlay').hidden = true;
133    },
134
135    newPinEntered_: function(newPin, newPin2) {
136      var changePinMode = this.state_ == SimUnlock.SIM_NOT_LOCKED_CHANGE_PIN;
137      if (newPin != newPin2) {
138        $('choose-pin-error').hidden = false;
139        $('old-pin-input').value = '';
140        $('new-pin-input').value = '';
141        $('retype-new-pin-input').value = '';
142        if (changePinMode)
143          $('old-pin-input').focus();
144        else
145          $('new-pin-input').focus();
146      } else {
147        $('choose-pin-error').hidden = true;
148        SimUnlock.enableChoosePinDialog(false);
149        if (changePinMode) {
150          var oldPin = $('old-pin-input').value;
151          chrome.send('changePinCode', [oldPin, newPin]);
152        } else {
153          chrome.send('enterPukCode', [this.pukValue_, newPin]);
154          this.pukValue_ = '';
155        }
156      }
157    },
158
159    pukEntered_: function(pukValue) {
160      this.pukValue_ = pukValue;
161      this.hideAll_();
162      SimUnlock.prepareChoosePinDialog(false);
163      SimUnlock.enableChoosePinDialog(true);
164      $('new-pin-input').value = '';
165      $('retype-new-pin-input').value = '';
166      $('choose-pin-overlay').hidden = false;
167      $('new-pin-input').focus();
168    },
169
170    updateSimStatus_: function(simInfo) {
171      this.changeState_(simInfo);
172    },
173  };
174
175  SimUnlock.cancel = function() {
176    chrome.send('cancel');
177    SimUnlock.close();
178  };
179
180  SimUnlock.close = function() {
181    window.close();
182  };
183
184  SimUnlock.initialize = function() {
185    this.initialized_ = true;
186
187    var modePattern = /(^\?|&)mode=([^&#]*)/;
188    var results = modePattern.exec(window.location.search);
189    if (results == null) {
190      this.mode_ = SimUnlock.SIM_DIALOG_UNLOCK;
191    } else {
192      var mode = results[2];
193      if (mode == 'change-pin')
194        this.mode_ = SimUnlock.SIM_DIALOG_CHANGE_PIN;
195      else if (mode == 'set-lock-on')
196        this.mode_ = SimUnlock.SIM_DIALOG_SET_LOCK_ON;
197      else if (mode == 'set-lock-off')
198        this.mode_ = SimUnlock.SIM_DIALOG_SET_LOCK_OFF;
199    }
200
201    $('enter-pin-confirm').addEventListener('click', function(event) {
202      SimUnlock.enablePinDialog(false);
203      chrome.send('enterPinCode', [$('pin-input').value]);
204    });
205    $('enter-pin-dismiss').addEventListener('click', function(event) {
206      SimUnlock.cancel();
207    });
208    $('pin-no-tries-proceed').addEventListener('click', function(event) {
209      chrome.send('proceedToPukInput');
210    });
211    $('pin-no-tries-dismiss').addEventListener('click', function(event) {
212      SimUnlock.cancel();
213    });
214    $('enter-puk-confirm').addEventListener('click', function(event) {
215      SimUnlock.pukEntered($('puk-input').value);
216    });
217    $('enter-puk-dismiss').addEventListener('click', function(event) {
218      SimUnlock.cancel();
219    });
220    $('choose-pin-confirm').addEventListener('click', function(event) {
221      SimUnlock.newPinEntered($('new-pin-input').value,
222                              $('retype-new-pin-input').value);
223    });
224    $('choose-pin-dismiss').addEventListener('click', function(event) {
225      SimUnlock.cancel();
226    });
227    $('puk-no-tries-confirm').addEventListener('click', function(event) {
228      SimUnlock.close();
229    });
230    $('sim-disabled-confirm').addEventListener('click', function(event) {
231      SimUnlock.close();
232    });
233    chrome.send('simStatusInitialize', [this.mode_]);
234  };
235
236  SimUnlock.enablePinDialog = function(enabled) {
237    $('pin-input').disabled = !enabled;
238    $('enter-pin-confirm').disabled = !enabled;
239    $('enter-pin-dismiss').disabled = !enabled;
240  };
241
242  SimUnlock.enableChoosePinDialog = function(enabled) {
243    $('old-pin-input').disabled = !enabled;
244    $('new-pin-input').disabled = !enabled;
245    $('retype-new-pin-input').disabled = !enabled;
246    $('choose-pin-confirm').disabled = !enabled;
247    $('choose-pin-dismiss').disabled = !enabled;
248  };
249
250  SimUnlock.prepareChoosePinDialog = function(changePin) {
251    // Our dialog has different height than choose-pin step of the
252    // unlock process which we're reusing.
253    if (changePin) {
254      $('choose-pin-content-area').classList.remove('choose-pin-content-area');
255      $('choose-pin-content-area').classList.add('change-pin-content-area');
256      var title = SimUnlock.localStrings_.getString('changePinTitle');
257      $('choose-pin-title').textContent = title;
258    } else {
259      $('choose-pin-content-area').classList.remove('change-pin-content-area');
260      $('choose-pin-content-area').classList.add('choose-pin-content-area');
261      var pinMessage = SimUnlock.localStrings_.getString('choosePinMessage');
262      $('choose-pin-msg').classList.remove('error');
263      $('choose-pin-msg').textContent = pinMessage;
264      var title = SimUnlock.localStrings_.getString('choosePinTitle');
265      $('choose-pin-title').textContent = title;
266    }
267    $('old-pin-label').hidden = !changePin;
268    $('old-pin-input-area').hidden = !changePin;
269  };
270
271  SimUnlock.newPinEntered = function(newPin, newPin2) {
272    SimUnlock.getInstance().newPinEntered_(newPin, newPin2);
273  };
274
275  SimUnlock.pukEntered = function(pukValue) {
276    SimUnlock.getInstance().pukEntered_(pukValue);
277  };
278
279  SimUnlock.simStateChanged = function(simInfo) {
280    SimUnlock.getInstance().updateSimStatus_(simInfo);
281  };
282
283  // Export
284  return {
285    SimUnlock: SimUnlock
286  };
287
288});
289