Skip to content

Commit

Permalink
Refactor code: cleanup startup/shutdown mess for some modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
patwonder committed Feb 10, 2015
1 parent 8e7fbde commit b07a628
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 108 deletions.
16 changes: 1 addition & 15 deletions extension/modules/ABPObserver.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ let ABPObserver = {

_abpBranch: null,

/**
* Whether init() has been called
*/
_isInitCalled: false,

/**
* Whether there's any pending updateState calls during filter loading
*/
Expand Down Expand Up @@ -236,14 +231,6 @@ let ABPObserver = {
*/
lazyStartup: function()
{
this.init();
},

init: function()
{
if (this._isInitCalled) return;
this._isInitCalled = true;

UtilsPluginManager.fireAfterInit(function()
{
// Apply a retry sequence to avoid the reported "Not Detected" bug
Expand All @@ -260,7 +247,6 @@ let ABPObserver = {

shutdown: function()
{
if (!this._isInitCalled) return;
UtilsPluginManager.fireAfterInit(function()
{
this._listeners = [];
Expand Down Expand Up @@ -677,9 +663,9 @@ let ABPObserver = {
if (this._clearTimer || this._cleared) return;
this._clearTimer = Utils.runAsyncTimeout(function()
{
UtilsPluginManager.getPlugin().ABPClear();
this._clearTimer = null;
this._cleared = true;
UtilsPluginManager.getPlugin().ABPClear();
Utils.LOG("[ABP] Cleared.");
}, this, 60000);
Utils.LOG("[ABP] Scheduled to clear in 60 seconds.");
Expand Down
11 changes: 7 additions & 4 deletions extension/modules/Bootstrap.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ let defaultModules = [
baseURL.spec + "UtilsPluginManager.jsm",
baseURL.spec + "ABPObserver.jsm",
baseURL.spec + "LightweightTheme.jsm",
baseURL.spec + "WinPathURI.jsm",
baseURL.spec + "HookManager.jsm",
baseURL.spec + "SharedHooks.jsm",
];

let loadedModules = Object.create(null);

let lazyLoadModules = Object.create(null);

// Ensures ordered initialization for lazy loaded modules
// Ensures ordered startup and shutdown for all modules
let loadedModulesOrdered = [];
let lazyLoadModulesOrdered = [];

let initialized = false;
Expand Down Expand Up @@ -123,8 +124,8 @@ var Bootstrap = {
if (!initialized) return;

// Shut down modules
for (let url in loadedModules)
Bootstrap.shutdownModule(url);
for (let i = loadedModulesOrdered.length - 1; i >= 0; i--)
Bootstrap.shutdownModule(loadedModulesOrdered[i]);

Services.obs.removeObserver(BootstrapPrivate, "xpcom-category-entry-added");
Services.obs.removeObserver(BootstrapPrivate, "xpcom-category-entry-removed");
Expand Down Expand Up @@ -158,6 +159,7 @@ var Bootstrap = {
{
obj.startup();
loadedModules[url] = obj;
loadedModulesOrdered.push(url);
}
catch (e)
{
Expand Down Expand Up @@ -212,6 +214,7 @@ var Bootstrap = {
{
obj.lazyStartup();
loadedModules[url] = obj;
loadedModulesOrdered.push(url);
}
catch (e)
{
Expand Down
6 changes: 0 additions & 6 deletions extension/modules/ChromeBridge.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,16 @@ function MsgRet(retArray, defaultValue)
return retArray[0];
}

let initialized = false;

let ChromeBridge = {
startup: function()
{
if (initialized) return;
initialized = true;

// Register component to allow retrieving private and public URL
let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(cidPrivate, "Fire-IE private module URL", contractIDPrivate, factoryPrivate);
},

shutdown: function()
{
if (!initialized) return;
},

reloadContainerPage: function(frameGlobal)
Expand Down
72 changes: 40 additions & 32 deletions extension/modules/HookManager.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,13 @@ Cu.import(baseURL.spec + "Utils.jsm");
*/
let globalHM = null;

const cidGlobalHM = Components.ID("{BC0178CF-665A-4BB6-BF00-BCF9A9A5FE11}");
const contractIDGlobalHM = "@fireie.org/fireie/hook-manager-global;1";

function globalEval(name) { return eval(name); }

let factoryGlobalHM = {
createInstance: function(outer, iid)
{
if (outer) throw Cr.NS_ERROR_NO_AGGREGATION;
if (!globalHM)
{
globalHM = new HookManager(
this, // globalScope
"Components.classes['" + contractIDGlobalHM + "'].getService().wrappedJSObject", // ref name
function(name) // evalInScope
{
return globalEval(name);
},
function(name, value) // assignInScope
{
return (new Function("return (" + name + ") = arguments[0];"))(value);
});
globalHM.wrappedJSObject = globalHM;
}
return globalHM.QueryInterface(iid);
}
};

let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(cidGlobalHM, "Fire-IE Global HookManager", contractIDGlobalHM, factoryGlobalHM);
let factoryGlobalHM = null;
let globalScope = this;

/**
* Helper objects to ease the use of the global HM
* Helper stuff to simplify the use of the global HM
*/

// Usage: HM.hookCodeHead("JSM(moduleURL).symbolName.XXX", function(...) {...});
let JSM = function(moduleURL) {
let jsm = {};
Expand Down Expand Up @@ -119,6 +91,42 @@ let HookManager = function(globalScope, globalReferencableName, evalInScope, ass
// Static scope of the class
let HMS = HookManager;

HMS.startup = function()
{
const cidGlobalHM = Components.ID("{BC0178CF-665A-4BB6-BF00-BCF9A9A5FE11}");
const contractIDGlobalHM = "@fireie.org/fireie/hook-manager-global;1";

factoryGlobalHM = {
createInstance: function(outer, iid)
{
if (outer) throw Cr.NS_ERROR_NO_AGGREGATION;
if (!globalHM)
{
globalHM = new HookManager(
globalScope, // globalScope
"Components.classes['" + contractIDGlobalHM + "'].getService().wrappedJSObject", // ref name
function(name) // evalInScope
{
return (new Function("return " + name + ";"))();
},
function(name, value) // assignInScope
{
return (new Function("return (" + name + ") = arguments[0];"))(value);
});
globalHM.wrappedJSObject = globalHM;
}
return globalHM.QueryInterface(iid);
}
};

let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(cidGlobalHM, "Fire-IE Global HookManager", contractIDGlobalHM, factoryGlobalHM);
};

HMS.shutdown = function()
{
};

HMS._getOriginalFunc = function(func)
{
let idx = func.FireIE_orgFuncIdx;
Expand Down
12 changes: 5 additions & 7 deletions extension/modules/IECookieManager.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ let IECookieManager = {
startup: function()
{
// To avoid cyclic import, we have to do it here
let jsm = {};
Cu.import(baseURL.spec + "UtilsPluginManager.jsm", jsm);
this._upm = jsm.UtilsPluginManager;
Cu.import(baseURL.spec + "UtilsPluginManager.jsm");

try
{
Expand Down Expand Up @@ -250,7 +248,7 @@ let IECookieManager = {
cookieData +="; httponly";
}

if (Prefs.OOPP_remoteSetCookie && this._upm.isRunningOOP)
if (Prefs.OOPP_remoteSetCookie && UtilsPluginManager.isRunningOOP)
{
this._deferredSaveIECookie(url, cookieData);
}
Expand Down Expand Up @@ -288,9 +286,9 @@ let IECookieManager = {

clearIESessionCookies: function()
{
if (Prefs.OOPP_remoteSetCookie && this._upm.isRunningOOP)
if (Prefs.OOPP_remoteSetCookie && UtilsPluginManager.isRunningOOP)
{
this._upm.getPlugin().ClearSessionCookies();
UtilsPluginManager.getPlugin().ClearSessionCookies();
}
else
{
Expand Down Expand Up @@ -321,7 +319,7 @@ let IECookieManager = {
if (Prefs.logCookies)
Utils.LOG("[CookieObserver] Saving " + this._deferredIECookies.length + " deferred IE cookie(s).");
let jsonCookies = JSON.stringify(this._deferredIECookies);
this._upm.getPlugin().BatchSetCookie(jsonCookies);
UtilsPluginManager.getPlugin().BatchSetCookie(jsonCookies);
}
}
catch (ex)
Expand Down
2 changes: 0 additions & 2 deletions extension/modules/RuleListener.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ var RuleListener = {
*/
shutdown: function()
{

if (isDirty > 0) RuleStorage.saveToDisk();

},

/**
Expand Down
7 changes: 7 additions & 0 deletions extension/modules/Synchronizer.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ var Synchronizer = {
timer.initWithCallback(callback, INITIAL_DELAY * MILLISECONDS_IN_SECOND, Ci.nsITimer.TYPE_REPEATING_SLACK);
},

/**
* Called on module shutdown.
*/
shutdown: function()
{
},

/**
* Checks whether a subscription is currently being downloaded.
* @param {String} url URL of the subscription
Expand Down
22 changes: 1 addition & 21 deletions extension/modules/UtilsPluginManager.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ let UtilsPluginManager = {
*/
_isPluginInitialized: false,

/**
* Whether init() has been called
*/
_isInitCalled: false,

/**
* Whether our plugin is running out-of-process
*/
Expand Down Expand Up @@ -75,29 +70,15 @@ let UtilsPluginManager = {

lazyStartup: function()
{
this.init();
},

shutdown: function()
{
this.uninit();
},

init: function()
{
if (this._isInitCalled) return;
this._isInitCalled = true;

this._isRunningOOP = Utils.isOOPP;
this._injectEventDispatchHelper();
this._handlePluginEvents();
this._install();
this._registerHandlers();
},

uninit: function()
shutdown: function()
{
if (!this._isInitCalled) return;
this._unregisterHandlers();
this._cancelPluginEvents();
},
Expand Down Expand Up @@ -703,5 +684,4 @@ let DNTObserverPrivate = {
},

QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObserver])

};
38 changes: 17 additions & 21 deletions extension/modules/WinPathURI.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,23 @@ let S_OK = 0;
let libShlwapi = null;
let abi = ctypes.winapi_abi;

let WinPathURI = {
startup: function()
function init()
{
try
{
try
{
libShlwapi = ctypes.open("Shlwapi.dll");
PathCreateFromUrlW = libShlwapi.declare("PathCreateFromUrlW", abi,
HRESULT, PCWSTR, PWSTR, DWORD.ptr, DWORD);
UrlCreateFromPathW = libShlwapi.declare("UrlCreateFromPathW", abi,
HRESULT, PCWSTR, PWSTR, DWORD.ptr, DWORD);
}
catch (ex)
{
Utils.ERROR("Failed to initialize WinPathURI: " + ex);
}
},

shutdown: function()
libShlwapi = ctypes.open("Shlwapi.dll");
PathCreateFromUrlW = libShlwapi.declare("PathCreateFromUrlW", abi,
HRESULT, PCWSTR, PWSTR, DWORD.ptr, DWORD);
UrlCreateFromPathW = libShlwapi.declare("UrlCreateFromPathW", abi,
HRESULT, PCWSTR, PWSTR, DWORD.ptr, DWORD);
}
catch (ex)
{
if (libShlwapi)
libShlwapi.close();
},

Utils.ERROR("Failed to initialize WinPathURI: " + ex);
}
}

let WinPathURI = {
filePathFromIEURI: function(ieFileURI)
{
if (!PathCreateFromUrlW)
Expand Down Expand Up @@ -194,3 +188,5 @@ let WinPathURI = {
return this.filePathToIEURI(localFilePath);
},
};

init();

0 comments on commit b07a628

Please sign in to comment.