Bug 302099 - XRE_InitEmbedding (xulrunner-only) - r=darin

This commit is contained in:
bsmedberg%covad.net 2005-09-01 13:21:55 +00:00
Родитель 61ab58b925
Коммит 4a5c0c8bd8
4 изменённых файлов: 254 добавлений и 5 удалений

Просмотреть файл

@ -41,4 +41,7 @@ void xxxNeverCalledXUL()
{
XRE_main(0, nsnull, nsnull);
XRE_GetFileFromPath(nsnull, nsnull);
XRE_GetStaticComponents(nsnull, nsnull);
XRE_InitEmbedding(nsnull, nsnull, nsnull, nsnull, 0);
XRE_TermEmbedding();
}

Просмотреть файл

@ -36,7 +36,140 @@
* ***** END LICENSE BLOCK ***** */
#include "nsXULAppAPI.h"
#include <stdlib.h>
#include "nsIAppStartupNotifier.h"
#include "nsIDirectoryService.h"
#include "nsIEventQueueService.h"
#include "nsILocalFile.h"
#include "nsIToolkitChromeRegistry.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsArrayEnumerator.h"
#include "nsCOMArray.h"
#include "nsDirectoryServiceDefs.h"
#include "nsEnumeratorUtils.h"
#include "nsStaticComponents.h"
#include "nsString.h"
class nsEmbeddingDirProvider : public nsIDirectoryServiceProvider2
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIDIRECTORYSERVICEPROVIDER
NS_DECL_NSIDIRECTORYSERVICEPROVIDER2
nsEmbeddingDirProvider(nsILocalFile* aGREDir,
nsILocalFile* aAppDir,
nsIDirectoryServiceProvider* aAppProvider) :
mGREDir(aGREDir),
mAppDir(aAppDir),
mAppProvider(aAppProvider) { }
private:
nsCOMPtr<nsILocalFile> mGREDir;
nsCOMPtr<nsILocalFile> mAppDir;
nsCOMPtr<nsIDirectoryServiceProvider> mAppProvider;
};
NS_IMPL_ISUPPORTS2(nsEmbeddingDirProvider,
nsIDirectoryServiceProvider,
nsIDirectoryServiceProvider2)
NS_IMETHODIMP
nsEmbeddingDirProvider::GetFile(const char *aProperty, PRBool *aPersistent,
nsIFile* *aFile)
{
nsresult rv;
if (mAppProvider) {
rv = mAppProvider->GetFile(aProperty, aPersistent, aFile);
if (NS_SUCCEEDED(rv) && *aFile)
return rv;
}
if (!strcmp(aProperty, NS_OS_CURRENT_PROCESS_DIR) ||
!strcmp(aProperty, NS_APP_INSTALL_CLEANUP_DIR)) {
// NOTE: this is *different* than NS_XPCOM_CURRENT_PROCESS_DIR. This points
// to the application dir. NS_XPCOM_CURRENT_PROCESS_DIR points to the toolkit.
return mAppDir->Clone(aFile);
}
if (!strcmp(aProperty, NS_GRE_DIR)) {
return mGREDir->Clone(aFile);
}
if (!strcmp(aProperty, NS_APP_PREF_DEFAULTS_50_DIR))
{
nsCOMPtr<nsIFile> file;
rv = mAppDir->Clone(getter_AddRefs(file));
NS_ENSURE_SUCCESS(rv, rv);
rv = file->AppendNative(NS_LITERAL_CSTRING("defaults"));
NS_ENSURE_SUCCESS(rv, rv);
rv = file->AppendNative(NS_LITERAL_CSTRING("pref"));
NS_ENSURE_SUCCESS(rv, rv);
NS_ADDREF(*aFile = file);
return NS_OK;
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsEmbeddingDirProvider::GetFiles(const char* aProperty,
nsISimpleEnumerator** aResult)
{
nsresult rv;
nsCOMPtr<nsISimpleEnumerator> appEnum;
nsCOMPtr<nsIDirectoryServiceProvider2> appP2
(do_QueryInterface(mAppProvider));
if (appP2) {
rv = appP2->GetFiles(aProperty, getter_AddRefs(appEnum));
if (NS_SUCCEEDED(rv) && rv != NS_SUCCESS_AGGREGATE_RESULT) {
NS_ADDREF(*aResult = appEnum);
return NS_OK;
}
}
nsCOMArray<nsIFile> dirs;
if (!strcmp(aProperty, NS_CHROME_MANIFESTS_FILE_LIST) ||
!strcmp(aProperty, NS_APP_CHROME_DIR_LIST)) {
nsCOMPtr<nsIFile> manifest;
mGREDir->Clone(getter_AddRefs(manifest));
manifest->AppendNative(NS_LITERAL_CSTRING("chrome"));
dirs.AppendObject(manifest);
mAppDir->Clone(getter_AddRefs(manifest));
manifest->AppendNative(NS_LITERAL_CSTRING("chrome"));
dirs.AppendObject(manifest);
}
if (dirs.Count()) {
nsCOMPtr<nsISimpleEnumerator> thisEnum;
rv = NS_NewArrayEnumerator(getter_AddRefs(thisEnum), dirs);
NS_ENSURE_SUCCESS(rv, rv);
if (appEnum) {
return NS_NewUnionEnumerator(aResult, appEnum, thisEnum);
}
NS_ADDREF(*aResult = thisEnum);
return NS_OK;
}
if (appEnum) {
NS_ADDREF(*aResult = appEnum);
return NS_OK;
}
return NS_ERROR_FAILURE;
}
void
XRE_GetStaticComponents(nsStaticModuleInfo const **aStaticComponents,
@ -46,3 +179,78 @@ XRE_GetStaticComponents(nsStaticModuleInfo const **aStaticComponents,
*aComponentCount = kStaticModuleCount;
}
static nsStaticModuleInfo *sCombined;
static PRInt32 sInitCounter;
nsresult
XRE_InitEmbedding(nsILocalFile *aLibXULDirectory,
nsILocalFile *aAppDirectory,
nsIDirectoryServiceProvider *aAppDirProvider,
nsStaticModuleInfo const *aStaticComponents,
PRUint32 aStaticComponentCount)
{
if (++sInitCounter > 1)
return NS_OK;
NS_ENSURE_ARG(aLibXULDirectory);
NS_ENSURE_ARG(aAppDirectory);
nsresult rv;
nsCOMPtr<nsIDirectoryServiceProvider> dirSvc
(new nsEmbeddingDirProvider(aLibXULDirectory,
aAppDirectory,
aAppDirProvider));
if (!dirSvc)
return NS_ERROR_OUT_OF_MEMORY;
// Combine the toolkit static components and the app components.
PRUint32 combinedCount = kStaticModuleCount + aStaticComponentCount;
sCombined = new nsStaticModuleInfo[combinedCount];
if (!sCombined)
return NS_ERROR_OUT_OF_MEMORY;
memcpy(sCombined, kPStaticModules,
sizeof(nsStaticModuleInfo) * kStaticModuleCount);
memcpy(sCombined + kStaticModuleCount, aStaticComponents,
sizeof(nsStaticModuleInfo) * aStaticComponentCount);
rv = NS_InitXPCOM3(nsnull, aAppDirectory, dirSvc,
sCombined, combinedCount);
if (NS_FAILED(rv))
return rv;
// We do not need to autoregister components here. The CheckUpdateFile()
// bits in NS_InitXPCOM3 check for an .autoreg file. If the app wants
// to autoregister every time (for instance, if it's debug), it can do
// so after we return from this function.
nsCOMPtr<nsIEventQueueService> eventQService
(do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv));
if (NS_FAILED(rv))
return rv;
rv = eventQService->CreateThreadEventQueue();
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIObserver> startupNotifier
(do_CreateInstance(NS_APPSTARTUPNOTIFIER_CONTRACTID));
if (!startupNotifier)
return NS_ERROR_FAILURE;
startupNotifier->Observe(nsnull, APPSTARTUP_TOPIC, nsnull);
return NS_OK;
}
void
XRE_TermEmbedding()
{
if (--sInitCounter != 0)
return;
NS_ShutdownXPCOM(nsnull);
delete [] sCombined;
}

Просмотреть файл

@ -175,17 +175,20 @@ NS_IMETHODIMP
nsXREDirProvider::GetFile(const char* aProperty, PRBool* aPersistent,
nsIFile** aFile)
{
nsresult rv = NS_ERROR_FAILURE;
*aPersistent = PR_TRUE;
nsCOMPtr<nsIFile> file;
if (!strcmp(aProperty, NS_OS_CURRENT_PROCESS_DIR) ||
!strcmp(aProperty, NS_APP_INSTALL_CLEANUP_DIR)) {
// NOTE: this is *different* than NS_XPCOM_CURRENT_PROCESS_DIR. This points
// to the application dir. NS_XPCOM_CURRENT_PROCESS_DIR points to the toolkit.
// NOTE: this should be *different* than NS_XPCOM_CURRENT_PROCESS_DIR.
// This should point to the application dir.
// NS_XPCOM_CURRENT_PROCESS_DIR points to the toolkit. But we suck.
return mAppDir->Clone(aFile);
}
else if (!strcmp(aProperty, NS_APP_PROFILE_DEFAULTS_50_DIR) ||
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsIFile> file;
if (!strcmp(aProperty, NS_APP_PROFILE_DEFAULTS_50_DIR) ||
!strcmp(aProperty, NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR)) {
return GetProfileDefaultsDir(aFile);
}

Просмотреть файл

@ -213,4 +213,39 @@ extern "C" XULAPI void
XRE_GetStaticComponents(nsStaticModuleInfo const **aStaticComponents,
PRUint32 *aComponentCount);
/**
* Initialize libXUL for embedding purposes.
*
* @param aLibXULDirectory The directory in which the libXUL shared library
* was found.
* @param aAppDirectory The directory in which the application components
* and resources can be found. This will map to
* the "resource:app" directory service key.
* @param aAppDirProvider A directory provider for the application. This
* provider will be aggregated by a libxul provider
* which will provide the base required GRE keys.
* @param aStaticComponents Static components provided by the embedding
* application. This should *not* include the
* components from XRE_GetStaticComponents. May be
* null if there are no static components.
* @param aStaticComponentCount the number of static components in
* aStaticComponents
*
* @note This function must be called from the "main" thread.
*
* @note At the present time, this function may only be called once in
* a given process. Use XRE_TermEmbedding to clean up and free
* resources allocated by XRE_InitEmbedding.
*/
extern "C" XULAPI nsresult
XRE_InitEmbedding(nsILocalFile *aLibXULDirectory,
nsILocalFile *aAppDirectory,
nsIDirectoryServiceProvider *aAppDirProvider = nsnull,
nsStaticModuleInfo const *aStaticComponents = nsnull,
PRUint32 aStaticComponentCount = 0);
extern "C" XULAPI void
XRE_TermEmbedding();
#endif // _nsXULAppAPI_h__