VirtualBox

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

Last change on this file since 549 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 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 if (!xpcomLib)
114 return NS_ERROR_FAILURE;
115
116 function = (GetFrozenFunctionsFunc) PR_FindSymbol(xpcomLib, "NS_GetFrozenFunctions");
117
118 if (!function)
119 rv = NS_ERROR_FAILURE;
120 else
121 rv = (*function)(&xpcomFunctions, libSpec.value.pathname);
122 }
123
124 if (NS_FAILED(rv))
125 goto bail;
126
127 rv = GlueStartupDebug();
128 if (NS_FAILED(rv))
129 goto bail;
130
131 // startup the nsMemory
132 rv = GlueStartupMemory();
133 if (NS_FAILED(rv)) {
134 GlueShutdownDebug();
135 goto bail;
136 }
137
138 GRE_AddGREToEnvironment();
139 return NS_OK;
140
141bail:
142 PR_UnloadLibrary(xpcomLib);
143 xpcomLib = nsnull;
144 memset(&xpcomFunctions, 0, sizeof(xpcomFunctions));
145 return NS_ERROR_FAILURE;
146#endif
147}
148
149extern "C"
150nsresult XPCOMGlueShutdown()
151{
152#ifdef XPCOM_GLUE_NO_DYNAMIC_LOADING
153 return NS_OK;
154#else
155
156 GlueShutdownMemory();
157
158 GlueShutdownDebug();
159
160 if (xpcomLib) {
161 PR_UnloadLibrary(xpcomLib);
162 xpcomLib = nsnull;
163 }
164
165 memset(&xpcomFunctions, 0, sizeof(xpcomFunctions));
166 return NS_OK;
167#endif
168}
169
170#ifndef XPCOM_GLUE_NO_DYNAMIC_LOADING
171extern "C" NS_COM nsresult
172NS_InitXPCOM2(nsIServiceManager* *result,
173 nsIFile* binDirectory,
174 nsIDirectoryServiceProvider* appFileLocationProvider)
175{
176 if (!xpcomFunctions.init)
177 return NS_ERROR_NOT_INITIALIZED;
178 return xpcomFunctions.init(result, binDirectory, appFileLocationProvider);
179}
180
181extern "C" NS_COM nsresult
182NS_ShutdownXPCOM(nsIServiceManager* servMgr)
183{
184 if (!xpcomFunctions.shutdown)
185 return NS_ERROR_NOT_INITIALIZED;
186 return xpcomFunctions.shutdown(servMgr);
187}
188
189extern "C" NS_COM nsresult
190NS_GetServiceManager(nsIServiceManager* *result)
191{
192 if (!xpcomFunctions.getServiceManager)
193 return NS_ERROR_NOT_INITIALIZED;
194 return xpcomFunctions.getServiceManager(result);
195}
196
197extern "C" NS_COM nsresult
198NS_GetComponentManager(nsIComponentManager* *result)
199{
200 if (!xpcomFunctions.getComponentManager)
201 return NS_ERROR_NOT_INITIALIZED;
202 return xpcomFunctions.getComponentManager(result);
203}
204
205extern "C" NS_COM nsresult
206NS_GetComponentRegistrar(nsIComponentRegistrar* *result)
207{
208 if (!xpcomFunctions.getComponentRegistrar)
209 return NS_ERROR_NOT_INITIALIZED;
210 return xpcomFunctions.getComponentRegistrar(result);
211}
212
213extern "C" NS_COM nsresult
214NS_GetMemoryManager(nsIMemory* *result)
215{
216 if (!xpcomFunctions.getMemoryManager)
217 return NS_ERROR_NOT_INITIALIZED;
218 return xpcomFunctions.getMemoryManager(result);
219}
220
221extern "C" NS_COM nsresult
222NS_NewLocalFile(const nsAString &path, PRBool followLinks, nsILocalFile* *result)
223{
224 if (!xpcomFunctions.newLocalFile)
225 return NS_ERROR_NOT_INITIALIZED;
226 return xpcomFunctions.newLocalFile(path, followLinks, result);
227}
228
229extern "C" NS_COM nsresult
230NS_NewNativeLocalFile(const nsACString &path, PRBool followLinks, nsILocalFile* *result)
231{
232 if (!xpcomFunctions.newNativeLocalFile)
233 return NS_ERROR_NOT_INITIALIZED;
234 return xpcomFunctions.newNativeLocalFile(path, followLinks, result);
235}
236
237extern "C" NS_COM nsresult
238NS_RegisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine, PRUint32 priority)
239{
240 if (!xpcomFunctions.registerExitRoutine)
241 return NS_ERROR_NOT_INITIALIZED;
242 return xpcomFunctions.registerExitRoutine(exitRoutine, priority);
243}
244
245extern "C" NS_COM nsresult
246NS_UnregisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine)
247{
248 if (!xpcomFunctions.unregisterExitRoutine)
249 return NS_ERROR_NOT_INITIALIZED;
250 return xpcomFunctions.unregisterExitRoutine(exitRoutine);
251}
252
253extern "C" NS_COM nsresult
254NS_GetDebug(nsIDebug* *result)
255{
256 if (!xpcomFunctions.getDebug)
257 return NS_ERROR_NOT_INITIALIZED;
258 return xpcomFunctions.getDebug(result);
259}
260
261
262extern "C" NS_COM nsresult
263NS_GetTraceRefcnt(nsITraceRefcnt* *result)
264{
265 if (!xpcomFunctions.getTraceRefcnt)
266 return NS_ERROR_NOT_INITIALIZED;
267 return xpcomFunctions.getTraceRefcnt(result);
268}
269
270
271extern "C" NS_COM nsresult
272NS_StringContainerInit(nsStringContainer &aStr)
273{
274 if (!xpcomFunctions.stringContainerInit)
275 return NS_ERROR_NOT_INITIALIZED;
276 return xpcomFunctions.stringContainerInit(aStr);
277}
278
279extern "C" NS_COM void
280NS_StringContainerFinish(nsStringContainer &aStr)
281{
282 if (xpcomFunctions.stringContainerFinish)
283 xpcomFunctions.stringContainerFinish(aStr);
284}
285
286extern "C" NS_COM PRUint32
287NS_StringGetData(const nsAString &aStr, const PRUnichar **aBuf, PRBool *aTerm)
288{
289 if (!xpcomFunctions.stringGetData) {
290 *aBuf = nsnull;
291 return 0;
292 }
293 return xpcomFunctions.stringGetData(aStr, aBuf, aTerm);
294}
295
296extern "C" NS_COM PRUnichar *
297NS_StringCloneData(const nsAString &aStr)
298{
299 if (!xpcomFunctions.stringCloneData)
300 return nsnull;
301 return xpcomFunctions.stringCloneData(aStr);
302}
303
304extern "C" NS_COM nsresult
305NS_StringSetData(nsAString &aStr, const PRUnichar *aBuf, PRUint32 aCount)
306{
307 if (!xpcomFunctions.stringSetData)
308 return NS_ERROR_NOT_INITIALIZED;
309
310 return xpcomFunctions.stringSetData(aStr, aBuf, aCount);
311}
312
313extern "C" NS_COM nsresult
314NS_StringSetDataRange(nsAString &aStr, PRUint32 aCutStart, PRUint32 aCutLength,
315 const PRUnichar *aBuf, PRUint32 aCount)
316{
317 if (!xpcomFunctions.stringSetDataRange)
318 return NS_ERROR_NOT_INITIALIZED;
319 return xpcomFunctions.stringSetDataRange(aStr, aCutStart, aCutLength, aBuf, aCount);
320}
321
322extern "C" NS_COM nsresult
323NS_StringCopy(nsAString &aDest, const nsAString &aSrc)
324{
325 if (!xpcomFunctions.stringCopy)
326 return NS_ERROR_NOT_INITIALIZED;
327 return xpcomFunctions.stringCopy(aDest, aSrc);
328}
329
330
331extern "C" NS_COM nsresult
332NS_CStringContainerInit(nsCStringContainer &aStr)
333{
334 if (!xpcomFunctions.cstringContainerInit)
335 return NS_ERROR_NOT_INITIALIZED;
336 return xpcomFunctions.cstringContainerInit(aStr);
337}
338
339extern "C" NS_COM void
340NS_CStringContainerFinish(nsCStringContainer &aStr)
341{
342 if (xpcomFunctions.cstringContainerFinish)
343 xpcomFunctions.cstringContainerFinish(aStr);
344}
345
346extern "C" NS_COM PRUint32
347NS_CStringGetData(const nsACString &aStr, const char **aBuf, PRBool *aTerm)
348{
349 if (!xpcomFunctions.cstringGetData) {
350 *aBuf = nsnull;
351 return 0;
352 }
353 return xpcomFunctions.cstringGetData(aStr, aBuf, aTerm);
354}
355
356extern "C" NS_COM char *
357NS_CStringCloneData(const nsACString &aStr)
358{
359 if (!xpcomFunctions.cstringCloneData)
360 return nsnull;
361 return xpcomFunctions.cstringCloneData(aStr);
362}
363
364extern "C" NS_COM nsresult
365NS_CStringSetData(nsACString &aStr, const char *aBuf, PRUint32 aCount)
366{
367 if (!xpcomFunctions.cstringSetData)
368 return NS_ERROR_NOT_INITIALIZED;
369 return xpcomFunctions.cstringSetData(aStr, aBuf, aCount);
370}
371
372extern "C" NS_COM nsresult
373NS_CStringSetDataRange(nsACString &aStr, PRUint32 aCutStart, PRUint32 aCutLength,
374 const char *aBuf, PRUint32 aCount)
375{
376 if (!xpcomFunctions.cstringSetDataRange)
377 return NS_ERROR_NOT_INITIALIZED;
378 return xpcomFunctions.cstringSetDataRange(aStr, aCutStart, aCutLength, aBuf, aCount);
379}
380
381extern "C" NS_COM nsresult
382NS_CStringCopy(nsACString &aDest, const nsACString &aSrc)
383{
384 if (!xpcomFunctions.cstringCopy)
385 return NS_ERROR_NOT_INITIALIZED;
386 return xpcomFunctions.cstringCopy(aDest, aSrc);
387}
388
389extern "C" NS_COM nsresult
390NS_CStringToUTF16(const nsACString &aSrc, PRUint32 aSrcEncoding, nsAString &aDest)
391{
392 if (!xpcomFunctions.cstringToUTF16)
393 return NS_ERROR_NOT_INITIALIZED;
394 return xpcomFunctions.cstringToUTF16(aSrc, aSrcEncoding, aDest);
395}
396
397extern "C" NS_COM nsresult
398NS_UTF16ToCString(const nsAString &aSrc, PRUint32 aDestEncoding, nsACString &aDest)
399{
400 if (!xpcomFunctions.utf16ToCString)
401 return NS_ERROR_NOT_INITIALIZED;
402 return xpcomFunctions.utf16ToCString(aSrc, aDestEncoding, aDest);
403}
404
405#endif // #ifndef XPCOM_GLUE_NO_DYNAMIC_LOADING
406
407
408static char sEnvString[MAXPATHLEN*10];
409static char* spEnvString = 0;
410
411void
412GRE_AddGREToEnvironment()
413{
414 const char* grePath = GRE_GetGREPath();
415 if (!grePath)
416 return;
417
418 const char* path = PR_GetEnv(XPCOM_SEARCH_KEY);
419 if (!path) {
420 path = "";
421 }
422
423 if (spEnvString) PR_smprintf_free(spEnvString);
424
425 /**
426 * if the PATH string is longer than our static buffer, allocate a
427 * buffer for the environment string. This buffer will be leaked at shutdown!
428 */
429 if (strlen(grePath) + strlen(path) +
430 sizeof(XPCOM_SEARCH_KEY) + sizeof(XPCOM_ENV_PATH_SEPARATOR) > MAXPATHLEN*10) {
431 if (PR_smprintf(XPCOM_SEARCH_KEY "=%s" XPCOM_ENV_PATH_SEPARATOR "%s",
432 grePath,
433 path)) {
434 PR_SetEnv(spEnvString);
435 }
436 } else {
437 if (sprintf(sEnvString,
438 XPCOM_SEARCH_KEY "=%s" XPCOM_ENV_PATH_SEPARATOR "%s",
439 grePath,
440 path) > 0) {
441 PR_SetEnv(sEnvString);
442 }
443 }
444
445#if XP_WIN32
446 // On windows, the current directory is searched before the
447 // PATH environment variable. This is a very bad thing
448 // since libraries in the cwd will be picked up before
449 // any that are in either the application or GRE directory.
450
451 if (grePath) {
452 SetCurrentDirectory(grePath);
453 }
454#endif
455}
456
457
458// Default GRE startup/shutdown code
459
460extern "C"
461nsresult GRE_Startup()
462{
463 const char* xpcomLocation = GRE_GetXPCOMPath();
464
465 // Startup the XPCOM Glue that links us up with XPCOM.
466 nsresult rv = XPCOMGlueStartup(xpcomLocation);
467
468 if (NS_FAILED(rv)) {
469 NS_WARNING("gre: XPCOMGlueStartup failed");
470 return rv;
471 }
472
473 nsGREDirServiceProvider *provider = new nsGREDirServiceProvider();
474 if ( !provider ) {
475 NS_WARNING("GRE_Startup failed");
476 XPCOMGlueShutdown();
477 return NS_ERROR_OUT_OF_MEMORY;
478 }
479
480 nsCOMPtr<nsIServiceManager> servMan;
481 NS_ADDREF( provider );
482 rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, provider);
483 NS_RELEASE(provider);
484
485 if ( NS_FAILED(rv) || !servMan) {
486 NS_WARNING("gre: NS_InitXPCOM failed");
487 XPCOMGlueShutdown();
488 return rv;
489 }
490
491 return NS_OK;
492}
493
494extern "C"
495nsresult GRE_Shutdown()
496{
497 NS_ShutdownXPCOM(nsnull);
498 XPCOMGlueShutdown();
499 return NS_OK;
500}
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