Bug 1944627 - update sidebar button checked state for non-revamped sidebar cases...
[gecko.git] / browser / components / urlbar / UrlbarProviderBookmarkKeywords.sys.mjs
blobfae00ffb68d50706469938c68d17326d1129610f
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /**
6  * This module exports a provider that offers bookmarks with keywords.
7  */
9 import {
10   UrlbarProvider,
11   UrlbarUtils,
12 } from "resource:///modules/UrlbarUtils.sys.mjs";
14 const lazy = {};
16 ChromeUtils.defineESModuleGetters(lazy, {
17   KeywordUtils: "resource://gre/modules/KeywordUtils.sys.mjs",
18   UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
19 });
21 /**
22  * Class used to create the provider.
23  */
24 class ProviderBookmarkKeywords extends UrlbarProvider {
25   /**
26    * Returns the name of this provider.
27    *
28    * @returns {string} the name of this provider.
29    */
30   get name() {
31     return "BookmarkKeywords";
32   }
34   /**
35    * Returns the type of this provider.
36    *
37    * @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
38    */
39   get type() {
40     return UrlbarUtils.PROVIDER_TYPE.HEURISTIC;
41   }
43   /**
44    * Whether this provider should be invoked for the given context.
45    * If this method returns false, the providers manager won't start a query
46    * with this provider, to save on resources.
47    *
48    * @param {UrlbarQueryContext} queryContext The query context object
49    * @returns {boolean} Whether this provider should be invoked for the search.
50    */
51   isActive(queryContext) {
52     return (
53       (!queryContext.restrictSource ||
54         queryContext.restrictSource == UrlbarUtils.RESULT_SOURCE.BOOKMARKS) &&
55       !queryContext.searchMode &&
56       queryContext.tokens.length
57     );
58   }
60   /**
61    * Starts querying.
62    *
63    * @param {object} queryContext The query context object
64    * @param {Function} addCallback Callback invoked by the provider to add a new
65    *        result.
66    */
67   async startQuery(queryContext, addCallback) {
68     let keyword = queryContext.tokens[0]?.value;
70     let searchString = UrlbarUtils.substringAfter(
71       queryContext.searchString,
72       keyword
73     ).trim();
74     let { entry, url, postData } = await lazy.KeywordUtils.getBindableKeyword(
75       keyword,
76       searchString
77     );
78     if (!entry || !url) {
79       return;
80     }
82     let title;
83     if (entry.url.host && searchString) {
84       // If we have a search string, the result has the title
85       // "host: searchString".
86       title = UrlbarUtils.strings.formatStringFromName(
87         "bookmarkKeywordSearch",
88         [
89           entry.url.host,
90           queryContext.tokens
91             .slice(1)
92             .map(t => t.value)
93             .join(" "),
94         ]
95       );
96     } else {
97       title = UrlbarUtils.prepareUrlForDisplay(url);
98     }
100     let result = new lazy.UrlbarResult(
101       UrlbarUtils.RESULT_TYPE.KEYWORD,
102       UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
103       ...lazy.UrlbarResult.payloadAndSimpleHighlights(queryContext.tokens, {
104         title: [title, UrlbarUtils.HIGHLIGHT.TYPED],
105         url: [url, UrlbarUtils.HIGHLIGHT.TYPED],
106         keyword: [keyword, UrlbarUtils.HIGHLIGHT.TYPED],
107         input: queryContext.searchString,
108         postData,
109         icon: UrlbarUtils.getIconForUrl(entry.url),
110       })
111     );
112     result.heuristic = true;
113     addCallback(this, result);
114   }
117 export var UrlbarProviderBookmarkKeywords = new ProviderBookmarkKeywords();