initterm.cpp revision 612f9ce48ee43980e278933074ed56a5498658c5
/* $Id$ */
/** @file
* MS COM / XPCOM Abstraction Layer - Initialization and Termination.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* 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 (GPL) 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
* distributed 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) */
#include "VBox/com/EventQueue.h"
#include "VBox/com/AutoLock.h"
namespace com
{
#if defined(VBOX_WITH_XPCOM)
class DirectoryServiceProvider : public nsIDirectoryServiceProvider
{
public:
{}
virtual ~DirectoryServiceProvider();
const char *aXPTIDatLocation,
const char *aComponentDirLocation,
const char *aCurrProcDirLocation);
private:
/** @remarks This is not a UTF-8 string. */
char *mCompRegLocation;
/** @remarks This is not a UTF-8 string. */
char *mXPTIDatLocation;
/** @remarks This is not a UTF-8 string. */
char *mComponentDirLocation;
/** @remarks This is not a UTF-8 string. */
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)
{
/** @todo r=bird: Gotta check how this encoding stuff plays out on darwin!
* We get down to [VBoxNsxp]NS_NewNativeLocalFile and that file isn't
* nsLocalFileUnix.cpp on 32-bit darwin. On 64-bit darwin it's a question
* of what we're doing in IPRT and such... We should probably add a
* RTPathConvertToNative for use here. */
if (RT_SUCCESS(vrc))
}
{
*aPersistent = PR_TRUE;
const char *fileLocation = NULL;
else
return NS_ERROR_FAILURE;
return rv;
}
/**
* Global XPCOM initialization flag (we maintain it ourselves since XPCOM
* doesn't provide such functionality)
*/
static bool volatile gIsXPCOMInitialized = false;
/**
* Number of Initialize() calls on the main thread.
*/
static unsigned int gXPCOMInitCount = 0;
#else /* !defined (VBOX_WITH_XPCOM) */
/**
* The COM main thread handle. (The first caller of com::Initialize().)
*/
/**
* Number of Initialize() calls on the main thread.
*/
static uint32_t gCOMMainInitCount = 0;
#endif /* !defined (VBOX_WITH_XPCOM) */
/**
* Initializes the COM runtime.
*
* This method must be called on each thread of the client application that
* wants to access COM facilities. The initialization must be performed before
* calling any other COM method or attempting to instantiate COM objects.
*
* On platforms using XPCOM, this method uses the following scheme to search for
* XPCOM runtime:
*
* 1. If the VBOX_APP_HOME environment variable is set, the path it specifies
* is used to search XPCOM libraries and components. If this method fails to
* initialize XPCOM runtime using this path, it will immediately return a
* failure and will NOT check for other paths as described below.
*
* 2. If VBOX_APP_HOME is not set, this methods tries the following paths in the
* given order:
*
* a) Compiled-in application data directory (as returned by
* RTPathAppPrivateArch())
* b) "/usr/lib/virtualbox" (Linux only)
* c) "/opt/VirtualBox" (Linux only)
*
* The first path for which the initialization succeeds will be used.
*
* On MS COM platforms, the COM runtime is provided by the system and does not
* need to be searched for.
*
* Once the COM subsystem is no longer necessary on a given thread, Shutdown()
* must be called to free resources allocated for it. Note that a thread may
* call Initialize() several times but for each of tese calls there must be a
* corresponding Shutdown() call.
*
* @return S_OK on success and a COM result code in case of failure.
*/
{
#if !defined(VBOX_WITH_XPCOM)
/*
* We initialize COM in GUI thread in STA, to be compliant with QT and
* OLE requirments (for example to allow D&D), while other threads
* initialized in regular MTA. To allow fast proxyless access from
* GUI thread to COM objects, we explicitly provide our COM objects
* with free threaded marshaller.
* !!!!! Please think twice before touching this code !!!!!
*/
:
/* the overall result must be either S_OK or S_FALSE (S_FALSE means
* "already initialized using the same apartment model") */
/* To be flow compatible with the XPCOM case, we return here if this isn't
* the main thread or if it isn't its first initialization call.
* Note! CoInitializeEx and CoUninitialize does it's own reference
* counting, so this exercise is entirely for the EventQueue init. */
bool fRc;
if (hSelf != NIL_RTTHREAD)
else
fRc = false;
if (fGui)
if (!fRc)
{
if ( gCOMMainThread == hSelf
return rc;
}
/* this is the first main thread initialization */
Assert(gCOMMainInitCount == 0);
gCOMMainInitCount = 1;
#else /* !defined (VBOX_WITH_XPCOM) */
/* Unused here */
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))
{
}
return rc;
}
/* this is the first initialization */
gXPCOMInitCount = 1;
bool const fInitEventQueues = true;
/* prepare paths for registry files */
char szCompReg[RTPATH_MAX];
char szXptiDat[RTPATH_MAX];
#if defined (XPCOM_GLUE)
#endif
static const char *kAppPathsToProbe[] =
{
NULL, /* 0: will use VBOX_APP_HOME */
NULL, /* 1: will try RTPathAppPrivateArch() */
#ifdef RT_OS_LINUX
"/usr/lib/virtualbox",
"/opt/VirtualBox",
"/opt/VirtualBox/amd64",
"/opt/VirtualBox/i386",
#endif
};
/* Find out the directory where VirtualBox binaries are located */
{
char szAppHomeDir[RTPATH_MAX];
if (i == 0)
{
/* Use VBOX_APP_HOME if present */
if (vrc == VERR_ENV_VAR_NOT_FOUND)
continue;
}
else if (i == 1)
{
/* Use RTPathAppPrivateArch() first */
}
else
{
/* Iterate over all other paths */
vrc = VINF_SUCCESS;
}
if (RT_FAILURE(vrc))
{
continue;
}
char szCompDir[RTPATH_MAX];
if (RT_FAILURE(vrc))
{
continue;
}
dsProv = new DirectoryServiceProvider();
if (dsProv)
else
break;
/* Setup the application path for NS_InitXPCOM2. Note that we properly
* answer the NS_XPCOM_CURRENT_PROCESS_DIR query in our directory
* service provider but it seems to be activated after the directory
* service is used for the first time (see the source NS_InitXPCOM2). So
* use the same value here to be on the safe side. */
{
if (RT_SUCCESS(vrc))
{
if (NS_SUCCEEDED(rc))
}
else
}
break;
/* Set VBOX_XPCOM_HOME to the same app path to make XPCOM sources that
* still use it instead of the directory service happy */
/* Finally, initialize XPCOM */
{
if (NS_SUCCEEDED(rc))
{
if (NS_SUCCEEDED(rc))
{
if (NS_SUCCEEDED(rc))
{
/* We succeeded, stop probing paths */
LogFlowFunc(("Succeeded.\n"));
break;
}
}
}
}
/* clean up before the new try */
if (i == 0)
{
/* We failed with VBOX_APP_HOME, don't probe other paths */
break;
}
}
#endif /* !defined (VBOX_WITH_XPCOM) */
// for both COM and XPCOM, we only get here if this is the main thread;
// only then initialize the autolock system (AutoLock.cpp)
/*
* Init the main event queue (ASSUMES it cannot fail).
*/
EventQueue::init();
return rc;
}
{
#if !defined(VBOX_WITH_XPCOM)
/* EventQueue::uninit reference counting fun. */
if ( hSelf == gCOMMainThread
&& hSelf != NIL_RTTHREAD)
{
if (-- gCOMMainInitCount == 0)
{
EventQueue::uninit();
}
}
#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)
{
EventQueue::uninit();
/* 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) */
return rc;
}
} /* namespace com */