2000-07-08 06:16:34 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2000-06-06 22:28:01 +04:00
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla browser.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications, Inc. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1999, Mozilla. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Adam Lock <adamlock@netscape.com>
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
2002-01-30 00:22:13 +03:00
|
|
|
#include "nsIComponentRegistrar.h"
|
2000-06-06 22:28:01 +04:00
|
|
|
#include "nsIEventQueueService.h"
|
2001-03-20 00:57:57 +03:00
|
|
|
#include "nsIAppStartupNotifier.h"
|
2000-06-12 19:39:43 +04:00
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
|
2000-08-12 00:10:06 +04:00
|
|
|
#include "nsIDirectoryService.h"
|
|
|
|
|
2000-06-21 17:05:49 +04:00
|
|
|
#include "nsEmbedAPI.h"
|
2001-09-27 00:17:35 +04:00
|
|
|
#include "nsLiteralString.h"
|
2000-06-12 19:39:43 +04:00
|
|
|
|
2000-06-06 22:28:01 +04:00
|
|
|
static nsIServiceManager *sServiceManager = nsnull;
|
2000-06-12 19:39:43 +04:00
|
|
|
static PRBool sRegistryInitializedFlag = PR_FALSE;
|
|
|
|
static PRUint32 sInitCounter = 0;
|
|
|
|
|
2000-06-28 00:54:15 +04:00
|
|
|
#define HACK_AROUND_THREADING_ISSUES
|
2000-06-21 16:48:11 +04:00
|
|
|
//#define HACK_AROUND_NONREENTRANT_INITXPCOM
|
2000-06-12 19:39:43 +04:00
|
|
|
|
2000-06-06 22:28:01 +04:00
|
|
|
#ifdef HACK_AROUND_NONREENTRANT_INITXPCOM
|
2000-06-12 19:39:43 +04:00
|
|
|
// XXX hack class to clean up XPCOM when this module is unloaded
|
|
|
|
class XPCOMCleanupHack
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PRBool mCleanOnExit;
|
|
|
|
|
|
|
|
XPCOMCleanupHack() : mCleanOnExit(PR_FALSE) {}
|
|
|
|
~XPCOMCleanupHack()
|
|
|
|
{
|
|
|
|
if (mCleanOnExit)
|
|
|
|
{
|
|
|
|
if (sInitCounter > 0)
|
|
|
|
{
|
|
|
|
sInitCounter = 1;
|
|
|
|
NS_TermEmbedding();
|
|
|
|
}
|
|
|
|
// XXX Global destructors and NS_ShutdownXPCOM don't seem to mix
|
2000-07-08 06:16:34 +04:00
|
|
|
// NS_ShutdownXPCOM(sServiceManager);
|
2000-06-12 19:39:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2000-06-06 22:28:01 +04:00
|
|
|
static PRBool sXPCOMInitializedFlag = PR_FALSE;
|
2000-06-12 19:39:43 +04:00
|
|
|
static XPCOMCleanupHack sXPCOMCleanupHack;
|
2000-06-06 22:28:01 +04:00
|
|
|
#endif
|
|
|
|
|
2000-06-21 17:19:12 +04:00
|
|
|
|
2000-08-12 00:10:06 +04:00
|
|
|
nsresult NS_InitEmbedding(nsILocalFile *mozBinDirectory,
|
|
|
|
nsIDirectoryServiceProvider *appFileLocProvider)
|
2000-06-21 17:19:12 +04:00
|
|
|
{
|
2000-08-12 00:10:06 +04:00
|
|
|
nsresult rv;
|
2000-08-10 10:46:00 +04:00
|
|
|
|
2000-06-12 19:39:43 +04:00
|
|
|
// Reentrant calls to this method do nothing except increment a counter
|
2000-07-08 06:16:34 +04:00
|
|
|
sInitCounter++;
|
|
|
|
if (sInitCounter > 1)
|
2000-06-12 19:39:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
2000-07-08 06:16:34 +04:00
|
|
|
// Initialise XPCOM
|
2000-06-06 22:28:01 +04:00
|
|
|
#ifdef HACK_AROUND_NONREENTRANT_INITXPCOM
|
2000-07-08 06:16:34 +04:00
|
|
|
// Can't call NS_InitXPCom more than once or things go boom!
|
|
|
|
if (!sXPCOMInitializedFlag)
|
2000-06-06 22:28:01 +04:00
|
|
|
#endif
|
2000-07-08 06:16:34 +04:00
|
|
|
{
|
|
|
|
// Initialise XPCOM
|
2001-01-19 01:51:57 +03:00
|
|
|
NS_InitXPCOM2(&sServiceManager, mozBinDirectory, appFileLocProvider);
|
|
|
|
|
2000-06-06 22:28:01 +04:00
|
|
|
#ifdef HACK_AROUND_NONREENTRANT_INITXPCOM
|
2000-07-08 06:16:34 +04:00
|
|
|
sXPCOMInitializedFlag = PR_TRUE;
|
2000-06-12 19:39:43 +04:00
|
|
|
sXPCOMCleanupHack.mCleanOnExit = PR_TRUE;
|
2000-06-06 22:28:01 +04:00
|
|
|
#endif
|
2000-07-08 06:16:34 +04:00
|
|
|
}
|
2000-06-06 22:28:01 +04:00
|
|
|
|
2001-08-03 00:31:54 +04:00
|
|
|
// Create the Event Queue for the UI thread...
|
|
|
|
//
|
|
|
|
// If an event queue already exists for the thread, then
|
|
|
|
// CreateThreadEventQueue(...) will fail...
|
|
|
|
// CreateThread0ueue(...) will fail...
|
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService(
|
|
|
|
do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
eventQService->CreateThreadEventQueue();
|
|
|
|
|
2000-07-08 06:16:34 +04:00
|
|
|
// Register components
|
|
|
|
if (!sRegistryInitializedFlag)
|
|
|
|
{
|
2002-01-30 00:22:13 +03:00
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(sServiceManager, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
NS_ASSERTION(PR_FALSE, "Could not QI to registrar");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = registrar->AutoRegister(nsnull);
|
|
|
|
|
2000-07-08 06:16:34 +04:00
|
|
|
if (NS_FAILED(rv))
|
2000-06-06 22:28:01 +04:00
|
|
|
{
|
2000-07-08 06:16:34 +04:00
|
|
|
NS_ASSERTION(PR_FALSE, "Could not AutoRegister");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
sRegistryInitializedFlag = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2001-03-20 00:57:57 +03:00
|
|
|
nsCOMPtr<nsIObserver> mStartupNotifier = do_CreateInstance(NS_APPSTARTUPNOTIFIER_CONTRACTID, &rv);
|
|
|
|
if(NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
mStartupNotifier->Observe(nsnull, APPSTARTUP_TOPIC, nsnull);
|
|
|
|
|
2000-06-12 19:39:43 +04:00
|
|
|
#ifdef HACK_AROUND_THREADING_ISSUES
|
|
|
|
// XXX force certain objects to be created on the main thread
|
2000-08-01 05:37:44 +04:00
|
|
|
nsCOMPtr<nsIStringBundleService> sBundleService;
|
2001-09-07 22:05:50 +04:00
|
|
|
sBundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
2000-07-08 06:16:34 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2000-06-12 19:39:43 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStringBundle> stringBundle;
|
2001-11-07 09:24:10 +03:00
|
|
|
const char propertyURL[] = "chrome://necko/locale/necko.properties";
|
2001-04-28 01:30:24 +04:00
|
|
|
rv = sBundleService->CreateBundle(propertyURL,
|
2000-07-08 06:16:34 +04:00
|
|
|
getter_AddRefs(stringBundle));
|
2000-06-12 19:39:43 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-07-08 06:16:34 +04:00
|
|
|
return NS_OK;
|
2000-07-05 01:53:27 +04:00
|
|
|
|
2000-06-06 22:28:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NS_TermEmbedding()
|
|
|
|
{
|
2000-06-12 19:39:43 +04:00
|
|
|
// Reentrant calls to this method do nothing except decrement a counter
|
|
|
|
if (sInitCounter > 1)
|
|
|
|
{
|
|
|
|
sInitCounter--;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
sInitCounter = 0;
|
|
|
|
|
2001-09-07 22:05:50 +04:00
|
|
|
NS_IF_RELEASE(sServiceManager);
|
2000-06-21 16:48:11 +04:00
|
|
|
|
2000-07-08 06:16:34 +04:00
|
|
|
// Terminate XPCOM & cleanup
|
2000-06-06 22:28:01 +04:00
|
|
|
#ifndef HACK_AROUND_NONREENTRANT_INITXPCOM
|
2000-07-08 06:16:34 +04:00
|
|
|
NS_ShutdownXPCOM(sServiceManager);
|
2000-06-06 22:28:01 +04:00
|
|
|
#endif
|
|
|
|
|
2000-07-08 06:16:34 +04:00
|
|
|
return NS_OK;
|
2000-06-06 22:28:01 +04:00
|
|
|
}
|