Skip to content

Commit

Permalink
🐛 Fix lifecycle for initialisation when booting
Browse files Browse the repository at this point in the history
  • Loading branch information
kierandrewett committed Jan 30, 2023
1 parent 1b6ff7e commit ba5c0be
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 70 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Expand Up @@ -25,7 +25,6 @@
"javascript.updateImportsOnFileMove.enabled": "always",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"source.fixAll": true
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
Expand Down
52 changes: 34 additions & 18 deletions base/content/browser-init.ts
Expand Up @@ -2,6 +2,7 @@
* 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/. */

import { dBrowser } from "./browser";
import { nsIXULBrowserWindow } from "./browser-window";

/* Firefox scripts */
Expand All @@ -27,34 +28,29 @@ ChromeUtils.defineESModuleGetters(globalThis, {
DevToolsShim: "chrome://devtools-startup/content/DevToolsShim.sys.mjs"
});

const { DotAppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/DotAppConstants.sys.mjs"
);

const { DotCustomizableUI } = ChromeUtils.importESModule(
"resource://dot/components/customizableui/CustomizableUI.js"
);

class BrowserInit {
private _startTime = Date.now();
export const dBrowserInit = {
_startTime: Date.now(),

onBeforeInitialXULLayout() {
console.time("onBeforeInitialXULLayout");

// Shim setToolbarVisibility as we aren't using trad FF toolbars
window.setToolbarVisibility = shimFunction("setToolbarVisibility", () => {});

gBrowserInit.onBeforeInitialXULLayout.bind(gBrowserInit)();

console.timeEnd("onBeforeInitialXULLayout");
}
},

onDOMContentLoaded() {
console.time("onDOMContentLoaded");

// Shim updateFxaToolbarMenu as we aren't using trad FF toolbars/menus
window.updateFxaToolbarMenu = shimFunction("updateFxaToolbarMenu", () => false);

gBrowserInit.onDOMContentLoaded.bind(gBrowserInit)();

// Creates an nsIXULBrowserWindow instance to handle browser communication and events
const XULBrowserWindow = new nsIXULBrowserWindow();

window.docShell.treeOwner
Expand All @@ -63,8 +59,12 @@ class BrowserInit {

window.XULBrowserWindow = XULBrowserWindow;

// Exposes dBrowser to global for debugging
globalThis.dBrowser = dBrowser;
dBrowser.init();

console.timeEnd("onDOMContentLoaded");
}
},

onLoad() {
console.time("onLoad");
Expand All @@ -74,19 +74,35 @@ class BrowserInit {
console.timeEnd("onLoad");

console.debug(`dBrowserInit: ready in ${Date.now() - this._startTime}ms`);
}
},

onUnload() {
console.time("onUnload");

gBrowserInit.onUnload.bind(gBrowserInit)();

console.timeEnd("onUnload");
}
},

onWindowClosing() {
WindowIsClosing();
onWindowClosing(event: CloseEvent) {
// Determines whether the browser is allowed to close
return WindowIsClosing(event);
}
}
};

globalThis.dBrowserInit = dBrowserInit; // Exposes dBrowserInit to global for debugging

/* Initialise events */
window.addEventListener("load", dBrowserInit.onLoad.bind(dBrowserInit));
window.addEventListener("unload", dBrowserInit.onUnload.bind(dBrowserInit));
window.addEventListener("close", dBrowserInit.onWindowClosing);

export const dBrowserInit = new BrowserInit();
window.addEventListener(
"MozBeforeInitialXULLayout",
dBrowserInit.onBeforeInitialXULLayout.bind(dBrowserInit),
{ once: true }
);

window.addEventListener("DOMContentLoaded", dBrowserInit.onDOMContentLoaded.bind(dBrowserInit), {
once: true
});
20 changes: 20 additions & 0 deletions base/content/browser.ts
Expand Up @@ -2,6 +2,26 @@
* 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/. */

import { DotCustomizableUI } from "../../components/customizableui/CustomizableUI";

// Component declarations
import "../../components/browser-element/content/StatusPanel";
import "../../components/panel/Panel";

ChromeUtils.defineESModuleGetters(globalThis, {
DotAppConstants: "resource://app/modules/DotAppConstants.sys.mjs"
});

export const dBrowser = {
_done: false,

init() {
if (this._done) {
throw new Error("Browser cannot be initialized twice!");
}

DotCustomizableUI.initialize();

this._done = true;
}
};
72 changes: 26 additions & 46 deletions base/content/browser.xhtml
Expand Up @@ -36,64 +36,44 @@
persist="screenX screenY width height sizemode"
>
<head>
# Load browser styles
<link rel="stylesheet" href="chrome://dot/skin/browser.css" />

# Load localisation data
<link rel="localization" href="branding/brand.ftl"/>
<link rel="localization" href="dot/browser.ftl"/>
<link rel="localization" href="browser/browserSets.ftl"/>

# Set the initial browser title before switching to the dynamic contentTitle
<title data-l10n-id="browser-main-window-title"></title>

# Load and initialise the browser logic
<script>
(async function () {
Services.scriptloader.loadSubScript("resource://dot/base/content/browser-utils.js", window);
Services.scriptloader.loadSubScript("resource://dot/base/content/browser-loader.js", window);

/* Eventually these will be shimmed */
Services.scriptloader.loadSubScript("chrome://browser/content/browser.js", this);
Services.scriptloader.loadSubScript("chrome://browser/content/browser-places.js", this);
Services.scriptloader.loadSubScript("chrome://global/content/globalOverlay.js", this);
Services.scriptloader.loadSubScript("chrome://global/content/editMenuOverlay.js", this);
Services.scriptloader.loadSubScript("chrome://browser/content/utilityOverlay.js", this);
if (AppConstants.platform == "macosx") {
Services.scriptloader.loadSubScript("chrome://global/content/macWindowMenu.js", this);
}

if (!AppConstants.MOZILLA_OFFICIAL) {
Services.scriptloader.loadSubScript(
"resource://dot/base/content/browser-development-helpers.js",
this
);

document.documentElement.removeAttribute("remotecontrol");
}

/* Load ES Modules */
await loadESMSubScript("resource://dot/base/content/browser-init.js", window);

/* Initialise events */
window.addEventListener("load", dBrowserInit.onLoad.bind(dBrowserInit));
window.addEventListener("unload", dBrowserInit.onUnload.bind(dBrowserInit));
window.addEventListener("close", dBrowserInit.onWindowClosing);

window.addEventListener(
"MozBeforeInitialXULLayout",
dBrowserInit.onBeforeInitialXULLayout.bind(dBrowserInit),
{ once: true }
var { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);

// Sets up DevTools connection for debugging if in a non-release build
if (!AppConstants.MOZILLA_OFFICIAL) {
Services.scriptloader.loadSubScript(
"resource://dot/base/content/browser-development-helpers.js",
this
);

window.addEventListener(
"DOMContentLoaded",
dBrowserInit.onDOMContentLoaded.bind(dBrowserInit),
{ once: true }
);
})()
document.documentElement.removeAttribute("remotecontrol");
}

// Important utilities/globals for browser
Services.scriptloader.loadSubScript("resource://dot/base/content/browser-utils.js", this);
Services.scriptloader.loadSubScript("resource://dot/base/content/browser-loader.js", this);

// FF modules and scripts
Services.scriptloader.loadSubScript("chrome://browser/content/browser.js", this);
Services.scriptloader.loadSubScript("chrome://browser/content/browser-places.js", this);
Services.scriptloader.loadSubScript("chrome://global/content/globalOverlay.js", this);
Services.scriptloader.loadSubScript("chrome://global/content/editMenuOverlay.js", this);
Services.scriptloader.loadSubScript("chrome://browser/content/utilityOverlay.js", this);
if (AppConstants.platform == "macosx") {
Services.scriptloader.loadSubScript("chrome://global/content/macWindowMenu.js", this);
}
</script>
<script type="module" src="resource://dot/base/content/browser.js"></script>
<script type="module" src="resource://dot/base/content/browser-init.js"></script>
</head>

<html:body xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
Expand Down
1 change: 1 addition & 0 deletions build/scripts/build.js
Expand Up @@ -45,6 +45,7 @@ function transform(inPath, outPath) {
bundle: true,
format: "esm",
treeShaking: false,
minify: false,
platform: "browser",
target: "firefox100",
jsx: "transform",
Expand Down
9 changes: 4 additions & 5 deletions types.d.ts
Expand Up @@ -2,13 +2,12 @@
* 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/. */

import { dBrowser } from "./base/content/browser";
import { dBrowserInit } from "./base/content/browser-init";
import { nsIXULBrowserWindow } from "./base/content/browser-window";
import { DotCustomizableUI } from "./components/customizableui/CustomizableUI";
import "./third_party/dothq/gecko-types";
import * as Gecko from "./third_party/dothq/gecko-types/lib";

declare module "resource://app/modules/DevToolsServer.sys.mjs" {}

declare global {
/* Only available in secure contexts */
interface Screen {
Expand All @@ -27,8 +26,8 @@ declare global {
updateFxaToolbarMenu: any;
SidebarUI: any;
LightweightThemeConsumer: any;
CustomizableUI: typeof DotCustomizableUI;
DotCustomizableUI: typeof DotCustomizableUI;
dBrowser: typeof dBrowser;
dBrowserInit: typeof dBrowserInit;
html: (
tagName: string,
attributes?: { [key: string]: any },
Expand Down

0 comments on commit ba5c0be

Please sign in to comment.