summaryrefslogtreecommitdiffstats
path: root/browser/modules/TabUnloader.jsm
blob: 772c7eac94cc9490f9ecbf2654cd34042b79b717 (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
/* -*- mode: js; indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* 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";

var EXPORTED_SYMBOLS = ["TabUnloader"];

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

/**
 * This module is responsible for detecting low-memory scenarios and unloading
 * tabs in response to them.
 */

var TabUnloader = {
  /**
   * Initialize low-memory detection and tab auto-unloading.
   */
  init() {
    if (Services.prefs.getBoolPref("browser.tabs.unloadOnLowMemory", true)) {
      Services.obs.addObserver(this, "memory-pressure", /* ownsWeak */ true);
    }
  },

  observe(subject, topic, data) {
    if (topic == "memory-pressure" && data != "heap-minimize") {
      unloadLeastRecentlyUsedTab();
    }
  },

  QueryInterface: ChromeUtils.generateQI([
    "nsIObserver",
    "nsISupportsWeakReference",
  ]),
};

function unloadLeastRecentlyUsedTab() {
  let bgTabBrowsers = getSortedBackgroundTabBrowsers();

  for (let tb of bgTabBrowsers) {
    if (tb.browser.discardBrowser(tb.tab)) {
      return;
    }
  }
}

/* Sort tabs in the order we use for unloading, first non-pinned, non-audible
 * tabs in LRU order, then non-audible tabs in LRU order, then non-pinned
 * audible tabs in LRU order and finally pinned, audible tabs in LRU order. */
function sortTabs(a, b) {
  if (a.tab.soundPlaying != b.tab.soundPlaying) {
    return a.tab.soundPlaying - b.tab.soundPlaying;
  }

  if (a.tab.pinned != b.tab.pinned) {
    return a.tab.pinned - b.tab.pinned;
  }

  return a.tab.lastAccessed - b.tab.lastAccessed;
}

function getSortedBackgroundTabBrowsers() {
  let bgTabBrowsers = [];

  for (let win of Services.wm.getEnumerator("navigator:browser")) {
    for (let tab of win.gBrowser.tabs) {
      if (!tab.selected && tab.linkedBrowser.isConnected) {
        bgTabBrowsers.push({ tab, browser: win.gBrowser });
      }
    }
  }

  return bgTabBrowsers.sort(sortTabs);
}