Bug 1707290 [wpt PR 28671] - Auto-expand details elements for find-in-page, a=testonly
[gecko.git] / testing / mochitest / ShutdownLeaksCollector.jsm
blobe6cd95831ddf8b4eccf534cabd12bc37b8b19044
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/. */
5 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
6 const { setTimeout } = ChromeUtils.import("resource://gre/modules/Timer.jsm");
8 var EXPORTED_SYMBOLS = ["ContentCollector"];
10 // This listens for the message "browser-test:collect-request". When it gets it,
11 // it runs some GCs and CCs, then prints out a message indicating the collections
12 // are complete. Mochitest uses this information to determine when windows and
13 // docshells should be destroyed.
15 var ContentCollector = {
16   init() {
17     let processType = Services.appinfo.processType;
18     if (processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
19       // In the main process, we handle triggering collections in browser-test.js
20       return;
21     }
23     Services.cpmm.addMessageListener("browser-test:collect-request", this);
24   },
26   receiveMessage(aMessage) {
27     switch (aMessage.name) {
28       case "browser-test:collect-request":
29         Services.obs.notifyObservers(null, "memory-pressure", "heap-minimize");
31         Cu.forceGC();
32         Cu.forceCC();
34         let shutdownCleanup = aCallback => {
35           Cu.schedulePreciseShrinkingGC(() => {
36             // Run the GC and CC a few times to make sure that as much
37             // as possible is freed.
38             Cu.forceGC();
39             Cu.forceCC();
40             aCallback();
41           });
42         };
44         shutdownCleanup(() => {
45           setTimeout(() => {
46             shutdownCleanup(() => {
47               this.finish();
48             });
49           }, 1000);
50         });
52         break;
53     }
54   },
56   finish() {
57     let pid = Services.appinfo.processID;
58     dump("Completed ShutdownLeaks collections in process " + pid + "\n");
60     Services.cpmm.removeMessageListener("browser-test:collect-request", this);
61   },
63 ContentCollector.init();