1 /* -*- mode: js; indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 export var BrowserTelemetryUtils = {
7 recordSiteOriginTelemetry(aWindows, aIsGeckoView) {
8 Services.tm.idleDispatchToMainThread(() => {
9 this._recordSiteOriginTelemetry(aWindows, aIsGeckoView);
13 computeSiteOriginCount(aWindows, aIsGeckoView) {
14 // Geckoview and Desktop work differently. On desktop, aBrowser objects
15 // holds an array of tabs which we can use to get the <browser> objects.
16 // In Geckoview, it is apps' responsibility to keep track of the tabs, so
17 // there isn't an easy way for us to get the tabs.
20 // To get all active windows; Each tab has its own window
23 for (const win of aWindows) {
24 tabs = tabs.concat(win.gBrowser.tabs);
30 for (const tab of tabs) {
33 browser = tab.browser;
35 browser = tab.linkedBrowser;
38 if (browser.browsingContext) {
39 // This is the top level browsingContext
40 topLevelBCs.push(browser.browsingContext);
44 return CanonicalBrowsingContext.countSiteOrigins(topLevelBCs);
47 _recordSiteOriginTelemetry(aWindows, aIsGeckoView) {
48 let currentTime = Date.now();
50 // default is 5 minutes
51 if (!this.min_interval) {
52 this.min_interval = Services.prefs.getIntPref(
53 "telemetry.number_of_site_origin.min_interval",
58 let originCount = this.computeSiteOriginCount(aWindows, aIsGeckoView);
60 // Discard the first load because most of the time the first load only has 1
61 // tab and 1 window open, so it is useless to report it.
62 if (!this._lastRecordSiteOrigin) {
63 this._lastRecordSiteOrigin = currentTime;
64 } else if (currentTime >= this._lastRecordSiteOrigin + this.min_interval) {
65 this._lastRecordSiteOrigin = currentTime;
67 Glean.geckoview.documentSiteOrigins.accumulateSingleSample(originCount);