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