Bug 1845017 - Disable the TestPHCExhaustion test r=glandium
[gecko.git] / browser / actors / AboutNewTabChild.sys.mjs
blob87e8a99135b681caf982f9b6b0b408a681de914c
1 /* vim: set ts=2 sw=2 sts=2 et tw=80: */
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 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
7 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
8 import { PrivateBrowsingUtils } from "resource://gre/modules/PrivateBrowsingUtils.sys.mjs";
9 import { RemotePageChild } from "resource://gre/actors/RemotePageChild.sys.mjs";
11 const lazy = {};
13 ChromeUtils.defineESModuleGetters(lazy, {
14   NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs",
15 });
17 XPCOMUtils.defineLazyPreferenceGetter(
18   lazy,
19   "ACTIVITY_STREAM_DEBUG",
20   "browser.newtabpage.activity-stream.debug",
21   false
24 let gNextPortID = 0;
26 export class AboutNewTabChild extends RemotePageChild {
27   handleEvent(event) {
28     if (event.type == "DOMDocElementInserted") {
29       let portID = Services.appinfo.processID + ":" + ++gNextPortID;
31       this.sendAsyncMessage("Init", {
32         portID,
33         url: this.contentWindow.document.documentURI.replace(/[\#|\?].*$/, ""),
34       });
35     } else if (event.type == "load") {
36       this.sendAsyncMessage("Load");
37     } else if (event.type == "DOMContentLoaded") {
38       if (!this.contentWindow.document.body.firstElementChild) {
39         return; // about:newtab is a blank page
40       }
42       // If the separate about:welcome page is enabled, we can skip all of this,
43       // since that mode doesn't load any of the Activity Stream bits.
44       if (
45         (lazy.NimbusFeatures.aboutwelcome.getVariable("enabled") ?? true) &&
46         this.contentWindow.location.pathname.includes("welcome")
47       ) {
48         return;
49       }
51       const debug = !AppConstants.RELEASE_OR_BETA && lazy.ACTIVITY_STREAM_DEBUG;
52       const debugString = debug ? "-dev" : "";
54       // This list must match any similar ones in render-activity-stream-html.js.
55       const scripts = [
56         "chrome://browser/content/contentSearchUI.js",
57         "chrome://browser/content/contentSearchHandoffUI.js",
58         "chrome://browser/content/contentTheme.js",
59         `resource://activity-stream/vendor/react${debugString}.js`,
60         `resource://activity-stream/vendor/react-dom${debugString}.js`,
61         "resource://activity-stream/vendor/prop-types.js",
62         "resource://activity-stream/vendor/react-transition-group.js",
63         "resource://activity-stream/vendor/redux.js",
64         "resource://activity-stream/vendor/react-redux.js",
65         "resource://activity-stream/data/content/activity-stream.bundle.js",
66         "resource://activity-stream/data/content/newtab-render.js",
67       ];
69       for (let script of scripts) {
70         Services.scriptloader.loadSubScript(script, this.contentWindow);
71       }
72     } else if (event.type == "unload") {
73       try {
74         this.sendAsyncMessage("Unload");
75       } catch (e) {
76         // If the tab has been closed the frame message manager has already been
77         // destroyed
78       }
79     } else if (
80       (event.type == "pageshow" || event.type == "visibilitychange") &&
81       // The default browser notification shouldn't be shown on about:welcome
82       // since we don't want to distract from the onboarding wizard.
83       !this.contentWindow.location.pathname.includes("welcome")
84     ) {
85       // Don't show the notification in non-permanent private windows
86       // since it is expected to have very little opt-in here.
87       let contentWindowPrivate = PrivateBrowsingUtils.isContentWindowPrivate(
88         this.contentWindow
89       );
90       if (
91         this.document.visibilityState == "visible" &&
92         (!contentWindowPrivate ||
93           (contentWindowPrivate &&
94             PrivateBrowsingUtils.permanentPrivateBrowsing))
95       ) {
96         this.sendAsyncMessage("AboutNewTabVisible");
98         // Note: newtab feature info is currently being loaded in PrefsFeed.jsm,
99         // But we're recording exposure events here.
100         lazy.NimbusFeatures.newtab.recordExposureEvent({ once: true });
101       }
102     }
103   }