1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* vim:set ts=4 sw=4 sts=4 ci et: */
3
/* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 1.1 (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
9
* http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
16
* The Original Code is mozilla.org code.
18
* The Initial Developer of the Original Code is
19
* Netscape Communications Corporation.
20
* Portions created by the Initial Developer are Copyright (C) 1998
21
* the Initial Developer. All Rights Reserved.
24
* Benjamin Smedberg <benjamin@smedbergs.us>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either of the GNU General Public License Version 2 or later (the "GPL"),
28
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the MPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the MPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
41
#include "nsXPCOMPrivate.h"
42
#include "nsXPCOMCIDInternal.h"
44
#include "nsIClassInfoImpl.h"
45
#include "nsStaticComponents.h"
48
#include "nsObserverList.h"
49
#include "nsObserverService.h"
50
#include "nsProperties.h"
51
#include "nsIProperties.h"
52
#include "nsPersistentProperties.h"
53
#include "nsScriptableInputStream.h"
54
#include "nsScriptableOutputStream.h"
55
#include "nsBinaryStream.h"
56
#include "nsStorageStream.h"
59
#include "nsMemoryImpl.h"
60
#include "nsDebugImpl.h"
61
#include "nsTraceRefcntImpl.h"
62
#include "nsErrorService.h"
63
#include "nsByteBuffer.h"
65
#include "nsSupportsArray.h"
67
#include "nsINIParserImpl.h"
68
#include "nsSupportsPrimitives.h"
69
#include "nsConsoleService.h"
70
#include "nsExceptionService.h"
72
#include "nsComponentManager.h"
73
#include "nsCategoryManagerUtils.h"
74
#include "nsIServiceManager.h"
75
#include "nsGenericFactory.h"
77
#include "nsThreadManager.h"
78
#include "nsThreadPool.h"
80
#include "nsIProxyObjectManager.h"
81
#include "nsProxyEventPrivate.h" // access to the impl of nsProxyObjectManager for the generic factory registration.
84
#include "nsIInterfaceInfoManager.h"
85
#include "xptiprivate.h"
87
#include "nsTimerImpl.h"
88
#include "TimerThread.h"
91
#include "nsProcess.h"
92
#include "nsEnvironment.h"
93
#include "nsVersionComparatorImpl.h"
95
#include "nsILocalFile.h"
96
#include "nsLocalFile.h"
97
#if defined(XP_UNIX) || defined(XP_OS2)
98
#include "nsNativeCharsetUtils.h"
100
#include "nsDirectoryService.h"
101
#include "nsDirectoryServiceDefs.h"
102
#include "nsCategoryManager.h"
103
#include "nsICategoryManager.h"
104
#include "nsMultiplexInputStream.h"
106
#include "nsStringStream.h"
107
extern NS_METHOD nsStringInputStreamConstructor(nsISupports *, REFNSIID, void **);
108
NS_DECL_CLASSINFO(nsStringInputStream)
110
#include "nsFastLoadService.h"
112
#include "nsAtomService.h"
113
#include "nsAtomTable.h"
114
#include "nsTraceRefcnt.h"
115
#include "nsTimelineService.h"
117
#include "nsHashPropertyBag.h"
119
#include "nsUnicharInputStream.h"
120
#include "nsVariant.h"
122
#include "nsUUIDGenerator.h"
124
#ifdef GC_LEAK_DETECTOR
125
#include "nsLeakDetector.h"
127
#include "nsRecyclingAllocator.h"
129
#include "SpecialSystemDirectory.h"
131
#if defined(XP_WIN) && !defined(WINCE)
132
#include "nsWindowsRegKey.h"
136
#include "nsMacUtilsImpl.h"
139
#include "nsSystemInfo.h"
143
// Registry Factory creation function defined in nsRegistry.cpp
144
// We hook into this function locally to create and register the registry
145
// Since noone outside xpcom needs to know about this and nsRegistry.cpp
146
// does not have a local include file, we are putting this definition
147
// here rather than in nsIRegistry.h
148
extern nsresult NS_RegistryGetFactory(nsIFactory** aFactory);
149
extern nsresult NS_CategoryManagerGetFactory( nsIFactory** );
152
extern void _FreeAutoLockStatics();
155
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
156
static NS_DEFINE_CID(kMemoryCID, NS_MEMORY_CID);
157
static NS_DEFINE_CID(kINIParserFactoryCID, NS_INIPARSERFACTORY_CID);
158
static NS_DEFINE_CID(kSimpleUnicharStreamFactoryCID, NS_SIMPLE_UNICHAR_STREAM_FACTORY_CID);
160
NS_GENERIC_FACTORY_CONSTRUCTOR(nsProcess)
162
#define NS_ENVIRONMENT_CLASSNAME "Environment Service"
165
// ds/nsISupportsPrimitives
166
#define NS_SUPPORTS_ID_CLASSNAME "Supports ID"
167
#define NS_SUPPORTS_CSTRING_CLASSNAME "Supports String"
168
#define NS_SUPPORTS_STRING_CLASSNAME "Supports WString"
169
#define NS_SUPPORTS_PRBOOL_CLASSNAME "Supports PRBool"
170
#define NS_SUPPORTS_PRUINT8_CLASSNAME "Supports PRUint8"
171
#define NS_SUPPORTS_PRUINT16_CLASSNAME "Supports PRUint16"
172
#define NS_SUPPORTS_PRUINT32_CLASSNAME "Supports PRUint32"
173
#define NS_SUPPORTS_PRUINT64_CLASSNAME "Supports PRUint64"
174
#define NS_SUPPORTS_PRTIME_CLASSNAME "Supports PRTime"
175
#define NS_SUPPORTS_CHAR_CLASSNAME "Supports Char"
176
#define NS_SUPPORTS_PRINT16_CLASSNAME "Supports PRInt16"
177
#define NS_SUPPORTS_PRINT32_CLASSNAME "Supports PRInt32"
178
#define NS_SUPPORTS_PRINT64_CLASSNAME "Supports PRInt64"
179
#define NS_SUPPORTS_FLOAT_CLASSNAME "Supports float"
180
#define NS_SUPPORTS_DOUBLE_CLASSNAME "Supports double"
181
#define NS_SUPPORTS_VOID_CLASSNAME "Supports void"
182
#define NS_SUPPORTS_INTERFACE_POINTER_CLASSNAME "Supports interface pointer"
184
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsIDImpl)
185
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsStringImpl)
186
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsCStringImpl)
187
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRBoolImpl)
188
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRUint8Impl)
189
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRUint16Impl)
190
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRUint32Impl)
191
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRUint64Impl)
192
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRTimeImpl)
193
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsCharImpl)
194
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRInt16Impl)
195
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRInt32Impl)
196
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsPRInt64Impl)
197
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsFloatImpl)
198
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsDoubleImpl)
199
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsVoidImpl)
200
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsInterfacePointerImpl)
202
NS_GENERIC_FACTORY_CONSTRUCTOR(nsArray)
203
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsConsoleService, Init)
204
NS_DECL_CLASSINFO(nsConsoleService)
205
NS_GENERIC_FACTORY_CONSTRUCTOR(nsAtomService)
206
NS_GENERIC_FACTORY_CONSTRUCTOR(nsExceptionService)
207
NS_GENERIC_FACTORY_CONSTRUCTOR(nsTimerImpl)
208
NS_GENERIC_FACTORY_CONSTRUCTOR(nsBinaryOutputStream)
209
NS_GENERIC_FACTORY_CONSTRUCTOR(nsBinaryInputStream)
210
NS_GENERIC_FACTORY_CONSTRUCTOR(nsStorageStream)
211
NS_GENERIC_FACTORY_CONSTRUCTOR(nsVersionComparatorImpl)
213
NS_GENERIC_FACTORY_CONSTRUCTOR(nsVariant)
215
NS_GENERIC_FACTORY_CONSTRUCTOR(nsRecyclingAllocatorImpl)
218
NS_GENERIC_FACTORY_CONSTRUCTOR(nsTimelineService)
221
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsHashPropertyBag, Init)
223
NS_GENERIC_AGGREGATED_CONSTRUCTOR_INIT(nsProperties, Init)
225
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsUUIDGenerator, Init)
228
NS_GENERIC_FACTORY_CONSTRUCTOR(nsMacUtilsImpl)
231
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsSystemInfo, Init)
234
nsThreadManagerGetSingleton(nsISupports* outer,
238
NS_ASSERTION(aInstancePtr, "null outptr");
239
NS_ENSURE_TRUE(!outer, NS_ERROR_NO_AGGREGATION);
241
return nsThreadManager::get()->QueryInterface(aIID, aInstancePtr);
243
NS_DECL_CLASSINFO(nsThreadManager)
245
NS_GENERIC_FACTORY_CONSTRUCTOR(nsThreadPool)
246
NS_DECL_CLASSINFO(nsThreadPool)
248
NS_DECL_CLASSINFO(nsScriptableInputStream)
249
NS_DECL_CLASSINFO(nsScriptableOutputStream)
252
nsXPTIInterfaceInfoManagerGetSingleton(nsISupports* outer,
256
NS_ASSERTION(aInstancePtr, "null outptr");
257
NS_ENSURE_TRUE(!outer, NS_ERROR_NO_AGGREGATION);
259
nsCOMPtr<nsIInterfaceInfoManager> iim
260
(xptiInterfaceInfoManager::GetInterfaceInfoManagerNoAddRef());
262
return NS_ERROR_FAILURE;
264
return iim->QueryInterface(aIID, aInstancePtr);
268
PR_STATIC_CALLBACK(nsresult)
269
RegisterGenericFactory(nsIComponentRegistrar* registrar,
270
const nsModuleComponentInfo *info)
273
nsIGenericFactory* fact;
274
rv = NS_NewGenericFactory(&fact, info);
275
if (NS_FAILED(rv)) return rv;
277
rv = registrar->RegisterFactory(info->mCID,
285
// In order to support the installer, we need
286
// to be told out of band if we should cause
287
// an autoregister. If the file ".autoreg" exists in the binary
288
// directory, we check its timestamp against the timestamp of the
289
// compreg.dat file. If the .autoreg file is newer, we autoregister.
290
static PRBool CheckUpdateFile()
293
nsCOMPtr<nsIFile> file;
294
rv = nsDirectoryService::gService->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
296
getter_AddRefs(file));
299
NS_WARNING("Getting NS_XPCOM_CURRENT_PROCESS_DIR failed");
303
file->AppendNative(nsDependentCString(".autoreg"));
306
file->Exists(&exists);
310
nsCOMPtr<nsIFile> compregFile;
311
rv = nsDirectoryService::gService->Get(NS_XPCOM_COMPONENT_REGISTRY_FILE,
313
getter_AddRefs(compregFile));
317
NS_WARNING("Getting NS_XPCOM_COMPONENT_REGISTRY_FILE failed");
321
if (NS_FAILED(compregFile->Exists(&exists)) || !exists)
324
PRInt64 compregModTime, autoregModTime;
325
compregFile->GetLastModifiedTime(&compregModTime);
326
file->GetLastModifiedTime(&autoregModTime);
328
return LL_CMP(autoregModTime, >, compregModTime);
332
nsComponentManagerImpl* nsComponentManagerImpl::gComponentManager = NULL;
333
PRBool gXPCOMShuttingDown = PR_FALSE;
335
// For each class that wishes to support nsIClassInfo, add a line like this
336
// NS_DECL_CLASSINFO(nsMyClass)
338
#define COMPONENT(NAME, Ctor) \
339
{ NS_##NAME##_CLASSNAME, NS_##NAME##_CID, NS_##NAME##_CONTRACTID, Ctor }
341
#define COMPONENT_CI(NAME, Ctor, Class) \
342
{ NS_##NAME##_CLASSNAME, NS_##NAME##_CID, NS_##NAME##_CONTRACTID, Ctor, \
343
NULL, NULL, NULL, NS_CI_INTERFACE_GETTER_NAME(Class), NULL, \
344
&NS_CLASSINFO_NAME(Class) }
346
#define COMPONENT_CI_FLAGS(NAME, Ctor, Class, Flags) \
347
{ NS_##NAME##_CLASSNAME, NS_##NAME##_CID, NS_##NAME##_CONTRACTID, Ctor, \
348
NULL, NULL, NULL, NS_CI_INTERFACE_GETTER_NAME(Class), NULL, \
349
&NS_CLASSINFO_NAME(Class), Flags }
351
static const nsModuleComponentInfo components[] = {
352
COMPONENT(MEMORY, nsMemoryImpl::Create),
353
COMPONENT(DEBUG, nsDebugImpl::Create),
354
#define NS_ERRORSERVICE_CLASSNAME NS_ERRORSERVICE_NAME
355
COMPONENT(ERRORSERVICE, nsErrorService::Create),
357
COMPONENT(BYTEBUFFER, ByteBufferImpl::Create),
358
COMPONENT_CI(SCRIPTABLEINPUTSTREAM, nsScriptableInputStream::Create,
359
nsScriptableInputStream),
360
COMPONENT_CI(SCRIPTABLEOUTPUTSTREAM, nsScriptableOutputStream::Create,
361
nsScriptableOutputStream),
362
COMPONENT(BINARYINPUTSTREAM, nsBinaryInputStreamConstructor),
363
COMPONENT(BINARYOUTPUTSTREAM, nsBinaryOutputStreamConstructor),
364
COMPONENT(STORAGESTREAM, nsStorageStreamConstructor),
365
COMPONENT(VERSIONCOMPARATOR, nsVersionComparatorImplConstructor),
366
COMPONENT(PIPE, nsPipeConstructor),
368
#define NS_PROPERTIES_CLASSNAME "Properties"
369
COMPONENT(PROPERTIES, nsPropertiesConstructor),
371
#define NS_PERSISTENTPROPERTIES_CID NS_IPERSISTENTPROPERTIES_CID /* sigh */
372
COMPONENT(PERSISTENTPROPERTIES, nsPersistentProperties::Create),
374
COMPONENT(SUPPORTSARRAY, nsSupportsArray::Create),
375
COMPONENT(ARRAY, nsArrayConstructor),
376
COMPONENT_CI_FLAGS(CONSOLESERVICE, nsConsoleServiceConstructor,
378
nsIClassInfo::THREADSAFE | nsIClassInfo::SINGLETON),
379
COMPONENT(EXCEPTIONSERVICE, nsExceptionServiceConstructor),
380
COMPONENT(ATOMSERVICE, nsAtomServiceConstructor),
382
COMPONENT(TIMELINESERVICE, nsTimelineServiceConstructor),
384
COMPONENT(OBSERVERSERVICE, nsObserverService::Create),
385
COMPONENT(GENERICFACTORY, nsGenericFactory::Create),
387
#define NS_XPCOMPROXY_CID NS_PROXYEVENT_MANAGER_CID
388
COMPONENT(XPCOMPROXY, nsProxyObjectManager::Create),
390
COMPONENT(TIMER, nsTimerImplConstructor),
392
#define COMPONENT_SUPPORTS(TYPE, Type) \
393
COMPONENT(SUPPORTS_##TYPE, nsSupports##Type##ImplConstructor)
395
COMPONENT_SUPPORTS(ID, ID),
396
COMPONENT_SUPPORTS(STRING, String),
397
COMPONENT_SUPPORTS(CSTRING, CString),
398
COMPONENT_SUPPORTS(PRBOOL, PRBool),
399
COMPONENT_SUPPORTS(PRUINT8, PRUint8),
400
COMPONENT_SUPPORTS(PRUINT16, PRUint16),
401
COMPONENT_SUPPORTS(PRUINT32, PRUint32),
402
COMPONENT_SUPPORTS(PRUINT64, PRUint64),
403
COMPONENT_SUPPORTS(PRTIME, PRTime),
404
COMPONENT_SUPPORTS(CHAR, Char),
405
COMPONENT_SUPPORTS(PRINT16, PRInt16),
406
COMPONENT_SUPPORTS(PRINT32, PRInt32),
407
COMPONENT_SUPPORTS(PRINT64, PRInt64),
408
COMPONENT_SUPPORTS(FLOAT, Float),
409
COMPONENT_SUPPORTS(DOUBLE, Double),
410
COMPONENT_SUPPORTS(VOID, Void),
411
COMPONENT_SUPPORTS(INTERFACE_POINTER, InterfacePointer),
413
#undef COMPONENT_SUPPORTS
414
#define NS_LOCAL_FILE_CLASSNAME "Local File Specification"
415
COMPONENT(LOCAL_FILE, nsLocalFile::nsLocalFileConstructor),
416
#define NS_DIRECTORY_SERVICE_CLASSNAME "nsIFile Directory Service"
417
COMPONENT(DIRECTORY_SERVICE, nsDirectoryService::Create),
418
COMPONENT(PROCESS, nsProcessConstructor),
419
COMPONENT(ENVIRONMENT, nsEnvironment::Create),
421
COMPONENT_CI_FLAGS(THREADMANAGER, nsThreadManagerGetSingleton,
423
nsIClassInfo::THREADSAFE | nsIClassInfo::SINGLETON),
424
COMPONENT_CI_FLAGS(THREADPOOL, nsThreadPoolConstructor,
425
nsThreadPool, nsIClassInfo::THREADSAFE),
427
COMPONENT_CI_FLAGS(STRINGINPUTSTREAM, nsStringInputStreamConstructor,
428
nsStringInputStream, nsIClassInfo::THREADSAFE),
429
COMPONENT(MULTIPLEXINPUTSTREAM, nsMultiplexInputStreamConstructor),
431
#ifndef MOZ_NO_FAST_LOAD
432
COMPONENT(FASTLOADSERVICE, nsFastLoadService::Create),
435
COMPONENT(VARIANT, nsVariantConstructor),
436
COMPONENT(INTERFACEINFOMANAGER_SERVICE, nsXPTIInterfaceInfoManagerGetSingleton),
438
COMPONENT(RECYCLINGALLOCATOR, nsRecyclingAllocatorImplConstructor),
440
#define NS_HASH_PROPERTY_BAG_CLASSNAME "Hashtable Property Bag"
441
COMPONENT(HASH_PROPERTY_BAG, nsHashPropertyBagConstructor),
443
COMPONENT(UUID_GENERATOR, nsUUIDGeneratorConstructor),
445
#if defined(XP_WIN) && !defined(WINCE)
446
COMPONENT(WINDOWSREGKEY, nsWindowsRegKeyConstructor),
450
COMPONENT(MACUTILSIMPL, nsMacUtilsImplConstructor),
453
COMPONENT(SYSTEMINFO, nsSystemInfoConstructor),
458
const int components_length = sizeof(components) / sizeof(components[0]);
460
// gDebug will be freed during shutdown.
461
static nsIDebug* gDebug = nsnull;
463
EXPORT_XPCOM_API(nsresult)
464
NS_GetDebug(nsIDebug** result)
466
return nsDebugImpl::Create(nsnull,
467
NS_GET_IID(nsIDebug),
471
EXPORT_XPCOM_API(nsresult)
472
NS_GetTraceRefcnt(nsITraceRefcnt** result)
474
return nsTraceRefcntImpl::Create(nsnull,
475
NS_GET_IID(nsITraceRefcnt),
479
EXPORT_XPCOM_API(nsresult)
480
NS_InitXPCOM(nsIServiceManager* *result,
481
nsIFile* binDirectory)
483
return NS_InitXPCOM3(result, binDirectory, nsnull, nsnull, 0);
486
EXPORT_XPCOM_API(nsresult)
487
NS_InitXPCOM2(nsIServiceManager* *result,
488
nsIFile* binDirectory,
489
nsIDirectoryServiceProvider* appFileLocationProvider)
491
return NS_InitXPCOM3(result, binDirectory, appFileLocationProvider, nsnull, 0);
494
EXPORT_XPCOM_API(nsresult)
495
NS_InitXPCOM3(nsIServiceManager* *result,
496
nsIFile* binDirectory,
497
nsIDirectoryServiceProvider* appFileLocationProvider,
498
nsStaticModuleInfo const *staticComponents,
499
PRUint32 componentCount)
503
#ifdef MOZ_ENABLE_LIBXUL
504
if (!staticComponents) {
505
staticComponents = kPStaticModules;
506
componentCount = kStaticModuleCount;
510
// We are not shutting down
511
gXPCOMShuttingDown = PR_FALSE;
515
// Establish the main thread here.
516
rv = nsThreadManager::get()->Init();
517
if (NS_FAILED(rv)) return rv;
519
// Set up the timer globals/timer thread
520
rv = nsTimerImpl::Startup();
521
NS_ENSURE_SUCCESS(rv, rv);
524
// If the locale hasn't already been setup by our embedder,
525
// get us out of the "C" locale and into the system
526
if (strcmp(setlocale(LC_ALL, NULL), "C") == 0)
527
setlocale(LC_ALL, "");
530
#if defined(XP_UNIX) || defined(XP_OS2)
531
NS_StartupNativeCharsetUtils();
533
NS_StartupLocalFile();
535
StartupSpecialSystemDirectory();
537
rv = nsDirectoryService::RealInit();
541
nsCOMPtr<nsIFile> xpcomLib;
546
rv = binDirectory->IsDirectory(&value);
548
if (NS_SUCCEEDED(rv) && value) {
549
nsDirectoryService::gService->Set(NS_XPCOM_INIT_CURRENT_PROCESS_DIR, binDirectory);
550
binDirectory->Clone(getter_AddRefs(xpcomLib));
554
nsDirectoryService::gService->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
556
getter_AddRefs(xpcomLib));
560
xpcomLib->AppendNative(nsDependentCString(XPCOM_DLL));
561
nsDirectoryService::gService->Set(NS_XPCOM_LIBRARY_FILE, xpcomLib);
564
if (appFileLocationProvider) {
565
rv = nsDirectoryService::gService->RegisterProvider(appFileLocationProvider);
566
if (NS_FAILED(rv)) return rv;
569
NS_ASSERTION(nsComponentManagerImpl::gComponentManager == NULL, "CompMgr not null at init");
571
// Create the Component/Service Manager
572
nsComponentManagerImpl *compMgr = new nsComponentManagerImpl();
574
return NS_ERROR_OUT_OF_MEMORY;
577
rv = compMgr->Init(staticComponents, componentCount);
584
nsComponentManagerImpl::gComponentManager = compMgr;
587
nsIServiceManager *serviceManager =
588
static_cast<nsIServiceManager*>(compMgr);
590
NS_ADDREF(*result = serviceManager);
593
nsCOMPtr<nsIMemory> memory;
594
NS_GetMemoryManager(getter_AddRefs(memory));
595
rv = compMgr->RegisterService(kMemoryCID, memory);
596
if (NS_FAILED(rv)) return rv;
598
rv = compMgr->RegisterService(kComponentManagerCID, static_cast<nsIComponentManager*>(compMgr));
599
if (NS_FAILED(rv)) return rv;
601
#ifdef GC_LEAK_DETECTOR
602
rv = NS_InitLeakDetector();
603
if (NS_FAILED(rv)) return rv;
606
rv = nsCycleCollector_startup();
607
if (NS_FAILED(rv)) return rv;
609
// 2. Register the global services with the component manager so that
610
// clients can create new objects.
614
nsCOMPtr<nsIFactory> categoryManagerFactory;
615
if ( NS_FAILED(rv = NS_CategoryManagerGetFactory(getter_AddRefs(categoryManagerFactory))) )
618
NS_DEFINE_CID(kCategoryManagerCID, NS_CATEGORYMANAGER_CID);
620
rv = compMgr->RegisterFactory(kCategoryManagerCID,
621
NS_CATEGORYMANAGER_CLASSNAME,
622
NS_CATEGORYMANAGER_CONTRACTID,
623
categoryManagerFactory,
625
if ( NS_FAILED(rv) ) return rv;
628
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(
629
static_cast<nsIComponentManager*>(compMgr), &rv);
631
for (int i = 0; i < components_length; i++)
632
RegisterGenericFactory(registrar, &components[i]);
634
nsCOMPtr<nsIFactory> iniParserFactory(new nsINIParserFactory());
635
if (iniParserFactory)
636
registrar->RegisterFactory(kINIParserFactoryCID,
637
"nsINIParserFactory",
638
NS_INIPARSERFACTORY_CONTRACTID,
642
RegisterFactory(kSimpleUnicharStreamFactoryCID,
643
"nsSimpleUnicharStreamFactory",
644
NS_SIMPLE_UNICHAR_STREAM_FACTORY_CONTRACTID,
645
nsSimpleUnicharStreamFactory::GetInstance());
648
// Pay the cost at startup time of starting this singleton.
649
nsIInterfaceInfoManager* iim =
650
xptiInterfaceInfoManager::GetInterfaceInfoManagerNoAddRef();
652
if (CheckUpdateFile() || NS_FAILED(
653
nsComponentManagerImpl::gComponentManager->ReadPersistentRegistry())) {
654
// If the component registry is out of date, malformed, or incomplete,
655
// autoregister the default component directories.
656
(void) iim->AutoRegisterInterfaces();
657
nsComponentManagerImpl::gComponentManager->AutoRegister(nsnull);
660
// After autoreg, but before we actually instantiate any components,
661
// add any services listed in the "xpcom-directory-providers" category
662
// to the directory service.
663
nsDirectoryService::gService->RegisterCategoryProviders();
665
// Initialize memory flusher
666
nsMemoryImpl::InitFlusher();
668
// Notify observers of xpcom autoregistration start
669
NS_CreateServicesFromCategory(NS_XPCOM_STARTUP_OBSERVER_ID,
671
NS_XPCOM_STARTUP_OBSERVER_ID);
678
// NS_ShutdownXPCOM()
680
// The shutdown sequence for xpcom would be
682
// - Notify "xpcom-shutdown" for modules to release primary (root) references
683
// - Shutdown XPCOM timers
684
// - Notify "xpcom-shutdown-threads" for thread joins
685
// - Shutdown the event queues
686
// - Release the Global Service Manager
687
// - Release all service instances held by the global service manager
688
// - Release the Global Service Manager itself
689
// - Release the Component Manager
690
// - Release all factories cached by the Component Manager
691
// - Notify module loaders to shut down
692
// - Unload Libraries
693
// - Release Contractid Cache held by Component Manager
694
// - Release dll abstraction held by Component Manager
695
// - Release the Registry held by Component Manager
696
// - Finally, release the component manager itself
698
EXPORT_XPCOM_API(nsresult)
699
NS_ShutdownXPCOM(nsIServiceManager* servMgr)
701
NS_ENSURE_STATE(NS_IsMainThread());
704
nsCOMPtr<nsISimpleEnumerator> moduleLoaders;
706
// Notify observers of xpcom shutting down
708
// Block it so that the COMPtr will get deleted before we hit
709
// servicemanager shutdown
711
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
712
NS_ENSURE_STATE(thread);
714
nsRefPtr<nsObserverService> observerService;
715
CallGetService("@mozilla.org/observer-service;1",
716
(nsObserverService**) getter_AddRefs(observerService));
720
nsCOMPtr<nsIServiceManager> mgr;
721
rv = NS_GetServiceManager(getter_AddRefs(mgr));
722
if (NS_SUCCEEDED(rv))
724
(void) observerService->
725
NotifyObservers(mgr, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
730
NS_ProcessPendingEvents(thread);
733
(void) observerService->
734
NotifyObservers(nsnull, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID,
737
NS_ProcessPendingEvents(thread);
739
// Shutdown the timer thread and all timers that might still be alive before
740
// shutting down the component manager
741
nsTimerImpl::Shutdown();
743
NS_ProcessPendingEvents(thread);
745
// Shutdown all remaining threads. This method does not return until
746
// all threads created using the thread manager (with the exception of
747
// the main thread) have exited.
748
nsThreadManager::get()->Shutdown();
750
NS_ProcessPendingEvents(thread);
752
// We save the "xpcom-shutdown-loaders" observers to notify after
753
// the observerservice is gone.
754
if (observerService) {
756
EnumerateObservers(NS_XPCOM_SHUTDOWN_LOADERS_OBSERVER_ID,
757
getter_AddRefs(moduleLoaders));
759
observerService->Shutdown();
763
// XPCOM is officially in shutdown mode NOW
764
// Set this only after the observers have been notified as this
765
// will cause servicemanager to become inaccessible.
766
gXPCOMShuttingDown = PR_TRUE;
769
fprintf(stderr, "* * * * XPCOM shutdown. Access will be denied * * * * \n");
771
// We may have AddRef'd for the caller of NS_InitXPCOM, so release it
773
NS_IF_RELEASE(servMgr);
775
// Shutdown global servicemanager
776
if (nsComponentManagerImpl::gComponentManager) {
777
nsComponentManagerImpl::gComponentManager->FreeServices();
780
nsProxyObjectManager::Shutdown();
782
// Release the directory service
783
NS_IF_RELEASE(nsDirectoryService::gService);
785
nsCycleCollector_shutdown();
789
nsCOMPtr<nsISupports> el;
790
while (NS_SUCCEEDED(moduleLoaders->HasMoreElements(&more)) &&
792
moduleLoaders->GetNext(getter_AddRefs(el));
794
// Don't worry about weak-reference observers here: there is
795
// no reason for weak-ref observers to register for
796
// xpcom-shutdown-loaders
798
nsCOMPtr<nsIObserver> obs(do_QueryInterface(el));
800
(void) obs->Observe(nsnull,
801
NS_XPCOM_SHUTDOWN_LOADERS_OBSERVER_ID,
805
moduleLoaders = nsnull;
808
// Shutdown nsLocalFile string conversion
809
NS_ShutdownLocalFile();
811
NS_ShutdownNativeCharsetUtils();
814
// Shutdown xpcom. This will release all loaders and cause others holding
815
// a refcount to the component manager to release it.
816
if (nsComponentManagerImpl::gComponentManager) {
817
rv = (nsComponentManagerImpl::gComponentManager)->Shutdown();
818
NS_ASSERTION(NS_SUCCEEDED(rv), "Component Manager shutdown failed.");
820
NS_WARNING("Component Manager was never created ...");
822
// Release our own singletons
823
// Do this _after_ shutting down the component manager, because the
824
// JS component loader will use XPConnect to call nsIModule::canUnload,
825
// and that will spin up the InterfaceInfoManager again -- bad mojo
826
xptiInterfaceInfoManager::FreeInterfaceInfoManager();
828
// Finally, release the component manager last because it unloads the
830
if (nsComponentManagerImpl::gComponentManager) {
832
NS_RELEASE2(nsComponentManagerImpl::gComponentManager, cnt);
833
NS_ASSERTION(cnt == 0, "Component Manager being held past XPCOM shutdown.");
835
nsComponentManagerImpl::gComponentManager = nsnull;
838
_FreeAutoLockStatics();
841
ShutdownSpecialSystemDirectory();
845
NS_IF_RELEASE(gDebug);
849
#ifdef GC_LEAK_DETECTOR
850
// Shutdown the Leak detector.
851
NS_ShutdownLeakDetector();