summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/private
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:44:51 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:44:51 +0000
commit9e3c08db40b8916968b9f30096c7be3f00ce9647 (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /browser/components/urlbar/private
parentInitial commit. (diff)
downloadthunderbird-upstream.tar.xz
thunderbird-upstream.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/urlbar/private')
-rw-r--r--browser/components/urlbar/private/AddonSuggestions.sys.mjs424
-rw-r--r--browser/components/urlbar/private/AdmWikipedia.sys.mjs280
-rw-r--r--browser/components/urlbar/private/BaseFeature.sys.mjs168
-rw-r--r--browser/components/urlbar/private/BlockedSuggestions.sys.mjs187
-rw-r--r--browser/components/urlbar/private/ImpressionCaps.sys.mjs566
-rw-r--r--browser/components/urlbar/private/QuickSuggestRemoteSettings.sys.mjs395
-rw-r--r--browser/components/urlbar/private/Weather.sys.mjs499
7 files changed, 2519 insertions, 0 deletions
diff --git a/browser/components/urlbar/private/AddonSuggestions.sys.mjs b/browser/components/urlbar/private/AddonSuggestions.sys.mjs
new file mode 100644
index 0000000000..7232ad99f8
--- /dev/null
+++ b/browser/components/urlbar/private/AddonSuggestions.sys.mjs
@@ -0,0 +1,424 @@
+/* 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/. */
+
+import { BaseFeature } from "resource:///modules/urlbar/private/BaseFeature.sys.mjs";
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
+ QuickSuggest: "resource:///modules/QuickSuggest.sys.mjs",
+ QuickSuggestRemoteSettings:
+ "resource:///modules/urlbar/private/QuickSuggestRemoteSettings.sys.mjs",
+ SuggestionsMap:
+ "resource:///modules/urlbar/private/QuickSuggestRemoteSettings.sys.mjs",
+ UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
+ UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
+ UrlbarUtils: "resource:///modules/UrlbarUtils.sys.mjs",
+ UrlbarView: "resource:///modules/UrlbarView.sys.mjs",
+});
+
+const VIEW_TEMPLATE = {
+ attributes: {
+ selectable: true,
+ },
+ children: [
+ {
+ name: "content",
+ tag: "span",
+ overflowable: true,
+ children: [
+ {
+ name: "icon",
+ tag: "img",
+ },
+ {
+ name: "header",
+ tag: "span",
+ children: [
+ {
+ name: "title",
+ tag: "span",
+ classList: ["urlbarView-title"],
+ },
+ {
+ name: "separator",
+ tag: "span",
+ classList: ["urlbarView-title-separator"],
+ },
+ {
+ name: "url",
+ tag: "span",
+ classList: ["urlbarView-url"],
+ },
+ ],
+ },
+ {
+ name: "description",
+ tag: "span",
+ },
+ {
+ name: "footer",
+ tag: "span",
+ children: [
+ {
+ name: "ratingContainer",
+ tag: "span",
+ children: [
+ {
+ classList: ["urlbarView-dynamic-addons-rating"],
+ name: "rating0",
+ tag: "span",
+ },
+ {
+ classList: ["urlbarView-dynamic-addons-rating"],
+ name: "rating1",
+ tag: "span",
+ },
+ {
+ classList: ["urlbarView-dynamic-addons-rating"],
+ name: "rating2",
+ tag: "span",
+ },
+ {
+ classList: ["urlbarView-dynamic-addons-rating"],
+ name: "rating3",
+ tag: "span",
+ },
+ {
+ classList: ["urlbarView-dynamic-addons-rating"],
+ name: "rating4",
+ tag: "span",
+ },
+ ],
+ },
+ {
+ name: "reviews",
+ tag: "span",
+ },
+ ],
+ },
+ ],
+ },
+ ],
+};
+
+const RESULT_MENU_COMMAND = {
+ HELP: "help",
+ NOT_INTERESTED: "not_interested",
+ NOT_RELEVANT: "not_relevant",
+ SHOW_LESS_FREQUENTLY: "show_less_frequently",
+};
+
+/**
+ * A feature that supports Addon suggestions.
+ */
+export class AddonSuggestions extends BaseFeature {
+ constructor() {
+ super();
+ lazy.UrlbarResult.addDynamicResultType("addons");
+ lazy.UrlbarView.addDynamicViewTemplate("addons", VIEW_TEMPLATE);
+ }
+
+ get shouldEnable() {
+ return (
+ lazy.UrlbarPrefs.get("addonsFeatureGate") &&
+ lazy.UrlbarPrefs.get("suggest.addons") &&
+ lazy.UrlbarPrefs.get("suggest.quicksuggest.nonsponsored")
+ );
+ }
+
+ get enablingPreferences() {
+ return ["suggest.addons", "suggest.quicksuggest.nonsponsored"];
+ }
+
+ enable(enabled) {
+ if (enabled) {
+ lazy.QuickSuggestRemoteSettings.register(this);
+ } else {
+ lazy.QuickSuggestRemoteSettings.unregister(this);
+ }
+ }
+
+ queryRemoteSettings(searchString) {
+ const suggestions = this.#suggestionsMap?.get(searchString);
+ if (!suggestions) {
+ return [];
+ }
+
+ return suggestions.map(suggestion => ({
+ icon: suggestion.icon,
+ url: suggestion.url,
+ title: suggestion.title,
+ description: suggestion.description,
+ rating: suggestion.rating,
+ number_of_ratings: suggestion.number_of_ratings,
+ guid: suggestion.guid,
+ score: suggestion.score,
+ is_top_pick: suggestion.is_top_pick,
+ }));
+ }
+
+ async onRemoteSettingsSync(rs) {
+ const records = await rs.get({ filters: { type: "amo-suggestions" } });
+ if (rs != lazy.QuickSuggestRemoteSettings.rs) {
+ return;
+ }
+
+ const suggestionsMap = new lazy.SuggestionsMap();
+
+ for (const record of records) {
+ const { buffer } = await rs.attachments.download(record);
+ if (rs != lazy.QuickSuggestRemoteSettings.rs) {
+ return;
+ }
+
+ const results = JSON.parse(new TextDecoder("utf-8").decode(buffer));
+
+ // The keywords in remote settings are full keywords. Map each one to an
+ // array containing the full keyword's first word plus every subsequent
+ // prefix of the full keyword.
+ await suggestionsMap.add(results, fullKeyword => {
+ let keywords = [fullKeyword];
+ let spaceIndex = fullKeyword.search(/\s/);
+ if (spaceIndex >= 0) {
+ for (let i = spaceIndex; i < fullKeyword.length; i++) {
+ keywords.push(fullKeyword.substring(0, i));
+ }
+ }
+ return keywords;
+ });
+ if (rs != lazy.QuickSuggestRemoteSettings.rs) {
+ return;
+ }
+ }
+
+ this.#suggestionsMap = suggestionsMap;
+ }
+
+ async makeResult(queryContext, suggestion, searchString) {
+ if (!this.isEnabled) {
+ // The feature is disabled on the client, but Merino may still return
+ // addon suggestions anyway, and we filter them out here.
+ return null;
+ }
+
+ // If the user hasn't clicked the "Show less frequently" command, the
+ // suggestion can be shown. Otherwise, the suggestion can be shown if the
+ // user typed more than one word with at least `showLessFrequentlyCount`
+ // characters after the first word, including spaces.
+ if (this.showLessFrequentlyCount) {
+ let spaceIndex = searchString.search(/\s/);
+ if (
+ spaceIndex < 0 ||
+ searchString.length - spaceIndex < this.showLessFrequentlyCount
+ ) {
+ return null;
+ }
+ }
+
+ // If is_top_pick is not specified, handle it as top pick suggestion.
+ suggestion.is_top_pick = suggestion.is_top_pick ?? true;
+
+ const { guid, rating, number_of_ratings } =
+ suggestion.source === "remote-settings"
+ ? suggestion
+ : suggestion.custom_details.amo;
+
+ const addon = await lazy.AddonManager.getAddonByID(guid);
+ if (addon) {
+ // Addon suggested is already installed.
+ return null;
+ }
+
+ const payload = {
+ source: suggestion.source,
+ icon: suggestion.icon,
+ url: suggestion.url,
+ title: suggestion.title,
+ description: suggestion.description,
+ rating: Number(rating),
+ reviews: Number(number_of_ratings),
+ helpUrl: lazy.QuickSuggest.HELP_URL,
+ shouldNavigate: true,
+ dynamicType: "addons",
+ telemetryType: "amo",
+ };
+
+ return Object.assign(
+ new lazy.UrlbarResult(
+ lazy.UrlbarUtils.RESULT_TYPE.DYNAMIC,
+ lazy.UrlbarUtils.RESULT_SOURCE.SEARCH,
+ ...lazy.UrlbarResult.payloadAndSimpleHighlights(
+ queryContext.tokens,
+ payload
+ )
+ ),
+ { showFeedbackMenu: true }
+ );
+ }
+
+ getViewUpdate(result) {
+ const treatment = lazy.UrlbarPrefs.get("addonsUITreatment");
+ const rating = result.payload.rating;
+
+ return {
+ content: {
+ attributes: { treatment },
+ },
+ icon: {
+ attributes: {
+ src: result.payload.icon,
+ },
+ },
+ url: {
+ textContent: result.payload.url,
+ },
+ title: {
+ textContent: result.payload.title,
+ },
+ description: {
+ textContent: result.payload.description,
+ },
+ rating0: {
+ attributes: {
+ fill: this.#getRatingStar(0, rating),
+ },
+ },
+ rating1: {
+ attributes: {
+ fill: this.#getRatingStar(1, rating),
+ },
+ },
+ rating2: {
+ attributes: {
+ fill: this.#getRatingStar(2, rating),
+ },
+ },
+ rating3: {
+ attributes: {
+ fill: this.#getRatingStar(3, rating),
+ },
+ },
+ rating4: {
+ attributes: {
+ fill: this.#getRatingStar(4, rating),
+ },
+ },
+ reviews: {
+ l10n:
+ treatment === "b"
+ ? { id: "firefox-suggest-addons-recommended" }
+ : {
+ id: "firefox-suggest-addons-reviews",
+ args: {
+ quantity: result.payload.reviews,
+ },
+ },
+ },
+ };
+ }
+
+ getResultCommands(result) {
+ const commands = [];
+
+ if (this.canShowLessFrequently) {
+ commands.push({
+ name: RESULT_MENU_COMMAND.SHOW_LESS_FREQUENTLY,
+ l10n: {
+ id: "firefox-suggest-command-show-less-frequently",
+ },
+ });
+ }
+
+ commands.push(
+ {
+ l10n: {
+ id: "firefox-suggest-command-dont-show-this",
+ },
+ children: [
+ {
+ name: RESULT_MENU_COMMAND.NOT_RELEVANT,
+ l10n: {
+ id: "firefox-suggest-command-not-relevant",
+ },
+ },
+ {
+ name: RESULT_MENU_COMMAND.NOT_INTERESTED,
+ l10n: {
+ id: "firefox-suggest-command-not-interested",
+ },
+ },
+ ],
+ },
+ { name: "separator" },
+ {
+ name: RESULT_MENU_COMMAND.HELP,
+ l10n: {
+ id: "urlbar-result-menu-learn-more-about-firefox-suggest",
+ },
+ }
+ );
+
+ return commands;
+ }
+
+ handlePossibleCommand(queryContext, result, selType) {
+ switch (selType) {
+ case RESULT_MENU_COMMAND.HELP:
+ // "help" is handled by UrlbarInput, no need to do anything here.
+ break;
+ // selType == "dismiss" when the user presses the dismiss key shortcut.
+ case "dismiss":
+ case RESULT_MENU_COMMAND.NOT_INTERESTED:
+ case RESULT_MENU_COMMAND.NOT_RELEVANT:
+ lazy.UrlbarPrefs.set("suggest.addons", false);
+ queryContext.view.acknowledgeDismissal(result);
+ break;
+ case RESULT_MENU_COMMAND.SHOW_LESS_FREQUENTLY:
+ queryContext.view.acknowledgeFeedback(result);
+ this.incrementShowLessFrequentlyCount();
+ break;
+ }
+ }
+
+ #getRatingStar(nth, rating) {
+ // 0 <= x < 0.25 = empty
+ // 0.25 <= x < 0.75 = half
+ // 0.75 <= x <= 1 = full
+ // ... et cetera, until x <= 5.
+ const distanceToFull = rating - nth;
+ if (distanceToFull < 0.25) {
+ return "empty";
+ }
+ if (distanceToFull < 0.75) {
+ return "half";
+ }
+ return "full";
+ }
+
+ incrementShowLessFrequentlyCount() {
+ if (this.canShowLessFrequently) {
+ lazy.UrlbarPrefs.set(
+ "addons.showLessFrequentlyCount",
+ this.showLessFrequentlyCount + 1
+ );
+ }
+ }
+
+ get showLessFrequentlyCount() {
+ const count = lazy.UrlbarPrefs.get("addons.showLessFrequentlyCount") || 0;
+ return Math.max(count, 0);
+ }
+
+ get canShowLessFrequently() {
+ const cap =
+ lazy.UrlbarPrefs.get("addonsShowLessFrequentlyCap") ||
+ lazy.QuickSuggestRemoteSettings.config.show_less_frequently_cap ||
+ 0;
+ return !cap || this.showLessFrequentlyCount < cap;
+ }
+
+ #suggestionsMap = null;
+}
diff --git a/browser/components/urlbar/private/AdmWikipedia.sys.mjs b/browser/components/urlbar/private/AdmWikipedia.sys.mjs
new file mode 100644
index 0000000000..4393ae317f
--- /dev/null
+++ b/browser/components/urlbar/private/AdmWikipedia.sys.mjs
@@ -0,0 +1,280 @@
+/* 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/. */
+
+import { BaseFeature } from "resource:///modules/urlbar/private/BaseFeature.sys.mjs";
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ QuickSuggest: "resource:///modules/QuickSuggest.sys.mjs",
+ QuickSuggestRemoteSettings:
+ "resource:///modules/urlbar/private/QuickSuggestRemoteSettings.sys.mjs",
+ SuggestionsMap:
+ "resource:///modules/urlbar/private/QuickSuggestRemoteSettings.sys.mjs",
+ UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
+ UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
+ UrlbarUtils: "resource:///modules/UrlbarUtils.sys.mjs",
+});
+
+const NONSPONSORED_IAB_CATEGORIES = new Set(["5 - Education"]);
+
+/**
+ * A feature that manages sponsored adM and non-sponsored Wikpedia (sometimes
+ * called "expanded Wikipedia") suggestions in remote settings.
+ */
+export class AdmWikipedia extends BaseFeature {
+ constructor() {
+ super();
+ this.#suggestionsMap = new lazy.SuggestionsMap();
+ }
+
+ get shouldEnable() {
+ return (
+ lazy.UrlbarPrefs.get("quickSuggestRemoteSettingsEnabled") &&
+ (lazy.UrlbarPrefs.get("suggest.quicksuggest.nonsponsored") ||
+ lazy.UrlbarPrefs.get("suggest.quicksuggest.sponsored"))
+ );
+ }
+
+ get enablingPreferences() {
+ return [
+ "suggest.quicksuggest.nonsponsored",
+ "suggest.quicksuggest.sponsored",
+ ];
+ }
+
+ enable(enabled) {
+ if (enabled) {
+ lazy.QuickSuggestRemoteSettings.register(this);
+ } else {
+ lazy.QuickSuggestRemoteSettings.unregister(this);
+ }
+ }
+
+ async queryRemoteSettings(searchString) {
+ let suggestions = this.#suggestionsMap.get(searchString);
+ if (!suggestions) {
+ return [];
+ }
+
+ // Start each icon fetch at the same time and wait for them all to finish.
+ let icons = await Promise.all(
+ suggestions.map(({ icon }) => this.#fetchIcon(icon))
+ );
+
+ return suggestions.map(suggestion => ({
+ full_keyword: this.#getFullKeyword(searchString, suggestion.keywords),
+ title: suggestion.title,
+ url: suggestion.url,
+ click_url: suggestion.click_url,
+ impression_url: suggestion.impression_url,
+ block_id: suggestion.id,
+ advertiser: suggestion.advertiser,
+ iab_category: suggestion.iab_category,
+ is_sponsored: !NONSPONSORED_IAB_CATEGORIES.has(suggestion.iab_category),
+ score: suggestion.score,
+ position: suggestion.position,
+ icon: icons.shift(),
+ }));
+ }
+
+ async onRemoteSettingsSync(rs) {
+ let dataType = lazy.UrlbarPrefs.get("quickSuggestRemoteSettingsDataType");
+ this.logger.debug("Loading remote settings with type: " + dataType);
+
+ let [data] = await Promise.all([
+ rs.get({ filters: { type: dataType } }),
+ rs
+ .get({ filters: { type: "icon" } })
+ .then(icons =>
+ Promise.all(icons.map(i => rs.attachments.downloadToDisk(i)))
+ ),
+ ]);
+ if (rs != lazy.QuickSuggestRemoteSettings.rs) {
+ return;
+ }
+
+ let suggestionsMap = new lazy.SuggestionsMap();
+
+ this.logger.debug(`Got data with ${data.length} records`);
+ for (let record of data) {
+ let { buffer } = await rs.attachments.download(record);
+ if (rs != lazy.QuickSuggestRemoteSettings.rs) {
+ return;
+ }
+
+ let results = JSON.parse(new TextDecoder("utf-8").decode(buffer));
+ this.logger.debug(`Adding ${results.length} results`);
+ await suggestionsMap.add(results);
+ if (rs != lazy.QuickSuggestRemoteSettings.rs) {
+ return;
+ }
+ }
+
+ this.#suggestionsMap = suggestionsMap;
+ }
+
+ makeResult(queryContext, suggestion, searchString) {
+ // Replace the suggestion's template substrings, but first save the original
+ // URL before its timestamp template is replaced.
+ let originalUrl = suggestion.url;
+ lazy.QuickSuggest.replaceSuggestionTemplates(suggestion);
+
+ let payload = {
+ originalUrl,
+ url: suggestion.url,
+ icon: suggestion.icon,
+ isSponsored: suggestion.is_sponsored,
+ source: suggestion.source,
+ telemetryType: suggestion.is_sponsored
+ ? "adm_sponsored"
+ : "adm_nonsponsored",
+ requestId: suggestion.request_id,
+ urlTimestampIndex: suggestion.urlTimestampIndex,
+ sponsoredImpressionUrl: suggestion.impression_url,
+ sponsoredClickUrl: suggestion.click_url,
+ sponsoredBlockId: suggestion.block_id,
+ sponsoredAdvertiser: suggestion.advertiser,
+ sponsoredIabCategory: suggestion.iab_category,
+ helpUrl: lazy.QuickSuggest.HELP_URL,
+ helpL10n: {
+ id: "urlbar-result-menu-learn-more-about-firefox-suggest",
+ },
+ blockL10n: {
+ id: "urlbar-result-menu-dismiss-firefox-suggest",
+ },
+ };
+
+ // Determine if the suggestion itself is a best match.
+ let isSuggestionBestMatch = false;
+ if (lazy.QuickSuggestRemoteSettings.config.best_match) {
+ let { best_match } = lazy.QuickSuggestRemoteSettings.config;
+ isSuggestionBestMatch =
+ best_match.min_search_string_length <= searchString.length &&
+ !best_match.blocked_suggestion_ids.includes(suggestion.block_id);
+ }
+
+ // Determine if the urlbar result should be a best match.
+ let isResultBestMatch =
+ isSuggestionBestMatch &&
+ lazy.UrlbarPrefs.get("bestMatchEnabled") &&
+ lazy.UrlbarPrefs.get("suggest.bestmatch");
+ if (isResultBestMatch) {
+ // Show the result as a best match. Best match titles don't include the
+ // `full_keyword`, and the user's search string is highlighted.
+ payload.title = [suggestion.title, lazy.UrlbarUtils.HIGHLIGHT.TYPED];
+ } else {
+ // Show the result as a usual quick suggest. Include the `full_keyword`
+ // and highlight the parts that aren't in the search string.
+ payload.title = suggestion.title;
+ payload.qsSuggestion = [
+ suggestion.full_keyword,
+ lazy.UrlbarUtils.HIGHLIGHT.SUGGESTED,
+ ];
+ }
+ payload.isBlockable = lazy.UrlbarPrefs.get(
+ isResultBestMatch
+ ? "bestMatchBlockingEnabled"
+ : "quickSuggestBlockingEnabled"
+ );
+
+ let result = new lazy.UrlbarResult(
+ lazy.UrlbarUtils.RESULT_TYPE.URL,
+ lazy.UrlbarUtils.RESULT_SOURCE.SEARCH,
+ ...lazy.UrlbarResult.payloadAndSimpleHighlights(
+ queryContext.tokens,
+ payload
+ )
+ );
+
+ if (isResultBestMatch) {
+ result.isBestMatch = true;
+ result.suggestedIndex = 1;
+ }
+
+ return result;
+ }
+
+ /**
+ * Gets the "full keyword" (i.e., suggestion) for a query from a list of
+ * keywords. The suggestions data doesn't include full keywords, so we make
+ * our own based on the result's keyword phrases and a particular query. We
+ * use two heuristics:
+ *
+ * (1) Find the first keyword phrase that has more words than the query. Use
+ * its first `queryWords.length` words as the full keyword. e.g., if the
+ * query is "moz" and `keywords` is ["moz", "mozi", "mozil", "mozill",
+ * "mozilla", "mozilla firefox"], pick "mozilla firefox", pop off the
+ * "firefox" and use "mozilla" as the full keyword.
+ * (2) If there isn't any keyword phrase with more words, then pick the
+ * longest phrase. e.g., pick "mozilla" in the previous example (assuming
+ * the "mozilla firefox" phrase isn't there). That might be the query
+ * itself.
+ *
+ * @param {string} query
+ * The query string.
+ * @param {Array} keywords
+ * An array of suggestion keywords.
+ * @returns {string}
+ * The full keyword.
+ */
+ #getFullKeyword(query, keywords) {
+ let longerPhrase;
+ let trimmedQuery = query.toLocaleLowerCase().trim();
+ let queryWords = trimmedQuery.split(" ");
+
+ for (let phrase of keywords) {
+ if (phrase.startsWith(query)) {
+ let trimmedPhrase = phrase.trim();
+ let phraseWords = trimmedPhrase.split(" ");
+ // As an exception to (1), if the query ends with a space, then look for
+ // phrases with one more word so that the suggestion includes a word
+ // following the space.
+ let extra = query.endsWith(" ") ? 1 : 0;
+ let len = queryWords.length + extra;
+ if (len < phraseWords.length) {
+ // We found a phrase with more words.
+ return phraseWords.slice(0, len).join(" ");
+ }
+ if (
+ query.length < phrase.length &&
+ (!longerPhrase || longerPhrase.length < trimmedPhrase.length)
+ ) {
+ // We found a longer phrase with the same number of words.
+ longerPhrase = trimmedPhrase;
+ }
+ }
+ }
+ return longerPhrase || trimmedQuery;
+ }
+
+ /**
+ * Fetch the icon from RemoteSettings attachments.
+ *
+ * @param {string} path
+ * The icon's remote settings path.
+ */
+ async #fetchIcon(path) {
+ if (!path) {
+ return null;
+ }
+
+ let { rs } = lazy.QuickSuggestRemoteSettings;
+ if (!rs) {
+ return null;
+ }
+
+ let record = (
+ await rs.get({
+ filters: { id: `icon-${path}` },
+ })
+ ).pop();
+ if (!record) {
+ return null;
+ }
+ return rs.attachments.downloadToDisk(record);
+ }
+
+ #suggestionsMap;
+}
diff --git a/browser/components/urlbar/private/BaseFeature.sys.mjs b/browser/components/urlbar/private/BaseFeature.sys.mjs
new file mode 100644
index 0000000000..4893ffdf1c
--- /dev/null
+++ b/browser/components/urlbar/private/BaseFeature.sys.mjs
@@ -0,0 +1,168 @@
+/* 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, {
+ UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
+ UrlbarUtils: "resource:///modules/UrlbarUtils.sys.mjs",
+});
+
+/**
+ * Base class for quick suggest features. It can be extended to implement a
+ * feature that is part of the larger quick suggest feature and that should be
+ * enabled only when quick suggest is enabled.
+ *
+ * You can extend this class as an alternative to implementing your feature
+ * directly in `QuickSuggest`. Doing so has the following advantages:
+ *
+ * - If your feature is gated on a Nimbus variable or preference, `QuickSuggest`
+ * will manage its lifetime automatically. This is really only useful if the
+ * feature has state that must be initialized when the feature is enabled and
+ * uninitialized when it's disabled.
+ *
+ * - Encapsulation. You can keep all the code related to your feature in one
+ * place, without mixing it with unrelated code and cluttering up
+ * `QuickSuggest`. You can also test it in isolation from `QuickSuggest`.
+ *
+ * - Remote settings management. You can register your feature with
+ * `QuickSuggestRemoteSettings` and it will be called at appropriate times to
+ * sync from remote settings.
+ *
+ * - If your feature also serves suggestions from remote settings, you can
+ * implement one method, `queryRemoteSettings()`, to hook into
+ * `UrlbarProviderQuickSuggest`.
+ *
+ * - Your feature will automatically get its own logger.
+ *
+ * To register your subclass with `QuickSuggest`, add it to the `FEATURES` const
+ * in QuickSuggest.sys.mjs.
+ */
+export class BaseFeature {
+ /**
+ * {boolean}
+ * Whether the feature should be enabled. Typically the subclass will check
+ * the values of one or more Nimbus variables or preferences. `QuickSuggest`
+ * will access this getter only when the quick suggest feature as a whole is
+ * enabled. Otherwise the subclass feature will be disabled automatically.
+ */
+ get shouldEnable() {
+ throw new Error("`shouldEnable` must be overridden");
+ }
+
+ /**
+ * @returns {Array}
+ * If the subclass's `shouldEnable` implementation depends on preferences
+ * instead of Nimbus variables, the subclass should override this getter and
+ * return their names in this array so that `enable()` can be called when
+ * they change. Names should be in the same format that `UrlbarPrefs.get()`
+ * expects.
+ */
+ get enablingPreferences() {
+ return null;
+ }
+
+ /**
+ * This method should initialize or uninitialize any state related to the
+ * feature.
+ *
+ * @param {boolean} enabled
+ * Whether the feature should be enabled or not.
+ */
+ enable(enabled) {}
+
+ /**
+ * If the feature manages suggestions from remote settings that should be
+ * returned by UrlbarProviderQuickSuggest, the subclass should override this
+ * method. It should return remote settings suggestions matching the given
+ * search string.
+ *
+ * @param {string} searchString
+ * The search string.
+ * @returns {Array}
+ * An array of matching suggestions, or null if not implemented.
+ */
+ async queryRemoteSettings(searchString) {
+ return null;
+ }
+
+ /**
+ * If the feature manages data in remote settings, the subclass should
+ * override this method. It should fetch the data and build whatever data
+ * structures are necessary to support the feature.
+ *
+ * @param {RemoteSettings} rs
+ * The `RemoteSettings` client object.
+ */
+ async onRemoteSettingsSync(rs) {}
+
+ /**
+ * If the feature corresponds to a type of suggestion, the subclass should
+ * override this method. It should return a new `UrlbarResult` for a given
+ * suggestion, which can come from either remote settings or Merino.
+ *
+ * @param {UrlbarQueryContext} queryContext
+ * The query context.
+ * @param {object} suggestion
+ * The suggestion from either remote settings or Merino.
+ * @param {string} searchString
+ * The search string that was used to fetch the suggestion. It may be
+ * different from `queryContext.searchString` due to trimming, lower-casing,
+ * etc. This is included as a param in case it's useful.
+ * @returns {UrlbarResult}
+ * A new result for the suggestion.
+ */
+ async makeResult(queryContext, suggestion, searchString) {
+ return null;
+ }
+
+ // Methods not designed for overriding below
+
+ /**
+ * @returns {Logger}
+ * The feature's logger.
+ */
+ get logger() {
+ if (!this._logger) {
+ this._logger = lazy.UrlbarUtils.getLogger({
+ prefix: `QuickSuggest.${this.name}`,
+ });
+ }
+ return this._logger;
+ }
+
+ /**
+ * @returns {boolean}
+ * Whether the feature is enabled. The enabled status is automatically
+ * managed by `QuickSuggest` and subclasses should not override this.
+ */
+ get isEnabled() {
+ return this.#isEnabled;
+ }
+
+ /**
+ * @returns {string}
+ * The feature's name.
+ */
+ get name() {
+ return this.constructor.name;
+ }
+
+ /**
+ * Enables or disables the feature according to `shouldEnable` and whether
+ * quick suggest is enabled. If the feature is already enabled appropriately,
+ * does nothing.
+ */
+ update() {
+ let enable =
+ lazy.UrlbarPrefs.get("quickSuggestEnabled") && this.shouldEnable;
+ if (enable != this.isEnabled) {
+ this.logger.info(`Setting enabled = ${enable}`);
+ this.enable(enable);
+ this.#isEnabled = enable;
+ }
+ }
+
+ #isEnabled = false;
+}
diff --git a/browser/components/urlbar/private/BlockedSuggestions.sys.mjs b/browser/components/urlbar/private/BlockedSuggestions.sys.mjs
new file mode 100644
index 0000000000..d74a0979d1
--- /dev/null
+++ b/browser/components/urlbar/private/BlockedSuggestions.sys.mjs
@@ -0,0 +1,187 @@
+/* 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/. */
+
+import { BaseFeature } from "resource:///modules/urlbar/private/BaseFeature.sys.mjs";
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ TaskQueue: "resource:///modules/UrlbarUtils.sys.mjs",
+ UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
+});
+
+/**
+ * A set of blocked suggestions for quick suggest.
+ */
+export class BlockedSuggestions extends BaseFeature {
+ constructor() {
+ super();
+ this.#taskQueue = new lazy.TaskQueue();
+ lazy.UrlbarPrefs.addObserver(this);
+ }
+
+ get shouldEnable() {
+ // Return true so that we'll always load blocked digests when quick suggest
+ // is enabled, even if blocking new suggestions is currently disabled.
+ // Blocking may have been enabled previously, and blocked suggestions should
+ // remain blocked as long as quick suggest as a whole remains enabled.
+ return true;
+ }
+
+ enable(enabled) {
+ if (enabled) {
+ this.#loadDigests();
+ }
+ }
+
+ /**
+ * Blocks a suggestion.
+ *
+ * @param {string} originalUrl
+ * The suggestion's original URL with its unreplaced timestamp template.
+ */
+ async add(originalUrl) {
+ this.logger.debug(`Queueing add: ${originalUrl}`);
+ await this.#taskQueue.queue(async () => {
+ this.logger.info(`Blocking suggestion: ${originalUrl}`);
+ let digest = await this.#getDigest(originalUrl);
+ this.logger.debug(`Got digest for '${originalUrl}': ${digest}`);
+ this.#digests.add(digest);
+ let json = JSON.stringify([...this.#digests]);
+ this.#updatingDigests = true;
+ try {
+ lazy.UrlbarPrefs.set("quicksuggest.blockedDigests", json);
+ } finally {
+ this.#updatingDigests = false;
+ }
+ this.logger.debug(`All blocked suggestions: ${json}`);
+ });
+ }
+
+ /**
+ * Gets whether a suggestion is blocked.
+ *
+ * @param {string} originalUrl
+ * The suggestion's original URL with its unreplaced timestamp template.
+ * @returns {boolean}
+ * Whether the suggestion is blocked.
+ */
+ async has(originalUrl) {
+ this.logger.debug(`Queueing has: ${originalUrl}`);
+ return this.#taskQueue.queue(async () => {
+ this.logger.info(`Getting blocked status: ${originalUrl}`);
+ let digest = await this.#getDigest(originalUrl);
+ this.logger.debug(`Got digest for '${originalUrl}': ${digest}`);
+ let isBlocked = this.#digests.has(digest);
+ this.logger.info(`Blocked status for '${originalUrl}': ${isBlocked}`);
+ return isBlocked;
+ });
+ }
+
+ /**
+ * Unblocks all suggestions.
+ */
+ async clear() {
+ this.logger.debug(`Queueing clearBlockedSuggestions`);
+ await this.#taskQueue.queue(() => {
+ this.logger.info(`Clearing all blocked suggestions`);
+ this.#digests.clear();
+ lazy.UrlbarPrefs.clear("quicksuggest.blockedDigests");
+ });
+ }
+
+ /**
+ * Called when a urlbar pref changes.
+ *
+ * @param {string} pref
+ * The name of the pref relative to `browser.urlbar`.
+ */
+ onPrefChanged(pref) {
+ switch (pref) {
+ case "quicksuggest.blockedDigests":
+ if (!this.#updatingDigests) {
+ this.logger.info(
+ "browser.urlbar.quicksuggest.blockedDigests changed"
+ );
+ this.#loadDigests();
+ }
+ break;
+ }
+ }
+
+ /**
+ * Loads blocked suggestion digests from the pref into `#digests`.
+ */
+ async #loadDigests() {
+ this.logger.debug(`Queueing #loadDigests`);
+ await this.#taskQueue.queue(() => {
+ this.logger.info(`Loading blocked suggestion digests`);
+ let json = lazy.UrlbarPrefs.get("quicksuggest.blockedDigests");
+ this.logger.debug(
+ `browser.urlbar.quicksuggest.blockedDigests value: ${json}`
+ );
+ if (!json) {
+ this.logger.info(`There are no blocked suggestion digests`);
+ this.#digests.clear();
+ } else {
+ try {
+ this.#digests = new Set(JSON.parse(json));
+ this.logger.info(`Successfully loaded blocked suggestion digests`);
+ } catch (error) {
+ this.logger.error(
+ `Error loading blocked suggestion digests: ${error}`
+ );
+ }
+ }
+ });
+ }
+
+ /**
+ * Returns the SHA-1 digest of a string as a 40-character hex-encoded string.
+ *
+ * @param {string} string
+ * The string to convert to SHA-1
+ * @returns {string}
+ * The hex-encoded digest of the given string.
+ */
+ async #getDigest(string) {
+ let stringArray = new TextEncoder().encode(string);
+ let hashBuffer = await crypto.subtle.digest("SHA-1", stringArray);
+ let hashArray = new Uint8Array(hashBuffer);
+ return Array.from(hashArray, b => b.toString(16).padStart(2, "0")).join("");
+ }
+
+ get _test_readyPromise() {
+ return this.#taskQueue.emptyPromise;
+ }
+
+ get _test_digests() {
+ return this.#digests;
+ }
+
+ _test_getDigest(string) {
+ return this.#getDigest(string);
+ }
+
+ // Set of digests of the original URLs of blocked suggestions. A suggestion's
+ // "original URL" is its URL straight from the source with an unreplaced
+ // timestamp template. For details on the digests, see `#getDigest()`.
+ //
+ // The only reason we use URL digests is that suggestions currently do not
+ // have persistent IDs. We could use the URLs themselves but SHA-1 digests are
+ // only 40 chars long, so they save a little space. This is also consistent
+ // with how blocked tiles on the newtab page are stored, but they use MD5. We
+ // do *not* store digests for any security or obfuscation reason.
+ //
+ // This value is serialized as a JSON'ed array to the
+ // `browser.urlbar.quicksuggest.blockedDigests` pref.
+ #digests = new Set();
+
+ // Used to serialize access to blocked suggestions. This is only necessary
+ // because getting a suggestion's URL digest is async.
+ #taskQueue = null;
+
+ // Whether blocked digests are currently being updated.
+ #updatingDigests = false;
+}
diff --git a/browser/components/urlbar/private/ImpressionCaps.sys.mjs b/browser/components/urlbar/private/ImpressionCaps.sys.mjs
new file mode 100644
index 0000000000..b50d87bf9e
--- /dev/null
+++ b/browser/components/urlbar/private/ImpressionCaps.sys.mjs
@@ -0,0 +1,566 @@
+/* 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/. */
+
+import { BaseFeature } from "resource:///modules/urlbar/private/BaseFeature.sys.mjs";
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ AsyncShutdown: "resource://gre/modules/AsyncShutdown.sys.mjs",
+ QuickSuggest: "resource:///modules/QuickSuggest.sys.mjs",
+ QuickSuggestRemoteSettings:
+ "resource:///modules/urlbar/private/QuickSuggestRemoteSettings.sys.mjs",
+ UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
+ clearInterval: "resource://gre/modules/Timer.sys.mjs",
+ setInterval: "resource://gre/modules/Timer.sys.mjs",
+});
+
+const IMPRESSION_COUNTERS_RESET_INTERVAL_MS = 60 * 60 * 1000; // 1 hour
+
+// This object maps impression stats object keys to their corresponding keys in
+// the `extra` object of impression cap telemetry events. The main reason this
+// is necessary is because the keys of the `extra` object are limited to 15
+// characters in length, which some stats object keys exceed. It also forces us
+// to be deliberate about keys we add to the `extra` object, since the `extra`
+// object is limited to 10 keys.
+const TELEMETRY_IMPRESSION_CAP_EXTRA_KEYS = {
+ // stats object key -> `extra` telemetry event object key
+ intervalSeconds: "intervalSeconds",
+ startDateMs: "startDate",
+ count: "count",
+ maxCount: "maxCount",
+ impressionDateMs: "impressionDate",
+};
+
+/**
+ * Impression caps and stats for quick suggest suggestions.
+ */
+export class ImpressionCaps extends BaseFeature {
+ constructor() {
+ super();
+ lazy.UrlbarPrefs.addObserver(this);
+ }
+
+ get shouldEnable() {
+ return (
+ lazy.UrlbarPrefs.get("quickSuggestImpressionCapsSponsoredEnabled") ||
+ lazy.UrlbarPrefs.get("quickSuggestImpressionCapsNonSponsoredEnabled")
+ );
+ }
+
+ enable(enabled) {
+ if (enabled) {
+ this.#init();
+ } else {
+ this.#uninit();
+ }
+ }
+
+ /**
+ * Increments the user's impression stats counters for the given type of
+ * suggestion. This should be called only when a suggestion impression is
+ * recorded.
+ *
+ * @param {string} type
+ * The suggestion type, one of: "sponsored", "nonsponsored"
+ */
+ updateStats(type) {
+ this.logger.info("Starting impression stats update");
+ this.logger.debug(
+ JSON.stringify({
+ type,
+ currentStats: this.#stats,
+ impression_caps: lazy.QuickSuggestRemoteSettings.config.impression_caps,
+ })
+ );
+
+ // Don't bother recording anything if caps are disabled.
+ let isSponsored = type == "sponsored";
+ if (
+ (isSponsored &&
+ !lazy.UrlbarPrefs.get("quickSuggestImpressionCapsSponsoredEnabled")) ||
+ (!isSponsored &&
+ !lazy.UrlbarPrefs.get("quickSuggestImpressionCapsNonSponsoredEnabled"))
+ ) {
+ this.logger.info("Impression caps disabled, skipping update");
+ return;
+ }
+
+ // Get the user's impression stats. Since stats are synced from caps, if the
+ // stats don't exist then the caps don't exist, and don't bother recording
+ // anything in that case.
+ let stats = this.#stats[type];
+ if (!stats) {
+ this.logger.info("Impression caps undefined, skipping update");
+ return;
+ }
+
+ // Increment counters.
+ for (let stat of stats) {
+ stat.count++;
+ stat.impressionDateMs = Date.now();
+
+ // Record a telemetry event for each newly hit cap.
+ if (stat.count == stat.maxCount) {
+ this.logger.info(`'${type}' impression cap hit`);
+ this.logger.debug(JSON.stringify({ type, hitStat: stat }));
+ this.#recordCapEvent({
+ stat,
+ eventType: "hit",
+ suggestionType: type,
+ });
+ }
+ }
+
+ // Save the stats.
+ this.#updatingStats = true;
+ try {
+ lazy.UrlbarPrefs.set(
+ "quicksuggest.impressionCaps.stats",
+ JSON.stringify(this.#stats)
+ );
+ } finally {
+ this.#updatingStats = false;
+ }
+
+ this.logger.info("Finished impression stats update");
+ this.logger.debug(JSON.stringify({ newStats: this.#stats }));
+ }
+
+ /**
+ * Returns a non-null value if an impression cap has been reached for the
+ * given suggestion type and null otherwise. This method can therefore be used
+ * to tell whether a cap has been reached for a given type. The actual return
+ * value an object describing the impression stats that caused the cap to be
+ * reached.
+ *
+ * @param {string} type
+ * The suggestion type, one of: "sponsored", "nonsponsored"
+ * @returns {object}
+ * An impression stats object or null.
+ */
+ getHitStats(type) {
+ this.#resetElapsedCounters();
+ let stats = this.#stats[type];
+ if (stats) {
+ let hitStats = stats.filter(s => s.maxCount <= s.count);
+ if (hitStats.length) {
+ return hitStats;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Called when a urlbar pref changes.
+ *
+ * @param {string} pref
+ * The name of the pref relative to `browser.urlbar`.
+ */
+ onPrefChanged(pref) {
+ switch (pref) {
+ case "quicksuggest.impressionCaps.stats":
+ if (!this.#updatingStats) {
+ this.logger.info(
+ "browser.urlbar.quicksuggest.impressionCaps.stats changed"
+ );
+ this.#loadStats();
+ }
+ break;
+ }
+ }
+
+ #init() {
+ this.#loadStats();
+
+ // Validate stats against any changes to the impression caps in the config.
+ this._onConfigSet = () => this.#validateStats();
+ lazy.QuickSuggestRemoteSettings.emitter.on("config-set", this._onConfigSet);
+
+ // Periodically record impression counters reset telemetry.
+ this.#setCountersResetInterval();
+
+ // On shutdown, record any final impression counters reset telemetry.
+ this._shutdownBlocker = () => this.#resetElapsedCounters();
+ lazy.AsyncShutdown.profileChangeTeardown.addBlocker(
+ "QuickSuggest: Record impression counters reset telemetry",
+ this._shutdownBlocker
+ );
+ }
+
+ #uninit() {
+ lazy.QuickSuggestRemoteSettings.emitter.off(
+ "config-set",
+ this._onConfigSet
+ );
+ this._onConfigSet = null;
+
+ lazy.clearInterval(this._impressionCountersResetInterval);
+ this._impressionCountersResetInterval = 0;
+
+ lazy.AsyncShutdown.profileChangeTeardown.removeBlocker(
+ this._shutdownBlocker
+ );
+ this._shutdownBlocker = null;
+ }
+
+ /**
+ * Loads and validates impression stats.
+ */
+ #loadStats() {
+ let json = lazy.UrlbarPrefs.get("quicksuggest.impressionCaps.stats");
+ if (!json) {
+ this.#stats = {};
+ } else {
+ try {
+ this.#stats = JSON.parse(
+ json,
+ // Infinity, which is the `intervalSeconds` for the lifetime cap, is
+ // stringified as `null` in the JSON, so convert it back to Infinity.
+ (key, value) =>
+ key == "intervalSeconds" && value === null ? Infinity : value
+ );
+ } catch (error) {}
+ }
+ this.#validateStats();
+ }
+
+ /**
+ * Validates impression stats, which includes two things:
+ *
+ * - Type checks stats and discards any that are invalid. We do this because
+ * stats are stored in prefs where anyone can modify them.
+ * - Syncs stats with impression caps so that there is one stats object
+ * corresponding to each impression cap. See the `#stats` comment for info.
+ */
+ #validateStats() {
+ let { impression_caps } = lazy.QuickSuggestRemoteSettings.config;
+
+ this.logger.info("Validating impression stats");
+ this.logger.debug(
+ JSON.stringify({
+ impression_caps,
+ currentStats: this.#stats,
+ })
+ );
+
+ if (!this.#stats || typeof this.#stats != "object") {
+ this.#stats = {};
+ }
+
+ for (let [type, cap] of Object.entries(impression_caps || {})) {
+ // Build a map from interval seconds to max counts in the caps.
+ let maxCapCounts = (cap.custom || []).reduce(
+ (map, { interval_s, max_count }) => {
+ map.set(interval_s, max_count);
+ return map;
+ },
+ new Map()
+ );
+ if (typeof cap.lifetime == "number") {
+ maxCapCounts.set(Infinity, cap.lifetime);
+ }
+
+ let stats = this.#stats[type];
+ if (!Array.isArray(stats)) {
+ stats = [];
+ this.#stats[type] = stats;
+ }
+
+ // Validate existing stats:
+ //
+ // * Discard stats with invalid properties.
+ // * Collect and remove stats with intervals that aren't in the caps. This
+ // should only happen when caps are changed or removed.
+ // * For stats with intervals that are in the caps:
+ // * Keep track of the max `stat.count` across all stats so we can
+ // update the lifetime stat below.
+ // * Set `stat.maxCount` to the max count in the corresponding cap.
+ let orphanStats = [];
+ let maxCountInStats = 0;
+ for (let i = 0; i < stats.length; ) {
+ let stat = stats[i];
+ if (
+ typeof stat.intervalSeconds != "number" ||
+ typeof stat.startDateMs != "number" ||
+ typeof stat.count != "number" ||
+ typeof stat.maxCount != "number" ||
+ typeof stat.impressionDateMs != "number"
+ ) {
+ stats.splice(i, 1);
+ } else {
+ maxCountInStats = Math.max(maxCountInStats, stat.count);
+ let maxCount = maxCapCounts.get(stat.intervalSeconds);
+ if (maxCount === undefined) {
+ stats.splice(i, 1);
+ orphanStats.push(stat);
+ } else {
+ stat.maxCount = maxCount;
+ i++;
+ }
+ }
+ }
+
+ // Create stats for caps that don't already have corresponding stats.
+ for (let [intervalSeconds, maxCount] of maxCapCounts.entries()) {
+ if (!stats.some(s => s.intervalSeconds == intervalSeconds)) {
+ stats.push({
+ maxCount,
+ intervalSeconds,
+ startDateMs: Date.now(),
+ count: 0,
+ impressionDateMs: 0,
+ });
+ }
+ }
+
+ // Merge orphaned stats into other ones if possible. For each orphan, if
+ // its interval is no bigger than an existing stat's interval, then the
+ // orphan's count can contribute to the existing stat's count, so merge
+ // the two.
+ for (let orphan of orphanStats) {
+ for (let stat of stats) {
+ if (orphan.intervalSeconds <= stat.intervalSeconds) {
+ stat.count = Math.max(stat.count, orphan.count);
+ stat.startDateMs = Math.min(stat.startDateMs, orphan.startDateMs);
+ stat.impressionDateMs = Math.max(
+ stat.impressionDateMs,
+ orphan.impressionDateMs
+ );
+ }
+ }
+ }
+
+ // If the lifetime stat exists, make its count the max count found above.
+ // This is only necessary when the lifetime cap wasn't present before, but
+ // it doesn't hurt to always do it.
+ let lifetimeStat = stats.find(s => s.intervalSeconds == Infinity);
+ if (lifetimeStat) {
+ lifetimeStat.count = maxCountInStats;
+ }
+
+ // Sort the stats by interval ascending. This isn't necessary except that
+ // it guarantees an ordering for tests.
+ stats.sort((a, b) => a.intervalSeconds - b.intervalSeconds);
+ }
+
+ this.logger.debug(JSON.stringify({ newStats: this.#stats }));
+ }
+
+ /**
+ * Resets the counters of impression stats whose intervals have elapased.
+ */
+ #resetElapsedCounters() {
+ this.logger.info("Checking for elapsed impression cap intervals");
+ this.logger.debug(
+ JSON.stringify({
+ currentStats: this.#stats,
+ impression_caps: lazy.QuickSuggestRemoteSettings.config.impression_caps,
+ })
+ );
+
+ let now = Date.now();
+ for (let [type, stats] of Object.entries(this.#stats)) {
+ for (let stat of stats) {
+ let elapsedMs = now - stat.startDateMs;
+ let intervalMs = 1000 * stat.intervalSeconds;
+ let elapsedIntervalCount = Math.floor(elapsedMs / intervalMs);
+ if (elapsedIntervalCount) {
+ // At least one interval period elapsed for the stat, so reset it. We
+ // may also need to record a telemetry event for the reset.
+ this.logger.info(
+ `Resetting impression counter for interval ${stat.intervalSeconds}s`
+ );
+ this.logger.debug(
+ JSON.stringify({ type, stat, elapsedMs, elapsedIntervalCount })
+ );
+
+ let newStartDateMs =
+ stat.startDateMs + elapsedIntervalCount * intervalMs;
+
+ // Compute the portion of `elapsedIntervalCount` that happened after
+ // startup. This will be the interval count we report in the telemetry
+ // event. By design we don't report intervals that elapsed while the
+ // app wasn't running. For example, if the user stopped using Firefox
+ // for a year, we don't want to report a year's worth of intervals.
+ //
+ // First, compute the count of intervals that elapsed before startup.
+ // This is the same arithmetic used above except here it's based on
+ // the startup date instead of `now`. Keep in mind that startup may be
+ // before the stat's start date. Then subtract that count from
+ // `elapsedIntervalCount` to get the portion after startup.
+ let startupDateMs = this._getStartupDateMs();
+ let elapsedIntervalCountBeforeStartup = Math.floor(
+ Math.max(0, startupDateMs - stat.startDateMs) / intervalMs
+ );
+ let elapsedIntervalCountAfterStartup =
+ elapsedIntervalCount - elapsedIntervalCountBeforeStartup;
+
+ if (elapsedIntervalCountAfterStartup) {
+ this.#recordCapEvent({
+ eventType: "reset",
+ suggestionType: type,
+ eventDateMs: newStartDateMs,
+ eventCount: elapsedIntervalCountAfterStartup,
+ stat: {
+ ...stat,
+ startDateMs:
+ stat.startDateMs +
+ elapsedIntervalCountBeforeStartup * intervalMs,
+ },
+ });
+ }
+
+ // Reset the stat.
+ stat.startDateMs = newStartDateMs;
+ stat.count = 0;
+ }
+ }
+ }
+
+ this.logger.debug(JSON.stringify({ newStats: this.#stats }));
+ }
+
+ /**
+ * Records an impression cap telemetry event.
+ *
+ * @param {object} options
+ * Options object
+ * @param {"hit" | "reset"} options.eventType
+ * One of: "hit", "reset"
+ * @param {string} options.suggestionType
+ * One of: "sponsored", "nonsponsored"
+ * @param {object} options.stat
+ * The stats object whose max count was hit or whose counter was reset.
+ * @param {number} options.eventCount
+ * The number of intervals that elapsed since the last event.
+ * @param {number} options.eventDateMs
+ * The `eventDate` that should be recorded in the event's `extra` object.
+ * We include this in `extra` even though events are timestamped because
+ * "reset" events are batched during periods where the user doesn't perform
+ * any searches and therefore impression counters are not reset.
+ */
+ #recordCapEvent({
+ eventType,
+ suggestionType,
+ stat,
+ eventCount = 1,
+ eventDateMs = Date.now(),
+ }) {
+ // All `extra` object values must be strings.
+ let extra = {
+ type: suggestionType,
+ eventDate: String(eventDateMs),
+ eventCount: String(eventCount),
+ };
+ for (let [statKey, value] of Object.entries(stat)) {
+ let extraKey = TELEMETRY_IMPRESSION_CAP_EXTRA_KEYS[statKey];
+ if (!extraKey) {
+ throw new Error("Unrecognized stats object key: " + statKey);
+ }
+ extra[extraKey] = String(value);
+ }
+ Services.telemetry.recordEvent(
+ lazy.QuickSuggest.TELEMETRY_EVENT_CATEGORY,
+ "impression_cap",
+ eventType,
+ "",
+ extra
+ );
+ }
+
+ /**
+ * Creates a repeating timer that resets impression counters and records
+ * related telemetry. Since counters are also reset when suggestions are
+ * triggered, the only point of this is to make sure we record reset telemetry
+ * events in a timely manner during periods when suggestions aren't triggered.
+ *
+ * @param {number} ms
+ * The number of milliseconds in the interval.
+ */
+ #setCountersResetInterval(ms = IMPRESSION_COUNTERS_RESET_INTERVAL_MS) {
+ if (this._impressionCountersResetInterval) {
+ lazy.clearInterval(this._impressionCountersResetInterval);
+ }
+ this._impressionCountersResetInterval = lazy.setInterval(
+ () => this.#resetElapsedCounters(),
+ ms
+ );
+ }
+
+ /**
+ * Gets the timestamp of app startup in ms since Unix epoch. This is only
+ * defined as its own method so tests can override it to simulate arbitrary
+ * startups.
+ *
+ * @returns {number}
+ * Startup timestamp in ms since Unix epoch.
+ */
+ _getStartupDateMs() {
+ return Services.startup.getStartupInfo().process.getTime();
+ }
+
+ get _test_stats() {
+ return this.#stats;
+ }
+
+ _test_reloadStats() {
+ this.#stats = null;
+ this.#loadStats();
+ }
+
+ _test_resetElapsedCounters() {
+ this.#resetElapsedCounters();
+ }
+
+ _test_setCountersResetInterval(ms) {
+ this.#setCountersResetInterval(ms);
+ }
+
+ // An object that keeps track of impression stats per sponsored and
+ // non-sponsored suggestion types. It looks like this:
+ //
+ // { sponsored: statsArray, nonsponsored: statsArray }
+ //
+ // The `statsArray` values are arrays of stats objects, one per impression
+ // cap, which look like this:
+ //
+ // { intervalSeconds, startDateMs, count, maxCount, impressionDateMs }
+ //
+ // {number} intervalSeconds
+ // The number of seconds in the corresponding cap's time interval.
+ // {number} startDateMs
+ // The timestamp at which the current interval period started and the
+ // object's `count` was reset to zero. This is a value returned from
+ // `Date.now()`. When the current date/time advances past `startDateMs +
+ // 1000 * intervalSeconds`, a new interval period will start and `count`
+ // will be reset to zero.
+ // {number} count
+ // The number of impressions during the current interval period.
+ // {number} maxCount
+ // The maximum number of impressions allowed during an interval period.
+ // This value is the same as the `max_count` value in the corresponding
+ // cap. It's stored in the stats object for convenience.
+ // {number} impressionDateMs
+ // The timestamp of the most recent impression, i.e., when `count` was
+ // last incremented.
+ //
+ // There are two types of impression caps: interval and lifetime. Interval
+ // caps are periodically reset, and lifetime caps are never reset. For stats
+ // objects corresponding to interval caps, `intervalSeconds` will be the
+ // `interval_s` value of the cap. For stats objects corresponding to lifetime
+ // caps, `intervalSeconds` will be `Infinity`.
+ //
+ // `#stats` is kept in sync with impression caps, and there is a one-to-one
+ // relationship between stats objects and caps. A stats object's corresponding
+ // cap is the one with the same suggestion type (sponsored or non-sponsored)
+ // and interval. See `#validateStats()` for more.
+ //
+ // Impression caps are stored in the remote settings config. See
+ // `QuickSuggestRemoteSettingsClient.confg.impression_caps`.
+ #stats = {};
+
+ // Whether impression stats are currently being updated.
+ #updatingStats = false;
+}
diff --git a/browser/components/urlbar/private/QuickSuggestRemoteSettings.sys.mjs b/browser/components/urlbar/private/QuickSuggestRemoteSettings.sys.mjs
new file mode 100644
index 0000000000..326b649780
--- /dev/null
+++ b/browser/components/urlbar/private/QuickSuggestRemoteSettings.sys.mjs
@@ -0,0 +1,395 @@
+/* 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, {
+ EventEmitter: "resource://gre/modules/EventEmitter.sys.mjs",
+ RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
+ UrlbarUtils: "resource:///modules/UrlbarUtils.sys.mjs",
+});
+
+const RS_COLLECTION = "quicksuggest";
+
+// Default score for remote settings suggestions.
+const DEFAULT_SUGGESTION_SCORE = 0.2;
+
+// Entries are added to `SuggestionsMap` map in chunks, and each chunk will add
+// at most this many entries.
+const SUGGESTIONS_MAP_CHUNK_SIZE = 1000;
+
+const TELEMETRY_LATENCY = "FX_URLBAR_QUICK_SUGGEST_REMOTE_SETTINGS_LATENCY_MS";
+
+/**
+ * Manages quick suggest remote settings data.
+ */
+class _QuickSuggestRemoteSettings {
+ /**
+ * @returns {number}
+ * The default score for remote settings suggestions, a value in the range
+ * [0, 1]. All suggestions require a score that can be used for comparison,
+ * so if a remote settings suggestion does not have one, it's assigned this
+ * value.
+ */
+ get DEFAULT_SUGGESTION_SCORE() {
+ return DEFAULT_SUGGESTION_SCORE;
+ }
+
+ constructor() {
+ this.#emitter = new lazy.EventEmitter();
+ }
+
+ /**
+ * @returns {RemoteSettings}
+ * The underlying `RemoteSettings` client object.
+ */
+ get rs() {
+ return this.#rs;
+ }
+
+ /**
+ * @returns {EventEmitter}
+ * The client will emit events on this object.
+ */
+ get emitter() {
+ return this.#emitter;
+ }
+
+ /**
+ * @returns {object}
+ * Global quick suggest configuration stored in remote settings. When the
+ * config changes the `emitter` property will emit a "config-set" event. The
+ * config is an object that looks like this:
+ *
+ * {
+ * best_match: {
+ * min_search_string_length,
+ * blocked_suggestion_ids,
+ * },
+ * impression_caps: {
+ * nonsponsored: {
+ * lifetime,
+ * custom: [
+ * { interval_s, max_count },
+ * ],
+ * },
+ * sponsored: {
+ * lifetime,
+ * custom: [
+ * { interval_s, max_count },
+ * ],
+ * },
+ * },
+ * show_less_frequently_cap,
+ * }
+ */
+ get config() {
+ return this.#config;
+ }
+
+ /**
+ * @returns {Array}
+ * Array of `BasicFeature` instances.
+ */
+ get features() {
+ return [...this.#features];
+ }
+
+ get logger() {
+ if (!this.#logger) {
+ this.#logger = lazy.UrlbarUtils.getLogger({
+ prefix: "QuickSuggestRemoteSettings",
+ });
+ }
+ return this.#logger;
+ }
+
+ /**
+ * Registers a quick suggest feature that uses remote settings.
+ *
+ * @param {BaseFeature} feature
+ * An instance of a `BaseFeature` subclass. See `BaseFeature` for methods
+ * that the subclass must implement.
+ */
+ register(feature) {
+ this.logger.debug("Registering feature: " + feature.name);
+ this.#features.add(feature);
+ if (this.#features.size == 1) {
+ this.#enableSettings(true);
+ }
+ this.#syncFeature(feature);
+ }
+
+ /**
+ * Unregisters a quick suggest feature that uses remote settings.
+ *
+ * @param {BaseFeature} feature
+ * An instance of a `BaseFeature` subclass.
+ */
+ unregister(feature) {
+ this.logger.debug("Unregistering feature: " + feature.name);
+ this.#features.delete(feature);
+ if (!this.#features.size) {
+ this.#enableSettings(false);
+ }
+ }
+
+ /**
+ * Queries remote settings suggestions from all registered features.
+ *
+ * @param {string} searchString
+ * The search string.
+ * @returns {Array}
+ * The remote settings suggestions. If there are no matches, an empty array
+ * is returned.
+ */
+ async query(searchString) {
+ let suggestions;
+ let stopwatchInstance = {};
+ TelemetryStopwatch.start(TELEMETRY_LATENCY, stopwatchInstance);
+ try {
+ suggestions = await this.#queryHelper(searchString);
+ TelemetryStopwatch.finish(TELEMETRY_LATENCY, stopwatchInstance);
+ } catch (error) {
+ TelemetryStopwatch.cancel(TELEMETRY_LATENCY, stopwatchInstance);
+ this.logger.error("Query error: " + error);
+ }
+
+ return suggestions || [];
+ }
+
+ async #queryHelper(searchString) {
+ this.logger.info("Handling query: " + JSON.stringify(searchString));
+
+ let results = await Promise.all(
+ [...this.#features].map(async feature => {
+ let suggestions = await feature.queryRemoteSettings(searchString);
+ return [feature, suggestions ?? []];
+ })
+ );
+
+ let allSuggestions = [];
+ for (let [feature, suggestions] of results) {
+ for (let suggestion of suggestions) {
+ suggestion.source = "remote-settings";
+ suggestion.provider = feature.name;
+ if (typeof suggestion.score != "number") {
+ suggestion.score = DEFAULT_SUGGESTION_SCORE;
+ }
+ allSuggestions.push(suggestion);
+ }
+ }
+
+ return allSuggestions;
+ }
+
+ async #enableSettings(enabled) {
+ if (enabled && !this.#rs) {
+ this.logger.debug("Creating RemoteSettings client");
+ this.#onSettingsSync = event => this.#syncAll({ event });
+ this.#rs = lazy.RemoteSettings(RS_COLLECTION);
+ this.#rs.on("sync", this.#onSettingsSync);
+ await this.#syncConfig();
+ } else if (!enabled && this.#rs) {
+ this.logger.debug("Destroying RemoteSettings client");
+ this.#rs.off("sync", this.#onSettingsSync);
+ this.#rs = null;
+ this.#onSettingsSync = null;
+ }
+ }
+
+ async #syncConfig() {
+ this.logger.debug("Syncing config");
+ let rs = this.#rs;
+
+ let configArray = await rs.get({ filters: { type: "configuration" } });
+ if (rs != this.#rs) {
+ return;
+ }
+
+ this.logger.debug("Got config array: " + JSON.stringify(configArray));
+ this.#setConfig(configArray?.[0]?.configuration || {});
+ }
+
+ async #syncFeature(feature) {
+ this.logger.debug("Syncing feature: " + feature.name);
+ await feature.onRemoteSettingsSync(this.#rs);
+ }
+
+ async #syncAll({ event = null } = {}) {
+ this.logger.debug("Syncing all");
+ let rs = this.#rs;
+
+ // Remove local files of deleted records
+ if (event?.data?.deleted) {
+ await Promise.all(
+ event.data.deleted
+ .filter(d => d.attachment)
+ .map(entry =>
+ Promise.all([
+ this.#rs.attachments.deleteDownloaded(entry), // type: data
+ this.#rs.attachments.deleteFromDisk(entry), // type: icon
+ ])
+ )
+ );
+ if (rs != this.#rs) {
+ return;
+ }
+ }
+
+ let promises = [this.#syncConfig()];
+ for (let feature of this.#features) {
+ promises.push(this.#syncFeature(feature));
+ }
+ await Promise.all(promises);
+ }
+
+ /**
+ * Sets the quick suggest config and emits a "config-set" event.
+ *
+ * @param {object} config
+ * The config object.
+ */
+ #setConfig(config) {
+ config ??= {};
+ this.logger.debug("Setting config: " + JSON.stringify(config));
+ this.#config = config;
+ this.#emitter.emit("config-set");
+ }
+
+ // The `RemoteSettings` client.
+ #rs = null;
+
+ // Registered `BaseFeature` instances.
+ #features = new Set();
+
+ // Configuration data synced from remote settings. See the `config` getter.
+ #config = {};
+
+ #emitter = null;
+ #logger = null;
+ #onSettingsSync = null;
+}
+
+export var QuickSuggestRemoteSettings = new _QuickSuggestRemoteSettings();
+
+/**
+ * A wrapper around `Map` that handles quick suggest suggestions from remote
+ * settings. It maps keywords to suggestions. It has two benefits over `Map`:
+ *
+ * - The main benefit is that map entries are added in batches on idle to avoid
+ * blocking the main thread for too long, since there can be many suggestions
+ * and keywords.
+ * - A secondary benefit is that the interface is tailored to quick suggest
+ * suggestions, which have a `keywords` property.
+ */
+export class SuggestionsMap {
+ /**
+ * Returns the list of suggestions for a keyword.
+ *
+ * @param {string} keyword
+ * The keyword.
+ * @returns {Array}
+ * The array of suggestions for the keyword. If the keyword isn't in the
+ * map, the array will be empty.
+ */
+ get(keyword) {
+ let object = this.#suggestionsByKeyword.get(keyword.toLocaleLowerCase());
+ if (!object) {
+ return [];
+ }
+ return Array.isArray(object) ? object : [object];
+ }
+
+ /**
+ * Adds a list of suggestion objects to the results map. Each suggestion must
+ * have a `keywords` property whose value is an array of keyword strings. The
+ * suggestion's keywords will be taken from this array either exactly as they
+ * are specified or by generating new keywords from them; see `mapKeyword`.
+ *
+ * @param {Array} suggestions
+ * Array of suggestion objects.
+ * @param {Function} mapKeyword
+ * If null, each suggestion's keywords will be taken from its `keywords`
+ * array exactly as they are specified. Otherwise, as each suggestion is
+ * processed, this function will be called for each string in its `keywords`
+ * array. The function should return an array of strings. The suggestion's
+ * final list of keywords will be all the keywords returned by this function
+ * as it is called for each string in `keywords`.
+ */
+ async add(suggestions, mapKeyword = null) {
+ // There can be many suggestions, and each suggestion can have many
+ // keywords. To avoid blocking the main thread for too long, update the map
+ // in chunks, and to avoid blocking the UI and other higher priority work,
+ // do each chunk only when the main thread is idle. During each chunk, we'll
+ // add at most `chunkSize` entries to the map.
+ let suggestionIndex = 0;
+ let keywordIndex = 0;
+
+ // Keep adding chunks until all suggestions have been fully added.
+ while (suggestionIndex < suggestions.length) {
+ await new Promise(resolve => {
+ Services.tm.idleDispatchToMainThread(() => {
+ // Keep updating the map until the current chunk is done.
+ let indexInChunk = 0;
+ while (
+ indexInChunk < SuggestionsMap.chunkSize &&
+ suggestionIndex < suggestions.length
+ ) {
+ let suggestion = suggestions[suggestionIndex];
+ if (keywordIndex == suggestion.keywords.length) {
+ // We've added entries for all keywords of the current suggestion.
+ // Move on to the next suggestion.
+ suggestionIndex++;
+ keywordIndex = 0;
+ continue;
+ }
+
+ let originalKeyword = suggestion.keywords[keywordIndex];
+ let keywords = mapKeyword?.(originalKeyword) ?? [originalKeyword];
+ for (let keyword of keywords) {
+ // If the keyword's only suggestion is `suggestion`, store it
+ // directly as the value. Otherwise store an array of unique
+ // suggestions. See the `#suggestionsByKeyword` comment.
+ let object = this.#suggestionsByKeyword.get(keyword);
+ if (!object) {
+ this.#suggestionsByKeyword.set(keyword, suggestion);
+ } else {
+ let isArray = Array.isArray(object);
+ if (!isArray && object != suggestion) {
+ this.#suggestionsByKeyword.set(keyword, [object, suggestion]);
+ } else if (isArray && !object.includes(suggestion)) {
+ object.push(suggestion);
+ }
+ }
+ }
+
+ keywordIndex++;
+ indexInChunk++;
+ }
+
+ // The current chunk is done.
+ resolve();
+ });
+ });
+ }
+ }
+
+ clear() {
+ this.#suggestionsByKeyword.clear();
+ }
+
+ // Maps each keyword in the dataset to one or more suggestions for the
+ // keyword. If only one suggestion uses a keyword, the keyword's value in the
+ // map will be the suggestion object. If more than one suggestion uses the
+ // keyword, the value will be an array of the suggestions. The reason for not
+ // always using an array is that we expect the vast majority of keywords to be
+ // used by only one suggestion, and since there are potentially very many
+ // keywords and suggestions and we keep them in memory all the time, we want
+ // to save as much memory as possible.
+ #suggestionsByKeyword = new Map();
+
+ // This is only defined as a property so that tests can override it.
+ static chunkSize = SUGGESTIONS_MAP_CHUNK_SIZE;
+}
diff --git a/browser/components/urlbar/private/Weather.sys.mjs b/browser/components/urlbar/private/Weather.sys.mjs
new file mode 100644
index 0000000000..ea9eddfa2c
--- /dev/null
+++ b/browser/components/urlbar/private/Weather.sys.mjs
@@ -0,0 +1,499 @@
+/* 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/. */
+
+import { BaseFeature } from "resource:///modules/urlbar/private/BaseFeature.sys.mjs";
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ clearTimeout: "resource://gre/modules/Timer.sys.mjs",
+ MerinoClient: "resource:///modules/MerinoClient.sys.mjs",
+ PromiseUtils: "resource://gre/modules/PromiseUtils.sys.mjs",
+ QuickSuggestRemoteSettings:
+ "resource:///modules/urlbar/private/QuickSuggestRemoteSettings.sys.mjs",
+ setTimeout: "resource://gre/modules/Timer.sys.mjs",
+ UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
+});
+
+const FETCH_DELAY_AFTER_COMING_ONLINE_MS = 3000; // 3s
+const FETCH_INTERVAL_MS = 30 * 60 * 1000; // 30 minutes
+const MERINO_PROVIDER = "accuweather";
+const MERINO_TIMEOUT_MS = 5000; // 5s
+
+const HISTOGRAM_LATENCY = "FX_URLBAR_MERINO_LATENCY_WEATHER_MS";
+const HISTOGRAM_RESPONSE = "FX_URLBAR_MERINO_RESPONSE_WEATHER";
+
+const NOTIFICATIONS = {
+ CAPTIVE_PORTAL_LOGIN: "captive-portal-login-success",
+ LINK_STATUS_CHANGED: "network:link-status-changed",
+ OFFLINE_STATUS_CHANGED: "network:offline-status-changed",
+ WAKE: "wake_notification",
+};
+
+/**
+ * A feature that periodically fetches weather suggestions from Merino.
+ */
+export class Weather extends BaseFeature {
+ get shouldEnable() {
+ // The feature itself is enabled by setting these prefs regardless of
+ // whether any config is defined. This is necessary to allow the feature to
+ // sync the config from remote settings and Nimbus. Suggestion fetches will
+ // not start until the config has been either synced from remote settings or
+ // set by Nimbus.
+ return (
+ lazy.UrlbarPrefs.get("weatherFeatureGate") &&
+ lazy.UrlbarPrefs.get("suggest.weather") &&
+ lazy.UrlbarPrefs.get("merinoEnabled")
+ );
+ }
+
+ get enablingPreferences() {
+ return ["suggest.weather"];
+ }
+
+ /**
+ * @returns {object}
+ * The last weather suggestion fetched from Merino or null if none.
+ */
+ get suggestion() {
+ return this.#suggestion;
+ }
+
+ /**
+ * @returns {Set}
+ * The set of keywords that should trigger the weather suggestion. This will
+ * be null when no config is defined.
+ */
+ get keywords() {
+ return this.#keywords;
+ }
+
+ /**
+ * @returns {number}
+ * The minimum prefix length of a weather keyword the user must type to
+ * trigger the suggestion. Note that the strings returned from `keywords`
+ * already take this into account. The min length is determined from the
+ * first config source below whose value is non-zero. If no source has a
+ * non-zero value, zero will be returned, and `this.keywords` will contain
+ * only full keywords.
+ *
+ * 1. The `weather.minKeywordLength` pref, which is set when the user
+ * increments the min length
+ * 2. `weatherKeywordsMinimumLength` in Nimbus
+ * 3. `min_keyword_length` in remote settings
+ */
+ get minKeywordLength() {
+ let minLength =
+ lazy.UrlbarPrefs.get("weather.minKeywordLength") ||
+ lazy.UrlbarPrefs.get("weatherKeywordsMinimumLength") ||
+ this.#rsData?.min_keyword_length ||
+ 0;
+ return Math.max(minLength, 0);
+ }
+
+ /**
+ * @returns {boolean}
+ * Weather the min keyword length can be incremented. A cap on the min
+ * length can be set in remote settings and Nimbus.
+ */
+ get canIncrementMinKeywordLength() {
+ let cap =
+ lazy.UrlbarPrefs.get("weatherKeywordsMinimumLengthCap") ||
+ this.#rsData?.min_keyword_length_cap ||
+ 0;
+ return !cap || this.minKeywordLength < cap;
+ }
+
+ update() {
+ let wasEnabled = this.isEnabled;
+ super.update();
+
+ // This method is called by `QuickSuggest` in a
+ // `NimbusFeatures.urlbar.onUpdate()` callback, when a change occurs to a
+ // Nimbus variable or to a pref that's a fallback for a Nimbus variable. A
+ // config-related variable or pref may have changed, so update it, but only
+ // if the feature was already enabled because if it wasn't, `enable(true)`
+ // was just called, which calls `#init()`, which calls `#updateConfig()`.
+ if (wasEnabled && this.isEnabled) {
+ this.#updateConfig();
+ }
+ }
+
+ enable(enabled) {
+ if (enabled) {
+ this.#init();
+ } else {
+ this.#uninit();
+ }
+ }
+
+ /**
+ * Increments the minimum prefix length of a weather keyword the user must
+ * type to trigger the suggestion, if possible. A cap on the min length can be
+ * set in remote settings and Nimbus, and if the cap has been reached, the
+ * length is not incremented.
+ */
+ incrementMinKeywordLength() {
+ if (this.canIncrementMinKeywordLength) {
+ lazy.UrlbarPrefs.set(
+ "weather.minKeywordLength",
+ this.minKeywordLength + 1
+ );
+ }
+ }
+
+ /**
+ * Returns a promise that resolves when all pending fetches finish, if there
+ * are pending fetches. If there aren't, the promise resolves when all pending
+ * fetches starting with the next fetch finish.
+ *
+ * @returns {Promise}
+ */
+ waitForFetches() {
+ if (!this.#waitForFetchesDeferred) {
+ this.#waitForFetchesDeferred = lazy.PromiseUtils.defer();
+ }
+ return this.#waitForFetchesDeferred.promise;
+ }
+
+ async onRemoteSettingsSync(rs) {
+ this.logger.debug("Loading weather config from remote settings");
+ let records = await rs.get({ filters: { type: "weather" } });
+ if (rs != lazy.QuickSuggestRemoteSettings.rs) {
+ return;
+ }
+
+ this.logger.debug("Got weather records: " + JSON.stringify(records));
+ this.#rsData = records?.[0]?.weather;
+ this.#updateConfig();
+ }
+
+ get #vpnDetected() {
+ if (lazy.UrlbarPrefs.get("weather.ignoreVPN")) {
+ return false;
+ }
+
+ let linkService =
+ this._test_linkService ||
+ Cc["@mozilla.org/network/network-link-service;1"].getService(
+ Ci.nsINetworkLinkService
+ );
+
+ // `platformDNSIndications` throws `NS_ERROR_NOT_IMPLEMENTED` on all
+ // platforms except Windows, so we can't detect a VPN on any other platform.
+ try {
+ return (
+ linkService.platformDNSIndications &
+ Ci.nsINetworkLinkService.VPN_DETECTED
+ );
+ } catch (e) {}
+ return false;
+ }
+
+ #init() {
+ // On feature init, we only update the config and listen for changes that
+ // affect the config. Suggestion fetches will not start until a config has
+ // been either synced from remote settings or set by Nimbus.
+ this.#updateConfig();
+ lazy.UrlbarPrefs.addObserver(this);
+ lazy.QuickSuggestRemoteSettings.register(this);
+ }
+
+ #uninit() {
+ this.#stopFetching();
+ lazy.QuickSuggestRemoteSettings.unregister(this);
+ lazy.UrlbarPrefs.removeObserver(this);
+ this.#keywords = null;
+ }
+
+ #startFetching() {
+ if (this.#merino) {
+ this.logger.debug("Suggestion fetching already started");
+ return;
+ }
+
+ this.logger.debug("Starting suggestion fetching");
+
+ this.#merino = new lazy.MerinoClient(this.constructor.name);
+ this.#fetch();
+ for (let notif of Object.values(NOTIFICATIONS)) {
+ Services.obs.addObserver(this, notif);
+ }
+ }
+
+ #stopFetching() {
+ if (!this.#merino) {
+ this.logger.debug("Suggestion fetching already stopped");
+ return;
+ }
+
+ this.logger.debug("Stopping suggestion fetching");
+
+ for (let notif of Object.values(NOTIFICATIONS)) {
+ Services.obs.removeObserver(this, notif);
+ }
+ lazy.clearTimeout(this.#fetchTimer);
+ this.#merino = null;
+ this.#suggestion = null;
+ this.#fetchTimer = 0;
+ }
+
+ async #fetch() {
+ this.logger.info("Fetching suggestion");
+
+ if (this.#vpnDetected) {
+ // A VPN is detected, so Merino will not be able to accurately determine
+ // the user's location. Set the suggestion to null. We treat this as if
+ // the network is offline (see below). When the VPN is disconnected, a
+ // `network:link-status-changed` notification will be sent, triggering a
+ // new fetch.
+ this.logger.info("VPN detected, not fetching");
+ this.#suggestion = null;
+ if (!this.#pendingFetchCount) {
+ this.#waitForFetchesDeferred?.resolve();
+ this.#waitForFetchesDeferred = null;
+ }
+ return;
+ }
+
+ // This `Weather` instance may be uninitialized while awaiting the fetch or
+ // even uninitialized and re-initialized a number of times. Multiple fetches
+ // may also happen at once. Ignore the fetch below if `#merino` changes or
+ // another fetch happens in the meantime.
+ let merino = this.#merino;
+ let instance = (this.#fetchInstance = {});
+
+ this.#restartFetchTimer();
+ this.#lastFetchTimeMs = Date.now();
+ this.#pendingFetchCount++;
+
+ let suggestions;
+ try {
+ suggestions = await merino.fetch({
+ query: "",
+ providers: [MERINO_PROVIDER],
+ timeoutMs: this.#timeoutMs,
+ extraLatencyHistogram: HISTOGRAM_LATENCY,
+ extraResponseHistogram: HISTOGRAM_RESPONSE,
+ });
+ } finally {
+ this.#pendingFetchCount--;
+ }
+
+ // Reset the Merino client's session so different fetches use different
+ // sessions. A single session is intended to represent a single user
+ // engagement in the urlbar, which this is not. Practically this isn't
+ // necessary since the client automatically resets the session on a timer
+ // whose period is much shorter than our fetch period, but there's no reason
+ // to keep it ticking in the meantime.
+ merino.resetSession();
+
+ if (merino != this.#merino || instance != this.#fetchInstance) {
+ this.logger.info("Fetch finished but is out of date, ignoring");
+ } else {
+ let suggestion = suggestions?.[0];
+ if (!suggestion) {
+ // No suggestion was received. The network may be offline or there may
+ // be some other problem. Set the suggestion to null: Better to show
+ // nothing than outdated weather information. When the network comes
+ // back online, one or more network notifications will be sent,
+ // triggering a new fetch.
+ this.logger.info("No suggestion received");
+ this.#suggestion = null;
+ } else {
+ this.logger.info("Got suggestion");
+ this.logger.debug(JSON.stringify({ suggestion }));
+ this.#suggestion = { ...suggestion, source: "merino" };
+ }
+ }
+
+ if (!this.#pendingFetchCount) {
+ this.#waitForFetchesDeferred?.resolve();
+ this.#waitForFetchesDeferred = null;
+ }
+ }
+
+ #restartFetchTimer(ms = this.#fetchIntervalMs) {
+ this.logger.debug(
+ "Restarting fetch timer: " +
+ JSON.stringify({ ms, fetchIntervalMs: this.#fetchIntervalMs })
+ );
+
+ lazy.clearTimeout(this.#fetchTimer);
+ this.#fetchTimer = lazy.setTimeout(() => {
+ this.logger.debug("Fetch timer fired");
+ this.#fetch();
+ }, ms);
+ this._test_fetchTimerMs = ms;
+ }
+
+ #onMaybeCameOnline() {
+ this.logger.debug("Maybe came online");
+
+ // If the suggestion is null, we were offline the last time we tried to
+ // fetch, at the start of the current fetch period. Otherwise the suggestion
+ // was fetched successfully at the start of the current fetch period and is
+ // therefore still fresh.
+ if (!this.suggestion) {
+ // Multiple notifications can occur at once when the network comes online,
+ // and we don't want to do separate fetches for each. Start the timer with
+ // a small timeout. If another notification happens in the meantime, we'll
+ // start it again.
+ this.#restartFetchTimer(this.#fetchDelayAfterComingOnlineMs);
+ }
+ }
+
+ #onWake() {
+ // Calculate the elapsed time between the last fetch and now, and the
+ // remaining interval in the current fetch period.
+ let elapsedMs = Date.now() - this.#lastFetchTimeMs;
+ let remainingIntervalMs = this.#fetchIntervalMs - elapsedMs;
+ this.logger.debug(
+ "Wake: " +
+ JSON.stringify({
+ elapsedMs,
+ remainingIntervalMs,
+ fetchIntervalMs: this.#fetchIntervalMs,
+ })
+ );
+
+ // Regardless of the elapsed time, we need to restart the fetch timer
+ // because it didn't tick while the computer was asleep. If the elapsed time
+ // >= the fetch interval, the remaining interval will be negative and we
+ // need to fetch now, but do it after a brief delay in case other
+ // notifications occur soon when the network comes online. If the elapsed
+ // time < the fetch interval, the suggestion is still fresh so there's no
+ // need to fetch. Just restart the timer with the remaining interval.
+ if (remainingIntervalMs <= 0) {
+ remainingIntervalMs = this.#fetchDelayAfterComingOnlineMs;
+ }
+ this.#restartFetchTimer(remainingIntervalMs);
+ }
+
+ #updateConfig() {
+ this.logger.debug("Starting config update");
+
+ // Get the full keywords, preferring Nimbus over remote settings.
+ let fullKeywords =
+ lazy.UrlbarPrefs.get("weatherKeywords") ?? this.#rsData?.keywords;
+ if (!fullKeywords) {
+ this.logger.debug("No keywords defined, stopping suggestion fetching");
+ this.#keywords = null;
+ this.#stopFetching();
+ return;
+ }
+
+ let minLength = this.minKeywordLength;
+ this.logger.debug(
+ "Updating keywords: " + JSON.stringify({ fullKeywords, minLength })
+ );
+
+ if (!minLength) {
+ this.logger.debug("Min length is undefined or zero, using full keywords");
+ this.#keywords = new Set(fullKeywords);
+ } else {
+ // Create keywords that are prefixes of the full keywords starting at the
+ // specified minimum length.
+ this.#keywords = new Set();
+ for (let full of fullKeywords) {
+ for (let i = minLength; i <= full.length; i++) {
+ this.#keywords.add(full.substring(0, i));
+ }
+ }
+ }
+
+ this.#startFetching();
+ }
+
+ onPrefChanged(pref) {
+ if (pref == "weather.minKeywordLength") {
+ this.#updateConfig();
+ }
+ }
+
+ observe(subject, topic, data) {
+ this.logger.debug(
+ "Observed notification: " + JSON.stringify({ topic, data })
+ );
+
+ switch (topic) {
+ case NOTIFICATIONS.CAPTIVE_PORTAL_LOGIN:
+ this.#onMaybeCameOnline();
+ break;
+ case NOTIFICATIONS.LINK_STATUS_CHANGED:
+ // This notificaton means the user's connection status changed. See
+ // nsINetworkLinkService.
+ if (data != "down") {
+ this.#onMaybeCameOnline();
+ }
+ break;
+ case NOTIFICATIONS.OFFLINE_STATUS_CHANGED:
+ // This notificaton means the user toggled the "Work Offline" pref.
+ // See nsIIOService.
+ if (data != "offline") {
+ this.#onMaybeCameOnline();
+ }
+ break;
+ case NOTIFICATIONS.WAKE:
+ this.#onWake();
+ break;
+ }
+ }
+
+ get _test_fetchDelayAfterComingOnlineMs() {
+ return this.#fetchDelayAfterComingOnlineMs;
+ }
+ set _test_fetchDelayAfterComingOnlineMs(ms) {
+ this.#fetchDelayAfterComingOnlineMs =
+ ms < 0 ? FETCH_DELAY_AFTER_COMING_ONLINE_MS : ms;
+ }
+
+ get _test_fetchIntervalMs() {
+ return this.#fetchIntervalMs;
+ }
+ set _test_fetchIntervalMs(ms) {
+ this.#fetchIntervalMs = ms < 0 ? FETCH_INTERVAL_MS : ms;
+ }
+
+ get _test_fetchTimer() {
+ return this.#fetchTimer;
+ }
+
+ get _test_lastFetchTimeMs() {
+ return this.#lastFetchTimeMs;
+ }
+
+ get _test_merino() {
+ return this.#merino;
+ }
+
+ get _test_pendingFetchCount() {
+ return this.#pendingFetchCount;
+ }
+
+ async _test_fetch() {
+ await this.#fetch();
+ }
+
+ _test_setSuggestionToNull() {
+ this.#suggestion = null;
+ }
+
+ _test_setTimeoutMs(ms) {
+ this.#timeoutMs = ms < 0 ? MERINO_TIMEOUT_MS : ms;
+ }
+
+ #fetchDelayAfterComingOnlineMs = FETCH_DELAY_AFTER_COMING_ONLINE_MS;
+ #fetchInstance = null;
+ #fetchIntervalMs = FETCH_INTERVAL_MS;
+ #fetchTimer = 0;
+ #keywords = null;
+ #lastFetchTimeMs = 0;
+ #merino = null;
+ #pendingFetchCount = 0;
+ #rsData = null;
+ #suggestion = null;
+ #timeoutMs = MERINO_TIMEOUT_MS;
+ #waitForFetchesDeferred = null;
+}