creating sandboxes in domwindow zone; this should ease GC pressure a little
[guerillascript.git] / main / addon.js
blobccd78421645e17c5cd24d95d67b61d16d97e458b
1 /* coded by Ketmar // Invisible Vector (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
9 */
10 ////////////////////////////////////////////////////////////////////////////////
11 const {utils:Cu, classes:Cc, interfaces:Ci, results:Cr} = Components;
13 let addonobj = null;
16 ////////////////////////////////////////////////////////////////////////////////
17 function initAddon (global, addonName, contentUrl, jsUrl, startupCB) {
18 if (addonobj === null) {
19 // check arguments
20 if (typeof(addonName) !== "string" || addonName.length == 0) throw new Error("addon name expected (non-empty string)");
21 if (typeof(contentUrl) !== "string" || contentUrl.length == 0) throw new Error("content url expected (non-empty string)");
22 if (contentUrl[contentUrl.length-1] != "/") contentUrl += "/";
23 if (typeof(jsUrl) === "undefined") jsUrl = contentUrl;
24 if (typeof(jsUrl) !== "string" || jsUrl.length == 0) throw new Error("js url expected (non-empty string)");
25 if (typeof(startupCB) === "undefined") startupCB = null;
26 if (startupCB && typeof(startupCB) !== "function") throw new Error("startup callback expected (function)");
28 //Components.utils.reportError("ADDON '"+addonName+"': initializing");
29 // load "mainjs:init.js"
30 let uri = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(jsUrl+"init.js", null, null);
31 let initfn = Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader).loadSubScript(uri.spec, this, "UTF-8");
32 try {
33 addonobj = initfn(global, addonName, contentUrl, jsUrl);
34 if (!addonobj) throw new Error("can't init addon '"+addonName+"'"); // alas, something is wrong
35 if (startupCB) startupCB(addonobj);
36 addonobj.onStartup();
37 } catch (e) {
38 if (addonobj) try { addonobj.onShutdown(); } catch (xx) {}
39 addonobj = false;
40 Components.utils.reportError(e.stack);
41 Components.utils.reportError(e);
42 throw e;
45 if (addonobj) return addonobj;
46 throw new Error("addon object wasn't initialized");
50 ////////////////////////////////////////////////////////////////////////////////
51 function getAddonObj () {
52 if (addonobj) return addonobj;
53 throw new Error("addon object wasn't initialized");
57 ////////////////////////////////////////////////////////////////////////////////
58 let windowCount = 0;
60 function newWindow (global, win) {
61 if (addonobj) {
62 win.addEventListener("load", function () {
63 ++windowCount;
64 //Components.utils.reportError("NEW WINDOW: "+windowCount);
65 // setup unload hook
66 global.window.addEventListener("unload", function () {
67 --windowCount;
68 //Components.utils.reportError("DEAD WINDOW: "+windowCount);
69 addonobj.onWindowUnload(win);
70 if (windowCount == 0) {
71 // no more registered windows --> shutdown
72 //Components.utils.reportError("SHUTTING DOWN!");
73 addonobj.onShutdown();
75 }, false);
76 addonobj.onWindowLoad(win);
77 }, false);
81 ////////////////////////////////////////////////////////////////////////////////
82 let EXPORTED_SYMBOLS = ["initAddon", "getAddonObj", "newWindow"];