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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/* 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/. */
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
import {
UrlbarProvider,
UrlbarUtils,
} from "resource:///modules/UrlbarUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
UrlbarSearchUtils: "resource:///modules/UrlbarSearchUtils.sys.mjs",
UrlbarView: "resource:///modules/UrlbarView.sys.mjs",
});
XPCOMUtils.defineLazyModuleGetters(lazy, {
BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
OpenSearchEngine: "resource://gre/modules/OpenSearchEngine.jsm",
});
const DYNAMIC_RESULT_TYPE = "contextualSearch";
const ENABLED_PREF = "contextualSearch.enabled";
const VIEW_TEMPLATE = {
attributes: {
role: "group",
selectable: true,
},
children: [
{
name: "no-wrap",
tag: "span",
classList: ["urlbarView-no-wrap"],
children: [
{
name: "icon",
tag: "img",
classList: ["urlbarView-favicon"],
},
{
name: "search",
tag: "span",
classList: ["urlbarView-title"],
},
{
name: "separator",
tag: "span",
classList: ["urlbarView-title-separator"],
},
{
name: "description",
tag: "span",
},
],
},
],
};
/**
* A provider that returns an option for using the search engine provided
* by the active view if it utilizes OpenSearch.
*/
class ProviderContextualSearch extends UrlbarProvider {
constructor() {
super();
this.engines = new Map();
lazy.UrlbarResult.addDynamicResultType(DYNAMIC_RESULT_TYPE);
lazy.UrlbarView.addDynamicViewTemplate(DYNAMIC_RESULT_TYPE, VIEW_TEMPLATE);
}
/**
* Unique name for the provider, used by the context to filter on providers.
* Not using a unique name will cause the newest registration to win.
*
* @returns {string}
*/
get name() {
return "UrlbarProviderContextualSearch";
}
/**
* The type of the provider.
*
* @returns {UrlbarUtils.PROVIDER_TYPE}
*/
get type() {
return UrlbarUtils.PROVIDER_TYPE.PROFILE;
}
/**
* 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.trimmedSearchString &&
!queryContext.searchMode &&
lazy.UrlbarPrefs.get(ENABLED_PREF)
);
}
/**
* Starts querying. Extended classes should return a Promise resolved when the
* provider is done searching AND returning results.
*
* @param {UrlbarQueryContext} queryContext The query context object
* @param {Function} addCallback Callback invoked by the provider to add a new
* result. A UrlbarResult should be passed to it.
*/
async startQuery(queryContext, addCallback) {
let engine;
const hostname =
queryContext?.currentPage && new URL(queryContext.currentPage).hostname;
// This happens on about pages, which won't have associated engines
if (!hostname) {
return;
}
// First check to see if there's a cached search engine for the host.
// If not, check to see if an installed engine matches the current view.
if (this.engines.has(hostname)) {
engine = this.engines.get(hostname);
} else {
// Strip www. to allow for partial matches when looking for an engine.
const [host] = UrlbarUtils.stripPrefixAndTrim(hostname, {
stripWww: true,
});
engine = (
await lazy.UrlbarSearchUtils.enginesForDomainPrefix(host, {
matchAllDomainLevels: true,
onlyEnabled: false,
})
)[0];
}
if (engine) {
this.engines.set(hostname, engine);
// Check to see if the engine that was found is the default engine.
// The default engine will often be used to populate the heuristic result,
// and we want to avoid ending up with two nearly identical search results.
let defaultEngine = lazy.UrlbarSearchUtils.getDefaultEngine();
if (engine.name === defaultEngine.name) {
return;
}
const [url] = UrlbarUtils.getSearchQueryUrl(
engine,
queryContext.searchString
);
let result = this.makeResult({
url,
engine: engine.name,
icon: engine.iconURI?.spec,
input: queryContext.searchString,
shouldNavigate: true,
});
addCallback(this, result);
return;
}
// If the current view has engines that haven't been added, return a result
// that will first add an engine, then use it to search.
let window = lazy.BrowserWindowTracker.getTopWindow();
let engineToAdd = window?.gBrowser.selectedBrowser?.engines?.[0];
if (engineToAdd) {
let result = this.makeResult({
hostname,
url: engineToAdd.uri,
engine: engineToAdd.title,
icon: engineToAdd.icon,
input: queryContext.searchString,
shouldAddEngine: true,
});
addCallback(this, result);
}
}
makeResult({
engine,
icon,
url,
input,
hostname,
shouldNavigate = false,
shouldAddEngine = false,
}) {
let result = new lazy.UrlbarResult(
UrlbarUtils.RESULT_TYPE.DYNAMIC,
UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
{
engine,
icon,
url,
input,
hostname,
shouldAddEngine,
shouldNavigate,
dynamicType: DYNAMIC_RESULT_TYPE,
}
);
result.suggestedIndex = -1;
return result;
}
/**
* This is called when the urlbar view updates the view of one of the results
* of the provider. It should return an object describing the view update.
* See the base UrlbarProvider class for more.
*
* @param {UrlbarResult} result The result whose view will be updated.
* @param {Map} idsByName
* A Map from an element's name, as defined by the provider; to its ID in
* the DOM, as defined by the browser.
* @returns {object} An object describing the view update.
*/
getViewUpdate(result, idsByName) {
return {
icon: {
attributes: {
src: result.payload.icon || UrlbarUtils.ICON.SEARCH_GLASS,
},
},
search: {
textContent: result.payload.input,
attributes: {
title: result.payload.input,
},
},
description: {
l10n: {
id: "urlbar-result-action-search-w-engine",
args: {
engine: result.payload.engine,
},
},
},
};
}
/**
* Called when any selectable element in a dynamic result's view is picked.
*
* @param {UrlbarResult} result
* The result that was picked.
* @param {Element} element
* The element in the result's view that was picked.
*/
async pickResult(result, element) {
// If we have an engine to add, first create a new OpenSearchEngine, then
// get and open a url to execute a search for the term in the url bar.
// In cases where we don't have to create a new engine, navigation is
// handled automatically by providing `shouldNavigate: true` in the result.
if (result.payload.shouldAddEngine) {
let newEngine = new lazy.OpenSearchEngine({ shouldPersist: false });
newEngine._setIcon(result.payload.icon, false);
await new Promise(resolve => {
newEngine.install(result.payload.url, errorCode => {
resolve(errorCode);
});
});
this.engines.set(result.payload.hostname, newEngine);
const [url] = UrlbarUtils.getSearchQueryUrl(
newEngine,
result.payload.input
);
let window = lazy.BrowserWindowTracker.getTopWindow();
window.gBrowser.loadURI(url, {
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
});
}
}
}
export var UrlbarProviderContextualSearch = new ProviderContextualSearch();
|