tor-browser

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

ShutdownLeaksCollector.sys.mjs (1881B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 import { setTimeout } from "resource://gre/modules/Timer.sys.mjs";
      6 
      7 // This listens for the message "browser-test:collect-request". When it gets it,
      8 // it runs some GCs and CCs, then prints out a message indicating the collections
      9 // are complete. Mochitest uses this information to determine when windows and
     10 // docshells should be destroyed.
     11 
     12 export var ContentCollector = {
     13  init() {
     14    let processType = Services.appinfo.processType;
     15    if (processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
     16      // In the main process, we handle triggering collections in browser-test.js
     17      return;
     18    }
     19 
     20    Services.cpmm.addMessageListener("browser-test:collect-request", this);
     21  },
     22 
     23  receiveMessage(aMessage) {
     24    switch (aMessage.name) {
     25      case "browser-test:collect-request": {
     26        Services.obs.notifyObservers(null, "memory-pressure", "heap-minimize");
     27 
     28        Cu.forceGC();
     29        Cu.forceCC();
     30 
     31        let shutdownCleanup = aCallback => {
     32          Cu.schedulePreciseShrinkingGC(() => {
     33            // Run the GC and CC a few times to make sure that as much
     34            // as possible is freed.
     35            Cu.forceGC();
     36            Cu.forceCC();
     37            aCallback();
     38          });
     39        };
     40 
     41        shutdownCleanup(() => {
     42          setTimeout(() => {
     43            shutdownCleanup(() => {
     44              this.finish();
     45            });
     46          }, 1000);
     47        });
     48 
     49        break;
     50      }
     51    }
     52  },
     53 
     54  finish() {
     55    let pid = Services.appinfo.processID;
     56    dump("Completed ShutdownLeaks collections in process " + pid + "\n");
     57 
     58    Services.cpmm.removeMessageListener("browser-test:collect-request", this);
     59  },
     60 };
     61 
     62 ContentCollector.init();