VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/components/xcDll.cpp@ 109065

Last change on this file since 109065 was 109065, checked in by vboxsync, 4 weeks ago

VBox/log.h,libs/xpcom: Added an XPCOM logging group and fixed missing newlines (++) in the logging in xpcom.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
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
4 *
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/
9 *
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.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/* nsDll
39 *
40 * Abstraction of a Dll. Stores modifiedTime and size for easy detection of
41 * change in dll.
42 *
43 * dp Suresh <dp@netscape.com>
44 */
45
46#include "xcDll.h"
47#include "nsDebug.h"
48#include "nsIComponentManager.h"
49#include "nsIComponentLoaderManager.h"
50#include "nsIModule.h"
51#include "nsILocalFile.h"
52#include "nsDirectoryServiceDefs.h"
53#include "nsDirectoryServiceUtils.h"
54#include "nsCOMPtr.h"
55#include "nsCRT.h"
56#include "nsString.h"
57#include "nsModule.h"
58#ifdef DEBUG
59#if defined(XP_MACOSX)
60#include <signal.h>
61#endif
62#endif /* defined(DEBUG) */
63
64#include "nsTraceRefcntImpl.h"
65
66#include "nsNativeComponentLoader.h"
67#include "nsMemory.h"
68#include <VBox/log.h>
69
70
71nsDll::nsDll(nsIFile *dllSpec, nsNativeComponentLoader *loader)
72 : m_dllSpec(do_QueryInterface(dllSpec)),
73 m_hMod(NIL_RTLDRMOD),
74 m_moduleObject(NULL),
75 m_loader(loader),
76 m_markForUnload(PR_FALSE)
77{
78 NS_ASSERTION(loader, "Null loader when creating a nsDLL");
79}
80
81nsDll::~nsDll(void)
82{
83 /** @todo r=aeichner Does this need fixing at all? */
84 //#if DEBUG_dougt
85 // The dll gets deleted when the dllStore is destroyed. This happens on
86 // app shutdown. At that point, unloading dlls can cause crashes if we have
87 // - dll dependencies
88 // - callbacks
89 // - static dtors
90 // Hence turn it back on after all the above have been removed.
91#ifdef VBOX_WITH_GCC_SANITIZER
92 /* Although this looks like it's what we should be doing here, we don't want to enable this for release builds (yet).
93 * Needs more testing and/or debugging first. See @bugref{10545c6}. */
94 Unload();
95#endif
96 //#endif
97}
98
99void
100nsDll::GetDisplayPath(nsACString& aLeafName)
101{
102 m_dllSpec->GetNativeLeafName(aLeafName);
103
104 if (aLeafName.IsEmpty())
105 aLeafName.AssignLiteral("unknown!");
106}
107
108PRBool
109nsDll::HasChanged()
110{
111 nsCOMPtr<nsIComponentLoaderManager> manager = do_QueryInterface(m_loader->mCompMgr);
112 if (!manager)
113 return PR_TRUE;
114
115 // If mod date has changed, then dll has changed
116 PRInt64 currentDate;
117 nsresult rv = m_dllSpec->GetLastModifiedTime(&currentDate);
118 if (NS_FAILED(rv))
119 return PR_TRUE;
120 Log2(("nsDll::HasChanged: currentDate=%RI64\n", currentDate));
121 PRBool changed = PR_TRUE;
122 manager->HasFileChanged(m_dllSpec, nsnull, currentDate, &changed);
123 return changed;
124}
125
126PRBool nsDll::Load(void)
127{
128 /* Already loaded? Nothing to do. */
129 if (m_hMod != NIL_RTLDRMOD)
130 return PR_TRUE;
131
132 if (m_dllSpec)
133 {
134#ifdef NS_BUILD_REFCNT_LOGGING
135 nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
136#endif
137
138 // Load any library dependencies
139 // The Component Loader Manager may hold onto some extra data
140 // set by either the native component loader or the native
141 // component. We assume that this data is a space delimited
142 // listing of dependent libraries which are required to be
143 // loaded prior to us loading the given component. Once, the
144 // component is loaded into memory, we can release our hold
145 // on the dependent libraries with the assumption that the
146 // component library holds a reference via the OS so loader.
147 nsCOMPtr<nsIComponentLoaderManager> manager = do_QueryInterface(m_loader->mCompMgr);
148 if (!manager)
149 return PR_TRUE;
150
151 nsXPIDLCString extraData;
152 manager->GetOptionalData(m_dllSpec, nsnull, getter_Copies(extraData));
153
154 nsVoidArray dependentLibArray;
155
156 // if there was any extra data, treat it as a listing of dependent libs
157 if (extraData != nsnull)
158 {
159 // all dependent libraries are suppose to be in the "gre" directory.
160 // note that the gre directory is the same as the "bin" directory,
161 // when there isn't a real "gre" found.
162
163 nsXPIDLCString path;
164 nsCOMPtr<nsIFile> file;
165 NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(file));
166
167 if (!file)
168 return NS_ERROR_FAILURE;
169
170 // we are talking about a file in the GRE dir. Lets append something
171 // stupid right now, so that later we can just set the leaf name.
172 file->AppendNative(NS_LITERAL_CSTRING("dummy"));
173
174 char *buffer = (char *)nsMemory::Clone(extraData, strlen(extraData) + 1);
175 if (!buffer)
176 return NS_ERROR_OUT_OF_MEMORY;
177
178 char* newStr;
179 char *token = nsCRT::strtok(buffer, " ", &newStr);
180 while (token!=nsnull)
181 {
182 nsCStringKey key(token);
183 if (m_loader->mLoadedDependentLibs.Get(&key)) {
184 token = nsCRT::strtok(newStr, " ", &newStr);
185 continue;
186 }
187
188 m_loader->mLoadedDependentLibs.Put(&key, (void*)1);
189
190 nsXPIDLCString libpath;
191 file->SetNativeLeafName(nsDependentCString(token));
192 file->GetNativePath(path);
193 if (!path)
194 return NS_ERROR_FAILURE;
195
196 // Load this dependent library with the global flag and stash
197 // the result for later so that we can unload it.
198 const char *pszFilename = NULL;
199
200 // if the depend library path starts with a / we are
201 // going to assume that it is a full path and should
202 // be loaded without prepending the gre diretory
203 // location. We could have short circuited the
204 // SetNativeLeafName above, but this is clearer and
205 // the common case is a relative path.
206 if (token[0] == '/')
207 pszFilename = token;
208 else
209 pszFilename = path;
210
211 RTLDRMOD hMod = NIL_RTLDRMOD;
212 RTERRINFOSTATIC ErrInfo;
213 RTErrInfoInitStatic(&ErrInfo);
214
215 int vrc = RTLdrLoadEx(pszFilename, &hMod, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
216 // if we couldn't load the dependent library. We did the best we
217 // can. Now just let us fail later if this really was a required
218 // dependency.
219 if (RT_SUCCESS(vrc))
220 dependentLibArray.AppendElement((void*)hMod);
221
222 token = nsCRT::strtok(newStr, " ", &newStr);
223 }
224 nsMemory::Free(buffer);
225 }
226
227 // load the component
228 nsCOMPtr<nsILocalFile> lf(do_QueryInterface(m_dllSpec));
229 NS_ASSERTION(lf, "nsIFile here must implement a nsILocalFile");
230 lf->Load(&m_hMod);
231
232 // Unload any of library dependencies we loaded earlier. The assumption
233 // here is that the component will have a "internal" reference count to
234 // the dependency library we just loaded.
235 // XXX should we unload later - or even at all?
236 if (extraData != nsnull)
237 {
238 PRInt32 arrayCount = dependentLibArray.Count();
239 for (PRInt32 index = 0; index < arrayCount; index++)
240 RTLdrClose((RTLDRMOD)dependentLibArray.ElementAt(index));
241 }
242
243#ifdef NS_BUILD_REFCNT_LOGGING
244 nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
245 if (m_hMod != NIL_RTLDRMOD)
246 {
247 // Inform refcnt tracer of new library so that calls through the
248 // new library can be traced.
249 nsXPIDLCString displayPath;
250 GetDisplayPath(displayPath);
251 nsTraceRefcntImpl::LoadLibrarySymbols(displayPath.get(), m_hMod);
252 }
253#endif
254 }
255
256 return ((m_hMod == NIL_RTLDRMOD) ? PR_FALSE : PR_TRUE);
257}
258
259PRBool nsDll::Unload(void)
260{
261 if (m_hMod == NULL)
262 return PR_FALSE;
263
264 // Shutdown the dll
265 Shutdown();
266
267#ifdef NS_BUILD_REFCNT_LOGGING
268 nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
269#endif
270 int vrc = RTLdrClose(m_hMod);
271#ifdef NS_BUILD_REFCNT_LOGGING
272 nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
273#endif
274
275 if (RT_SUCCESS(vrc))
276 {
277 m_hMod = NIL_RTLDRMOD;
278 return PR_TRUE;
279 }
280
281 return PR_FALSE;
282}
283
284void * nsDll::FindSymbol(const char *symbol)
285{
286 if (symbol == NULL)
287 return NULL;
288
289 // If not already loaded, load it now.
290 if (Load() != PR_TRUE)
291 return NULL;
292
293 void *pvSym = NULL;
294 int vrc = RTLdrGetSymbol(m_hMod, symbol, &pvSym);
295 RT_NOREF(vrc);
296
297 return pvSym;
298}
299
300
301// Component dll specific functions
302nsresult nsDll::GetDllSpec(nsIFile **fsobj)
303{
304 NS_ASSERTION(m_dllSpec, "m_dllSpec NULL");
305 NS_ASSERTION(fsobj, "xcDll::GetModule : Null argument" );
306
307 *fsobj = m_dllSpec;
308 NS_ADDREF(*fsobj);
309 return NS_OK;
310}
311
312nsresult nsDll::GetModule(nsISupports *servMgr, nsIModule **cobj)
313{
314 // using the backpointer of the loader.
315 nsIComponentManager* compMgr = m_loader->mCompMgr;
316 NS_ASSERTION(compMgr, "Global Component Manager is null" );
317 if (!compMgr) return NS_ERROR_UNEXPECTED;
318
319 NS_ASSERTION(cobj, "xcDll::GetModule : Null argument" );
320
321 if (m_moduleObject)
322 {
323 NS_ADDREF(m_moduleObject);
324 *cobj = m_moduleObject;
325 return NS_OK;
326 }
327
328 // If not already loaded, load it now.
329 if (Load() != PR_TRUE) return NS_ERROR_FAILURE;
330
331 // We need a nsIFile for location
332 if (!m_dllSpec)
333 {
334 return NS_ERROR_FAILURE;
335 }
336
337 nsGetModuleProc proc =
338 (nsGetModuleProc) FindSymbol(NS_GET_MODULE_SYMBOL);
339
340 if (proc == NULL)
341 return NS_ERROR_FACTORY_NOT_LOADED;
342
343 nsresult rv = (*proc) (compMgr, m_dllSpec, &m_moduleObject);
344 if (NS_SUCCEEDED(rv))
345 {
346 NS_ADDREF(m_moduleObject);
347 *cobj = m_moduleObject;
348 }
349 return rv;
350}
351
352nsresult nsDll::Shutdown(void)
353{
354 // Release the module object if we got one
355 nsrefcnt refcnt;
356 if (m_moduleObject)
357 {
358 NS_RELEASE2(m_moduleObject, refcnt);
359 NS_ASSERTION(refcnt == 0, "Dll moduleObject refcount non zero");
360 }
361
362 return NS_OK;
363
364}
365
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette