VirtualBox

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

最後變更 在這個檔案從102334是 102334,由 vboxsync 提交於 14 月 前

libs/xpcom: Drop VBOX_USE_IPRT_IN_XPCOM andmake the code the default as running without IPRT is impossible anyways, bugref:10545

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

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