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
288
289
290
291
292
293
294
295
296
297
|
/* 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 that provides preloaded site results. These
* are intended to populate address bar results when the user has no history.
* They can be both autofilled and provided as regular results.
*/
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import {
UrlbarProvider,
UrlbarUtils,
} from "resource:///modules/UrlbarUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ProfileAge: "resource://gre/modules/ProfileAge.sys.mjs",
UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.sys.mjs",
});
const MS_PER_DAY = 86400000; // 24 * 60 * 60 * 1000
function PreloadedSite(url, title) {
this.uri = Services.io.newURI(url);
this.title = title;
this._matchTitle = title.toLowerCase();
this._hasWWW = this.uri.host.startsWith("www.");
this._hostWithoutWWW = this._hasWWW ? this.uri.host.slice(4) : this.uri.host;
}
/**
* Storage object for Preloaded Sites.
* add(url, title): adds a site to storage
* populate(sites) : populates the storage with array of [url,title]
* sites[]: resulting array of sites (PreloadedSite objects)
*/
XPCOMUtils.defineLazyGetter(lazy, "PreloadedSiteStorage", () =>
Object.seal({
sites: [],
add(url, title) {
let site = new PreloadedSite(url, title);
this.sites.push(site);
},
populate(sites) {
this.sites = [];
for (let site of sites) {
this.add(site[0], site[1]);
}
},
})
);
XPCOMUtils.defineLazyGetter(lazy, "ProfileAgeCreatedPromise", async () => {
let times = await lazy.ProfileAge();
return times.created;
});
/**
* Class used to create the provider.
*/
class ProviderPreloadedSites extends UrlbarProvider {
constructor() {
super();
if (lazy.UrlbarPrefs.get("usepreloadedtopurls.enabled")) {
fetch("chrome://browser/content/urlbar/preloaded-top-urls.json")
.then(response => response.json())
.then(sites => lazy.PreloadedSiteStorage.populate(sites))
.catch(ex => this.logger.error(ex));
}
}
/**
* Returns the name of this provider.
*
* @returns {string} the name of this provider.
*/
get name() {
return "PreloadedSites";
}
/**
* Returns the type of this provider.
*
* @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
*/
get type() {
return UrlbarUtils.PROVIDER_TYPE.HEURISTIC;
}
/**
* 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.
*/
async isActive(queryContext) {
let instance = this.queryInstance;
// This is usually reset on canceling or completing the query, but since we
// query in isActive, it may not have been canceled by the previous call.
// It is an object with values { result: UrlbarResult, instance: Query }.
// See the documentation for _getAutofillData for more information.
this._autofillData = null;
await this._checkPreloadedSitesExpiry();
if (instance != this.queryInstance) {
return false;
}
if (!lazy.UrlbarPrefs.get("usepreloadedtopurls.enabled")) {
return false;
}
if (
!lazy.UrlbarPrefs.get("autoFill") ||
!queryContext.allowAutofill ||
queryContext.tokens.length != 1
) {
return false;
}
// Trying to autofill an extremely long string would be expensive, and
// not particularly useful since the filled part falls out of screen anyway.
if (queryContext.searchString.length > UrlbarUtils.MAX_TEXT_LENGTH) {
return false;
}
[this._strippedPrefix, this._searchString] = UrlbarUtils.stripURLPrefix(
queryContext.searchString
);
if (!this._searchString || !this._searchString.length) {
return false;
}
this._lowerCaseSearchString = this._searchString.toLowerCase();
this._strippedPrefix = this._strippedPrefix.toLowerCase();
// As an optimization, don't try to autofill if the search term includes any
// whitespace.
if (lazy.UrlbarTokenizer.REGEXP_SPACES.test(queryContext.searchString)) {
return false;
}
// Fetch autofill result now, rather than in startQuery. We do this so the
// muxer doesn't have to wait on autofill for every query, since startQuery
// will be guaranteed to return a result very quickly using this approach.
// Bug 1651101 is filed to improve this behaviour.
let result = await this._getAutofillResult(queryContext);
if (instance != this.queryInstance) {
return false;
}
this._autofillData = { result, instance };
return true;
}
/**
* Starts querying.
*
* @param {object} queryContext The query context object
* @param {Function} addCallback Callback invoked by the provider to add a new
* result.
* @returns {Promise} resolved when the query stops.
*/
async startQuery(queryContext, addCallback) {
// Check if the query was cancelled while the autofill result was being
// fetched. We don't expect this to be true since we also check the instance
// in isActive and clear _autofillData in cancelQuery, but we sanity check it.
if (
this._autofillData.result &&
this._autofillData.instance == this.queryInstance
) {
this._autofillData.result.heuristic = true;
addCallback(this, this._autofillData.result);
this._autofillData = null;
}
// Now, add non-autofill preloaded sites.
for (let site of lazy.PreloadedSiteStorage.sites) {
let url = site.uri.spec;
if (
(!this._strippedPrefix || url.startsWith(this._strippedPrefix)) &&
(site.uri.host.includes(this._lowerCaseSearchString) ||
site._matchTitle.includes(this._lowerCaseSearchString))
) {
let result = new lazy.UrlbarResult(
UrlbarUtils.RESULT_TYPE.URL,
UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
...lazy.UrlbarResult.payloadAndSimpleHighlights(queryContext.tokens, {
title: [site.title, UrlbarUtils.HIGHLIGHT.TYPED],
url: [url, UrlbarUtils.HIGHLIGHT.TYPED],
icon: UrlbarUtils.getIconForUrl(url),
})
);
addCallback(this, result);
}
}
}
/**
* Cancels a running query.
*
* @param {object} queryContext The query context object
*/
cancelQuery(queryContext) {
if (this._autofillData?.instance == this.queryInstance) {
this._autofillData = null;
}
}
/**
* Populates the preloaded site cache with a list of sites. Intended for tests
* only.
*
* @param {Array} list
* An array of URLs mapped to titles. See preloaded-top-urls.json for
* the format.
*/
populatePreloadedSiteStorage(list) {
lazy.PreloadedSiteStorage.populate(list);
}
async _getAutofillResult(queryContext) {
let matchedSite = lazy.PreloadedSiteStorage.sites.find(site => {
return (
(!this._strippedPrefix ||
site.uri.spec.startsWith(this._strippedPrefix)) &&
(site.uri.host.startsWith(this._lowerCaseSearchString) ||
site.uri.host.startsWith("www." + this._lowerCaseSearchString))
);
});
if (!matchedSite) {
return null;
}
let url = matchedSite.uri.spec;
let [title] = UrlbarUtils.stripPrefixAndTrim(url, {
stripHttp: true,
trimEmptyQuery: true,
trimSlash: !this._searchString.includes("/"),
});
let result = new lazy.UrlbarResult(
UrlbarUtils.RESULT_TYPE.URL,
UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
...lazy.UrlbarResult.payloadAndSimpleHighlights(queryContext.tokens, {
title: [title, UrlbarUtils.HIGHLIGHT.TYPED],
url: [url, UrlbarUtils.HIGHLIGHT.TYPED],
icon: UrlbarUtils.getIconForUrl(url),
})
);
let value = UrlbarUtils.stripURLPrefix(url)[1];
value =
this._strippedPrefix + value.substr(value.indexOf(this._searchString));
let autofilledValue =
queryContext.searchString +
value.substring(queryContext.searchString.length);
result.autofill = {
type: "preloaded",
value: autofilledValue,
selectionStart: queryContext.searchString.length,
selectionEnd: autofilledValue.length,
};
return result;
}
async _checkPreloadedSitesExpiry() {
if (!lazy.UrlbarPrefs.get("usepreloadedtopurls.enabled")) {
return;
}
let profileCreationDate = await lazy.ProfileAgeCreatedPromise;
let daysSinceProfileCreation =
(Date.now() - profileCreationDate) / MS_PER_DAY;
if (
daysSinceProfileCreation >
lazy.UrlbarPrefs.get("usepreloadedtopurls.expire_days")
) {
Services.prefs.setBoolPref(
"browser.urlbar.usepreloadedtopurls.enabled",
false
);
}
}
}
export var UrlbarProviderPreloadedSites = new ProviderPreloadedSites();
|