1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/* 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 class that is used for providers created by
* extensions using the `omnibox` API.
*/
import {
SkippableTimer,
UrlbarProvider,
UrlbarUtils,
} from "resource:///modules/UrlbarUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ExtensionSearchHandler:
"resource://gre/modules/ExtensionSearchHandler.sys.mjs",
UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
});
/**
* This provider handles results returned by extensions using the WebExtensions
* Omnibox API. If the user types a registered keyword, we send subsequent
* keystrokes to the extension.
*/
class ProviderOmnibox extends UrlbarProvider {
constructor() {
super();
}
/**
* Returns the name of this provider.
*
* @returns {string} the name of this provider.
*/
get name() {
return "Omnibox";
}
/**
* Returns the type of this provider.
*
* @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
*/
get type() {
return UrlbarUtils.PROVIDER_TYPE.HEURISTIC;
}
/**
* Whether the 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) {
if (
queryContext.tokens[0] &&
queryContext.tokens[0].value.length &&
lazy.ExtensionSearchHandler.isKeywordRegistered(
queryContext.tokens[0].value
) &&
UrlbarUtils.substringAfter(
queryContext.searchString,
queryContext.tokens[0].value
)
) {
return true;
}
// We need to handle cancellation here since isActive is called once per
// query but cancelQuery can be called multiple times per query.
// The frequent cancels can cause the extension's state to drift from the
// provider's state.
if (lazy.ExtensionSearchHandler.hasActiveInputSession()) {
lazy.ExtensionSearchHandler.handleInputCancelled();
}
return false;
}
/**
* Gets the provider's priority.
*
* @returns {number}
* The provider's priority for the given query.
*/
getPriority() {
return 0;
}
/**
* This method is called by the providers manager when a query starts to fetch
* each extension provider's results. It fires the resultsRequested event.
*
* @param {UrlbarQueryContext} queryContext
* The query context object.
* @param {Function} addCallback
* The callback invoked by this method to add each result.
*/
async startQuery(queryContext, addCallback) {
let instance = this.queryInstance;
// Fetch heuristic result.
let keyword = queryContext.tokens[0].value;
let description = lazy.ExtensionSearchHandler.getDescription(keyword);
let heuristicResult = new lazy.UrlbarResult(
UrlbarUtils.RESULT_TYPE.OMNIBOX,
UrlbarUtils.RESULT_SOURCE.ADDON,
...lazy.UrlbarResult.payloadAndSimpleHighlights(queryContext.tokens, {
title: [description, UrlbarUtils.HIGHLIGHT.TYPED],
content: [queryContext.searchString, UrlbarUtils.HIGHLIGHT.TYPED],
keyword: [queryContext.tokens[0].value, UrlbarUtils.HIGHLIGHT.TYPED],
icon: UrlbarUtils.ICON.EXTENSION,
})
);
heuristicResult.heuristic = true;
addCallback(this, heuristicResult);
// Fetch non-heuristic results.
let data = {
keyword,
text: queryContext.searchString,
inPrivateWindow: queryContext.isPrivate,
};
this._resultsPromise = lazy.ExtensionSearchHandler.handleSearch(
data,
suggestions => {
if (instance != this.queryInstance) {
return;
}
for (let suggestion of suggestions) {
let content = `${queryContext.tokens[0].value} ${suggestion.content}`;
if (content == heuristicResult.payload.content) {
continue;
}
let result = new lazy.UrlbarResult(
UrlbarUtils.RESULT_TYPE.OMNIBOX,
UrlbarUtils.RESULT_SOURCE.ADDON,
...lazy.UrlbarResult.payloadAndSimpleHighlights(
queryContext.tokens,
{
title: [suggestion.description, UrlbarUtils.HIGHLIGHT.TYPED],
content: [content, UrlbarUtils.HIGHLIGHT.TYPED],
keyword: [
queryContext.tokens[0].value,
UrlbarUtils.HIGHLIGHT.TYPED,
],
blockL10n: { id: "urlbar-result-menu-dismiss-firefox-suggest" },
isBlockable: suggestion.deletable,
icon: UrlbarUtils.ICON.EXTENSION,
}
)
);
addCallback(this, result);
}
}
);
// Since the extension has no way to signal when it's done pushing results,
// we add a timer racing with the addition.
let timeoutPromise = new SkippableTimer({
name: "ProviderOmnibox",
time: lazy.UrlbarPrefs.get("extension.omnibox.timeout"),
logger: this.logger,
}).promise;
await Promise.race([timeoutPromise, this._resultsPromise]).catch(ex =>
this.logger.error(ex)
);
}
onLegacyEngagement(state, queryContext, details, controller) {
let { result } = details;
if (result?.providerName != this.name) {
return;
}
if (details.selType == "dismiss" && result.payload.isBlockable) {
lazy.ExtensionSearchHandler.handleInputDeleted(result.payload.title);
controller.removeResult(result);
}
}
}
export var UrlbarProviderOmnibox = new ProviderOmnibox();
|