Back to index

lightning-sunbird  0.9+nobinonly
nsStaticComponentLoader.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; 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  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Benjamin Smedberg <benjamin@smedbergs.us>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsXPCOMPrivate.h"
00040 
00041 #include "nsIComponentLoader.h"
00042 #include "nsIModule.h"
00043 
00044 #include "nsVoidArray.h"
00045 #include "pldhash.h"
00046 #include NEW_H
00047 #include "prlog.h"
00048 
00049 #ifdef PR_LOGGING
00050 static PRLogModuleInfo *sLog = PR_NewLogModule("StaticComponentLoader");
00051 #endif
00052 #define LOG(args) PR_LOG(sLog, PR_LOG_DEBUG, args)
00053 
00054 extern const char staticComponentType[];
00055 
00056 struct StaticModuleInfo : public PLDHashEntryHdr {
00057     nsStaticModuleInfo  info;
00058     nsCOMPtr<nsIModule> module;
00059 
00060     // We want to autoregister the components in the order they come to us
00061     // in the static component list, so we keep a linked list.
00062     StaticModuleInfo   *next;
00063 };
00064 
00065 class nsStaticComponentLoader : public nsIComponentLoader
00066 {
00067 public:
00068     NS_DECL_ISUPPORTS
00069     NS_DECL_NSICOMPONENTLOADER
00070 
00071     nsStaticComponentLoader() :
00072         mAutoRegistered(PR_FALSE),
00073         mFirst(nsnull)
00074     { }
00075 
00076     nsresult Init(nsStaticModuleInfo const *aStaticModules,
00077                   PRUint32 aModuleCount);
00078 
00079 private:
00080     ~nsStaticComponentLoader() {
00081         if (mInfoHash.ops)
00082             PL_DHashTableFinish(&mInfoHash);
00083     }
00084 
00085 protected:
00086     nsresult GetInfoFor(const char *aLocation, StaticModuleInfo **retval);
00087 
00088     PRBool                        mAutoRegistered;
00089     nsCOMPtr<nsIComponentManager> mComponentMgr;
00090     PLDHashTable                  mInfoHash;
00091     static PLDHashTableOps        sInfoHashOps;
00092     nsVoidArray                   mDeferredComponents;
00093     StaticModuleInfo             *mFirst;
00094 };
00095 
00096 PR_STATIC_CALLBACK(void)
00097 info_ClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
00098 {
00099     StaticModuleInfo *info = NS_STATIC_CAST(StaticModuleInfo *, entry);
00100     info->module = 0;
00101     info->~StaticModuleInfo();
00102 }
00103 
00104 PR_STATIC_CALLBACK(PRBool)
00105 info_InitEntry(PLDHashTable *table, PLDHashEntryHdr *entry, const void *key)
00106 {
00107     // Construct so that our nsCOMPtr is zeroed, etc.
00108     new (NS_STATIC_CAST(void *, entry)) StaticModuleInfo();
00109     return PR_TRUE;
00110 }
00111 
00112 /* static */ PLDHashTableOps nsStaticComponentLoader::sInfoHashOps = {
00113     PL_DHashAllocTable,    PL_DHashFreeTable,
00114     PL_DHashGetKeyStub,    PL_DHashStringKey, PL_DHashMatchStringKey,
00115     PL_DHashMoveEntryStub, info_ClearEntry,
00116     PL_DHashFinalizeStub,  info_InitEntry
00117 };
00118 
00119 NS_IMPL_THREADSAFE_ISUPPORTS1(nsStaticComponentLoader, nsIComponentLoader)
00120 
00121 nsresult
00122 nsStaticComponentLoader::Init(nsStaticModuleInfo const *aStaticModules,
00123                               PRUint32 aModuleCount)
00124 {
00125     if (!PL_DHashTableInit(&mInfoHash, &sInfoHashOps, nsnull,
00126                            sizeof(StaticModuleInfo), 1024)) {
00127         mInfoHash.ops = nsnull;
00128         return NS_ERROR_OUT_OF_MEMORY;
00129     }
00130 
00131     if (! aStaticModules)
00132         return NS_OK;
00133 
00134     StaticModuleInfo *prev = nsnull;
00135 
00136     for (PRUint32 i = 0; i < aModuleCount; ++i) {
00137         StaticModuleInfo *info =
00138             NS_STATIC_CAST(StaticModuleInfo *,
00139                            PL_DHashTableOperate(&mInfoHash, aStaticModules[i].name,
00140                                                 PL_DHASH_ADD));
00141         if (!info)
00142             return NS_ERROR_OUT_OF_MEMORY;
00143 
00144         info->info = aStaticModules[i];
00145         if (prev)
00146             prev->next = info;
00147         else
00148             mFirst = info;
00149 
00150         prev = info;
00151     }
00152 
00153     return NS_OK;
00154 }
00155 
00156 nsresult
00157 nsStaticComponentLoader::GetInfoFor(const char *aLocation,
00158                                     StaticModuleInfo **retval)
00159 {
00160     nsresult rv;
00161     StaticModuleInfo *info = 
00162         NS_STATIC_CAST(StaticModuleInfo *,
00163                        PL_DHashTableOperate(&mInfoHash, aLocation,
00164                                             PL_DHASH_LOOKUP));
00165 
00166     if (PL_DHASH_ENTRY_IS_FREE(info))
00167         return NS_ERROR_FACTORY_NOT_REGISTERED;
00168 
00169     if (!info->module) {
00170         rv = info->info.getModule(mComponentMgr, nsnull,
00171                              getter_AddRefs(info->module));
00172         LOG(("nSCL: GetInfoFor(\"%s\"): %lx\n", aLocation, rv));
00173         if (NS_FAILED(rv))
00174             return rv;
00175     }
00176 
00177     *retval = info;
00178     return NS_OK;
00179 }
00180 
00181 NS_IMETHODIMP
00182 nsStaticComponentLoader::Init(nsIComponentManager *mgr, nsISupports *aReg)
00183 {
00184     mComponentMgr = mgr;
00185     return NS_OK;
00186 }
00187 
00188 NS_IMETHODIMP
00189 nsStaticComponentLoader::AutoRegisterComponents(PRInt32 when, nsIFile *dir)
00190 {
00191     if (mAutoRegistered)
00192         return NS_OK;
00193 
00194     // if a directory has been explicitly specified, then return early.  we
00195     // don't load static components from disk ;)
00196     if (dir)
00197         return NS_OK;
00198 
00199     nsresult rv;
00200     StaticModuleInfo *info = mFirst;
00201 
00202     while (info) {
00203         if (!info->module) {
00204             rv = info->info.getModule(mComponentMgr, nsnull, getter_AddRefs(info->module));
00205             LOG(("nSCL: getModule(\"%s\"): %lx\n", info->info.name, rv));
00206             if (NS_FAILED(rv) || !info->module)
00207                 continue; // oh well
00208         }
00209 
00210         rv = info->module->RegisterSelf(mComponentMgr, nsnull, info->info.name,
00211                                         staticComponentType);
00212         LOG(("nSCL: autoreg of \"%s\": %lx\n", info->info.name, rv));
00213 
00214         if (rv == NS_ERROR_FACTORY_REGISTER_AGAIN)
00215             mDeferredComponents.AppendElement(info);
00216 
00217         info = info->next;
00218     }
00219 
00220     mAutoRegistered = PR_TRUE;
00221     return NS_OK;
00222 }
00223 
00224 NS_IMETHODIMP
00225 nsStaticComponentLoader::AutoUnregisterComponent(PRInt32 when, 
00226                                                  nsIFile *component,
00227                                                  PRBool *retval)
00228 {
00229     *retval = PR_FALSE;
00230     return NS_OK;
00231 }
00232 
00233 NS_IMETHODIMP
00234 nsStaticComponentLoader::AutoRegisterComponent(PRInt32 when, nsIFile *component,
00235                                                PRBool *retval)
00236 {
00237     *retval = PR_FALSE;
00238     return NS_OK;
00239 }
00240 
00241 NS_IMETHODIMP
00242 nsStaticComponentLoader::RegisterDeferredComponents(PRInt32 when,
00243                                                     PRBool *aRegistered)
00244 {
00245     *aRegistered = PR_FALSE;
00246     if (!mDeferredComponents.Count())
00247         return NS_OK;
00248 
00249     for (int i = mDeferredComponents.Count() - 1; i >= 0; i--) {
00250         StaticModuleInfo *info = NS_STATIC_CAST(StaticModuleInfo *,
00251                                                 mDeferredComponents[i]);
00252         nsresult rv = info->module->RegisterSelf(mComponentMgr, nsnull,
00253                                                  info->info.name,
00254                                                  staticComponentType);
00255         if (rv != NS_ERROR_FACTORY_REGISTER_AGAIN) {
00256             if (NS_SUCCEEDED(rv))
00257                 *aRegistered = PR_TRUE;
00258             mDeferredComponents.RemoveElementAt(i);
00259         }
00260     }
00261     return NS_OK;
00262 }
00263 
00264 NS_IMETHODIMP
00265 nsStaticComponentLoader::OnRegister(const nsCID &aCID, const char *aType,
00266                                     const char *aClassName,
00267                                     const char *aContractID,
00268                                     const char *aLocation,
00269                                     PRBool aReplace, PRBool aPersist)
00270 {
00271     return NS_OK;
00272 }
00273 
00274 NS_IMETHODIMP
00275 nsStaticComponentLoader::UnloadAll(PRInt32 aWhen)
00276 {
00277     return NS_OK;
00278 }
00279 
00280 NS_IMETHODIMP
00281 nsStaticComponentLoader::GetFactory(const nsCID &aCID, const char *aLocation,
00282                                     const char *aType, nsIFactory **_retval)
00283 {
00284     StaticModuleInfo *info;
00285     nsresult rv;
00286 
00287     if (NS_FAILED(rv = GetInfoFor(aLocation, &info)))
00288         return rv;
00289 
00290     return info->module->GetClassObject(mComponentMgr, aCID,
00291                                         NS_GET_IID(nsIFactory),
00292                                         (void **)_retval);
00293 }
00294 
00295 nsresult
00296 NewStaticComponentLoader(nsStaticModuleInfo const *aStaticModules,
00297                          PRUint32 aStaticModuleCount,
00298                          nsIComponentLoader **retval)
00299 {
00300     nsCOMPtr<nsStaticComponentLoader> lthis = new nsStaticComponentLoader();
00301     if (!lthis)
00302         return NS_ERROR_OUT_OF_MEMORY;
00303 
00304     nsresult rv = ((nsStaticComponentLoader*) lthis)->
00305         Init(aStaticModules, aStaticModuleCount);
00306 
00307     if (NS_FAILED(rv))
00308         return rv;
00309 
00310     NS_ADDREF(*retval = lthis);
00311     return NS_OK;
00312 }