VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/glue/standalone/nsXPCOMGlue.cpp@ 3049

Last change on this file since 3049 was 3049, checked in by vboxsync, 18 years ago

fixed another putenv problem found by valgrind

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* vim:set ts=4 sw=4 et cindent: */
3/* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#include "nsXPCOMGlue.h"
40
41#include "nspr.h"
42#include "nsMemory.h"
43#include "nsGREDirServiceProvider.h"
44#include "nsXPCOMPrivate.h"
45#include <stdlib.h>
46
47#if XP_WIN32
48#include <windows.h>
49#endif
50
51void GRE_AddGREToEnvironment();
52
53// functions provided by nsMemory.cpp and nsDebug.cpp
54nsresult GlueStartupMemory();
55void GlueShutdownMemory();
56nsresult GlueStartupDebug();
57void GlueShutdownDebug();
58
59static PRLibrary *xpcomLib;
60static XPCOMFunctions xpcomFunctions;
61
62extern "C"
63nsresult XPCOMGlueStartup(const char* xpcomFile)
64{
65#ifdef XPCOM_GLUE_NO_DYNAMIC_LOADING
66 return NS_OK;
67#else
68 nsresult rv = NS_OK;
69 GetFrozenFunctionsFunc function = nsnull;
70
71 xpcomFunctions.version = XPCOM_GLUE_VERSION;
72 xpcomFunctions.size = sizeof(XPCOMFunctions);
73
74 //
75 // if xpcomFile == ".", then we assume xpcom is already loaded, and we'll
76 // use NSPR to find NS_GetFrozenFunctions from the list of already loaded
77 // libraries.
78 //
79 // otherwise, we try to load xpcom and then look for NS_GetFrozenFunctions.
80 // if xpcomFile == NULL, then we try to load xpcom by name w/o a fully
81 // qualified path.
82 //
83
84 if (xpcomFile && (xpcomFile[0] == '.' && xpcomFile[1] == '\0')) {
85 function = (GetFrozenFunctionsFunc)
86 PR_FindSymbolAndLibrary("NS_GetFrozenFunctions", &xpcomLib);
87 if (!function) {
88 // The symbol was not found, so failover to loading XPCOM_DLL,
89 // and look for the symbol there. See bug 240986 for details.
90 xpcomFile = nsnull;
91 }
92 else {
93 char *libPath = PR_GetLibraryFilePathname(XPCOM_DLL, (PRFuncPtr) function);
94 if (!libPath)
95 rv = NS_ERROR_FAILURE;
96 else {
97 rv = (*function)(&xpcomFunctions, libPath);
98 PR_Free(libPath);
99 }
100 }
101 }
102
103 if (!function) {
104 PRLibSpec libSpec;
105
106 libSpec.type = PR_LibSpec_Pathname;
107 if (!xpcomFile)
108 libSpec.value.pathname = XPCOM_DLL;
109 else
110 libSpec.value.pathname = xpcomFile;
111
112 xpcomLib = PR_LoadLibraryWithFlags(libSpec, PR_LD_LAZY|PR_LD_GLOBAL);
113#ifdef __DARWIN__
114 /* works around bundle problem. */
115 if (!xpcomLib) {
116 const char *home = PR_GetEnv("VBOX_XPCOM_HOME");
117 if (home) {
118 char path[PATH_MAX];
119 snprintf(path, sizeof(path), "%s/%s", home, libSpec.value.pathname);
120 libSpec.value.pathname = path;
121 xpcomLib = PR_LoadLibraryWithFlags(libSpec, PR_LD_LAZY|PR_LD_GLOBAL);
122 }
123 }
124#endif
125 if (!xpcomLib)
126 return NS_ERROR_FAILURE;
127
128 function = (GetFrozenFunctionsFunc) PR_FindSymbol(xpcomLib, "NS_GetFrozenFunctions");
129
130 if (!function)
131 rv = NS_ERROR_FAILURE;
132 else
133 rv = (*function)(&xpcomFunctions, libSpec.value.pathname);
134 }
135
136 if (NS_FAILED(rv))
137 goto bail;
138
139 rv = GlueStartupDebug();
140 if (NS_FAILED(rv))
141 goto bail;
142
143 // startup the nsMemory
144 rv = GlueStartupMemory();
145 if (NS_FAILED(rv)) {
146 GlueShutdownDebug();
147 goto bail;
148 }
149
150 GRE_AddGREToEnvironment();
151 return NS_OK;
152
153bail:
154 PR_UnloadLibrary(xpcomLib);
155 xpcomLib = nsnull;
156 memset(&xpcomFunctions, 0, sizeof(xpcomFunctions));
157 return NS_ERROR_FAILURE;
158#endif
159}
160
161extern "C"
162nsresult XPCOMGlueShutdown()
163{
164#ifdef XPCOM_GLUE_NO_DYNAMIC_LOADING
165 return NS_OK;
166#else
167
168 GlueShutdownMemory();
169
170 GlueShutdownDebug();
171
172 if (xpcomLib) {
173 PR_UnloadLibrary(xpcomLib);
174 xpcomLib = nsnull;
175 }
176
177 memset(&xpcomFunctions, 0, sizeof(xpcomFunctions));
178 return NS_OK;
179#endif
180}
181
182#ifndef XPCOM_GLUE_NO_DYNAMIC_LOADING
183extern "C" NS_COM nsresult
184NS_InitXPCOM2(nsIServiceManager* *result,
185 nsIFile* binDirectory,
186 nsIDirectoryServiceProvider* appFileLocationProvider)
187{
188 if (!xpcomFunctions.init)
189 return NS_ERROR_NOT_INITIALIZED;
190 return xpcomFunctions.init(result, binDirectory, appFileLocationProvider);
191}
192
193extern "C" NS_COM nsresult
194NS_ShutdownXPCOM(nsIServiceManager* servMgr)
195{
196 if (!xpcomFunctions.shutdown)
197 return NS_ERROR_NOT_INITIALIZED;
198 return xpcomFunctions.shutdown(servMgr);
199}
200
201extern "C" NS_COM nsresult
202NS_GetServiceManager(nsIServiceManager* *result)
203{
204 if (!xpcomFunctions.getServiceManager)
205 return NS_ERROR_NOT_INITIALIZED;
206 return xpcomFunctions.getServiceManager(result);
207}
208
209extern "C" NS_COM nsresult
210NS_GetComponentManager(nsIComponentManager* *result)
211{
212 if (!xpcomFunctions.getComponentManager)
213 return NS_ERROR_NOT_INITIALIZED;
214 return xpcomFunctions.getComponentManager(result);
215}
216
217extern "C" NS_COM nsresult
218NS_GetComponentRegistrar(nsIComponentRegistrar* *result)
219{
220 if (!xpcomFunctions.getComponentRegistrar)
221 return NS_ERROR_NOT_INITIALIZED;
222 return xpcomFunctions.getComponentRegistrar(result);
223}
224
225extern "C" NS_COM nsresult
226NS_GetMemoryManager(nsIMemory* *result)
227{
228 if (!xpcomFunctions.getMemoryManager)
229 return NS_ERROR_NOT_INITIALIZED;
230 return xpcomFunctions.getMemoryManager(result);
231}
232
233extern "C" NS_COM nsresult
234NS_NewLocalFile(const nsAString &path, PRBool followLinks, nsILocalFile* *result)
235{
236 if (!xpcomFunctions.newLocalFile)
237 return NS_ERROR_NOT_INITIALIZED;
238 return xpcomFunctions.newLocalFile(path, followLinks, result);
239}
240
241extern "C" NS_COM nsresult
242NS_NewNativeLocalFile(const nsACString &path, PRBool followLinks, nsILocalFile* *result)
243{
244 if (!xpcomFunctions.newNativeLocalFile)
245 return NS_ERROR_NOT_INITIALIZED;
246 return xpcomFunctions.newNativeLocalFile(path, followLinks, result);
247}
248
249extern "C" NS_COM nsresult
250NS_RegisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine, PRUint32 priority)
251{
252 if (!xpcomFunctions.registerExitRoutine)
253 return NS_ERROR_NOT_INITIALIZED;
254 return xpcomFunctions.registerExitRoutine(exitRoutine, priority);
255}
256
257extern "C" NS_COM nsresult
258NS_UnregisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine)
259{
260 if (!xpcomFunctions.unregisterExitRoutine)
261 return NS_ERROR_NOT_INITIALIZED;
262 return xpcomFunctions.unregisterExitRoutine(exitRoutine);
263}
264
265extern "C" NS_COM nsresult
266NS_GetDebug(nsIDebug* *result)
267{
268 if (!xpcomFunctions.getDebug)
269 return NS_ERROR_NOT_INITIALIZED;
270 return xpcomFunctions.getDebug(result);
271}
272
273
274extern "C" NS_COM nsresult
275NS_GetTraceRefcnt(nsITraceRefcnt* *result)
276{
277 if (!xpcomFunctions.getTraceRefcnt)
278 return NS_ERROR_NOT_INITIALIZED;
279 return xpcomFunctions.getTraceRefcnt(result);
280}
281
282
283extern "C" NS_COM nsresult
284NS_StringContainerInit(nsStringContainer &aStr)
285{
286 if (!xpcomFunctions.stringContainerInit)
287 return NS_ERROR_NOT_INITIALIZED;
288 return xpcomFunctions.stringContainerInit(aStr);
289}
290
291extern "C" NS_COM void
292NS_StringContainerFinish(nsStringContainer &aStr)
293{
294 if (xpcomFunctions.stringContainerFinish)
295 xpcomFunctions.stringContainerFinish(aStr);
296}
297
298extern "C" NS_COM PRUint32
299NS_StringGetData(const nsAString &aStr, const PRUnichar **aBuf, PRBool *aTerm)
300{
301 if (!xpcomFunctions.stringGetData) {
302 *aBuf = nsnull;
303 return 0;
304 }
305 return xpcomFunctions.stringGetData(aStr, aBuf, aTerm);
306}
307
308extern "C" NS_COM PRUnichar *
309NS_StringCloneData(const nsAString &aStr)
310{
311 if (!xpcomFunctions.stringCloneData)
312 return nsnull;
313 return xpcomFunctions.stringCloneData(aStr);
314}
315
316extern "C" NS_COM nsresult
317NS_StringSetData(nsAString &aStr, const PRUnichar *aBuf, PRUint32 aCount)
318{
319 if (!xpcomFunctions.stringSetData)
320 return NS_ERROR_NOT_INITIALIZED;
321
322 return xpcomFunctions.stringSetData(aStr, aBuf, aCount);
323}
324
325extern "C" NS_COM nsresult
326NS_StringSetDataRange(nsAString &aStr, PRUint32 aCutStart, PRUint32 aCutLength,
327 const PRUnichar *aBuf, PRUint32 aCount)
328{
329 if (!xpcomFunctions.stringSetDataRange)
330 return NS_ERROR_NOT_INITIALIZED;
331 return xpcomFunctions.stringSetDataRange(aStr, aCutStart, aCutLength, aBuf, aCount);
332}
333
334extern "C" NS_COM nsresult
335NS_StringCopy(nsAString &aDest, const nsAString &aSrc)
336{
337 if (!xpcomFunctions.stringCopy)
338 return NS_ERROR_NOT_INITIALIZED;
339 return xpcomFunctions.stringCopy(aDest, aSrc);
340}
341
342
343extern "C" NS_COM nsresult
344NS_CStringContainerInit(nsCStringContainer &aStr)
345{
346 if (!xpcomFunctions.cstringContainerInit)
347 return NS_ERROR_NOT_INITIALIZED;
348 return xpcomFunctions.cstringContainerInit(aStr);
349}
350
351extern "C" NS_COM void
352NS_CStringContainerFinish(nsCStringContainer &aStr)
353{
354 if (xpcomFunctions.cstringContainerFinish)
355 xpcomFunctions.cstringContainerFinish(aStr);
356}
357
358extern "C" NS_COM PRUint32
359NS_CStringGetData(const nsACString &aStr, const char **aBuf, PRBool *aTerm)
360{
361 if (!xpcomFunctions.cstringGetData) {
362 *aBuf = nsnull;
363 return 0;
364 }
365 return xpcomFunctions.cstringGetData(aStr, aBuf, aTerm);
366}
367
368extern "C" NS_COM char *
369NS_CStringCloneData(const nsACString &aStr)
370{
371 if (!xpcomFunctions.cstringCloneData)
372 return nsnull;
373 return xpcomFunctions.cstringCloneData(aStr);
374}
375
376extern "C" NS_COM nsresult
377NS_CStringSetData(nsACString &aStr, const char *aBuf, PRUint32 aCount)
378{
379 if (!xpcomFunctions.cstringSetData)
380 return NS_ERROR_NOT_INITIALIZED;
381 return xpcomFunctions.cstringSetData(aStr, aBuf, aCount);
382}
383
384extern "C" NS_COM nsresult
385NS_CStringSetDataRange(nsACString &aStr, PRUint32 aCutStart, PRUint32 aCutLength,
386 const char *aBuf, PRUint32 aCount)
387{
388 if (!xpcomFunctions.cstringSetDataRange)
389 return NS_ERROR_NOT_INITIALIZED;
390 return xpcomFunctions.cstringSetDataRange(aStr, aCutStart, aCutLength, aBuf, aCount);
391}
392
393extern "C" NS_COM nsresult
394NS_CStringCopy(nsACString &aDest, const nsACString &aSrc)
395{
396 if (!xpcomFunctions.cstringCopy)
397 return NS_ERROR_NOT_INITIALIZED;
398 return xpcomFunctions.cstringCopy(aDest, aSrc);
399}
400
401extern "C" NS_COM nsresult
402NS_CStringToUTF16(const nsACString &aSrc, PRUint32 aSrcEncoding, nsAString &aDest)
403{
404 if (!xpcomFunctions.cstringToUTF16)
405 return NS_ERROR_NOT_INITIALIZED;
406 return xpcomFunctions.cstringToUTF16(aSrc, aSrcEncoding, aDest);
407}
408
409extern "C" NS_COM nsresult
410NS_UTF16ToCString(const nsAString &aSrc, PRUint32 aDestEncoding, nsACString &aDest)
411{
412 if (!xpcomFunctions.utf16ToCString)
413 return NS_ERROR_NOT_INITIALIZED;
414 return xpcomFunctions.utf16ToCString(aSrc, aDestEncoding, aDest);
415}
416
417#endif // #ifndef XPCOM_GLUE_NO_DYNAMIC_LOADING
418
419
420static char sEnvString[MAXPATHLEN*10];
421static char* spEnvString = 0;
422
423void
424GRE_AddGREToEnvironment()
425{
426 const char* grePath = GRE_GetGREPath();
427 char szPath[MAXPATHLEN];
428 if (!grePath)
429 return;
430
431 const char* path = PR_GetEnv(XPCOM_SEARCH_KEY);
432 if (!path)
433 path = "";
434#ifdef VBOX
435 else
436 {
437 /* sEnvString is part of the environment because of putenv().
438 * path is only temporarily used and not argument of putenv() itself */
439 snprintf(szPath, sizeof(szPath), "%s", path);
440 path = szPath;
441 }
442#endif
443
444 if (spEnvString) PR_smprintf_free(spEnvString);
445
446 /**
447 * if the PATH string is longer than our static buffer, allocate a
448 * buffer for the environment string. This buffer will be leaked at shutdown!
449 */
450 if (strlen(grePath) + strlen(path) +
451 sizeof(XPCOM_SEARCH_KEY) + sizeof(XPCOM_ENV_PATH_SEPARATOR) > MAXPATHLEN*10) {
452 if (PR_smprintf(XPCOM_SEARCH_KEY "=%s" XPCOM_ENV_PATH_SEPARATOR "%s",
453 grePath,
454 path)) {
455 PR_SetEnv(spEnvString);
456 }
457 } else {
458 if (sprintf(sEnvString,
459 XPCOM_SEARCH_KEY "=%s" XPCOM_ENV_PATH_SEPARATOR "%s",
460 grePath,
461 path) > 0) {
462 PR_SetEnv(sEnvString);
463 }
464 }
465
466#if XP_WIN32
467 // On windows, the current directory is searched before the
468 // PATH environment variable. This is a very bad thing
469 // since libraries in the cwd will be picked up before
470 // any that are in either the application or GRE directory.
471
472 if (grePath) {
473 SetCurrentDirectory(grePath);
474 }
475#endif
476}
477
478
479// Default GRE startup/shutdown code
480
481extern "C"
482nsresult GRE_Startup()
483{
484 const char* xpcomLocation = GRE_GetXPCOMPath();
485
486 // Startup the XPCOM Glue that links us up with XPCOM.
487 nsresult rv = XPCOMGlueStartup(xpcomLocation);
488
489 if (NS_FAILED(rv)) {
490 NS_WARNING("gre: XPCOMGlueStartup failed");
491 return rv;
492 }
493
494 nsGREDirServiceProvider *provider = new nsGREDirServiceProvider();
495 if ( !provider ) {
496 NS_WARNING("GRE_Startup failed");
497 XPCOMGlueShutdown();
498 return NS_ERROR_OUT_OF_MEMORY;
499 }
500
501 nsCOMPtr<nsIServiceManager> servMan;
502 NS_ADDREF( provider );
503 rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, provider);
504 NS_RELEASE(provider);
505
506 if ( NS_FAILED(rv) || !servMan) {
507 NS_WARNING("gre: NS_InitXPCOM failed");
508 XPCOMGlueShutdown();
509 return rv;
510 }
511
512 return NS_OK;
513}
514
515extern "C"
516nsresult GRE_Shutdown()
517{
518 NS_ShutdownXPCOM(nsnull);
519 XPCOMGlueShutdown();
520 return NS_OK;
521}
Note: See TracBrowser for help on using the repository browser.

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