AboutNewTabChild.sys.mjs (3456B)
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/. */ 5 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"; 10 11 const lazy = {}; 12 13 ChromeUtils.defineESModuleGetters(lazy, { 14 NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs", 15 }); 16 17 XPCOMUtils.defineLazyPreferenceGetter( 18 lazy, 19 "ACTIVITY_STREAM_DEBUG", 20 "browser.newtabpage.activity-stream.debug", 21 false 22 ); 23 24 let gNextPortID = 0; 25 26 export class AboutNewTabChild extends RemotePageChild { 27 handleEvent(event) { 28 if (event.type == "DOMDocElementInserted") { 29 let portID = Services.appinfo.processID + ":" + ++gNextPortID; 30 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 } 41 42 const debug = !AppConstants.RELEASE_OR_BETA && lazy.ACTIVITY_STREAM_DEBUG; 43 const debugString = debug ? "-dev" : ""; 44 45 // This list must match any similar ones in render-activity-stream-html.js. 46 const scripts = [ 47 "chrome://browser/content/contentTheme.js", 48 `chrome://global/content/vendor/react${debugString}.js`, 49 `chrome://global/content/vendor/react-dom${debugString}.js`, 50 "chrome://global/content/vendor/prop-types.js", 51 "chrome://global/content/vendor/react-transition-group.js", 52 "chrome://global/content/vendor/redux.js", 53 "chrome://global/content/vendor/react-redux.js", 54 "resource://newtab/data/content/activity-stream.bundle.js", 55 "resource://newtab/data/content/newtab-render.js", 56 ]; 57 58 for (let script of scripts) { 59 Services.scriptloader.loadSubScriptWithOptions(script, { 60 target: this.contentWindow, 61 ignoreCache: true, 62 }); 63 } 64 } else if (event.type == "unload") { 65 try { 66 this.sendAsyncMessage("Unload"); 67 } catch (e) { 68 // If the tab has been closed the frame message manager has already been 69 // destroyed 70 } 71 } else if (event.type == "pageshow" || event.type == "visibilitychange") { 72 // Don't show the notification in non-permanent private windows 73 // since it is expected to have very little opt-in here. 74 let contentWindowPrivate = PrivateBrowsingUtils.isContentWindowPrivate( 75 this.contentWindow 76 ); 77 if ( 78 this.document.visibilityState == "visible" && 79 (!contentWindowPrivate || 80 (contentWindowPrivate && 81 PrivateBrowsingUtils.permanentPrivateBrowsing)) 82 ) { 83 this.sendAsyncMessage("AboutNewTabVisible"); 84 85 // Note: newtab feature info is currently being loaded in PrefsFeed.sys.mjs, 86 // But we're recording exposure events here. 87 lazy.NimbusFeatures.newtab.recordExposureEvent({ once: true }); 88 } 89 } 90 } 91 }