TODO: Update this chapter. It's partly obsolete since commit 355cf5d
.
RequestPolicy has a class called Environment
which can be used from anywhere. A new environment is created via
let env = new Environment("my environment");
To each instance of Environment
, startup
and shutdown
functions can be added. This means that the Environment
class helps to manage tasks that have to be done when an environment is created or destroyed. When the Environment is about to start up, all registered startup functions will be called; same with shutdown.
By the way, it is possible to add startup and shutdown functions at any time. If a startup function is added when the Environment is already up and running, that function will be called immediately.
To have some more control, there are different startup/shutdown levels: ESSENTIAL
, BACKEND
, INTERFACE
and UI
. To know for what each of them is used, please refer to the code.
EnvironmentManager
is a JavaScript module which – as the name says – manages environments. Each Environment
imports the Environment Manager and registers itself on the Environment's startup. Similarly, the Environment unregisters itself when it shuts down. Note that because of the nature of JavaScript modules, there will be one Environment Manager for each process.
EnvironmentManager
enables to shut down all registered Environments at once. This is needed when the addon gets disabled (RP is a bootstrapped extension). More specifically, the main process' Environment Manager's startup
and shutdown
functions are called by bootstrap.js
. The child Environment Managers on the other hand will listen to the shutdown message (see MessageManager).
The startup sequence is as follows:
startup
function in bootstrap.js
gets called by the browserbootstrap.js
calls Environment Manager.startup()
The shutdown sequence is quite similar:
shutdown
function in bootstrap.js
gets called by the browserbootstrap.js
calls Environment Manager.shutdown()
. This is the main process' Environment Manager, as bootsrap.js
is in the main process.Many modules define startup and shutdown functions. Most modules attach those functions to the Process Environment. That's a small module which simply creates a new Environment once it's called.
Frame scripts are needed to support Electrolysis (abbr. e10s; codename for Multiprocess Firefox). RequestPolicy loads a frame script into each tab of each browser window, regardless of whether Electrolysis is enabled or not.
Special attention needs to be given to the fact that even when Electrolysis is enabled there might still be tabs which are in the main process. So if a new Environment is created in the framescript's scope, it will be managed by either the main process' or the child process' EnvironmentManager
. However, the frame script will create its own environment only if it's in the main process; framescripts in child processes can use the Process Environment. The reason is that also the framescript imports some JavaScript modules, and those modules might add startup functions to the Process Environment as well. This way the frame script needs to manage only one instead of two Environments in the child process.
So the summary of the framescript's startup process is:
registerFramescript()
function. The Environment Manager will then listen to the "shutdown" message, which will be sent from the main process.The shutdown works the same as in the main process,