summaryrefslogtreecommitdiffstats
path: root/components/sync/genericChange.js
blob: df6639178991a0f96a5f5a24670772f721851118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var Ci = Components.interfaces;
var Cc = Components.classes;

Components.utils.import("resource://services-sync/main.js");
Components.utils.import("resource://gre/modules/Services.jsm");

var Change = {
  _dialog: null,
  _dialogType: null,
  _status: null,
  _statusIcon: null,
  _firstBox: null,
  _secondBox: null,

  get _passphraseBox() {
    delete this._passphraseBox;
    return this._passphraseBox = document.getElementById("passphraseBox");
  },

  get _currentPasswordInvalid() {
    return Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED;
  },

  get _updatingPassphrase() {
    return this._dialogType == "UpdatePassphrase";
  },

  onLoad: function Change_onLoad() {
    /* Load labels */
    let introText = document.getElementById("introText");
    let introText2 = document.getElementById("introText2");
    let warningText = document.getElementById("warningText");

    // load some other elements & info from the window
    this._dialog = document.getElementById("change-dialog");
    this._dialogType = window.arguments[0];
    this._duringSetup = window.arguments[1];
    this._status = document.getElementById("status");
    this._statusIcon = document.getElementById("statusIcon");
    this._statusRow = document.getElementById("statusRow");
    this._firstBox = document.getElementById("textBox1");
    this._secondBox = document.getElementById("textBox2");

    this._dialog.getButton("finish").disabled = true;
    this._dialog.getButton("back").hidden = true;

    this._stringBundle =
      Services.strings.createBundle("chrome://browser/locale/syncGenericChange.properties");

    switch (this._dialogType) {
      case "UpdatePassphrase":
      case "ResetPassphrase":
        document.getElementById("textBox1Row").hidden = true;
        document.getElementById("textBox2Row").hidden = true;
        document.getElementById("passphraseLabel").value
          = this._str("new.recoverykey.label");
        document.getElementById("passphraseSpacer").hidden = false;

        if (this._updatingPassphrase) {
          document.getElementById("passphraseHelpBox").hidden = false;
          document.title = this._str("new.recoverykey.title");
          introText.textContent = this._str("new.recoverykey.introText");
          this._dialog.getButton("finish").label
            = this._str("new.recoverykey.acceptButton");
        }
        else {
          document.getElementById("generatePassphraseButton").hidden = false;
          document.getElementById("passphraseBackupButtons").hidden = false;
          let pp = Weave.Service.identity.syncKey;
          if (Weave.Utils.isPassphrase(pp))
             pp = Weave.Utils.hyphenatePassphrase(pp);
          this._passphraseBox.value = pp;
          this._passphraseBox.focus();
          document.title = this._str("change.recoverykey.title");
          introText.textContent = this._str("change.synckey.introText2");
          warningText.textContent = this._str("change.recoverykey.warningText");
          this._dialog.getButton("finish").label
            = this._str("change.recoverykey.acceptButton");
          if (this._duringSetup) {
            this._dialog.getButton("finish").disabled = false;
          }
        }
        break;
      case "ChangePassword":
        document.getElementById("passphraseRow").hidden = true;
        let box1label = document.getElementById("textBox1Label");
        let box2label = document.getElementById("textBox2Label");
        box1label.value = this._str("new.password.label");

        if (this._currentPasswordInvalid) {
          document.title = this._str("new.password.title");
          introText.textContent = this._str("new.password.introText");
          this._dialog.getButton("finish").label
            = this._str("new.password.acceptButton");
          document.getElementById("textBox2Row").hidden = true;
        }
        else {
          document.title = this._str("change.password.title");
          box2label.value = this._str("new.password.confirm");
          introText.textContent = this._str("change.password3.introText");
          warningText.textContent = this._str("change.password.warningText");
          this._dialog.getButton("finish").label
            = this._str("change.password.acceptButton");
        }
        break;
    }
    document.getElementById("change-page")
            .setAttribute("label", document.title);
  },

  _clearStatus: function _clearStatus() {
    this._status.value = "";
    this._statusIcon.removeAttribute("status");
  },

  _updateStatus: function Change__updateStatus(str, state) {
     this._updateStatusWithString(this._str(str), state);
  },
  
  _updateStatusWithString: function Change__updateStatusWithString(string, state) {
    this._statusRow.hidden = false;
    this._status.value = string;
    this._statusIcon.setAttribute("status", state);

    let error = state == "error";
    this._dialog.getButton("cancel").disabled = !error;
    this._dialog.getButton("finish").disabled = !error;
    document.getElementById("printSyncKeyButton").disabled = !error;
    document.getElementById("saveSyncKeyButton").disabled = !error;

    if (state == "success")
      window.setTimeout(window.close, 1500);
  },

  onDialogAccept: function() {
    switch (this._dialogType) {
      case "UpdatePassphrase":
      case "ResetPassphrase":
        return this.doChangePassphrase();
        break;
      case "ChangePassword":
        return this.doChangePassword();
        break;
    }
  },

  doGeneratePassphrase: function () {
    let passphrase = Weave.Utils.generatePassphrase();
    this._passphraseBox.value = Weave.Utils.hyphenatePassphrase(passphrase);
    this._dialog.getButton("finish").disabled = false;
  },

  doChangePassphrase: function Change_doChangePassphrase() {
    let pp = Weave.Utils.normalizePassphrase(this._passphraseBox.value);
    if (this._updatingPassphrase) {
      Weave.Service.identity.syncKey = pp;
      if (Weave.Service.login()) {
        this._updateStatus("change.recoverykey.success", "success");
        Weave.Service.persistLogin();
        Weave.Service.scheduler.delayedAutoConnect(0);
      }
      else {
        this._updateStatus("new.passphrase.status.incorrect", "error");
      }
    }
    else {
      this._updateStatus("change.recoverykey.label", "active");

      if (Weave.Service.changePassphrase(pp))
        this._updateStatus("change.recoverykey.success", "success");
      else
        this._updateStatus("change.recoverykey.error", "error");
    }

    return false;
  },

  doChangePassword: function Change_doChangePassword() {
    if (this._currentPasswordInvalid) {
      Weave.Service.identity.basicPassword = this._firstBox.value;
      if (Weave.Service.login()) {
        this._updateStatus("change.password.status.success", "success");
        Weave.Service.persistLogin();
      }
      else {
        this._updateStatus("new.password.status.incorrect", "error");
      }
    }
    else {
      this._updateStatus("change.password.status.active", "active");

      if (Weave.Service.changePassword(this._firstBox.value))
        this._updateStatus("change.password.status.success", "success");
      else
        this._updateStatus("change.password.status.error", "error");
    }

    return false;
  },

  validate: function (event) {
    let valid = false;
    let errorString = "";

    if (this._dialogType == "ChangePassword") {
      if (this._currentPasswordInvalid)
        [valid, errorString] = gSyncUtils.validatePassword(this._firstBox);
      else
        [valid, errorString] = gSyncUtils.validatePassword(this._firstBox, this._secondBox);
    }
    else {
      //Pale Moon: Enforce minimum length of 8 for allowed custom passphrase
      //and don't restrict it to "out of sync" situations only. People who
      //go to this page generally know what they are doing ;)
      valid = this._passphraseBox.value.length >= 8;
    }

    if (errorString == "")
      this._clearStatus();
    else
      this._updateStatusWithString(errorString, "error");

    this._statusRow.hidden = valid;
    this._dialog.getButton("finish").disabled = !valid;
  },

  _str: function Change__string(str) {
    return this._stringBundle.GetStringFromName(str);
  }
};