Bug 1944627 - update sidebar button checked state for non-revamped sidebar cases...
[gecko.git] / browser / components / urlbar / UrlbarProviderPrivateSearch.sys.mjs
blobd67295324870e2a65cdd49c45f52f0bd929437d8
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 returning a private search entry.
7  */
9 import {
10   SkippableTimer,
11   UrlbarProvider,
12   UrlbarUtils,
13 } from "resource:///modules/UrlbarUtils.sys.mjs";
15 const lazy = {};
17 ChromeUtils.defineESModuleGetters(lazy, {
18   UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
19   UrlbarSearchUtils: "resource:///modules/UrlbarSearchUtils.sys.mjs",
20   UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.sys.mjs",
21 });
23 /**
24  * Class used to create the provider.
25  */
26 class ProviderPrivateSearch extends UrlbarProvider {
27   constructor() {
28     super();
29     // Maps the open tabs by userContextId.
30     this.openTabs = new Map();
31   }
33   /**
34    * Returns the name of this provider.
35    *
36    * @returns {string} the name of this provider.
37    */
38   get name() {
39     return "PrivateSearch";
40   }
42   /**
43    * Returns the type of this provider.
44    *
45    * @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
46    */
47   get type() {
48     return UrlbarUtils.PROVIDER_TYPE.PROFILE;
49   }
51   /**
52    * Whether this provider should be invoked for the given context.
53    * If this method returns false, the providers manager won't start a query
54    * with this provider, to save on resources.
55    *
56    * @param {UrlbarQueryContext} queryContext The query context object
57    * @returns {boolean} Whether this provider should be invoked for the search.
58    */
59   isActive(queryContext) {
60     return (
61       lazy.UrlbarSearchUtils.separatePrivateDefaultUIEnabled &&
62       !queryContext.isPrivate &&
63       queryContext.tokens.length
64     );
65   }
67   /**
68    * Starts querying.
69    *
70    * @param {object} queryContext The query context object
71    * @param {Function} addCallback Callback invoked by the provider to add a new
72    *        match.
73    * @returns {Promise} resolved when the query stops.
74    */
75   async startQuery(queryContext, addCallback) {
76     let searchString = queryContext.trimmedSearchString;
77     if (
78       queryContext.tokens.some(
79         t => t.type == lazy.UrlbarTokenizer.TYPE.RESTRICT_SEARCH
80       )
81     ) {
82       if (queryContext.tokens.length == 1) {
83         // There's only the restriction token, bail out.
84         return;
85       }
86       // Remove the restriction char from the search string.
87       searchString = queryContext.tokens
88         .filter(t => t.type != lazy.UrlbarTokenizer.TYPE.RESTRICT_SEARCH)
89         .map(t => t.value)
90         .join(" ");
91     }
93     let instance = this.queryInstance;
95     let engine = queryContext.searchMode?.engineName
96       ? Services.search.getEngineByName(queryContext.searchMode.engineName)
97       : await Services.search.getDefaultPrivate();
98     let isPrivateEngine =
99       lazy.UrlbarSearchUtils.separatePrivateDefault &&
100       engine != (await Services.search.getDefault());
101     this.logger.info(`isPrivateEngine: ${isPrivateEngine}`);
103     // This is a delay added before returning results, to avoid flicker.
104     // Our result must appear only when all results are searches, but if search
105     // results arrive first, then the muxer would insert our result and then
106     // immediately remove it when non-search results arrive.
107     await new SkippableTimer({
108       name: "ProviderPrivateSearch",
109       time: 100,
110       logger: this.logger,
111     }).promise;
113     let icon = await engine.getIconURL();
114     if (instance != this.queryInstance) {
115       return;
116     }
118     let result = new lazy.UrlbarResult(
119       UrlbarUtils.RESULT_TYPE.SEARCH,
120       UrlbarUtils.RESULT_SOURCE.SEARCH,
121       ...lazy.UrlbarResult.payloadAndSimpleHighlights(queryContext.tokens, {
122         engine: [engine.name, UrlbarUtils.HIGHLIGHT.TYPED],
123         query: [searchString, UrlbarUtils.HIGHLIGHT.NONE],
124         icon,
125         inPrivateWindow: true,
126         isPrivateEngine,
127       })
128     );
129     result.suggestedIndex = 1;
130     addCallback(this, result);
131   }
134 export var UrlbarProviderPrivateSearch = new ProviderPrivateSearch();