1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
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/
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
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.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
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.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include <iprt/initterm.h>
|
---|
39 | #include <iprt/time.h>
|
---|
40 |
|
---|
41 | #include "nsXPCOM.h"
|
---|
42 | #include "nsXPCOMPrivate.h"
|
---|
43 | #include "nscore.h"
|
---|
44 | #include "nsCOMPtr.h"
|
---|
45 | #include "nsObserverList.h"
|
---|
46 | #include "nsObserverService.h"
|
---|
47 | #include "nsProperties.h"
|
---|
48 | #include "nsIProperties.h"
|
---|
49 |
|
---|
50 | #include "nsDebugImpl.h"
|
---|
51 | #include "nsTraceRefcntImpl.h"
|
---|
52 | #include "nsErrorService.h"
|
---|
53 |
|
---|
54 | #include "nsSupportsArray.h"
|
---|
55 | #include "nsArray.h"
|
---|
56 | #include "nsSupportsPrimitives.h"
|
---|
57 | #include "nsExceptionService.h"
|
---|
58 |
|
---|
59 | #include "nsComponentManager.h"
|
---|
60 | #include "nsCategoryManagerUtils.h"
|
---|
61 | #include "nsIServiceManager.h"
|
---|
62 | #include "nsGenericFactory.h"
|
---|
63 |
|
---|
64 | #include "nsEventQueueService.h"
|
---|
65 | #include "nsEventQueue.h"
|
---|
66 | #ifdef VBOX
|
---|
67 | # include "nsEventQueueUtils.h"
|
---|
68 | # include "nsProxyRelease.h"
|
---|
69 | #endif /* VBOX */
|
---|
70 |
|
---|
71 | #include "nsIProxyObjectManager.h"
|
---|
72 | #include "nsProxyEventPrivate.h" // access to the impl of nsProxyObjectManager for the generic factory registration.
|
---|
73 |
|
---|
74 | #include "xptinfo.h"
|
---|
75 | #include "nsIInterfaceInfoManager.h"
|
---|
76 |
|
---|
77 | #include "nsEmptyEnumerator.h"
|
---|
78 |
|
---|
79 | #include "nsILocalFile.h"
|
---|
80 | #include "nsLocalFile.h"
|
---|
81 | #if defined(XP_UNIX) || defined(XP_OS2)
|
---|
82 | #include "nsNativeCharsetUtils.h"
|
---|
83 | #endif
|
---|
84 | #include "nsDirectoryService.h"
|
---|
85 | #include "nsDirectoryServiceDefs.h"
|
---|
86 | #include "nsCategoryManager.h"
|
---|
87 | #include "nsICategoryManager.h"
|
---|
88 |
|
---|
89 | #include "nsAtomService.h"
|
---|
90 | #include "nsAtomTable.h"
|
---|
91 | #include "nsTraceRefcnt.h"
|
---|
92 |
|
---|
93 | #include "nsVariant.h"
|
---|
94 |
|
---|
95 | #include "SpecialSystemDirectory.h"
|
---|
96 |
|
---|
97 | #include "ipcdclient.h"
|
---|
98 | #include "ipcService.h"
|
---|
99 | #include "ipcConfig.h"
|
---|
100 | #include "ipcCID.h"
|
---|
101 | #include "ipcDConnectService.h"
|
---|
102 |
|
---|
103 | #include <locale.h>
|
---|
104 |
|
---|
105 | // Registry Factory creation function defined in nsRegistry.cpp
|
---|
106 | // We hook into this function locally to create and register the registry
|
---|
107 | // Since noone outside xpcom needs to know about this and nsRegistry.cpp
|
---|
108 | // does not have a local include file, we are putting this definition
|
---|
109 | // here rather than in nsIRegistry.h
|
---|
110 | extern nsresult NS_RegistryGetFactory(nsIFactory** aFactory);
|
---|
111 | extern nsresult NS_CategoryManagerGetFactory( nsIFactory** );
|
---|
112 |
|
---|
113 | static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
|
---|
114 | static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
---|
115 |
|
---|
116 | NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsEventQueueServiceImpl, Init)
|
---|
117 |
|
---|
118 | static RTTHREAD g_hMainThread = 0;
|
---|
119 |
|
---|
120 | #define NS_ENVIRONMENT_CLASSNAME "Environment Service"
|
---|
121 |
|
---|
122 | #include "nsXPCOM.h"
|
---|
123 | // ds/nsISupportsPrimitives
|
---|
124 | #define NS_SUPPORTS_ID_CLASSNAME "Supports ID"
|
---|
125 | #define NS_SUPPORTS_CSTRING_CLASSNAME "Supports String"
|
---|
126 | #define NS_SUPPORTS_STRING_CLASSNAME "Supports WString"
|
---|
127 | #define NS_SUPPORTS_PRBOOL_CLASSNAME "Supports PRBool"
|
---|
128 | #define NS_SUPPORTS_PRUINT8_CLASSNAME "Supports PRUint8"
|
---|
129 | #define NS_SUPPORTS_PRUINT16_CLASSNAME "Supports PRUint16"
|
---|
130 | #define NS_SUPPORTS_PRUINT32_CLASSNAME "Supports PRUint32"
|
---|
131 | #define NS_SUPPORTS_PRUINT64_CLASSNAME "Supports PRUint64"
|
---|
132 | #define NS_SUPPORTS_PRTIME_CLASSNAME "Supports PRTime"
|
---|
133 | #define NS_SUPPORTS_CHAR_CLASSNAME "Supports Char"
|
---|
134 | #define NS_SUPPORTS_PRINT16_CLASSNAME "Supports PRInt16"
|
---|
135 | #define NS_SUPPORTS_PRINT32_CLASSNAME "Supports PRInt32"
|
---|
136 | #define NS_SUPPORTS_PRINT64_CLASSNAME "Supports PRInt64"
|
---|
137 | #define NS_SUPPORTS_FLOAT_CLASSNAME "Supports float"
|
---|
138 | #define NS_SUPPORTS_DOUBLE_CLASSNAME "Supports double"
|
---|
139 | #define NS_SUPPORTS_VOID_CLASSNAME "Supports void"
|
---|
140 | #define NS_SUPPORTS_INTERFACE_POINTER_CLASSNAME "Supports interface pointer"
|
---|
141 |
|
---|
142 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsIDImpl)
|
---|
143 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsStringImpl)
|
---|
144 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsCStringImpl)
|
---|
145 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRBoolImpl)
|
---|
146 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRUint8Impl)
|
---|
147 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRUint16Impl)
|
---|
148 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRUint32Impl)
|
---|
149 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRUint64Impl)
|
---|
150 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRTimeImpl)
|
---|
151 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsCharImpl)
|
---|
152 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRInt16Impl)
|
---|
153 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRInt32Impl)
|
---|
154 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRInt64Impl)
|
---|
155 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsFloatImpl)
|
---|
156 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsDoubleImpl)
|
---|
157 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsVoidImpl)
|
---|
158 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsInterfacePointerImpl)
|
---|
159 |
|
---|
160 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsArray)
|
---|
161 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsAtomService)
|
---|
162 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsExceptionService)
|
---|
163 |
|
---|
164 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsVariant)
|
---|
165 |
|
---|
166 | static NS_METHOD
|
---|
167 | nsXPTIInterfaceInfoManagerGetSingleton(nsISupports* outer,
|
---|
168 | const nsIID& aIID,
|
---|
169 | void* *aInstancePtr)
|
---|
170 | {
|
---|
171 | NS_ENSURE_ARG_POINTER(aInstancePtr);
|
---|
172 | NS_ENSURE_TRUE(!outer, NS_ERROR_NO_AGGREGATION);
|
---|
173 |
|
---|
174 | nsCOMPtr<nsIInterfaceInfoManager> iim(dont_AddRef(XPTI_GetInterfaceInfoManager()));
|
---|
175 | if (!iim) {
|
---|
176 | return NS_ERROR_FAILURE;
|
---|
177 | }
|
---|
178 |
|
---|
179 | return iim->QueryInterface(aIID, aInstancePtr);
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | PR_STATIC_CALLBACK(nsresult)
|
---|
184 | RegisterGenericFactory(nsIComponentRegistrar* registrar,
|
---|
185 | const nsModuleComponentInfo *info)
|
---|
186 | {
|
---|
187 | nsresult rv;
|
---|
188 | nsIGenericFactory* fact;
|
---|
189 | rv = NS_NewGenericFactory(&fact, info);
|
---|
190 | if (NS_FAILED(rv)) return rv;
|
---|
191 |
|
---|
192 | rv = registrar->RegisterFactory(info->mCID,
|
---|
193 | info->mDescription,
|
---|
194 | info->mContractID,
|
---|
195 | fact);
|
---|
196 | NS_RELEASE(fact);
|
---|
197 | return rv;
|
---|
198 | }
|
---|
199 |
|
---|
200 | // In order to support the installer, we need
|
---|
201 | // to be told out of band if we should cause
|
---|
202 | // an autoregister. If the file ".autoreg" exists in the binary
|
---|
203 | // directory, we check its timestamp against the timestamp of the
|
---|
204 | // compreg.dat file. If the .autoreg file is newer, we autoregister.
|
---|
205 | static PRBool CheckUpdateFile()
|
---|
206 | {
|
---|
207 | nsresult rv;
|
---|
208 | nsCOMPtr<nsIProperties> directoryService;
|
---|
209 | nsDirectoryService::Create(nsnull,
|
---|
210 | NS_GET_IID(nsIProperties),
|
---|
211 | getter_AddRefs(directoryService));
|
---|
212 |
|
---|
213 | if (!directoryService)
|
---|
214 | return PR_FALSE;
|
---|
215 |
|
---|
216 | nsCOMPtr<nsIFile> file;
|
---|
217 | rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
|
---|
218 | NS_GET_IID(nsIFile),
|
---|
219 | getter_AddRefs(file));
|
---|
220 |
|
---|
221 | if (NS_FAILED(rv)) {
|
---|
222 | NS_WARNING("Getting NS_XPCOM_CURRENT_PROCESS_DIR failed");
|
---|
223 | return PR_FALSE;
|
---|
224 | }
|
---|
225 |
|
---|
226 | file->AppendNative(nsDependentCString(".autoreg"));
|
---|
227 |
|
---|
228 | PRBool exists;
|
---|
229 | file->Exists(&exists);
|
---|
230 | if (!exists)
|
---|
231 | return PR_FALSE;
|
---|
232 |
|
---|
233 | nsCOMPtr<nsIFile> compregFile;
|
---|
234 | rv = directoryService->Get(NS_XPCOM_COMPONENT_REGISTRY_FILE,
|
---|
235 | NS_GET_IID(nsIFile),
|
---|
236 | getter_AddRefs(compregFile));
|
---|
237 |
|
---|
238 |
|
---|
239 | if (NS_FAILED(rv)) {
|
---|
240 | NS_WARNING("Getting NS_XPCOM_COMPONENT_REGISTRY_FILE failed");
|
---|
241 | return PR_FALSE;
|
---|
242 | }
|
---|
243 |
|
---|
244 | // Don't need to check whether compreg exists; if it doesn't
|
---|
245 | // we won't even be here.
|
---|
246 |
|
---|
247 | PRInt64 compregModTime, autoregModTime;
|
---|
248 | compregFile->GetLastModifiedTime(&compregModTime);
|
---|
249 | file->GetLastModifiedTime(&autoregModTime);
|
---|
250 |
|
---|
251 | return LL_CMP(autoregModTime, >, compregModTime);
|
---|
252 | }
|
---|
253 |
|
---|
254 | nsComponentManagerImpl* nsComponentManagerImpl::gComponentManager = NULL;
|
---|
255 | nsIProperties *gDirectoryService = NULL;
|
---|
256 | PRBool gXPCOMShuttingDown = PR_FALSE;
|
---|
257 | #ifdef VBOX
|
---|
258 | static PRBool gXPCOMInitialized = PR_FALSE;
|
---|
259 | #endif
|
---|
260 |
|
---|
261 | // For each class that wishes to support nsIClassInfo, add a line like this
|
---|
262 | // NS_DECL_CLASSINFO(nsMyClass)
|
---|
263 |
|
---|
264 | #define COMPONENT(NAME, Ctor) \
|
---|
265 | { NS_##NAME##_CLASSNAME, NS_##NAME##_CID, NS_##NAME##_CONTRACTID, Ctor }
|
---|
266 |
|
---|
267 | #define COMPONENT_CI(NAME, Ctor, Class) \
|
---|
268 | { NS_##NAME##_CLASSNAME, NS_##NAME##_CID, NS_##NAME##_CONTRACTID, Ctor, \
|
---|
269 | NULL, NULL, NULL, NS_CI_INTERFACE_GETTER_NAME(Class), NULL, \
|
---|
270 | &NS_CLASSINFO_NAME(Class) }
|
---|
271 |
|
---|
272 | static const nsModuleComponentInfo components[] = {
|
---|
273 | COMPONENT(DEBUG, nsDebugImpl::Create),
|
---|
274 | #define NS_ERRORSERVICE_CLASSNAME NS_ERRORSERVICE_NAME
|
---|
275 | COMPONENT(ERRORSERVICE, nsErrorService::Create),
|
---|
276 |
|
---|
277 | #define NS_PROPERTIES_CLASSNAME "Properties"
|
---|
278 | COMPONENT(PROPERTIES, nsProperties::Create),
|
---|
279 |
|
---|
280 | COMPONENT(SUPPORTSARRAY, nsSupportsArray::Create),
|
---|
281 | COMPONENT(ARRAY, nsArrayConstructor),
|
---|
282 | COMPONENT(EXCEPTIONSERVICE, nsExceptionServiceConstructor),
|
---|
283 | COMPONENT(ATOMSERVICE, nsAtomServiceConstructor),
|
---|
284 | COMPONENT(OBSERVERSERVICE, nsObserverService::Create),
|
---|
285 | COMPONENT(GENERICFACTORY, nsGenericFactory::Create),
|
---|
286 | COMPONENT(EVENTQUEUESERVICE, nsEventQueueServiceImplConstructor),
|
---|
287 | COMPONENT(EVENTQUEUE, nsEventQueueImpl::Create),
|
---|
288 |
|
---|
289 | #define NS_XPCOMPROXY_CID NS_PROXYEVENT_MANAGER_CID
|
---|
290 | COMPONENT(XPCOMPROXY, nsProxyObjectManager::Create),
|
---|
291 |
|
---|
292 | #define COMPONENT_SUPPORTS(TYPE, Type) \
|
---|
293 | COMPONENT(SUPPORTS_##TYPE, nsSupports##Type##ImplConstructor)
|
---|
294 |
|
---|
295 | COMPONENT_SUPPORTS(ID, ID),
|
---|
296 | COMPONENT_SUPPORTS(STRING, String),
|
---|
297 | COMPONENT_SUPPORTS(CSTRING, CString),
|
---|
298 | COMPONENT_SUPPORTS(PRBOOL, PRBool),
|
---|
299 | COMPONENT_SUPPORTS(PRUINT8, PRUint8),
|
---|
300 | COMPONENT_SUPPORTS(PRUINT16, PRUint16),
|
---|
301 | COMPONENT_SUPPORTS(PRUINT32, PRUint32),
|
---|
302 | COMPONENT_SUPPORTS(PRUINT64, PRUint64),
|
---|
303 | COMPONENT_SUPPORTS(PRTIME, PRTime),
|
---|
304 | COMPONENT_SUPPORTS(CHAR, Char),
|
---|
305 | COMPONENT_SUPPORTS(PRINT16, PRInt16),
|
---|
306 | COMPONENT_SUPPORTS(PRINT32, PRInt32),
|
---|
307 | COMPONENT_SUPPORTS(PRINT64, PRInt64),
|
---|
308 | COMPONENT_SUPPORTS(FLOAT, Float),
|
---|
309 | COMPONENT_SUPPORTS(DOUBLE, Double),
|
---|
310 | COMPONENT_SUPPORTS(VOID, Void),
|
---|
311 | COMPONENT_SUPPORTS(INTERFACE_POINTER, InterfacePointer),
|
---|
312 |
|
---|
313 | #undef COMPONENT_SUPPORTS
|
---|
314 | #define NS_LOCAL_FILE_CLASSNAME "Local File Specification"
|
---|
315 | COMPONENT(LOCAL_FILE, nsLocalFile::nsLocalFileConstructor),
|
---|
316 | #define NS_DIRECTORY_SERVICE_CLASSNAME "nsIFile Directory Service"
|
---|
317 | COMPONENT(DIRECTORY_SERVICE, nsDirectoryService::Create),
|
---|
318 |
|
---|
319 | COMPONENT(VARIANT, nsVariantConstructor),
|
---|
320 | COMPONENT(INTERFACEINFOMANAGER_SERVICE, nsXPTIInterfaceInfoManagerGetSingleton),
|
---|
321 | };
|
---|
322 |
|
---|
323 | #undef COMPONENT
|
---|
324 |
|
---|
325 | const int components_length = sizeof(components) / sizeof(components[0]);
|
---|
326 |
|
---|
327 | // gDebug will be freed during shutdown.
|
---|
328 | static nsIDebug* gDebug = nsnull;
|
---|
329 | nsresult NS_COM NS_GetDebug(nsIDebug** result)
|
---|
330 | {
|
---|
331 | nsresult rv = NS_OK;
|
---|
332 | if (!gDebug)
|
---|
333 | {
|
---|
334 | rv = nsDebugImpl::Create(nsnull,
|
---|
335 | NS_GET_IID(nsIDebug),
|
---|
336 | (void**)&gDebug);
|
---|
337 | }
|
---|
338 | NS_IF_ADDREF(*result = gDebug);
|
---|
339 | return rv;
|
---|
340 | }
|
---|
341 |
|
---|
342 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
343 | // gTraceRefcnt will be freed during shutdown.
|
---|
344 | static nsITraceRefcnt* gTraceRefcnt = nsnull;
|
---|
345 | #endif
|
---|
346 |
|
---|
347 | nsresult NS_COM NS_GetTraceRefcnt(nsITraceRefcnt** result)
|
---|
348 | {
|
---|
349 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
350 | nsresult rv = NS_OK;
|
---|
351 | if (!gTraceRefcnt)
|
---|
352 | {
|
---|
353 | rv = nsTraceRefcntImpl::Create(nsnull,
|
---|
354 | NS_GET_IID(nsITraceRefcnt),
|
---|
355 | (void**)&gTraceRefcnt);
|
---|
356 | }
|
---|
357 | NS_IF_ADDREF(*result = gTraceRefcnt);
|
---|
358 | return rv;
|
---|
359 | #else
|
---|
360 | return NS_ERROR_NOT_INITIALIZED;
|
---|
361 | #endif
|
---|
362 | }
|
---|
363 |
|
---|
364 | #ifdef VBOX
|
---|
365 | PRBool NS_COM NS_IsXPCOMInitialized(void)
|
---|
366 | {
|
---|
367 | return gXPCOMInitialized;
|
---|
368 | }
|
---|
369 | #endif
|
---|
370 |
|
---|
371 | nsresult NS_COM
|
---|
372 | NS_GetMainThread(RTTHREAD *phThreadMain)
|
---|
373 | {
|
---|
374 | NS_ASSERTION(phThreadMain, "bad result pointer");
|
---|
375 | if (g_hMainThread == NIL_RTTHREAD)
|
---|
376 | return NS_ERROR_FAILURE;
|
---|
377 | *phThreadMain = g_hMainThread;
|
---|
378 | return NS_OK;
|
---|
379 | }
|
---|
380 |
|
---|
381 | nsresult NS_COM NS_InitXPCOM2(nsIServiceManager* *result,
|
---|
382 | nsIFile* binDirectory,
|
---|
383 | nsIDirectoryServiceProvider* appFileLocationProvider)
|
---|
384 | {
|
---|
385 | nsresult rv = NS_OK;
|
---|
386 |
|
---|
387 | /* Make sure IPRT is initialized. */
|
---|
388 | RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
|
---|
389 |
|
---|
390 | // We are not shutting down
|
---|
391 | gXPCOMShuttingDown = PR_FALSE;
|
---|
392 |
|
---|
393 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
394 | nsTraceRefcntImpl::Startup();
|
---|
395 | #endif
|
---|
396 |
|
---|
397 | // Establish the main thread here.
|
---|
398 | g_hMainThread = RTThreadSelf();
|
---|
399 |
|
---|
400 | // If the locale hasn't already been setup by our embedder,
|
---|
401 | // get us out of the "C" locale and into the system
|
---|
402 | if (strcmp(setlocale(LC_ALL, NULL), "C") == 0)
|
---|
403 | setlocale(LC_ALL, "");
|
---|
404 |
|
---|
405 | #if defined(XP_UNIX) || defined(XP_OS2)
|
---|
406 | NS_StartupNativeCharsetUtils();
|
---|
407 | #endif
|
---|
408 | NS_StartupLocalFile();
|
---|
409 |
|
---|
410 | StartupSpecialSystemDirectory();
|
---|
411 |
|
---|
412 | // Start the directory service so that the component manager init can use it.
|
---|
413 | rv = nsDirectoryService::Create(nsnull,
|
---|
414 | NS_GET_IID(nsIProperties),
|
---|
415 | (void**)&gDirectoryService);
|
---|
416 | if (NS_FAILED(rv))
|
---|
417 | return rv;
|
---|
418 |
|
---|
419 | nsCOMPtr<nsIDirectoryService> dirService = do_QueryInterface(gDirectoryService, &rv);
|
---|
420 | if (NS_FAILED(rv))
|
---|
421 | return rv;
|
---|
422 | rv = dirService->Init();
|
---|
423 | if (NS_FAILED(rv))
|
---|
424 | return rv;
|
---|
425 |
|
---|
426 | // Create the Component/Service Manager
|
---|
427 | nsComponentManagerImpl *compMgr = NULL;
|
---|
428 |
|
---|
429 | if (nsComponentManagerImpl::gComponentManager == NULL)
|
---|
430 | {
|
---|
431 | compMgr = new nsComponentManagerImpl();
|
---|
432 | if (compMgr == NULL)
|
---|
433 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
434 | NS_ADDREF(compMgr);
|
---|
435 |
|
---|
436 | nsCOMPtr<nsIFile> xpcomLib;
|
---|
437 |
|
---|
438 | PRBool value;
|
---|
439 | if (binDirectory)
|
---|
440 | {
|
---|
441 | rv = binDirectory->IsDirectory(&value);
|
---|
442 |
|
---|
443 | if (NS_SUCCEEDED(rv) && value) {
|
---|
444 | gDirectoryService->Set(NS_XPCOM_INIT_CURRENT_PROCESS_DIR, binDirectory);
|
---|
445 | binDirectory->Clone(getter_AddRefs(xpcomLib));
|
---|
446 | }
|
---|
447 | }
|
---|
448 | else {
|
---|
449 | gDirectoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
|
---|
450 | NS_GET_IID(nsIFile),
|
---|
451 | getter_AddRefs(xpcomLib));
|
---|
452 | }
|
---|
453 |
|
---|
454 | if (xpcomLib) {
|
---|
455 | xpcomLib->AppendNative(nsDependentCString(XPCOM_DLL));
|
---|
456 | gDirectoryService->Set(NS_XPCOM_LIBRARY_FILE, xpcomLib);
|
---|
457 | }
|
---|
458 |
|
---|
459 | if (appFileLocationProvider) {
|
---|
460 | rv = dirService->RegisterProvider(appFileLocationProvider);
|
---|
461 | if (NS_FAILED(rv)) return rv;
|
---|
462 | }
|
---|
463 |
|
---|
464 | rv = compMgr->Init();
|
---|
465 | if (NS_FAILED(rv))
|
---|
466 | {
|
---|
467 | NS_RELEASE(compMgr);
|
---|
468 | return rv;
|
---|
469 | }
|
---|
470 |
|
---|
471 | nsComponentManagerImpl::gComponentManager = compMgr;
|
---|
472 |
|
---|
473 | if (result) {
|
---|
474 | nsIServiceManager *serviceManager =
|
---|
475 | NS_STATIC_CAST(nsIServiceManager*, compMgr);
|
---|
476 |
|
---|
477 | NS_ADDREF(*result = serviceManager);
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | rv = compMgr->RegisterService(kComponentManagerCID, NS_STATIC_CAST(nsIComponentManager*, compMgr));
|
---|
482 | if (NS_FAILED(rv)) return rv;
|
---|
483 |
|
---|
484 | // 2. Register the global services with the component manager so that
|
---|
485 | // clients can create new objects.
|
---|
486 |
|
---|
487 | // Category Manager
|
---|
488 | {
|
---|
489 | nsCOMPtr<nsIFactory> categoryManagerFactory;
|
---|
490 | if ( NS_FAILED(rv = NS_CategoryManagerGetFactory(getter_AddRefs(categoryManagerFactory))) )
|
---|
491 | return rv;
|
---|
492 |
|
---|
493 | NS_DEFINE_CID(kCategoryManagerCID, NS_CATEGORYMANAGER_CID);
|
---|
494 |
|
---|
495 | rv = compMgr->RegisterFactory(kCategoryManagerCID,
|
---|
496 | NS_CATEGORYMANAGER_CLASSNAME,
|
---|
497 | NS_CATEGORYMANAGER_CONTRACTID,
|
---|
498 | categoryManagerFactory,
|
---|
499 | PR_TRUE);
|
---|
500 | if ( NS_FAILED(rv) ) return rv;
|
---|
501 | }
|
---|
502 |
|
---|
503 | // what I want to do here is QI for a Component Registration Manager. Since this
|
---|
504 | // has not been invented yet, QI to the obsolete manager. Kids, don't do this at home.
|
---|
505 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(
|
---|
506 | NS_STATIC_CAST(nsIComponentManager*,compMgr), &rv);
|
---|
507 | if (registrar) {
|
---|
508 | for (int i = 0; i < components_length; i++)
|
---|
509 | RegisterGenericFactory(registrar, &components[i]);
|
---|
510 | }
|
---|
511 | rv = nsComponentManagerImpl::gComponentManager->ReadPersistentRegistry();
|
---|
512 | #ifdef DEBUG
|
---|
513 | if (NS_FAILED(rv)) {
|
---|
514 | printf("No Persistent Registry Found.\n");
|
---|
515 | }
|
---|
516 | #endif
|
---|
517 |
|
---|
518 | #if 0 /// @todo later
|
---|
519 | rv = IPC_Init();
|
---|
520 | if (NS_FAILED(rv))
|
---|
521 | return rv;
|
---|
522 | #endif
|
---|
523 |
|
---|
524 | if ( NS_FAILED(rv) || CheckUpdateFile()) {
|
---|
525 | // if we find no persistent registry, we will try to autoregister
|
---|
526 | // the default components directory.
|
---|
527 | nsComponentManagerImpl::gComponentManager->AutoRegister(nsnull);
|
---|
528 |
|
---|
529 | // If the application is using a GRE, then,
|
---|
530 | // auto register components in the GRE directory as well.
|
---|
531 | //
|
---|
532 | // The application indicates that it's using an GRE by
|
---|
533 | // returning a valid nsIFile when queried (via appFileLocProvider)
|
---|
534 | // for the NS_GRE_DIR atom as shown below
|
---|
535 | //
|
---|
536 |
|
---|
537 | if ( appFileLocationProvider ) {
|
---|
538 | nsCOMPtr<nsIFile> greDir;
|
---|
539 | PRBool persistent = PR_TRUE;
|
---|
540 |
|
---|
541 | appFileLocationProvider->GetFile(NS_GRE_DIR, &persistent, getter_AddRefs(greDir));
|
---|
542 |
|
---|
543 | if (greDir) {
|
---|
544 | #ifdef DEBUG_dougt
|
---|
545 | printf("start - Registering GRE components\n");
|
---|
546 | #endif
|
---|
547 | rv = gDirectoryService->Get(NS_GRE_COMPONENT_DIR,
|
---|
548 | NS_GET_IID(nsIFile),
|
---|
549 | getter_AddRefs(greDir));
|
---|
550 | if (NS_FAILED(rv)) {
|
---|
551 | NS_ERROR("Could not get GRE components directory!");
|
---|
552 | return rv;
|
---|
553 | }
|
---|
554 |
|
---|
555 | // If the GRE contains any loaders, we want to know about it so that we can cause another
|
---|
556 | // autoregistration of the applications component directory.
|
---|
557 | int loaderCount = nsComponentManagerImpl::gComponentManager->GetLoaderCount();
|
---|
558 | rv = nsComponentManagerImpl::gComponentManager->AutoRegister(greDir);
|
---|
559 |
|
---|
560 | if (loaderCount != nsComponentManagerImpl::gComponentManager->GetLoaderCount())
|
---|
561 | nsComponentManagerImpl::gComponentManager->AutoRegisterNonNativeComponents(nsnull);
|
---|
562 |
|
---|
563 | #ifdef DEBUG_dougt
|
---|
564 | printf("end - Registering GRE components\n");
|
---|
565 | #endif
|
---|
566 | if (NS_FAILED(rv)) {
|
---|
567 | NS_ERROR("Could not AutoRegister GRE components");
|
---|
568 | return rv;
|
---|
569 | }
|
---|
570 | }
|
---|
571 | }
|
---|
572 |
|
---|
573 | //
|
---|
574 | // If additional component directories have been specified, then
|
---|
575 | // register them as well.
|
---|
576 | //
|
---|
577 |
|
---|
578 | nsCOMPtr<nsISimpleEnumerator> dirList;
|
---|
579 | gDirectoryService->Get(NS_XPCOM_COMPONENT_DIR_LIST,
|
---|
580 | NS_GET_IID(nsISimpleEnumerator),
|
---|
581 | getter_AddRefs(dirList));
|
---|
582 | if (dirList) {
|
---|
583 | PRBool hasMore;
|
---|
584 | while (NS_SUCCEEDED(dirList->HasMoreElements(&hasMore)) && hasMore) {
|
---|
585 | nsCOMPtr<nsISupports> elem;
|
---|
586 | dirList->GetNext(getter_AddRefs(elem));
|
---|
587 | if (elem) {
|
---|
588 | nsCOMPtr<nsIFile> dir = do_QueryInterface(elem);
|
---|
589 | if (dir)
|
---|
590 | nsComponentManagerImpl::gComponentManager->AutoRegister(dir);
|
---|
591 |
|
---|
592 | // XXX should we worry about new component loaders being
|
---|
593 | // XXX defined by this process?
|
---|
594 | }
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | // Make sure the compreg file's mod time is current.
|
---|
600 | nsCOMPtr<nsIFile> compregFile;
|
---|
601 | rv = gDirectoryService->Get(NS_XPCOM_COMPONENT_REGISTRY_FILE,
|
---|
602 | NS_GET_IID(nsIFile),
|
---|
603 | getter_AddRefs(compregFile));
|
---|
604 | RTTIMESPEC Time;
|
---|
605 | RTTimeNow(&Time);
|
---|
606 | compregFile->SetLastModifiedTime(RTTimeSpecGetMilli(&Time));
|
---|
607 | }
|
---|
608 |
|
---|
609 | // Pay the cost at startup time of starting this singleton.
|
---|
610 | nsIInterfaceInfoManager* iim = XPTI_GetInterfaceInfoManager();
|
---|
611 | NS_IF_RELEASE(iim);
|
---|
612 | #ifdef VBOX
|
---|
613 | // Must initialize the EventQueueService singleton before anyone is
|
---|
614 | // using it. The notification below creates a thread which races creating
|
---|
615 | // the EventQueueService creation otherwise, no matter what.
|
---|
616 | nsCOMPtr<nsIEventQueue> eventQ;
|
---|
617 | rv = NS_GetMainEventQ(getter_AddRefs(eventQ));
|
---|
618 | if (NS_FAILED(rv)) {
|
---|
619 | NS_ERROR("Could not create event queue for main thread");
|
---|
620 | /* this is just a build-time hack, to reference NS_ProxyRelease */
|
---|
621 | if (rv == 666)
|
---|
622 | NS_ProxyRelease(nsnull, nsnull);
|
---|
623 | return rv;
|
---|
624 | }
|
---|
625 | #endif /* VBOX */
|
---|
626 |
|
---|
627 | // Notify observers of xpcom autoregistration start
|
---|
628 | NS_CreateServicesFromCategory(NS_XPCOM_STARTUP_OBSERVER_ID,
|
---|
629 | nsnull,
|
---|
630 | NS_XPCOM_STARTUP_OBSERVER_ID);
|
---|
631 |
|
---|
632 | #ifdef VBOX
|
---|
633 | gXPCOMInitialized = PR_TRUE;
|
---|
634 | #endif
|
---|
635 | return NS_OK;
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|
639 | static nsVoidArray* gExitRoutines;
|
---|
640 |
|
---|
641 | static void CallExitRoutines()
|
---|
642 | {
|
---|
643 | if (!gExitRoutines)
|
---|
644 | return;
|
---|
645 |
|
---|
646 | PRInt32 count = gExitRoutines->Count();
|
---|
647 | for (PRInt32 i = 0; i < count; i++) {
|
---|
648 | XPCOMExitRoutine func = (XPCOMExitRoutine) gExitRoutines->ElementAt(i);
|
---|
649 | func();
|
---|
650 | }
|
---|
651 | gExitRoutines->Clear();
|
---|
652 | delete gExitRoutines;
|
---|
653 | gExitRoutines = nsnull;
|
---|
654 | }
|
---|
655 |
|
---|
656 | nsresult NS_COM
|
---|
657 | NS_RegisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine, PRUint32 priority)
|
---|
658 | {
|
---|
659 | // priority are not used right now. It will need to be implemented as more
|
---|
660 | // classes are moved into the glue library --dougt
|
---|
661 | if (!gExitRoutines) {
|
---|
662 | gExitRoutines = new nsVoidArray();
|
---|
663 | if (!gExitRoutines) {
|
---|
664 | NS_WARNING("Failed to allocate gExitRoutines");
|
---|
665 | return NS_ERROR_FAILURE;
|
---|
666 | }
|
---|
667 | }
|
---|
668 |
|
---|
669 | PRBool okay = gExitRoutines->AppendElement((void*)exitRoutine);
|
---|
670 | return okay ? NS_OK : NS_ERROR_FAILURE;
|
---|
671 | }
|
---|
672 |
|
---|
673 | nsresult NS_COM
|
---|
674 | NS_UnregisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine)
|
---|
675 | {
|
---|
676 | if (!gExitRoutines)
|
---|
677 | return NS_ERROR_FAILURE;
|
---|
678 |
|
---|
679 | PRBool okay = gExitRoutines->RemoveElement((void*)exitRoutine);
|
---|
680 | return okay ? NS_OK : NS_ERROR_FAILURE;
|
---|
681 | }
|
---|
682 |
|
---|
683 |
|
---|
684 | //
|
---|
685 | // NS_ShutdownXPCOM()
|
---|
686 | //
|
---|
687 | // The shutdown sequence for xpcom would be
|
---|
688 | //
|
---|
689 | // - Release the Global Service Manager
|
---|
690 | // - Release all service instances held by the global service manager
|
---|
691 | // - Release the Global Service Manager itself
|
---|
692 | // - Release the Component Manager
|
---|
693 | // - Release all factories cached by the Component Manager
|
---|
694 | // - Unload Libraries
|
---|
695 | // - Release Contractid Cache held by Component Manager
|
---|
696 | // - Release dll abstraction held by Component Manager
|
---|
697 | // - Release the Registry held by Component Manager
|
---|
698 | // - Finally, release the component manager itself
|
---|
699 | //
|
---|
700 | nsresult NS_COM NS_ShutdownXPCOM(nsIServiceManager* servMgr)
|
---|
701 | {
|
---|
702 |
|
---|
703 | // Notify observers of xpcom shutting down
|
---|
704 | nsresult rv = NS_OK;
|
---|
705 | {
|
---|
706 | // Block it so that the COMPtr will get deleted before we hit
|
---|
707 | // servicemanager shutdown
|
---|
708 | nsCOMPtr<nsIObserverService> observerService =
|
---|
709 | do_GetService("@mozilla.org/observer-service;1", &rv);
|
---|
710 | if (NS_SUCCEEDED(rv))
|
---|
711 | {
|
---|
712 | nsCOMPtr<nsIServiceManager> mgr;
|
---|
713 | rv = NS_GetServiceManager(getter_AddRefs(mgr));
|
---|
714 | if (NS_SUCCEEDED(rv))
|
---|
715 | {
|
---|
716 | (void) observerService->NotifyObservers(mgr,
|
---|
717 | NS_XPCOM_SHUTDOWN_OBSERVER_ID,
|
---|
718 | nsnull);
|
---|
719 | }
|
---|
720 | }
|
---|
721 | }
|
---|
722 |
|
---|
723 | // grab the event queue so that we can process events one last time before exiting
|
---|
724 | nsCOMPtr <nsIEventQueue> currentQ;
|
---|
725 | {
|
---|
726 | nsCOMPtr<nsIEventQueueService> eventQService =
|
---|
727 | do_GetService(kEventQueueServiceCID, &rv);
|
---|
728 |
|
---|
729 | if (eventQService) {
|
---|
730 | eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(currentQ));
|
---|
731 | }
|
---|
732 | }
|
---|
733 | // XPCOM is officially in shutdown mode NOW
|
---|
734 | // Set this only after the observers have been notified as this
|
---|
735 | // will cause servicemanager to become inaccessible.
|
---|
736 | gXPCOMShuttingDown = PR_TRUE;
|
---|
737 |
|
---|
738 | #ifdef DEBUG_dougt
|
---|
739 | fprintf(stderr, "* * * * XPCOM shutdown. Access will be denied * * * * \n");
|
---|
740 | #endif
|
---|
741 |
|
---|
742 | #if 0 /// @todo later
|
---|
743 | IPC_Shutdown();
|
---|
744 | #endif
|
---|
745 |
|
---|
746 | // We may have AddRef'd for the caller of NS_InitXPCOM, so release it
|
---|
747 | // here again:
|
---|
748 | NS_IF_RELEASE(servMgr);
|
---|
749 |
|
---|
750 | // Shutdown global servicemanager
|
---|
751 | if (nsComponentManagerImpl::gComponentManager) {
|
---|
752 | nsComponentManagerImpl::gComponentManager->FreeServices();
|
---|
753 | }
|
---|
754 | nsServiceManager::ShutdownGlobalServiceManager(nsnull);
|
---|
755 |
|
---|
756 | if (currentQ) {
|
---|
757 | currentQ->ProcessPendingEvents();
|
---|
758 | currentQ = 0;
|
---|
759 | }
|
---|
760 |
|
---|
761 | nsProxyObjectManager::Shutdown();
|
---|
762 |
|
---|
763 | // Release the directory service
|
---|
764 | NS_IF_RELEASE(gDirectoryService);
|
---|
765 |
|
---|
766 | // Shutdown nsLocalFile string conversion
|
---|
767 | NS_ShutdownLocalFile();
|
---|
768 | #ifdef XP_UNIX
|
---|
769 | NS_ShutdownNativeCharsetUtils();
|
---|
770 | #endif
|
---|
771 |
|
---|
772 | CallExitRoutines();
|
---|
773 |
|
---|
774 | // Shutdown xpcom. This will release all loaders and cause others holding
|
---|
775 | // a refcount to the component manager to release it.
|
---|
776 | if (nsComponentManagerImpl::gComponentManager) {
|
---|
777 | rv = (nsComponentManagerImpl::gComponentManager)->Shutdown();
|
---|
778 | NS_ASSERTION(NS_SUCCEEDED(rv), "Component Manager shutdown failed.");
|
---|
779 | } else
|
---|
780 | NS_WARNING("Component Manager was never created ...");
|
---|
781 |
|
---|
782 | // Release our own singletons
|
---|
783 | // Do this _after_ shutting down the component manager, because the
|
---|
784 | // JS component loader will use XPConnect to call nsIModule::canUnload,
|
---|
785 | // and that will spin up the InterfaceInfoManager again -- bad mojo
|
---|
786 | XPTI_FreeInterfaceInfoManager();
|
---|
787 |
|
---|
788 | // Finally, release the component manager last because it unloads the
|
---|
789 | // libraries:
|
---|
790 | if (nsComponentManagerImpl::gComponentManager) {
|
---|
791 | nsrefcnt cnt;
|
---|
792 | NS_RELEASE2(nsComponentManagerImpl::gComponentManager, cnt);
|
---|
793 | NS_WARN_IF_FALSE(cnt == 0, "Component Manager being held past XPCOM shutdown.");
|
---|
794 | }
|
---|
795 | nsComponentManagerImpl::gComponentManager = nsnull;
|
---|
796 |
|
---|
797 | ShutdownSpecialSystemDirectory();
|
---|
798 |
|
---|
799 | EmptyEnumeratorImpl::Shutdown();
|
---|
800 |
|
---|
801 | NS_PurgeAtomTable();
|
---|
802 |
|
---|
803 | NS_IF_RELEASE(gDebug);
|
---|
804 |
|
---|
805 | #ifdef NS_BUILD_REFCNT_LOGGING
|
---|
806 | nsTraceRefcntImpl::DumpStatistics();
|
---|
807 | nsTraceRefcntImpl::ResetStatistics();
|
---|
808 | nsTraceRefcntImpl::Shutdown();
|
---|
809 | #endif
|
---|
810 |
|
---|
811 | #ifdef VBOX
|
---|
812 | gXPCOMInitialized = PR_FALSE;
|
---|
813 | #endif
|
---|
814 | return NS_OK;
|
---|
815 | }
|
---|
816 |
|
---|