DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (638459094c25)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

var EXPORTED_SYMBOLS = ["PrivateBrowsingUtils"];

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

function PrivateBrowsingContentBlockingAllowList() {
  Services.obs.addObserver(this, "last-pb-context-exited", true);
}

PrivateBrowsingContentBlockingAllowList.prototype = {
  QueryInterface: ChromeUtils.generateQI([
    Ci.nsIObserver,
    Ci.nsISupportsWeakReference,
  ]),

  /**
   * Add the provided URI to the list of allowed tracking sites.
   *
   * @param uri nsIURI
   *        The URI to add to the list.
   */
  addToAllowList(uri) {
    Services.perms.add(
      uri,
      "trackingprotection-pb",
      Ci.nsIPermissionManager.ALLOW_ACTION,
      Ci.nsIPermissionManager.EXPIRE_SESSION
    );
  },

  /**
   * Remove the provided URI from the list of allowed tracking sites.
   *
   * @param uri nsIURI
   *        The URI to remove from the list.
   */
  removeFromAllowList(uri) {
    Services.perms.remove(uri, "trackingprotection-pb");
  },

  observe(subject, topic, data) {
    if (topic == "last-pb-context-exited") {
      Services.perms.removeByType("trackingprotection-pb");
    }
  },
};

const kAutoStartPref = "browser.privatebrowsing.autostart";

// This will be set to true when the PB mode is autostarted from the command
// line for the current session.
var gTemporaryAutoStartMode = false;

var PrivateBrowsingUtils = {
  get enabled() {
    return Services.policies.isAllowed("privatebrowsing");
  },

  // Rather than passing content windows to this function, please use
  // isBrowserPrivate since it works with e10s.
  isWindowPrivate: function pbu_isWindowPrivate(aWindow) {
    if (!aWindow.isChromeWindow) {
      dump(
        "WARNING: content window passed to PrivateBrowsingUtils.isWindowPrivate. " +
          "Use isContentWindowPrivate instead (but only for frame scripts).\n" +
          new Error().stack
      );
    }

    return this.privacyContextFromWindow(aWindow).usePrivateBrowsing;
  },

  // This should be used only in frame scripts.
  isContentWindowPrivate: function pbu_isWindowPrivate(aWindow) {
    return this.privacyContextFromWindow(aWindow).usePrivateBrowsing;
  },

  isBrowserPrivate(aBrowser) {
    let chromeWin = aBrowser.ownerGlobal;
    if (chromeWin.gMultiProcessBrowser || !aBrowser.contentWindow) {
      // In e10s we have to look at the chrome window's private
      // browsing status since the only alternative is to check the
      // content window, which is in another process.  If the browser
      // is lazy or is running in windowless configuration then the
      // content window doesn't exist.
      return this.isWindowPrivate(chromeWin);
    }
    return this.privacyContextFromWindow(aBrowser.contentWindow)
      .usePrivateBrowsing;
  },

  privacyContextFromWindow: function pbu_privacyContextFromWindow(aWindow) {
    return aWindow.docShell.QueryInterface(Ci.nsILoadContext);
  },

  get _pbCBAllowList() {
    delete this._pbCBAllowList;
    return (this._pbCBAllowList = new PrivateBrowsingContentBlockingAllowList());
  },

  addToTrackingAllowlist(aURI) {
    this._pbCBAllowList.addToAllowList(aURI);
  },

  removeFromTrackingAllowlist(aURI) {
    this._pbCBAllowList.removeFromAllowList(aURI);
  },

  get permanentPrivateBrowsing() {
    try {
      return (
        gTemporaryAutoStartMode || Services.prefs.getBoolPref(kAutoStartPref)
      );
    } catch (e) {
      // The pref does not exist
      return false;
    }
  },

  // These should only be used from internal code
  enterTemporaryAutoStartMode: function pbu_enterTemporaryAutoStartMode() {
    gTemporaryAutoStartMode = true;
  },
  get isInTemporaryAutoStartMode() {
    return gTemporaryAutoStartMode;
  },
};