1999-09-01 01:40:21 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
1999-02-02 01:40:48 +03:00
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
1999-02-03 18:39:13 +03:00
|
|
|
/* nsDll
|
1999-02-02 01:40:48 +03:00
|
|
|
*
|
|
|
|
* Abstraction of a Dll. Stores modifiedTime and size for easy detection of
|
|
|
|
* change in dll.
|
|
|
|
*
|
|
|
|
* dp Suresh <dp@netscape.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "xcDll.h"
|
1999-08-09 04:19:08 +04:00
|
|
|
#include "nsDebug.h"
|
1999-06-22 18:02:58 +04:00
|
|
|
#include "nsIComponentManager.h"
|
1999-08-09 04:19:08 +04:00
|
|
|
#include "nsIModule.h"
|
|
|
|
#include "nsIFileSpec.h"
|
1999-08-13 23:27:58 +04:00
|
|
|
#include "nsCOMPtr.h"
|
1999-02-02 01:40:48 +03:00
|
|
|
|
1999-10-06 05:09:13 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#include "prlink_mac.h"
|
|
|
|
#endif
|
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
nsDll::nsDll(const char *codeDllName, int type)
|
|
|
|
: m_dllName(NULL), m_dllSpec(NULL), m_modDate(0), m_size(0),
|
1999-08-11 05:46:43 +04:00
|
|
|
m_instance(NULL), m_status(DLL_OK), m_moduleObject(NULL),
|
1999-09-14 02:34:42 +04:00
|
|
|
m_persistentDescriptor(NULL), m_nativePath(NULL),
|
|
|
|
m_markForUnload(PR_FALSE), m_registryLocation(0)
|
|
|
|
|
1999-02-02 01:40:48 +03:00
|
|
|
{
|
1999-06-22 18:02:58 +04:00
|
|
|
if (!codeDllName || !*codeDllName)
|
|
|
|
{
|
|
|
|
m_status = DLL_INVALID_PARAM;
|
|
|
|
return;
|
|
|
|
}
|
1999-07-31 04:28:51 +04:00
|
|
|
m_dllName = nsCRT::strdup(codeDllName);
|
1999-06-22 18:02:58 +04:00
|
|
|
if (!m_dllName)
|
|
|
|
{
|
|
|
|
m_status = DLL_NO_MEM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1999-02-03 21:49:04 +03:00
|
|
|
|
1999-09-01 01:40:21 +04:00
|
|
|
nsDll::nsDll(nsIFileSpec *dllSpec, const char *registryLocation)
|
1999-06-22 18:02:58 +04:00
|
|
|
: m_dllName(NULL), m_dllSpec(dllSpec), m_modDate(0), m_size(0),
|
1999-08-11 05:46:43 +04:00
|
|
|
m_instance(NULL), m_status(DLL_OK), m_moduleObject(NULL),
|
1999-08-13 23:27:58 +04:00
|
|
|
m_persistentDescriptor(NULL), m_nativePath(NULL), m_markForUnload(PR_FALSE)
|
1999-08-11 05:46:43 +04:00
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
{
|
1999-09-01 01:40:21 +04:00
|
|
|
m_registryLocation = nsCRT::strdup(registryLocation);
|
1999-06-22 18:02:58 +04:00
|
|
|
Init(dllSpec);
|
1999-09-01 10:22:54 +04:00
|
|
|
// Populate m_modDate and m_size
|
|
|
|
if (NS_FAILED(Sync()))
|
|
|
|
{
|
|
|
|
m_status = DLL_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDll::nsDll(nsIFileSpec *dllSpec, const char *registryLocation, PRUint32 modDate, PRUint32 fileSize)
|
|
|
|
: m_dllName(NULL), m_dllSpec(dllSpec), m_modDate(0), m_size(0),
|
|
|
|
m_instance(NULL), m_status(DLL_OK), m_moduleObject(NULL),
|
|
|
|
m_persistentDescriptor(NULL), m_nativePath(NULL), m_markForUnload(PR_FALSE)
|
|
|
|
|
|
|
|
{
|
|
|
|
m_registryLocation = nsCRT::strdup(registryLocation);
|
|
|
|
Init(dllSpec);
|
|
|
|
m_modDate = modDate;
|
|
|
|
m_size = fileSize;
|
1999-02-02 01:40:48 +03:00
|
|
|
}
|
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
nsDll::nsDll(const char *libPersistentDescriptor)
|
|
|
|
: m_dllName(NULL), m_dllSpec(NULL), m_modDate(0), m_size(0),
|
1999-08-11 05:46:43 +04:00
|
|
|
m_instance(NULL), m_status(DLL_OK), m_moduleObject(NULL),
|
1999-09-14 02:34:42 +04:00
|
|
|
m_persistentDescriptor(NULL), m_nativePath(NULL),
|
|
|
|
m_markForUnload(PR_FALSE), m_registryLocation(0)
|
1999-08-11 05:46:43 +04:00
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
{
|
|
|
|
Init(libPersistentDescriptor);
|
1999-09-01 10:22:54 +04:00
|
|
|
// Populate m_modDate and m_size
|
|
|
|
if (NS_FAILED(Sync()))
|
|
|
|
{
|
|
|
|
m_status = DLL_INVALID_PARAM;
|
|
|
|
}
|
1999-06-22 18:02:58 +04:00
|
|
|
}
|
1999-02-09 23:30:34 +03:00
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
nsDll::nsDll(const char *libPersistentDescriptor, PRUint32 modDate, PRUint32 fileSize)
|
|
|
|
: m_dllName(NULL), m_dllSpec(NULL), m_modDate(0), m_size(0),
|
1999-08-11 05:46:43 +04:00
|
|
|
m_instance(NULL), m_status(DLL_OK), m_moduleObject(NULL),
|
1999-09-14 02:34:42 +04:00
|
|
|
m_persistentDescriptor(NULL), m_nativePath(NULL),
|
|
|
|
m_markForUnload(PR_FALSE), m_registryLocation(0)
|
1999-08-11 05:46:43 +04:00
|
|
|
|
1999-02-09 23:30:34 +03:00
|
|
|
{
|
1999-06-22 18:02:58 +04:00
|
|
|
Init(libPersistentDescriptor);
|
|
|
|
|
|
|
|
// and overwrite the modData and fileSize
|
|
|
|
m_modDate = modDate;
|
1999-02-09 23:30:34 +03:00
|
|
|
m_size = fileSize;
|
1999-06-22 18:02:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDll::Init(nsIFileSpec *dllSpec)
|
|
|
|
{
|
|
|
|
// Addref the m_dllSpec
|
|
|
|
m_dllSpec = dllSpec;
|
|
|
|
NS_ADDREF(m_dllSpec);
|
|
|
|
|
|
|
|
// Make sure we are dealing with a file
|
|
|
|
PRBool isFile = PR_FALSE;
|
1999-08-25 09:26:25 +04:00
|
|
|
nsresult rv = m_dllSpec->IsFile(&isFile);
|
1999-06-22 18:02:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
m_status = DLL_INVALID_PARAM;
|
|
|
|
return;
|
|
|
|
}
|
1999-09-01 01:40:21 +04:00
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
if (isFile == PR_FALSE)
|
|
|
|
{
|
|
|
|
// Not a file. Cant work with it.
|
|
|
|
m_status = DLL_NOT_FILE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_status = DLL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDll::Init(const char *libPersistentDescriptor)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
m_modDate = 0;
|
|
|
|
m_size = 0;
|
1999-02-09 23:30:34 +03:00
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
if (libPersistentDescriptor == NULL)
|
1999-02-09 23:30:34 +03:00
|
|
|
{
|
|
|
|
m_status = DLL_INVALID_PARAM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
// Create a FileSpec from the persistentDescriptor
|
|
|
|
nsIFileSpec *dllSpec = NULL;
|
1999-08-23 14:14:16 +04:00
|
|
|
rv = nsComponentManager::CreateInstance(NS_FILESPEC_PROGID, NULL, NS_GET_IID(nsIFileSpec), (void **) &dllSpec);
|
1999-06-22 18:02:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
m_status = DLL_INVALID_PARAM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = dllSpec->SetPersistentDescriptorString((char *)libPersistentDescriptor);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
m_status = DLL_INVALID_PARAM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Init(dllSpec);
|
1999-07-28 12:01:03 +04:00
|
|
|
NS_RELEASE(dllSpec);
|
1999-02-09 23:30:34 +03:00
|
|
|
}
|
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
|
1999-02-03 18:39:13 +03:00
|
|
|
nsDll::~nsDll(void)
|
1999-02-02 01:40:48 +03:00
|
|
|
{
|
1999-10-07 00:22:23 +04:00
|
|
|
#if defined(DEBUG_dp) || defined(DEBUG_akkana)
|
1999-09-28 23:42:06 +04:00
|
|
|
// The dll gets deleted when the dllStore is destroyed. This happens on
|
|
|
|
// app shutdown. At that point, unloading dlls can cause crashes if we have
|
|
|
|
// - dll dependencies
|
|
|
|
// - callbacks
|
|
|
|
// - static dtors
|
|
|
|
// Hence turn it back on after all the above have been removed.
|
1999-07-31 04:28:51 +04:00
|
|
|
Unload();
|
1999-09-28 23:42:06 +04:00
|
|
|
#endif
|
1999-06-22 18:02:58 +04:00
|
|
|
if (m_dllSpec)
|
|
|
|
NS_RELEASE(m_dllSpec);
|
|
|
|
if (m_dllName)
|
1999-07-31 04:28:51 +04:00
|
|
|
nsCRT::free(m_dllName);
|
1999-08-11 05:46:43 +04:00
|
|
|
if (m_persistentDescriptor)
|
|
|
|
nsCRT::free(m_persistentDescriptor);
|
|
|
|
if (m_nativePath)
|
|
|
|
nsCRT::free(m_nativePath);
|
1999-09-01 01:40:21 +04:00
|
|
|
if (m_registryLocation)
|
|
|
|
nsCRT::free(m_registryLocation);
|
1999-08-11 05:46:43 +04:00
|
|
|
|
1999-02-02 01:40:48 +03:00
|
|
|
}
|
|
|
|
|
1999-09-01 10:22:54 +04:00
|
|
|
nsresult
|
|
|
|
nsDll::Sync()
|
|
|
|
{
|
|
|
|
if (!m_dllSpec)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Populate m_modDate and m_size
|
|
|
|
nsresult rv = m_dllSpec->GetModDate(&m_modDate);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = m_dllSpec->GetFileSize(&m_size);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-06-22 18:02:58 +04:00
|
|
|
const char *
|
|
|
|
nsDll::GetNativePath()
|
|
|
|
{
|
|
|
|
if (m_dllName)
|
|
|
|
return m_dllName;
|
1999-08-11 05:46:43 +04:00
|
|
|
if (m_nativePath)
|
|
|
|
return m_nativePath;
|
|
|
|
m_dllSpec->GetNativePath(&m_nativePath);
|
|
|
|
return m_nativePath;
|
1999-06-22 18:02:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
nsDll::GetPersistentDescriptorString()
|
|
|
|
{
|
|
|
|
if (m_dllName)
|
|
|
|
return m_dllName;
|
1999-08-11 05:46:43 +04:00
|
|
|
if (m_persistentDescriptor)
|
|
|
|
return m_persistentDescriptor;
|
|
|
|
m_dllSpec->GetPersistentDescriptorString(&m_persistentDescriptor);
|
|
|
|
return m_persistentDescriptor;
|
1999-06-22 18:02:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsDll::HasChanged()
|
|
|
|
{
|
|
|
|
if (m_dllName)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
// If mod date has changed, then dll has changed
|
|
|
|
PRBool modDateChanged = PR_FALSE;
|
1999-08-25 09:26:25 +04:00
|
|
|
nsresult rv = m_dllSpec->ModDateChanged(m_modDate, &modDateChanged);
|
1999-06-22 18:02:58 +04:00
|
|
|
if (NS_FAILED(rv) || modDateChanged == PR_TRUE)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
// If size has changed, then dll has changed
|
|
|
|
PRUint32 aSize = 0;
|
|
|
|
rv = m_dllSpec->GetFileSize(&aSize);
|
|
|
|
if (NS_FAILED(rv) || aSize != m_size)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-02-18 03:24:38 +03:00
|
|
|
|
1999-02-03 18:39:13 +03:00
|
|
|
PRBool nsDll::Load(void)
|
1999-02-02 01:40:48 +03:00
|
|
|
{
|
|
|
|
if (m_status != DLL_OK)
|
|
|
|
{
|
|
|
|
return (PR_FALSE);
|
|
|
|
}
|
|
|
|
if (m_instance != NULL)
|
|
|
|
{
|
|
|
|
// Already loaded
|
|
|
|
return (PR_TRUE);
|
|
|
|
}
|
1999-06-22 18:02:58 +04:00
|
|
|
|
|
|
|
if (m_dllName)
|
|
|
|
{
|
|
|
|
m_instance = PR_LoadLibrary(m_dllName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-10-06 05:09:13 +04:00
|
|
|
#ifndef XP_MAC
|
1999-06-22 18:02:58 +04:00
|
|
|
char *nsprPath = NULL;
|
|
|
|
nsresult rv = m_dllSpec->GetNSPRPath(&nsprPath);
|
|
|
|
if (NS_FAILED(rv)) return PR_FALSE;
|
|
|
|
|
|
|
|
m_instance = PR_LoadLibrary(nsprPath);
|
1999-10-06 05:09:13 +04:00
|
|
|
|
1999-07-09 07:43:29 +04:00
|
|
|
nsCRT::free(nsprPath);
|
1999-10-06 05:09:13 +04:00
|
|
|
#else
|
|
|
|
|
|
|
|
nsFileSpec fileSpec;
|
|
|
|
m_dllSpec->GetFileSpec(&fileSpec);
|
|
|
|
FSSpec libSpec = fileSpec.GetFSSpec();
|
|
|
|
m_instance = PR_LoadIndexedFragment(&libSpec, 0);
|
|
|
|
|
|
|
|
#endif /* XP_MAC */
|
1999-06-22 18:02:58 +04:00
|
|
|
}
|
|
|
|
return ((m_instance == NULL) ? PR_FALSE : PR_TRUE);
|
1999-02-02 01:40:48 +03:00
|
|
|
}
|
|
|
|
|
1999-02-03 18:39:13 +03:00
|
|
|
PRBool nsDll::Unload(void)
|
1999-02-02 01:40:48 +03:00
|
|
|
{
|
|
|
|
if (m_status != DLL_OK || m_instance == NULL)
|
|
|
|
return (PR_FALSE);
|
1999-08-09 04:19:08 +04:00
|
|
|
|
1999-09-26 22:06:41 +04:00
|
|
|
// Shutdown the dll
|
|
|
|
Shutdown();
|
1999-08-09 04:19:08 +04:00
|
|
|
|
1999-02-02 01:40:48 +03:00
|
|
|
PRStatus ret = PR_UnloadLibrary(m_instance);
|
|
|
|
if (ret == PR_SUCCESS)
|
|
|
|
{
|
|
|
|
m_instance = NULL;
|
|
|
|
return (PR_TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (PR_FALSE);
|
|
|
|
}
|
|
|
|
|
1999-02-03 18:39:13 +03:00
|
|
|
void * nsDll::FindSymbol(const char *symbol)
|
1999-02-02 01:40:48 +03:00
|
|
|
{
|
|
|
|
if (symbol == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
// If not already loaded, load it now.
|
|
|
|
if (Load() != PR_TRUE)
|
|
|
|
return (NULL);
|
|
|
|
|
1999-09-01 01:40:21 +04:00
|
|
|
return(PR_FindSymbol(m_instance, symbol));
|
1999-02-02 01:40:48 +03:00
|
|
|
}
|
1999-08-09 04:19:08 +04:00
|
|
|
|
|
|
|
|
|
|
|
// Component dll specific functions
|
|
|
|
nsresult nsDll::GetDllSpec(nsIFileSpec **fsobj)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(m_dllSpec, "m_dllSpec NULL");
|
|
|
|
NS_ASSERTION(fsobj, "xcDll::GetModule : Null argument" );
|
|
|
|
|
|
|
|
NS_ADDREF(m_dllSpec);
|
|
|
|
*fsobj = m_dllSpec;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDll::GetModule(nsISupports *servMgr, nsIModule **cobj)
|
|
|
|
{
|
1999-08-13 23:27:58 +04:00
|
|
|
nsIComponentManager *compMgr;
|
|
|
|
nsresult rv = NS_GetGlobalComponentManager(&compMgr);
|
|
|
|
NS_ASSERTION(compMgr, "Global Component Manager is null" );
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-08-09 04:19:08 +04:00
|
|
|
NS_ASSERTION(cobj, "xcDll::GetModule : Null argument" );
|
|
|
|
|
|
|
|
if (m_moduleObject)
|
|
|
|
{
|
|
|
|
NS_ADDREF(m_moduleObject);
|
|
|
|
*cobj = m_moduleObject;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not already loaded, load it now.
|
|
|
|
if (Load() != PR_TRUE) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// We need a nsIFileSpec for location. If we dont
|
|
|
|
// have one, create one.
|
|
|
|
if (m_dllSpec == NULL && m_dllName)
|
|
|
|
{
|
|
|
|
// Create m_dllSpec from m_dllName
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGetModuleProc proc =
|
|
|
|
(nsGetModuleProc) FindSymbol(NS_GET_MODULE_SYMBOL);
|
|
|
|
|
1999-09-01 01:40:21 +04:00
|
|
|
if (proc == NULL)
|
|
|
|
return NS_ERROR_FACTORY_NOT_LOADED;
|
1999-08-09 04:19:08 +04:00
|
|
|
|
1999-08-13 23:27:58 +04:00
|
|
|
rv = (*proc) (compMgr, m_dllSpec, &m_moduleObject);
|
1999-08-09 04:19:08 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
NS_ADDREF(m_moduleObject);
|
|
|
|
*cobj = m_moduleObject;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
1999-09-26 22:06:41 +04:00
|
|
|
|
|
|
|
nsresult nsDll::Shutdown(void)
|
|
|
|
{
|
|
|
|
// Release the module object if we got one
|
|
|
|
nsrefcnt refcnt;
|
|
|
|
if (m_moduleObject)
|
|
|
|
{
|
|
|
|
NS_RELEASE2(m_moduleObject, refcnt);
|
|
|
|
NS_ASSERTION(refcnt == 0, "Dll moduleObject refcount non zero");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|