1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/preferences/in-content/privacy.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,540 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +var gPrivacyPane = { 1.9 + 1.10 + /** 1.11 + * Whether the use has selected the auto-start private browsing mode in the UI. 1.12 + */ 1.13 + _autoStartPrivateBrowsing: false, 1.14 + 1.15 + /** 1.16 + * Whether the prompt to restart Firefox should appear when changing the autostart pref. 1.17 + */ 1.18 + _shouldPromptForRestart: true, 1.19 + 1.20 + /** 1.21 + * Sets up the UI for the number of days of history to keep, and updates the 1.22 + * label of the "Clear Now..." button. 1.23 + */ 1.24 + init: function () 1.25 + { 1.26 + this._updateSanitizeSettingsButton(); 1.27 + this.initializeHistoryMode(); 1.28 + this.updateHistoryModePane(); 1.29 + this.updatePrivacyMicroControls(); 1.30 + this.initAutoStartPrivateBrowsingReverter(); 1.31 + }, 1.32 + 1.33 + // HISTORY MODE 1.34 + 1.35 + /** 1.36 + * The list of preferences which affect the initial history mode settings. 1.37 + * If the auto start private browsing mode pref is active, the initial 1.38 + * history mode would be set to "Don't remember anything". 1.39 + * If all of these preferences have their default values, and the auto-start 1.40 + * private browsing mode is not active, the initial history mode would be 1.41 + * set to "Remember everything". 1.42 + * Otherwise, the initial history mode would be set to "Custom". 1.43 + * 1.44 + * Extensions adding their own preferences can append their IDs to this array if needed. 1.45 + */ 1.46 + prefsForDefault: [ 1.47 + "places.history.enabled", 1.48 + "browser.formfill.enable", 1.49 + "network.cookie.cookieBehavior", 1.50 + "network.cookie.lifetimePolicy", 1.51 + "privacy.sanitize.sanitizeOnShutdown" 1.52 + ], 1.53 + 1.54 + /** 1.55 + * The list of control IDs which are dependent on the auto-start private 1.56 + * browsing setting, such that in "Custom" mode they would be disabled if 1.57 + * the auto-start private browsing checkbox is checked, and enabled otherwise. 1.58 + * 1.59 + * Extensions adding their own controls can append their IDs to this array if needed. 1.60 + */ 1.61 + dependentControls: [ 1.62 + "rememberHistory", 1.63 + "rememberForms", 1.64 + "keepUntil", 1.65 + "keepCookiesUntil", 1.66 + "alwaysClear", 1.67 + "clearDataSettings" 1.68 + ], 1.69 + 1.70 + /** 1.71 + * Check whether all the preferences values are set to their default values 1.72 + * 1.73 + * @param aPrefs an array of pref names to check for 1.74 + * @returns boolean true if all of the prefs are set to their default values, 1.75 + * false otherwise 1.76 + */ 1.77 + _checkDefaultValues: function(aPrefs) { 1.78 + for (let i = 0; i < aPrefs.length; ++i) { 1.79 + let pref = document.getElementById(aPrefs[i]); 1.80 + if (pref.value != pref.defaultValue) 1.81 + return false; 1.82 + } 1.83 + return true; 1.84 + }, 1.85 + 1.86 + /** 1.87 + * Initialize the history mode menulist based on the privacy preferences 1.88 + */ 1.89 + initializeHistoryMode: function PPP_initializeHistoryMode() 1.90 + { 1.91 + let mode; 1.92 + let getVal = function (aPref) 1.93 + document.getElementById(aPref).value; 1.94 + 1.95 + if (this._checkDefaultValues(this.prefsForDefault)) { 1.96 + if (getVal("browser.privatebrowsing.autostart")) 1.97 + mode = "dontremember"; 1.98 + else 1.99 + mode = "remember"; 1.100 + } 1.101 + else 1.102 + mode = "custom"; 1.103 + 1.104 + document.getElementById("historyMode").value = mode; 1.105 + }, 1.106 + 1.107 + /** 1.108 + * Update the selected pane based on the history mode menulist 1.109 + */ 1.110 + updateHistoryModePane: function PPP_updateHistoryModePane() 1.111 + { 1.112 + let selectedIndex = -1; 1.113 + switch (document.getElementById("historyMode").value) { 1.114 + case "remember": 1.115 + selectedIndex = 0; 1.116 + break; 1.117 + case "dontremember": 1.118 + selectedIndex = 1; 1.119 + break; 1.120 + case "custom": 1.121 + selectedIndex = 2; 1.122 + break; 1.123 + } 1.124 + document.getElementById("historyPane").selectedIndex = selectedIndex; 1.125 + }, 1.126 + 1.127 + /** 1.128 + * Update the Tracking preferences based on controls. 1.129 + */ 1.130 + setTrackingPrefs: function PPP_setTrackingPrefs() 1.131 + { 1.132 + let dntRadioGroup = document.getElementById("doNotTrackSelection"), 1.133 + dntValuePref = document.getElementById("privacy.donottrackheader.value"), 1.134 + dntEnabledPref = document.getElementById("privacy.donottrackheader.enabled"); 1.135 + 1.136 + // if the selected radio button says "no preference", set on/off pref to 1.137 + // false and don't change the value pref. 1.138 + if (dntRadioGroup.selectedItem.value == -1) { 1.139 + dntEnabledPref.value = false; 1.140 + return dntValuePref.value; 1.141 + } 1.142 + 1.143 + dntEnabledPref.value = true; 1.144 + return dntRadioGroup.selectedItem.value; 1.145 + }, 1.146 + 1.147 + /** 1.148 + * Obtain the tracking preference value and reflect it in the UI. 1.149 + */ 1.150 + getTrackingPrefs: function PPP_getTrackingPrefs() 1.151 + { 1.152 + // XXX avoid using bindings that might not be attached, see bug 859982 1.153 + let dntValue = Services.prefs.getIntPref("privacy.donottrackheader.value"), 1.154 + dntEnabled = Services.prefs.getBoolPref("privacy.donottrackheader.enabled"); 1.155 + 1.156 + // if DNT is enbaled, select the value from the selected radio 1.157 + // button, otherwise choose the "no preference" radio button 1.158 + if (dntEnabled) 1.159 + return dntValue; 1.160 + 1.161 + return document.getElementById("dntnopref").value; 1.162 + }, 1.163 + 1.164 + /** 1.165 + * Update the private browsing auto-start pref and the history mode 1.166 + * micro-management prefs based on the history mode menulist 1.167 + */ 1.168 + updateHistoryModePrefs: function PPP_updateHistoryModePrefs() 1.169 + { 1.170 + let pref = document.getElementById("browser.privatebrowsing.autostart"); 1.171 + switch (document.getElementById("historyMode").value) { 1.172 + case "remember": 1.173 + if (pref.value) 1.174 + pref.value = false; 1.175 + 1.176 + // select the remember history option if needed 1.177 + let rememberHistoryCheckbox = document.getElementById("rememberHistory"); 1.178 + if (!rememberHistoryCheckbox.checked) 1.179 + rememberHistoryCheckbox.checked = true; 1.180 + 1.181 + // select the remember forms history option 1.182 + document.getElementById("browser.formfill.enable").value = true; 1.183 + 1.184 +#ifdef RELEASE_BUILD 1.185 + // select the allow cookies option 1.186 + document.getElementById("network.cookie.cookieBehavior").value = 0; 1.187 +#else 1.188 + // select the limit cookies option 1.189 + document.getElementById("network.cookie.cookieBehavior").value = 3; 1.190 +#endif 1.191 + // select the cookie lifetime policy option 1.192 + document.getElementById("network.cookie.lifetimePolicy").value = 0; 1.193 + 1.194 + // select the clear on close option 1.195 + document.getElementById("privacy.sanitize.sanitizeOnShutdown").value = false; 1.196 + break; 1.197 + case "dontremember": 1.198 + if (!pref.value) 1.199 + pref.value = true; 1.200 + break; 1.201 + } 1.202 + }, 1.203 + 1.204 + /** 1.205 + * Update the privacy micro-management controls based on the 1.206 + * value of the private browsing auto-start checkbox. 1.207 + */ 1.208 + updatePrivacyMicroControls: function PPP_updatePrivacyMicroControls() 1.209 + { 1.210 + if (document.getElementById("historyMode").value == "custom") { 1.211 + let disabled = this._autoStartPrivateBrowsing = 1.212 + document.getElementById("privateBrowsingAutoStart").checked; 1.213 + this.dependentControls 1.214 + .forEach(function (aElement) 1.215 + document.getElementById(aElement).disabled = disabled); 1.216 + 1.217 + // adjust the cookie controls status 1.218 + this.readAcceptCookies(); 1.219 + document.getElementById("keepCookiesUntil").value = disabled ? 2 : 1.220 + document.getElementById("network.cookie.lifetimePolicy").value; 1.221 + 1.222 + // adjust the checked state of the sanitizeOnShutdown checkbox 1.223 + document.getElementById("alwaysClear").checked = disabled ? false : 1.224 + document.getElementById("privacy.sanitize.sanitizeOnShutdown").value; 1.225 + 1.226 + // adjust the checked state of the remember history checkboxes 1.227 + document.getElementById("rememberHistory").checked = disabled ? false : 1.228 + document.getElementById("places.history.enabled").value; 1.229 + document.getElementById("rememberForms").checked = disabled ? false : 1.230 + document.getElementById("browser.formfill.enable").value; 1.231 + 1.232 + if (!disabled) { 1.233 + // adjust the Settings button for sanitizeOnShutdown 1.234 + this._updateSanitizeSettingsButton(); 1.235 + } 1.236 + } 1.237 + }, 1.238 + 1.239 + // PRIVATE BROWSING 1.240 + 1.241 + /** 1.242 + * Initialize the starting state for the auto-start private browsing mode pref reverter. 1.243 + */ 1.244 + initAutoStartPrivateBrowsingReverter: function PPP_initAutoStartPrivateBrowsingReverter() 1.245 + { 1.246 + let mode = document.getElementById("historyMode"); 1.247 + let autoStart = document.getElementById("privateBrowsingAutoStart"); 1.248 + this._lastMode = mode.selectedIndex; 1.249 + this._lastCheckState = autoStart.hasAttribute('checked'); 1.250 + }, 1.251 + 1.252 + _lastMode: null, 1.253 + _lastCheckState: null, 1.254 + updateAutostart: function PPP_updateAutostart() { 1.255 + let mode = document.getElementById("historyMode"); 1.256 + let autoStart = document.getElementById("privateBrowsingAutoStart"); 1.257 + let pref = document.getElementById("browser.privatebrowsing.autostart"); 1.258 + if ((mode.value == "custom" && this._lastCheckState == autoStart.checked) || 1.259 + (mode.value == "remember" && !this._lastCheckState) || 1.260 + (mode.value == "dontremember" && this._lastCheckState)) { 1.261 + // These are all no-op changes, so we don't need to prompt. 1.262 + this._lastMode = mode.selectedIndex; 1.263 + this._lastCheckState = autoStart.hasAttribute('checked'); 1.264 + return; 1.265 + } 1.266 + 1.267 + if (!this._shouldPromptForRestart) { 1.268 + // We're performing a revert. Just let it happen. 1.269 + return; 1.270 + } 1.271 + 1.272 + const Cc = Components.classes, Ci = Components.interfaces; 1.273 + let brandName = document.getElementById("bundleBrand").getString("brandShortName"); 1.274 + let bundle = document.getElementById("bundlePreferences"); 1.275 + let msg = bundle.getFormattedString(autoStart.checked ? 1.276 + "featureEnableRequiresRestart" : "featureDisableRequiresRestart", 1.277 + [brandName]); 1.278 + let title = bundle.getFormattedString("shouldRestartTitle", [brandName]); 1.279 + let prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService); 1.280 + let shouldProceed = prompts.confirm(window, title, msg) 1.281 + if (shouldProceed) { 1.282 + let cancelQuit = Cc["@mozilla.org/supports-PRBool;1"] 1.283 + .createInstance(Ci.nsISupportsPRBool); 1.284 + Services.obs.notifyObservers(cancelQuit, "quit-application-requested", 1.285 + "restart"); 1.286 + shouldProceed = !cancelQuit.data; 1.287 + 1.288 + if (shouldProceed) { 1.289 + pref.value = autoStart.hasAttribute('checked'); 1.290 + document.documentElement.acceptDialog(); 1.291 + let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"] 1.292 + .getService(Ci.nsIAppStartup); 1.293 + appStartup.quit(Ci.nsIAppStartup.eAttemptQuit | Ci.nsIAppStartup.eRestart); 1.294 + return; 1.295 + } 1.296 + } 1.297 + 1.298 + this._shouldPromptForRestart = false; 1.299 + 1.300 + if (this._lastCheckState) { 1.301 + autoStart.checked = "checked"; 1.302 + } else { 1.303 + autoStart.removeAttribute('checked'); 1.304 + } 1.305 + pref.value = autoStart.hasAttribute('checked'); 1.306 + mode.selectedIndex = this._lastMode; 1.307 + mode.doCommand(); 1.308 + 1.309 + this._shouldPromptForRestart = true; 1.310 + }, 1.311 + 1.312 + // HISTORY 1.313 + 1.314 + /** 1.315 + * Read the location bar enabled and suggestion prefs 1.316 + * @return Int value for suggestion menulist 1.317 + */ 1.318 + readSuggestionPref: function PPP_readSuggestionPref() 1.319 + { 1.320 + let getVal = function(aPref) 1.321 + document.getElementById("browser.urlbar." + aPref).value; 1.322 + 1.323 + // Suggest nothing if autocomplete is not enabled 1.324 + if (!getVal("autocomplete.enabled")) 1.325 + return -1; 1.326 + 1.327 + // Bottom 2 bits of default.behavior specify history/bookmark 1.328 + return getVal("default.behavior") & 3; 1.329 + }, 1.330 + 1.331 + /** 1.332 + * Write the location bar enabled and suggestion prefs when necessary 1.333 + * @return Bool value for enabled pref 1.334 + */ 1.335 + writeSuggestionPref: function PPP_writeSuggestionPref() 1.336 + { 1.337 + let menuVal = document.getElementById("locationBarSuggestion").value; 1.338 + let enabled = menuVal != -1; 1.339 + 1.340 + // Only update default.behavior if we're giving suggestions 1.341 + if (enabled) { 1.342 + // Put the selected menu item's value directly into the bottom 2 bits 1.343 + let behavior = document.getElementById("browser.urlbar.default.behavior"); 1.344 + behavior.value = behavior.value >> 2 << 2 | menuVal; 1.345 + } 1.346 + 1.347 + // Always update the enabled pref 1.348 + return enabled; 1.349 + }, 1.350 + 1.351 + /* 1.352 + * Preferences: 1.353 + * 1.354 + * places.history.enabled 1.355 + * - whether history is enabled or not 1.356 + * browser.formfill.enable 1.357 + * - true if entries in forms and the search bar should be saved, false 1.358 + * otherwise 1.359 + */ 1.360 + 1.361 + // COOKIES 1.362 + 1.363 + /* 1.364 + * Preferences: 1.365 + * 1.366 + * network.cookie.cookieBehavior 1.367 + * - determines how the browser should handle cookies: 1.368 + * 0 means enable all cookies 1.369 + * 1 means reject all third party cookies 1.370 + * 2 means disable all cookies 1.371 + * 3 means reject third party cookies unless at least one is already set for the eTLD 1.372 + * see netwerk/cookie/src/nsCookieService.cpp for details 1.373 + * network.cookie.lifetimePolicy 1.374 + * - determines how long cookies are stored: 1.375 + * 0 means keep cookies until they expire 1.376 + * 1 means ask how long to keep each cookie 1.377 + * 2 means keep cookies until the browser is closed 1.378 + */ 1.379 + 1.380 + /** 1.381 + * Reads the network.cookie.cookieBehavior preference value and 1.382 + * enables/disables the rest of the cookie UI accordingly, returning true 1.383 + * if cookies are enabled. 1.384 + */ 1.385 + readAcceptCookies: function () 1.386 + { 1.387 + var pref = document.getElementById("network.cookie.cookieBehavior"); 1.388 + var acceptThirdPartyLabel = document.getElementById("acceptThirdPartyLabel"); 1.389 + var acceptThirdPartyMenu = document.getElementById("acceptThirdPartyMenu"); 1.390 + var keepUntil = document.getElementById("keepUntil"); 1.391 + var menu = document.getElementById("keepCookiesUntil"); 1.392 + 1.393 + // enable the rest of the UI for anything other than "disable all cookies" 1.394 + var acceptCookies = (pref.value != 2); 1.395 + 1.396 + acceptThirdPartyLabel.disabled = acceptThirdPartyMenu.disabled = !acceptCookies; 1.397 + keepUntil.disabled = menu.disabled = this._autoStartPrivateBrowsing || !acceptCookies; 1.398 + 1.399 + return acceptCookies; 1.400 + }, 1.401 + 1.402 + /** 1.403 + * Enables/disables the "keep until" label and menulist in response to the 1.404 + * "accept cookies" checkbox being checked or unchecked. 1.405 + */ 1.406 + writeAcceptCookies: function () 1.407 + { 1.408 + var accept = document.getElementById("acceptCookies"); 1.409 + var acceptThirdPartyMenu = document.getElementById("acceptThirdPartyMenu"); 1.410 + 1.411 +#ifdef RELEASE_BUILD 1.412 + // if we're enabling cookies, automatically select 'accept third party always' 1.413 + if (accept.checked) 1.414 + acceptThirdPartyMenu.selectedIndex = 0; 1.415 + 1.416 + return accept.checked ? 0 : 2; 1.417 +#else 1.418 + // if we're enabling cookies, automatically select 'accept third party from visited' 1.419 + if (accept.checked) 1.420 + acceptThirdPartyMenu.selectedIndex = 1; 1.421 + 1.422 + return accept.checked ? 3 : 2; 1.423 +#endif 1.424 + }, 1.425 + 1.426 + /** 1.427 + * Converts between network.cookie.cookieBehavior and the third-party cookie UI 1.428 + */ 1.429 + readAcceptThirdPartyCookies: function () 1.430 + { 1.431 + var pref = document.getElementById("network.cookie.cookieBehavior"); 1.432 + switch (pref.value) 1.433 + { 1.434 + case 0: 1.435 + return "always"; 1.436 + case 1: 1.437 + return "never"; 1.438 + case 2: 1.439 + return "never"; 1.440 + case 3: 1.441 + return "visited"; 1.442 + default: 1.443 + return undefined; 1.444 + } 1.445 + }, 1.446 + 1.447 + writeAcceptThirdPartyCookies: function () 1.448 + { 1.449 + var accept = document.getElementById("acceptThirdPartyMenu").selectedItem; 1.450 + switch (accept.value) 1.451 + { 1.452 + case "always": 1.453 + return 0; 1.454 + case "visited": 1.455 + return 3; 1.456 + case "never": 1.457 + return 1; 1.458 + default: 1.459 + return undefined; 1.460 + } 1.461 + }, 1.462 + 1.463 + /** 1.464 + * Displays fine-grained, per-site preferences for cookies. 1.465 + */ 1.466 + showCookieExceptions: function () 1.467 + { 1.468 + var bundlePreferences = document.getElementById("bundlePreferences"); 1.469 + var params = { blockVisible : true, 1.470 + sessionVisible : true, 1.471 + allowVisible : true, 1.472 + prefilledHost : "", 1.473 + permissionType : "cookie", 1.474 + windowTitle : bundlePreferences.getString("cookiepermissionstitle"), 1.475 + introText : bundlePreferences.getString("cookiepermissionstext") }; 1.476 + openDialog("chrome://browser/content/preferences/permissions.xul", 1.477 + "Browser:Permissions", 1.478 + "modal=yes", params); 1.479 + }, 1.480 + 1.481 + /** 1.482 + * Displays all the user's cookies in a dialog. 1.483 + */ 1.484 + showCookies: function (aCategory) 1.485 + { 1.486 + openDialog("chrome://browser/content/preferences/cookies.xul", 1.487 + "Browser:Cookies", 1.488 + "modal=yes", null); 1.489 + }, 1.490 + 1.491 + // CLEAR PRIVATE DATA 1.492 + 1.493 + /* 1.494 + * Preferences: 1.495 + * 1.496 + * privacy.sanitize.sanitizeOnShutdown 1.497 + * - true if the user's private data is cleared on startup according to the 1.498 + * Clear Private Data settings, false otherwise 1.499 + */ 1.500 + 1.501 + /** 1.502 + * Displays the Clear Private Data settings dialog. 1.503 + */ 1.504 + showClearPrivateDataSettings: function () 1.505 + { 1.506 + openDialog("chrome://browser/content/preferences/sanitize.xul", 1.507 + "modal=yes", null); 1.508 + }, 1.509 + 1.510 + 1.511 + /** 1.512 + * Displays a dialog from which individual parts of private data may be 1.513 + * cleared. 1.514 + */ 1.515 + clearPrivateDataNow: function (aClearEverything) 1.516 + { 1.517 + var ts = document.getElementById("privacy.sanitize.timeSpan"); 1.518 + var timeSpanOrig = ts.value; 1.519 + if (aClearEverything) 1.520 + ts.value = 0; 1.521 + 1.522 + const Cc = Components.classes, Ci = Components.interfaces; 1.523 + var glue = Cc["@mozilla.org/browser/browserglue;1"] 1.524 + .getService(Ci.nsIBrowserGlue); 1.525 + glue.sanitize(window); 1.526 + 1.527 + // reset the timeSpan pref 1.528 + if (aClearEverything) 1.529 + ts.value = timeSpanOrig; 1.530 + }, 1.531 + 1.532 + /** 1.533 + * Enables or disables the "Settings..." button depending 1.534 + * on the privacy.sanitize.sanitizeOnShutdown preference value 1.535 + */ 1.536 + _updateSanitizeSettingsButton: function () { 1.537 + var settingsButton = document.getElementById("clearDataSettings"); 1.538 + var sanitizeOnShutdownPref = document.getElementById("privacy.sanitize.sanitizeOnShutdown"); 1.539 + 1.540 + settingsButton.disabled = !sanitizeOnShutdownPref.value; 1.541 + } 1.542 + 1.543 +};