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 (329340b1608d)

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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
/* 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/. */

/*
 * This module is in charge of preloading 'new tab' pages for use when
 * the user opens a new tab.
 */

var EXPORTED_SYMBOLS = ["NewTabPagePreloading"];

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

XPCOMUtils.defineLazyModuleGetters(this, {
  BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
  E10SUtils: "resource://gre/modules/E10SUtils.jsm",
  PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
});

XPCOMUtils.defineLazyServiceGetters(this, {
  gAboutNewTabService: [
    "@mozilla.org/browser/aboutnewtab-service;1",
    "nsIAboutNewTabService",
  ],
});

let NewTabPagePreloading = {
  // Maximum number of instances of a given page we'll preload at any time.
  // Because we preload about:newtab for normal windows, and about:privatebrowsing
  // for private ones, we could have 3 of each.
  MAX_COUNT: 3,

  // How many preloaded tabs we have, across all windows, for the private and non-private
  // case:
  browserCounts: {
    normal: 0,
    private: 0,
  },

  get enabled() {
    return (
      this.prefEnabled && this.newTabEnabled && !gAboutNewTabService.overridden
    );
  },

  /**
   * Create a browser in the right process type.
   */
  _createBrowser(win) {
    const {
      gBrowser,
      gMultiProcessBrowser,
      gFissionBrowser,
      BROWSER_NEW_TAB_URL,
    } = win;

    let remoteType = E10SUtils.getRemoteTypeForURI(
      BROWSER_NEW_TAB_URL,
      gMultiProcessBrowser,
      gFissionBrowser
    );
    let browser = gBrowser.createBrowser({
      isPreloadBrowser: true,
      remoteType,
    });
    gBrowser.preloadedBrowser = browser;

    let panel = gBrowser.getPanel(browser);
    gBrowser.tabpanels.appendChild(panel);

    return browser;
  },

  /**
   * Move the contents of a preload browser across to a different window.
   */
  _adoptBrowserFromOtherWindow(window) {
    let winPrivate = PrivateBrowsingUtils.isWindowPrivate(window);
    // Grab the least-recently-focused window with a preloaded browser:
    let oldWin = BrowserWindowTracker.orderedWindows
      .filter(w => {
        return (
          winPrivate == PrivateBrowsingUtils.isWindowPrivate(w) &&
          w.gBrowser &&
          w.gBrowser.preloadedBrowser
        );
      })
      .pop();
    if (!oldWin) {
      return null;
    }
    // Don't call getPreloadedBrowser because it'll consume the browser:
    let oldBrowser = oldWin.gBrowser.preloadedBrowser;
    oldWin.gBrowser.preloadedBrowser = null;

    let newBrowser = this._createBrowser(window);

    oldWin.gBrowser._outerWindowIDBrowserMap.delete(oldBrowser.outerWindowID);
    window.gBrowser._outerWindowIDBrowserMap.delete(newBrowser.outerWindowID);

    oldBrowser.swapBrowsers(newBrowser);

    // Switch outerWindowIDs for remote browsers.
    if (newBrowser.isRemoteBrowser) {
      newBrowser._outerWindowID = oldBrowser._outerWindowID;
    }

    window.gBrowser._outerWindowIDBrowserMap.set(
      newBrowser.outerWindowID,
      newBrowser
    );
    newBrowser.permanentKey = oldBrowser.permanentKey;

    oldWin.gBrowser.getPanel(oldBrowser).remove();
    return newBrowser;
  },

  maybeCreatePreloadedBrowser(window) {
    // If we're not enabled, have already got one, or are in a popup window,
    // don't bother creating a preload browser - there's no point.
    if (
      !this.enabled ||
      window.gBrowser.preloadedBrowser ||
      !window.toolbar.visible
    ) {
      return;
    }

    // Don't bother creating a preload browser if we're not in the top set of windows:
    let windowPrivate = PrivateBrowsingUtils.isWindowPrivate(window);
    let countKey = windowPrivate ? "private" : "normal";
    let topWindows = BrowserWindowTracker.orderedWindows.filter(
      w => PrivateBrowsingUtils.isWindowPrivate(w) == windowPrivate
    );
    if (topWindows.indexOf(window) >= this.MAX_COUNT) {
      return;
    }

    // If we're in the top set of windows, and we already have enough preloaded
    // tabs, don't create yet another one, just steal an existing one:
    if (this.browserCounts[countKey] >= this.MAX_COUNT) {
      let browser = this._adoptBrowserFromOtherWindow(window);
      // We can potentially get null here if we couldn't actually find another
      // browser to adopt from. This can be the case when there's a mix of
      // private and non-private windows, for instance.
      if (browser) {
        return;
      }
    }

    let browser = this._createBrowser(window);
    browser.loadURI(window.BROWSER_NEW_TAB_URL, {
      triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
    });
    browser.docShellIsActive = false;
    browser._urlbarFocused = true;

    // Make sure the preloaded browser is loaded with desired zoom level
    let tabURI = Services.io.newURI(window.BROWSER_NEW_TAB_URL);
    window.FullZoom.onLocationChange(tabURI, false, browser);

    this.browserCounts[countKey]++;
  },

  getPreloadedBrowser(window) {
    if (!this.enabled) {
      return null;
    }

    // The preloaded browser might be null.
    let browser = window.gBrowser.preloadedBrowser;

    // Consume the browser.
    window.gBrowser.preloadedBrowser = null;

    // Attach the nsIFormFillController now that we know the browser
    // will be used. If we do that before and the preloaded browser
    // won't be consumed until shutdown then we leak a docShell.
    // Also, we do not need to take care of attaching nsIFormFillControllers
    // in the case that the browser is remote, as remote browsers take
    // care of that themselves.
    if (browser) {
      let countKey = PrivateBrowsingUtils.isWindowPrivate(window)
        ? "private"
        : "normal";
      this.browserCounts[countKey]--;
      browser.setAttribute("preloadedState", "consumed");
      browser.setAttribute("autocompletepopup", "PopupAutoComplete");
    }

    return browser;
  },

  removePreloadedBrowser(window) {
    let browser = this.getPreloadedBrowser(window);
    if (browser) {
      window.gBrowser.getPanel(browser).remove();
    }
  },
};

XPCOMUtils.defineLazyPreferenceGetter(
  NewTabPagePreloading,
  "prefEnabled",
  "browser.newtab.preload",
  true
);
XPCOMUtils.defineLazyPreferenceGetter(
  NewTabPagePreloading,
  "newTabEnabled",
  "browser.newtabpage.enabled",
  true
);