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
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/
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
16 * The Original Code is mozilla.org code.
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.
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.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsXPCOMGlue.h"
40 #include "nsGlueLinking.h"
44 #include "nsIServiceManager.h"
45 #include "nsXPCOMPrivate.h"
54 #define snprintf _snprintf
57 static XPCOMFunctions xpcomFunctions
;
60 nsresult
XPCOMGlueStartup(const char* xpcomFile
)
62 xpcomFunctions
.version
= XPCOM_GLUE_VERSION
;
63 xpcomFunctions
.size
= sizeof(XPCOMFunctions
);
65 GetFrozenFunctionsFunc func
= nsnull
;
68 xpcomFile
= XPCOM_DLL
;
71 func
= XPCOMGlueLoad(xpcomFile
);
74 return NS_ERROR_FAILURE
;
76 nsresult rv
= (*func
)(&xpcomFunctions
, nsnull
);
85 #if defined(XP_WIN) || defined(XP_OS2)
86 #define READ_TEXTMODE "t"
92 XPCOMGlueLoadDependentLibs(const char *xpcomDir
, DependentLibsCallback cb
)
94 char buffer
[MAXPATHLEN
];
95 sprintf(buffer
, "%s" XPCOM_FILE_PATH_SEPARATOR XPCOM_DEPENDENT_LIBS_LIST
,
98 FILE *flist
= fopen(buffer
, "r" READ_TEXTMODE
);
102 while (fgets(buffer
, sizeof(buffer
), flist
)) {
103 int l
= strlen(buffer
);
105 // ignore empty lines and comments
106 if (l
== 0 || *buffer
== '#')
109 // cut the trailing newline, if present
110 if (buffer
[l
- 1] == '\n')
111 buffer
[l
- 1] = '\0';
113 char buffer2
[MAXPATHLEN
];
114 snprintf(buffer2
, sizeof(buffer2
),
115 "%s" XPCOM_FILE_PATH_SEPARATOR
"%s",
124 nsresult
XPCOMGlueShutdown()
128 memset(&xpcomFunctions
, 0, sizeof(xpcomFunctions
));
133 NS_InitXPCOM2(nsIServiceManager
* *result
,
134 nsIFile
* binDirectory
,
135 nsIDirectoryServiceProvider
* appFileLocationProvider
)
137 if (!xpcomFunctions
.init
)
138 return NS_ERROR_NOT_INITIALIZED
;
139 return xpcomFunctions
.init(result
, binDirectory
, appFileLocationProvider
);
143 NS_InitXPCOM3(nsIServiceManager
* *result
,
144 nsIFile
* binDirectory
,
145 nsIDirectoryServiceProvider
* appFileLocationProvider
,
146 nsStaticModuleInfo
const *staticComponents
,
147 PRUint32 componentCount
)
149 if (!xpcomFunctions
.init3
)
150 return NS_ERROR_NOT_INITIALIZED
;
151 return xpcomFunctions
.init3(result
, binDirectory
, appFileLocationProvider
,
152 staticComponents
, componentCount
);
156 NS_ShutdownXPCOM(nsIServiceManager
* servMgr
)
158 if (!xpcomFunctions
.shutdown
)
159 return NS_ERROR_NOT_INITIALIZED
;
160 return xpcomFunctions
.shutdown(servMgr
);
164 NS_GetServiceManager(nsIServiceManager
* *result
)
166 if (!xpcomFunctions
.getServiceManager
)
167 return NS_ERROR_NOT_INITIALIZED
;
168 return xpcomFunctions
.getServiceManager(result
);
172 NS_GetComponentManager(nsIComponentManager
* *result
)
174 if (!xpcomFunctions
.getComponentManager
)
175 return NS_ERROR_NOT_INITIALIZED
;
176 return xpcomFunctions
.getComponentManager(result
);
180 NS_GetComponentRegistrar(nsIComponentRegistrar
* *result
)
182 if (!xpcomFunctions
.getComponentRegistrar
)
183 return NS_ERROR_NOT_INITIALIZED
;
184 return xpcomFunctions
.getComponentRegistrar(result
);
188 NS_GetMemoryManager(nsIMemory
* *result
)
190 if (!xpcomFunctions
.getMemoryManager
)
191 return NS_ERROR_NOT_INITIALIZED
;
192 return xpcomFunctions
.getMemoryManager(result
);
196 NS_NewLocalFile(const nsAString
&path
, PRBool followLinks
, nsILocalFile
* *result
)
198 if (!xpcomFunctions
.newLocalFile
)
199 return NS_ERROR_NOT_INITIALIZED
;
200 return xpcomFunctions
.newLocalFile(path
, followLinks
, result
);
204 NS_NewNativeLocalFile(const nsACString
&path
, PRBool followLinks
, nsILocalFile
* *result
)
206 if (!xpcomFunctions
.newNativeLocalFile
)
207 return NS_ERROR_NOT_INITIALIZED
;
208 return xpcomFunctions
.newNativeLocalFile(path
, followLinks
, result
);
212 NS_GetDebug(nsIDebug
* *result
)
214 if (!xpcomFunctions
.getDebug
)
215 return NS_ERROR_NOT_INITIALIZED
;
216 return xpcomFunctions
.getDebug(result
);
221 NS_GetTraceRefcnt(nsITraceRefcnt
* *result
)
223 if (!xpcomFunctions
.getTraceRefcnt
)
224 return NS_ERROR_NOT_INITIALIZED
;
225 return xpcomFunctions
.getTraceRefcnt(result
);
230 NS_StringContainerInit(nsStringContainer
&aStr
)
232 if (!xpcomFunctions
.stringContainerInit
)
233 return NS_ERROR_NOT_INITIALIZED
;
234 return xpcomFunctions
.stringContainerInit(aStr
);
238 NS_StringContainerInit2(nsStringContainer
&aStr
,
239 const PRUnichar
*aData
,
240 PRUint32 aDataLength
,
243 if (!xpcomFunctions
.stringContainerInit2
)
244 return NS_ERROR_NOT_INITIALIZED
;
245 return xpcomFunctions
.stringContainerInit2(aStr
, aData
, aDataLength
, aFlags
);
249 NS_StringContainerFinish(nsStringContainer
&aStr
)
251 if (xpcomFunctions
.stringContainerFinish
)
252 xpcomFunctions
.stringContainerFinish(aStr
);
256 NS_StringGetData(const nsAString
&aStr
, const PRUnichar
**aBuf
, PRBool
*aTerm
)
258 if (!xpcomFunctions
.stringGetData
) {
262 return xpcomFunctions
.stringGetData(aStr
, aBuf
, aTerm
);
266 NS_StringGetMutableData(nsAString
&aStr
, PRUint32 aLen
, PRUnichar
**aBuf
)
268 if (!xpcomFunctions
.stringGetMutableData
) {
272 return xpcomFunctions
.stringGetMutableData(aStr
, aLen
, aBuf
);
275 XPCOM_API(PRUnichar
*)
276 NS_StringCloneData(const nsAString
&aStr
)
278 if (!xpcomFunctions
.stringCloneData
)
280 return xpcomFunctions
.stringCloneData(aStr
);
284 NS_StringSetData(nsAString
&aStr
, const PRUnichar
*aBuf
, PRUint32 aCount
)
286 if (!xpcomFunctions
.stringSetData
)
287 return NS_ERROR_NOT_INITIALIZED
;
289 return xpcomFunctions
.stringSetData(aStr
, aBuf
, aCount
);
293 NS_StringSetDataRange(nsAString
&aStr
, PRUint32 aCutStart
, PRUint32 aCutLength
,
294 const PRUnichar
*aBuf
, PRUint32 aCount
)
296 if (!xpcomFunctions
.stringSetDataRange
)
297 return NS_ERROR_NOT_INITIALIZED
;
298 return xpcomFunctions
.stringSetDataRange(aStr
, aCutStart
, aCutLength
, aBuf
, aCount
);
302 NS_StringCopy(nsAString
&aDest
, const nsAString
&aSrc
)
304 if (!xpcomFunctions
.stringCopy
)
305 return NS_ERROR_NOT_INITIALIZED
;
306 return xpcomFunctions
.stringCopy(aDest
, aSrc
);
310 NS_StringSetIsVoid(nsAString
&aStr
, const PRBool aIsVoid
)
312 if (xpcomFunctions
.stringSetIsVoid
)
313 xpcomFunctions
.stringSetIsVoid(aStr
, aIsVoid
);
317 NS_StringGetIsVoid(const nsAString
&aStr
)
319 if (!xpcomFunctions
.stringGetIsVoid
)
321 return xpcomFunctions
.stringGetIsVoid(aStr
);
325 NS_CStringContainerInit(nsCStringContainer
&aStr
)
327 if (!xpcomFunctions
.cstringContainerInit
)
328 return NS_ERROR_NOT_INITIALIZED
;
329 return xpcomFunctions
.cstringContainerInit(aStr
);
333 NS_CStringContainerInit2(nsCStringContainer
&aStr
,
335 PRUint32 aDataLength
,
338 if (!xpcomFunctions
.cstringContainerInit2
)
339 return NS_ERROR_NOT_INITIALIZED
;
340 return xpcomFunctions
.cstringContainerInit2(aStr
, aData
, aDataLength
, aFlags
);
344 NS_CStringContainerFinish(nsCStringContainer
&aStr
)
346 if (xpcomFunctions
.cstringContainerFinish
)
347 xpcomFunctions
.cstringContainerFinish(aStr
);
351 NS_CStringGetData(const nsACString
&aStr
, const char **aBuf
, PRBool
*aTerm
)
353 if (!xpcomFunctions
.cstringGetData
) {
357 return xpcomFunctions
.cstringGetData(aStr
, aBuf
, aTerm
);
361 NS_CStringGetMutableData(nsACString
&aStr
, PRUint32 aLen
, char **aBuf
)
363 if (!xpcomFunctions
.cstringGetMutableData
) {
367 return xpcomFunctions
.cstringGetMutableData(aStr
, aLen
, aBuf
);
371 NS_CStringCloneData(const nsACString
&aStr
)
373 if (!xpcomFunctions
.cstringCloneData
)
375 return xpcomFunctions
.cstringCloneData(aStr
);
379 NS_CStringSetData(nsACString
&aStr
, const char *aBuf
, PRUint32 aCount
)
381 if (!xpcomFunctions
.cstringSetData
)
382 return NS_ERROR_NOT_INITIALIZED
;
383 return xpcomFunctions
.cstringSetData(aStr
, aBuf
, aCount
);
387 NS_CStringSetDataRange(nsACString
&aStr
, PRUint32 aCutStart
, PRUint32 aCutLength
,
388 const char *aBuf
, PRUint32 aCount
)
390 if (!xpcomFunctions
.cstringSetDataRange
)
391 return NS_ERROR_NOT_INITIALIZED
;
392 return xpcomFunctions
.cstringSetDataRange(aStr
, aCutStart
, aCutLength
, aBuf
, aCount
);
396 NS_CStringCopy(nsACString
&aDest
, const nsACString
&aSrc
)
398 if (!xpcomFunctions
.cstringCopy
)
399 return NS_ERROR_NOT_INITIALIZED
;
400 return xpcomFunctions
.cstringCopy(aDest
, aSrc
);
404 NS_CStringSetIsVoid(nsACString
&aStr
, const PRBool aIsVoid
)
406 if (xpcomFunctions
.cstringSetIsVoid
)
407 xpcomFunctions
.cstringSetIsVoid(aStr
, aIsVoid
);
411 NS_CStringGetIsVoid(const nsACString
&aStr
)
413 if (!xpcomFunctions
.cstringGetIsVoid
)
415 return xpcomFunctions
.cstringGetIsVoid(aStr
);
419 NS_CStringToUTF16(const nsACString
&aSrc
, nsCStringEncoding aSrcEncoding
, nsAString
&aDest
)
421 if (!xpcomFunctions
.cstringToUTF16
)
422 return NS_ERROR_NOT_INITIALIZED
;
423 return xpcomFunctions
.cstringToUTF16(aSrc
, aSrcEncoding
, aDest
);
427 NS_UTF16ToCString(const nsAString
&aSrc
, nsCStringEncoding aDestEncoding
, nsACString
&aDest
)
429 if (!xpcomFunctions
.utf16ToCString
)
430 return NS_ERROR_NOT_INITIALIZED
;
431 return xpcomFunctions
.utf16ToCString(aSrc
, aDestEncoding
, aDest
);
435 NS_Alloc(PRSize size
)
437 if (!xpcomFunctions
.allocFunc
)
439 return xpcomFunctions
.allocFunc(size
);
443 NS_Realloc(void* ptr
, PRSize size
)
445 if (!xpcomFunctions
.reallocFunc
)
447 return xpcomFunctions
.reallocFunc(ptr
, size
);
453 if (xpcomFunctions
.freeFunc
)
454 xpcomFunctions
.freeFunc(ptr
);
458 NS_DebugBreak(PRUint32 aSeverity
, const char *aStr
, const char *aExpr
,
459 const char *aFile
, PRInt32 aLine
)
461 if (xpcomFunctions
.debugBreakFunc
)
462 xpcomFunctions
.debugBreakFunc(aSeverity
, aStr
, aExpr
, aFile
, aLine
);
468 if (xpcomFunctions
.logInitFunc
)
469 xpcomFunctions
.logInitFunc();
475 if (xpcomFunctions
.logTermFunc
)
476 xpcomFunctions
.logTermFunc();
480 NS_LogAddRef(void *aPtr
, nsrefcnt aNewRefCnt
,
481 const char *aTypeName
, PRUint32 aInstanceSize
)
483 if (xpcomFunctions
.logAddRefFunc
)
484 xpcomFunctions
.logAddRefFunc(aPtr
, aNewRefCnt
,
485 aTypeName
, aInstanceSize
);
489 NS_LogRelease(void *aPtr
, nsrefcnt aNewRefCnt
, const char *aTypeName
)
491 if (xpcomFunctions
.logReleaseFunc
)
492 xpcomFunctions
.logReleaseFunc(aPtr
, aNewRefCnt
, aTypeName
);
496 NS_LogCtor(void *aPtr
, const char *aTypeName
, PRUint32 aInstanceSize
)
498 if (xpcomFunctions
.logCtorFunc
)
499 xpcomFunctions
.logCtorFunc(aPtr
, aTypeName
, aInstanceSize
);
503 NS_LogDtor(void *aPtr
, const char *aTypeName
, PRUint32 aInstanceSize
)
505 if (xpcomFunctions
.logDtorFunc
)
506 xpcomFunctions
.logDtorFunc(aPtr
, aTypeName
, aInstanceSize
);
510 NS_LogCOMPtrAddRef(void *aCOMPtr
, nsISupports
*aObject
)
512 if (xpcomFunctions
.logCOMPtrAddRefFunc
)
513 xpcomFunctions
.logCOMPtrAddRefFunc(aCOMPtr
, aObject
);
517 NS_LogCOMPtrRelease(void *aCOMPtr
, nsISupports
*aObject
)
519 if (xpcomFunctions
.logCOMPtrReleaseFunc
)
520 xpcomFunctions
.logCOMPtrReleaseFunc(aCOMPtr
, aObject
);
524 NS_GetXPTCallStub(REFNSIID aIID
, nsIXPTCProxy
* aOuter
,
525 nsISomeInterface
* *aStub
)
527 if (!xpcomFunctions
.getXPTCallStubFunc
)
528 return NS_ERROR_NOT_INITIALIZED
;
530 return xpcomFunctions
.getXPTCallStubFunc(aIID
, aOuter
, aStub
);
534 NS_DestroyXPTCallStub(nsISomeInterface
* aStub
)
536 if (xpcomFunctions
.destroyXPTCallStubFunc
)
537 xpcomFunctions
.destroyXPTCallStubFunc(aStub
);
541 NS_InvokeByIndex(nsISupports
* that
, PRUint32 methodIndex
,
542 PRUint32 paramCount
, nsXPTCVariant
* params
)
544 if (!xpcomFunctions
.invokeByIndexFunc
)
545 return NS_ERROR_NOT_INITIALIZED
;
547 return xpcomFunctions
.invokeByIndexFunc(that
, methodIndex
,
552 NS_CycleCollectorSuspect(nsISupports
* obj
)
554 if (!xpcomFunctions
.cycleSuspectFunc
)
557 return xpcomFunctions
.cycleSuspectFunc(obj
);
561 NS_CycleCollectorForget(nsISupports
* obj
)
563 if (!xpcomFunctions
.cycleForgetFunc
)
566 return xpcomFunctions
.cycleForgetFunc(obj
);