Bug 1883706: part 3) Implement `createHTML`, `createScript` and `createScriptURL...
[gecko.git] / browser / actors / AboutTabCrashedParent.sys.mjs
blobd24b838a23af50f99ef0ed91c4fdbdd7c64599e1
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const lazy = {};
7 ChromeUtils.defineESModuleGetters(lazy, {
8   SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
9   TabCrashHandler: "resource:///modules/ContentCrashHandlers.sys.mjs",
10 });
12 // A list of all of the open about:tabcrashed pages.
13 let gAboutTabCrashedPages = new Map();
15 export class AboutTabCrashedParent extends JSWindowActorParent {
16   didDestroy() {
17     this.removeCrashedPage();
18   }
20   async receiveMessage(message) {
21     let browser = this.browsingContext.top.embedderElement;
22     if (!browser) {
23       // If there is no browser, remove the crashed page from the set
24       // and return.
25       this.removeCrashedPage();
26       return;
27     }
29     let gBrowser = browser.getTabBrowser();
30     let tab = gBrowser.getTabForBrowser(browser);
32     switch (message.name) {
33       case "Load": {
34         gAboutTabCrashedPages.set(this, browser);
35         this.updateTabCrashedCount();
37         let report = lazy.TabCrashHandler.onAboutTabCrashedLoad(browser);
38         this.sendAsyncMessage("SetCrashReportAvailable", report);
39         break;
40       }
42       case "closeTab": {
43         lazy.TabCrashHandler.maybeSendCrashReport(browser, message);
44         gBrowser.removeTab(tab, { animate: true });
45         break;
46       }
48       case "restoreTab": {
49         lazy.TabCrashHandler.maybeSendCrashReport(browser, message);
50         lazy.SessionStore.reviveCrashedTab(tab);
51         break;
52       }
54       case "restoreAll": {
55         lazy.TabCrashHandler.maybeSendCrashReport(browser, message);
56         lazy.SessionStore.reviveAllCrashedTabs();
57         break;
58       }
59     }
60   }
62   removeCrashedPage() {
63     let browser =
64       this.browsingContext.top.embedderElement ||
65       gAboutTabCrashedPages.get(this);
67     gAboutTabCrashedPages.delete(this);
68     this.updateTabCrashedCount();
70     lazy.TabCrashHandler.onAboutTabCrashedUnload(browser);
71   }
73   updateTabCrashedCount() {
74     // Broadcast to all about:tabcrashed pages a count of
75     // how many about:tabcrashed pages exist, so that they
76     // can decide whether or not to display the "Restore All
77     // Crashed Tabs" button.
78     let count = gAboutTabCrashedPages.size;
80     for (let actor of gAboutTabCrashedPages.keys()) {
81       let browser = actor.browsingContext.top.embedderElement;
82       if (browser) {
83         browser.sendMessageToActor("UpdateCount", { count }, "AboutTabCrashed");
84       }
85     }
86   }