VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/build/nsXPComInit.cpp@ 102103

Last change on this file since 102103 was 102103, checked in by vboxsync, 15 months ago

libs/xpcom: Make sure IPRT is initialized when NS_InitXPCOM2() is called, fixes some testcase failures, bugref:10545

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette