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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
|
/* 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/. */
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import {
UrlbarProvider,
UrlbarUtils,
} from "resource:///modules/UrlbarUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
AppUpdater: "resource://gre/modules/AppUpdater.sys.mjs",
NLP: "resource://gre/modules/NLP.sys.mjs",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
ResetProfile: "resource://gre/modules/ResetProfile.sys.mjs",
Sanitizer: "resource:///modules/Sanitizer.sys.mjs",
UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.sys.mjs",
});
XPCOMUtils.defineLazyModuleGetters(lazy, {
BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
});
XPCOMUtils.defineLazyGetter(lazy, "appUpdater", () => new lazy.AppUpdater());
// The possible tips to show. These names (except NONE) are used in the names
// of keys in the `urlbar.tips` keyed scalar telemetry (see telemetry.rst).
// Don't modify them unless you've considered that. If you do modify them or
// add new tips, then you are also adding new `urlbar.tips` keys and therefore
// need an expanded data collection review.
const TIPS = {
NONE: "",
CLEAR: "intervention_clear",
REFRESH: "intervention_refresh",
// There's an update available, but the user's pref says we should ask them to
// download and apply it.
UPDATE_ASK: "intervention_update_ask",
// The updater is currently checking. We don't actually show a tip for this,
// but we use it to tell whether we should wait for the check to complete in
// startQuery. See startQuery for details.
UPDATE_CHECKING: "intervention_update_checking",
// The user's browser is up to date, but they triggered the update
// intervention. We show this special refresh intervention instead.
UPDATE_REFRESH: "intervention_update_refresh",
// There's an update and it's been downloaded and applied. The user needs to
// restart to finish.
UPDATE_RESTART: "intervention_update_restart",
// We can't update the browser or possibly even check for updates for some
// reason, so the user should download the latest version from the web.
UPDATE_WEB: "intervention_update_web",
};
const EN_LOCALE_MATCH = /^en(-.*)$/;
// The search "documents" corresponding to each tip type.
const DOCUMENTS = {
clear: [
"cache firefox",
"clear cache firefox",
"clear cache in firefox",
"clear cookies firefox",
"clear firefox cache",
"clear history firefox",
"cookies firefox",
"delete cookies firefox",
"delete history firefox",
"firefox cache",
"firefox clear cache",
"firefox clear cookies",
"firefox clear history",
"firefox cookie",
"firefox cookies",
"firefox delete cookies",
"firefox delete history",
"firefox history",
"firefox not loading pages",
"history firefox",
"how to clear cache",
"how to clear history",
],
refresh: [
"firefox crashing",
"firefox keeps crashing",
"firefox not responding",
"firefox not working",
"firefox refresh",
"firefox slow",
"how to reset firefox",
"refresh firefox",
"reset firefox",
],
update: [
"download firefox",
"download mozilla",
"firefox browser",
"firefox download",
"firefox for mac",
"firefox for windows",
"firefox free download",
"firefox install",
"firefox installer",
"firefox latest version",
"firefox mac",
"firefox quantum",
"firefox update",
"firefox version",
"firefox windows",
"get firefox",
"how to update firefox",
"install firefox",
"mozilla download",
"mozilla firefox 2019",
"mozilla firefox 2020",
"mozilla firefox download",
"mozilla firefox for mac",
"mozilla firefox for windows",
"mozilla firefox free download",
"mozilla firefox mac",
"mozilla firefox update",
"mozilla firefox windows",
"mozilla update",
"update firefox",
"update mozilla",
"www.firefox.com",
],
};
// In order to determine whether we should show an update tip, we check for app
// updates, but only once per this time period.
const UPDATE_CHECK_PERIOD_MS = 12 * 60 * 60 * 1000; // 12 hours
/**
* A node in the QueryScorer's phrase tree.
*/
class Node {
constructor(word) {
this.word = word;
this.documents = new Set();
this.childrenByWord = new Map();
}
}
/**
* This class scores a query string against sets of phrases. To refer to a
* single set of phrases, we borrow the term "document" from search engine
* terminology. To use this class, first add your documents with `addDocument`,
* and then call `score` with a query string. `score` returns a sorted array of
* document-score pairs.
*
* The scoring method is fairly simple and is based on Levenshtein edit
* distance. Therefore, lower scores indicate a better match than higher
* scores. In summary, a query matches a phrase if the query starts with the
* phrase. So a query "firefox update foo bar" matches the phrase "firefox
* update" for example. A query matches a document if it matches any phrase in
* the document. The query and phrases are compared word for word, and we allow
* fuzzy matching by computing the Levenshtein edit distance in each comparison.
* The amount of fuzziness allowed is controlled with `distanceThreshold`. If
* the distance in a comparison is greater than this threshold, then the phrase
* does not match the query. The final score for a document is the minimum edit
* distance between its phrases and the query.
*
* As mentioned, `score` returns a sorted array of document-score pairs. It's
* up to you to filter the array to exclude scores above a certain threshold, or
* to take the top scorer, etc.
*/
export class QueryScorer {
/**
* @param {object} options
* Constructor options.
* @param {number} [options.distanceThreshold]
* Edit distances no larger than this value are considered matches.
* @param {Map} [options.variations]
* For convenience, the scorer can augment documents by replacing certain
* words with other words and phrases. This mechanism is called variations.
* This keys of this map are words that should be replaced, and the values
* are the replacement words or phrases. For example, if you add a document
* whose only phrase is "firefox update", normally the scorer will register
* only this single phrase for the document. However, if you pass the value
* `new Map(["firefox", ["fire fox", "fox fire", "foxfire"]])` for this
* parameter, it will register 4 total phrases for the document: "fire fox
* update", "fox fire update", "foxfire update", and the original "firefox
* update".
*/
constructor({ distanceThreshold = 1, variations = new Map() } = {}) {
this._distanceThreshold = distanceThreshold;
this._variations = variations;
this._documents = new Set();
this._rootNode = new Node();
}
/**
* Adds a document to the scorer.
*
* @param {object} doc
* The document.
* @param {string} doc.id
* The document's ID.
* @param {Array} doc.phrases
* The set of phrases in the document. Each phrase should be a string.
*/
addDocument(doc) {
this._documents.add(doc);
for (let phraseStr of doc.phrases) {
// Split the phrase and lowercase the words.
let phrase = phraseStr
.trim()
.split(/\s+/)
.map(word => word.toLocaleLowerCase());
// Build a phrase list that contains the original phrase plus its
// variations, if any.
let phrases = [phrase];
for (let [triggerWord, variations] of this._variations) {
let index = phrase.indexOf(triggerWord);
if (index >= 0) {
for (let variation of variations) {
let variationPhrase = Array.from(phrase);
variationPhrase.splice(index, 1, ...variation.split(/\s+/));
phrases.push(variationPhrase);
}
}
}
// Finally, add the phrases to the phrase tree.
for (let completedPhrase of phrases) {
this._buildPhraseTree(this._rootNode, doc, completedPhrase, 0);
}
}
}
/**
* Scores a query string against the documents in the scorer.
*
* @param {string} queryString
* The query string to score.
* @returns {Array}
* An array of objects: { document, score }. Each element in the array is a
* a document and its score against the query string. The elements are
* ordered by score from low to high. Scores represent edit distance, so
* lower scores are better.
*/
score(queryString) {
let queryWords = queryString
.trim()
.split(/\s+/)
.map(word => word.toLocaleLowerCase());
let minDistanceByDoc = this._traverse({ queryWords });
let results = [];
for (let doc of this._documents) {
let distance = minDistanceByDoc.get(doc);
results.push({
document: doc,
score: distance === undefined ? Infinity : distance,
});
}
results.sort((a, b) => a.score - b.score);
return results;
}
/**
* Builds the phrase tree based on the current documents.
*
* The phrase tree lets us efficiently match queries against phrases. Each
* path through the tree starting from the root and ending at a leaf
* represents a complete phrase in a document (or more than one document, if
* the same phrase is present in multiple documents). Each node in the path
* represents a word in the phrase. To match a query, we start at the root,
* and in the root we look up the query's first word. If the word matches the
* first word of any phrase, then the root will have a child node representing
* that word, and we move on to the child node. Then we look up the query's
* second word in the child node, and so on, until either a lookup fails or we
* reach a leaf node.
*
* @param {Node} node
* The current node being visited.
* @param {object} doc
* The document whose phrases are being added to the tree.
* @param {Array} phrase
* The phrase to add to the tree.
* @param {number} wordIndex
* The index in the phrase of the current word.
*/
_buildPhraseTree(node, doc, phrase, wordIndex) {
if (phrase.length == wordIndex) {
// We're done with this phrase.
return;
}
let word = phrase[wordIndex].toLocaleLowerCase();
let child = node.childrenByWord.get(word);
if (!child) {
child = new Node(word);
node.childrenByWord.set(word, child);
}
child.documents.add(doc);
// Recurse with the next word in the phrase.
this._buildPhraseTree(child, doc, phrase, wordIndex + 1);
}
/**
* Traverses a path in the phrase tree in order to score a query. See
* `_buildPhraseTree` for a description of how this works.
*
* @param {object} options
* Options.
* @param {Array} options.queryWords
* The query being scored, split into words.
* @param {Node} [options.node]
* The node currently being visited.
* @param {Map} [options.minDistanceByDoc]
* Keeps track of the minimum edit distance for each document as the
* traversal continues.
* @param {number} [options.queryWordsIndex]
* The current index in the query words array.
* @param {number} [options.phraseDistance]
* The total edit distance between the query and the path in the tree that's
* been traversed so far.
* @returns {Map} minDistanceByDoc
*/
_traverse({
queryWords,
node = this._rootNode,
minDistanceByDoc = new Map(),
queryWordsIndex = 0,
phraseDistance = 0,
} = {}) {
if (!node.childrenByWord.size) {
// We reached a leaf node. The query has matched a phrase. If the query
// and the phrase have the same number of words, then queryWordsIndex ==
// queryWords.length also. Otherwise the query contains more words than
// the phrase. We still count that as a match.
for (let doc of node.documents) {
minDistanceByDoc.set(
doc,
Math.min(
phraseDistance,
minDistanceByDoc.has(doc) ? minDistanceByDoc.get(doc) : Infinity
)
);
}
return minDistanceByDoc;
}
if (queryWordsIndex == queryWords.length) {
// We exhausted all the words in the query but have not reached a leaf
// node. No match; the query has matched a phrase(s) up to this point,
// but it doesn't have enough words.
return minDistanceByDoc;
}
// Compare each word in the node to the current query word.
let queryWord = queryWords[queryWordsIndex];
for (let [childWord, child] of node.childrenByWord) {
let distance = lazy.NLP.levenshtein(queryWord, childWord);
if (distance <= this._distanceThreshold) {
// The word represented by this child node matches the current query
// word. Recurse into the child node.
this._traverse({
node: child,
queryWords,
queryWordsIndex: queryWordsIndex + 1,
phraseDistance: phraseDistance + distance,
minDistanceByDoc,
});
}
// Else, the path that continues at the child node can't possibly match
// the query, so don't recurse into it.
}
return minDistanceByDoc;
}
}
/**
* Gets appropriate values for each tip's payload.
*
* @param {string} tip a value from the TIPS enum
* @returns {object} Properties to include in the payload
*/
function getPayloadForTip(tip) {
const baseURL = Services.urlFormatter.formatURLPref("app.support.baseURL");
switch (tip) {
case TIPS.CLEAR:
return {
titleL10n: { id: "intervention-clear-data" },
buttons: [{ l10n: { id: "intervention-clear-data-confirm" } }],
helpUrl: baseURL + "delete-browsing-search-download-history-firefox",
};
case TIPS.REFRESH:
return {
titleL10n: { id: "intervention-refresh-profile" },
buttons: [{ l10n: { id: "intervention-refresh-profile-confirm" } }],
helpUrl: baseURL + "refresh-firefox-reset-add-ons-and-settings",
};
case TIPS.UPDATE_ASK:
return {
titleL10n: { id: "intervention-update-ask" },
buttons: [{ l10n: { id: "intervention-update-ask-confirm" } }],
helpUrl: baseURL + "update-firefox-latest-release",
};
case TIPS.UPDATE_REFRESH:
return {
titleL10n: { id: "intervention-update-refresh" },
buttons: [{ l10n: { id: "intervention-update-refresh-confirm" } }],
helpUrl: baseURL + "refresh-firefox-reset-add-ons-and-settings",
};
case TIPS.UPDATE_RESTART:
return {
titleL10n: { id: "intervention-update-restart" },
buttons: [{ l10n: { id: "intervention-update-restart-confirm" } }],
helpUrl: baseURL + "update-firefox-latest-release",
};
case TIPS.UPDATE_WEB:
return {
titleL10n: { id: "intervention-update-web" },
buttons: [{ l10n: { id: "intervention-update-web-confirm" } }],
helpUrl: baseURL + "update-firefox-latest-release",
};
default:
throw new Error("Unknown TIP type.");
}
}
/**
* A provider that returns actionable tip results when the user is performing
* a search related to those actions.
*/
class ProviderInterventions extends UrlbarProvider {
constructor() {
super();
// The tip we should currently show.
this.currentTip = TIPS.NONE;
this.tipsShownInCurrentEngagement = new Set();
// This object is used to match the user's queries to tips.
XPCOMUtils.defineLazyGetter(this, "queryScorer", () => {
let queryScorer = new QueryScorer({
variations: new Map([
// Recognize "fire fox", "fox fire", and "foxfire" as "firefox".
["firefox", ["fire fox", "fox fire", "foxfire"]],
// Recognize "mozila" as "mozilla". This will catch common mispellings
// "mozila", "mozzila", and "mozzilla" (among others) due to the edit
// distance threshold of 1.
["mozilla", ["mozila"]],
]),
});
for (let [id, phrases] of Object.entries(DOCUMENTS)) {
queryScorer.addDocument({ id, phrases });
}
return queryScorer;
});
}
/**
* Enum of the types of intervention tips.
*
* @returns {{ NONE: string; CLEAR: string; REFRESH: string; UPDATE_ASK: string; UPDATE_CHECKING: string; UPDATE_REFRESH: string; UPDATE_RESTART: string; UPDATE_WEB: string; }}
*/
get TIP_TYPE() {
return TIPS;
}
/**
* Unique name for the provider, used by the context to filter on providers.
*
* @returns {string}
*/
get name() {
return "UrlbarProviderInterventions";
}
/**
* The type of the provider, must be one of UrlbarUtils.PROVIDER_TYPE.
*
* @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) {
if (
!queryContext.searchString ||
queryContext.searchString.length > UrlbarUtils.MAX_TEXT_LENGTH ||
lazy.UrlbarTokenizer.REGEXP_LIKE_PROTOCOL.test(
queryContext.searchString
) ||
!EN_LOCALE_MATCH.test(Services.locale.appLocaleAsBCP47) ||
!Services.policies.isAllowed("urlbarinterventions")
) {
return false;
}
this.currentTip = TIPS.NONE;
// Get the scores and the top score.
let docScores = this.queryScorer.score(queryContext.searchString);
let topDocScore = docScores[0];
// Multiple docs may have the top score, so collect them all.
let topDocIDs = new Set();
if (topDocScore.score != Infinity) {
for (let { score, document } of docScores) {
if (score != topDocScore.score) {
break;
}
topDocIDs.add(document.id);
}
}
// Determine the tip to show, if any. If there are multiple top-score docs,
// prefer them in the following order.
if (topDocIDs.has("update")) {
this._setCurrentTipFromAppUpdaterStatus();
} else if (topDocIDs.has("clear")) {
let window = lazy.BrowserWindowTracker.getTopWindow();
if (!lazy.PrivateBrowsingUtils.isWindowPrivate(window)) {
this.currentTip = TIPS.CLEAR;
}
} else if (topDocIDs.has("refresh")) {
// Note that the "update" case can set currentTip to TIPS.REFRESH too.
this.currentTip = TIPS.REFRESH;
}
return (
this.currentTip != TIPS.NONE &&
(this.currentTip != TIPS.REFRESH ||
Services.policies.isAllowed("profileRefresh"))
);
}
async _setCurrentTipFromAppUpdaterStatus(waitForCheck) {
// The update tips depend on the app's update status, so check for updates
// now (if we haven't already checked within the update-check period). If
// we're running in an xpcshell test, then checkForBrowserUpdate's attempt
// to use appUpdater will throw an exception because it won't be available.
// In that case, return false to disable the provider.
//
// This causes synchronous IO within the updater the first time it's called
// (at least) so be careful not to do it the first time the urlbar is used.
try {
this.checkForBrowserUpdate();
} catch (ex) {
return;
}
// There are several update tips. Figure out which one to show.
switch (lazy.appUpdater.status) {
case lazy.AppUpdater.STATUS.READY_FOR_RESTART:
// Prompt the user to restart.
this.currentTip = TIPS.UPDATE_RESTART;
break;
case lazy.AppUpdater.STATUS.DOWNLOAD_AND_INSTALL:
// There's an update available, but the user's pref says we should ask
// them to download and apply it.
this.currentTip = TIPS.UPDATE_ASK;
break;
case lazy.AppUpdater.STATUS.NO_UPDATES_FOUND:
// We show a special refresh tip when the browser is up to date.
this.currentTip = TIPS.UPDATE_REFRESH;
break;
case lazy.AppUpdater.STATUS.CHECKING:
// This will be the case the first time we check. See startQuery for
// how this special tip is handled.
this.currentTip = TIPS.UPDATE_CHECKING;
break;
case lazy.AppUpdater.STATUS.NO_UPDATER:
case lazy.AppUpdater.STATUS.UPDATE_DISABLED_BY_POLICY:
// If the updater is disabled at build time or at runtime, either by
// policy or because we're in a package, do not select any update tips.
this.currentTip = TIPS.NONE;
break;
default:
// Give up and ask the user to download the latest version from the
// web. We default to this case when the update is still downloading
// because an update doesn't actually occur if the user were to
// restart the browser. See bug 1625241.
this.currentTip = TIPS.UPDATE_WEB;
break;
}
}
/**
* Starts querying.
*
* @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 instance = this.queryInstance;
// TIPS.UPDATE_CHECKING is special, and we never actually show a tip that
// reflects a "checking" status. Instead it's handled like this. We call
// appUpdater.check() to start an update check. If we haven't called it
// before, then when it returns, appUpdater.status will be
// AppUpdater.STATUS.CHECKING, and it will remain CHECKING until the check
// finishes. We can add a listener to appUpdater to be notified when the
// check finishes. We don't want to wait for it to finish in isActive
// because that would block other providers from adding their results, so
// instead we wait here in startQuery. The results from other providers
// will be added while we're waiting. When the check finishes, we call
// addCallback and add our result. It doesn't matter how long the check
// takes because if another query starts, the view is closed, or the user
// changes the selection, the query will be canceled.
if (this.currentTip == TIPS.UPDATE_CHECKING) {
// First check the status because it may have changed between the time
// isActive was called and now.
this._setCurrentTipFromAppUpdaterStatus();
if (this.currentTip == TIPS.UPDATE_CHECKING) {
// The updater is still checking, so wait for it to finish.
await new Promise(resolve => {
this._appUpdaterListener = () => {
lazy.appUpdater.removeListener(this._appUpdaterListener);
delete this._appUpdaterListener;
resolve();
};
lazy.appUpdater.addListener(this._appUpdaterListener);
});
if (instance != this.queryInstance) {
// The query was canceled before the check finished.
return;
}
// Finally, set the tip from the updater status. The updater should no
// longer be checking, but guard against it just in case by returning
// early.
this._setCurrentTipFromAppUpdaterStatus();
if (this.currentTip == TIPS.UPDATE_CHECKING) {
return;
}
}
}
// At this point, this.currentTip != TIPS.UPDATE_CHECKING because we
// returned early above if it was.
let result = new lazy.UrlbarResult(
UrlbarUtils.RESULT_TYPE.TIP,
UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
{
...getPayloadForTip(this.currentTip),
type: this.currentTip,
icon: UrlbarUtils.ICON.TIP,
helpL10n: {
id: lazy.UrlbarPrefs.get("resultMenu")
? "urlbar-result-menu-tip-get-help"
: "urlbar-tip-help-icon",
},
}
);
result.suggestedIndex = 1;
this.tipsShownInCurrentEngagement.add(this.currentTip);
addCallback(this, result);
}
/**
* Cancels a running query,
*
* @param {UrlbarQueryContext} queryContext the query context object to cancel
* query for.
*/
cancelQuery(queryContext) {
// If we're waiting for appUpdater to finish its update check,
// this._appUpdaterListener will be defined. We can stop listening now.
if (this._appUpdaterListener) {
lazy.appUpdater.removeListener(this._appUpdaterListener);
delete this._appUpdaterListener;
}
}
#pickResult(result, window) {
let tip = result.payload.type;
// Do the tip action.
switch (tip) {
case TIPS.CLEAR:
openClearHistoryDialog(window);
break;
case TIPS.REFRESH:
case TIPS.UPDATE_REFRESH:
resetBrowser(window);
break;
case TIPS.UPDATE_ASK:
installBrowserUpdateAndRestart();
break;
case TIPS.UPDATE_RESTART:
restartBrowser();
break;
case TIPS.UPDATE_WEB:
window.gBrowser.selectedTab = window.gBrowser.addWebTab(
"https://www.mozilla.org/firefox/new/"
);
break;
}
}
onEngagement(isPrivate, state, queryContext, details, window) {
let { result } = details;
// `selType` is "tip" when the tip's main button is picked. Ignore clicks on
// the help command ("tiphelp"), which is handled by UrlbarInput since we
// set `helpUrl` on the result payload. Currently there aren't any other
// buttons or commands but this will ignore clicks on them too.
if (result?.providerName == this.name && details.selType == "tip") {
this.#pickResult(result, window);
}
if (["engagement", "abandonment"].includes(state)) {
for (let tip of this.tipsShownInCurrentEngagement) {
Services.telemetry.keyedScalarAdd("urlbar.tips", `${tip}-shown`, 1);
}
}
this.tipsShownInCurrentEngagement.clear();
}
/**
* Checks for app updates.
*
* @param {boolean} force If false, this only checks for updates if we haven't
* already checked within the update-check period. If true, we check
* regardless.
*/
checkForBrowserUpdate(force = false) {
if (
force ||
!this._lastUpdateCheckTime ||
Date.now() - this._lastUpdateCheckTime >= UPDATE_CHECK_PERIOD_MS
) {
this._lastUpdateCheckTime = Date.now();
lazy.appUpdater.check();
}
}
/**
* Resets the provider's app updater state by making a new app updater. This
* is intended to be used by tests.
*/
resetAppUpdater() {
// Reset only if the object has already been initialized.
if (!Object.getOwnPropertyDescriptor(lazy, "appUpdater").get) {
lazy.appUpdater = new lazy.AppUpdater();
}
}
}
export var UrlbarProviderInterventions = new ProviderInterventions();
/**
* Tip callbacks follow.
*/
function installBrowserUpdateAndRestart() {
if (lazy.appUpdater.status != lazy.AppUpdater.STATUS.DOWNLOAD_AND_INSTALL) {
return Promise.resolve();
}
return new Promise(resolve => {
let listener = () => {
// Once we call allowUpdateDownload, there are two possible end
// states: DOWNLOAD_FAILED and READY_FOR_RESTART.
if (
lazy.appUpdater.status != lazy.AppUpdater.STATUS.READY_FOR_RESTART &&
lazy.appUpdater.status != lazy.AppUpdater.STATUS.DOWNLOAD_FAILED
) {
return;
}
lazy.appUpdater.removeListener(listener);
if (lazy.appUpdater.status == lazy.AppUpdater.STATUS.READY_FOR_RESTART) {
restartBrowser();
}
resolve();
};
lazy.appUpdater.addListener(listener);
lazy.appUpdater.allowUpdateDownload();
});
}
function openClearHistoryDialog(window) {
// The behaviour of the Clear Recent History dialog in PBM does
// not have the expected effect (bug 463607).
if (lazy.PrivateBrowsingUtils.isWindowPrivate(window)) {
return;
}
lazy.Sanitizer.showUI(window);
}
function restartBrowser() {
// Notify all windows that an application quit has been requested.
let cancelQuit = Cc["@mozilla.org/supports-PRBool;1"].createInstance(
Ci.nsISupportsPRBool
);
Services.obs.notifyObservers(
cancelQuit,
"quit-application-requested",
"restart"
);
// Something aborted the quit process.
if (cancelQuit.data) {
return;
}
// If already in safe mode restart in safe mode.
if (Services.appinfo.inSafeMode) {
Services.startup.restartInSafeMode(Ci.nsIAppStartup.eAttemptQuit);
} else {
Services.startup.quit(
Ci.nsIAppStartup.eAttemptQuit | Ci.nsIAppStartup.eRestart
);
}
}
function resetBrowser(window) {
if (!lazy.ResetProfile.resetSupported()) {
return;
}
lazy.ResetProfile.openConfirmationDialog(window);
}
|