diff --git a/content/bookmarks.js b/content/bookmarks.js index ebb56345..42b5c156 100644 --- a/content/bookmarks.js +++ b/content/bookmarks.js @@ -41,6 +41,7 @@ liberator.Bookmarks = function () //{{{ const ioService = Components.classes["@mozilla.org/network/io-service;1"] .getService(Components.interfaces.nsIIOService); + const storage = liberator.storage; function Cache(name, store, serial) { const properties = { uri: 0, title: 1, keyword: 2, tags: 3, id: 4 }; @@ -140,7 +141,7 @@ liberator.Bookmarks = function () //{{{ if (rootFolders.indexOf(findRoot(itemId)) >= 0) { loadBookmark(readBookmark(itemId)); - liberator.storage.fireEvent(name, "add", itemId); + storage.fireEvent(name, "add", itemId); } } }, @@ -148,7 +149,7 @@ liberator.Bookmarks = function () //{{{ { // liberator.dump("onItemRemoved(" + itemId + ", " + folder + ", " + index + ")\n"); if (deleteBookmark(itemId)) - liberator.storage.fireEvent(name, "remove", itemId); + storage.fireEvent(name, "remove", itemId); }, onItemChanged: function (itemId, property, isAnnotation, value) { @@ -162,7 +163,7 @@ liberator.Bookmarks = function () //{{{ value = taggingService.getTagsForURI(ioService.newURI(bookmark[properties.uri], null, null), {}); if (property in properties) bookmark[properties[property]] = value; - liberator.storage.fireEvent(name, "change", itemId); + storage.fireEvent(name, "change", itemId); } }, QueryInterface: function (iid) @@ -184,7 +185,7 @@ liberator.Bookmarks = function () //{{{ } var cache = liberator.storage.newObject("bookmark-cache", Cache, false); liberator.storage.addObserver("bookmark-cache", bookmarkObserver); - liberator.registerCallback("shutdown", 0, function () { + liberator.registerObserver("shutdown", function () { liberator.storage.removeObserver("bookmark-cache", bookmarkObserver) }); diff --git a/content/buffer.js b/content/buffer.js index a17ed100..0a6a44e0 100644 --- a/content/buffer.js +++ b/content/buffer.js @@ -45,7 +45,7 @@ liberator.Buffer = function () //{{{ const util = liberator.util; const consoleService = Components.classes["@mozilla.org/consoleservice;1"] .getService(Components.interfaces.nsIConsoleService); - + const sleep = liberator.sleep; function Styles(name, store, serial) { const XHTML = "http://www.w3.org/1999/xhtml"; @@ -128,6 +128,8 @@ liberator.Buffer = function () //{{{ } let queryinterface = XPCOMUtils.generateQI([Components.interfaces.nsIConsoleListener]); + /* What happens if more than one thread tries to use this? */ + let testDoc = document.implementation.createDocument(XHTML, "doc", null); function checkSyntax(css) { let errors = []; @@ -148,23 +150,24 @@ liberator.Buffer = function () //{{{ try { consoleService.registerListener(listener); - let doc = document.implementation.createDocument(XHTML, "doc", null); - doc.documentElement.appendChild(util.xmlToDom( - , doc)); + if (testDoc.documentElement.firstChild) + testDoc.documentElement.removeChild(testDoc.documentElement.firstChild); + testDoc.documentElement.appendChild(util.xmlToDom( + , testDoc)); while (true) { try { // Throws NS_ERROR_DOM_INVALID_ACCESS_ERR if not finished loading - doc.styleSheets[0].cssRules.length; + testDoc.styleSheets[0].cssRules.length; break; } catch (e) { if (e.name != "NS_ERROR_DOM_INVALID_ACCESS_ERR") return [e.toString()]; - liberator.sleep(10); + sleep(10); } } } diff --git a/content/events.js b/content/events.js index b2b62325..b241db6b 100644 --- a/content/events.js +++ b/content/events.js @@ -1560,15 +1560,15 @@ liberator.Events = function () //{{{ { var prefService = Components.classes["@mozilla.org/preferences-service;1"] .getService(Components.interfaces.nsIPrefService); - this._branch = prefService.getBranch(""); // better way to monitor all changes? - this._branch.QueryInterface(Components.interfaces.nsIPrefBranch2); - this._branch.addObserver("", this, false); + this._branch = prefService.getBranch(""); // better way to monitor all changes? + this._branch.QueryInterface(Components.interfaces.nsIPrefBranch2); + this._branch.addObserver("", this, false); }, unregister: function () { - if (!this._branch) return; - this._branch.removeObserver("", this); + if (this._branch) + this._branch.removeObserver("", this); }, observe: function (aSubject, aTopic, aData) @@ -1603,7 +1603,10 @@ liberator.Events = function () //{{{ catch (e) {} eventManager.prefObserver.register(); - liberator.registerCallback("shutdown", 0, eventManager.destroy); + liberator.registerObserver("shutdown", function () { + eventManager.destroy(); + eventManager.prefObserver.unregister(); + }); window.addEventListener("keypress", eventManager.onKeyPress, true); window.addEventListener("keydown", eventManager.onKeyUpOrDown, true); diff --git a/content/liberator.js b/content/liberator.js index 978dffdc..79ac99c2 100644 --- a/content/liberator.js +++ b/content/liberator.js @@ -32,7 +32,11 @@ const liberator = (function () //{{{ ////////////////////// PRIVATE SECTION ///////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ + const threadManager = Components.classes["@mozilla.org/thread-manager;1"] + .getService(Components.interfaces.nsIThreadManager); + var callbacks = []; + var observers = []; function loadModule(name, func) { @@ -627,12 +631,26 @@ const liberator = (function () //{{{ for (let i = 0; i < callbacks.length; i++) { var [thistype, thismode, thisfunc] = callbacks[i]; - if (mode == liberator.modes.NONE || mode == thismode && type == thistype) + if (mode == thismode && type == thistype) return thisfunc.call(this, data); } return false; }, + registerObserver: function (type, callback) + { + observers.push([type, callback]); + }, + + triggerObserver: function (type, data) + { + for (let [,[thistype, callback]] in Iterator(observers)) + { + if (thistype == type) + callback(data); + } + }, + beep: function () { if (liberator.options["visualbell"]) @@ -1181,7 +1199,7 @@ const liberator = (function () //{{{ liberator.storage.saveAll(); - liberator.triggerCallback("shutdown", 0, null); + liberator.triggerObserver("shutdown", null); liberator.dump("All liberator modules destroyed\n"); @@ -1190,8 +1208,6 @@ const liberator = (function () //{{{ sleep: function (ms) { - var threadManager = Components.classes["@mozilla.org/thread-manager;1"] - .getService(Components.interfaces.nsIThreadManager); var mainThread = threadManager.mainThread; var then = new Date().getTime(), now = then; diff --git a/content/options.js b/content/options.js index 71ebc326..3f6caccc 100644 --- a/content/options.js +++ b/content/options.js @@ -174,7 +174,7 @@ liberator.Options = function () //{{{ liberator.storage.newMap("options", false); liberator.storage.addObserver("options", optionObserver); - liberator.registerCallback("shutdown", 0, function () { + liberator.registerObserver("shutdown", function () { liberator.storage.removeObserver("options", optionObserver) }); @@ -252,7 +252,7 @@ liberator.Options = function () //{{{ if (!/keypress/.test(popupAllowedEvents)) { storePreference("dom.popup_allowed_events", popupAllowedEvents + " keypress"); - liberator.registerCallback("shutdown", 0, function () + liberator.registerObserver("shutdown", function () { if (loadPreference("dom.popup_allowed_events", "") == popupAllowedEvents + " keypress") @@ -735,7 +735,7 @@ liberator.Options = function () //{{{ if (filter.length > 0 && filter.lastIndexOf("=") == filter.length - 1) { - for (let [,name] in prefArray) + for (let [,name] in Iterator(prefArray)) { if (name.match("^" + filter.substr(0, filter.length - 1) + "$" )) {