VirtualBox

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

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

libs/xpcom: Get rid of nsMemoryImpl.{cpp,h} and nsIMemory and repalce it with direct calls to RTMem* APIs, bugref:10545

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