VirtualBox

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

最後變更 在這個檔案從31232是 6068,由 vboxsync 提交於 17 年 前

DARWIN -> RT_OS_DARWIN.

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

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