initterm.cpp revision 467e5c726e41b34b1f2c808581311ffb82520db4
/** @file
* MS COM / XPCOM Abstraction Layer - Initialization and Termination.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#if !defined (VBOX_WITH_XPCOM)
#include <objbase.h>
#else /* !defined (VBOX_WITH_XPCOM) */
#include <stdlib.h>
/* XPCOM_GLUE is defined when the client uses the standalone glue
* (i.e. dynamically picks up the existing XPCOM shared library installation).
* This is not the case for VirtualBox XPCOM clients (they are always
* distrubuted with the self-built XPCOM library, and therefore have a binary
* dependency on it) but left here for clarity.
*/
#if defined (XPCOM_GLUE)
#include <nsXPCOMGlue.h>
#endif
#include <nsIComponentRegistrar.h>
#include <nsIServiceManager.h>
#include <nsCOMPtr.h>
#include <nsEventQueueUtils.h>
#include <nsEmbedString.h>
#include <nsILocalFile.h>
#include <nsIDirectoryService.h>
#include <nsDirectoryServiceDefs.h>
#endif /* !defined (VBOX_WITH_XPCOM) */
namespace com
{
#if defined (VBOX_WITH_XPCOM)
class DirectoryServiceProvider : public nsIDirectoryServiceProvider
{
public:
{}
virtual ~DirectoryServiceProvider();
const char *aXPTIDatLocation,
const char *aComponentDirLocation,
const char *aCurrProcDirLocation);
private:
char *mCompRegLocation;
char *mXPTIDatLocation;
char *mComponentDirLocation;
char *mCurrProcDirLocation;
};
{
if (mCompRegLocation)
{
}
if (mXPTIDatLocation)
{
}
{
}
if (mCurrProcDirLocation)
{
}
}
/**
* @param aCompRegLocation Path to compreg.dat, in Utf8.
* @param aXPTIDatLocation Path to xpti.data, in Utf8.
*/
const char *aXPTIDatLocation,
const char *aComponentDirLocation,
const char *aCurrProcDirLocation)
{
if (RT_SUCCESS (vrc))
}
{
*aPersistent = PR_TRUE;
const char *fileLocation = NULL;
else
return NS_ERROR_FAILURE;
return rv;
(void **) aRetval);
}
/**
* Global XPCOM initialization flag (we maintain it ourselves since XPCOM
* doesn't provide such functionality)
*/
static bool gIsXPCOMInitialized = false;
/**
* Number of Initialize() calls on the main thread.
*/
static unsigned int gXPCOMInitCount = 0;
#endif /* defined (VBOX_WITH_XPCOM) */
{
#if !defined (VBOX_WITH_XPCOM)
/// @todo the below rough method of changing the aparment type doesn't
/// work on some systems for unknown reason (CoUninitialize() simply does
/// nothing there, or at least all 10 000 of subsequent CoInitializeEx()
/// continue to return RPC_E_CHANGED_MODE there). The problem on those
/// systems is related to the "Extend support for advanced text services
/// to all programs" checkbox in the advanced language settings dialog,
/// i.e. the problem appears when this checkbox is checked and disappears
/// if you clear it. For this reason, we disable the code below and
/// instead initialize COM in MTA as early as possible, before 3rd party
/// libraries we use have done so (i.e. Qt3).
#if 0
/* If we fail to set the necessary apartment model, it may mean that some
* DLL that was indirectly loaded by the process calling this function has
* already initialized COM on the given thread in an incompatible way
* which we can't leave with. Therefore, we try to fix this by using the
* brute force method: */
if (rc == RPC_E_CHANGED_MODE)
{
/* Before we use brute force, we need to check if we are in the
* neutral threaded apartment -- in this case there is no need to
* worry at all. */
if (rc == RPC_E_CHANGED_MODE)
{
/* This is a neutral apartment, reset the error */
LogFlowFunc (("COM is already initialized in neutral threaded "
"apartment mode,\nwill accept it.\n"));
}
{
/* balance the test CoInitializeEx above */
LogFlowFunc (("COM is already initialized in single threaded "
"apartment mode,\nwill reinitialize as "
"multi threaded.\n"));
enum { MaxTries = 10000 };
{
{
/* We've successfully reinitialized COM; restore the
* initialization reference counter */
LogFlowFunc (("Will call CoInitializeEx() %d times.\n",
{
}
}
}
}
else
}
#endif
/* the overall result must be either S_OK or S_FALSE (S_FALSE means
* "already initialized using the same apartment model") */
#else /* !defined (VBOX_WITH_XPCOM) */
if (ASMAtomicXchgBool (&gIsXPCOMInitialized, true) == true)
{
/* XPCOM is already initialized on the main thread, no special
* initialization is necessary on additional threads. Just increase
* the init counter if it's a main thread again (to correctly support
* nested calls to Initialize()/Shutdown() for compatibility with
* Win32). */
if (NS_SUCCEEDED (rc))
{
++ gXPCOMInitCount;
}
AssertComRC (rc);
return rc;
}
/* this is the first initialization */
gXPCOMInitCount = 1;
/* Set VBOX_XPCOM_HOME if not present */
if (!RTEnvExist ("VBOX_XPCOM_HOME"))
{
/* get the executable path */
char pathProgram [RTPATH_MAX];
if (RT_SUCCESS (vrc))
{
char *pathProgramCP = NULL;
if (RT_SUCCESS (vrc))
{
}
}
}
#if defined (XPCOM_GLUE)
#endif
/* prepare paths for registry files */
char homeDir [RTPATH_MAX];
char privateArchDir [RTPATH_MAX];
if (RT_SUCCESS (vrc))
if (RT_SUCCESS (vrc))
{
char compReg [RTPATH_MAX];
char xptiDat [RTPATH_MAX];
char compDir [RTPATH_MAX];
dsProv = new DirectoryServiceProvider();
if (dsProv)
else
}
else
if (NS_SUCCEEDED (rc))
{
/* get the path to the executable */
{
char path [RTPATH_MAX];
#if defined (DEBUG)
if (env)
{
char *appDirUtf8 = NULL;
if (RT_SUCCESS (vrc))
{
if (RT_SUCCESS (vrc))
}
}
else
#endif
{
if (RT_SUCCESS (vrc))
}
if (RT_SUCCESS (vrc))
{
if (NS_SUCCEEDED (rc))
}
else
}
/* Finally, initialize XPCOM */
if (NS_SUCCEEDED (rc))
{
if (NS_SUCCEEDED (rc))
{
if (NS_SUCCEEDED (rc))
}
}
}
#endif /* !defined (VBOX_WITH_XPCOM) */
AssertComRC (rc);
return rc;
}
{
#if !defined (VBOX_WITH_XPCOM)
#else /* !defined (VBOX_WITH_XPCOM) */
{
/* NS_ERROR_NOT_AVAILABLE seems to mean that
* nsIEventQueue::StopAcceptingEvents() has been called (see
* nsEventQueueService.cpp). We hope that this error code always means
* just that in this case and assume that we're on the main thread
* (it's a kind of unexpected behavior if a non-main thread ever calls
* StopAcceptingEvents() on the main event queue). */
if (NS_SUCCEEDED (rc))
{
}
else
{
}
{
/* only the main thread needs to uninitialize XPCOM and only if
* init counter drops to zero */
if (-- gXPCOMInitCount == 0)
{
/* This is a thread initialized XPCOM and set gIsXPCOMInitialized to
* true. Reset it back to false. */
#if defined (XPCOM_GLUE)
#endif
}
}
}
#endif /* !defined (VBOX_WITH_XPCOM) */
AssertComRC (rc);
return rc;
}
}; // namespace com