This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathBrowserUsageTelemetry.jsm
More file actions
898 lines (797 loc) · 27.2 KB
/
Copy pathBrowserUsageTelemetry.jsm
File metadata and controls
898 lines (797 loc) · 27.2 KB
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
/* 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/. */
"use strict";
var EXPORTED_SYMBOLS = [
"BrowserUsageTelemetry",
"getUniqueDomainsVisitedInPast24Hours",
"URICountListener",
"URLBAR_SELECTED_RESULT_TYPES",
"URLBAR_SELECTED_RESULT_METHODS",
"MINIMUM_TAB_COUNT_INTERVAL_MS",
];
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
SearchTelemetry: "resource:///modules/SearchTelemetry.jsm",
Services: "resource://gre/modules/Services.jsm",
setTimeout: "resource://gre/modules/Timer.jsm",
});
// This pref is in seconds!
XPCOMUtils.defineLazyPreferenceGetter(
this,
"gRecentVisitedOriginsExpiry",
"browser.engagement.recent_visited_origins.expiry"
);
// The upper bound for the count of the visited unique domain names.
const MAX_UNIQUE_VISITED_DOMAINS = 100;
// Observed topic names.
const TAB_RESTORING_TOPIC = "SSTabRestoring";
const TELEMETRY_SUBSESSIONSPLIT_TOPIC =
"internal-telemetry-after-subsession-split";
const DOMWINDOW_OPENED_TOPIC = "domwindowopened";
const AUTOCOMPLETE_ENTER_TEXT_TOPIC = "autocomplete-did-enter-text";
// Probe names.
const MAX_TAB_COUNT_SCALAR_NAME = "browser.engagement.max_concurrent_tab_count";
const MAX_WINDOW_COUNT_SCALAR_NAME =
"browser.engagement.max_concurrent_window_count";
const TAB_OPEN_EVENT_COUNT_SCALAR_NAME =
"browser.engagement.tab_open_event_count";
const MAX_TAB_PINNED_COUNT_SCALAR_NAME =
"browser.engagement.max_concurrent_tab_pinned_count";
const TAB_PINNED_EVENT_COUNT_SCALAR_NAME =
"browser.engagement.tab_pinned_event_count";
const WINDOW_OPEN_EVENT_COUNT_SCALAR_NAME =
"browser.engagement.window_open_event_count";
const UNIQUE_DOMAINS_COUNT_SCALAR_NAME =
"browser.engagement.unique_domains_count";
const TOTAL_URI_COUNT_SCALAR_NAME = "browser.engagement.total_uri_count";
const UNFILTERED_URI_COUNT_SCALAR_NAME =
"browser.engagement.unfiltered_uri_count";
// A list of known search origins.
const KNOWN_SEARCH_SOURCES = [
"abouthome",
"contextmenu",
"newtab",
"searchbar",
"urlbar",
"webextension",
];
const KNOWN_ONEOFF_SOURCES = [
"oneoff-urlbar",
"oneoff-searchbar",
"unknown", // Edge case: this is the searchbar (see bug 1195733 comment 7).
];
/**
* The buckets used for logging telemetry to the FX_URLBAR_SELECTED_RESULT_TYPE
* histogram.
*/
const URLBAR_SELECTED_RESULT_TYPES = {
autofill: 0,
bookmark: 1,
history: 2,
keyword: 3,
searchengine: 4,
searchsuggestion: 5,
switchtab: 6,
tag: 7,
visiturl: 8,
remotetab: 9,
extension: 10,
"preloaded-top-site": 11,
tip: 12,
};
/**
* This maps the categories used by the FX_URLBAR_SELECTED_RESULT_METHOD and
* FX_SEARCHBAR_SELECTED_RESULT_METHOD histograms to their indexes in the
* `labels` array. This only needs to be used by tests that need to map from
* category names to indexes in histogram snapshots. Actual app code can use
* these category names directly when they add to a histogram.
*/
const URLBAR_SELECTED_RESULT_METHODS = {
enter: 0,
enterSelection: 1,
click: 2,
arrowEnterSelection: 3,
tabEnterSelection: 4,
rightClickEnter: 5,
};
const MINIMUM_TAB_COUNT_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes, in ms
function getOpenTabsAndWinsCounts() {
let tabCount = 0;
let winCount = 0;
for (let win of Services.wm.getEnumerator("navigator:browser")) {
winCount++;
tabCount += win.gBrowser.tabs.length;
}
return { tabCount, winCount };
}
function getTabCount() {
return getOpenTabsAndWinsCounts().tabCount;
}
function getPinnedTabsCount() {
let pinnedTabs = 0;
for (let win of Services.wm.getEnumerator("navigator:browser")) {
pinnedTabs += [...win.ownerGlobal.gBrowser.tabs].filter(t => t.pinned)
.length;
}
return pinnedTabs;
}
function getSearchEngineId(engine) {
if (engine) {
if (engine.identifier) {
return engine.identifier;
}
if (engine.name) {
return "other-" + engine.name;
}
}
return "other";
}
function shouldRecordSearchCount(tabbrowser) {
return (
!PrivateBrowsingUtils.isWindowPrivate(tabbrowser.ownerGlobal) ||
!Services.prefs.getBoolPref("browser.engagement.search_counts.pbm", false)
);
}
let URICountListener = {
// A set containing the visited domains, see bug 1271310.
_domainSet: new Set(),
// A set containing the visited origins during the last 24 hours (similar to domains, but not quite the same)
_domain24hrSet: new Set(),
// A map to keep track of the URIs loaded from the restored tabs.
_restoredURIsMap: new WeakMap(),
isHttpURI(uri) {
// Only consider http(s) schemas.
return uri.schemeIs("http") || uri.schemeIs("https");
},
addRestoredURI(browser, uri) {
if (!this.isHttpURI(uri)) {
return;
}
this._restoredURIsMap.set(browser, uri.spec);
},
onLocationChange(browser, webProgress, request, uri, flags) {
// By default, assume we no longer need to track this tab.
SearchTelemetry.stopTrackingBrowser(browser);
// Don't count this URI if it's an error page.
if (flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_ERROR_PAGE) {
return;
}
// We only care about top level loads.
if (!webProgress.isTopLevel) {
return;
}
// The SessionStore sets the URI of a tab first, firing onLocationChange the
// first time, then manages content loading using its scheduler. Once content
// loads, we will hit onLocationChange again.
// We can catch the first case by checking for null requests: be advised that
// this can also happen when navigating page fragments, so account for it.
if (
!request &&
!(flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT)
) {
return;
}
// Don't include URI and domain counts when in private mode.
let shouldCountURI =
!PrivateBrowsingUtils.isWindowPrivate(browser.ownerGlobal) ||
Services.prefs.getBoolPref(
"browser.engagement.total_uri_count.pbm",
false
);
// Track URI loads, even if they're not http(s).
let uriSpec = null;
try {
uriSpec = uri.spec;
} catch (e) {
// If we have troubles parsing the spec, still count this as
// an unfiltered URI.
if (shouldCountURI) {
Services.telemetry.scalarAdd(UNFILTERED_URI_COUNT_SCALAR_NAME, 1);
}
return;
}
// Don't count about:blank and similar pages, as they would artificially
// inflate the counts.
if (browser.ownerGlobal.gInitialPages.includes(uriSpec)) {
return;
}
// If the URI we're loading is in the _restoredURIsMap, then it comes from a
// restored tab. If so, let's skip it and remove it from the map as we want to
// count page refreshes.
if (this._restoredURIsMap.get(browser) === uriSpec) {
this._restoredURIsMap.delete(browser);
return;
}
// The URI wasn't from a restored tab. Count it among the unfiltered URIs.
// If this is an http(s) URI, this also gets counted by the "total_uri_count"
// probe.
if (shouldCountURI) {
Services.telemetry.scalarAdd(UNFILTERED_URI_COUNT_SCALAR_NAME, 1);
}
if (!this.isHttpURI(uri)) {
return;
}
if (
shouldRecordSearchCount(browser.getTabBrowser()) &&
!(flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT)
) {
SearchTelemetry.updateTrackingStatus(browser, uriSpec);
}
if (!shouldCountURI) {
return;
}
// Update the URI counts.
Services.telemetry.scalarAdd(TOTAL_URI_COUNT_SCALAR_NAME, 1);
// Update tab count
BrowserUsageTelemetry._recordTabCount();
// Unique domains should be aggregated by (eTLD + 1): x.test.com and y.test.com
// are counted once as test.com.
let baseDomain;
try {
// Even if only considering http(s) URIs, |getBaseDomain| could still throw
// due to the URI containing invalid characters or the domain actually being
// an ipv4 or ipv6 address.
baseDomain = Services.eTLD.getBaseDomain(uri);
} catch (e) {
return;
}
// We only want to count the unique domains up to MAX_UNIQUE_VISITED_DOMAINS.
if (this._domainSet.size < MAX_UNIQUE_VISITED_DOMAINS) {
this._domainSet.add(baseDomain);
Services.telemetry.scalarSet(
UNIQUE_DOMAINS_COUNT_SCALAR_NAME,
this._domainSet.size
);
}
this._domain24hrSet.add(baseDomain);
if (gRecentVisitedOriginsExpiry) {
setTimeout(() => {
this._domain24hrSet.delete(baseDomain);
}, gRecentVisitedOriginsExpiry * 1000);
}
},
/**
* Reset the counts. This should be called when breaking a session in Telemetry.
*/
reset() {
this._domainSet.clear();
},
/**
* Returns the number of unique domains visited in this session during the
* last 24 hours.
*/
get uniqueDomainsVisitedInPast24Hours() {
return this._domain24hrSet.size;
},
/**
* Resets the number of unique domains visited in this session.
*/
resetUniqueDomainsVisitedInPast24Hours() {
this._domain24hrSet.clear();
},
QueryInterface: ChromeUtils.generateQI([
Ci.nsIWebProgressListener,
Ci.nsISupportsWeakReference,
]),
};
let urlbarListener = {
// This is needed for recordUrlbarSelectedResultMethod().
selectedIndex: -1,
init() {
Services.obs.addObserver(this, AUTOCOMPLETE_ENTER_TEXT_TOPIC, true);
},
uninit() {
Services.obs.removeObserver(this, AUTOCOMPLETE_ENTER_TEXT_TOPIC);
},
observe(subject, topic, data) {
switch (topic) {
case AUTOCOMPLETE_ENTER_TEXT_TOPIC:
this._handleURLBarTelemetry(
subject.QueryInterface(Ci.nsIAutoCompleteInput)
);
break;
}
},
/**
* Used to log telemetry when the user enters text in the urlbar.
*
* @param {nsIAutoCompleteInput} input The autocomplete element where the
* text was entered.
*/
_handleURLBarTelemetry(input) {
if (!input || input.id != "urlbar") {
return;
}
if (input.inPrivateContext || input.popup.selectedIndex < 0) {
this.selectedIndex = -1;
return;
}
// Except for the history popup, the urlbar always has a selection. The
// first result at index 0 is the "heuristic" result that indicates what
// will happen when you press the Enter key. Treat it as no selection.
this.selectedIndex =
input.popup.selectedIndex > 0 || !input.popup._isFirstResultHeuristic
? input.popup.selectedIndex
: -1;
let controller = input.popup.view.QueryInterface(
Ci.nsIAutoCompleteController
);
let idx = input.popup.selectedIndex;
let value = controller.getValueAt(idx);
let action = input._parseActionUrl(value);
let actionType;
if (action) {
actionType =
action.type == "searchengine" && action.params.searchSuggestion
? "searchsuggestion"
: action.type;
}
if (!actionType) {
let styles = new Set(controller.getStyleAt(idx).split(/\s+/));
let style = ["preloaded-top-site", "autofill", "tag", "bookmark"].find(
s => styles.has(s)
);
actionType = style || "history";
}
Services.telemetry
.getHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX")
.add(idx);
// You can add values but don't change any of the existing values.
// Otherwise you'll break our data.
if (actionType in URLBAR_SELECTED_RESULT_TYPES) {
Services.telemetry
.getHistogramById("FX_URLBAR_SELECTED_RESULT_TYPE")
.add(URLBAR_SELECTED_RESULT_TYPES[actionType]);
Services.telemetry
.getKeyedHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX_BY_TYPE")
.add(actionType, idx);
} else {
Cu.reportError(
"Unknown FX_URLBAR_SELECTED_RESULT_TYPE type: " + actionType
);
}
},
QueryInterface: ChromeUtils.generateQI([
Ci.nsIObserver,
Ci.nsISupportsWeakReference,
]),
};
let BrowserUsageTelemetry = {
_inited: false,
init() {
this._lastRecordTabCount = 0;
urlbarListener.init();
this._setupAfterRestore();
this._inited = true;
},
/**
* Handle subsession splits in the parent process.
*/
afterSubsessionSplit() {
// Scalars just got cleared due to a subsession split. We need to set the maximum
// concurrent tab and window counts so that they reflect the correct value for the
// new subsession.
const counts = getOpenTabsAndWinsCounts();
Services.telemetry.scalarSetMaximum(
MAX_TAB_COUNT_SCALAR_NAME,
counts.tabCount
);
Services.telemetry.scalarSetMaximum(
MAX_WINDOW_COUNT_SCALAR_NAME,
counts.winCount
);
// Reset the URI counter.
URICountListener.reset();
},
QueryInterface: ChromeUtils.generateQI([
Ci.nsIObserver,
Ci.nsISupportsWeakReference,
]),
uninit() {
if (!this._inited) {
return;
}
Services.obs.removeObserver(this, DOMWINDOW_OPENED_TOPIC);
Services.obs.removeObserver(this, TELEMETRY_SUBSESSIONSPLIT_TOPIC);
urlbarListener.uninit();
},
observe(subject, topic, data) {
switch (topic) {
case DOMWINDOW_OPENED_TOPIC:
this._onWindowOpen(subject);
break;
case TELEMETRY_SUBSESSIONSPLIT_TOPIC:
this.afterSubsessionSplit();
break;
}
},
handleEvent(event) {
switch (event.type) {
case "TabOpen":
this._onTabOpen();
break;
case "TabPinned":
this._onTabPinned();
break;
case "unload":
this._unregisterWindow(event.target);
break;
case TAB_RESTORING_TOPIC:
// We're restoring a new tab from a previous or crashed session.
// We don't want to track the URIs from these tabs, so let
// |URICountListener| know about them.
let browser = event.target.linkedBrowser;
URICountListener.addRestoredURI(browser, browser.currentURI);
break;
}
},
/**
* The main entry point for recording search related Telemetry. This includes
* search counts and engagement measurements.
*
* Telemetry records only search counts per engine and action origin, but
* nothing pertaining to the search contents themselves.
*
* @param {tabbrowser} tabbrowser
* The tabbrowser where the search was loaded.
* @param {nsISearchEngine} engine
* The engine handling the search.
* @param {String} source
* Where the search originated from. See KNOWN_SEARCH_SOURCES for allowed
* values.
* @param {Object} [details] Options object.
* @param {Boolean} [details.isOneOff=false]
* true if this event was generated by a one-off search.
* @param {Boolean} [details.isSuggestion=false]
* true if this event was generated by a suggested search.
* @param {String} [details.alias=null]
* The search engine alias used in the search, if any.
* @param {Object} [details.type=null]
* The object describing the event that triggered the search.
* @throws if source is not in the known sources list.
*/
recordSearch(tabbrowser, engine, source, details = {}) {
if (!shouldRecordSearchCount(tabbrowser)) {
return;
}
const countIdPrefix = getSearchEngineId(engine) + ".";
const countIdSource = countIdPrefix + source;
let histogram = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS");
if (details.isOneOff) {
if (!KNOWN_ONEOFF_SOURCES.includes(source)) {
// Silently drop the error if this bogus call
// came from 'urlbar' or 'searchbar'. They're
// calling |recordSearch| twice from two different
// code paths because they want to record the search
// in SEARCH_COUNTS.
if (["urlbar", "searchbar"].includes(source)) {
histogram.add(countIdSource);
return;
}
throw new Error("Unknown source for one-off search: " + source);
}
} else {
if (!KNOWN_SEARCH_SOURCES.includes(source)) {
throw new Error("Unknown source for search: " + source);
}
if (
details.alias &&
engine.wrappedJSObject._internalAliases.includes(details.alias)
) {
// This search uses an internal @search keyword. Record the source as
// "alias", not "urlbar".
histogram.add(countIdPrefix + "alias");
} else {
histogram.add(countIdSource);
}
}
// Dispatch the search signal to other handlers.
this._handleSearchAction(engine, source, details);
},
_recordSearch(engine, source, action = null) {
let scalarKey = action ? "search_" + action : "search";
Services.telemetry.keyedScalarAdd(
"browser.engagement.navigation." + source,
scalarKey,
1
);
Services.telemetry.recordEvent("navigation", "search", source, action, {
engine: getSearchEngineId(engine),
});
},
_handleSearchAction(engine, source, details) {
switch (source) {
case "urlbar":
case "oneoff-urlbar":
case "searchbar":
case "oneoff-searchbar":
case "unknown": // Edge case: this is the searchbar (see bug 1195733 comment 7).
this._handleSearchAndUrlbar(engine, source, details);
break;
case "abouthome":
this._recordSearch(engine, "about_home", "enter");
break;
case "newtab":
this._recordSearch(engine, "about_newtab", "enter");
break;
case "contextmenu":
case "webextension":
this._recordSearch(engine, source);
break;
}
},
/**
* This function handles the "urlbar", "urlbar-oneoff", "searchbar" and
* "searchbar-oneoff" sources.
*/
_handleSearchAndUrlbar(engine, source, details) {
// We want "urlbar" and "urlbar-oneoff" (and similar cases) to go in the same
// scalar, but in a different key.
// When using one-offs in the searchbar we get an "unknown" source. See bug
// 1195733 comment 7 for the context. Fix-up the label here.
const sourceName =
source === "unknown" ? "searchbar" : source.replace("oneoff-", "");
const isOneOff = !!details.isOneOff;
if (isOneOff) {
// We will receive a signal from the "urlbar"/"searchbar" even when the
// search came from "oneoff-urlbar". That's because both signals
// are propagated from search.xml. Skip it if that's the case.
// Moreover, we skip the "unknown" source that comes from the searchbar
// when performing searches from the default search engine. See bug 1195733
// comment 7 for context.
if (["urlbar", "searchbar", "unknown"].includes(source)) {
return;
}
// If that's a legit one-off search signal, record it using the relative key.
this._recordSearch(engine, sourceName, "oneoff");
return;
}
// The search was not a one-off. It was a search with the default search engine.
if (details.isSuggestion) {
// It came from a suggested search, so count it as such.
this._recordSearch(engine, sourceName, "suggestion");
return;
} else if (details.alias) {
// This one came from a search that used an alias.
this._recordSearch(engine, sourceName, "alias");
return;
}
// The search signal was generated by typing something and pressing enter.
this._recordSearch(engine, sourceName, "enter");
},
/**
* Records the method by which the user selected a urlbar result for the
* legacy urlbar.
*
* @param {Event} event
* The event that triggered the selection.
* @param {string} userSelectionBehavior
* How the user cycled through results before picking the current match.
* Could be one of "tab", "arrow" or "none".
*/
recordLegacyUrlbarSelectedResultMethod(
event,
userSelectionBehavior = "none"
) {
// The reason this method relies on urlbarListener instead of having the
// caller pass in an index is that by the time the urlbar handles a
// selection, the selection in its popup has been cleared, so it's not easy
// to tell which popup index was selected. Fortunately this file already
// has urlbarListener, which gets notified of selections in the urlbar
// before the popup selection is cleared, so just use that.
this._recordUrlOrSearchbarSelectedResultMethod(
event,
urlbarListener.selectedIndex,
"FX_URLBAR_SELECTED_RESULT_METHOD",
userSelectionBehavior
);
},
/**
* Records the method by which the user selected a urlbar result for the
* legacy urlbar.
*
* @param {Event} event
* The event that triggered the selection.
* @param {number} index
* The index that the user chose in the popup, or -1 if there wasn't a
* selection.
* @param {string} userSelectionBehavior
* How the user cycled through results before picking the current match.
* Could be one of "tab", "arrow" or "none".
*/
recordUrlbarSelectedResultMethod(
event,
index,
userSelectionBehavior = "none"
) {
// The reason this method relies on urlbarListener instead of having the
// caller pass in an index is that by the time the urlbar handles a
// selection, the selection in its popup has been cleared, so it's not easy
// to tell which popup index was selected. Fortunately this file already
// has urlbarListener, which gets notified of selections in the urlbar
// before the popup selection is cleared, so just use that.
this._recordUrlOrSearchbarSelectedResultMethod(
event,
index,
"FX_URLBAR_SELECTED_RESULT_METHOD",
userSelectionBehavior
);
},
/**
* Records the method by which the user selected a searchbar result.
*
* @param {Event} event
* The event that triggered the selection.
* @param {number} highlightedIndex
* The index that the user chose in the popup, or -1 if there wasn't a
* selection.
*/
recordSearchbarSelectedResultMethod(event, highlightedIndex) {
this._recordUrlOrSearchbarSelectedResultMethod(
event,
highlightedIndex,
"FX_SEARCHBAR_SELECTED_RESULT_METHOD",
"none"
);
},
_recordUrlOrSearchbarSelectedResultMethod(
event,
highlightedIndex,
histogramID,
userSelectionBehavior
) {
let histogram = Services.telemetry.getHistogramById(histogramID);
// command events are from the one-off context menu. Treat them as clicks.
// Note that we don't care about MouseEvent subclasses here, since
// those are not clicks.
let isClick =
event &&
(ChromeUtils.getClassName(event) == "MouseEvent" ||
event.type == "command");
let category;
if (isClick) {
category = "click";
} else if (highlightedIndex >= 0) {
switch (userSelectionBehavior) {
case "tab":
category = "tabEnterSelection";
break;
case "arrow":
category = "arrowEnterSelection";
break;
case "rightClick":
// Selected by right mouse button.
category = "rightClickEnter";
break;
default:
category = "enterSelection";
}
} else {
category = "enter";
}
histogram.add(category);
},
/**
* This gets called shortly after the SessionStore has finished restoring
* windows and tabs. It counts the open tabs and adds listeners to all the
* windows.
*/
_setupAfterRestore() {
// Make sure to catch new chrome windows and subsession splits.
Services.obs.addObserver(this, DOMWINDOW_OPENED_TOPIC, true);
Services.obs.addObserver(this, TELEMETRY_SUBSESSIONSPLIT_TOPIC, true);
// Attach the tabopen handlers to the existing Windows.
for (let win of Services.wm.getEnumerator("navigator:browser")) {
this._registerWindow(win);
}
// Get the initial tab and windows max counts.
const counts = getOpenTabsAndWinsCounts();
Services.telemetry.scalarSetMaximum(
MAX_TAB_COUNT_SCALAR_NAME,
counts.tabCount
);
Services.telemetry.scalarSetMaximum(
MAX_WINDOW_COUNT_SCALAR_NAME,
counts.winCount
);
},
/**
* Adds listeners to a single chrome window.
*/
_registerWindow(win) {
win.addEventListener("unload", this);
win.addEventListener("TabOpen", this, true);
win.addEventListener("TabPinned", this, true);
win.gBrowser.tabContainer.addEventListener(TAB_RESTORING_TOPIC, this);
win.gBrowser.addTabsProgressListener(URICountListener);
},
/**
* Removes listeners from a single chrome window.
*/
_unregisterWindow(win) {
win.removeEventListener("unload", this);
win.removeEventListener("TabOpen", this, true);
win.removeEventListener("TabPinned", this, true);
win.defaultView.gBrowser.tabContainer.removeEventListener(
TAB_RESTORING_TOPIC,
this
);
win.defaultView.gBrowser.removeTabsProgressListener(URICountListener);
},
/**
* Updates the tab counts.
* @param {Number} [newTabCount=0] The count of the opened tabs across all windows. This
* is computed manually if not provided.
*/
_onTabOpen(tabCount = 0) {
// Use the provided tab count if available. Otherwise, go on and compute it.
tabCount = tabCount || getOpenTabsAndWinsCounts().tabCount;
// Update the "tab opened" count and its maximum.
Services.telemetry.scalarAdd(TAB_OPEN_EVENT_COUNT_SCALAR_NAME, 1);
Services.telemetry.scalarSetMaximum(MAX_TAB_COUNT_SCALAR_NAME, tabCount);
this._recordTabCount(tabCount);
},
_onTabPinned(target) {
const pinnedTabs = getPinnedTabsCount();
// Update the "tab pinned" count and its maximum.
Services.telemetry.scalarAdd(TAB_PINNED_EVENT_COUNT_SCALAR_NAME, 1);
Services.telemetry.scalarSetMaximum(
MAX_TAB_PINNED_COUNT_SCALAR_NAME,
pinnedTabs
);
},
/**
* Tracks the window count and registers the listeners for the tab count.
* @param{Object} win The window object.
*/
_onWindowOpen(win) {
// Make sure to have a |nsIDOMWindow|.
if (!(win instanceof Ci.nsIDOMWindow)) {
return;
}
let onLoad = () => {
win.removeEventListener("load", onLoad);
// Ignore non browser windows.
if (
win.document.documentElement.getAttribute("windowtype") !=
"navigator:browser"
) {
return;
}
this._registerWindow(win);
// Track the window open event and check the maximum.
const counts = getOpenTabsAndWinsCounts();
Services.telemetry.scalarAdd(WINDOW_OPEN_EVENT_COUNT_SCALAR_NAME, 1);
Services.telemetry.scalarSetMaximum(
MAX_WINDOW_COUNT_SCALAR_NAME,
counts.winCount
);
// We won't receive the "TabOpen" event for the first tab within a new window.
// Account for that.
this._onTabOpen(counts.tabCount);
};
win.addEventListener("load", onLoad);
},
_recordTabCount(tabCount) {
let currentTime = Date.now();
if (
currentTime >
this._lastRecordTabCount + MINIMUM_TAB_COUNT_INTERVAL_MS
) {
if (tabCount === undefined) {
tabCount = getTabCount();
}
Services.telemetry.getHistogramById("TAB_COUNT").add(tabCount);
this._lastRecordTabCount = currentTime;
}
},
};
// Used by nsIBrowserUsage
function getUniqueDomainsVisitedInPast24Hours() {
return URICountListener.uniqueDomainsVisitedInPast24Hours;
}