summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js
blob: 2c54bea61c07b650b6a23c636107970d10244aa9 (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
/* eslint-env mozilla/chrome-script */

"use strict";

const { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

let listener = msg => {
  void (msg instanceof Ci.nsIConsoleMessage);
  dump(`Console message: ${msg}\n`);
};

Services.console.registerListener(listener);

let getBrowserApp, getTabBrowser;
if (AppConstants.MOZ_BUILD_APP === "mobile/android") {
  getBrowserApp = win => win.BrowserApp;
  getTabBrowser = tab => tab.browser;
} else {
  getBrowserApp = win => win.gBrowser;
  getTabBrowser = tab => tab.linkedBrowser;
}

function* iterBrowserWindows() {
  for (let win of Services.wm.getEnumerator("navigator:browser")) {
    if (!win.closed && getBrowserApp(win)) {
      yield win;
    }
  }
}

let initialTabs = new Map();
for (let win of iterBrowserWindows()) {
  initialTabs.set(win, new Set(getBrowserApp(win).tabs));
}

addMessageListener("check-cleanup", () => {
  Services.console.unregisterListener(listener);

  let results = {
    extraWindows: [],
    extraTabs: [],
  };

  for (let win of iterBrowserWindows()) {
    if (initialTabs.has(win)) {
      let tabs = initialTabs.get(win);

      for (let tab of getBrowserApp(win).tabs) {
        if (!tabs.has(tab)) {
          results.extraTabs.push(getTabBrowser(tab).currentURI.spec);
        }
      }
    } else {
      results.extraWindows.push(
        Array.from(win.gBrowser.tabs, tab => getTabBrowser(tab).currentURI.spec)
      );
    }
  }

  initialTabs = null;

  sendAsyncMessage("cleanup-results", results);
});