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 (d09edbe2abfe)

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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/* 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/. */

"use strict";

var EXPORTED_SYMBOLS = ["HomePage"];

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

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

const kPrefName = "browser.startup.homepage";
const kDefaultHomePage = "about:home";
const kExtensionControllerPref =
  "browser.startup.homepage_override.extensionControlled";
const kHomePageIgnoreListId = "homepage-urls";

function getHomepagePref(useDefault) {
  let homePage;
  let prefs = Services.prefs;
  if (useDefault) {
    prefs = prefs.getDefaultBranch(null);
  }
  try {
    // Historically, this was a localizable pref, but default Firefox builds
    // don't use this.
    // Distributions and local customizations might still use this, so let's
    // keep it.
    homePage = prefs.getComplexValue(kPrefName, Ci.nsIPrefLocalizedString).data;
  } catch (ex) {}

  if (!homePage) {
    homePage = prefs.getStringPref(kPrefName);
  }

  // Apparently at some point users ended up with blank home pages somehow.
  // If that happens, reset the pref and read it again.
  if (!homePage && !useDefault) {
    Services.prefs.clearUserPref(kPrefName);
    homePage = getHomepagePref(true);
  }

  return homePage;
}

/**
 * HomePage provides tools to keep try of the current homepage, and the
 * applications's default homepage. It includes tools to insure that certain
 * urls are ignored. As a result, all set/get requests for the homepage
 * preferences should be routed through here.
 */
let HomePage = {
  // This is an array of strings that should be matched against URLs to see
  // if they should be ignored or not.
  _ignoreList: [],

  // A promise that is set when initialization starts and resolved when it
  // completes.
  _initializationPromise: null,

  /**
   * Used to initialise the ignore lists. This may be called later than
   * the first call to get or set, which may cause a used to get an ignored
   * homepage, but this is deemed acceptable, as we'll correct it once
   * initialised.
   */
  async init() {
    if (this._initializationPromise) {
      await this._initializationPromise;
      return;
    }

    Services.telemetry.setEventRecordingEnabled("homepage", true);

    // Now we have the values, listen for future updates.
    this._ignoreListListener = this._handleIgnoreListUpdated.bind(this);

    this._initializationPromise = IgnoreLists.getAndSubscribe(
      this._ignoreListListener
    );

    const current = await this._initializationPromise;

    await this._handleIgnoreListUpdated({ data: { current } });
  },

  /**
   * Gets the homepage for the given window.
   *
   * @param {DOMWindow} [aWindow]
   *   The window associated with the get, used to check for private browsing
   *   mode. If not supplied, normal mode is assumed.
   * @returns {string}
   *   Returns the home page value, this could be a single url, or a `|`
   *   separated list of URLs.
   */
  get(aWindow) {
    let homePages = getHomepagePref();
    if (
      PrivateBrowsingUtils.permanentPrivateBrowsing ||
      (aWindow && PrivateBrowsingUtils.isWindowPrivate(aWindow))
    ) {
      // If an extension controls the setting and does not have private
      // browsing permission, use the default setting.
      let extensionControlled = Services.prefs.getBoolPref(
        kExtensionControllerPref,
        false
      );
      let privateAllowed = Services.prefs.getBoolPref(
        "browser.startup.homepage_override.privateAllowed",
        false
      );
      // There is a potential on upgrade that the prefs are not set yet, so we double check
      // for moz-extension.
      if (
        !privateAllowed &&
        (extensionControlled || homePages.includes("moz-extension://"))
      ) {
        return this.getDefault();
      }
    }

    return homePages;
  },

  /**
   * @returns {string}
   *   Returns the application default homepage.
   */
  getDefault() {
    return getHomepagePref(true);
  },

  /**
   * @returns {boolean}
   *   Returns true if the homepage has been changed.
   */
  get overridden() {
    return Services.prefs.prefHasUserValue(kPrefName);
  },

  /**
   * @returns {boolean}
   *   Returns true if the homepage preference is locked.
   */
  get locked() {
    return Services.prefs.prefIsLocked(kPrefName);
  },

  /**
   * @returns {boolean}
   *   Returns true if the current homepage is the application default.
   */
  get isDefault() {
    return HomePage.get() === kDefaultHomePage;
  },

  /**
   * Sets the homepage preference to a new page.
   *
   * @param {string} value
   *   The new value to set the preference to. This could be a single url, or a
   *   `|` separated list of URLs.
   */
  async set(value) {
    await this.init();

    if (await this.shouldIgnore(value)) {
      Cu.reportError(
        `Ignoring homepage setting for ${value} as it is on the ignore list.`
      );
      Services.telemetry.recordEvent(
        "homepage",
        "preference",
        "ignore",
        "set_blocked"
      );
      return false;
    }
    Services.prefs.setStringPref(kPrefName, value);
    return true;
  },

  /**
   * Sets the homepage preference to a new page. This is an synchronous version
   * that should only be used when we know the source is safe as it bypasses the
   * ignore list, e.g. when setting directly to about:blank or a value not
   * supplied externally.
   *
   * @param {string} value
   *   The new value to set the preference to. This could be a single url, or a
   *   `|` separated list of URLs.
   */
  safeSet(value) {
    Services.prefs.setStringPref(kPrefName, value);
  },

  /**
   * Clears the homepage preference if it is not the default. Note that for
   * policy/locking use, the default homepage might not be about:home after this.
   */
  clear() {
    Services.prefs.clearUserPref(kPrefName);
  },

  /**
   * Resets the homepage preference to be about:home.
   */
  reset() {
    Services.prefs.setStringPref(kPrefName, kDefaultHomePage);
  },

  /**
   * Determines if a url should be ignored according to the ignore list.
   *
   * @param {string} url
   *   A string that is the url or urls to be ignored.
   * @returns {boolean}
   *   True if the url should be ignored.
   */
  async shouldIgnore(url) {
    await this.init();

    const lowerURL = url.toLowerCase();
    return this._ignoreList.some(code => lowerURL.includes(code));
  },

  /**
   * Handles updates of the ignore list, checking the existing preference and
   * correcting it as necessary.
   *
   * @param {Object} eventData
   *   The event data as received from RemoteSettings.
   */
  async _handleIgnoreListUpdated({ data: { current } }) {
    for (const entry of current) {
      if (entry.id == kHomePageIgnoreListId) {
        this._ignoreList = [...entry.matches];
      }
    }

    // Only check if we're overridden as we assume the default value is fine,
    // or won't be changeable (e.g. enterprise policy).
    if (this.overridden) {
      let homePages = getHomepagePref().toLowerCase();
      if (
        this._ignoreList.some(code => homePages.includes(code.toLowerCase()))
      ) {
        this.clear();
        Services.prefs.clearUserPref(kExtensionControllerPref);
        Services.telemetry.recordEvent(
          "homepage",
          "preference",
          "ignore",
          "saved_reset"
        );
      }
    }
  },
};