genericChange.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. var Ci = Components.interfaces;
  5. var Cc = Components.classes;
  6. Components.utils.import("resource://services-sync/main.js");
  7. Components.utils.import("resource://gre/modules/Services.jsm");
  8. var Change = {
  9. _dialog: null,
  10. _dialogType: null,
  11. _status: null,
  12. _statusIcon: null,
  13. _firstBox: null,
  14. _secondBox: null,
  15. get _passphraseBox() {
  16. delete this._passphraseBox;
  17. return this._passphraseBox = document.getElementById("passphraseBox");
  18. },
  19. get _currentPasswordInvalid() {
  20. return Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED;
  21. },
  22. get _updatingPassphrase() {
  23. return this._dialogType == "UpdatePassphrase";
  24. },
  25. onLoad: function() {
  26. /* Load labels */
  27. let introText = document.getElementById("introText");
  28. let introText2 = document.getElementById("introText2");
  29. let warningText = document.getElementById("warningText");
  30. // load some other elements & info from the window
  31. this._dialog = document.getElementById("change-dialog");
  32. this._dialogType = window.arguments[0];
  33. this._duringSetup = window.arguments[1];
  34. this._status = document.getElementById("status");
  35. this._statusIcon = document.getElementById("statusIcon");
  36. this._statusRow = document.getElementById("statusRow");
  37. this._firstBox = document.getElementById("textBox1");
  38. this._secondBox = document.getElementById("textBox2");
  39. this._dialog.getButton("finish").disabled = true;
  40. this._dialog.getButton("back").hidden = true;
  41. this._stringBundle =
  42. Services.strings.createBundle("chrome://browser/locale/syncGenericChange.properties");
  43. switch (this._dialogType) {
  44. case "UpdatePassphrase":
  45. case "ResetPassphrase":
  46. document.getElementById("textBox1Row").hidden = true;
  47. document.getElementById("textBox2Row").hidden = true;
  48. document.getElementById("passphraseLabel").value
  49. = this._str("new.recoverykey.label");
  50. document.getElementById("passphraseSpacer").hidden = false;
  51. if (this._updatingPassphrase) {
  52. document.getElementById("passphraseHelpBox").hidden = false;
  53. document.title = this._str("new.recoverykey.title");
  54. introText.textContent = this._str("new.recoverykey.introText");
  55. this._dialog.getButton("finish").label
  56. = this._str("new.recoverykey.acceptButton");
  57. }
  58. else {
  59. document.getElementById("generatePassphraseButton").hidden = false;
  60. document.getElementById("passphraseBackupButtons").hidden = false;
  61. let pp = Weave.Service.identity.syncKey;
  62. if (Weave.Utils.isPassphrase(pp))
  63. pp = Weave.Utils.hyphenatePassphrase(pp);
  64. this._passphraseBox.value = pp;
  65. this._passphraseBox.focus();
  66. document.title = this._str("change.recoverykey.title");
  67. introText.textContent = this._str("change.synckey.introText2");
  68. warningText.textContent = this._str("change.recoverykey.warningText");
  69. this._dialog.getButton("finish").label
  70. = this._str("change.recoverykey.acceptButton");
  71. if (this._duringSetup) {
  72. this._dialog.getButton("finish").disabled = false;
  73. }
  74. }
  75. break;
  76. case "ChangePassword":
  77. document.getElementById("passphraseRow").hidden = true;
  78. let box1label = document.getElementById("textBox1Label");
  79. let box2label = document.getElementById("textBox2Label");
  80. box1label.value = this._str("new.password.label");
  81. if (this._currentPasswordInvalid) {
  82. document.title = this._str("new.password.title");
  83. introText.textContent = this._str("new.password.introText");
  84. this._dialog.getButton("finish").label
  85. = this._str("new.password.acceptButton");
  86. document.getElementById("textBox2Row").hidden = true;
  87. }
  88. else {
  89. document.title = this._str("change.password.title");
  90. box2label.value = this._str("new.password.confirm");
  91. introText.textContent = this._str("change.password3.introText");
  92. warningText.textContent = this._str("change.password.warningText");
  93. this._dialog.getButton("finish").label
  94. = this._str("change.password.acceptButton");
  95. }
  96. break;
  97. }
  98. document.getElementById("change-page")
  99. .setAttribute("label", document.title);
  100. },
  101. _clearStatus: function() {
  102. this._status.value = "";
  103. this._statusIcon.removeAttribute("status");
  104. },
  105. _updateStatus: function(str, state) {
  106. this._updateStatusWithString(this._str(str), state);
  107. },
  108. _updateStatusWithString: function(string, state) {
  109. this._statusRow.hidden = false;
  110. this._status.value = string;
  111. this._statusIcon.setAttribute("status", state);
  112. let error = state == "error";
  113. this._dialog.getButton("cancel").disabled = !error;
  114. this._dialog.getButton("finish").disabled = !error;
  115. document.getElementById("printSyncKeyButton").disabled = !error;
  116. document.getElementById("saveSyncKeyButton").disabled = !error;
  117. if (state == "success")
  118. window.setTimeout(window.close, 1500);
  119. },
  120. onDialogAccept: function() {
  121. switch (this._dialogType) {
  122. case "UpdatePassphrase":
  123. case "ResetPassphrase":
  124. return this.doChangePassphrase();
  125. break;
  126. case "ChangePassword":
  127. return this.doChangePassword();
  128. break;
  129. }
  130. },
  131. doGeneratePassphrase: function() {
  132. let passphrase = Weave.Utils.generatePassphrase();
  133. this._passphraseBox.value = Weave.Utils.hyphenatePassphrase(passphrase);
  134. this._dialog.getButton("finish").disabled = false;
  135. },
  136. doChangePassphrase: function() {
  137. let pp = Weave.Utils.normalizePassphrase(this._passphraseBox.value);
  138. if (this._updatingPassphrase) {
  139. Weave.Service.identity.syncKey = pp;
  140. if (Weave.Service.login()) {
  141. this._updateStatus("change.recoverykey.success", "success");
  142. Weave.Service.persistLogin();
  143. Weave.Service.scheduler.delayedAutoConnect(0);
  144. }
  145. else {
  146. this._updateStatus("new.passphrase.status.incorrect", "error");
  147. }
  148. }
  149. else {
  150. this._updateStatus("change.recoverykey.label", "active");
  151. if (Weave.Service.changePassphrase(pp))
  152. this._updateStatus("change.recoverykey.success", "success");
  153. else
  154. this._updateStatus("change.recoverykey.error", "error");
  155. }
  156. return false;
  157. },
  158. doChangePassword: function() {
  159. if (this._currentPasswordInvalid) {
  160. Weave.Service.identity.basicPassword = this._firstBox.value;
  161. if (Weave.Service.login()) {
  162. this._updateStatus("change.password.status.success", "success");
  163. Weave.Service.persistLogin();
  164. }
  165. else {
  166. this._updateStatus("new.password.status.incorrect", "error");
  167. }
  168. }
  169. else {
  170. this._updateStatus("change.password.status.active", "active");
  171. if (Weave.Service.changePassword(this._firstBox.value))
  172. this._updateStatus("change.password.status.success", "success");
  173. else
  174. this._updateStatus("change.password.status.error", "error");
  175. }
  176. return false;
  177. },
  178. validate: function(event) {
  179. let valid = false;
  180. let errorString = "";
  181. if (this._dialogType == "ChangePassword") {
  182. if (this._currentPasswordInvalid)
  183. [valid, errorString] = gSyncUtils.validatePassword(this._firstBox);
  184. else
  185. [valid, errorString] = gSyncUtils.validatePassword(this._firstBox, this._secondBox);
  186. }
  187. else {
  188. //Pale Moon: Enforce minimum length of 8 for allowed custom passphrase
  189. //and don't restrict it to "out of sync" situations only. People who
  190. //go to this page generally know what they are doing ;)
  191. valid = this._passphraseBox.value.length >= 8;
  192. }
  193. if (errorString == "")
  194. this._clearStatus();
  195. else
  196. this._updateStatusWithString(errorString, "error");
  197. this._statusRow.hidden = valid;
  198. this._dialog.getButton("finish").disabled = !valid;
  199. },
  200. _str: function(str) {
  201. return this._stringBundle.GetStringFromName(str);
  202. }
  203. };