Bug 1537736 - Use HomePage.get()/set() more consistently r=nanj
authorRicky Rosario <rickyrosario@gmail.com>
Tue, 23 Apr 2019 18:30:07 +0000
changeset 470543 1ed5609db4afed63c7b9dd79e2426e907fd19458
parent 470542 b9a9068783f7848082d2a6559974ade2a78b3e6f
child 470544 aacb0c9ecddbc2e76361a843faf2d5a0b334c298
push id35908
push useraciure@mozilla.com
push dateWed, 24 Apr 2019 04:28:40 +0000
treeherdermozilla-central@c9f0730a57a6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnanj
bugs1537736
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1537736 - Use HomePage.get()/set() more consistently r=nanj MozReview-Commit-ID: 5PzW6ONmuKd Differential Revision: https://phabricator.services.mozilla.com/D28505
browser/base/content/browser.js
browser/components/BrowserGlue.jsm
browser/components/preferences/in-content/home.js
browser/components/preferences/in-content/main.js
browser/modules/HomePage.jsm
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -3757,17 +3757,17 @@ var homeButtonObserver = {
           return;
         }
 
         setTimeout(openHomeDialog, 0, urls.join("|"));
       }
     },
 
   onDragOver(aEvent) {
-      if (Services.prefs.prefIsLocked("browser.startup.homepage")) {
+      if (HomePage.locked) {
         return;
       }
       browserDragAndDrop.dragOver(aEvent);
       aEvent.dropEffect = "link";
     },
   onDragExit(aEvent) {
     },
 };
--- a/browser/components/BrowserGlue.jsm
+++ b/browser/components/BrowserGlue.jsm
@@ -2538,28 +2538,27 @@ BrowserGlue.prototype = {
       const kLocalHosts = new Set(["localhost", "127.0.0.1"]);
       hosts = hosts.split(/[ ,]+/).filter(host => !kLocalHosts.has(host)).join(", ");
       Services.prefs.setCharPref("network.proxy.no_proxies_on", hosts);
     }
 
     if (currentUIVersion < 81) {
       // Reset homepage pref for users who have it set to a default from before Firefox 4:
       //   <locale>.(start|start2|start3).mozilla.(com|org)
-      const HOMEPAGE_PREF = "browser.startup.homepage";
-      if (Services.prefs.prefHasUserValue(HOMEPAGE_PREF)) {
-        const DEFAULT = Services.prefs.getDefaultBranch(HOMEPAGE_PREF).getCharPref("");
-        let value = Services.prefs.getCharPref(HOMEPAGE_PREF);
+      if (HomePage.overridden) {
+        const DEFAULT = HomePage.getDefault();
+        let value = HomePage.get();
         let updated = value.replace(
           /https?:\/\/([\w\-]+\.)?start\d*\.mozilla\.(org|com)[^|]*/ig, DEFAULT);
         if (updated != value) {
           if (updated == DEFAULT) {
-            Services.prefs.clearUserPref(HOMEPAGE_PREF);
+            HomePage.reset();
           } else {
             value = updated;
-            Services.prefs.setCharPref(HOMEPAGE_PREF, value);
+            HomePage.set(value);
           }
         }
       }
     }
 
     // Update the migration version.
     Services.prefs.setIntPref("browser.migration.version", UI_VERSION);
   },
