[Pkg-mozext-commits] [requestpolicy] 50/280: BootstrapManager: introduce startup/shutdown function stacks

David Prévot taffit at moszumanska.debian.org
Sat May 2 20:29:58 UTC 2015


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

taffit pushed a commit to branch master
in repository requestpolicy.

commit cfbd75808cc621e86db17216c7e1fe5267ba4f49
Author: Martin Kimmerle <dev at 256k.de>
Date:   Fri Dec 5 07:58:10 2014 +0100

    BootstrapManager: introduce startup/shutdown function stacks
    
    From now on any module registers its startup/shutdown function(s) by itself,
    so the BootstrapManager doesn't have to care about this anymore.
---
 src/bootstrap.js                          |   4 +-
 src/content/lib/about-uri.jsm             |  40 ++++----
 src/content/lib/bootstrap-manager.jsm     | 127 +++++++++---------------
 src/content/lib/content-policy.jsm        | 157 +++++++++++++++---------------
 src/content/lib/requestpolicy-service.jsm |  62 ++++++------
 src/content/lib/script-loader.jsm         |   7 ++
 src/content/lib/window-manager.jsm        |  11 ++-
 7 files changed, 198 insertions(+), 210 deletions(-)

diff --git a/src/bootstrap.js b/src/bootstrap.js
index d83b605..3289022 100644
--- a/src/bootstrap.js
+++ b/src/bootstrap.js
@@ -61,9 +61,9 @@ function shutdown(data, reason) {
 }
 
 function install(data, reason) {
-  // do not call managers, as the addon might be not activated
+  // note: the addon might be not activated
 }
 
 function uninstall(data, reason) {
-  // do not call managers, as the addon might be not activated
+  // note: the addon might be not activated
 }
diff --git a/src/content/lib/about-uri.jsm b/src/content/lib/about-uri.jsm
index 14480fa..614f7b4 100644
--- a/src/content/lib/about-uri.jsm
+++ b/src/content/lib/about-uri.jsm
@@ -31,6 +31,9 @@ Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 let EXPORTED_SYMBOLS = ["AboutRequestPolicy"];
 
+Cu.import("chrome://requestpolicy/content/lib/script-loader.jsm");
+ScriptLoader.importModule("bootstrap-manager", this);
+
 var filenames = {
   "basicprefs": "basicprefs.html",
   "advancedprefs": "advancedprefs.html",
@@ -74,23 +77,6 @@ let AboutRequestPolicy = (function() {
       return channel;
     },
 
-
-
-    startup: function() {
-      Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
-          .registerFactory(self.classID, self.classDescription, self.contractID,
-              self);
-    },
-
-    shutdown: function() {
-      let registrar = Components.manager
-          .QueryInterface(Ci.nsIComponentRegistrar);
-      // This needs to run asynchronously, see bug 753687
-      Utils.runAsync(function() {
-        registrar.unregisterFactory(self.classID, self);
-      });
-    },
-
     //
     // nsIFactory interface implementation
     //
@@ -101,6 +87,24 @@ let AboutRequestPolicy = (function() {
       }
       return self.QueryInterface(iid);
     }
-  }
+  };
+
+
+
+  BootstrapManager.registerStartupFunction(function() {
+    Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
+        .registerFactory(self.classID, self.classDescription, self.contractID,
+            self);
+  });
+
+  BootstrapManager.registerShutdownFunction(function() {
+    let registrar = Components.manager
+        .QueryInterface(Ci.nsIComponentRegistrar);
+    // This needs to run asynchronously, see bug 753687
+    Utils.runAsync(function() {
+      registrar.unregisterFactory(self.classID, self);
+    });
+  });
+
   return self;
 }());
diff --git a/src/content/lib/bootstrap-manager.jsm b/src/content/lib/bootstrap-manager.jsm
index 19c5941..c97d0b1 100644
--- a/src/content/lib/bootstrap-manager.jsm
+++ b/src/content/lib/bootstrap-manager.jsm
@@ -31,114 +31,83 @@ Cu.import("resource://gre/modules/Services.jsm");
 
 let globalScope = this;
 let scriptLoaderURI = "chrome://requestpolicy/content/lib/script-loader.jsm";
