summaryrefslogtreecommitdiffstats
path: root/browser/actors/AboutTabCrashedParent.sys.mjs
blob: 4c11c3430d7926c2ccfae345ce8f657b4bb17a85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* This Source Code Form is subject to the terms of the Mozilla Public
 * 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 { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(lazy, {
  TabCrashHandler: "resource:///modules/ContentCrashHandlers.jsm",
});

// A list of all of the open about:tabcrashed pages.
let gAboutTabCrashedPages = new Map();

export class AboutTabCrashedParent extends JSWindowActorParent {
  didDestroy() {
    this.removeCrashedPage();
  }

  async receiveMessage(message) {
    let browser = this.browsingContext.top.embedderElement;
    if (!browser) {
      // If there is no browser, remove the crashed page from the set
      // and return.
      this.removeCrashedPage();
      return;
    }

    let gBrowser = browser.getTabBrowser();
    let tab = gBrowser.getTabForBrowser(browser);

    switch (message.name) {
      case "Load": {
        gAboutTabCrashedPages.set(this, browser);
        this.updateTabCrashedCount();

        let report = lazy.TabCrashHandler.onAboutTabCrashedLoad(browser);
        this.sendAsyncMessage("SetCrashReportAvailable", report);
        break;
      }

      case "closeTab": {
        lazy.TabCrashHandler.maybeSendCrashReport(browser, message);
        gBrowser.removeTab(tab, { animate: true });
        break;
      }

      case "restoreTab": {
        lazy.TabCrashHandler.maybeSendCrashReport(browser, message);
        lazy.SessionStore.reviveCrashedTab(tab);
        break;
      }

      case "restoreAll": {
        lazy.TabCrashHandler.maybeSendCrashReport(browser, message);
        lazy.SessionStore.reviveAllCrashedTabs();
        break;
      }
    }
  }

  removeCrashedPage() {
    let browser =
      this.browsingContext.top.embedderElement ||
      gAboutTabCrashedPages.get(this);

    gAboutTabCrashedPages.delete(this);
    this.updateTabCrashedCount();

    lazy.TabCrashHandler.onAboutTabCrashedUnload(browser);
  }

  updateTabCrashedCount() {
    // Broadcast to all about:tabcrashed pages a count of
    // how many about:tabcrashed pages exist, so that they
    // can decide whether or not to display the "Restore All
    // Crashed Tabs" button.
    let count = gAboutTabCrashedPages.size;

    for (let actor of gAboutTabCrashedPages.keys()) {
      let browser = actor.browsingContext.top.embedderElement;
      if (browser) {
        browser.sendMessageToActor("UpdateCount", { count }, "AboutTabCrashed");
      }
    }
  }
}