1999-08-31 21:40:21 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2004-04-18 14:21:17 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
1999-08-31 21:40:21 +00:00
|
|
|
* for the specific language governing rights and limitations under the
|
2004-04-18 14:21:17 +00:00
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org Code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-08-31 21:40:21 +00:00
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2000-05-02 22:38:04 +00:00
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* ***** END LICENSE BLOCK *****
|
2000-05-02 22:38:04 +00:00
|
|
|
* This Original Code has been modified by IBM Corporation.
|
|
|
|
* Modifications made by IBM described herein are
|
|
|
|
* Copyright (c) International Business Machines
|
|
|
|
* Corporation, 2000
|
|
|
|
*
|
|
|
|
* Modifications to Mozilla code or documentation
|
|
|
|
* identified per MPL Section 3.3
|
|
|
|
*
|
|
|
|
* Date Modified by Description of modification
|
|
|
|
* 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
|
1999-08-31 21:40:21 +00:00
|
|
|
*/
|
|
|
|
|
2006-04-13 17:03:18 +00:00
|
|
|
/* Allow logging in the release build */
|
|
|
|
#ifdef MOZ_LOGGING
|
2005-12-13 17:55:43 +00:00
|
|
|
#define FORCE_PR_LOG
|
2006-04-13 17:03:18 +00:00
|
|
|
#endif
|
2005-12-03 15:27:17 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#include "nsNativeComponentLoader.h"
|
2000-01-24 21:28:28 +00:00
|
|
|
|
2000-10-28 22:17:53 +00:00
|
|
|
#include "prlog.h"
|
2005-12-13 17:55:43 +00:00
|
|
|
#include "prinit.h"
|
2006-01-18 15:55:10 +00:00
|
|
|
#include "prerror.h"
|
1999-08-31 21:40:21 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#include "nsComponentManager.h"
|
|
|
|
#include "nsCRTGlue.h"
|
|
|
|
#include "nsModule.h"
|
2008-11-26 19:39:36 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2005-12-13 17:55:43 +00:00
|
|
|
#include "nsTraceRefcntImpl.h"
|
2005-12-03 15:27:17 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsIModule.h"
|
2008-11-26 19:39:36 +00:00
|
|
|
#include "nsIProxyObjectManager.h"
|
2000-10-28 22:17:53 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
2003-04-08 20:29:43 +00:00
|
|
|
#endif
|
1999-09-26 18:06:41 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2003-04-08 20:29:43 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#ifdef VMS
|
|
|
|
#include <lib$routines.h>
|
|
|
|
#include <ssdef.h>
|
|
|
|
#endif
|
2003-04-08 20:29:43 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#if defined(DEBUG) && !defined(XP_BEOS)
|
|
|
|
#define IMPLEMENT_BREAK_AFTER_LOAD
|
|
|
|
#endif
|
2003-04-08 20:29:43 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
static PRLogModuleInfo *nsNativeModuleLoaderLog =
|
|
|
|
PR_NewLogModule("nsNativeModuleLoader");
|
2003-04-08 20:29:43 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#define LOG(level, args) PR_LOG(nsNativeModuleLoaderLog, level, args)
|
2005-12-03 15:27:17 +00:00
|
|
|
|
2008-02-21 20:39:20 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE1(nsNativeModuleLoader,
|
|
|
|
nsIModuleLoader)
|
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
NS_IMPL_ADDREF_USING_AGGREGATOR(nsNativeModuleLoader,
|
|
|
|
nsComponentManagerImpl::gComponentManager)
|
|
|
|
NS_IMPL_RELEASE_USING_AGGREGATOR(nsNativeModuleLoader,
|
|
|
|
nsComponentManagerImpl::gComponentManager)
|
2005-12-03 15:27:17 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
nsresult
|
|
|
|
nsNativeModuleLoader::Init()
|
1999-09-14 13:40:22 +00:00
|
|
|
{
|
2008-11-26 19:39:36 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Startup not on main thread?");
|
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
LOG(PR_LOG_DEBUG, ("nsNativeModuleLoader::Init()"));
|
1999-09-14 13:40:22 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
return mLibraries.Init() ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
1999-09-14 13:40:22 +00:00
|
|
|
}
|
|
|
|
|
2005-12-02 16:34:42 +00:00
|
|
|
NS_IMETHODIMP
|
2005-12-13 17:55:43 +00:00
|
|
|
nsNativeModuleLoader::LoadModule(nsILocalFile* aFile, nsIModule* *aResult)
|
2005-12-03 15:27:17 +00:00
|
|
|
{
|
2005-12-13 17:55:43 +00:00
|
|
|
nsresult rv;
|
2005-12-03 15:27:17 +00:00
|
|
|
|
2008-11-26 19:39:36 +00:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
// If this call is off the main thread, synchronously proxy it
|
|
|
|
// to the main thread.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIModuleLoader> proxythis;
|
|
|
|
rv = NS_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
|
|
|
|
NS_GET_IID(nsIModuleLoader),
|
|
|
|
this, NS_PROXY_SYNC,
|
|
|
|
getter_AddRefs(proxythis));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return proxythis->LoadModule(aFile, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "LoadModule should always proxy to the main thread!");
|
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
// Only load components that end in the proper dynamic library suffix
|
|
|
|
nsCAutoString filePath;
|
|
|
|
aFile->GetNativePath(filePath);
|
|
|
|
if (!StringTail(filePath, sizeof(MOZ_DLL_SUFFIX) - 1).
|
|
|
|
LowerCaseEqualsLiteral(MOZ_DLL_SUFFIX))
|
2005-12-02 16:34:42 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2001-12-19 00:12:41 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
nsCOMPtr<nsIHashable> hashedFile(do_QueryInterface(aFile));
|
|
|
|
if (!hashedFile) {
|
|
|
|
NS_ERROR("nsIFile is not nsIHashable");
|
|
|
|
return NS_NOINTERFACE;
|
1999-10-22 04:47:14 +00:00
|
|
|
}
|
2005-12-03 15:27:17 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
NativeLoadData data;
|
2005-12-03 15:27:17 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
if (mLibraries.Get(hashedFile, &data)) {
|
|
|
|
NS_ASSERTION(data.module, "Corrupt mLibraries hash");
|
|
|
|
NS_ADDREF(*aResult = data.module);
|
2005-12-03 15:27:17 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
LOG(PR_LOG_DEBUG,
|
|
|
|
("nsNativeModuleLoader::LoadModule(\"%s\") - found in cache",
|
|
|
|
filePath.get()));
|
|
|
|
return NS_OK;
|
2005-12-03 15:27:17 +00:00
|
|
|
}
|
2002-11-21 02:36:50 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
// We haven't loaded this module before
|
|
|
|
|
|
|
|
rv = aFile->Load(&data.library);
|
2006-02-14 14:45:32 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2006-01-18 15:55:10 +00:00
|
|
|
char errorMsg[1024] = "<unknown; can't get error from NSPR>";
|
|
|
|
|
|
|
|
if (PR_GetErrorTextLength() < (int) sizeof(errorMsg))
|
|
|
|
PR_GetErrorText(errorMsg);
|
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
LOG(PR_LOG_ERROR,
|
2006-01-18 15:55:10 +00:00
|
|
|
("nsNativeModuleLoader::LoadModule(\"%s\") - load FAILED, "
|
|
|
|
"rv: %lx, error:\n\t%s\n",
|
|
|
|
filePath.get(), rv, errorMsg));
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,
|
|
|
|
"nsNativeModuleLoader::LoadModule(\"%s\") - load FAILED, "
|
|
|
|
"rv: %lx, error:\n\t%s\n",
|
2010-05-17 23:36:31 +00:00
|
|
|
filePath.get(), (unsigned long)rv, errorMsg);
|
2006-01-18 15:55:10 +00:00
|
|
|
#endif
|
|
|
|
|
2002-11-21 02:36:50 +00:00
|
|
|
return rv;
|
2005-12-02 16:34:42 +00:00
|
|
|
}
|
2002-11-21 02:36:50 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#ifdef IMPLEMENT_BREAK_AFTER_LOAD
|
|
|
|
nsCAutoString leafName;
|
|
|
|
aFile->GetNativeLeafName(leafName);
|
|
|
|
|
|
|
|
char *env = getenv("XPCOM_BREAK_ON_LOAD");
|
|
|
|
char *blist;
|
|
|
|
if (env && *env && (blist = strdup(env))) {
|
|
|
|
char *nextTok = blist;
|
|
|
|
while (char *token = NS_strtok(":", &nextTok)) {
|
2006-01-12 01:19:05 +00:00
|
|
|
if (leafName.Find(token, PR_TRUE) != kNotFound) {
|
2005-12-13 17:55:43 +00:00
|
|
|
NS_BREAK();
|
|
|
|
}
|
1999-08-31 21:40:21 +00:00
|
|
|
}
|
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
free(blist);
|
2005-12-02 16:34:42 +00:00
|
|
|
}
|
2003-04-08 20:29:43 +00:00
|
|
|
#endif
|
2000-05-15 19:28:12 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
nsGetModuleProc proc = (nsGetModuleProc)
|
|
|
|
PR_FindFunctionSymbol(data.library, NS_GET_MODULE_SYMBOL);
|
2000-05-15 19:28:12 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
if (proc) {
|
|
|
|
rv = proc(nsComponentManagerImpl::gComponentManager,
|
|
|
|
aFile,
|
|
|
|
getter_AddRefs(data.module));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
LOG(PR_LOG_DEBUG,
|
|
|
|
("nsNativeModuleLoader::LoadModule(\"%s\") - Success",
|
|
|
|
filePath.get()));
|
2000-05-15 19:28:12 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
if (mLibraries.Put(hashedFile, data)) {
|
|
|
|
NS_ADDREF(*aResult = data.module);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-10-22 04:47:14 +00:00
|
|
|
}
|
2005-12-13 17:55:43 +00:00
|
|
|
else {
|
|
|
|
LOG(PR_LOG_WARNING,
|
|
|
|
("nsNativeModuleLoader::LoadModule(\"%s\") - "
|
|
|
|
"Call to NSGetModule failed, rv: %lx", filePath.get(), rv));
|
1999-11-10 00:28:34 +00:00
|
|
|
}
|
1999-10-22 04:47:14 +00:00
|
|
|
}
|
2005-12-13 17:55:43 +00:00
|
|
|
else {
|
|
|
|
LOG(PR_LOG_ERROR,
|
|
|
|
("nsNativeModuleLoader::LoadModule(\"%s\") - "
|
|
|
|
"Symbol NSGetModule not found", filePath.get()));
|
1999-10-22 04:47:14 +00:00
|
|
|
}
|
1999-11-10 00:28:34 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
// at some point we failed, clean up
|
|
|
|
data.module = nsnull;
|
|
|
|
PR_UnloadLibrary(data.library);
|
2005-12-02 16:34:42 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-08-31 21:40:21 +00:00
|
|
|
}
|
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
PLDHashOperator
|
|
|
|
nsNativeModuleLoader::ReleaserFunc(nsIHashable* aHashedFile,
|
|
|
|
NativeLoadData& aLoadData, void*)
|
1999-08-31 21:40:21 +00:00
|
|
|
{
|
2005-12-13 17:55:43 +00:00
|
|
|
aLoadData.module = nsnull;
|
|
|
|
return PL_DHASH_NEXT;
|
1999-08-31 21:40:21 +00:00
|
|
|
}
|
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
PLDHashOperator
|
|
|
|
nsNativeModuleLoader::UnloaderFunc(nsIHashable* aHashedFile,
|
|
|
|
NativeLoadData& aLoadData, void*)
|
1999-08-31 21:40:21 +00:00
|
|
|
{
|
2005-12-13 17:55:43 +00:00
|
|
|
if (PR_LOG_TEST(nsNativeModuleLoaderLog, PR_LOG_DEBUG)) {
|
|
|
|
nsCOMPtr<nsIFile> file(do_QueryInterface(aHashedFile));
|
1999-09-26 18:06:41 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
nsCAutoString filePath;
|
|
|
|
file->GetNativePath(filePath);
|
1999-08-31 21:40:21 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
LOG(PR_LOG_DEBUG,
|
|
|
|
("nsNativeModuleLoader::UnloaderFunc(\"%s\")", filePath.get()));
|
1999-10-22 04:47:14 +00:00
|
|
|
}
|
1999-08-31 21:40:21 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
|
2005-12-02 20:27:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2005-12-13 17:55:43 +00:00
|
|
|
// XXXbsmedberg: do this as soon as the static-destructor crash(es)
|
|
|
|
// are fixed
|
|
|
|
PRStatus ret = PR_UnloadLibrary(aLoadData.library);
|
|
|
|
NS_ASSERTION(ret == PR_SUCCESS, "Failed to unload library");
|
2005-12-02 20:27:52 +00:00
|
|
|
#endif
|
1999-08-31 21:40:21 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
|
2005-12-02 16:34:42 +00:00
|
|
|
#endif
|
1999-09-02 07:00:29 +00:00
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
return PL_DHASH_REMOVE;
|
1999-08-31 21:40:21 +00:00
|
|
|
}
|
|
|
|
|
2010-06-15 19:38:46 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNativeModuleLoader::LoadModuleFromJAR(nsILocalFile* aJAR, const nsACString &aPath, nsIModule* *aResult)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
void
|
|
|
|
nsNativeModuleLoader::UnloadLibraries()
|
1999-08-31 21:40:21 +00:00
|
|
|
{
|
2008-11-26 19:39:36 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Shutdown not on main thread?");
|
|
|
|
|
2005-12-13 17:55:43 +00:00
|
|
|
mLibraries.Enumerate(ReleaserFunc, nsnull);
|
|
|
|
mLibraries.Enumerate(UnloaderFunc, nsnull);
|
1999-08-31 21:40:21 +00:00
|
|
|
}
|