Bug 385524 - Reduce boilerplate code in passwordmgr. r=dolske/gavin
authorrflint@ryanflint.com
Sat, 30 Jun 2007 18:41:33 -0700
changeset 2992 5ec8be148f90f4bdc156959b78a3072379497481
parent 2991 a93e3ba7ee1cb7ee0bf189e59c6f9638b83fb9c2
child 2993 56432244715491b7afc5d8a1c30b563823a522b3
push idunknown
push userunknown
push dateunknown
reviewersdolske, gavin
bugs385524
milestone1.9a6pre
Bug 385524 - Reduce boilerplate code in passwordmgr. r=dolske/gavin
toolkit/components/passwordmgr/src/nsLoginInfo.js
toolkit/components/passwordmgr/src/nsLoginManager.js
toolkit/components/passwordmgr/src/nsLoginManagerPrompter.js
toolkit/components/passwordmgr/src/storage-Legacy.js
--- a/toolkit/components/passwordmgr/src/nsLoginInfo.js
+++ b/toolkit/components/passwordmgr/src/nsLoginInfo.js
@@ -33,27 +33,26 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
 function nsLoginInfo() {}
 
 nsLoginInfo.prototype = {
 
-    QueryInterface : function (iid) {
-        var interfaces = [Ci.nsILoginInfo, Ci.nsISupports];
-        if (!interfaces.some( function(v) { return iid.equals(v) } ))
-            throw Components.results.NS_ERROR_NO_INTERFACE;
-        return this;
-    },
-
+    classDescription  : "LoginInfo",
+    contractID : "@mozilla.org/login-manager/loginInfo;1",
+    classID : Components.ID("{0f2f347c-1e4f-40cc-8efd-792dea70a85e}"),
+    QueryInterface: XPCOMUtils.generateQI([Ci.nsILoginInfo]), 
 
     hostname      : null,
     formSubmitURL : null,
     httpRealm     : null,
     username      : null,
     password      : null,
     usernameField : null,
     passwordField : null,
@@ -99,63 +98,12 @@ nsLoginInfo.prototype = {
             this.passwordField != aLogin.passwordField)
             return false;
 
         return true;
     }
 
 }; // end of nsLoginInfo implementation
 
-
-
-
-// Boilerplate code for component registration...
-var gModule = {
-    registerSelf: function(componentManager, fileSpec, location, type) {
-        componentManager = componentManager.QueryInterface(
-                                                Ci.nsIComponentRegistrar);
-        for each (var obj in this._objects) 
-            componentManager.registerFactoryLocation(obj.CID,
-                    obj.className, obj.contractID,
-                    fileSpec, location, type);
-    },
-
-    unregisterSelf: function (componentManager, location, type) {
-        for each (var obj in this._objects) 
-            componentManager.unregisterFactoryLocation(obj.CID, location);
-    },
-
-    getClassObject: function(componentManager, cid, iid) {
-        if (!iid.equals(Ci.nsIFactory))
-            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
-        for (var key in this._objects) {
-            if (cid.equals(this._objects[key].CID))
-                return this._objects[key].factory;
-        }
-
-        throw Components.results.NS_ERROR_NO_INTERFACE;
-    },
-
-    _objects: {
-        service: {
-            CID : Components.ID("{0f2f347c-1e4f-40cc-8efd-792dea70a85e}"),
-            contractID : "@mozilla.org/login-manager/loginInfo;1",
-            className  : "LoginInfo",
-            factory    : LoginInfoFactory = {
-                createInstance: function(aOuter, aIID) {
-                    if (aOuter != null)
-                        throw Components.results.NS_ERROR_NO_AGGREGATION;
-                    var svc = new nsLoginInfo();
-                    return svc.QueryInterface(aIID);
-                }
-            }
-        }
-    },
-
-    canUnload: function(componentManager) {
-        return true;
-    }
-};
-
+var component = [nsLoginInfo];
 function NSGetModule(compMgr, fileSpec) {
-    return gModule;
+    return XPCOMUtils.generateModule(component);
 }
