1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org Code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK *****
37 * This Original Code has been modified by IBM Corporation.
38 * Modifications made by IBM described herein are
39 * Copyright (c) International Business Machines
42 * Modifications to Mozilla code or documentation
43 * identified per MPL Section 3.3
45 * Date Modified by Description of modification
46 * 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
49 /* Allow logging in the release build */
54 #include "nsNativeComponentLoader.h"
60 #include "nsComponentManager.h"
61 #include "nsCRTGlue.h"
62 #include "nsThreadUtils.h"
63 #include "nsTraceRefcntImpl.h"
65 #include "nsILocalFile.h"
66 #include "nsIProxyObjectManager.h"
77 #include <lib$routines.h>
82 #define IMPLEMENT_BREAK_AFTER_LOAD
85 static PRLogModuleInfo
*nsNativeModuleLoaderLog
=
86 PR_NewLogModule("nsNativeModuleLoader");
88 #define LOG(level, args) PR_LOG(nsNativeModuleLoaderLog, level, args)
90 NS_IMPL_QUERY_INTERFACE1(nsNativeModuleLoader
,
91 mozilla::ModuleLoader
)
93 NS_IMPL_ADDREF_USING_AGGREGATOR(nsNativeModuleLoader
,
94 nsComponentManagerImpl::gComponentManager
)
95 NS_IMPL_RELEASE_USING_AGGREGATOR(nsNativeModuleLoader
,
96 nsComponentManagerImpl::gComponentManager
)
99 nsNativeModuleLoader::Init()
101 NS_ASSERTION(NS_IsMainThread(), "Startup not on main thread?");
103 LOG(PR_LOG_DEBUG
, ("nsNativeModuleLoader::Init()"));
105 return mLibraries
.Init() ? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
108 class LoadModuleMainThreadRunnable
: public nsRunnable
111 LoadModuleMainThreadRunnable(nsNativeModuleLoader
* loader
,
120 mResult
= mLoader
->LoadModule(mFile
);
124 nsRefPtr
<nsNativeModuleLoader
> mLoader
;
125 nsCOMPtr
<nsILocalFile
> mFile
;
126 const mozilla::Module
* mResult
;
129 const mozilla::Module
*
130 nsNativeModuleLoader::LoadModule(nsILocalFile
* aFile
)
134 if (!NS_IsMainThread()) {
135 // If this call is off the main thread, synchronously proxy it
136 // to the main thread.
137 nsRefPtr
<LoadModuleMainThreadRunnable
> r
= new LoadModuleMainThreadRunnable(this, aFile
);
138 NS_DispatchToMainThread(r
, NS_DISPATCH_SYNC
);
142 nsCOMPtr
<nsIHashable
> hashedFile(do_QueryInterface(aFile
));
144 NS_ERROR("nsIFile is not nsIHashable");
148 nsCAutoString filePath
;
149 aFile
->GetNativePath(filePath
);
153 if (mLibraries
.Get(hashedFile
, &data
)) {
154 NS_ASSERTION(data
.module
, "Corrupt mLibraries hash");
156 ("nsNativeModuleLoader::LoadModule(\"%s\") - found in cache",
161 // We haven't loaded this module before
163 rv
= aFile
->Load(&data
.library
);
166 char errorMsg
[1024] = "<unknown; can't get error from NSPR>";
168 if (PR_GetErrorTextLength() < (int) sizeof(errorMsg
))
169 PR_GetErrorText(errorMsg
);
172 ("nsNativeModuleLoader::LoadModule(\"%s\") - load FAILED, "
173 "rv: %lx, error:\n\t%s\n",
174 filePath
.get(), rv
, errorMsg
));
178 "nsNativeModuleLoader::LoadModule(\"%s\") - load FAILED, "
179 "rv: %lx, error:\n\t%s\n",
180 filePath
.get(), (unsigned long)rv
, errorMsg
);
186 #ifdef IMPLEMENT_BREAK_AFTER_LOAD
187 nsCAutoString leafName
;
188 aFile
->GetNativeLeafName(leafName
);
190 char *env
= getenv("XPCOM_BREAK_ON_LOAD");
192 if (env
&& *env
&& (blist
= strdup(env
))) {
193 char *nextTok
= blist
;
194 while (char *token
= NS_strtok(":", &nextTok
)) {
195 if (leafName
.Find(token
, PR_TRUE
) != kNotFound
) {
204 void *module
= PR_FindSymbol(data
.library
, "NSModule");
206 data
.module
= *(mozilla::Module
const *const *) module
;
207 if (mLibraries
.Put(hashedFile
, data
))
212 ("nsNativeModuleLoader::LoadModule(\"%s\") - "
213 "Symbol NSModule not found", filePath
.get()));
216 // at some point we failed, clean up
217 data
.module
= nsnull
;
218 PR_UnloadLibrary(data
.library
);
223 const mozilla::Module
*
224 nsNativeModuleLoader::LoadModuleFromJAR(nsILocalFile
* aJARFile
, const nsACString
&aPath
)
226 NS_ERROR("Binary components cannot be loaded from JARs");
231 nsNativeModuleLoader::ReleaserFunc(nsIHashable
* aHashedFile
,
232 NativeLoadData
& aLoadData
, void*)
234 aLoadData
.module
= nsnull
;
235 return PL_DHASH_NEXT
;
239 nsNativeModuleLoader::UnloaderFunc(nsIHashable
* aHashedFile
,
240 NativeLoadData
& aLoadData
, void*)
242 if (PR_LOG_TEST(nsNativeModuleLoaderLog
, PR_LOG_DEBUG
)) {
243 nsCOMPtr
<nsIFile
> file(do_QueryInterface(aHashedFile
));
245 nsCAutoString filePath
;
246 file
->GetNativePath(filePath
);
249 ("nsNativeModuleLoader::UnloaderFunc(\"%s\")", filePath
.get()));
252 #ifdef NS_BUILD_REFCNT_LOGGING
253 nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE
);
257 // XXXbsmedberg: do this as soon as the static-destructor crash(es)
259 PRStatus ret
= PR_UnloadLibrary(aLoadData
.library
);
260 NS_ASSERTION(ret
== PR_SUCCESS
, "Failed to unload library");
263 #ifdef NS_BUILD_REFCNT_LOGGING
264 nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE
);
267 return PL_DHASH_REMOVE
;
271 nsNativeModuleLoader::UnloadLibraries()
273 NS_ASSERTION(NS_IsMainThread(), "Shutdown not on main thread?");
275 mLibraries
.Enumerate(ReleaserFunc
, nsnull
);
276 mLibraries
.Enumerate(UnloaderFunc
, nsnull
);