summaryrefslogtreecommitdiffstats
path: root/browser/modules/HomePage.sys.mjs
blob: 612e559e2c2f705bc4d3591afe973f0edc031e8b (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* 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/. */

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  CustomizableUI: "resource:///modules/CustomizableUI.sys.mjs",
  ExtensionParent: "resource://gre/modules/ExtensionParent.sys.mjs",
  ExtensionPreferencesManager:
    "resource://gre/modules/ExtensionPreferencesManager.sys.mjs",
  IgnoreLists: "resource://gre/modules/IgnoreLists.sys.mjs",
  PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
});

const kPrefName = "browser.startup.homepage";
const kDefaultHomePage = "about:home";
const kExtensionControllerPref =
  "browser.startup.homepage_override.extensionControlled";
const kHomePageIgnoreListId = "homepage-urls";
const kWidgetId = "home-button";
const kWidgetRemovedPref = "browser.engagement.home-button.has-removed";

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 track 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.
 */
export 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 delayedStartup() {
    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 = lazy.IgnoreLists.getAndSubscribe(
      this._ignoreListListener
    );

    this._addCustomizableUiListener();

    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 (
      lazy.PrivateBrowsingUtils.permanentPrivateBrowsing ||
      (aWindow && lazy.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();
      }
    }

    if (homePages == "about:blank") {
      homePages = "chrome://browser/content/blanktab.html";
    }

    return homePages;
  },

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

  /**
   * @returns {string}
   *   Returns the original application homepage URL (not from prefs).
   */
  getOriginalDefault() {
    return kDefaultHomePage;
  },

  /**
   * @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.delayedStartup();

    if (await this.shouldIgnore(value)) {
      console.error(
        `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);
    this._maybeAddHomeButtonToToolbar(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.delayedStartup();

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

  /**
   * 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()))
      ) {
        if (Services.prefs.getBoolPref(kExtensionControllerPref, false)) {
          if (Services.appinfo.inSafeMode) {
            // Add-ons don't get started in safe mode, so just abort this.
            // We'll get to remove them when we next start in normal mode.
            return;
          }
          // getSetting does not need the module to be loaded.
          const item = await lazy.ExtensionPreferencesManager.getSetting(
            "homepage_override"
          );
          if (item && item.id) {
            // During startup some modules may not be loaded yet, so we load
            // the setting we need prior to removal.
            await lazy.ExtensionParent.apiManager.asyncLoadModule(
              "chrome_settings_overrides"
            );
            lazy.ExtensionPreferencesManager.removeSetting(
              item.id,
              "homepage_override"
            ).catch(console.error);
          } else {
            // If we don't have a setting for it, we assume the pref has
            // been incorrectly set somehow.
            Services.prefs.clearUserPref(kExtensionControllerPref);
            Services.prefs.clearUserPref(
              "browser.startup.homepage_override.privateAllowed"
            );
          }
        } else {
          this.clear();
        }
        Services.telemetry.recordEvent(
          "homepage",
          "preference",
          "ignore",
          "saved_reset"
        );
      }
    }
  },

  onWidgetRemoved(widgetId) {
    if (widgetId == kWidgetId) {
      Services.prefs.setBoolPref(kWidgetRemovedPref, true);
      lazy.CustomizableUI.removeListener(this);
    }
  },

  /**
   * Add the home button to the toolbar if the user just set a custom homepage.
   *
   * This should only be done once, so we check HOME_BUTTON_REMOVED_PREF which
   * gets set to true when the home button is removed from the toolbar.
   *
   * If the home button is already on the toolbar it won't be moved.
   */
  _maybeAddHomeButtonToToolbar(homePage) {
    if (
      homePage !== "about:home" &&
      homePage !== "about:blank" &&
      !Services.prefs.getBoolPref(kExtensionControllerPref, false) &&
      !Services.prefs.getBoolPref(kWidgetRemovedPref, false) &&
      !lazy.CustomizableUI.getWidget(kWidgetId).areaType
    ) {
      // Find a spot for the home button, ideally it will be in its default
      // position beside the stop/refresh button.
      // Work backwards from the URL bar since it can't be removed and put
      // the button after the first non-spring we find.
      let navbarPlacements = lazy.CustomizableUI.getWidgetIdsInArea("nav-bar");
      let position = navbarPlacements.indexOf("urlbar-container");
      for (let i = position - 1; i >= 0; i--) {
        if (!navbarPlacements[i].startsWith("customizableui-special-spring")) {
          position = i + 1;
          break;
        }
      }
      lazy.CustomizableUI.addWidgetToArea(kWidgetId, "nav-bar", position);
    }
  },

  _addCustomizableUiListener() {
    if (!Services.prefs.getBoolPref(kWidgetRemovedPref, false)) {
      lazy.CustomizableUI.addListener(this);
    }
  },
};