tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AboutTabCrashedParent.sys.mjs (2589B)


      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 const lazy = {};
      6 
      7 ChromeUtils.defineESModuleGetters(lazy, {
      8  SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
      9  TabCrashHandler: "resource:///modules/ContentCrashHandlers.sys.mjs",
     10 });
     11 
     12 // A list of all of the open about:tabcrashed pages.
     13 let gAboutTabCrashedPages = new Map();
     14 
     15 export class AboutTabCrashedParent extends JSWindowActorParent {
     16  didDestroy() {
     17    this.removeCrashedPage();
     18  }
     19 
     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    }
     28 
     29    let gBrowser = browser.getTabBrowser();
     30    let tab = gBrowser.getTabForBrowser(browser);
     31 
     32    switch (message.name) {
     33      case "Load": {
     34        gAboutTabCrashedPages.set(this, browser);
     35        this.updateTabCrashedCount();
     36 
     37        let report = lazy.TabCrashHandler.onAboutTabCrashedLoad(browser);
     38        this.sendAsyncMessage("SetCrashReportAvailable", report);
     39        break;
     40      }
     41 
     42      case "closeTab": {
     43        lazy.TabCrashHandler.maybeSendCrashReport(browser, message);
     44        gBrowser.removeTab(tab, { animate: true });
     45        break;
     46      }
     47 
     48      case "restoreTab": {
     49        lazy.TabCrashHandler.maybeSendCrashReport(browser, message);
     50        lazy.SessionStore.reviveCrashedTab(tab);
     51        break;
     52      }
     53 
     54      case "restoreAll": {
     55        lazy.TabCrashHandler.maybeSendCrashReport(browser, message);
     56        lazy.SessionStore.reviveAllCrashedTabs();
     57        break;
     58      }
     59    }
     60  }
     61 
     62  removeCrashedPage() {
     63    let browser =
     64      this.browsingContext.top.embedderElement ||
     65      gAboutTabCrashedPages.get(this);
     66 
     67    gAboutTabCrashedPages.delete(this);
     68    this.updateTabCrashedCount();
     69 
     70    lazy.TabCrashHandler.onAboutTabCrashedUnload(browser);
     71  }
     72 
     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;
     79 
     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  }
     87 }