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/. */
4
5"use strict";
6
7var EXPORTED_SYMBOLS = ["AboutTabCrashedParent"];
8
9const { XPCOMUtils } = ChromeUtils.import(
10  "resource://gre/modules/XPCOMUtils.jsm"
11);
12XPCOMUtils.defineLazyModuleGetters(this, {
13  SessionStore: "resource:///modules/sessionstore/SessionStore.jsm",
14  TabCrashHandler: "resource:///modules/ContentCrashHandlers.jsm",
15});
16
17// A list of all of the open about:tabcrashed pages.
18let gAboutTabCrashedPages = new Map();
19
20class AboutTabCrashedParent extends JSWindowActorParent {
21  didDestroy() {
22    this.removeCrashedPage();
23  }
24
25  async receiveMessage(message) {
26    let browser = this.browsingContext.top.embedderElement;
27    if (!browser) {
28      // If there is no browser, remove the crashed page from the set
29      // and return.
30      this.removeCrashedPage();
31      return;
32    }
33
34    let gBrowser = browser.getTabBrowser();
35    let tab = gBrowser.getTabForBrowser(browser);
36
37    switch (message.name) {
38      case "Load": {
39        gAboutTabCrashedPages.set(this, browser);
40        this.updateTabCrashedCount();
41
42        let report = TabCrashHandler.onAboutTabCrashedLoad(browser);
43        this.sendAsyncMessage("SetCrashReportAvailable", report);
44        break;
45      }
46
47      case "closeTab": {
48        TabCrashHandler.maybeSendCrashReport(browser, message);
49        gBrowser.removeTab(tab, { animate: true });
50        break;
51      }
52
53      case "restoreTab": {
54        TabCrashHandler.maybeSendCrashReport(browser, message);
55        SessionStore.reviveCrashedTab(tab);
56        break;
57      }
58
59      case "restoreAll": {
60        TabCrashHandler.maybeSendCrashReport(browser, message);
61        SessionStore.reviveAllCrashedTabs();
62        break;
63      }
64    }
65  }
66
67  removeCrashedPage() {
68    let browser =
69      this.browsingContext.top.embedderElement ||
70      gAboutTabCrashedPages.get(this);
71
72    gAboutTabCrashedPages.delete(this);
73    this.updateTabCrashedCount();
74
75    TabCrashHandler.onAboutTabCrashedUnload(browser);
76  }
77
78  updateTabCrashedCount() {
79    // Broadcast to all about:tabcrashed pages a count of
80    // how many about:tabcrashed pages exist, so that they
81    // can decide whether or not to display the "Restore All
82    // Crashed Tabs" button.
83    let count = gAboutTabCrashedPages.size;
84
85    for (let actor of gAboutTabCrashedPages.keys()) {
86      let browser = actor.browsingContext.top.embedderElement;
87      if (browser) {
88        browser.sendMessageToActor("UpdateCount", { count }, "AboutTabCrashed");
89      }
90    }
91  }
92}
93