browser/base/content/sync/utils.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // Equivalent to 0600 permissions; used for saved Sync Recovery Key.
michael@0 6 // This constant can be replaced when the equivalent values are available to
michael@0 7 // chrome JS; see Bug 433295 and Bug 757351.
michael@0 8 const PERMISSIONS_RWUSR = 0x180;
michael@0 9
michael@0 10 // Weave should always exist before before this file gets included.
michael@0 11 let gSyncUtils = {
michael@0 12 get bundle() {
michael@0 13 delete this.bundle;
michael@0 14 return this.bundle = Services.strings.createBundle("chrome://browser/locale/syncSetup.properties");
michael@0 15 },
michael@0 16
michael@0 17 get fxAccountsEnabled() {
michael@0 18 let service = Components.classes["@mozilla.org/weave/service;1"]
michael@0 19 .getService(Components.interfaces.nsISupports)
michael@0 20 .wrappedJSObject;
michael@0 21 return service.fxAccountsEnabled;
michael@0 22 },
michael@0 23
michael@0 24 // opens in a new window if we're in a modal prefwindow world, in a new tab otherwise
michael@0 25 _openLink: function (url) {
michael@0 26 let thisDocEl = document.documentElement,
michael@0 27 openerDocEl = window.opener && window.opener.document.documentElement;
michael@0 28 if (thisDocEl.id == "accountSetup" && window.opener &&
michael@0 29 openerDocEl.id == "BrowserPreferences" && !openerDocEl.instantApply)
michael@0 30 openUILinkIn(url, "window");
michael@0 31 else if (thisDocEl.id == "BrowserPreferences" && !thisDocEl.instantApply)
michael@0 32 openUILinkIn(url, "window");
michael@0 33 else if (document.documentElement.id == "change-dialog")
michael@0 34 Services.wm.getMostRecentWindow("navigator:browser")
michael@0 35 .openUILinkIn(url, "tab");
michael@0 36 else
michael@0 37 openUILinkIn(url, "tab");
michael@0 38 },
michael@0 39
michael@0 40 changeName: function changeName(input) {
michael@0 41 // Make sure to update to a modified name, e.g., empty-string -> default
michael@0 42 Weave.Service.clientsEngine.localName = input.value;
michael@0 43 input.value = Weave.Service.clientsEngine.localName;
michael@0 44 },
michael@0 45
michael@0 46 openChange: function openChange(type, duringSetup) {
michael@0 47 // Just re-show the dialog if it's already open
michael@0 48 let openedDialog = Services.wm.getMostRecentWindow("Sync:" + type);
michael@0 49 if (openedDialog != null) {
michael@0 50 openedDialog.focus();
michael@0 51 return;
michael@0 52 }
michael@0 53
michael@0 54 // Open up the change dialog
michael@0 55 let changeXUL = "chrome://browser/content/sync/genericChange.xul";
michael@0 56 let changeOpt = "centerscreen,chrome,resizable=no";
michael@0 57 Services.ww.activeWindow.openDialog(changeXUL, "", changeOpt,
michael@0 58 type, duringSetup);
michael@0 59 },
michael@0 60
michael@0 61 changePassword: function () {
michael@0 62 if (Weave.Utils.ensureMPUnlocked())
michael@0 63 this.openChange("ChangePassword");
michael@0 64 },
michael@0 65
michael@0 66 resetPassphrase: function (duringSetup) {
michael@0 67 if (Weave.Utils.ensureMPUnlocked())
michael@0 68 this.openChange("ResetPassphrase", duringSetup);
michael@0 69 },
michael@0 70
michael@0 71 updatePassphrase: function () {
michael@0 72 if (Weave.Utils.ensureMPUnlocked())
michael@0 73 this.openChange("UpdatePassphrase");
michael@0 74 },
michael@0 75
michael@0 76 resetPassword: function () {
michael@0 77 this._openLink(Weave.Service.pwResetURL);
michael@0 78 },
michael@0 79
michael@0 80 openToS: function () {
michael@0 81 let root = this.fxAccountsEnabled ? "fxa." : "";
michael@0 82 this._openLink(Weave.Svc.Prefs.get(root + "termsURL"));
michael@0 83 },
michael@0 84
michael@0 85 openPrivacyPolicy: function () {
michael@0 86 let root = this.fxAccountsEnabled ? "fxa." : "";
michael@0 87 this._openLink(Weave.Svc.Prefs.get(root + "privacyURL"));
michael@0 88 },
michael@0 89
michael@0 90 openMPInfoPage: function (event) {
michael@0 91 event.stopPropagation();
michael@0 92 let baseURL = Services.urlFormatter.formatURLPref("app.support.baseURL");
michael@0 93 this._openLink(baseURL + "sync-master-password");
michael@0 94 },
michael@0 95
michael@0 96 openFirstSyncProgressPage: function () {
michael@0 97 this._openLink("about:sync-progress");
michael@0 98 },
michael@0 99
michael@0 100 /**
michael@0 101 * Prepare an invisible iframe with the passphrase backup document.
michael@0 102 * Used by both the print and saving methods.
michael@0 103 *
michael@0 104 * @param elid : ID of the form element containing the passphrase.
michael@0 105 * @param callback : Function called once the iframe has loaded.
michael@0 106 */
michael@0 107 _preparePPiframe: function(elid, callback) {
michael@0 108 let pp = document.getElementById(elid).value;
michael@0 109
michael@0 110 // Create an invisible iframe whose contents we can print.
michael@0 111 let iframe = document.createElement("iframe");
michael@0 112 iframe.setAttribute("src", "chrome://browser/content/sync/key.xhtml");
michael@0 113 iframe.collapsed = true;
michael@0 114 document.documentElement.appendChild(iframe);
michael@0 115 iframe.contentWindow.addEventListener("load", function() {
michael@0 116 iframe.contentWindow.removeEventListener("load", arguments.callee, false);
michael@0 117
michael@0 118 // Insert the Sync Key into the page.
michael@0 119 let el = iframe.contentDocument.getElementById("synckey");
michael@0 120 el.firstChild.nodeValue = pp;
michael@0 121
michael@0 122 // Insert the TOS and Privacy Policy URLs into the page.
michael@0 123 let termsURL = Weave.Svc.Prefs.get("termsURL");
michael@0 124 el = iframe.contentDocument.getElementById("tosLink");
michael@0 125 el.setAttribute("href", termsURL);
michael@0 126 el.firstChild.nodeValue = termsURL;
michael@0 127
michael@0 128 let privacyURL = Weave.Svc.Prefs.get("privacyURL");
michael@0 129 el = iframe.contentDocument.getElementById("ppLink");
michael@0 130 el.setAttribute("href", privacyURL);
michael@0 131 el.firstChild.nodeValue = privacyURL;
michael@0 132
michael@0 133 callback(iframe);
michael@0 134 }, false);
michael@0 135 },
michael@0 136
michael@0 137 /**
michael@0 138 * Print passphrase backup document.
michael@0 139 *
michael@0 140 * @param elid : ID of the form element containing the passphrase.
michael@0 141 */
michael@0 142 passphrasePrint: function(elid) {
michael@0 143 this._preparePPiframe(elid, function(iframe) {
michael@0 144 let webBrowserPrint = iframe.contentWindow
michael@0 145 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 146 .getInterface(Ci.nsIWebBrowserPrint);
michael@0 147 let printSettings = PrintUtils.getPrintSettings();
michael@0 148
michael@0 149 // Display no header/footer decoration except for the date.
michael@0 150 printSettings.headerStrLeft
michael@0 151 = printSettings.headerStrCenter
michael@0 152 = printSettings.headerStrRight
michael@0 153 = printSettings.footerStrLeft
michael@0 154 = printSettings.footerStrCenter = "";
michael@0 155 printSettings.footerStrRight = "&D";
michael@0 156
michael@0 157 try {
michael@0 158 webBrowserPrint.print(printSettings, null);
michael@0 159 } catch (ex) {
michael@0 160 // print()'s return codes are expressed as exceptions. Ignore.
michael@0 161 }
michael@0 162 });
michael@0 163 },
michael@0 164
michael@0 165 /**
michael@0 166 * Save passphrase backup document to disk as HTML file.
michael@0 167 *
michael@0 168 * @param elid : ID of the form element containing the passphrase.
michael@0 169 */
michael@0 170 passphraseSave: function(elid) {
michael@0 171 let dialogTitle = this.bundle.GetStringFromName("save.recoverykey.title");
michael@0 172 let defaultSaveName = this.bundle.GetStringFromName("save.recoverykey.defaultfilename");
michael@0 173 this._preparePPiframe(elid, function(iframe) {
michael@0 174 let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
michael@0 175 let fpCallback = function fpCallback_done(aResult) {
michael@0 176 if (aResult == Ci.nsIFilePicker.returnOK ||
michael@0 177 aResult == Ci.nsIFilePicker.returnReplace) {
michael@0 178 let stream = Cc["@mozilla.org/network/file-output-stream;1"].
michael@0 179 createInstance(Ci.nsIFileOutputStream);
michael@0 180 stream.init(fp.file, -1, PERMISSIONS_RWUSR, 0);
michael@0 181
michael@0 182 let serializer = new XMLSerializer();
michael@0 183 let output = serializer.serializeToString(iframe.contentDocument);
michael@0 184 output = output.replace(/<!DOCTYPE (.|\n)*?]>/,
michael@0 185 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' +
michael@0 186 '"DTD/xhtml1-strict.dtd">');
michael@0 187 output = Weave.Utils.encodeUTF8(output);
michael@0 188 stream.write(output, output.length);
michael@0 189 }
michael@0 190 };
michael@0 191
michael@0 192 fp.init(window, dialogTitle, Ci.nsIFilePicker.modeSave);
michael@0 193 fp.appendFilters(Ci.nsIFilePicker.filterHTML);
michael@0 194 fp.defaultString = defaultSaveName;
michael@0 195 fp.open(fpCallback);
michael@0 196 return false;
michael@0 197 });
michael@0 198 },
michael@0 199
michael@0 200 /**
michael@0 201 * validatePassword
michael@0 202 *
michael@0 203 * @param el1 : the first textbox element in the form
michael@0 204 * @param el2 : the second textbox element, if omitted it's an update form
michael@0 205 *
michael@0 206 * returns [valid, errorString]
michael@0 207 */
michael@0 208 validatePassword: function (el1, el2) {
michael@0 209 let valid = false;
michael@0 210 let val1 = el1.value;
michael@0 211 let val2 = el2 ? el2.value : "";
michael@0 212 let error = "";
michael@0 213
michael@0 214 if (!el2)
michael@0 215 valid = val1.length >= Weave.MIN_PASS_LENGTH;
michael@0 216 else if (val1 && val1 == Weave.Service.identity.username)
michael@0 217 error = "change.password.pwSameAsUsername";
michael@0 218 else if (val1 && val1 == Weave.Service.identity.account)
michael@0 219 error = "change.password.pwSameAsEmail";
michael@0 220 else if (val1 && val1 == Weave.Service.identity.basicPassword)
michael@0 221 error = "change.password.pwSameAsPassword";
michael@0 222 else if (val1 && val2) {
michael@0 223 if (val1 == val2 && val1.length >= Weave.MIN_PASS_LENGTH)
michael@0 224 valid = true;
michael@0 225 else if (val1.length < Weave.MIN_PASS_LENGTH)
michael@0 226 error = "change.password.tooShort";
michael@0 227 else if (val1 != val2)
michael@0 228 error = "change.password.mismatch";
michael@0 229 }
michael@0 230 let errorString = error ? Weave.Utils.getErrorString(error) : "";
michael@0 231 return [valid, errorString];
michael@0 232 }
michael@0 233 };

mercurial