From 3fb8d58b926303156ad3ff24c4d5d28686cfcc42 Mon Sep 17 00:00:00 2001 From: "ashishbhatt%netscape.com" Date: Mon, 24 Jun 2002 19:32:18 +0000 Subject: [PATCH] Updated testEmbed for MRE application support. Changed files testembed.cpp, winfilelocprovider.cpp & .h git-svn-id: svn://10.0.0.236/trunk@123928 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/embedding/qa/testembed/TestEmbed.cpp | 21 +++- .../qa/testembed/winEmbedFileLocProvider.cpp | 103 +++++++++++++++++- .../qa/testembed/winEmbedFileLocProvider.h | 3 +- 3 files changed, 121 insertions(+), 6 deletions(-) diff --git a/mozilla/embedding/qa/testembed/TestEmbed.cpp b/mozilla/embedding/qa/testembed/TestEmbed.cpp index 988e571fa06..28ac60f95fd 100644 --- a/mozilla/embedding/qa/testembed/TestEmbed.cpp +++ b/mozilla/embedding/qa/testembed/TestEmbed.cpp @@ -55,11 +55,11 @@ // Local Includes #include "stdafx.h" +#include "BrowserImpl.h" #include "TestEmbed.h" #include "BrowserFrm.h" #include "winEmbedFileLocProvider.h" #include "ProfileMgr.h" -#include "BrowserImpl.h" #include "BrowserView.h" #include "nsIWindowWatcher.h" #include "plstr.h" @@ -246,6 +246,22 @@ BOOL CTestEmbedApp::InitInstance() Enable3dControls(); + // + // 1. Determine the name of the dir from which the MRE based app is being run + // from [It's OK to do this even if you're not running in an MRE env] + // + // 2. Create an nsILocalFile out of it which will passed in to NS_InitEmbedding() + // + // Please see http://www.mozilla.org/projects/embedding/MRE.html + // for more info. on MRE + + char curDir[_MAX_PATH+1]; + ::GetCurrentDirectory(_MAX_PATH, curDir); + nsresult rv; + nsCOMPtr mreAppDir; + rv = NS_NewNativeLocalFile(nsDependentCString(curDir), TRUE, getter_AddRefs(mreAppDir)); + NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create mreAppDir localfile"); + // Take a look at // http://www.mozilla.org/projects/xpcom/file_locations.html // for more info on File Locations @@ -257,8 +273,7 @@ BOOL CTestEmbedApp::InitInstance() return FALSE; } - nsresult rv; - rv = NS_InitEmbedding(nsnull, provider); + rv = NS_InitEmbedding(mreAppDir, provider); if(NS_FAILED(rv)) { QAOutput("TestEmbed didn't start up."); diff --git a/mozilla/embedding/qa/testembed/winEmbedFileLocProvider.cpp b/mozilla/embedding/qa/testembed/winEmbedFileLocProvider.cpp index f4b5e1fd859..83f62d2d163 100644 --- a/mozilla/embedding/qa/testembed/winEmbedFileLocProvider.cpp +++ b/mozilla/embedding/qa/testembed/winEmbedFileLocProvider.cpp @@ -47,7 +47,7 @@ #define CHROME_DIR_NAME NS_LITERAL_CSTRING("chrome") #define PLUGINS_DIR_NAME NS_LITERAL_CSTRING("plugins") #define SEARCH_DIR_NAME NS_LITERAL_CSTRING("searchplugins") - +#define COMPONENTS_DIR_NAME NS_LITERAL_CSTRING("components") //***************************************************************************** // winEmbedFileLocProvider::Constructor/Destructor @@ -148,14 +148,113 @@ winEmbedFileLocProvider::GetFile(const char *prop, PRBool *persistant, nsIFile * if (NS_SUCCEEDED(rv)) rv = localFile->AppendRelativeNativePath(SEARCH_DIR_NAME); } - + //--------------------------------------------------------------- + // Note that by returning a valid localFile's for NS_MRE_DIR and + // NS_NRE_COMPONENT_DIR your app is indicating to XPCOM that + // it found an MRE version with which it's compatible with and + // it intends to be "run against" that MRE + // + // Please see http://www.mozilla.org/projects/embedding/MRE.html + // for more info. on MRE + //--------------------------------------------------------------- + else if (nsCRT::strcmp(prop, NS_MRE_DIR) == 0) + { + rv = GetMreDirectory(getter_AddRefs(localFile)); + } + else if (nsCRT::strcmp(prop, NS_MRE_COMPONENT_DIR) == 0) + { + rv = GetMreDirectory(getter_AddRefs(localFile)); + if (NS_SUCCEEDED(rv)) + rv = localFile->AppendRelativeNativePath(COMPONENTS_DIR_NAME); + } + if (localFile && NS_SUCCEEDED(rv)) return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)_retval); return rv; } +// Get the location of the MRE version we're compatible with from +// the registry +// +static char * GetMreLocationFromRegistry() +{ + char szKey[256]; + HKEY hRegKey = NULL; + DWORD dwLength = _MAX_PATH * sizeof(char); + long rc; + char keyValue[_MAX_PATH + 1]; + char *pMreLocation = NULL; + // A couple of key points here: + // 1. Note the usage of the "Software\\Mozilla\\MRE" subkey - this allows + // us to have multiple versions of MREs on the same machine by having + // subkeys such as 1.0, 1.1, 2.0 etc. under it. + // 2. In this sample below we're looking for the location of MRE version 1.0 + // i.e. we're compatible with MRE 1.0 and we're trying to find it's install + // location. + // + // Please see http://www.mozilla.org/projects/embedding/MRE.html for + // more info. + // + strcpy(szKey, "Software\\Mozilla\\MRE\\1.0"); + + if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, KEY_QUERY_VALUE, &hRegKey) == ERROR_SUCCESS) + { + if ((rc = ::RegQueryValueEx(hRegKey, "MreHome", NULL, NULL, (BYTE *)keyValue, &dwLength))==ERROR_SUCCESS) + { + pMreLocation = ::strdup(keyValue); + ::RegCloseKey(hRegKey); + } + } + + return pMreLocation; +} + +// Create and return the location of the MRE the application is +// currently using, if any, via the |aLocalFile| param +// +// If an embedding application is written to use an MRE it determines +// the compatible MRE's location by looking in the Windows registry +// In this case GetMreDirectory() creates a new localFile based on the +// MRE path it just read from the registry +// +// If the embedding appliction is not using an MRE and is running in +// a "regular" embedding scenario GetMreDirectory() simply returns a +// failure code indicating to the caller to fallback to a non-MRE +// based operation - which is the default mode of operation. +// +// Please see http://www.mozilla.org/projects/embedding/MRE.html for +// more information on the Mozilla Runtime Environment(MRE) and for +// the actual registry key whichs contains the MRE path, if any. + +NS_METHOD winEmbedFileLocProvider::GetMreDirectory(nsILocalFile **aLocalFile) +{ + NS_ENSURE_ARG_POINTER(aLocalFile); + nsresult rv = NS_ERROR_FAILURE; + + // Get the path of the MRE which is compatible with our embedding application + // from the registry + // + char *pMreDir = GetMreLocationFromRegistry(); + if(pMreDir) + { + nsCOMPtr tempLocal; + rv = NS_NewNativeLocalFile(nsDependentCString(pMreDir), TRUE, getter_AddRefs(tempLocal)); + + if (tempLocal) + { + *aLocalFile = tempLocal; + NS_ADDREF(*aLocalFile); + rv = NS_OK; + } + + ::free(pMreDir); + } + + return rv; +} + NS_METHOD winEmbedFileLocProvider::CloneMozBinDirectory(nsILocalFile **aLocalFile) { NS_ENSURE_ARG_POINTER(aLocalFile); diff --git a/mozilla/embedding/qa/testembed/winEmbedFileLocProvider.h b/mozilla/embedding/qa/testembed/winEmbedFileLocProvider.h index 2c66b8f8f19..dea9339e7cd 100644 --- a/mozilla/embedding/qa/testembed/winEmbedFileLocProvider.h +++ b/mozilla/embedding/qa/testembed/winEmbedFileLocProvider.h @@ -47,7 +47,8 @@ protected: NS_METHOD CloneMozBinDirectory(nsILocalFile **aLocalFile); NS_METHOD GetProductDirectory(nsILocalFile **aLocalFile); NS_METHOD GetDefaultUserProfileRoot(nsILocalFile **aLocalFile); - + NS_METHOD GetMreDirectory(nsILocalFile **aLocalFile); + char mProductDirName[256]; nsCOMPtr mMozBinDirectory; };