📜 ⬆️ ⬇️

Delayed restart of Firefox: interactive and automatic ways

There are cases when Firefox has to be left unattended for a long time (for example, it runs an extension that collects critical news, or a built-in torrent client, or even a mini-server). It's no secret that with the time of the browser memory consumption increases. Sooner or later, the system may feel discomfort. Therefore, it would be good to be able to set the browser to postpone tasks to restart. There are at least three types of such tasks.



1. Single restart upon user request


First install the Custom Buttons extension. It allows you to create buttons on the toolbar to execute arbitrary code (and the code will be privileged, with extension rights). In fact, it is a framework for quickly creating mini-extensions.

After installation, create a button and in the first tab of the settings for this button, enter the following code:
')
  var timeout = prompt ("Enter the number of minutes to restart:", "1440");
 if (timeout) {
	 window.setTimeout ("Application.restart ();", timeout * 60 * 1000);
 } 

In the dialog called up by pressing the button, you can enter the number of minutes before restarting. The default is set to day.



2. Unconditional periodic restart without user intervention


If the browser is provided to itself for a particularly long period, you may have to resort to repeated periodic restarts. To do this, in the second tab of the settings of the newly created button (tab "Initialization"), you must enter the following code:

  var timeout = 1440;  // minutes
 window.setTimeout ("Application.restart ();", timeout * 60 * 1000); 

After each launch of the browser, the time set by the timeout parameter will begin. Again, the default is to restart once a day, but the user can replace the number 1440 with any other (do not accidentally enter too small an interval (fraction), otherwise you will have to deal with the consequences in safe mode).

If a restart is required at a specified time, and not after a specified period, there is a suitable solution.



3. Conditional periodic restart without user intervention


To the previous method, we can add a check of one of the suitable conditions. Consider two: the period of idle browser and the amount of memory occupied.

A. Check the time of inactivity user

The function will check the inactivity time every minute, and if it exceeds the idleLimit parameter (set in minutes), the browser will restart.

  var idleLimit = 60;  // minutes
 var idleService =
	  Components.classes ["@ mozilla.org/widget/idleservice;1"]
	 .getService (Components.interfaces.nsIIdleService);
 window.setInterval (
	 function () {
		 if (idleService.idleTime / 60000> idleLimit) {
			 Application.restart ();
		 }
	 },
	 60,000
 ); 

B. Check the amount of memory

It's all a bit more complicated. A search in the MDC did not provide documented interfaces for obtaining such information. But in Firefox 3.6, an about:memory page appeared. Analysis of its code leads to the chrome://global/content/aboutMemory.js file, which uses several undocumented interfaces again. With their help, you can take a chance on such a code (the function goes through the available types of memory, and if at least one of them exceeds the set limit (specified in megabytes), the browser will restart):

  var memoryLimit = 200;  // megabytes
 var enumeratedReporters =
	  Components.classes ["@ mozilla.org/memory-reporter-manager;1"]
	 .getService (Components.interfaces.nsIMemoryReporterManager)
	 .enumerateReporters ();
 while (enumeratedReporters.hasMoreElements ()) {
	 if (
		  enumeratedReporters.getNext ()
		 .QueryInterface (Components.interfaces.nsIMemoryReporter)
		 .memoryUsed / 1000000
			 > memoryLimit
	 ) {
		 Application.restart ();
	 }
 } 

However, it is not known how the interfaces used depend on the operating systems (there are mentions in the network that about:memory does not work in Linux). It is also unclear in which version of Firefox these interfaces appeared. Therefore, I would be grateful to the readers if they could test the following code and unsubscribe in the comments about what it issues on different OCs and different versions of Firefox (try not to repeat the configurations that have already been unsubscribed):

  try {
	
	 var enumeratedReporters =
		  Components.classes ["@ mozilla.org/memory-reporter-manager;1"]
		 .getService (Components.interfaces.nsIMemoryReporterManager)
		 .enumerateReporters ();
	
	 var report = "";
	
	 while (enumeratedReporters.hasMoreElements ()) {
		 var memoryReporter =
			 enumeratedReporters.getNext ()
			 .QueryInterface (Components.interfaces.nsIMemoryReporter);
			
		 report + =
			 memoryReporter.path + ": \ t" +
			 memoryReporter.memoryUsed / 1000000 + "\ n";
	 }
	
	 alert (report || "Memory information not available.");
	
 } catch (error) {alert (error)} 

B. We combine both checks

The function checks the idle time every minute. When it exceeds the limit, the function will start checking the memory size every minute. When this limit is exceeded, the browser will restart.

  var idleLimit = 60;  // minutes
 var memoryLimit = 200;  // megabytes

 var idleService =
	  Components.classes ["@ mozilla.org/widget/idleservice;1"]
	 .getService (Components.interfaces.nsIIdleService);
 var reporterManager = 
	  Components.classes ["@ mozilla.org/memory-reporter-manager;1"]
	 .getService (Components.interfaces.nsIMemoryReporterManager);

 window.setInterval (
	 function () {
		 if (idleService.idleTime / 60000> idleLimit) {
			 var enumeratedReporters = reporterManager.enumerateReporters ();
			 while (enumeratedReporters.hasMoreElements ()) {
				 if (
				    enumeratedReporters.getNext ()
				   .QueryInterface (Components.interfaces.nsIMemoryReporter)
				   .memoryUsed / 1000000
					 > memoryLimit
				 ) {
					 Application.restart ();
				 }
			 }		
		 }
	 },
	 60,000
 ); 



It remains to add that before setting restarts, it is necessary to disable the check for browser updates and extensions (or at least make the updates automatic), otherwise the update dialogs may block Firefox after the next restart.

Source: https://habr.com/ru/post/94550/


All Articles