summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/UrlbarProviderAliasEngines.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /browser/components/urlbar/UrlbarProviderAliasEngines.sys.mjs
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--browser/components/urlbar/UrlbarProviderAliasEngines.sys.mjs95
1 files changed, 95 insertions, 0 deletions
diff --git a/browser/components/urlbar/UrlbarProviderAliasEngines.sys.mjs b/browser/components/urlbar/UrlbarProviderAliasEngines.sys.mjs
new file mode 100644
index 0000000000..da14203492
--- /dev/null
+++ b/browser/components/urlbar/UrlbarProviderAliasEngines.sys.mjs
@@ -0,0 +1,95 @@
+/* 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/. */
+
+/**
+ * This module exports a provider that offers engines with aliases as heuristic
+ * results.
+ */
+
+import {
+ UrlbarProvider,
+ UrlbarUtils,
+} from "resource:///modules/UrlbarUtils.sys.mjs";
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
+ UrlbarSearchUtils: "resource:///modules/UrlbarSearchUtils.sys.mjs",
+ UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.sys.mjs",
+});
+
+/**
+ * Class used to create the provider.
+ */
+class ProviderAliasEngines extends UrlbarProvider {
+ /**
+ * Returns the name of this provider.
+ *
+ * @returns {string} the name of this provider.
+ */
+ get name() {
+ return "AliasEngines";
+ }
+
+ /**
+ * Returns the type of this provider.
+ *
+ * @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
+ */
+ get type() {
+ return UrlbarUtils.PROVIDER_TYPE.HEURISTIC;
+ }
+
+ /**
+ * Whether this provider should be invoked for the given context.
+ * If this method returns false, the providers manager won't start a query
+ * with this provider, to save on resources.
+ *
+ * @param {UrlbarQueryContext} queryContext The query context object
+ * @returns {boolean} Whether this provider should be invoked for the search.
+ */
+ isActive(queryContext) {
+ return (
+ (!queryContext.restrictSource ||
+ queryContext.restrictSource == lazy.UrlbarTokenizer.RESTRICT.SEARCH) &&
+ !queryContext.searchMode &&
+ queryContext.tokens.length
+ );
+ }
+
+ /**
+ * Starts querying.
+ *
+ * @param {object} queryContext The query context object
+ * @param {Function} addCallback Callback invoked by the provider to add a new
+ * result.
+ */
+ async startQuery(queryContext, addCallback) {
+ let instance = this.queryInstance;
+ let alias = queryContext.tokens[0]?.value;
+ let engine = await lazy.UrlbarSearchUtils.engineForAlias(
+ alias,
+ queryContext.searchString
+ );
+ if (!engine || instance != this.queryInstance) {
+ return;
+ }
+ let query = UrlbarUtils.substringAfter(queryContext.searchString, alias);
+ let result = new lazy.UrlbarResult(
+ UrlbarUtils.RESULT_TYPE.SEARCH,
+ UrlbarUtils.RESULT_SOURCE.SEARCH,
+ ...lazy.UrlbarResult.payloadAndSimpleHighlights(queryContext.tokens, {
+ engine: engine.name,
+ keyword: alias,
+ query: query.trimStart(),
+ icon: engine.iconURI?.spec,
+ })
+ );
+ result.heuristic = true;
+ addCallback(this, result);
+ }
+}
+
+export var UrlbarProviderAliasEngines = new ProviderAliasEngines();