Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
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 | const C = Components.classes; |
michael@0 | 6 | const I = Components.interfaces; |
michael@0 | 7 | |
michael@0 | 8 | const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1"; |
michael@0 | 9 | |
michael@0 | 10 | var gProfileService; |
michael@0 | 11 | var gProfileManagerBundle; |
michael@0 | 12 | |
michael@0 | 13 | var gDefaultProfileParent; |
michael@0 | 14 | var gOldProfileName; |
michael@0 | 15 | |
michael@0 | 16 | // The directory where the profile will be created. |
michael@0 | 17 | var gProfileRoot; |
michael@0 | 18 | |
michael@0 | 19 | // Text node to display the location and name of the profile to create. |
michael@0 | 20 | var gProfileDisplay; |
michael@0 | 21 | |
michael@0 | 22 | // Called once when the wizard is opened. |
michael@0 | 23 | function initWizard() |
michael@0 | 24 | { |
michael@0 | 25 | try { |
michael@0 | 26 | gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService); |
michael@0 | 27 | gProfileManagerBundle = document.getElementById("bundle_profileManager"); |
michael@0 | 28 | |
michael@0 | 29 | var dirService = C["@mozilla.org/file/directory_service;1"].getService(I.nsIProperties); |
michael@0 | 30 | gDefaultProfileParent = dirService.get("DefProfRt", I.nsIFile); |
michael@0 | 31 | |
michael@0 | 32 | gOldProfileName = document.getElementById("profileName").value; |
michael@0 | 33 | |
michael@0 | 34 | // Initialize the profile location display. |
michael@0 | 35 | gProfileDisplay = document.getElementById("profileDisplay").firstChild; |
michael@0 | 36 | setDisplayToDefaultFolder(); |
michael@0 | 37 | } |
michael@0 | 38 | catch(e) { |
michael@0 | 39 | window.close(); |
michael@0 | 40 | throw (e); |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | // Called every time the second wizard page is displayed. |
michael@0 | 45 | function initSecondWizardPage() |
michael@0 | 46 | { |
michael@0 | 47 | var profileName = document.getElementById("profileName"); |
michael@0 | 48 | profileName.select(); |
michael@0 | 49 | profileName.focus(); |
michael@0 | 50 | |
michael@0 | 51 | // Initialize profile name validation. |
michael@0 | 52 | checkCurrentInput(profileName.value); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | const kSaltTable = [ |
michael@0 | 56 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
michael@0 | 57 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
michael@0 | 58 | '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ]; |
michael@0 | 59 | |
michael@0 | 60 | var kSaltString = ""; |
michael@0 | 61 | for (var i = 0; i < 8; ++i) { |
michael@0 | 62 | kSaltString += kSaltTable[Math.floor(Math.random() * kSaltTable.length)]; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | |
michael@0 | 66 | function saltName(aName) |
michael@0 | 67 | { |
michael@0 | 68 | return kSaltString + "." + aName; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | function setDisplayToDefaultFolder() |
michael@0 | 72 | { |
michael@0 | 73 | var defaultProfileDir = gDefaultProfileParent.clone(); |
michael@0 | 74 | defaultProfileDir.append(saltName(document.getElementById("profileName").value)); |
michael@0 | 75 | gProfileRoot = defaultProfileDir; |
michael@0 | 76 | document.getElementById("useDefault").disabled = true; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | function updateProfileDisplay() |
michael@0 | 80 | { |
michael@0 | 81 | gProfileDisplay.data = gProfileRoot.path; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | // Invoke a folder selection dialog for choosing the directory of profile storage. |
michael@0 | 85 | function chooseProfileFolder() |
michael@0 | 86 | { |
michael@0 | 87 | var newProfileRoot; |
michael@0 | 88 | |
michael@0 | 89 | var dirChooser = C["@mozilla.org/filepicker;1"].createInstance(I.nsIFilePicker); |
michael@0 | 90 | dirChooser.init(window, gProfileManagerBundle.getString("chooseFolder"), |
michael@0 | 91 | I.nsIFilePicker.modeGetFolder); |
michael@0 | 92 | dirChooser.appendFilters(I.nsIFilePicker.filterAll); |
michael@0 | 93 | |
michael@0 | 94 | // default to the Profiles folder |
michael@0 | 95 | dirChooser.displayDirectory = gDefaultProfileParent; |
michael@0 | 96 | |
michael@0 | 97 | dirChooser.show(); |
michael@0 | 98 | newProfileRoot = dirChooser.file; |
michael@0 | 99 | |
michael@0 | 100 | // Disable the "Default Folder..." button when the default profile folder |
michael@0 | 101 | // was selected manually in the File Picker. |
michael@0 | 102 | document.getElementById("useDefault").disabled = |
michael@0 | 103 | (newProfileRoot.parent.equals(gDefaultProfileParent)); |
michael@0 | 104 | |
michael@0 | 105 | gProfileRoot = newProfileRoot; |
michael@0 | 106 | updateProfileDisplay(); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | // Checks the current user input for validity and triggers an error message accordingly. |
michael@0 | 110 | function checkCurrentInput(currentInput) |
michael@0 | 111 | { |
michael@0 | 112 | var finishButton = document.documentElement.getButton("finish"); |
michael@0 | 113 | var finishText = document.getElementById("finishText"); |
michael@0 | 114 | var canAdvance; |
michael@0 | 115 | |
michael@0 | 116 | var errorMessage = checkProfileName(currentInput); |
michael@0 | 117 | |
michael@0 | 118 | if (!errorMessage) { |
michael@0 | 119 | finishText.className = ""; |
michael@0 | 120 | #ifndef XP_MACOSX |
michael@0 | 121 | finishText.firstChild.data = gProfileManagerBundle.getString("profileFinishText"); |
michael@0 | 122 | #else |
michael@0 | 123 | finishText.firstChild.data = gProfileManagerBundle.getString("profileFinishTextMac"); |
michael@0 | 124 | #endif |
michael@0 | 125 | canAdvance = true; |
michael@0 | 126 | } |
michael@0 | 127 | else { |
michael@0 | 128 | finishText.className = "error"; |
michael@0 | 129 | finishText.firstChild.data = errorMessage; |
michael@0 | 130 | canAdvance = false; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | document.documentElement.canAdvance = canAdvance; |
michael@0 | 134 | finishButton.disabled = !canAdvance; |
michael@0 | 135 | |
michael@0 | 136 | updateProfileDisplay(); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | function updateProfileName(aNewName) { |
michael@0 | 140 | checkCurrentInput(aNewName); |
michael@0 | 141 | |
michael@0 | 142 | var re = new RegExp("^[a-z0-9]{8}\\." + |
michael@0 | 143 | gOldProfileName.replace(/[|^$()\[\]{}\\+?.*]/g, "\\$&") |
michael@0 | 144 | + '$'); |
michael@0 | 145 | |
michael@0 | 146 | if (re.test(gProfileRoot.leafName)) { |
michael@0 | 147 | gProfileRoot.leafName = saltName(aNewName); |
michael@0 | 148 | updateProfileDisplay(); |
michael@0 | 149 | } |
michael@0 | 150 | gOldProfileName = aNewName; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | // Checks whether the given string is a valid profile name. |
michael@0 | 154 | // Returns an error message describing the error in the name or "" when it's valid. |
michael@0 | 155 | function checkProfileName(profileNameToCheck) |
michael@0 | 156 | { |
michael@0 | 157 | // Check for emtpy profile name. |
michael@0 | 158 | if (!/\S/.test(profileNameToCheck)) |
michael@0 | 159 | return gProfileManagerBundle.getString("profileNameEmpty"); |
michael@0 | 160 | |
michael@0 | 161 | // Check whether all characters in the profile name are allowed. |
michael@0 | 162 | if (/([\\*:?<>|\/\"])/.test(profileNameToCheck)) |
michael@0 | 163 | return gProfileManagerBundle.getFormattedString("invalidChar", [RegExp.$1]); |
michael@0 | 164 | |
michael@0 | 165 | // Check whether a profile with the same name already exists. |
michael@0 | 166 | if (profileExists(profileNameToCheck)) |
michael@0 | 167 | return gProfileManagerBundle.getString("profileExists"); |
michael@0 | 168 | |
michael@0 | 169 | // profileNameToCheck is valid. |
michael@0 | 170 | return ""; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | function profileExists(aName) |
michael@0 | 174 | { |
michael@0 | 175 | var profiles = gProfileService.profiles; |
michael@0 | 176 | while (profiles.hasMoreElements()) { |
michael@0 | 177 | var profile = profiles.getNext().QueryInterface(I.nsIToolkitProfile); |
michael@0 | 178 | if (profile.name.toLowerCase() == aName.toLowerCase()) |
michael@0 | 179 | return true; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | return false; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | // Called when the first wizard page is shown. |
michael@0 | 186 | function enableNextButton() |
michael@0 | 187 | { |
michael@0 | 188 | document.documentElement.canAdvance = true; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | function onFinish() |
michael@0 | 192 | { |
michael@0 | 193 | var profileName = document.getElementById("profileName").value; |
michael@0 | 194 | var profile; |
michael@0 | 195 | |
michael@0 | 196 | // Create profile named profileName in profileRoot. |
michael@0 | 197 | try { |
michael@0 | 198 | profile = gProfileService.createProfile(gProfileRoot, profileName); |
michael@0 | 199 | } |
michael@0 | 200 | catch (e) { |
michael@0 | 201 | var profileCreationFailed = |
michael@0 | 202 | gProfileManagerBundle.getString("profileCreationFailed"); |
michael@0 | 203 | var profileCreationFailedTitle = |
michael@0 | 204 | gProfileManagerBundle.getString("profileCreationFailedTitle"); |
michael@0 | 205 | var promptService = C["@mozilla.org/embedcomp/prompt-service;1"]. |
michael@0 | 206 | getService(I.nsIPromptService); |
michael@0 | 207 | promptService.alert(window, profileCreationFailedTitle, |
michael@0 | 208 | profileCreationFailed + "\n" + e); |
michael@0 | 209 | |
michael@0 | 210 | return false; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | // window.opener is false if the Create Profile Wizard was opened from the |
michael@0 | 214 | // command line. |
michael@0 | 215 | if (window.opener) { |
michael@0 | 216 | // Add new profile to the list in the Profile Manager. |
michael@0 | 217 | window.opener.CreateProfile(profile); |
michael@0 | 218 | } |
michael@0 | 219 | else { |
michael@0 | 220 | // Use the newly created Profile. |
michael@0 | 221 | var profileLock = profile.lock(null); |
michael@0 | 222 | |
michael@0 | 223 | var dialogParams = window.arguments[0].QueryInterface(I.nsIDialogParamBlock); |
michael@0 | 224 | dialogParams.objects.insertElementAt(profileLock, 0, false); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | // Exit the wizard. |
michael@0 | 228 | return true; |
michael@0 | 229 | } |