bug 313956: expand installer .exe contents to make complete mar. r=ted.
[gecko.git] / xpcom / components / nsNativeComponentLoader.cpp
blobd7f8fee3ae4195ae5d9b557b2917e26915bc681c
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
13 * License.
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.
22 * Contributor(s):
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
40 * Corporation, 2000
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 */
50 #ifdef MOZ_LOGGING
51 #define FORCE_PR_LOG
52 #endif
54 #include "nsNativeComponentLoader.h"
56 #include "prlog.h"
57 #include "prinit.h"
58 #include "prerror.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"
68 #ifdef XP_WIN
69 #include <windows.h>
70 #endif
72 #ifdef XP_MACOSX
73 #include <signal.h>
74 #endif
76 #ifdef VMS
77 #include <lib$routines.h>
78 #include <ssdef.h>
79 #endif
81 #ifdef DEBUG
82 #define IMPLEMENT_BREAK_AFTER_LOAD
83 #endif
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)
98 nsresult
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
110 public:
111 LoadModuleMainThreadRunnable(nsNativeModuleLoader* loader,
112 nsILocalFile* file)
113 : mLoader(loader)
114 , mFile(file)
115 , mResult(NULL)
118 NS_IMETHOD Run()
120 mResult = mLoader->LoadModule(mFile);
121 return NS_OK;
124 nsRefPtr<nsNativeModuleLoader> mLoader;
125 nsCOMPtr<nsILocalFile> mFile;
126 const mozilla::Module* mResult;
129 const mozilla::Module*
130 nsNativeModuleLoader::LoadModule(nsILocalFile* aFile)
132 nsresult rv;
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);
139 return r->mResult;
142 nsCOMPtr<nsIHashable> hashedFile(do_QueryInterface(aFile));
143 if (!hashedFile) {
144 NS_ERROR("nsIFile is not nsIHashable");
145 return NULL;
148 nsCAutoString filePath;
149 aFile->GetNativePath(filePath);
151 NativeLoadData data;
153 if (mLibraries.Get(hashedFile, &data)) {
154 NS_ASSERTION(data.module, "Corrupt mLibraries hash");
155 LOG(PR_LOG_DEBUG,
156 ("nsNativeModuleLoader::LoadModule(\"%s\") - found in cache",
157 filePath.get()));
158 return data.module;
161 // We haven't loaded this module before
163 rv = aFile->Load(&data.library);
165 if (NS_FAILED(rv)) {
166 char errorMsg[1024] = "<unknown; can't get error from NSPR>";
168 if (PR_GetErrorTextLength() < (int) sizeof(errorMsg))
169 PR_GetErrorText(errorMsg);
171 LOG(PR_LOG_ERROR,
172 ("nsNativeModuleLoader::LoadModule(\"%s\") - load FAILED, "
173 "rv: %lx, error:\n\t%s\n",
174 filePath.get(), rv, errorMsg));
176 #ifdef DEBUG
177 fprintf(stderr,
178 "nsNativeModuleLoader::LoadModule(\"%s\") - load FAILED, "
179 "rv: %lx, error:\n\t%s\n",
180 filePath.get(), (unsigned long)rv, errorMsg);
181 #endif
183 return NULL;
186 #ifdef IMPLEMENT_BREAK_AFTER_LOAD
187 nsCAutoString leafName;
188 aFile->GetNativeLeafName(leafName);
190 char *env = getenv("XPCOM_BREAK_ON_LOAD");
191 char *blist;
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) {
196 NS_BREAK();
200 free(blist);
202 #endif
204 void *module = PR_FindSymbol(data.library, "NSModule");
205 if (module) {
206 data.module = *(mozilla::Module const *const *) module;
207 if (mLibraries.Put(hashedFile, data))
208 return data.module;
210 else {
211 LOG(PR_LOG_ERROR,
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);
220 return NULL;
223 const mozilla::Module*
224 nsNativeModuleLoader::LoadModuleFromJAR(nsILocalFile* aJARFile, const nsACString &aPath)
226 NS_ERROR("Binary components cannot be loaded from JARs");
227 return NULL;
230 PLDHashOperator
231 nsNativeModuleLoader::ReleaserFunc(nsIHashable* aHashedFile,
232 NativeLoadData& aLoadData, void*)
234 aLoadData.module = nsnull;
235 return PL_DHASH_NEXT;
238 PLDHashOperator
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);
248 LOG(PR_LOG_DEBUG,
249 ("nsNativeModuleLoader::UnloaderFunc(\"%s\")", filePath.get()));
252 #ifdef NS_BUILD_REFCNT_LOGGING
253 nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
254 #endif
256 #if 0
257 // XXXbsmedberg: do this as soon as the static-destructor crash(es)
258 // are fixed
259 PRStatus ret = PR_UnloadLibrary(aLoadData.library);
260 NS_ASSERTION(ret == PR_SUCCESS, "Failed to unload library");
261 #endif
263 #ifdef NS_BUILD_REFCNT_LOGGING
264 nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
265 #endif
267 return PL_DHASH_REMOVE;
270 void
271 nsNativeModuleLoader::UnloadLibraries()
273 NS_ASSERTION(NS_IsMainThread(), "Shutdown not on main thread?");
275 mLibraries.Enumerate(ReleaserFunc, nsnull);
276 mLibraries.Enumerate(UnloaderFunc, nsnull);