Files
tubestation/browser/components/urlbar/private/BaseFeature.sys.mjs
Drew Willcoxon 5b6f606320 Bug 1907696 - Change the Suggest ingest strategy so only enabled suggestion types are ingested. r=daisuke
Please see the bug for context. This changes how ingest works. Right now we
ingest all "default" suggestion types at once. This patch causes a suggestion
type to be ingested only when it's enabled.

Before this patch, there are two times ingest happens, on startup and when the
ingest timer fires.

With this patch, there are now a few times ingest happens:

* On startup (as before). `SuggestBackendRust` will ingest all registered and
  enabled suggestion types.
* When the ingest timer fires (as before). Similar to startup, all registered
  and enabled types will be ingested.
* When a suggestion type becomes enabled. I added this functionality to
  `BaseFeature.update()` so that each feature gets this behavior automatically.

It's worth saying more about the startup case. On startup, all `BaseFeature`s'
initializations race each other. `SuggestBackendRust` is itself a `BaseFeature`,
so there's no guarantee it will be initialized before other `BaseFeature`s that
implement Rust suggestions. If any `BaseFeature`s are initialized before
`SuggestBackendRust`, they'll try to ingest but nothing will happen because
`SuggestBackendRust` won't be initialized yet. Once `SuggestBackendRust` is
initialized, it will ingest suggestions for all registered `BaseFeature`s that
already tried and failed to ingest. And after `SuggestBackendRust` initializes,
any `BaseFeature`s that are initialized later will be able to successfully
ingest.

Differential Revision: https://phabricator.services.mozilla.com/D216488
2024-07-17 01:25:44 +00:00

254 lines
8.9 KiB
JavaScript

/* 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, {
QuickSuggest: "resource:///modules/QuickSuggest.sys.mjs",
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 any prefs that
* are not fallbacks for Nimbus variables, the subclass should override this
* getter and return their names in this array so that `update()` can be
* called when they change. Names should be relative to `browser.urlbar.`.
* It doesn't hurt to include prefs that are fallbacks for Nimbus variables,
* it's just not necessary because `QuickSuggest` will update all features
* whenever a `urlbar` Nimbus variable or its fallback pref changes.
*/
get enablingPreferences() {
return null;
}
/**
* @returns {string}
* If the feature manages suggestions served by Merino, the subclass should
* override this getter and return the name of the specific Merino provider
* that serves them.
*/
get merinoProvider() {
return "";
}
/**
* @returns {Array}
* If the feature manages one or more types of suggestions served by the
* Suggest Rust component, the subclass should override this getter and
* return an array of the type names as defined in `suggest.udl`.
*/
get rustSuggestionTypes() {
return [];
}
/**
* 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 manages suggestions that either aren't served by Merino or
* whose telemetry type is different from `merinoProvider`, the subclass
* should override this method. It should return the telemetry type for the
* given suggestion. A telemetry type uniquely identifies a type of suggestion
* as well as the kind of `UrlbarResult` instances created from it.
*
* @param {object} _suggestion
* A suggestion from either remote settings or Merino.
* @returns {string}
* The suggestion's telemetry type.
*/
getSuggestionTelemetryType(_suggestion) {
return this.merinoProvider;
}
/**
* If the feature manages more than one type of suggestion served by the
* Suggest Rust component, the subclass should override this method and return
* true if the given suggestion type is enabled and false otherwise. Ideally a
* feature manages at most one type of Rust suggestion, and in that case it's
* fine to rely on the default implementation here because the suggestion type
* will be enabled iff the feature itself is enabled.
*
* @param {string} _type
* A Rust suggestion type name as defined in `suggest.udl`. See also
* `rustSuggestionTypes`.
* @returns {boolean}
* Whether the suggestion type is enabled.
*/
isRustSuggestionTypeEnabled(_type) {
return true;
}
/**
* 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's enabled status changes,
* `enable()` is called with the new status; otherwise `enable()` is not
* called. If the feature manages any Rust suggestion types that become
* enabled as a result, they will be ingested.
*/
update() {
// Collect the feature's Rust suggestion types that are currently enabled.
// Features can manage multiple types. Some may be enabled while others are
// disabled. As long as one is enabled, the feature itself will be enabled.
let { rustBackend } = lazy.QuickSuggest;
let oldEnabledRustSuggestionTypes =
rustBackend.isEnabled && this.isEnabled
? this.rustSuggestionTypes.filter(type =>
this.isRustSuggestionTypeEnabled(type)
)
: [];
// Update the feature's enabled status.
let enable =
lazy.UrlbarPrefs.get("quickSuggestEnabled") && this.shouldEnable;
if (enable != this.isEnabled) {
this.logger.info(`Setting enabled = ${enable}`);
this.enable(enable);
this.#isEnabled = enable;
}
// Ingest all the feature's Rust suggestion types that just became enabled.
// This will be their initial ingests. After that, their ingests will occur
// according to the ingest timer in the backend.
if (rustBackend.isEnabled && enable) {
for (let type of this.rustSuggestionTypes) {
if (
this.isRustSuggestionTypeEnabled(type) &&
!oldEnabledRustSuggestionTypes.includes(type)
) {
rustBackend.ingestSuggestionType(type);
}
}
}
}
#isEnabled = false;
}