-let ScriptLoader = null;
-let Logger = null;
-let rpService = null;
-let WindowManager = null;
 // TODO: implement. see https://github.com/RequestPolicyContinued/requestpolicy/issues/486
 //let SevereErrorHandler = {};
 
 
 
-
 let BootstrapManager = (function() {
   let self = {};
 
-  let managers = {
-    // id     :   object name of the manager
-    'requestpolicy-service': 'rpService',
-    'window-manager': 'rpWindowManager',
-    'about-uri': 'AboutRequestPolicy'
-  };
 
-  //let startupFunctions = [];
-  //let shutdownFunctions = [];
+  let startupFunctionStack = [];
+  let shutdownFunctionStack = [];
+  //let installFunctionStack = [];
+  //let uninstallFunctionStack = [];
 
   /**
-   * this function calls another function with functionName
+   * The functions in one of the arrays above will be called. Not that the list
+   * itself might get even more entries while it is being called; therefore
+   * pop() is used.
    */
-  function callBootstrapFunction(managerID, functionName, data, reason) {
-    let scope = {};
-    let managerName = managers[managerID];
-    //let manager = ScriptLoader.require(managerID)[managerName];
-    //let manager = ScriptLoader.importModule(managerID, scope)[managerName];
-    let manager = globalScope[managerName];
-
-    // if manager (e.g. "rpService") doesn't exist or doesn't have the function to be
-    // called, just skip without an error
-    if (manager && manager[functionName] &&
-        (typeof manager[functionName]) == 'function') {
-      manager[functionName](data, reason);
+  let callBootstrapFunctions = function(functions, data, reason) {
+    // pop the topmost function as long as there is one.
+    //
+    // The combination of push() and pop() leads to FILO (first in, last out)
+    // for the shutdown process. In other words, it's a stack
+    for (let f = functions.pop(); !!f; f = functions.pop()) {
+      f(data, reason);
     }
   };
-  function forEachManager(functionToCall, args) {
-    for (let managerID in managers) {
-      if (!managers.hasOwnProperty(managerID)) {
-        continue;
-      }
-      try {
-        let functionArgs = [managerID].concat(args);
-        functionToCall.apply(null, functionArgs);
-      } catch (e) {
-        Logger.severeError("error catched in bootstrap script: " + e, e);
-      }
-    }
-  };
-
-
-
+  let callStartupFunctions = callBootstrapFunctions.bind(this, startupFunctionStack);
+  let callShutdownFunctions = callBootstrapFunctions.bind(this, shutdownFunctionStack);
+  //let callInstallFunctions = callBootstrapFunctions.bind(this, installFunctionStack);
+  //let callUninstallFunctions = callBootstrapFunctions.bind(this, uninstallFunctionStack);
 
+  /**
+   * This set of functions can be used for adding startup/shutdown functions.
+   * Note: the first startup function to be executed will import all modules so
+   * that all subsequent startup-functions get called *after* all modules have
+   * been loaded.
+   */
+  let registerFunction = function(target, f) {
+    target.push(f);
+  };
+  self.registerStartupFunction = registerFunction.bind(this, startupFunctionStack);
+  self.registerShutdownFunction = registerFunction.bind(this, shutdownFunctionStack);
+  //self.registerInstallFunction = registerFunction.bind(this, installFunctionStack);
+  //self.registerUninstallFunction = registerFunction.bind(this, uninstallFunctionStack);
 
 
 
-  function importScriptLoader() {
+  /**
+   * Import main modules on startup. This will be the first function that will
+   * be called, and as the modules depend on all other modules recursively, all
+   * modules will be loaded already when the second startup function gets
+   * called.
+   */
+  self.registerStartupFunction(function() {
+    // manually load the ScriptLoader
     Cu.import(scriptLoaderURI, globalScope);
-  }
-
-  function init() {
-    importScriptLoader();
-    ScriptLoader.importModule("logger", globalScope);
-    ScriptLoader.importModule("requestpolicy-service", globalScope);
-    ScriptLoader.importModule("window-manager", globalScope);
-    ScriptLoader.importModule("about-uri", globalScope);
-  }
+    ScriptLoader.importModules(["logger", "requestpolicy-service",
+                                "window-manager", "about-uri"], globalScope);
+  });
 
-  function finish() {
+  self.registerShutdownFunction(function() {
     // HACK WARNING: The Addon Manager does not properly clear all addon
     //               related caches on update; in order to fully update
     //               images and locales, their caches need clearing here.
     Services.obs.notifyObservers(null, "chrome-flush-caches", null);
 
-    ScriptLoader.unloadAllLibraries();
-    ScriptLoader.unloadAllModules();
-
+    // manually unload the ScriptLoader
     Cu.unload(scriptLoaderURI);
-    ScriptLoader = null;
-    Logger = null;
-    rpService = null;
-    WindowManager = null;
-  }
-
-  function startupManagers(data, reason) {
-    // call the startup function of all managers
-    forEachManager(callBootstrapFunction, ['startup', data, reason]);
-  }
+  });
 
-  function shutdownManagers(data, reason) {
-    forEachManager(callBootstrapFunction, ['shutdown', data, reason]);
-  }
 
 
-
-  self.startup = function(data, reason) {
-    init();
-    startupManagers(data, reason);
-  };
-
-  self.shutdown = function(data, reason) {
-    shutdownManagers(data, reason);
-    finish();
-  };
+  // when startup() and shutdown() are called, simply call all
+  self.startup = callStartupFunctions.bind(this);
+  self.shutdown = callShutdownFunctions.bind(this);
 
   return self;
 }());
diff --git a/src/content/lib/content-policy.jsm b/src/content/lib/content-policy.jsm
index 0dcf7d5..5ce6beb 100644
--- a/src/content/lib/content-policy.jsm
+++ b/src/content/lib/content-policy.jsm
@@ -38,7 +38,8 @@ ScriptLoader.importModules([
   "logger",
   "request",
   "utils",
-  "request-processor"
+  "request-processor",
+  "bootstrap-manager"
 ], globalScope);
 
 
@@ -49,81 +50,85 @@ let PolicyImplementation = (function() {
   let self = {
     classDescription: "RequestPolicy JavaScript XPCOM Component",
     classID:          Components.ID("{14027e96-1afb-4066-8846-e6c89b5faf3b}"),
-    contractID:       "@requestpolicy.com/requestpolicy-service;1",
-
-    /**
-     * Registers the content policy on startup.
-     */
-    init: function() {
-      Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
-          .registerFactory(self.classID, self.classDescription, self.contractID,
-              self);
-
-      let catMan = Utils.categoryManager;
-      for each (let category in xpcom_categories) {
-        catMan.addCategoryEntry(category, self.contractID, self.contractID, false,
-            true);
-      }
-
-      if (!self.mimeService) {
-        // self.rejectCode = typeof(/ /) == "object" ? -4 : -3;
-        self.rejectCode = Ci.nsIContentPolicy.REJECT_SERVER;
-        self.mimeService =
-            Cc['@mozilla.org/uriloader/external-helper-app-service;1']
-            .getService(Ci.nsIMIMEService);
-      }
-    },
-
-    shutdown: function() {
-      let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
-      let catMan = Utils.categoryManager;
-
-      for (let category in xpcom_categories) {
-        catMan.deleteCategoryEntry(category, self.contractID, false);
-      }
-
-      // This needs to run asynchronously, see bug 753687
-      Utils.runAsync(function() {
-        registrar.unregisterFactory(self.classID, self);
-      });
-    },
-
-    //
-    // nsISupports interface implementation
-    //
-
-    QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIObserver,
-      Ci.nsIFactory, Ci.nsISupportsWeakReference]),
-
-    //
-    // nsIContentPolicy interface implementation
-    //
-
-    // https://developer.mozilla.org/en/nsIContentPolicy
-
-    shouldLoad: function(aContentType, aContentLocation, aRequestOrigin,
-        aContext, aMimeTypeGuess, aExtra, aRequestPrincipal) {
-      var request = new NormalRequest(
-          aContentType, aContentLocation, aRequestOrigin, aContext,
-          aMimeTypeGuess, aExtra, aRequestPrincipal);
-      return RequestProcessor.process(request);
-      // TODO: implement the following
-      // return request.shouldLoad(aContentType, aContentLocation, aRequestOrigin,
-      //     aContext, aMimeTypeGuess, aExtra, aRequestPrincipal);
-    },
-
-    shouldProcess: (() => CP_OK),
-
-    //
-    // nsIFactory interface implementation
-    //
-
-    createInstance: function(outer, iid) {
-      if (outer) {
-        throw Cr.NS_ERROR_NO_AGGREGATION;
-      }
-      return self.QueryInterface(iid);
+    contractID:       "@requestpolicy.com/requestpolicy-service;1"
+  };
+
+  /**
+   * Registers the content policy on startup.
+   */
+  self.init = function() {
+    Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
+                      .registerFactory(self.classID, self.classDescription,
+                                       self.contractID, self);
+
+    let catMan = Utils.categoryManager;
+    for each (let category in xpcom_categories) {
+      catMan.addCategoryEntry(category, self.contractID, self.contractID, false,
+          true);
     }
-  }
+
+    if (!self.mimeService) {
+      // self.rejectCode = typeof(/ /) == "object" ? -4 : -3;
+      self.rejectCode = Ci.nsIContentPolicy.REJECT_SERVER;
+      self.mimeService =
+          Cc['@mozilla.org/uriloader/external-helper-app-service;1']
+          .getService(Ci.nsIMIMEService);
+    }
+  };
+
+
+  BootstrapManager.registerShutdownFunction(function() {
+    let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
+    let catMan = Utils.categoryManager;
+
+    for (let category in xpcom_categories) {
+      catMan.deleteCategoryEntry(category, self.contractID, false);
+    }
+
+    // This needs to run asynchronously, see bug 753687
+    Utils.runAsync(function() {
+      registrar.unregisterFactory(self.classID, self);
+    });
+  });
+
+  //
+  // nsISupports interface implementation
+  //
+
+  self.QueryInterface = XPCOMUtils.generateQI([Ci.nsIContentPolicy,
+                                               Ci.nsIObserver,
+                                               Ci.nsIFactory,
+                                               Ci.nsISupportsWeakReference]);
+
+  //
+  // nsIContentPolicy interface implementation
+  //
+
+  // https://developer.mozilla.org/en/nsIContentPolicy
+
+  self.shouldLoad = function(aContentType, aContentLocation, aRequestOrigin,
+      aContext, aMimeTypeGuess, aExtra, aRequestPrincipal) {
+    var request = new NormalRequest(
+        aContentType, aContentLocation, aRequestOrigin, aContext,
+        aMimeTypeGuess, aExtra, aRequestPrincipal);
+    return RequestProcessor.process(request);
+    // TODO: implement the following
+    // return request.shouldLoad(aContentType, aContentLocation, aRequestOrigin,
+    //     aContext, aMimeTypeGuess, aExtra, aRequestPrincipal);
+  };
+
+  self.shouldProcess = (() => CP_OK);
+
+  //
+  // nsIFactory interface implementation
+  //
+
+  self.createInstance = function(outer, iid) {
+    if (outer) {
+      throw Cr.NS_ERROR_NO_AGGREGATION;
+    }
+    return self.QueryInterface(iid);
+  };
+
   return self;
 }());
