VirtualBox

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

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

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