diff --git a/components/greasemonkey.js b/components/greasemonkey.js index d07b4c971..e64515180 100644 --- a/components/greasemonkey.js +++ b/components/greasemonkey.js @@ -1,9 +1,14 @@ -const CLASSNAME = "GM_GreasemonkeyService"; + +// XPCOM info +const DESCRIPTION = "GM_GreasemonkeyService"; const CONTRACTID = "@greasemonkey.mozdev.org/greasemonkey-service;1"; -const CID = Components.ID("{77bf3650-1cd6-11da-8cd6-0800200c9a66}"); +const CLASSID = Components.ID("{77bf3650-1cd6-11da-8cd6-0800200c9a66}"); const Cc = Components.classes; const Ci = Components.interfaces; +const Cu = Components.utils; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); const appSvc = Cc["@mozilla.org/appshell/appShellService;1"] .getService(Ci.nsIAppShellService); @@ -11,12 +16,10 @@ const appSvc = Cc["@mozilla.org/appshell/appShellService;1"] const gmSvcFilename = Components.stack.filename; const maxJSVersion = (function getMaxJSVersion() { - var appInfo = Components - .classes["@mozilla.org/xre/app-info;1"] - .getService(Components.interfaces.nsIXULAppInfo); - var versionChecker = Components - .classes["@mozilla.org/xpcom/version-comparator;1"] - .getService(Components.interfaces.nsIVersionComparator); + var appInfo = Cc["@mozilla.org/xre/app-info;1"] + .getService(Ci.nsIXULAppInfo); + var versionChecker = Cc["@mozilla.org/xpcom/version-comparator;1"] + .getService(Ci.nsIVersionComparator); // Firefox 3.5 and higher supports 1.8. if (versionChecker.compare(appInfo.version, "3.5") >= 0) { @@ -59,7 +62,34 @@ function GM_apiLeakCheck(apiName) { return true; } -var greasemonkeyService = { + +function GM_GreasemonkeyService() { + this.wrappedJSObject = this; +} + +GM_GreasemonkeyService.prototype = { + classDescription: DESCRIPTION, + classID: CLASSID, + contractID: CONTRACTID, + _xpcom_categories: [{category: "app-startup", + entry: DESCRIPTION, + value: CONTRACTID, + service: true}, + {category: "content-policy", + entry: CONTRACTID, + value: CONTRACTID, + service: true}], + + // nsISupports + QueryInterface: XPCOMUtils.generateQI([ + Ci.nsIObserver, + Ci.nsISupports, + Ci.nsISupportsWeakReference, + Ci.gmIGreasemonkeyService, + Ci.nsIWindowMediatorListener, + Ci.nsIContentPolicy + ]), + _config: null, get config() { if (!this._config) { @@ -74,22 +104,6 @@ var greasemonkeyService = { }, browserWindows: [], - - // nsISupports - QueryInterface: function(aIID) { - if (!aIID.equals(Ci.nsIObserver) && - !aIID.equals(Ci.nsISupports) && - !aIID.equals(Ci.nsISupportsWeakReference) && - !aIID.equals(Ci.gmIGreasemonkeyService) && - !aIID.equals(Ci.nsIWindowMediatorListener) && - !aIID.equals(Ci.nsIContentPolicy)) { - throw Components.results.NS_ERROR_NO_INTERFACE; - } - - return this; - }, - - // nsIObserver observe: function(aSubject, aTopic, aData) { if (aTopic == "app-startup") { @@ -474,68 +488,7 @@ var greasemonkeyService = { } }; -greasemonkeyService.wrappedJSObject = greasemonkeyService; - - - -/** - * XPCOM Registration goop - */ -var Module = new Object(); - -Module.registerSelf = function(compMgr, fileSpec, location, type) { - compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar); - compMgr.registerFactoryLocation(CID, - CLASSNAME, - CONTRACTID, - fileSpec, - location, - type); - - var catMgr = Cc["@mozilla.org/categorymanager;1"] - .getService(Ci.nsICategoryManager); - - catMgr.addCategoryEntry("app-startup", - CLASSNAME, - CONTRACTID, - true, - true); - - catMgr.addCategoryEntry("content-policy", - CONTRACTID, - CONTRACTID, - true, - true); -}; - -Module.getClassObject = function(compMgr, cid, iid) { - if (!cid.equals(CID)) { - throw Components.results.NS_ERROR_NOT_IMPLEMENTED; - } - - if (!iid.equals(Ci.nsIFactory)) { - throw Components.results.NS_ERROR_NO_INTERFACE; - } - - return Factory; -}; - -Module.canUnload = function(compMgr) { - return true; -}; - - -var Factory = new Object(); - -Factory.createInstance = function(outer, iid) { - if (outer != null) { - throw Components.results.NS_ERROR_NO_AGGREGATION; - } - - return greasemonkeyService; -}; - - +// XPCOM module registration. function NSGetModule(compMgr, fileSpec) { - return Module; + return XPCOMUtils.generateModule([GM_GreasemonkeyService]); } diff --git a/content/addons.js b/content/addons.js index a1fdb8dc1..6bfb206e5 100644 --- a/content/addons.js +++ b/content/addons.js @@ -272,7 +272,7 @@ var greasemonkeyAddons = { GM_openInEditor(script); break; case 'cmd_userscript_show': - GM_openFolder(script._file); + GM_openFolder(script.file); break; case 'cmd_userscript_enable': script.enabled = true; diff --git a/content/config.js b/content/config.js index 0f466f93a..356d28140 100644 --- a/content/config.js +++ b/content/config.js @@ -46,12 +46,8 @@ Config.prototype = { }, _find: function(aScript) { - var namespace = aScript._namespace.toLowerCase(); - var name = aScript._name.toLowerCase(); - for (var i = 0, script; script = this._scripts[i]; i++) { - if (script._namespace.toLowerCase() == namespace - && script._name.toLowerCase() == name) { + if (script.id == aScript.id) { return i; } } @@ -268,7 +264,7 @@ Config.prototype = { } script._modified = script.file.lastModifiedTime; - script._metahash = GM_sha1(script._rawMeta); + script._dependhash = GM_sha1(script._rawMeta); this._scripts.push(script); this._changed(script, "install", null); diff --git a/content/script.js b/content/script.js index b10d918c9..9b03bf6da 100644 --- a/content/script.js +++ b/content/script.js @@ -273,28 +273,40 @@ Script.prototype = { return false; }, - updateFromNewScript: function(newScript, safeWin, chromeWin) { - // Migrate preferences. - if (this.prefroot != newScript.prefroot) { - var storageOld = new GM_ScriptStorage(this); - var storageNew = new GM_ScriptStorage(newScript); - - var names = storageOld.listValues(); - for (var i = 0, name = null; name = names[i]; i++) { - storageNew.setValue(name, storageOld.getValue(name)); - storageOld.deleteValue(name); + updateFromNewScript: function(newScript) { + // if the @name and @namespace have changed + // make sure they don't conflict with another installed script + if (newScript.id != this.id) { + if (!GM_getConfig().installIsUpdate(newScript)) { + // Migrate preferences. + if (this.prefroot != newScript.prefroot) { + var storageOld = new GM_ScriptStorage(this); + var storageNew = new GM_ScriptStorage(newScript); + + var names = storageOld.listValues(); + for (var i = 0, name = null; name = names[i]; i++) { + storageNew.setValue(name, storageOld.getValue(name)); + storageOld.deleteValue(name); + } + } + + // Empty cached values. + this._id = null; + this._prefroot = null; + + this._name = newScript._name; + this._namespace = newScript._namespace; + } else { + // Notify the user of the conflict + alert('Error: Another script with @name: "' + newScript._name + + '" and @namespace: "' + newScript._namespace + + '" is already installed.\nThese values must be unique.'); } } - // Empty cached values. - this._id = null; - this._prefroot = null; - // Copy new values. this._includes = newScript._includes; this._excludes = newScript._excludes; - this._name = newScript._name; - this._namespace = newScript._namespace; this._description = newScript._description; this._unwrap = newScript._unwrap; this._version = newScript._version; diff --git a/content/utils.js b/content/utils.js index b714b094a..78c8e1d01 100644 --- a/content/utils.js +++ b/content/utils.js @@ -8,10 +8,6 @@ var GM_stringBundle = Components .getService(Components.interfaces.nsIStringBundleService) .createBundle("chrome://greasemonkey/locale/gm-browser.properties"); -function GM_isDef(thing) { - return typeof(thing) != "undefined"; -} - function GM_getConfig() { return Components .classes["@greasemonkey.mozdev.org/greasemonkey-service;1"]