Mozilla/mozilla/embedding/tests/mfcembed/winEmbedFileLocProvider.cpp
dougt%netscape.com ecdd8ab65f Fixes the following:
154047
This change cleans up the category manager in hopes to freeze it for 1.1 final.  This change removes dead and unsupported methods from the interface as well as removes the C++ code from the interface.  This should land for 1.1b since users of the current category manager will have to update their components.

157597
Make embedding base and examples use XPCOM Glue
This change makes our embedding samples use the XPCOM glue.

157625
nsLocalFile::Remove's file pointer is sometimes null afte...
This fixes a crash when we dereference null.  It is a bandaide fix as the real problems involves retooling the directory enumerator.

157801
This fixes a terrible state which you can get into whereby XPCOM will fail to startup regardless of what you do.  The fix is to cause autoreg to happen if the component registry (compreg.dat) is not found.

r/sr=alecf@netscape.com, rpotts@netscape.com.  a=scc@mozilla.org


git-svn-id: svn://10.0.0.236/trunk@125444 18797224-902f-48f8-a5cc-f745e15eee43
2002-07-18 05:09:10 +00:00

374 lines
13 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: Mozilla-sample-code 1.0
*
* Copyright (c) 2002 Netscape Communications Corporation and
* other contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this Mozilla sample software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Contributor(s):
* Conrad Carlen <conrad@ingress.com>
*
* ***** END LICENSE BLOCK ***** */
#include "winEmbedFileLocProvider.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceDefs.h"
#include "nsILocalFile.h"
#include "nsString.h"
#include "nsXPIDLString.h"
#include <windows.h>
#include <shlobj.h>
// WARNING: These hard coded names need to go away. They need to
// come from localizable resources
#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("registry.dat")
#define PROFILE_ROOT_DIR_NAME NS_LITERAL_CSTRING("Profiles")
#define DEFAULTS_DIR_NAME NS_LITERAL_CSTRING("defaults")
#define DEFAULTS_PREF_DIR_NAME NS_LITERAL_CSTRING("pref")
#define DEFAULTS_PROFILE_DIR_NAME NS_LITERAL_CSTRING("profile")
#define RES_DIR_NAME NS_LITERAL_CSTRING("res")
#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
//*****************************************************************************
winEmbedFileLocProvider::winEmbedFileLocProvider(const char* productDirName)
{
NS_INIT_ISUPPORTS();
strncpy(mProductDirName, productDirName, sizeof(mProductDirName) - 1);
mProductDirName[sizeof(mProductDirName) - 1] = '\0';
}
winEmbedFileLocProvider::~winEmbedFileLocProvider()
{
}
//*****************************************************************************
// winEmbedFileLocProvider::nsISupports
//*****************************************************************************
NS_IMPL_ISUPPORTS1(winEmbedFileLocProvider, nsIDirectoryServiceProvider)
//*****************************************************************************
// winEmbedFileLocProvider::nsIDirectoryServiceProvider
//*****************************************************************************
NS_IMETHODIMP
winEmbedFileLocProvider::GetFile(const char *prop, PRBool *persistant, nsIFile **_retval)
{
nsCOMPtr<nsILocalFile> localFile;
nsresult rv = NS_ERROR_FAILURE;
*_retval = nsnull;
*persistant = PR_TRUE;
if (strcmp(prop, NS_APP_APPLICATION_REGISTRY_DIR) == 0)
{
rv = GetProductDirectory(getter_AddRefs(localFile));
}
else if (strcmp(prop, NS_APP_APPLICATION_REGISTRY_FILE) == 0)
{
rv = GetProductDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv))
rv = localFile->AppendNative(APP_REGISTRY_NAME);
}
else if (strcmp(prop, NS_APP_DEFAULTS_50_DIR) == 0)
{
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv))
rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
}
else if (strcmp(prop, NS_APP_PREF_DEFAULTS_50_DIR) == 0)
{
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
if (NS_SUCCEEDED(rv))
rv = localFile->AppendRelativeNativePath(DEFAULTS_PREF_DIR_NAME);
}
}
else if (strcmp(prop, NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR) == 0 ||
strcmp(prop, NS_APP_PROFILE_DEFAULTS_50_DIR) == 0)
{
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
if (NS_SUCCEEDED(rv))
rv = localFile->AppendRelativeNativePath(DEFAULTS_PROFILE_DIR_NAME);
}
}
else if (strcmp(prop, NS_APP_USER_PROFILES_ROOT_DIR) == 0)
{
rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile));
}
else if (strcmp(prop, NS_APP_RES_DIR) == 0)
{
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv))
rv = localFile->AppendRelativeNativePath(RES_DIR_NAME);
}
else if (strcmp(prop, NS_APP_CHROME_DIR) == 0)
{
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv))
rv = localFile->AppendRelativeNativePath(CHROME_DIR_NAME);
}
else if (strcmp(prop, NS_APP_PLUGINS_DIR) == 0)
{
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv))
rv = localFile->AppendRelativeNativePath(PLUGINS_DIR_NAME);
}
else if (strcmp(prop, NS_APP_SEARCH_DIR) == 0)
{
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
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 (strcmp(prop, NS_MRE_DIR) == 0)
{
rv = GetMreDirectory(getter_AddRefs(localFile));
}
else if (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<nsILocalFile> 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);
nsresult rv;
if (!mMozBinDirectory)
{
// Get the mozilla bin directory
// 1. Check the directory service first for NS_XPCOM_CURRENT_PROCESS_DIR
// This will be set if a directory was passed to NS_InitXPCOM
// 2. If that doesn't work, set it to be the current process directory
nsCOMPtr<nsIProperties> directoryService =
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv))
return rv;
rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(mMozBinDirectory));
if (NS_FAILED(rv)) {
rv = directoryService->Get(NS_OS_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(mMozBinDirectory));
if (NS_FAILED(rv))
return rv;
}
}
nsCOMPtr<nsIFile> aFile;
rv = mMozBinDirectory->Clone(getter_AddRefs(aFile));
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsILocalFile> lfile = do_QueryInterface (aFile);
if (!lfile)
return NS_ERROR_FAILURE;
NS_IF_ADDREF(*aLocalFile = lfile);
return NS_OK;
}
//----------------------------------------------------------------------------------------
// GetProductDirectory - Gets the directory which contains the application data folder
//
// WIN : <Application Data folder on user's machine>\Mozilla
//----------------------------------------------------------------------------------------
NS_METHOD winEmbedFileLocProvider::GetProductDirectory(nsILocalFile **aLocalFile)
{
NS_ENSURE_ARG_POINTER(aLocalFile);
nsresult rv;
PRBool exists;
nsCOMPtr<nsILocalFile> localDir;
nsCOMPtr<nsIProperties> directoryService =
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
rv = directoryService->Get(NS_WIN_APPDATA_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(localDir));
if (NS_SUCCEEDED(rv))
rv = localDir->Exists(&exists);
if (NS_FAILED(rv) || !exists)
{
// On some Win95 machines, NS_WIN_APPDATA_DIR does not exist - revert to NS_WIN_WINDOWS_DIR
localDir = nsnull;
rv = directoryService->Get(NS_WIN_WINDOWS_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(localDir));
}
if (NS_FAILED(rv)) return rv;
rv = localDir->AppendRelativeNativePath(nsDependentCString(mProductDirName));
if (NS_FAILED(rv)) return rv;
rv = localDir->Exists(&exists);
if (NS_SUCCEEDED(rv) && !exists)
rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
if (NS_FAILED(rv)) return rv;
*aLocalFile = localDir;
NS_ADDREF(*aLocalFile);
return rv;
}
//----------------------------------------------------------------------------------------
// GetDefaultUserProfileRoot - Gets the directory which contains each user profile dir
//
// WIN : <Application Data folder on user's machine>\Mozilla\Users50
//----------------------------------------------------------------------------------------
NS_METHOD winEmbedFileLocProvider::GetDefaultUserProfileRoot(nsILocalFile **aLocalFile)
{
NS_ENSURE_ARG_POINTER(aLocalFile);
nsresult rv;
PRBool exists;
nsCOMPtr<nsILocalFile> localDir;
rv = GetProductDirectory(getter_AddRefs(localDir));
if (NS_FAILED(rv)) return rv;
// These 3 platforms share this part of the path - do them as one
rv = localDir->AppendRelativeNativePath(PROFILE_ROOT_DIR_NAME);
if (NS_FAILED(rv)) return rv;
rv = localDir->Exists(&exists);
if (NS_SUCCEEDED(rv) && !exists)
rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
if (NS_FAILED(rv)) return rv;
*aLocalFile = localDir;
NS_ADDREF(*aLocalFile);
return rv;
}