VirtualBox

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

最後變更 在這個檔案從102320是 102320,由 vboxsync 提交於 16 月 前

libs/xpcom: Remove some unused code from xpcom, bugref:10545

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

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