Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / xpcom / glue / standalone / nsXPCOMGlue.cpp
blob6533024c1b22d651700d380e5758b7f6c07619b3
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
14 * License.
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.
23 * Contributor(s):
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"
42 #include "nspr.h"
43 #include "nsDebug.h"
44 #include "nsIServiceManager.h"
45 #include "nsXPCOMPrivate.h"
46 #include "nsCOMPtr.h"
47 #include <stdlib.h>
48 #include <stdio.h>
50 #ifdef XP_WIN
51 #include <windows.h>
52 #include <mbstring.h>
53 #include <malloc.h>
54 #define snprintf _snprintf
55 #endif
57 static XPCOMFunctions xpcomFunctions;
59 extern "C"
60 nsresult XPCOMGlueStartup(const char* xpcomFile)
62 xpcomFunctions.version = XPCOM_GLUE_VERSION;
63 xpcomFunctions.size = sizeof(XPCOMFunctions);
65 GetFrozenFunctionsFunc func = nsnull;
67 if (!xpcomFile)
68 xpcomFile = XPCOM_DLL;
71 func = XPCOMGlueLoad(xpcomFile);
73 if (!func)
74 return NS_ERROR_FAILURE;
76 nsresult rv = (*func)(&xpcomFunctions, nsnull);
77 if (NS_FAILED(rv)) {
78 XPCOMGlueUnload();
79 return rv;
82 return NS_OK;
85 #if defined(XP_WIN) || defined(XP_OS2)
86 #define READ_TEXTMODE "t"
87 #else
88 #define READ_TEXTMODE
89 #endif
91 void
92 XPCOMGlueLoadDependentLibs(const char *xpcomDir, DependentLibsCallback cb)
94 char buffer[MAXPATHLEN];
95 sprintf(buffer, "%s" XPCOM_FILE_PATH_SEPARATOR XPCOM_DEPENDENT_LIBS_LIST,
96 xpcomDir);
98 FILE *flist = fopen(buffer, "r" READ_TEXTMODE);
99 if (!flist)
100 return;
102 while (fgets(buffer, sizeof(buffer), flist)) {
103 int l = strlen(buffer);
105 // ignore empty lines and comments
106 if (l == 0 || *buffer == '#')
107 continue;
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",
116 xpcomDir, buffer);
117 cb(buffer2);
120 fclose(flist);
123 extern "C"
124 nsresult XPCOMGlueShutdown()
126 XPCOMGlueUnload();
128 memset(&xpcomFunctions, 0, sizeof(xpcomFunctions));
129 return NS_OK;
132 XPCOM_API(nsresult)
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);
142 XPCOM_API(nsresult)
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);
155 XPCOM_API(nsresult)
156 NS_ShutdownXPCOM(nsIServiceManager* servMgr)
158 if (!xpcomFunctions.shutdown)
159 return NS_ERROR_NOT_INITIALIZED;
160 return xpcomFunctions.shutdown(servMgr);
163 XPCOM_API(nsresult)
164 NS_GetServiceManager(nsIServiceManager* *result)
166 if (!xpcomFunctions.getServiceManager)
167 return NS_ERROR_NOT_INITIALIZED;
168 return xpcomFunctions.getServiceManager(result);
171 XPCOM_API(nsresult)
172 NS_GetComponentManager(nsIComponentManager* *result)
174 if (!xpcomFunctions.getComponentManager)
175 return NS_ERROR_NOT_INITIALIZED;
176 return xpcomFunctions.getComponentManager(result);
179 XPCOM_API(nsresult)
180 NS_GetComponentRegistrar(nsIComponentRegistrar* *result)
182 if (!xpcomFunctions.getComponentRegistrar)
183 return NS_ERROR_NOT_INITIALIZED;
184 return xpcomFunctions.getComponentRegistrar(result);
187 XPCOM_API(nsresult)
188 NS_GetMemoryManager(nsIMemory* *result)
190 if (!xpcomFunctions.getMemoryManager)
191 return NS_ERROR_NOT_INITIALIZED;
192 return xpcomFunctions.getMemoryManager(result);
195 XPCOM_API(nsresult)
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);
203 XPCOM_API(nsresult)
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);
211 XPCOM_API(nsresult)
212 NS_GetDebug(nsIDebug* *result)
214 if (!xpcomFunctions.getDebug)
215 return NS_ERROR_NOT_INITIALIZED;
216 return xpcomFunctions.getDebug(result);
220 XPCOM_API(nsresult)
221 NS_GetTraceRefcnt(nsITraceRefcnt* *result)
223 if (!xpcomFunctions.getTraceRefcnt)
224 return NS_ERROR_NOT_INITIALIZED;
225 return xpcomFunctions.getTraceRefcnt(result);
229 XPCOM_API(nsresult)
230 NS_StringContainerInit(nsStringContainer &aStr)
232 if (!xpcomFunctions.stringContainerInit)
233 return NS_ERROR_NOT_INITIALIZED;
234 return xpcomFunctions.stringContainerInit(aStr);
237 XPCOM_API(nsresult)
238 NS_StringContainerInit2(nsStringContainer &aStr,
239 const PRUnichar *aData,
240 PRUint32 aDataLength,
241 PRUint32 aFlags)
243 if (!xpcomFunctions.stringContainerInit2)
244 return NS_ERROR_NOT_INITIALIZED;
245 return xpcomFunctions.stringContainerInit2(aStr, aData, aDataLength, aFlags);
248 XPCOM_API(void)
249 NS_StringContainerFinish(nsStringContainer &aStr)
251 if (xpcomFunctions.stringContainerFinish)
252 xpcomFunctions.stringContainerFinish(aStr);
255 XPCOM_API(PRUint32)
256 NS_StringGetData(const nsAString &aStr, const PRUnichar **aBuf, PRBool *aTerm)
258 if (!xpcomFunctions.stringGetData) {
259 *aBuf = nsnull;
260 return 0;
262 return xpcomFunctions.stringGetData(aStr, aBuf, aTerm);
265 XPCOM_API(PRUint32)
266 NS_StringGetMutableData(nsAString &aStr, PRUint32 aLen, PRUnichar **aBuf)
268 if (!xpcomFunctions.stringGetMutableData) {
269 *aBuf = nsnull;
270 return 0;
272 return xpcomFunctions.stringGetMutableData(aStr, aLen, aBuf);
275 XPCOM_API(PRUnichar*)
276 NS_StringCloneData(const nsAString &aStr)
278 if (!xpcomFunctions.stringCloneData)
279 return nsnull;
280 return xpcomFunctions.stringCloneData(aStr);
283 XPCOM_API(nsresult)
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);
292 XPCOM_API(nsresult)
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);
301 XPCOM_API(nsresult)
302 NS_StringCopy(nsAString &aDest, const nsAString &aSrc)
304 if (!xpcomFunctions.stringCopy)
305 return NS_ERROR_NOT_INITIALIZED;
306 return xpcomFunctions.stringCopy(aDest, aSrc);
309 XPCOM_API(void)
310 NS_StringSetIsVoid(nsAString &aStr, const PRBool aIsVoid)
312 if (xpcomFunctions.stringSetIsVoid)
313 xpcomFunctions.stringSetIsVoid(aStr, aIsVoid);
316 XPCOM_API(PRBool)
317 NS_StringGetIsVoid(const nsAString &aStr)
319 if (!xpcomFunctions.stringGetIsVoid)
320 return PR_FALSE;
321 return xpcomFunctions.stringGetIsVoid(aStr);
324 XPCOM_API(nsresult)
325 NS_CStringContainerInit(nsCStringContainer &aStr)
327 if (!xpcomFunctions.cstringContainerInit)
328 return NS_ERROR_NOT_INITIALIZED;
329 return xpcomFunctions.cstringContainerInit(aStr);
332 XPCOM_API(nsresult)
333 NS_CStringContainerInit2(nsCStringContainer &aStr,
334 const char *aData,
335 PRUint32 aDataLength,
336 PRUint32 aFlags)
338 if (!xpcomFunctions.cstringContainerInit2)
339 return NS_ERROR_NOT_INITIALIZED;
340 return xpcomFunctions.cstringContainerInit2(aStr, aData, aDataLength, aFlags);
343 XPCOM_API(void)
344 NS_CStringContainerFinish(nsCStringContainer &aStr)
346 if (xpcomFunctions.cstringContainerFinish)
347 xpcomFunctions.cstringContainerFinish(aStr);
350 XPCOM_API(PRUint32)
351 NS_CStringGetData(const nsACString &aStr, const char **aBuf, PRBool *aTerm)
353 if (!xpcomFunctions.cstringGetData) {
354 *aBuf = nsnull;
355 return 0;
357 return xpcomFunctions.cstringGetData(aStr, aBuf, aTerm);
360 XPCOM_API(PRUint32)
361 NS_CStringGetMutableData(nsACString &aStr, PRUint32 aLen, char **aBuf)
363 if (!xpcomFunctions.cstringGetMutableData) {
364 *aBuf = nsnull;
365 return 0;
367 return xpcomFunctions.cstringGetMutableData(aStr, aLen, aBuf);
370 XPCOM_API(char*)
371 NS_CStringCloneData(const nsACString &aStr)
373 if (!xpcomFunctions.cstringCloneData)
374 return nsnull;
375 return xpcomFunctions.cstringCloneData(aStr);
378 XPCOM_API(nsresult)
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);
386 XPCOM_API(nsresult)
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);
395 XPCOM_API(nsresult)
396 NS_CStringCopy(nsACString &aDest, const nsACString &aSrc)
398 if (!xpcomFunctions.cstringCopy)
399 return NS_ERROR_NOT_INITIALIZED;
400 return xpcomFunctions.cstringCopy(aDest, aSrc);
403 XPCOM_API(void)
404 NS_CStringSetIsVoid(nsACString &aStr, const PRBool aIsVoid)
406 if (xpcomFunctions.cstringSetIsVoid)
407 xpcomFunctions.cstringSetIsVoid(aStr, aIsVoid);
410 XPCOM_API(PRBool)
411 NS_CStringGetIsVoid(const nsACString &aStr)
413 if (!xpcomFunctions.cstringGetIsVoid)
414 return PR_FALSE;
415 return xpcomFunctions.cstringGetIsVoid(aStr);
418 XPCOM_API(nsresult)
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);
426 XPCOM_API(nsresult)
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);
434 XPCOM_API(void*)
435 NS_Alloc(PRSize size)
437 if (!xpcomFunctions.allocFunc)
438 return nsnull;
439 return xpcomFunctions.allocFunc(size);
442 XPCOM_API(void*)
443 NS_Realloc(void* ptr, PRSize size)
445 if (!xpcomFunctions.reallocFunc)
446 return nsnull;
447 return xpcomFunctions.reallocFunc(ptr, size);
450 XPCOM_API(void)
451 NS_Free(void* ptr)
453 if (xpcomFunctions.freeFunc)
454 xpcomFunctions.freeFunc(ptr);
457 XPCOM_API(void)
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);
465 XPCOM_API(void)
466 NS_LogInit()
468 if (xpcomFunctions.logInitFunc)
469 xpcomFunctions.logInitFunc();
472 XPCOM_API(void)
473 NS_LogTerm()
475 if (xpcomFunctions.logTermFunc)
476 xpcomFunctions.logTermFunc();
479 XPCOM_API(void)
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);
488 XPCOM_API(void)
489 NS_LogRelease(void *aPtr, nsrefcnt aNewRefCnt, const char *aTypeName)
491 if (xpcomFunctions.logReleaseFunc)
492 xpcomFunctions.logReleaseFunc(aPtr, aNewRefCnt, aTypeName);
495 XPCOM_API(void)
496 NS_LogCtor(void *aPtr, const char *aTypeName, PRUint32 aInstanceSize)
498 if (xpcomFunctions.logCtorFunc)
499 xpcomFunctions.logCtorFunc(aPtr, aTypeName, aInstanceSize);
502 XPCOM_API(void)
503 NS_LogDtor(void *aPtr, const char *aTypeName, PRUint32 aInstanceSize)
505 if (xpcomFunctions.logDtorFunc)
506 xpcomFunctions.logDtorFunc(aPtr, aTypeName, aInstanceSize);
509 XPCOM_API(void)
510 NS_LogCOMPtrAddRef(void *aCOMPtr, nsISupports *aObject)
512 if (xpcomFunctions.logCOMPtrAddRefFunc)
513 xpcomFunctions.logCOMPtrAddRefFunc(aCOMPtr, aObject);
516 XPCOM_API(void)
517 NS_LogCOMPtrRelease(void *aCOMPtr, nsISupports *aObject)
519 if (xpcomFunctions.logCOMPtrReleaseFunc)
520 xpcomFunctions.logCOMPtrReleaseFunc(aCOMPtr, aObject);
523 XPCOM_API(nsresult)
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);
533 XPCOM_API(void)
534 NS_DestroyXPTCallStub(nsISomeInterface* aStub)
536 if (xpcomFunctions.destroyXPTCallStubFunc)
537 xpcomFunctions.destroyXPTCallStubFunc(aStub);
540 XPCOM_API(nsresult)
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,
548 paramCount, params);
551 XPCOM_API(PRBool)
552 NS_CycleCollectorSuspect(nsISupports* obj)
554 if (!xpcomFunctions.cycleSuspectFunc)
555 return PR_FALSE;
557 return xpcomFunctions.cycleSuspectFunc(obj);
560 XPCOM_API(PRBool)
561 NS_CycleCollectorForget(nsISupports* obj)
563 if (!xpcomFunctions.cycleForgetFunc)
564 return PR_FALSE;
566 return xpcomFunctions.cycleForgetFunc(obj);