diff --git a/src/content/lib/requestpolicy-service.jsm b/src/content/lib/requestpolicy-service.jsm
index 496e2be..83c6d06 100644
--- a/src/content/lib/requestpolicy-service.jsm
+++ b/src/content/lib/requestpolicy-service.jsm
@@ -43,7 +43,8 @@ ScriptLoader.importModules([
   "subscription",
   "utils",
   "content-policy",
-  "constants"
+  "constants",
+  "bootstrap-manager"
 ], this);
 
 
@@ -444,6 +445,36 @@ let rpService = (function() {
 
 
 
+  // /////////////////////////////////////////////////////////////////////////
+  // Bootstrap functions
+  // /////////////////////////////////////////////////////////////////////////
+
+  BootstrapManager.registerStartupFunction(function() {
+    init();
+
+    loadConfigAndRules();
+    // Detect other installed extensions and the current application and do
+    // what is needed to allow their requests.
+    initializeExtensionCompatibility();
+    initializeApplicationCompatibility();
+  });
+
+  BootstrapManager.registerShutdownFunction(function(data, reason) {
+    if (reason == ADDON_DISABLE || reason == ADDON_UNINSTALL) {
+      handleUninstallOrDisable();
+    }
+    unregister();
+    PolicyImplementation.shutdown(data, reason);
+    rpServiceInitialized = false;
+  });
+
+  //BootstrapManager.registerUninstallFunction(function(data, reason) {
+  //  handleUninstallOrDisable();
+  //});
+
+
+
+
 
 
   self = {
@@ -463,35 +494,6 @@ let rpService = (function() {
 
 
 
-    // /////////////////////////////////////////////////////////////////////////
-    // Bootstrap methods
-    // /////////////////////////////////////////////////////////////////////////
-
-    startup: function() {
-      init();
-
-      loadConfigAndRules();
-      // Detect other installed extensions and the current application and do
-      // what is needed to allow their requests.
-      initializeExtensionCompatibility();
-      initializeApplicationCompatibility();
-    },
-    shutdown: function(data, reason) {
-      if (reason == ADDON_DISABLE || reason == ADDON_UNINSTALL) {
-        handleUninstallOrDisable();
-      }
-      unregister();
-      PolicyImplementation.shutdown(data, reason);
-      rpServiceInitialized = false;
-    },
-    install: function(data, reason) {
-    },
-    uninstall: function(data, reason) {
-      handleUninstallOrDisable();
-    },
-
-
-
 
     // /////////////////////////////////////////////////////////////////////////
     // nsIRequestPolicy interface
diff --git a/src/content/lib/script-loader.jsm b/src/content/lib/script-loader.jsm
index c60e938..4138dce 100644
--- a/src/content/lib/script-loader.jsm
+++ b/src/content/lib/script-loader.jsm
@@ -246,3 +246,10 @@ let ScriptLoader = (function() {
 
   return self;
 }());
+
+let {BootstrapManager} = ScriptLoader.importModule("bootstrap-manager");
+
+BootstrapManager.registerShutdownFunction(function() {
+  ScriptLoader.unloadAllLibraries();
+  ScriptLoader.unloadAllModules();
+});
diff --git a/src/content/lib/window-manager.jsm b/src/content/lib/window-manager.jsm
index 0771845..875dd65 100644
--- a/src/content/lib/window-manager.jsm
+++ b/src/content/lib/window-manager.jsm
@@ -38,7 +38,8 @@ let rpWindowManager = (function(self) {
   ScriptLoader.importModules([
     "utils",
     "xul-utils",
-    "constants"
+    "constants",
+    "bootstrap-manager"
   ], globalScope);
 
   let styleSheets = [
@@ -125,19 +126,19 @@ let rpWindowManager = (function(self) {
 
 
 
-  self.startup = function(data, reason) {
+  BootstrapManager.registerStartupFunction(function(data, reason) {
     forEachOpenWindow(loadIntoWindow);
     Services.wm.addListener(WindowListener);
 
     loadStyleSheets();
-  };
+  });
 
-  self.shutdown = function() {
+  BootstrapManager.registerShutdownFunction(function() {
     forEachOpenWindow(unloadFromWindow);
     Services.wm.removeListener(WindowListener);
 
     unloadStyleSheets();
-  };
+  });
 
 
 

-- 
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