nsTraceRefcnt::Startup();
nsTraceRefcntImpl::Startup();
nsresult NS_COM NS_InitXPCOM2(nsIServiceManager* *result,                              nsIFile* binDirectory,                              nsIDirectoryServiceProvider* appFileLocationProvider){    if (!gXPCOMHasGlobalsBeenInitalized)        return NS_ERROR_NOT_INITIALIZED;    nsresult rv = NS_OK;     // We are not shutting down    gXPCOMShuttingDown = PR_FALSE;#ifdef NS_BUILD_REFCNT_LOGGING    nsTraceRefcnt::Startup();#endif    // Establish the main thread here.    rv = nsIThread::SetMainThread();    if (NS_FAILED(rv)) return rv;    // Startup the memory manager    rv = nsMemoryImpl::Startup();    if (NS_FAILED(rv)) return rv;#if defined(XP_UNIX) || defined(XP_OS2)    NS_StartupNativeCharsetUtils();#endif    NS_StartupLocalFile();    StartupSpecialSystemDirectory();    // Start the directory service so that the component manager init can use it.    rv = nsDirectoryService::Create(nsnull,                                    NS_GET_IID(nsIProperties),                                    (void**)&gDirectoryService);    if (NS_FAILED(rv))        return rv;    nsCOMPtr<nsIDirectoryService> dirService = do_QueryInterface(gDirectoryService, &rv);    if (NS_FAILED(rv))        return rv;    rv = dirService->Init();    if (NS_FAILED(rv))        return rv;    // Create the Component/Service Manager    nsComponentManagerImpl *compMgr = NULL;    if (nsComponentManagerImpl::gComponentManager == NULL)    {        compMgr = new nsComponentManagerImpl();        if (compMgr == NULL)            return NS_ERROR_OUT_OF_MEMORY;        NS_ADDREF(compMgr);                nsCOMPtr<nsIFile> xpcomLib;                        PRBool value;        if (binDirectory)        {            rv = binDirectory->IsDirectory(&value);            if (NS_SUCCEEDED(rv) && value) {                gDirectoryService->Set(NS_XPCOM_INIT_CURRENT_PROCESS_DIR, binDirectory);                binDirectory->Clone(getter_AddRefs(xpcomLib));            }        }        else {            gDirectoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR,                                    NS_GET_IID(nsIFile),                                    getter_AddRefs(xpcomLib));        }        if (xpcomLib) {            xpcomLib->AppendNative(nsDependentCString(XPCOM_DLL));            gDirectoryService->Set(NS_XPCOM_LIBRARY_FILE, xpcomLib);        }                if (appFileLocationProvider) {            rv = dirService->RegisterProvider(appFileLocationProvider);            if (NS_FAILED(rv)) return rv;        }        rv = compMgr->Init();        if (NS_FAILED(rv))        {            NS_RELEASE(compMgr);            return rv;        }        nsComponentManagerImpl::gComponentManager = compMgr;        if (result) {            nsIServiceManager *serviceManager =                NS_STATIC_CAST(nsIServiceManager*, compMgr);            NS_ADDREF(*result = serviceManager);        }    }    nsCOMPtr<nsIMemory> memory;    NS_GetMemoryManager(getter_AddRefs(memory));    // dougt - these calls will be moved into a new interface when nsIComponentManager is frozen.    rv = compMgr->RegisterService(kMemoryCID, memory);    if (NS_FAILED(rv)) return rv;    rv = compMgr->RegisterService(kComponentManagerCID, NS_STATIC_CAST(nsIComponentManager*, compMgr));    if (NS_FAILED(rv)) return rv;#ifdef GC_LEAK_DETECTOR  rv = NS_InitLeakDetector();    if (NS_FAILED(rv)) return rv;#endif    // 2. Register the global services with the component manager so that    //    clients can create new objects.    // Category Manager    {      nsCOMPtr<nsIFactory> categoryManagerFactory;      if ( NS_FAILED(rv = NS_CategoryManagerGetFactory(getter_AddRefs(categoryManagerFactory))) )        return rv;      NS_DEFINE_CID(kCategoryManagerCID, NS_CATEGORYMANAGER_CID);      rv = compMgr->RegisterFactory(kCategoryManagerCID,                                    NS_CATEGORYMANAGER_CLASSNAME,                                    NS_CATEGORYMANAGER_CONTRACTID,                                    categoryManagerFactory,                                    PR_TRUE);      if ( NS_FAILED(rv) ) return rv;    }    // what I want to do here is QI for a Component Registration Manager.  Since this    // has not been invented yet, QI to the obsolete manager.  Kids, don't do this at home.    nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(        NS_STATIC_CAST(nsIComponentManager*,compMgr), &rv);    if (registrar) {        for (int i = 0; i < components_length; i++)            RegisterGenericFactory(registrar, &components[i]);    }    rv = nsComponentManagerImpl::gComponentManager->ReadPersistentRegistry();#ifdef DEBUG        if (NS_FAILED(rv)) {        printf("No Persistent Registry Found.\n");            }#endif    if ( NS_FAILED(rv) || CheckAndRemoveUpdateFile()) {        // if we find no persistent registry, we will try to autoregister        // the default components directory.        nsComponentManagerImpl::gComponentManager->AutoRegister(nsnull);                // If the application is using a GRE, then,         // auto register components in the GRE directory as well.        //        // The application indicates that it's using an GRE by        // returning a valid nsIFile when queried (via appFileLocProvider)        // for the NS_GRE_DIR atom as shown below        //        if ( appFileLocationProvider ) {            nsCOMPtr<nsIFile> greDir;            PRBool persistent = PR_TRUE;            appFileLocationProvider->GetFile(NS_GRE_COMPONENT_DIR, &persistent, getter_AddRefs(greDir));            if (greDir)            {#ifdef DEBUG_dougt	printf("start - Registering GRE components\n");#endif                // If the GRE contains any loaders, we want to know about it so that we can cause another                // autoregistration of the applications component directory.                int loaderCount = nsComponentManagerImpl::gComponentManager->GetLoaderCount();                rv = nsComponentManagerImpl::gComponentManager->AutoRegister(greDir);                                if (loaderCount != nsComponentManagerImpl::gComponentManager->GetLoaderCount())                     nsComponentManagerImpl::gComponentManager->AutoRegisterNonNativeComponents(nsnull);        #ifdef DEBUG_dougt	printf("end - Registering GRE components\n");#endif          				if (NS_FAILED(rv))                {                    NS_ERROR("Could not AutoRegister GRE components");                    return rv;                }            }        }    }        // Pay the cost at startup time of starting this singleton.    nsIInterfaceInfoManager* iim = XPTI_GetInterfaceInfoManager();    NS_IF_RELEASE(iim);    nsCOMPtr<nsIEventQueueService> eventQService(do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv));    if ( NS_FAILED(rv) ) return rv;    rv = eventQService->CreateThreadEventQueue();    if ( NS_FAILED(rv) ) return rv;    // Notify observers of xpcom autoregistration start    NS_CreateServicesFromCategory(NS_XPCOM_STARTUP_OBSERVER_ID,                                   nsnull,                                  NS_XPCOM_STARTUP_OBSERVER_ID);        return rv;}