Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / xpcom / components / nsComponentManager.h
blobd6eee6c161ea9e5ea0397710f1ca7735ab032896
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsComponentManager_h__
39 #define nsComponentManager_h__
41 #include "nsXPCOM.h"
43 #include "xpcom-private.h"
44 #include "nsIComponentManager.h"
45 #include "nsIComponentRegistrar.h"
46 #include "nsIServiceManager.h"
47 #include "nsILocalFile.h"
48 #include "mozilla/Module.h"
49 #include "mozilla/ModuleLoader.h"
50 #include "nsXULAppAPI.h"
51 #include "nsNativeComponentLoader.h"
52 #include "nsIFactory.h"
53 #include "nsIInterfaceRequestor.h"
54 #include "nsIInterfaceRequestorUtils.h"
55 #include "pldhash.h"
56 #include "prtime.h"
57 #include "prmon.h"
58 #include "nsCOMPtr.h"
59 #include "nsAutoPtr.h"
60 #include "nsWeakReference.h"
61 #include "nsIFile.h"
62 #include "plarena.h"
63 #include "nsCOMArray.h"
64 #include "nsDataHashtable.h"
65 #include "nsInterfaceHashtable.h"
66 #include "nsClassHashtable.h"
67 #include "nsTArray.h"
69 #include "mozilla/Omnijar.h"
70 #include "nsIZipReader.h"
72 struct nsFactoryEntry;
73 class nsIServiceManager;
74 struct PRThread;
76 #define NS_COMPONENTMANAGER_CID \
77 { /* 91775d60-d5dc-11d2-92fb-00e09805570f */ \
78 0x91775d60, \
79 0xd5dc, \
80 0x11d2, \
81 {0x92, 0xfb, 0x00, 0xe0, 0x98, 0x05, 0x57, 0x0f} \
84 /* keys for registry use */
85 extern const char xpcomKeyName[];
86 extern const char xpcomComponentsKeyName[];
87 extern const char lastModValueName[];
88 extern const char fileSizeValueName[];
89 extern const char nativeComponentType[];
90 extern const char staticComponentType[];
92 #ifdef DEBUG
93 #define XPCOM_CHECK_PENDING_CIDS
94 #endif
95 ////////////////////////////////////////////////////////////////////////////////
97 extern const mozilla::Module kXPCOMModule;
99 // Array of Loaders and their type strings
100 struct nsLoaderdata {
101 nsCOMPtr<mozilla::ModuleLoader> loader;
102 nsCString type;
105 class nsComponentManagerImpl
106 : public nsIComponentManager
107 , public nsIServiceManager
108 , public nsSupportsWeakReference
109 , public nsIComponentRegistrar
110 , public nsIInterfaceRequestor
112 public:
113 NS_DECL_ISUPPORTS
114 NS_DECL_NSIINTERFACEREQUESTOR
115 NS_DECL_NSICOMPONENTMANAGER
116 NS_DECL_NSICOMPONENTREGISTRAR
118 static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
120 nsresult RegistryLocationForFile(nsIFile* aFile,
121 nsCString& aResult);
122 nsresult FileForRegistryLocation(const nsCString &aLocation,
123 nsILocalFile **aSpec);
125 NS_DECL_NSISERVICEMANAGER
127 // nsComponentManagerImpl methods:
128 nsComponentManagerImpl();
130 static nsComponentManagerImpl* gComponentManager;
131 nsresult Init();
133 nsresult Shutdown(void);
135 nsresult FreeServices();
137 already_AddRefed<mozilla::ModuleLoader> LoaderForExtension(const nsACString& aExt);
138 nsInterfaceHashtable<nsCStringHashKey, mozilla::ModuleLoader> mLoaderMap;
140 already_AddRefed<nsIFactory> FindFactory(const nsCID& aClass);
141 already_AddRefed<nsIFactory> FindFactory(const char *contractID,
142 PRUint32 aContractIDLen);
144 already_AddRefed<nsIFactory> LoadFactory(nsFactoryEntry *aEntry);
146 nsFactoryEntry *GetFactoryEntry(const char *aContractID,
147 PRUint32 aContractIDLen);
148 nsFactoryEntry *GetFactoryEntry(const nsCID &aClass);
150 nsDataHashtable<nsIDHashKey, nsFactoryEntry*> mFactories;
151 nsDataHashtable<nsCStringHashKey, nsFactoryEntry*> mContractIDs;
153 PRMonitor* mMon;
155 static void InitializeStaticModules();
156 static void InitializeModuleLocations();
158 struct ComponentLocation
160 NSLocationType type;
161 nsCOMPtr<nsILocalFile> location;
162 bool jar;
165 static nsTArray<const mozilla::Module*>* sStaticModules;
166 static nsTArray<ComponentLocation>* sModuleLocations;
167 static nsTArray<ComponentLocation>* sJarModuleLocations;
169 nsNativeModuleLoader mNativeModuleLoader;
171 class KnownModule
173 public:
175 * Static or binary module.
177 KnownModule(const mozilla::Module* aModule, nsILocalFile* aFile)
178 : mModule(aModule)
179 , mFile(aFile)
180 , mLoaded(false)
181 , mFailed(false)
184 KnownModule(nsILocalFile* aFile)
185 : mModule(NULL)
186 , mFile(aFile)
187 , mLoader(NULL)
188 , mLoaded(false)
189 , mFailed(false)
192 KnownModule(nsILocalFile* aFile, const nsACString& aPath)
193 : mModule(NULL)
194 , mFile(aFile)
195 , mPath(aPath)
196 , mLoader(NULL)
197 , mLoaded(false)
198 , mFailed(false)
201 ~KnownModule()
203 if (mLoaded && mModule->unloadProc)
204 mModule->unloadProc();
207 bool EnsureLoader();
208 bool Load();
210 const mozilla::Module* Module() const
212 return mModule;
216 * For error logging, get a description of this module, either the
217 * file path, or <static module>.
219 nsCString Description() const;
221 private:
222 const mozilla::Module* mModule;
223 nsCOMPtr<nsILocalFile> mFile;
224 nsCString mPath;
225 nsCOMPtr<mozilla::ModuleLoader> mLoader;
226 bool mLoaded;
227 bool mFailed;
230 // The KnownModule is kept alive by these members, it is
231 // referenced by pointer from the factory entries.
232 nsTArray< nsAutoPtr<KnownModule> > mKnownStaticModules;
233 nsClassHashtable<nsHashableHashKey, KnownModule> mKnownFileModules;
234 // The key is a string in this format "<jar path>|<path within jar>"
235 nsClassHashtable<nsCStringHashKey, KnownModule> mKnownJARModules;
237 void RegisterModule(const mozilla::Module* aModule,
238 nsILocalFile* aFile);
239 void RegisterCIDEntry(const mozilla::Module::CIDEntry* aEntry,
240 KnownModule* aModule);
241 void RegisterContractID(const mozilla::Module::ContractIDEntry* aEntry);
243 void RegisterJarManifest(nsIZipReader* aReader,
244 const char* aPath, bool aChromeOnly);
246 void RegisterManifestFile(NSLocationType aType, nsILocalFile* aFile,
247 bool aChromeOnly);
249 struct ManifestProcessingContext
251 ManifestProcessingContext(NSLocationType aType, nsILocalFile* aFile, bool aChromeOnly)
252 : mType(aType)
253 , mFile(aFile)
254 , mPath(NULL)
255 , mChromeOnly(aChromeOnly)
258 ManifestProcessingContext(NSLocationType aType, nsIZipReader* aReader, const char* aPath, bool aChromeOnly)
259 : mType(aType)
260 , mReader(aReader)
261 , mPath(aPath)
262 , mChromeOnly(aChromeOnly)
264 nsCOMPtr<nsIFile> file;
265 aReader->GetFile(getter_AddRefs(file));
266 nsCOMPtr<nsILocalFile> localfile = do_QueryInterface(file);
267 mFile = localfile;
270 ~ManifestProcessingContext() { }
272 NSLocationType mType;
273 nsILocalFile* mFile;
274 nsIZipReader* mReader;
275 const char* mPath;
276 bool mChromeOnly;
279 void ManifestManifest(ManifestProcessingContext& cx, int lineno, char *const * argv);
280 void ManifestBinaryComponent(ManifestProcessingContext& cx, int lineno, char *const * argv);
281 void ManifestXPT(ManifestProcessingContext& cx, int lineno, char *const * argv);
282 void ManifestComponent(ManifestProcessingContext& cx, int lineno, char *const * argv);
283 void ManifestContract(ManifestProcessingContext& cx, int lineno, char* const * argv);
284 void ManifestCategory(ManifestProcessingContext& cx, int lineno, char* const * argv);
286 void RereadChromeManifests();
288 // Shutdown
289 enum {
290 NOT_INITIALIZED,
291 NORMAL,
292 SHUTDOWN_IN_PROGRESS,
293 SHUTDOWN_COMPLETE
294 } mStatus;
296 nsTArray<nsLoaderdata> mLoaderData;
298 PLArenaPool mArena;
300 struct PendingServiceInfo {
301 const nsCID* cid;
302 PRThread* thread;
305 inline PendingServiceInfo* AddPendingService(const nsCID& aServiceCID,
306 PRThread* aThread);
307 inline void RemovePendingService(const nsCID& aServiceCID);
308 inline PRThread* GetPendingServiceThread(const nsCID& aServiceCID) const;
310 nsTArray<PendingServiceInfo> mPendingServices;
312 private:
313 ~nsComponentManagerImpl();
317 #define NS_MAX_FILENAME_LEN 1024
319 #define NS_ERROR_IS_DIR NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_XPCOM, 24)
321 struct nsFactoryEntry
323 nsFactoryEntry(const mozilla::Module::CIDEntry* entry,
324 nsComponentManagerImpl::KnownModule* module);
326 // nsIComponentRegistrar.registerFactory support
327 nsFactoryEntry(const nsCID& aClass, nsIFactory* factory);
329 ~nsFactoryEntry();
331 already_AddRefed<nsIFactory> GetFactory();
333 const mozilla::Module::CIDEntry* mCIDEntry;
334 nsComponentManagerImpl::KnownModule* mModule;
336 nsCOMPtr<nsIFactory> mFactory;
337 nsCOMPtr<nsISupports> mServiceObject;
340 #endif // nsComponentManager_h__