summaryrefslogtreecommitdiffstats
path: root/browser/components/tabunloader/content/aboutUnloads.js
blob: 635f1243d33205c1db88d9e64d38da75add17d8e (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* 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/. */
"use strict";

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

async function refreshData() {
  const sortedTabs = await TabUnloader.getSortedTabs(null);
  const tabTable = document.querySelector(".tab-table > tbody");
  const getHost = uri => {
    try {
      return uri?.host;
    } catch (e) {
      return uri?.spec;
    }
  };
  const updateTimestamp = () => {
    document.l10n.setAttributes(
      document.getElementById("label-last-updated"),
      "about-unloads-last-updated",
      { date: Date.now() }
    );
  };

  // Reset the table
  // Don't delete the first row showing the "no unloadable tab" message
  while (tabTable.rows.length > 1) {
    tabTable.deleteRow(1);
  }
  if (!sortedTabs.length) {
    document.getElementById("button-unload").disabled = true;
    document.getElementById("no-unloadable-tab-message").hidden = false;
    updateTimestamp();
    return;
  }
  document.getElementById("button-unload").disabled =
    !TabUnloader.isDiscardable(sortedTabs[0]);
  document.getElementById("no-unloadable-tab-message").hidden = true;

  const fragmentRows = new DocumentFragment();
  const templateRow = document.querySelector("template[name=tab-table-row]");

  let ordinal = 0;
  for (const tabInfo of sortedTabs) {
    if (!("tab" in tabInfo)) {
      continue;
    }

    const fragment = templateRow.content.cloneNode(true);
    const row = fragment.querySelector("tr");

    row.children[0].textContent = TabUnloader.isDiscardable(tabInfo)
      ? ++ordinal
      : "-";
    row.children[1].textContent = getHost(
      tabInfo.tab?.linkedBrowser?.currentURI
    );
    if ("lastAccessed" in tabInfo.tab) {
      document.l10n.setAttributes(
        row.children[2],
        "about-unloads-last-accessed",
        { date: tabInfo.tab.lastAccessed }
      );
    }
    row.children[3].textContent = tabInfo.weight;
    row.children[4].textContent = tabInfo.sortWeight;
    if ("memory" in tabInfo) {
      document.l10n.setAttributes(
        row.children[5],
        "about-unloads-memory-in-mb",
        { mem: tabInfo.memory / 1024 / 1024 }
      );
    }

    if (tabInfo.processes) {
      for (const [pid, procEntry] of tabInfo.processes) {
        if (pid < 0) {
          // Tab is hosted by the main process
          continue;
        }

        const procLabel = document.createElement("span");
        const procInfo = procEntry.entryToProcessMap;

        if (procEntry.isTopLevel) {
          procLabel.classList.add("top-level-process");
        }
        if (procInfo.tabSet.size > 1) {
          procLabel.classList.add("shared-process");
        }
        procLabel.textContent = pid;
        document.l10n.setAttributes(
          procLabel,
          "about-unloads-memory-in-mb-tooltip",
          { mem: procInfo.memory / 1024 / 1024 }
        );
        row.children[6].appendChild(procLabel);
        row.children[6].appendChild(document.createTextNode(" "));
      }
    }

    fragmentRows.appendChild(fragment);
  }

  tabTable.appendChild(fragmentRows);
  updateTimestamp();
}

async function onLoad() {
  document
    .getElementById("button-unload")
    .addEventListener("click", async () => {
      await TabUnloader.unloadLeastRecentlyUsedTab(null);
      await refreshData();
    });
  await refreshData();
}

try {
  document.addEventListener("DOMContentLoaded", onLoad, { once: true });
} catch (ex) {
  console.error(ex);
}