--- a/browser/components/preferences/in-content/home.js
+++ b/browser/components/preferences/in-content/home.js
@@ -31,16 +31,17 @@ Preferences.addAll([
 const HOMEPAGE_OVERRIDE_KEY = "homepage_override";
 const URL_OVERRIDES_TYPE = "url_overrides";
 const NEW_TAB_KEY = "newTabURL";
 
 var gHomePane = {
   HOME_MODE_FIREFOX_HOME: "0",
   HOME_MODE_BLANK: "1",
   HOME_MODE_CUSTOM: "2",
+  HOMEPAGE_PREF: "browser.startup.homepage",
   NEWTAB_ENABLED_PREF: "browser.newtabpage.enabled",
   ACTIVITY_STREAM_PREF_BRANCH: "browser.newtabpage.activity-stream.",
 
   get homePanePrefs() {
     return Preferences.getAll().filter(pref => pref.id.includes(this.ACTIVITY_STREAM_PREF_BRANCH));
   },
 
   get isPocketNewtabEnabled() {
@@ -84,86 +85,84 @@ var gHomePane = {
 
   /**
    * Listen for all preferences changes on the Home Tab in order to show or
    * hide the Restore Defaults button.
    */
   watchHomeTabPrefChange() {
     const observer = () => this.toggleRestoreDefaultsBtn();
     Services.prefs.addObserver(this.ACTIVITY_STREAM_PREF_BRANCH, observer);
-    Services.prefs.addObserver("browser.startup.homepage", observer);
+    Services.prefs.addObserver(this.HOMEPAGE_PREF, observer);
     Services.prefs.addObserver(this.NEWTAB_ENABLED_PREF, observer);
 
     window.addEventListener("unload", () => {
       Services.prefs.removeObserver(this.ACTIVITY_STREAM_PREF_BRANCH, observer);
-      Services.prefs.removeObserver("browser.startup.homepage", observer);
+      Services.prefs.removeObserver(this.HOMEPAGE_PREF, observer);
       Services.prefs.removeObserver(this.NEWTAB_ENABLED_PREF, observer);
     });
   },
 
   /**
    * _renderCustomSettings: Hides or shows the UI for setting a custom
    * homepage URL
    * @param {obj} options
    * @param {bool} options.shouldShow Should the custom UI be shown?
    * @param {bool} options.isControlled Is an extension controlling the home page?
    */
   _renderCustomSettings(options = {}) {
     let {shouldShow, isControlled} = options;
     const customSettingsContainerEl = document.getElementById("customSettings");
     const customUrlEl = document.getElementById("homePageUrl");
-    const homePref = Preferences.get("browser.startup.homepage");
+    const homePage = HomePage.get();
 
     const isHomePageCustom = isControlled || (!this._isHomePageDefaultValue() && !this.isHomePageBlank());
     if (typeof shouldShow === "undefined") {
       shouldShow = isHomePageCustom;
     }
     customSettingsContainerEl.hidden = !shouldShow;
 
     // We can't use isHomePageDefaultValue and isHomePageBlank here because we want to disregard the blank
     // possibility triggered by the browser.startup.page being 0.
+    // We also skip when HomePage is locked because it might be locked to a default that isn't "about:home"
+    // (and it makes existing tests happy).
     let newValue;
-    if (homePref.value !== homePref.defaultValue && homePref.value !== "about:blank") {
-      newValue = homePref.value;
+    if (homePage === "about:blank" || (HomePage.isDefault && !HomePage.locked)) {
+      newValue = "";
     } else {
-      newValue = "";
+      newValue = homePage;
     }
     if (customUrlEl.value !== newValue) {
       customUrlEl.value = newValue;
     }
   },
 
   /**
    * _isHomePageDefaultValue
-   * @param {bool} isControlled Is an extension controlling the home page?
    * @returns {bool} Is the homepage set to the default pref value?
    */
   _isHomePageDefaultValue() {
     const startupPref = Preferences.get("browser.startup.page");
-    const homePref = Preferences.get("browser.startup.homepage");
-    return startupPref.value !== gMainPane.STARTUP_PREF_BLANK && homePref.value === homePref.defaultValue;
+    return startupPref.value !== gMainPane.STARTUP_PREF_BLANK && HomePage.isDefault;
   },
 
   /**
    * isHomePageBlank
    * @returns {bool} Is the homepage set to about:blank?
    */
   isHomePageBlank() {
     const startupPref = Preferences.get("browser.startup.page");
-    const homePref = Preferences.get("browser.startup.homepage");
-    return homePref.value === "about:blank" || homePref.value === "" || startupPref.value === gMainPane.STARTUP_PREF_BLANK;
+    return ["about:blank", ""].includes(HomePage.get()) || startupPref.value === gMainPane.STARTUP_PREF_BLANK;
   },
   /**
    * isHomePageControlled
    * @resolves {bool} Is the homepage being controlled by an extension?
    * @returns {Promise}
    */
   isHomePageControlled() {
-    const homePref = Preferences.get("browser.startup.homepage");
-    if (homePref.locked) {
+    if (HomePage.locked) {
       return Promise.resolve(false);
     }
     return handleControllingExtension(PREF_SETTING_TYPE, HOMEPAGE_OVERRIDE_KEY);
   },
 
   /**
    * _isTabAboutPreferences: Is a given tab set to about:preferences?
    * @param {Element} aTab A tab element
@@ -218,30 +217,34 @@ var gHomePane = {
     // Disable or enable the inputs based on if this is controlled by an extension.
     document.querySelectorAll(".check-home-page-controlled")
       .forEach((element) => {
         let isDisabled;
         let pref = element.getAttribute("preference") || element.getAttribute("data-preference-related");
         if (!pref) {
           throw new Error(`Element with id ${element.id} did not have preference or data-preference-related attribute defined.`);
         }
-        isDisabled = Preferences.get(pref).locked || isControlled;
+
+        if (pref === this.HOMEPAGE_PREF) {
+          isDisabled = HomePage.locked || isControlled;
+        } else {
+          isDisabled = Preferences.get(pref).locked || isControlled;
+        }
 
         if (pref === "pref.browser.disable_button.current_page") {
           // Special case for current_page to disable it if tabCount is 0
           isDisabled = isDisabled || tabCount < 1;
         }
 
         element.disabled = isDisabled;
       });
   },
 
   async _handleHomePageOverrides() {
-    const homePref = Preferences.get("browser.startup.homepage");
-    if (homePref.locked) {
+    if (HomePage.locked) {
       // An extension can't control these settings if they're locked.
       hideControllingExtension(HOMEPAGE_OVERRIDE_KEY);
       this._setInputDisabledStates(false);
     } else {
       const isControlled = await this.isHomePageControlled();
       this._setInputDisabledStates(isControlled);
       this._renderCustomSettings({isControlled});
       this._renderHomepageMode(isControlled);
@@ -262,32 +265,31 @@ var gHomePane = {
 
   syncToNewTabPref(value) {
     return value !== this.HOME_MODE_BLANK;
   },
 
   onMenuChange(event) {
     const {value} = event.target;
     const startupPref = Preferences.get("browser.startup.page");
-    const homePref = Preferences.get("browser.startup.homepage");
 
     switch (value) {
       case this.HOME_MODE_FIREFOX_HOME:
         if (startupPref.value === gMainPane.STARTUP_PREF_BLANK) {
           startupPref.value = gMainPane.STARTUP_PREF_HOMEPAGE;
         }
-        if (homePref.value !== homePref.defaultValue) {
-          Services.prefs.clearUserPref(homePref.id);
+        if (!HomePage.isDefault) {
+          HomePage.reset();
         } else {
           this._renderCustomSettings({shouldShow: false});
         }
         break;
       case this.HOME_MODE_BLANK:
-        if (homePref.value !== "about:blank") {
-          homePref.value = "about:blank";
+        if (HomePage.get() !== "about:blank") {
+          HomePage.set("about:blank");
         } else {
           this._renderCustomSettings({shouldShow: false});
         }
         break;
       case this.HOME_MODE_CUSTOM:
         if (startupPref.value === gMainPane.STARTUP_PREF_BLANK) {
           Services.prefs.clearUserPref(startupPref.id);
         }
@@ -320,38 +322,35 @@ var gHomePane = {
     useCurrent.disabled = tabCount < 1;
   },
 
   /**
    * Sets the home page to the URL(s) of any currently opened tab(s),
    * updating about:preferences#home UI to reflect this.
    */
   setHomePageToCurrent() {
-    let homePage = Preferences.get("browser.startup.homepage");
     let tabs = this._getTabsForHomePage();
     function getTabURI(t) {
       return t.linkedBrowser.currentURI.spec;
     }
 
     // FIXME Bug 244192: using dangerous "|" joiner!
     if (tabs.length) {
-      homePage.value = tabs.map(getTabURI).join("|");
+      HomePage.set(tabs.map(getTabURI).join("|"));
     }
 
     Services.telemetry.scalarAdd("preferences.use_current_page", 1);
   },
 
   _setHomePageToBookmarkClosed(rv, aEvent) {
     if (aEvent.detail.button != "accept")
       return;
     if (rv.urls && rv.names) {
-      let homePage = Preferences.get("browser.startup.homepage");
-
       // XXX still using dangerous "|" joiner!
-      homePage.value = rv.urls.join("|");
+      HomePage.set(rv.urls.join("|"));
     }
   },
 
    /**
    * Displays a dialog in which the user can select a bookmark to use as home
    * page.  If the user selects a bookmark, that bookmark's name is displayed in
    * UI and the bookmark's address is stored to the home page preference.
    */
@@ -359,17 +358,17 @@ var gHomePane = {
     const rv = { urls: null, names: null };
     gSubDialog.open("chrome://browser/content/preferences/selectBookmark.xul",
       "resizable=yes, modal=yes", rv,
       this._setHomePageToBookmarkClosed.bind(this, rv));
     Services.telemetry.scalarAdd("preferences.use_bookmark", 1);
   },
 
   restoreDefaultHomePage() {
-    Services.prefs.clearUserPref("browser.startup.homepage");
+    HomePage.reset();
     Services.prefs.clearUserPref(this.NEWTAB_ENABLED_PREF);
   },
 
   onCustomHomePageInput(event) {
     if (this._telemetryHomePageTimer) {
       clearTimeout(this._telemetryHomePageTimer);
     }
     let browserHomePage = event.target.value;
@@ -380,31 +379,29 @@ var gHomePane = {
         let homePageNumber = browserHomePage.split("|").length;
         Services.telemetry.scalarAdd("preferences.browser_home_page_change", 1);
         Services.telemetry.keyedScalarAdd("preferences.browser_home_page_count", homePageNumber, 1);
       }, 3000);
     }
   },
 
   onCustomHomePageChange(event) {
-    const homePref = Preferences.get("browser.startup.homepage");
-    const value = event.target.value || homePref.defaultValue;
-    homePref.value = value;
+    const value = event.target.value || HomePage.getDefault();
+    HomePage.set(value);
   },
 
   /**
    * Check all Home Tab preferences for user set values.
    */
   _changedHomeTabDefaultPrefs() {
     // If Discovery Stream is enabled Firefox Home Content preference options are hidden
     const homeContentChanged = !this.isPocketNewtabEnabled && this.homePanePrefs.some(pref => pref.hasUserValue);
-    const homePref = Preferences.get("browser.startup.homepage");
     const newtabPref = Preferences.get(this.NEWTAB_ENABLED_PREF);
 
-    return homeContentChanged || homePref.hasUserValue || newtabPref.hasUserValue;
+    return homeContentChanged || HomePage.overridden || newtabPref.hasUserValue;
   },
 
   /**
    * Show the Restore Defaults button if any preference on the Home tab was
    * changed, or hide it otherwise.
    */
   toggleRestoreDefaultsBtn() {
     const btn = document.getElementById("restoreDefaultHomePageBtn");
--- a/browser/components/preferences/in-content/main.js
+++ b/browser/components/preferences/in-content/main.js
@@ -9,16 +9,17 @@
 
 var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 var {Downloads} = ChromeUtils.import("resource://gre/modules/Downloads.jsm");
 var {FileUtils} = ChromeUtils.import("resource://gre/modules/FileUtils.jsm");
 var {TransientPrefs} = ChromeUtils.import("resource:///modules/TransientPrefs.jsm");
 var {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
 var {L10nRegistry} = ChromeUtils.import("resource://gre/modules/L10nRegistry.jsm");
 var {Localization} = ChromeUtils.import("resource://gre/modules/Localization.jsm");
+var {HomePage} = ChromeUtils.import("resource:///modules/HomePage.jsm");
 ChromeUtils.defineModuleGetter(this, "CloudStorage",
   "resource://gre/modules/CloudStorage.jsm");
 ChromeUtils.defineModuleGetter(this, "SelectionChangedMenulist",
                                "resource:///modules/SelectionChangedMenulist.jsm");
 ChromeUtils.defineModuleGetter(this, "UpdateUtils",
   "resource://gre/modules/UpdateUtils.jsm");
 
 XPCOMUtils.defineLazyServiceGetters(this, {
@@ -873,17 +874,17 @@ var gMainPane = {
     const value = event.target.checked;
     const startupPref = Preferences.get("browser.startup.page");
     let newValue;
 
     let warnOnQuitCheckbox = document.getElementById("browserRestoreSessionQuitWarning");
     if (value) {
       // We need to restore the blank homepage setting in our other pref
       if (startupPref.value === this.STARTUP_PREF_BLANK) {
-        Preferences.get("browser.startup.homepage").value = "about:blank";
+        HomePage.set("about:blank");
       }
       newValue = this.STARTUP_PREF_RESTORE_SESSION;
       let warnOnQuitPref = Preferences.get("browser.sessionstore.warnOnQuit");
       if (!warnOnQuitPref.locked) {
         warnOnQuitCheckbox.removeAttribute("disabled");
       }
     } else {
       newValue = this.STARTUP_PREF_HOMEPAGE;
--- a/browser/modules/HomePage.jsm
+++ b/browser/modules/HomePage.jsm
@@ -66,16 +66,24 @@ let HomePage = {
   getDefault() {
     return getHomepagePref(true);
   },
 
   get overridden() {
     return Services.prefs.prefHasUserValue(kPrefName);
   },
 
+  get locked() {
+    return Services.prefs.prefIsLocked(kPrefName);
+  },
+
+  get isDefault() {
+    return HomePage.get() === HomePage.getDefault();
+  },
+
   set(value) {
     Services.prefs.setStringPref(kPrefName, value);
   },
 
   reset() {
     Services.prefs.clearUserPref(kPrefName);
   },
 };