browser/components/sessionstore/src/PrivacyLevel.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/src/PrivacyLevel.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,82 @@
     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 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["PrivacyLevel"];
    1.11 +
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/Services.jsm");
    1.15 +
    1.16 +const PREF_NORMAL = "browser.sessionstore.privacy_level";
    1.17 +const PREF_DEFERRED = "browser.sessionstore.privacy_level_deferred";
    1.18 +
    1.19 +// The following constants represent the different possible privacy levels that
    1.20 +// can be set by the user and that we need to consider when collecting text
    1.21 +// data, and cookies.
    1.22 +//
    1.23 +// Collect data from all sites (http and https).
    1.24 +const PRIVACY_NONE = 0;
    1.25 +// Collect data from unencrypted sites (http), only.
    1.26 +const PRIVACY_ENCRYPTED = 1;
    1.27 +// Collect no data.
    1.28 +const PRIVACY_FULL = 2;
    1.29 +
    1.30 +/**
    1.31 + * Returns whether we will resume the session automatically on next startup.
    1.32 + */
    1.33 +function willResumeAutomatically() {
    1.34 +  return Services.prefs.getIntPref("browser.startup.page") == 3 ||
    1.35 +         Services.prefs.getBoolPref("browser.sessionstore.resume_session_once");
    1.36 +}
    1.37 +
    1.38 +/**
    1.39 + * Determines the current privacy level as set by the user.
    1.40 + *
    1.41 + * @param isPinned
    1.42 + *        Whether to return the privacy level for pinned tabs.
    1.43 + * @return {int} The privacy level as read from the user's preferences.
    1.44 + */
    1.45 +function getCurrentLevel(isPinned) {
    1.46 +  let pref = PREF_NORMAL;
    1.47 +
    1.48 +  // If we're in the process of quitting and we're not autoresuming the session
    1.49 +  // then we will use the deferred privacy level for non-pinned tabs.
    1.50 +  if (!isPinned && Services.startup.shuttingDown && !willResumeAutomatically()) {
    1.51 +    pref = PREF_DEFERRED;
    1.52 +  }
    1.53 +
    1.54 +  return Services.prefs.getIntPref(pref);
    1.55 +}
    1.56 +
    1.57 +/**
    1.58 + * The external API as exposed by this module.
    1.59 + */
    1.60 +let PrivacyLevel = Object.freeze({
    1.61 +  /**
    1.62 +   * Checks whether we're allowed to save data for a specific site.
    1.63 +   *
    1.64 +   * @param {isHttps: boolean, isPinned: boolean}
    1.65 +   *        An object that must have two properties: 'isHttps' and 'isPinned'.
    1.66 +   *        'isHttps' tells whether the site us secure communication (HTTPS).
    1.67 +   *        'isPinned' tells whether the site is loaded in a pinned tab.
    1.68 +   * @return {bool} Whether we can save data for the specified site.
    1.69 +   */
    1.70 +  canSave: function ({isHttps, isPinned}) {
    1.71 +    let level = getCurrentLevel(isPinned);
    1.72 +
    1.73 +    // Never save any data when full privacy is requested.
    1.74 +    if (level == PRIVACY_FULL) {
    1.75 +      return false;
    1.76 +    }
    1.77 +
    1.78 +    // Don't save data for encrypted sites when requested.
    1.79 +    if (isHttps && level == PRIVACY_ENCRYPTED) {
    1.80 +      return false;
    1.81 +    }
    1.82 +
    1.83 +    return true;
    1.84 +  }
    1.85 +});

mercurial