Back to index

lightning-sunbird  0.9+nobinonly
OJITestLoaderFactory.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "nsIModule.h"
00038 #include "nsIGenericFactory.h"
00039 
00040 #include "nsIComponentManager.h"
00041 #include "nsCOMPtr.h"
00042 #include "nsIServiceManager.h"
00043 
00044 #include "OJITestLoader.h"
00045 #include "OJITestLoaderFactory.h"
00046 
00047 static NS_DEFINE_IID(kISupportsIID,    NS_ISUPPORTS_IID);
00048 static NS_DEFINE_IID(kIServiceManagerIID,    NS_ISERVICEMANAGER_IID);
00049 static NS_DEFINE_IID(kIFactoryIID,     NS_IFACTORY_IID);
00050 static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
00051 static NS_DEFINE_CID(kOJITestLoaderCID, OJITESTLOADER_CID);
00052 
00053 static const char* loaderRegistryPath = "@mozilla.org/oji/test/api/loader;1";
00054 static const char* loaderRegistryDesc = "OJI API Test Loader";
00055 
00056 // The list of components we register
00057 static const nsModuleComponentInfo components[] = 
00058 {
00059     { loaderRegistryDesc, 
00060       OJITESTLOADER_CID,  
00061       loaderRegistryPath, 
00062       OJITestLoader::Create
00063     },
00064 };
00065 
00066 NS_IMPL_NSGETMODULE(OJITestLoader, components)
00067 
00068 extern "C" NS_EXPORT nsresult NSRegisterSelf(nsISupports* aServMgr, 
00069                                              const char *path)
00070 {
00071     printf("Registering OJITestLoader\n");
00072 
00073     nsresult rv;
00074 
00075     nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
00076     if (NS_FAILED(rv)) {
00077         return rv;
00078     }
00079 
00080     nsIComponentManager* compMgr;
00081     rv = servMgr->GetService(kComponentManagerCID, 
00082                              nsIComponentManager::GetIID(), 
00083                              (nsISupports**)&compMgr);
00084     if (NS_FAILED(rv)) {
00085         return rv;
00086     }
00087 
00088     rv = compMgr->RegisterComponent(kOJITestLoaderCID,
00089                                     loaderRegistryDesc,
00090                                     loaderRegistryPath,
00091                                     path, 
00092                                     PR_TRUE, PR_TRUE);
00093 
00094     (void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
00095     return rv;
00096 }
00097 
00098 extern "C" NS_EXPORT nsresult NSUnregisterSelf(nsISupports* aServMgr, 
00099                                                const char *path)
00100 {
00101     nsresult rv;
00102     
00103     nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
00104     if (NS_FAILED(rv)) {
00105         return rv;
00106     }
00107 
00108     nsIComponentManager* compMgr;
00109     rv = servMgr->GetService(kComponentManagerCID, 
00110                              nsIComponentManager::GetIID(), 
00111                              (nsISupports**)&compMgr);
00112     if (NS_FAILED(rv)) {
00113         return rv;
00114     }
00115     
00116     rv = compMgr->UnregisterComponent(kOJITestLoaderCID, path);
00117 
00118     (void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
00119     return rv;
00120 }
00121 
00122 extern "C" NS_EXPORT nsresult
00123 NSGetFactory(nsISupports* serviceMgr,
00124              const nsCID &aClass,
00125              const char *aClassName,
00126              const char *aContractID,
00127              nsIFactory **aFactory)
00128 {
00129     if (!aClass.Equals(kOJITestLoaderCID)) {
00130         return NS_ERROR_FACTORY_NOT_LOADED;     // XXX right error?
00131     }
00132 
00133     OJITestLoaderFactory* factory = new OJITestLoaderFactory();
00134     if (factory == NULL) {
00135         return NS_ERROR_OUT_OF_MEMORY;
00136     }
00137     
00138     NS_ADDREF(factory);
00139     *aFactory = factory;
00140     
00141     return NS_OK;
00142 }
00143 
00144 extern "C" NS_EXPORT PRBool
00145 NSCanUnload(nsISupports* serviceMgr)
00146 {
00147     return PR_FALSE;
00148 }
00149 
00150 NS_METHOD
00151 OJITestLoaderFactory::QueryInterface(const nsIID& aIID, void** aInstancePtr) 
00152 {
00153   //PR_ASSERT(NULL != aInstancePtr); 
00154     if (NULL == aInstancePtr) {
00155         return NS_ERROR_NULL_POINTER; 
00156     } 
00157     if (aIID.Equals(kIFactoryIID) ||
00158         aIID.Equals(kISupportsIID)) {
00159         *aInstancePtr = (void*) this; 
00160         AddRef(); 
00161         return NS_OK; 
00162     }
00163     return NS_NOINTERFACE; 
00164 }
00165 
00166 NS_IMPL_ADDREF(OJITestLoaderFactory)
00167 NS_IMPL_RELEASE(OJITestLoaderFactory)
00168 
00169 NS_METHOD
00170 OJITestLoaderFactory::CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00171 {
00172     OJITestLoader* loader = new OJITestLoader();
00173     if (loader == NULL) 
00174         return NS_ERROR_OUT_OF_MEMORY;
00175     loader->AddRef();
00176     *aResult = loader;
00177     return NS_OK;
00178 }
00179 
00180 
00181 NS_METHOD
00182 OJITestLoaderFactory::LockFactory(PRBool aLock)
00183 {
00184    return NS_OK;
00185 }
00186 
00187 OJITestLoaderFactory::OJITestLoaderFactory(void)
00188 {
00189 }
00190 
00191 OJITestLoaderFactory::~OJITestLoaderFactory()
00192 {
00193 }