Bug 1888590 - Mark some subtests on trusted-types-event-handlers.html as failing...
[gecko.git] / toolkit / modules / BrowserTelemetryUtils.sys.mjs
blob39b12676422c019b1e4a04a48bdeaeba1c777113
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);
10     });
11   },
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.
18     let tabs = [];
19     if (aIsGeckoView) {
20       // To get all active windows; Each tab has its own window
21       tabs = aWindows;
22     } else {
23       for (const win of aWindows) {
24         tabs = tabs.concat(win.gBrowser.tabs);
25       }
26     }
28     let topLevelBCs = [];
30     for (const tab of tabs) {
31       let browser;
32       if (aIsGeckoView) {
33         browser = tab.browser;
34       } else {
35         browser = tab.linkedBrowser;
36       }
38       if (browser.browsingContext) {
39         // This is the top level browsingContext
40         topLevelBCs.push(browser.browsingContext);
41       }
42     }
44     return CanonicalBrowsingContext.countSiteOrigins(topLevelBCs);
45   },
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",
54         300000
55       );
56     }
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);
68     }
69   },