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
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // Fills up aProcesses until max and then selects randomly from the available
7 function RandomSelector() {}
9 RandomSelector.prototype = {
10 classID: Components.ID("{c616fcfd-9737-41f1-aa74-cee72a38f91b}"),
11 QueryInterface: ChromeUtils.generateQI(["nsIContentProcessProvider"]),
13 provideProcess(aType, aProcesses, aMaxCount) {
14 if (aProcesses.length < aMaxCount) {
15 return Ci.nsIContentProcessProvider.NEW_PROCESS;
18 return Math.floor(Math.random() * aMaxCount);
22 // Fills up aProcesses until max and then selects one from the available
23 // ones that host the least number of tabs.
24 function MinTabSelector() {}
26 MinTabSelector.prototype = {
27 classID: Components.ID("{2dc08eaf-6eef-4394-b1df-a3a927c1290b}"),
28 QueryInterface: ChromeUtils.generateQI(["nsIContentProcessProvider"]),
30 provideProcess(aType, aProcesses, aMaxCount) {
31 let min = Number.MAX_VALUE;
32 let candidate = Ci.nsIContentProcessProvider.NEW_PROCESS;
34 // The reason for not directly using aProcesses.length here is because if
35 // we keep processes alive for testing but want a test to use only single
36 // content process we can just keep relying on dom.ipc.processCount = 1
38 let numIters = Math.min(aProcesses.length, aMaxCount);
40 for (let i = 0; i < numIters; i++) {
41 let process = aProcesses[i];
42 let tabCount = process.tabCount;
49 // If all current processes have at least one tab and we have not yet
50 // reached the maximum, spawn a new process.
51 if (min > 0 && aProcesses.length < aMaxCount) {
52 return Ci.nsIContentProcessProvider.NEW_PROCESS;
55 // Otherwise we use candidate.
60 var EXPORTED_SYMBOLS = ["RandomSelector", "MinTabSelector"];