[Pkg-mozext-commits] [requestpolicy] 96/280: don't import any modules in ScriptLoader anymore
David Prévot
taffit at moszumanska.debian.org
Sat May 2 20:30:05 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 e9fccae040c0866797dbcc4ca432e240bd6c6332
Author: Martin Kimmerle <dev at 256k.de>
Date: Thu Jan 1 14:00:20 2015 +0100
don't import any modules in ScriptLoader anymore
---
src/content/lib/logger.jsm | 60 ++++++++++++++++++++++++---------------
src/content/lib/prefs.jsm | 3 +-
src/content/lib/script-loader.jsm | 38 +++++++++----------------
3 files changed, 52 insertions(+), 49 deletions(-)
diff --git a/src/content/lib/logger.jsm b/src/content/lib/logger.jsm
index f7e5f71..f06ba3b 100644
--- a/src/content/lib/logger.jsm
+++ b/src/content/lib/logger.jsm
@@ -29,10 +29,8 @@ let EXPORTED_SYMBOLS = ["Logger"];
Cu.import("resource://gre/modules/Services.jsm");
-
-// We can't import the ScriptLoader when the Logger is loaded, because the
-// Logger gets imported by the ScriptLoader itself on startup.
-let ScriptLoader = null;
+Cu.import("chrome://requestpolicy/content/lib/script-loader.jsm");
+ScriptLoader.importModules(["bootstrap-manager"], this)
/**
@@ -107,40 +105,50 @@ let Logger = (function() {
"yet, so logging is still enabled.\n");
}
+ /**
+ * init() is called by doLog() until initialization was successful.
+ * For the case that nothing is logged at all, init is registered as a
+ * startup-function.
+ */
function init() {
- if (!ScriptLoader) {
- // try to import the ScriptLoader
- try {
- // We can't import the ScriptLoader when the Logger is loaded, because
- // the Logger gets imported by the ScriptLoader itself on startup.
- let mod = {};
- Cu.import("chrome://requestpolicy/content/lib/script-loader.jsm", mod);
- ScriptLoader = mod.ScriptLoader;
- } catch (e) {
- ScriptLoader = null;
- displayMessageNotInitiallized();
- return;
- }
+ if (initialized === true) {
+ // don't initialize several times
+ return;
}
+
+ // Try to get rpPrefBranch.
+ // That pref branch might not be available when init() is called, e.g. when
+ // prefs.jsm logs something on initialization.
let prefScope = ScriptLoader.importModule("prefs");
if (!("rpPrefBranch" in prefScope)) {
displayMessageNotInitiallized();
+ // cancel init()
return;
}
rpPrefBranch = prefScope.rpPrefBranch;
- initialized = true;
rpPrefBranch.addObserver("log", prefObserver, false);
updateLoggingSettings();
+
+ // don't call init() anymore when doLog() is called
+ doLog = log;
+
+ initialized = true;
}
+ BootstrapManager.registerStartupFunction(init);
- function doLog(aLevel, aType, aMessage, aError) {
- if (!initialized) {
- init();
- }
+ /**
+ * This function will be called in case Logger isn't fully initialized yet.
+ */
+ let initialLog = function() {
+ init();
+ log.apply(this, arguments);
+ };
+
+ let log = function(aLevel, aType, aMessage, aError) {
if (enabled && aLevel >= level && types & aType) {
let levelName = self._LEVEL_NAMES[aLevel.toString()];
let typeName = self._TYPE_NAMES[aType.toString()];
@@ -165,7 +173,13 @@ let Logger = (function() {
}
}
}
- }
+ };
+
+ /**
+ * Initially call initialLog() on doLog().
+ * After initialization it will be log().
+ */
+ let doLog = initialLog;
diff --git a/src/content/lib/prefs.jsm b/src/content/lib/prefs.jsm
index 6e2fba9..ade49a7 100644
--- a/src/content/lib/prefs.jsm
+++ b/src/content/lib/prefs.jsm
@@ -49,7 +49,6 @@ let rootPrefBranch = Services.prefs.getBranch("")
-
let Prefs = (function() {
let self = {};
@@ -91,7 +90,7 @@ let Prefs = (function() {
/**
* Take necessary actions when preferences are updated.
*
- * @paramString{} prefName name of the preference that was updated.
+ * @param {String} prefName name of the preference that was updated.
*/
function prefChanged(prefName) {
switch (prefName) {
diff --git a/src/content/lib/script-loader.jsm b/src/content/lib/script-loader.jsm
index b31c17f..3f7e453 100644
--- a/src/content/lib/script-loader.jsm
+++ b/src/content/lib/script-loader.jsm
@@ -27,43 +27,33 @@ const Cu = Components.utils;
let EXPORTED_SYMBOLS = ["ScriptLoader"];
+// import some modules
+// NOTICE: This file should NOT import any of RP's modules when it is loaded!
+// Doing so would be a bad practice, and might produce import() loops
+// when the module to be imported wants to import ScriptLoader.
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
const rpChromeContentURI = 'chrome://requestpolicy/content/';
function getModuleURI(id) {
return rpChromeContentURI + "lib/" + id + ".jsm";
}
-let loggerURI = getModuleURI("logger");
-Cu.import(loggerURI);
-
-/*
-// remove
-
-might be helpful. This will import an arbitrary module into a
-singleton object, which it returns. If the argument is not an
-absolute path, the module is imported relative to the caller's
-filename.
-
-function module(uri) {
- if (!/^[a-z-]+:/.exec(uri)) {
- uri = /([^ ]+\/)[^\/]+$/.exec(Components.stack.caller.filename)[1] + uri + ".jsm";
- }
-
- let obj = {};
- Components.utils.import(uri, obj);
- return obj;
+/**
+ * If the ScriptLoader catches an Exception, it will be a severe error.
+ */
+function logSevereError(msg, stack) {
+ dump("[RequestPolicy] [SEVERE] [ERROR] " + msg +
+ (stack ? ", stack was: " + stack : ""));
}
-*/
+
let ScriptLoader = (function() {
- // the logger is always imported
let importedModuleURIs = {};
- importedModuleURIs[loggerURI] = true;
let scopes = {};
@@ -118,8 +108,8 @@ let ScriptLoader = (function() {
delete modulesCurrentlyBeingImported[moduleID];
}
} catch (e) {
- Logger.severeError("Failed to import module \"" + moduleID + "\": " + e,
- e);
+ logSevereError("Failed to import module \"" + moduleID + "\": " + e,
+ e.stack);
}
return scope;
},
--
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