summaryrefslogtreecommitdiffstats
path: root/dom/base/ProcessSelector.jsm
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/base/ProcessSelector.jsm
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/base/ProcessSelector.jsm')
-rw-r--r--dom/base/ProcessSelector.jsm60
1 files changed, 60 insertions, 0 deletions
diff --git a/dom/base/ProcessSelector.jsm b/dom/base/ProcessSelector.jsm
new file mode 100644
index 0000000000..e582d485b4
--- /dev/null
+++ b/dom/base/ProcessSelector.jsm
@@ -0,0 +1,60 @@
+/* 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/. */
+
+// Fills up aProcesses until max and then selects randomly from the available
+// ones.
+function RandomSelector() {}
+
+RandomSelector.prototype = {
+ classID: Components.ID("{c616fcfd-9737-41f1-aa74-cee72a38f91b}"),
+ QueryInterface: ChromeUtils.generateQI(["nsIContentProcessProvider"]),
+
+ provideProcess(aType, aProcesses, aMaxCount) {
+ if (aProcesses.length < aMaxCount) {
+ return Ci.nsIContentProcessProvider.NEW_PROCESS;
+ }
+
+ return Math.floor(Math.random() * aMaxCount);
+ },
+};
+
+// Fills up aProcesses until max and then selects one from the available
+// ones that host the least number of tabs.
+function MinTabSelector() {}
+
+MinTabSelector.prototype = {
+ classID: Components.ID("{2dc08eaf-6eef-4394-b1df-a3a927c1290b}"),
+ QueryInterface: ChromeUtils.generateQI(["nsIContentProcessProvider"]),
+
+ provideProcess(aType, aProcesses, aMaxCount) {
+ let min = Number.MAX_VALUE;
+ let candidate = Ci.nsIContentProcessProvider.NEW_PROCESS;
+
+ // The reason for not directly using aProcesses.length here is because if
+ // we keep processes alive for testing but want a test to use only single
+ // content process we can just keep relying on dom.ipc.processCount = 1
+ // this way.
+ let numIters = Math.min(aProcesses.length, aMaxCount);
+
+ for (let i = 0; i < numIters; i++) {
+ let process = aProcesses[i];
+ let tabCount = process.tabCount;
+ if (tabCount < min) {
+ min = tabCount;
+ candidate = i;
+ }
+ }
+
+ // If all current processes have at least one tab and we have not yet
+ // reached the maximum, spawn a new process.
+ if (min > 0 && aProcesses.length < aMaxCount) {
+ return Ci.nsIContentProcessProvider.NEW_PROCESS;
+ }
+
+ // Otherwise we use candidate.
+ return candidate;
+ },
+};
+
+var EXPORTED_SYMBOLS = ["RandomSelector", "MinTabSelector"];