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
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2000-06-06 22:28:01 +04:00
|
|
|
* The Original Code is the Mozilla browser.
|
2004-04-19 02:01:16 +04:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications, Inc.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2000-06-06 22:28:01 +04:00
|
|
|
*
|
|
|
|
* Contributor(s):
|
2004-04-19 02:01:16 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2000-06-06 22:28:01 +04:00
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
2002-01-30 00:22:13 +03:00
|
|
|
#include "nsIComponentRegistrar.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"
|
2002-06-12 00:33:04 +04:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2000-08-12 00:10:06 +04:00
|
|
|
|
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
|
2003-05-12 16:13:21 +04:00
|
|
|
rv = NS_InitXPCOM2(&sServiceManager, mozBinDirectory, appFileLocProvider);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-19 01:51:57 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
// Register components
|
|
|
|
if (!sRegistryInitializedFlag)
|
|
|
|
{
|
2002-01-30 00:22:13 +03:00
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(sServiceManager, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2002-09-07 00:19:14 +04:00
|
|
|
NS_WARNING("Could not QI to registrar");
|
2002-01-30 00:22:13 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2002-08-13 02:09:25 +04:00
|
|
|
#ifdef DEBUG
|
2002-01-30 00:22:13 +03:00
|
|
|
rv = registrar->AutoRegister(nsnull);
|
|
|
|
|
2000-07-08 06:16:34 +04:00
|
|
|
if (NS_FAILED(rv))
|
2000-06-06 22:28:01 +04:00
|
|
|
{
|
2002-09-07 00:19:14 +04:00
|
|
|
NS_WARNING("Could not AutoRegister");
|
2000-07-08 06:16:34 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-09-14 02:18:43 +04:00
|
|
|
// If the application is using an GRE, then,
|
|
|
|
// auto register components in the GRE directory as well.
|
2002-06-12 00:33:04 +04:00
|
|
|
//
|
2002-09-14 02:18:43 +04:00
|
|
|
// The application indicates that it's using an GRE by
|
2002-06-12 00:33:04 +04:00
|
|
|
// returning a valid nsIFile when queried (via appFileLocProvider)
|
2002-09-14 02:18:43 +04:00
|
|
|
// for the NS_GRE_DIR atom as shown below
|
2002-06-12 00:33:04 +04:00
|
|
|
//
|
|
|
|
if (appFileLocProvider)
|
|
|
|
{
|
2002-09-14 02:18:43 +04:00
|
|
|
nsCOMPtr<nsIFile> greDir;
|
2002-06-12 00:33:04 +04:00
|
|
|
PRBool persistent = PR_TRUE;
|
|
|
|
|
2002-09-14 02:18:43 +04:00
|
|
|
appFileLocProvider->GetFile(NS_GRE_DIR, &persistent, getter_AddRefs(greDir));
|
2002-06-12 00:33:04 +04:00
|
|
|
|
2002-09-14 02:18:43 +04:00
|
|
|
if (greDir)
|
2002-06-12 00:33:04 +04:00
|
|
|
{
|
2002-09-14 02:18:43 +04:00
|
|
|
rv = registrar->AutoRegister(greDir);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Could not AutoRegister GRE components");
|
2002-06-12 00:33:04 +04:00
|
|
|
}
|
|
|
|
}
|
2002-08-13 02:09:25 +04:00
|
|
|
#endif
|
2000-07-08 06:16:34 +04:00
|
|
|
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
|
2003-05-12 16:13:21 +04:00
|
|
|
nsresult rv = NS_ShutdownXPCOM(sServiceManager);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
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
|
|
|
}
|