--- a/toolkit/components/passwordmgr/src/nsLoginManager.js
+++ b/toolkit/components/passwordmgr/src/nsLoginManager.js
@@ -33,29 +33,29 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
 function LoginManager() {
     this.init();
 }
 
 LoginManager.prototype = {
 
-    QueryInterface : function (iid) {
-        const interfaces = [Ci.nsILoginManager,
-                            Ci.nsISupports, Ci.nsISupportsWeakReference];
-        if (!interfaces.some( function(v) { return iid.equals(v) } ))
-            throw Components.results.NS_ERROR_NO_INTERFACE;
-        return this;
-    },
+    classDescription: "LoginManager",
+    contractID: "@mozilla.org/login-manager;1",
+    classID: Components.ID("{cb9e0de8-3598-4ed7-857b-827f011ad5d8}"),
+    QueryInterface : XPCOMUtils.generateQI([Ci.nsILoginManager,
+                                            Ci.nsISupportsWeakReference]),
 
 
     /* ---------- private memebers ---------- */
 
 
     __logService : null, // Console logging service, used for debugging.
     get _logService() {
         if (!this.__logService)
@@ -179,17 +179,17 @@ LoginManager.prototype = {
         this._nsLoginInfo = new Components.Constructor(
             "@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo);
 
 
         // Form submit observer checks forms for new logins and pw changes.
         var observerService = Cc["@mozilla.org/observer-service;1"]
                                 .getService(Ci.nsIObserverService);
         observerService.addObserver(this._observer, "earlyformsubmit", false);
-        observerService.addObserver(this._observer, "xpcom-shutdown", false);
+        observerService.addObserver(this._observer, "xpcom-shutdown", false);
 
         // WebProgressListener for getting notification of new doc loads.
         var progress = Cc["@mozilla.org/docloaderservice;1"]
                         .getService(Ci.nsIWebProgress);
         progress.addProgressListener(this._webProgressListener,
                                      Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
 
 
@@ -216,23 +216,19 @@ LoginManager.prototype = {
      * _observer object
      *
      * Internal utility object, implements the nsIObserver interface.
      * Used to receive notification for: form submission, preference changes.
      */
     _observer : {
         _pwmgr : null,
 
-        QueryInterface : function (iid) {
-            const interfaces = [Ci.nsIObserver, Ci.nsIFormSubmitObserver,
-                                Ci.nsISupports, Ci.nsISupportsWeakReference];
-            if (!interfaces.some( function(v) { return iid.equals(v) } ))
-                throw Components.results.NS_ERROR_NO_INTERFACE;
-            return this;
-        },
+        QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, 
+                                                Ci.nsIFormSubmitObserver,
+                                                Ci.nsISupportsWeakReference]),
 
 
         // nsFormSubmitObserver
         notify : function (formElement, aWindow, actionURI) {
             this._pwmgr.log("observer notified for form submission.");
 
             // We're invoked before the content's |onsubmit| handlers, so we
             // can grab form data before it might be modified (see bug 257781).
@@ -282,23 +278,18 @@ LoginManager.prototype = {
      * Internal utility object, implements nsIWebProgressListener interface.
      * This is attached to the document loader service, so we get
      * notifications about all page loads.
      */
     _webProgressListener : {
         _pwmgr : null,
         _domEventListener : null,
 
-        QueryInterface : function (iid) {
-            const interfaces = [Ci.nsIWebProgressListener,
-                                Ci.nsISupports, Ci.nsISupportsWeakReference];
-            if (!interfaces.some( function(v) { return iid.equals(v) } ))
-                throw Components.results.NS_ERROR_NO_INTERFACE;
-            return this;
-        },
+        QueryInterface : XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
+                                                Ci.nsISupportsWeakReference]),
 
 
         onStateChange : function (aWebProgress, aRequest,
                                   aStateFlags,  aStatus) {
 
             // STATE_START is too early, doc is still the old page.
             if (!(aStateFlags & Ci.nsIWebProgressListener.STATE_TRANSFERRING))
                 return 0;
@@ -342,23 +333,18 @@ LoginManager.prototype = {
      * _domEventListener object
      *
      * Internal utility object, implements nsIDOMEventListener
      * Used to catch certain DOM events needed to properly implement form fill.
      */
     _domEventListener : {
         _pwmgr : null,
 
-        QueryInterface : function (iid) {
-            const interfaces = [Ci.nsIDOMEventListener,
-                                Ci.nsISupports, Ci.nsISupportsWeakReference];
-            if (!interfaces.some( function(v) { return iid.equals(v) } ))
-                throw Components.results.NS_ERROR_NO_INTERFACE;
-            return this;
-        },
+        QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMEventListener,
+                                                Ci.nsISupportsWeakReference]),
 
 
         handleEvent : function (event) {
             this._pwmgr.log("domEventListener: got event " + event.type);
 
             var doc, inputElement;
             switch (event.type) {
                 case "DOMContentLoaded":
@@ -1265,23 +1251,18 @@ function UserAutoCompleteResult (aSearch
 
     if (this.matchCount > 0) {
         this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
         this.defaultIndex = 0;
     }
 }
 
 UserAutoCompleteResult.prototype = {
-    QueryInterface : function (iid) {
-        const interfaces = [Ci.nsIAutoCompleteResult,
-                            Ci.nsISupports, Ci.nsISupportsWeakReference];
-        if (!interfaces.some( function(v) { return iid.equals(v) } ))
-            throw Components.results.NS_ERROR_NO_INTERFACE;
-        return this;
-    },
+    QueryInterface : XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult,
+                                            Ci.nsISupportsWeakReference]),
 
     // private
     logins : null,
 
     // Interfaces from idl...
     searchString : null,
     searchResult : Ci.nsIAutoCompleteResult.RESULT_NOMATCH,
     defaultIndex : -1,
@@ -1315,65 +1296,12 @@ UserAutoCompleteResult.prototype = {
         if (removeFromDB) {
             var pwmgr = Cc["@mozilla.org/login-manager;1"]
                             .getService(Ci.nsILoginManager);
             pwmgr.removeLogin(removedLogin);
         }
     },
 };
 
-
-
-
-// Boilerplate code for component registration...
-var gModule = {
-    registerSelf: function (componentManager, fileSpec, location, type) {
-        componentManager = componentManager.QueryInterface(
-                                                Ci.nsIComponentRegistrar);
-        for each (var obj in this._objects)
-            componentManager.registerFactoryLocation(obj.CID,
-                    obj.className, obj.contractID,
-                    fileSpec, location, type);
-    },
-
-    unregisterSelf: function (componentManager, location, type) {
-        for each (var obj in this._objects)
-            componentManager.unregisterFactoryLocation(obj.CID, location);
-    },
-
-    getClassObject: function (componentManager, cid, iid) {
-        if (!iid.equals(Ci.nsIFactory))
-            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
-        for (var key in this._objects) {
-            if (cid.equals(this._objects[key].CID))
-                return this._objects[key].factory;
-        }
-
-        throw Components.results.NS_ERROR_NO_INTERFACE;
-    },
-
-    _objects: {
-        service: {
-            CID : Components.ID("{cb9e0de8-3598-4ed7-857b-827f011ad5d8}"),
-            contractID : "@mozilla.org/login-manager;1",
-            className  : "LoginManager",
-            factory    : LoginManagerFactory = {
-                createInstance: function (aOuter, aIID) {
-                    if (aOuter != null)
-                        throw Components.results.NS_ERROR_NO_AGGREGATION;
-        
-                    var svc = new LoginManager();
-
-                    return svc.QueryInterface(aIID);
-                }
-            }
-        }
-    },
-
-    canUnload: function (componentManager) {
-        return true;
-    }
-};
-
+var component = [LoginManager];
 function NSGetModule (compMgr, fileSpec) {
-    return gModule;
+    return XPCOMUtils.generateModule(component);
 }
--- a/toolkit/components/passwordmgr/src/nsLoginManagerPrompter.js
+++ b/toolkit/components/passwordmgr/src/nsLoginManagerPrompter.js
@@ -33,34 +33,34 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
 /*
  * LoginManagerPromptFactory
  *
  * Implements nsIPromptFactory
  *
  * Invoked by NS_NewAuthPrompter2()
  * [embedding/components/windowwatcher/src/nsPrompt.cpp]
  */
 function LoginManagerPromptFactory() {}
 
 LoginManagerPromptFactory.prototype = {
 
-    QueryInterface : function (iid) {
-        const interfaces = [Ci.nsIPromptFactory, Ci.nsISupports];
-        if (!interfaces.some( function(v) { return iid.equals(v) } ))
-            throw Components.results.NS_ERROR_NO_INTERFACE;
-        return this;
-    },
+    classDescription : "LoginManagerPromptFactory",
+    contractID : "@mozilla.org/passwordmanager/authpromptfactory;1",
+    classID : Components.ID("{447fc780-1d28-412a-91a1-466d48129c65}"),
+    QueryInterface : XPCOMUtils.generateQI([Ci.nsIPromptFactory]),
 
     _promptService : null,
     _pwmgr         : null,
 
     _initialized : false,
 
     getPrompt : function (aWindow, aIID) {
 
@@ -101,23 +101,17 @@ LoginManagerPromptFactory.prototype = {
  * Implements nsIAuthPrompt2.
  *
  * Invoked by a channel for protocol-based authentication (eg HTTP
  * Authenticate, FTP login)
  */
 function LoginManagerPrompter() {}
 LoginManagerPrompter.prototype = {
 
-    QueryInterface : function (iid) {
-        var interfaces = [Ci.nsIAuthPrompt2, Ci.nsISupports];
-        if (!interfaces.some( function(v) { return iid.equals(v) } ))
-            throw Components.results.NS_ERROR_NO_INTERFACE;
-        return this;
-    },
-
+    QueryInterface : XPCOMUtils.generateQI([Ci.nsIAuthPrompt2]),
 
     __logService : null, // Console logging service, used for debugging.
     get _logService() {
         if (!this.__logService)
             this.__logService = Cc["@mozilla.org/consoleservice;1"]
                                     .getService(Ci.nsIConsoleService);
         return this.__logService;
     },
@@ -355,63 +349,12 @@ LoginManagerPrompter.prototype = {
             if (!this._bundle)
                 throw "String bundle not present!";
         }
 
         return this._bundle.GetStringFromName(key);
     }
 }; // end of LoginManagerPrompter implementation
 
-
-
-
-// Boilerplate code...
-var gModule = {
-    registerSelf: function(componentManager, fileSpec, location, type) {
-        componentManager = componentManager.QueryInterface(
-                                                Ci.nsIComponentRegistrar);
-        for each (var obj in this._objects) 
-            componentManager.registerFactoryLocation(obj.CID,
-                    obj.className, obj.contractID,
-                    fileSpec, location, type);
-    },
-
-    unregisterSelf: function (componentManager, location, type) {
-        for each (var obj in this._objects) 
-            componentManager.unregisterFactoryLocation(obj.CID, location);
-    },
-  
-    getClassObject: function(componentManager, cid, iid) {
-        if (!iid.equals(Ci.nsIFactory))
-            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-  
-        for (var key in this._objects) {
-            if (cid.equals(this._objects[key].CID))
-                return this._objects[key].factory;
-        }
-    
-        throw Components.results.NS_ERROR_NO_INTERFACE;
-    },
-
-    _objects: {
-        service: {
-            CID : Components.ID("{447fc780-1d28-412a-91a1-466d48129c65}"),
-            contractID : "@mozilla.org/passwordmanager/authpromptfactory;1",
-            className  : "LoginManagerPromptFactory",
-            factory    : LoginManagerPromptFactory_Factory = {
-                createInstance: function (aOuter, aIID) {
-                    if (aOuter != null)
-                        throw Components.results.NS_ERROR_NO_AGGREGATION;
-
-                    return new LoginManagerPromptFactory().QueryInterface(aIID);
-                }
-            }
-        }
-    },
-
-    canUnload: function(componentManager) {
-        return true;
-    }
-};
-
+var component = [LoginManagerPromptFactory];
 function NSGetModule(compMgr, fileSpec) {
-    return gModule;
+    return XPCOMUtils.generateModule(component);
 }
--- a/toolkit/components/passwordmgr/src/storage-Legacy.js
+++ b/toolkit/components/passwordmgr/src/storage-Legacy.js
@@ -33,26 +33,26 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
 function LoginManagerStorage_legacy() { };
 
 LoginManagerStorage_legacy.prototype = {
 
-    QueryInterface : function (iid) {
-        const interfaces = [Ci.nsILoginManagerStorage, Ci.nsISupports];
-        if (!interfaces.some( function(v) { return iid.equals(v) } ))
-            throw Components.results.NS_ERROR_NO_INTERFACE;
-        return this;
-    },
+    classDescription  : "LoginManagerStorage_legacy",
+    contractID : "@mozilla.org/login-manager/storage/legacy;1",
+    classID : Components.ID("{e09e4ca6-276b-4bb4-8b71-0635a3a2a007}"),
+    QueryInterface : XPCOMUtils.generateQI([Ci.nsILoginManagerStorage]),
 
     __logService : null, // Console logging service, used for debugging.
     get _logService() {
         if (!this.__logService)
             this.__logService = Cc["@mozilla.org/consoleservice;1"].
                                 getService(Ci.nsIConsoleService);
         return this.__logService;
     },
@@ -673,63 +673,12 @@ LoginManagerStorage_legacy.prototype = {
             this.log("Failed to decrypt string: " + cipherText);
         }
 
         return plainText;
     },
 
 }; // end of nsLoginManagerStorage_legacy implementation
 
-
-
-
-// Boilerplate code for component registration...
-var gModule = {
-    registerSelf: function(componentManager, fileSpec, location, type) {
-        componentManager = componentManager.QueryInterface(
-                                                Ci.nsIComponentRegistrar);
-        for each (var obj in this._objects) 
-            componentManager.registerFactoryLocation(obj.CID,
-                    obj.className, obj.contractID,
-                    fileSpec, location, type);
-    },
-
-    unregisterSelf: function (componentManager, location, type) {
-        for each (var obj in this._objects) 
-            componentManager.unregisterFactoryLocation(obj.CID, location);
-    },
-    
-    getClassObject: function(componentManager, cid, iid) {
-        if (!iid.equals(Ci.nsIFactory))
-            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-  
-        for (var key in this._objects) {
-            if (cid.equals(this._objects[key].CID))
-                return this._objects[key].factory;
-        }
-    
-        throw Components.results.NS_ERROR_NO_INTERFACE;
-    },
-  
-    _objects: {
-        service: {
-            CID : Components.ID("{e09e4ca6-276b-4bb4-8b71-0635a3a2a007}"),
-            contractID : "@mozilla.org/login-manager/storage/legacy;1",
-            className  : "LoginManagerStorage_legacy",
-            factory    : aFactory = {
-                createInstance: function(aOuter, aIID) {
-                    if (aOuter != null)
-                        throw Components.results.NS_ERROR_NO_AGGREGATION;
-                    var svc = new LoginManagerStorage_legacy();
-                    return svc.QueryInterface(aIID);
-                }
-            }
-        }
-    },
-  
-    canUnload: function(componentManager) {
-        return true;
-    }
-};
-
+var component = [LoginManagerStorage_legacy];
 function NSGetModule(compMgr, fileSpec) {
-    return gModule;
+    return XPCOMUtils.generateModule(component);
 }