[Pkg-mozext-commits] [requestpolicy] 152/257: [ref] Environment: wrap code in IIFEs

David Prévot taffit at moszumanska.debian.org
Thu Jan 28 03:20:07 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository requestpolicy.

commit 55f998dcc7fc56603c729b747f566419f9b5a428
Author: Martin Kimmerle <dev at 256k.de>
Date:   Mon Nov 30 22:54:10 2015 +0100

    [ref] Environment: wrap code in IIFEs
---
 src/content/lib/environment.jsm        | 856 +++++++++++++++++----------------
 src/content/lib/environment.process.js |  23 +-
 2 files changed, 460 insertions(+), 419 deletions(-)

diff --git a/src/content/lib/environment.jsm b/src/content/lib/environment.jsm
index c1357b2..fc40a86 100644
--- a/src/content/lib/environment.jsm
+++ b/src/content/lib/environment.jsm
@@ -46,493 +46,521 @@ ScriptLoader.defineLazyModuleGetters({
   "lib/observer-manager": ["ObserverManager"]
 }, globalScope);
 
+//==============================================================================
+// Environment
+//==============================================================================
+
+var Environment = (function () {
+
+  //----------------------------------------------------------------------------
+  // constants, metadata
+  //----------------------------------------------------------------------------
+
+  let ENV_STATES = {
+    "NOT_STARTED": 0,
+    "STARTING_UP": 1,
+    "STARTUP_DONE": 2,
+    "SHUTTING_DOWN": 3,
+    "SHUT_DOWN": 4
+  };
 
+  let LEVELS = {
+    // Essential functions do tasks that must be run first on startup and last
+    // on shutdown, that is they do tasks that are requirements for the Backend.
+    "ESSENTIAL": 1,
+    // Backend functions start up/shut down main parts of RequestPolicy, but
+    // they do not enable RequestPolicy at all.
+    "BACKEND": 2,
+    // Interface functions enable/disable external as well as internal
+    // interfaces, e.g. Event Listeners, Message Listeners, Observers,
+    // Factories.
+    "INTERFACE": 3,
+    // UI functions will enable/disable UI elements such as the menu.
+    "UI": 4
+  };
 
+  // a level can be entered, being processed, or finished being processed.
+  let LEVEL_STATES = {
+    "NOT_ENTERED": 0,
+    "PROCESSING": 1,
+    "FINISHED_PROCESSING": 2
+  };
 
-let ENV_STATES = {
-  "NOT_STARTED": 0,
-  "STARTING_UP": 1,
-  "STARTUP_DONE": 2,
-  "SHUTTING_DOWN": 3,
-  "SHUT_DOWN": 4
-};
-
-let LEVELS = {
-  // Essential functions do tasks that must be run first on startup and last
-  // on shutdown, that is they do tasks that are requirements for the Backend.
-  "ESSENTIAL": 1,
-  // Backend functions start up/shut down main parts of RequestPolicy, but
-  // they do not enable RequestPolicy at all.
-  "BACKEND": 2,
-  // Interface functions enable/disable external as well as internal interfaces,
-  // e.g. Event Listeners, Message Listeners, Observers, Factories.
-  "INTERFACE": 3,
-  // UI functions will enable/disable UI elements such as the menu.
-  "UI": 4
-};
-
-// a level can be entered, being processed, or finished being processed.
-let LEVEL_STATES = {
-  "NOT_ENTERED": 0,
-  "PROCESSING": 1,
-  "FINISHED_PROCESSING": 2
-};
-
-
-let BOOTSTRAP = {
-  "startup": {
-    levelSequence: [
-      LEVELS.ESSENTIAL,
-      LEVELS.BACKEND,
-      LEVELS.INTERFACE,
-      LEVELS.UI
-    ],
-    lastLevel: LEVELS.UI,
-    envStates: {
-      "beforeProcessing": ENV_STATES.NOT_STARTED,
-      "duringProcessing": ENV_STATES.STARTING_UP,
-      "afterProcessing": ENV_STATES.STARTUP_DONE
-    },
-    functions: {
-      "beforeProcessing": function() {
-        // "this" will be an environment
-        let self = this;
-        self.register();
+  let BOOTSTRAP = {
+    "startup": {
+      levelSequence: [
+        LEVELS.ESSENTIAL,
+        LEVELS.BACKEND,
+        LEVELS.INTERFACE,
+        LEVELS.UI
+      ],
+      lastLevel: LEVELS.UI,
+      envStates: {
+        "beforeProcessing": ENV_STATES.NOT_STARTED,
+        "duringProcessing": ENV_STATES.STARTING_UP,
+        "afterProcessing": ENV_STATES.STARTUP_DONE
       },
-      "afterProcessing": function() {}
-    }
-  },
-  "shutdown": {
-    levelSequence: [
-      LEVELS.UI,
-      LEVELS.INTERFACE,
-      LEVELS.BACKEND,
-      LEVELS.ESSENTIAL
-    ],
-    lastLevel: LEVELS.ESSENTIAL,
-    envStates: {
-      "beforeProcessing": ENV_STATES.STARTUP_DONE,
-      "duringProcessing": ENV_STATES.SHUTTING_DOWN,
-      "afterProcessing": ENV_STATES.SHUT_DOWN
+      functions: {
+        "beforeProcessing": function() {
+          // "this" will be an environment
+          let self = this;
+          self.register();
+        },
+        "afterProcessing": function() {}
+      }
     },
-    functions: {
-      "beforeProcessing": function() {},
-      "afterProcessing": function() {
-        // "this" will be an environment
-        let self = this;
-        self.innerEnvs.length = 0;
-        self.unregister();
+    "shutdown": {
+      levelSequence: [
+        LEVELS.UI,
+        LEVELS.INTERFACE,
+        LEVELS.BACKEND,
+        LEVELS.ESSENTIAL
+      ],
+      lastLevel: LEVELS.ESSENTIAL,
+      envStates: {
+        "beforeProcessing": ENV_STATES.STARTUP_DONE,
+        "duringProcessing": ENV_STATES.SHUTTING_DOWN,
+        "afterProcessing": ENV_STATES.SHUT_DOWN
+      },
+      functions: {
+        "beforeProcessing": function() {},
+        "afterProcessing": function() {
+          // "this" will be an environment
+          let self = this;
+          self.innerEnvs.length = 0;
+          self.unregister();
+        }
       }
     }
+  };
+  function getBootstrapMetadata(startupOrShutdown) {
+    return BOOTSTRAP[startupOrShutdown];
   }
-};
-function getBootstrapMetadata(startupOrShutdown) {
-  return BOOTSTRAP[startupOrShutdown];
-}
 
+  //----------------------------------------------------------------------------
+  // Environment
+  //----------------------------------------------------------------------------
 
-/**
- * The `Environment` class can take care of the "startup" (=initialization)
- * and "shutdown" of any environment.
- *
- * To each `Environment` instance, `startup` and `shutdown` functions can be
- * added. As soon as the Environment starts up, e.g. via its startup()
- * function, all those functions will be called. Equally the shutdown
- * functions are called on the environment's shutdown.
- *
- * Both startup and shutdown functions will have Levels assigned. The levels
- * of the functions determine in which sequence they are called.
- *
- * @constructor
- * @param {Environment=} aOuterEnv - the Environment to which this environment
- *     will register itself. Inner environments shut down when its outer
- *     environment shuts down.
- * @param {string=} aName - the Environment's name; only needed for debugging.
- */
-function Environment(aOuterEnv, aName="anonymous") {
-  let self = this;
-
-  self.envState = ENV_STATES.NOT_STARTED;
-
-  self.name = aName;
-
-  self.outerEnv = aOuterEnv instanceof Environment ? aOuterEnv : null;
-  self.innerEnvs = new Set();
+  /**
+   * The `Environment` class can take care of the "startup" (=initialization)
+   * and "shutdown" of any environment.
+   *
+   * To each `Environment` instance, `startup` and `shutdown` functions can be
+   * added. As soon as the Environment starts up, e.g. via its startup()
+   * function, all those functions will be called. Equally the shutdown
+   * functions are called on the environment's shutdown.
+   *
+   * Both startup and shutdown functions will have Levels assigned. The levels
+   * of the functions determine in which sequence they are called.
+   *
+   * @constructor
+   * @param {Environment=} aOuterEnv - the Environment to which this environment
+   *     will register itself. Inner environments shut down when its outer
+   *     environment shuts down.
+   * @param {string=} aName - the Environment's name; only needed for debugging.
+   */
+  function Environment(aOuterEnv, aName="anonymous") {
+    let self = this;
 
-  self.levels = {
-    "startup": generateLevelObjects(),
-    "shutdown": generateLevelObjects()
-  };
+    self.envState = ENV_STATES.NOT_STARTED;
 
-  // Define a Lazy Getter to get an ObserverManager for this Environment.
-  // Using that Getter is more convenient than doing it manually, as the
-  // Environment has to be created *before* the ObserverManager.
-  XPCOMUtils.defineLazyGetter(self, "obMan", function() {
-    return new ObserverManager(self);
-  });
+    self.name = aName;
 
-  // Define a Lazy Getter to get an instance of `ManagerForEventListeners` for
-  // this Environment.
-  XPCOMUtils.defineLazyGetter(self, "elManager", function() {
-    return new ManagerForEventListeners(self);
-  });
+    self.outerEnv = aOuterEnv instanceof Environment ? aOuterEnv : null;
+    self.innerEnvs = new Set();
 
-  // generate an unique ID for debugging purposes
-  XPCOMUtils.defineLazyGetter(self, "uid", function() {
-    return Math.random().toString(36).substr(2, 5);
-  });
+    self.levels = {
+      "startup": generateLevelObjects(),
+      "shutdown": generateLevelObjects()
+    };
 
-  //console.debug('[RPC] created new Environment "'+self.name+'"');
-}
+    // Define a Lazy Getter to get an ObserverManager for this Environment.
+    // Using that Getter is more convenient than doing it manually, as the
+    // Environment has to be created *before* the ObserverManager.
+    XPCOMUtils.defineLazyGetter(self, "obMan", function() {
+      return new ObserverManager(self);
+    });
 
-Environment.LEVELS = LEVELS;
+    // Define a Lazy Getter to get an instance of `ManagerForEventListeners` for
+    // this Environment.
+    XPCOMUtils.defineLazyGetter(self, "elManager", function() {
+      return new ManagerForEventListeners(self);
+    });
 
+    // generate an unique ID for debugging purposes
+    XPCOMUtils.defineLazyGetter(self, "uid", function() {
+      return Math.random().toString(36).substr(2, 5);
+    });
 
-/**
- * This function creates one "Level Object" for each level. Those objects
- * mainly will hold the startup- or shutdown-functions of the corresponding
- * level. All of the Level Objects are put together in another object which
- * is then returned.
- */
-function generateLevelObjects() {
-  let obj = {};
-  for (let levelName in Environment.LEVELS) {
-    let level = Environment.LEVELS[levelName];
-    obj[level] = {"functions": [], "levelState": LEVEL_STATES.NOT_ENTERED};
+    //console.debug('[RPC] created new Environment "'+self.name+'"');
   }
-  return obj;
-}
 
-
-/**
- * Registers the environment to its outer environment.
- */
-Environment.prototype.register = function() {
-  let self = this;
-  if (self.outerEnv) {
-    self.outerEnv.registerInnerEnvironment(self);
-  }
-}
-/**
- * Unregisters the environment from its outer environment.
- */
-Environment.prototype.unregister = function() {
-  let self = this;
-  if (self.outerEnv) {
-    self.outerEnv.unregisterInnerEnvironment(self);
-  }
-}
-/**
- * Function called by an inner environment when it starts up.
- *
- * @param {Environment} aEnv - the environment that wants to register itself.
- */
-Environment.prototype.registerInnerEnvironment = function(aEnv) {
-  let self = this;
-  if (self.envState === ENV_STATES.NOT_STARTED) {
-    console.warn("[RPC] registerInnerEnvironment() has been called, " +
-                 "but the outer environment hasn't started up yet. " +
-                 "Starting up now.");
-    self.startup();
-  }
-  //console.debug("[RPC] registering inner environment.");
-  self.innerEnvs.add(aEnv);
-};
-/**
- * Function that is called each time an inner environment shuts down.
- *
- * @param {Environment} aEnv - the environment that is unregistering
- */
-Environment.prototype.unregisterInnerEnvironment = function(aEnv) {
-  let self = this;
-
-  if (self.innerEnvs.has(aEnv) === false) {
-    console.error("[RPC] it seems like an inner Environment " +
-                  "did not register.");
-  } else {
-    self.innerEnvs.delete(aEnv);
-  }
-};
+  Environment.LEVELS = LEVELS;
+  Environment.ENV_STATES = ENV_STATES;
 
 
+  /**
+   * This function creates one "Level Object" for each level. Those objects
+   * mainly will hold the startup- or shutdown-functions of the corresponding
+   * level. All of the Level Objects are put together in another object which
+   * is then returned.
+   */
+  function generateLevelObjects() {
+    let obj = {};
+    for (let levelName in Environment.LEVELS) {
+      let level = Environment.LEVELS[levelName];
+      obj[level] = {"functions": [], "levelState": LEVEL_STATES.NOT_ENTERED};
+    }
+    return obj;
+  }
 
 
-/**
- * Add a startup function to the environment.
- */
-Environment.prototype.addStartupFunction = function(aLevel, f) {
-  let self = this;
-  if (self.envState >= ENV_STATES.SHUTTING_DOWN) {
-    // the environment is shutting down or already shut down.
-    return;
-  }
-  if (self.levels["startup"][aLevel].levelState >= LEVEL_STATES.PROCESSING) {
-    // Either the startup functions of the same level as `aLevel` have
-    //        already been processed
-    //    OR  they are currently being processed.
-    //
-    // ==> call the function immediately.
-    f();
-  } else {
-    // the startup process did not reach the function's level yet.
-    //
-    // ==> remember the function.
-    self.levels["startup"][aLevel].functions.push(f);
+  /**
+   * Registers the environment to its outer environment.
+   */
+  Environment.prototype.register = function() {
+    let self = this;
+    if (self.outerEnv) {
+      self.outerEnv.registerInnerEnvironment(self);
+    }
   }
-};
-
-/**
- * Add a shutdown function to the environment.
- */
-Environment.prototype.addShutdownFunction = function(aLevel, f) {
-  let self = this;
-  if (self.levels["shutdown"][aLevel].levelState >= LEVEL_STATES.PROCESSING) {
-    // Either the shutdown functions of the same level as `aLevel` have
-    //        already been processed
-    //    OR  they are currently being processed.
-    //
-    // ==> call the function immediately.
-    f();
-    //console.debug('[RPC] calling shutdown function immediately: "' +
-    //              (f.name || "anonymous") + '" (' + self.name + ')');
-  } else {
-    // The opposite, i.e. the startup process did not reach the function's
-    // level yet.
-    //
-    // ==> remember the function.
-    self.levels["shutdown"][aLevel].functions.push(f);
+  /**
+   * Unregisters the environment from its outer environment.
+   */
+  Environment.prototype.unregister = function() {
+    let self = this;
+    if (self.outerEnv) {
+      self.outerEnv.unregisterInnerEnvironment(self);
+    }
   }
-};
-
-
-// have a scope/closure for private functions specific to
-// startup() and shutdown().
-(function createMethods_StartupAndShutdown(Environment) {
   /**
-   * Iterates all levels of either the startup or the shutdown
-   * sequence and calls a function for each level.
+   * Function called by an inner environment when it starts up.
    *
-   * @param {string} aStartupOrShutdown
-   * @param {function()} aFn - the function to call
-   * @param {integer=} aUntilLevel - if specified, iteration stops
-   *     after that level.
+   * @param {Environment} aEnv - the environment that wants to register itself.
    */
-  function iterateLevels(aStartupOrShutdown, aFn, aUntilLevel=null) {
-    let sequence = BOOTSTRAP[aStartupOrShutdown].levelSequence;
+  Environment.prototype.registerInnerEnvironment = function(aEnv) {
+    let self = this;
+    if (self.envState === ENV_STATES.NOT_STARTED) {
+      console.warn("[RPC] registerInnerEnvironment() has been called, " +
+                   "but the outer environment hasn't started up yet. " +
+                   "Starting up now.");
+      self.startup();
+    }
+    //console.debug("[RPC] registering inner environment.");
+    self.innerEnvs.add(aEnv);
+  };
+  /**
+   * Function that is called each time an inner environment shuts down.
+   *
+   * @param {Environment} aEnv - the environment that is unregistering
+   */
+  Environment.prototype.unregisterInnerEnvironment = function(aEnv) {
+    let self = this;
 
-    for (let i = 0, len = sequence.length; i < len; ++i) {
-      // Note: It's necessary to use for(;;) instead of  for(..of..)
-      //       because the order/sequence must be exactly the same as in the
-      //       array.  for(..of..) doesn't guarantee that the elements are
-      //       called in order.
+    if (self.innerEnvs.has(aEnv) === false) {
+      console.error("[RPC] it seems like an inner Environment " +
+                    "did not register.");
+    } else {
+      self.innerEnvs.delete(aEnv);
+    }
+  };
 
-      let level = sequence[i];
-      aFn(level);
 
-      if (level === aUntilLevel) {
-        // Stop after aUntilLevel
-        break;
-      }
-    }
-  }
 
 
   /**
-   * This function calls all functions in an array.
-   *
-   * @param {Array.<function()>} aFunctions
-   * @param {Array} aBootstrapArgs - the arguments to apply
+   * Add a startup function to the environment.
    */
-  function callFunctions(aFunctions, aBootstrapArgs) {
-    // process the Array as long as it contains elements
-    while (aFunctions.length > 0) {
-      // The following is either `fnArray.pop()` or `fnArray.shift()`
-      // depending on `sequence`.
-      let f = aFunctions.pop();
-
-      // call the function
-      f.apply(null, aBootstrapArgs);
-      //console.debug("[RPC] function called! (" + fnArray.length +
-      //              " functions left)");
+  Environment.prototype.addStartupFunction = function(aLevel, f) {
+    let self = this;
+    if (self.envState >= ENV_STATES.SHUTTING_DOWN) {
+      // the environment is shutting down or already shut down.
+      return;
+    }
+    if (self.levels["startup"][aLevel].levelState >= LEVEL_STATES.PROCESSING) {
+      // Either the startup functions of the same level as `aLevel` have
+      //        already been processed
+      //    OR  they are currently being processed.
+      //
+      // ==> call the function immediately.
+      f();
+    } else {
+      // the startup process did not reach the function's level yet.
+      //
+      // ==> remember the function.
+      self.levels["startup"][aLevel].functions.push(f);
     }
   };
 
-
   /**
-   * Process a level independently of the environment's states and
-   * independently of the other levels' states.
-   *
-   * @this {Environment}
-   * @param {string} aStartupOrShutdown - either "startup" or "shutdown"
-   * @param {integer} aLevel
+   * Add a shutdown function to the environment.
    */
-  function processLevel(aStartupOrShutdown, aLevel, aBootstrapArgs) {
+  Environment.prototype.addShutdownFunction = function(aLevel, f) {
     let self = this;
+    if (self.levels["shutdown"][aLevel].levelState >= LEVEL_STATES.PROCESSING) {
+      // Either the shutdown functions of the same level as `aLevel` have
+      //        already been processed
+      //    OR  they are currently being processed.
+      //
+      // ==> call the function immediately.
+      f();
+      //console.debug('[RPC] calling shutdown function immediately: "' +
+      //              (f.name || "anonymous") + '" (' + self.name + ')');
+    } else {
+      // The opposite, i.e. the startup process did not reach the function's
+      // level yet.
+      //
+      // ==> remember the function.
+      self.levels["shutdown"][aLevel].functions.push(f);
+    }
+  };
 
-    let levelObj = self.levels[aStartupOrShutdown][aLevel];
 
-    if (levelObj.levelState === LEVEL_STATES.NOT_ENTERED) {
-      levelObj.levelState = LEVEL_STATES.PROCESSING;
+  // have a scope/closure for private functions specific to
+  // startup() and shutdown().
+  (function createMethods_StartupAndShutdown(Environment) {
+    /**
+     * Iterates all levels of either the startup or the shutdown
+     * sequence and calls a function for each level.
+     *
+     * @param {string} aStartupOrShutdown
+     * @param {function()} aFn - the function to call
+     * @param {integer=} aUntilLevel - if specified, iteration stops
+     *     after that level.
+     */
+    function iterateLevels(aStartupOrShutdown, aFn, aUntilLevel=null) {
+      let sequence = BOOTSTRAP[aStartupOrShutdown].levelSequence;
+
+      for (let i = 0, len = sequence.length; i < len; ++i) {
+        // Note: It's necessary to use for(;;) instead of  for(..of..)
+        //       because the order/sequence must be exactly the same as in the
+        //       array.  for(..of..) doesn't guarantee that the elements are
+        //       called in order.
+
+        let level = sequence[i];
+        aFn(level);
+
+        if (level === aUntilLevel) {
+          // Stop after aUntilLevel
+          break;
+        }
+      }
+    }
 
-      if (aStartupOrShutdown === "shutdown") {
-        // shut down all inner environments
-        self.innerEnvs.forEach(function(innerEnv) {
-          innerEnv.shutdown(aBootstrapArgs, aLevel);
-        });
+
+    /**
+     * This function calls all functions in an array.
+     *
+     * @param {Array.<function()>} aFunctions
+     * @param {Array} aBootstrapArgs - the arguments to apply
+     */
+    function callFunctions(aFunctions, aBootstrapArgs) {
+      // process the Array as long as it contains elements
+      while (aFunctions.length > 0) {
+        // The following is either `fnArray.pop()` or `fnArray.shift()`
+        // depending on `sequence`.
+        let f = aFunctions.pop();
+
+        // call the function
+        f.apply(null, aBootstrapArgs);
+        //console.debug("[RPC] function called! (" + fnArray.length +
+        //              " functions left)");
       }
+    };
 
-      callFunctions(levelObj.functions, aBootstrapArgs);
 
-      levelObj.levelState = LEVEL_STATES.FINISHED_PROCESSING;
-    }
-  }
+    /**
+     * Process a level independently of the environment's states and
+     * independently of the other levels' states.
+     *
+     * @this {Environment}
+     * @param {string} aStartupOrShutdown - either "startup" or "shutdown"
+     * @param {integer} aLevel
+     */
+    function processLevel(aStartupOrShutdown, aLevel, aBootstrapArgs) {
+      let self = this;
 
+      let levelObj = self.levels[aStartupOrShutdown][aLevel];
 
-  /**
-   * Iterate levels and call processLevel() for each level.
-   *
-   * @this {Environment}
-   * @param {string} aStartupOrShutdown
-   * @param {Array} aBootstrapArgs
-   * @param {integer=} aUntilLevel
-   */
-  function processLevels(aStartupOrShutdown, aBootstrapArgs, aUntilLevel) {
-    let self = this;
-    iterateLevels(aStartupOrShutdown, function (level) {
-      processLevel.call(self, aStartupOrShutdown, level, aBootstrapArgs);
-    }, aUntilLevel);
-  }
+      if (levelObj.levelState === LEVEL_STATES.NOT_ENTERED) {
+        levelObj.levelState = LEVEL_STATES.PROCESSING;
 
+        if (aStartupOrShutdown === "shutdown") {
+          // shut down all inner environments
+          self.innerEnvs.forEach(function(innerEnv) {
+            innerEnv.shutdown(aBootstrapArgs, aLevel);
+          });
+        }
 
-  /**
-   * Return some information about an environment.
-   *
-   * @param {Environment} env
-   * @return {string}
-   */
-  function getEnvInfo(env) {
-    return "'" + env.name + "' (" + env.uid + ")";
-  }
+        callFunctions(levelObj.functions, aBootstrapArgs);
 
-  // #ifdef LOG_ENVIRONMENT
-  /**
-   * Log some debug information on startup or shutdown.
-   *
-   * @this {Environment}
-   * @param {string} aStartupOrShutdown
-   */
-  function logStartupOrShutdown(aStartupOrShutdown) {
-    let self = this;
-    console.log("[RPC] " + aStartupOrShutdown + ": " + getEnvInfo(self) + "." +
-                (self.outerEnv ?
-                 " OuterEnv is " + getEnvInfo(self.outerEnv) + "." :
-                 " No OuterEnv."));
-  }
-  // #endif
+        levelObj.levelState = LEVEL_STATES.FINISHED_PROCESSING;
+      }
+    }
 
-  /**
-   * Actual body of the functions startup() and shutdown().
-   *
-   * @this {Environment}
-   * @param {string} aStartupOrShutdown - either "startup" or "shutdown"
-   * @param {Array} aBootstrapArgs
-   * @param {integer=} aUntilLevel - The level after which the startup
-   *     (or shutdown) processing is stopped.
-   */
-  function bootstrap(aStartupOrShutdown,
-                     aBootstrapArgs,
-                     aUntilLevel=BOOTSTRAP[aStartupOrShutdown].lastLevel) {
-    let self = this;
 
-    let {
-      lastLevel,
-      envStates,
-      functions
-    } = getBootstrapMetadata(aStartupOrShutdown);
+    /**
+     * Iterate levels and call processLevel() for each level.
+     *
+     * @this {Environment}
+     * @param {string} aStartupOrShutdown
+     * @param {Array} aBootstrapArgs
+     * @param {integer=} aUntilLevel
+     */
+    function processLevels(aStartupOrShutdown, aBootstrapArgs, aUntilLevel) {
+      let self = this;
+      iterateLevels(aStartupOrShutdown, function (level) {
+        processLevel.call(self, aStartupOrShutdown, level, aBootstrapArgs);
+      }, aUntilLevel);
+    }
 
-    if (self.envState === envStates["beforeProcessing"]) {
-      // #ifdef LOG_ENVIRONMENT
-      logStartupOrShutdown.call(self, aStartupOrShutdown);
-      // #endif
-      functions["beforeProcessing"].call(self);
 
-      self.envState = envStates["duringProcessing"];
+    /**
+     * Return some information about an environment.
+     *
+     * @param {Environment} env
+     * @return {string}
+     */
+    function getEnvInfo(env) {
+      return "'" + env.name + "' (" + env.uid + ")";
+    }
+
+    // #ifdef LOG_ENVIRONMENT
+    /**
+     * Log some debug information on startup or shutdown.
+     *
+     * @this {Environment}
+     * @param {string} aStartupOrShutdown
+     */
+    function logStartupOrShutdown(aStartupOrShutdown) {
+      let self = this;
+      console.log("[RPC] " + aStartupOrShutdown + ": " + getEnvInfo(self) + "." +
+                  (self.outerEnv ?
+                   " OuterEnv is " + getEnvInfo(self.outerEnv) + "." :
+                   " No OuterEnv."));
     }
+    // #endif
+
+    /**
+     * Actual body of the functions startup() and shutdown().
+     *
+     * @this {Environment}
+     * @param {string} aStartupOrShutdown - either "startup" or "shutdown"
+     * @param {Array} aBootstrapArgs
+     * @param {integer=} aUntilLevel - The level after which the startup
+     *     (or shutdown) processing is stopped.
+     */
+    function bootstrap(aStartupOrShutdown,
+                       aBootstrapArgs,
+                       aUntilLevel=BOOTSTRAP[aStartupOrShutdown].lastLevel) {
+      let self = this;
+
+      let {
+        lastLevel,
+        envStates,
+        functions
+      } = getBootstrapMetadata(aStartupOrShutdown);
+
+      if (self.envState === envStates["beforeProcessing"]) {
+        // #ifdef LOG_ENVIRONMENT
+        logStartupOrShutdown.call(self, aStartupOrShutdown);
+        // #endif
+        functions["beforeProcessing"].call(self);
+
+        self.envState = envStates["duringProcessing"];
+      }
 
-    if (self.envState === envStates["duringProcessing"]) {
-      processLevels.call(self, aStartupOrShutdown, aBootstrapArgs, aUntilLevel);
+      if (self.envState === envStates["duringProcessing"]) {
+        processLevels.call(self, aStartupOrShutdown, aBootstrapArgs,
+            aUntilLevel);
 
-      if (aUntilLevel === lastLevel) {
-        self.envState = envStates["afterProcessing"];
-        functions["afterProcessing"].call(self);
+        if (aUntilLevel === lastLevel) {
+          self.envState = envStates["afterProcessing"];
+          functions["afterProcessing"].call(self);
+        }
       }
-    }
-  };
+    };
 
-  Environment.prototype.startup = function(aBootstrapArgs, aUntilLevel) {
-    let self = this;
-    bootstrap.call(self, "startup", aBootstrapArgs, aUntilLevel);
-  };
+    Environment.prototype.startup = function(aBootstrapArgs, aUntilLevel) {
+      let self = this;
+      bootstrap.call(self, "startup", aBootstrapArgs, aUntilLevel);
+    };
+
+    Environment.prototype.shutdown = function(aBootstrapArgs, aUntilLevel) {
+      let self = this;
+      bootstrap.call(self, "shutdown", aBootstrapArgs, aUntilLevel);
+    };
 
-  Environment.prototype.shutdown = function(aBootstrapArgs, aUntilLevel) {
+  }(Environment));
+
+  /**
+   * Tell the Environment to shut down when an EventTarget's
+   * "unload" event occurres.
+   *
+   * @param {EventTarget} aEventTarget - an object having the functions
+   *     addEventListener() and removeEventListener().
+   */
+  Environment.prototype.shutdownOnUnload = function(aEventTarget) {
     let self = this;
-    bootstrap.call(self, "shutdown", aBootstrapArgs, aUntilLevel);
+    self.elManager.addListener(aEventTarget, "unload", function() {
+      // #ifdef LOG_ENVIRONMENT
+      console.log("[RPC] an EventTarget's `unload` function has been called. " +
+                  'Going to shut down Environment "' + self.name + '"');
+      // #endif
+      self.shutdown();
+    });
   };
 
-})(Environment);
+  return Environment;
+}());
 
-/**
- * Tell the Environment to shut down when an EventTarget's
- * "unload" event occurres.
- *
- * @param {EventTarget} aEventTarget - an object having the functions
- *     addEventListener() and removeEventListener().
- */
-Environment.prototype.shutdownOnUnload = function(aEventTarget) {
-  let self = this;
-  self.elManager.addListener(aEventTarget, "unload", function() {
-    // #ifdef LOG_ENVIRONMENT
-    console.log("[RPC] an EventTarget's `unload` function has been called. " +
-                'Going to shut down Environment "' + self.name + '"');
-    // #endif
-    self.shutdown();
-  });
-};
+//==============================================================================
+// ProcessEnvironment
+//==============================================================================
 
+var ProcessEnvironment = (function () {
+  let scope = {Environment};
+  Services.scriptloader.loadSubScript(
+      "chrome://rpcontinued/content/lib/environment.process.js", scope);
+  return scope.ProcessEnvironment;
+}());
 
-/**
- * @constructor
- * @extends {Environment}
- * @param {ContentFrameMessageManager} aMM
- * @param {string=} aName - the environment's name, passed to the superclass.
- */
-function FrameScriptEnvironment(aMM, aName="frame script environment") {
-  let self = this;
+//==============================================================================
+// FrameScriptEnvironment
+//==============================================================================
 
-  // The outer Environment will be either ChildProcessEnvironment
-  // or ParentProcessEnvironment.
-  let _outerEnv = ProcessEnvironment;
+var FrameScriptEnvironment = (function () {
+  /**
+   * @constructor
+   * @extends {Environment}
+   * @param {ContentFrameMessageManager} aMM
+   * @param {string=} aName - the environment's name, passed to the superclass.
+   */
+  function FrameScriptEnvironment(aMM, aName="frame script environment") {
+    let self = this;
 
-  Environment.call(self, _outerEnv, aName);
+    // The outer Environment will be either ChildProcessEnvironment
+    // or ParentProcessEnvironment.
+    let _outerEnv = ProcessEnvironment;
 
-  self.mm = aMM;
+    Environment.call(self, _outerEnv, aName);
 
-  self.addStartupFunction(LEVELS.INTERFACE, function() {
-    // shut down the framescript on the message manager's
-    // `unload`. That event will occur when the browsing context
-    // (e.g. the tab) has been closed.
-    self.shutdownOnUnload(self.mm);
-  });
+    self.mm = aMM;
 
-  // a "MessageListener"-Manager for this environment
-  XPCOMUtils.defineLazyGetter(self, "mlManager", function() {
-    return new ManagerForMessageListeners(self, self.mm);
-  });
-}
-FrameScriptEnvironment.prototype = Object.create(Environment.prototype);
-FrameScriptEnvironment.prototype.constructor = Environment;
+    self.addStartupFunction(Environment.LEVELS.INTERFACE, function() {
+      // shut down the framescript on the message manager's
+      // `unload`. That event will occur when the browsing context
+      // (e.g. the tab) has been closed.
+      self.shutdownOnUnload(self.mm);
+    });
 
+    // a "MessageListener"-Manager for this environment
+    XPCOMUtils.defineLazyGetter(self, "mlManager", function() {
+      return new ManagerForMessageListeners(self, self.mm);
+    });
+  }
+  FrameScriptEnvironment.prototype = Object.create(Environment.prototype);
+  FrameScriptEnvironment.prototype.constructor = Environment;
 
-// Load the "ProcessEnvironment"
-Services.scriptloader.loadSubScript("chrome://rpcontinued/content/" +
-                                    "lib/environment.process.js");
+  return FrameScriptEnvironment;
+}());
diff --git a/src/content/lib/environment.process.js b/src/content/lib/environment.process.js
index 8de0269..d0b7ebc 100644
--- a/src/content/lib/environment.process.js
+++ b/src/content/lib/environment.process.js
@@ -21,12 +21,25 @@
  * ***** END LICENSE BLOCK *****
  */
 
+/* global Components */
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
-// ProcessEnvironment is either a ParentProcessEnvironment or
-// a ChildProcessEnvironment.
-var ProcessEnvironment = (function() {
+/* exported ProcessEnvironment */
+/* global Environment */
+
+let {ScriptLoader} = Cu.import(
+    "chrome://rpcontinued/content/lib/script-loader.jsm");
+let {C} = ScriptLoader.importModule("lib/utils/constants");
 
+//==============================================================================
+// ProcessEnvironment
+//==============================================================================
 
+/**
+ * ProcessEnvironment is either a ParentProcessEnvironment or
+ * a ChildProcessEnvironment.
+ */
+var ProcessEnvironment = (function() {
   // determine if this is the main process
   let isMainProcess = (function isMainProcess() {
     let xulRuntime = Cc["@mozilla.org/xre/app-info;1"]
@@ -194,7 +207,7 @@ var ProcessEnvironment = (function() {
 
   ChildProcessEnvironment.prototype.registerInnerEnvironment = function(aEnv) {
     let self = this;
-    if (self.envState === ENV_STATES.NOT_STARTED) {
+    if (self.envState === Environment.ENV_STATES.NOT_STARTED) {
       // The child Process Environment needs to start up when
       // the first framescript in that child is loading.
       //console.debug("[RPC] Going to start up Child Process Environment.");
@@ -213,4 +226,4 @@ var ProcessEnvironment = (function() {
     return new ChildProcessEnvironment();
   }
 
-})();
+}());

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/requestpolicy.git



More information about the Pkg-mozext-commits mailing list