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/. */
6 * This module exports a provider returning a private search entry.
13 } from "resource:///modules/UrlbarUtils.sys.mjs";
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",
24 * Class used to create the provider.
26 class ProviderPrivateSearch extends UrlbarProvider {
29 // Maps the open tabs by userContextId.
30 this.openTabs = new Map();
34 * Returns the name of this provider.
36 * @returns {string} the name of this provider.
39 return "PrivateSearch";
43 * Returns the type of this provider.
45 * @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
48 return UrlbarUtils.PROVIDER_TYPE.PROFILE;
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.
56 * @param {UrlbarQueryContext} queryContext The query context object
57 * @returns {boolean} Whether this provider should be invoked for the search.
59 isActive(queryContext) {
61 lazy.UrlbarSearchUtils.separatePrivateDefaultUIEnabled &&
62 !queryContext.isPrivate &&
63 queryContext.tokens.length
70 * @param {object} queryContext The query context object
71 * @param {Function} addCallback Callback invoked by the provider to add a new
73 * @returns {Promise} resolved when the query stops.
75 async startQuery(queryContext, addCallback) {
76 let searchString = queryContext.trimmedSearchString;
78 queryContext.tokens.some(
79 t => t.type == lazy.UrlbarTokenizer.TYPE.RESTRICT_SEARCH
82 if (queryContext.tokens.length == 1) {
83 // There's only the restriction token, bail out.
86 // Remove the restriction char from the search string.
87 searchString = queryContext.tokens
88 .filter(t => t.type != lazy.UrlbarTokenizer.TYPE.RESTRICT_SEARCH)
93 let instance = this.queryInstance;
95 let engine = queryContext.searchMode?.engineName
96 ? Services.search.getEngineByName(queryContext.searchMode.engineName)
97 : await Services.search.getDefaultPrivate();
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",
113 let icon = await engine.getIconURL();
114 if (instance != this.queryInstance) {
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],
125 inPrivateWindow: true,
129 result.suggestedIndex = 1;
130 addCallback(this, result);
134 export var UrlbarProviderPrivateSearch = new ProviderPrivateSearch();