michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["PrivacyLevel"]; michael@0: michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: const PREF_NORMAL = "browser.sessionstore.privacy_level"; michael@0: const PREF_DEFERRED = "browser.sessionstore.privacy_level_deferred"; michael@0: michael@0: // The following constants represent the different possible privacy levels that michael@0: // can be set by the user and that we need to consider when collecting text michael@0: // data, and cookies. michael@0: // michael@0: // Collect data from all sites (http and https). michael@0: const PRIVACY_NONE = 0; michael@0: // Collect data from unencrypted sites (http), only. michael@0: const PRIVACY_ENCRYPTED = 1; michael@0: // Collect no data. michael@0: const PRIVACY_FULL = 2; michael@0: michael@0: /** michael@0: * Returns whether we will resume the session automatically on next startup. michael@0: */ michael@0: function willResumeAutomatically() { michael@0: return Services.prefs.getIntPref("browser.startup.page") == 3 || michael@0: Services.prefs.getBoolPref("browser.sessionstore.resume_session_once"); michael@0: } michael@0: michael@0: /** michael@0: * Determines the current privacy level as set by the user. michael@0: * michael@0: * @param isPinned michael@0: * Whether to return the privacy level for pinned tabs. michael@0: * @return {int} The privacy level as read from the user's preferences. michael@0: */ michael@0: function getCurrentLevel(isPinned) { michael@0: let pref = PREF_NORMAL; michael@0: michael@0: // If we're in the process of quitting and we're not autoresuming the session michael@0: // then we will use the deferred privacy level for non-pinned tabs. michael@0: if (!isPinned && Services.startup.shuttingDown && !willResumeAutomatically()) { michael@0: pref = PREF_DEFERRED; michael@0: } michael@0: michael@0: return Services.prefs.getIntPref(pref); michael@0: } michael@0: michael@0: /** michael@0: * The external API as exposed by this module. michael@0: */ michael@0: let PrivacyLevel = Object.freeze({ michael@0: /** michael@0: * Checks whether we're allowed to save data for a specific site. michael@0: * michael@0: * @param {isHttps: boolean, isPinned: boolean} michael@0: * An object that must have two properties: 'isHttps' and 'isPinned'. michael@0: * 'isHttps' tells whether the site us secure communication (HTTPS). michael@0: * 'isPinned' tells whether the site is loaded in a pinned tab. michael@0: * @return {bool} Whether we can save data for the specified site. michael@0: */ michael@0: canSave: function ({isHttps, isPinned}) { michael@0: let level = getCurrentLevel(isPinned); michael@0: michael@0: // Never save any data when full privacy is requested. michael@0: if (level == PRIVACY_FULL) { michael@0: return false; michael@0: } michael@0: michael@0: // Don't save data for encrypted sites when requested. michael@0: if (isHttps && level == PRIVACY_ENCRYPTED) { michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: });