Back out due to mochiserver breakage. (no_r=me)
[mozilla-central.git] / xpcom / components / nsComponentManager.h
blob3cf8e21e1395a2740526e361cada3f3e02d1f498
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 #ifdef MOZ_OMNIJAR
70 #include "mozilla/Omnijar.h"
71 #include "nsManifestZIPLoader.h"
72 #endif
74 struct nsFactoryEntry;
75 class nsIServiceManager;
76 struct PRThread;
78 #define NS_COMPONENTMANAGER_CID \
79 { /* 91775d60-d5dc-11d2-92fb-00e09805570f */ \
80 0x91775d60, \
81 0xd5dc, \
82 0x11d2, \
83 {0x92, 0xfb, 0x00, 0xe0, 0x98, 0x05, 0x57, 0x0f} \
86 /* keys for registry use */
87 extern const char xpcomKeyName[];
88 extern const char xpcomComponentsKeyName[];
89 extern const char lastModValueName[];
90 extern const char fileSizeValueName[];
91 extern const char nativeComponentType[];
92 extern const char staticComponentType[];
94 typedef int LoaderType;
96 // Predefined loader types.
97 #define NS_LOADER_TYPE_NATIVE -1
98 #define NS_LOADER_TYPE_STATIC -2
99 #define NS_LOADER_TYPE_JAR -3
100 #define NS_LOADER_TYPE_INVALID -4
102 #ifdef DEBUG
103 #define XPCOM_CHECK_PENDING_CIDS
104 #endif
105 ////////////////////////////////////////////////////////////////////////////////
107 extern const mozilla::Module kXPCOMModule;
109 // Array of Loaders and their type strings
110 struct nsLoaderdata {
111 nsCOMPtr<mozilla::ModuleLoader> loader;
112 nsCString type;
115 class nsComponentManagerImpl
116 : public nsIComponentManager
117 , public nsIServiceManager
118 , public nsSupportsWeakReference
119 , public nsIComponentRegistrar
120 , public nsIInterfaceRequestor
122 public:
123 NS_DECL_ISUPPORTS
124 NS_DECL_NSIINTERFACEREQUESTOR
125 NS_DECL_NSICOMPONENTMANAGER
126 NS_DECL_NSICOMPONENTREGISTRAR
128 static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
130 nsresult RegistryLocationForFile(nsIFile* aFile,
131 nsCString& aResult);
132 nsresult FileForRegistryLocation(const nsCString &aLocation,
133 nsILocalFile **aSpec);
135 NS_DECL_NSISERVICEMANAGER
137 // nsComponentManagerImpl methods:
138 nsComponentManagerImpl();
140 static nsComponentManagerImpl* gComponentManager;
141 nsresult Init();
143 nsresult Shutdown(void);
145 nsresult FreeServices();
147 already_AddRefed<mozilla::ModuleLoader> LoaderForExtension(const nsACString& aExt);
148 nsInterfaceHashtable<nsCStringHashKey, mozilla::ModuleLoader> mLoaderMap;
150 already_AddRefed<nsIFactory> FindFactory(const nsCID& aClass);
151 already_AddRefed<nsIFactory> FindFactory(const char *contractID,
152 PRUint32 aContractIDLen);
154 already_AddRefed<nsIFactory> LoadFactory(nsFactoryEntry *aEntry);
156 nsFactoryEntry *GetFactoryEntry(const char *aContractID,
157 PRUint32 aContractIDLen);
158 nsFactoryEntry *GetFactoryEntry(const nsCID &aClass);
160 nsDataHashtable<nsIDHashKey, nsFactoryEntry*> mFactories;
161 nsDataHashtable<nsCStringHashKey, nsFactoryEntry*> mContractIDs;
163 PRMonitor* mMon;
165 static void InitializeStaticModules();
166 static void InitializeModuleLocations();
168 struct ComponentLocation
170 NSLocationType type;
171 nsCOMPtr<nsILocalFile> location;
174 static nsTArray<const mozilla::Module*>* sStaticModules;
175 static nsTArray<ComponentLocation>* sModuleLocations;
177 nsNativeModuleLoader mNativeModuleLoader;
179 class KnownModule
181 public:
183 * Static or binary module.
185 KnownModule(const mozilla::Module* aModule, nsILocalFile* aFile)
186 : mModule(aModule)
187 , mFile(aFile)
188 , mLoaded(false)
189 , mFailed(false)
192 KnownModule(nsILocalFile* aFile)
193 : mModule(NULL)
194 , mFile(aFile)
195 , mLoader(NULL)
196 , mLoaded(false)
197 , mFailed(false)
200 #ifdef MOZ_OMNIJAR
201 KnownModule(const nsACString& aPath)
202 : mModule(NULL)
203 , mFile(NULL)
204 , mPath(aPath)
205 , mLoader(NULL)
206 , mLoaded(false)
207 , mFailed(false)
209 #endif
211 ~KnownModule()
213 if (mLoaded && mModule->unloadProc)
214 mModule->unloadProc();
217 bool EnsureLoader();
218 bool Load();
220 const mozilla::Module* Module() const
222 return mModule;
226 * For error logging, get a description of this module, either the
227 * file path, or <static module>.
229 nsCString Description() const;
231 private:
232 const mozilla::Module* mModule;
233 nsCOMPtr<nsILocalFile> mFile;
234 #ifdef MOZ_OMNIJAR
235 nsCString mPath;
236 #endif
237 nsCOMPtr<mozilla::ModuleLoader> mLoader;
238 bool mLoaded;
239 bool mFailed;
242 // The KnownModule is kept alive by these members, it is
243 // referenced by pointer from the factory entries.
244 nsTArray< nsAutoPtr<KnownModule> > mKnownStaticModules;
245 nsClassHashtable<nsHashableHashKey, KnownModule> mKnownFileModules;
246 #ifdef MOZ_OMNIJAR
247 nsClassHashtable<nsCStringHashKey, KnownModule> mKnownJARModules;
248 #endif
250 void RegisterModule(const mozilla::Module* aModule,
251 nsILocalFile* aFile);
252 void RegisterCIDEntry(const mozilla::Module::CIDEntry* aEntry,
253 KnownModule* aModule);
254 void RegisterContractID(const mozilla::Module::ContractIDEntry* aEntry);
256 #ifdef MOZ_OMNIJAR
257 void RegisterOmnijar(const char* aPath, bool aChromeOnly);
258 #endif
260 void RegisterManifestFile(NSLocationType aType, nsILocalFile* aFile,
261 bool aChromeOnly);
263 struct ManifestProcessingContext
265 ManifestProcessingContext(NSLocationType aType, nsILocalFile* aFile, bool aChromeOnly)
266 : mType(aType)
267 , mFile(aFile)
268 , mPath(NULL)
269 , mChromeOnly(aChromeOnly)
272 #ifdef MOZ_OMNIJAR
273 ManifestProcessingContext(NSLocationType aType, const char* aPath, bool aChromeOnly)
274 : mType(aType)
275 , mFile(mozilla::OmnijarPath())
276 , mPath(aPath)
277 , mChromeOnly(aChromeOnly)
279 #endif
281 ~ManifestProcessingContext() { }
283 NSLocationType mType;
284 nsILocalFile* mFile;
285 const char* mPath;
286 bool mChromeOnly;
289 void ManifestManifest(ManifestProcessingContext& cx, int lineno, char *const * argv);
290 void ManifestBinaryComponent(ManifestProcessingContext& cx, int lineno, char *const * argv);
291 void ManifestXPT(ManifestProcessingContext& cx, int lineno, char *const * argv);
292 void ManifestComponent(ManifestProcessingContext& cx, int lineno, char *const * argv);
293 void ManifestContract(ManifestProcessingContext& cx, int lineno, char* const * argv);
294 void ManifestCategory(ManifestProcessingContext& cx, int lineno, char* const * argv);
296 void RereadChromeManifests();
298 // Shutdown
299 enum {
300 NOT_INITIALIZED,
301 NORMAL,
302 SHUTDOWN_IN_PROGRESS,
303 SHUTDOWN_COMPLETE
304 } mStatus;
306 nsTArray<nsLoaderdata> mLoaderData;
308 PLArenaPool mArena;
310 struct PendingServiceInfo {
311 const nsCID* cid;
312 PRThread* thread;
315 inline PendingServiceInfo* AddPendingService(const nsCID& aServiceCID,
316 PRThread* aThread);
317 inline void RemovePendingService(const nsCID& aServiceCID);
318 inline PRThread* GetPendingServiceThread(const nsCID& aServiceCID) const;
320 nsTArray<PendingServiceInfo> mPendingServices;
322 private:
323 ~nsComponentManagerImpl();
325 #ifdef MOZ_OMNIJAR
326 nsAutoPtr<nsManifestZIPLoader> mManifestLoader;
327 #endif
331 #define NS_MAX_FILENAME_LEN 1024
333 #define NS_ERROR_IS_DIR NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_XPCOM, 24)
335 struct nsFactoryEntry
337 nsFactoryEntry(const mozilla::Module::CIDEntry* entry,
338 nsComponentManagerImpl::KnownModule* module);
340 // nsIComponentRegistrar.registerFactory support
341 nsFactoryEntry(const nsCID& aClass, nsIFactory* factory);
343 ~nsFactoryEntry();
345 already_AddRefed<nsIFactory> GetFactory();
347 const mozilla::Module::CIDEntry* mCIDEntry;
348 nsComponentManagerImpl::KnownModule* mModule;
350 nsCOMPtr<nsIFactory> mFactory;
351 nsCOMPtr<nsISupports> mServiceObject;
354 #endif // nsComponentManager_h__