xpcom/glue/standalone/nsXPCOMGlue.cpp

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:7a021afc301e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sw=4 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "nsXPCOMGlue.h"
8
9 #include "nspr.h"
10 #include "nsDebug.h"
11 #include "nsIServiceManager.h"
12 #include "nsXPCOMPrivate.h"
13 #include "nsCOMPtr.h"
14 #include <stdlib.h>
15 #include <stdio.h>
16
17 #include "mozilla/FileUtils.h"
18
19 using namespace mozilla;
20
21 #define XPCOM_DEPENDENT_LIBS_LIST "dependentlibs.list"
22
23 static XPCOMFunctions xpcomFunctions;
24 static bool do_preload = false;
25
26 #if defined(XP_WIN)
27 #define READ_TEXTMODE L"rt"
28 #else
29 #define READ_TEXTMODE "r"
30 #endif
31
32 #if defined(SUNOS4) || defined(NEXTSTEP) || \
33 defined(XP_DARWIN) || \
34 (defined(OPENBSD) || defined(NETBSD)) && !defined(__ELF__)
35 #define LEADING_UNDERSCORE "_"
36 #else
37 #define LEADING_UNDERSCORE
38 #endif
39
40 #if defined(XP_WIN)
41 #include <windows.h>
42 #include <mbstring.h>
43 #define snprintf _snprintf
44
45 typedef HINSTANCE LibHandleType;
46
47 static LibHandleType
48 GetLibHandle(pathstr_t aDependentLib)
49 {
50 LibHandleType libHandle =
51 LoadLibraryExW(aDependentLib, nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
52
53 if (!libHandle) {
54 DWORD err = GetLastError();
55 #ifdef DEBUG
56 LPVOID lpMsgBuf;
57 FormatMessage(
58 FORMAT_MESSAGE_ALLOCATE_BUFFER |
59 FORMAT_MESSAGE_FROM_SYSTEM |
60 FORMAT_MESSAGE_IGNORE_INSERTS,
61 nullptr,
62 err,
63 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
64 (LPTSTR) &lpMsgBuf,
65 0,
66 nullptr
67 );
68 wprintf(L"Error loading %ls: %s\n", aDependentLib, lpMsgBuf);
69 LocalFree(lpMsgBuf);
70 #endif
71 }
72
73 return libHandle;
74 }
75
76 static NSFuncPtr
77 GetSymbol(LibHandleType aLibHandle, const char *aSymbol)
78 {
79 return (NSFuncPtr) GetProcAddress(aLibHandle, aSymbol);
80 }
81
82 static void
83 CloseLibHandle(LibHandleType aLibHandle)
84 {
85 FreeLibrary(aLibHandle);
86 }
87
88 #elif defined(XP_MACOSX)
89 #include <mach-o/dyld.h>
90
91 typedef const mach_header *LibHandleType;
92
93 static LibHandleType
94 GetLibHandle(pathstr_t aDependentLib)
95 {
96 LibHandleType libHandle = NSAddImage(aDependentLib,
97 NSADDIMAGE_OPTION_RETURN_ON_ERROR |
98 NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME);
99 if (!libHandle) {
100 NSLinkEditErrors linkEditError;
101 int errorNum;
102 const char *errorString;
103 const char *fileName;
104 NSLinkEditError(&linkEditError, &errorNum, &fileName, &errorString);
105 fprintf(stderr, "XPCOMGlueLoad error %d:%d for file %s:\n%s\n",
106 linkEditError, errorNum, fileName, errorString);
107 }
108 return libHandle;
109 }
110
111 static NSFuncPtr
112 GetSymbol(LibHandleType aLibHandle, const char *aSymbol)
113 {
114 // Try to use |NSLookupSymbolInImage| since it is faster than searching
115 // the global symbol table. If we couldn't get a mach_header pointer
116 // for the XPCOM dylib, then use |NSLookupAndBindSymbol| to search the
117 // global symbol table (this shouldn't normally happen, unless the user
118 // has called XPCOMGlueStartup(".") and relies on libxpcom.dylib
119 // already being loaded).
120 NSSymbol sym = nullptr;
121 if (aLibHandle) {
122 sym = NSLookupSymbolInImage(aLibHandle, aSymbol,
123 NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
124 NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
125 } else {
126 if (NSIsSymbolNameDefined(aSymbol))
127 sym = NSLookupAndBindSymbol(aSymbol);
128 }
129 if (!sym)
130 return nullptr;
131
132 return (NSFuncPtr) NSAddressOfSymbol(sym);
133 }
134
135 static void
136 CloseLibHandle(LibHandleType aLibHandle)
137 {
138 // we cannot unload dylibs on OS X
139 }
140
141 #else
142 #include <dlfcn.h>
143
144 #if defined(MOZ_LINKER) && !defined(ANDROID)
145 extern "C" {
146 NS_HIDDEN __typeof(dlopen) __wrap_dlopen;
147 NS_HIDDEN __typeof(dlsym) __wrap_dlsym;
148 NS_HIDDEN __typeof(dlclose) __wrap_dlclose;
149 }
150
151 #define dlopen __wrap_dlopen
152 #define dlsym __wrap_dlsym
153 #define dlclose __wrap_dlclose
154 #endif
155
156 #ifdef NS_TRACE_MALLOC
157 extern "C" {
158 NS_EXPORT_(__ptr_t) __libc_malloc(size_t);
159 NS_EXPORT_(__ptr_t) __libc_calloc(size_t, size_t);
160 NS_EXPORT_(__ptr_t) __libc_realloc(__ptr_t, size_t);
161 NS_EXPORT_(void) __libc_free(__ptr_t);
162 NS_EXPORT_(__ptr_t) __libc_memalign(size_t, size_t);
163 NS_EXPORT_(__ptr_t) __libc_valloc(size_t);
164 }
165
166 static __ptr_t (*_malloc)(size_t) = __libc_malloc;
167 static __ptr_t (*_calloc)(size_t, size_t) = __libc_calloc;
168 static __ptr_t (*_realloc)(__ptr_t, size_t) = __libc_realloc;
169 static void (*_free)(__ptr_t) = __libc_free;
170 static __ptr_t (*_memalign)(size_t, size_t) = __libc_memalign;
171 static __ptr_t (*_valloc)(size_t) = __libc_valloc;
172
173 NS_EXPORT_(__ptr_t) malloc(size_t size)
174 {
175 return _malloc(size);
176 }
177
178 NS_EXPORT_(__ptr_t) calloc(size_t nmemb, size_t size)
179 {
180 return _calloc(nmemb, size);
181 }
182
183 NS_EXPORT_(__ptr_t) realloc(__ptr_t ptr, size_t size)
184 {
185 return _realloc(ptr, size);
186 }
187
188 NS_EXPORT_(void) free(__ptr_t ptr)
189 {
190 _free(ptr);
191 }
192
193 NS_EXPORT_(void) cfree(__ptr_t ptr)
194 {
195 _free(ptr);
196 }
197
198 NS_EXPORT_(__ptr_t) memalign(size_t boundary, size_t size)
199 {
200 return _memalign(boundary, size);
201 }
202
203 NS_EXPORT_(int)
204 posix_memalign(void **memptr, size_t alignment, size_t size)
205 {
206 __ptr_t ptr = _memalign(alignment, size);
207 if (!ptr)
208 return ENOMEM;
209 *memptr = ptr;
210 return 0;
211 }
212
213 NS_EXPORT_(__ptr_t) valloc(size_t size)
214 {
215 return _valloc(size);
216 }
217 #endif /* NS_TRACE_MALLOC */
218
219 typedef void *LibHandleType;
220
221 static LibHandleType
222 GetLibHandle(pathstr_t aDependentLib)
223 {
224 LibHandleType libHandle = dlopen(aDependentLib, RTLD_GLOBAL | RTLD_LAZY);
225 if (!libHandle) {
226 fprintf(stderr, "XPCOMGlueLoad error for file %s:\n%s\n", aDependentLib, dlerror());
227 }
228 return libHandle;
229 }
230
231 static NSFuncPtr
232 GetSymbol(LibHandleType aLibHandle, const char *aSymbol)
233 {
234 return (NSFuncPtr) dlsym(aLibHandle, aSymbol);
235 }
236
237 static void
238 CloseLibHandle(LibHandleType aLibHandle)
239 {
240 dlclose(aLibHandle);
241 }
242 #endif
243
244 struct DependentLib
245 {
246 LibHandleType libHandle;
247 DependentLib *next;
248 };
249
250 static DependentLib *sTop;
251
252 static void
253 AppendDependentLib(LibHandleType libHandle)
254 {
255 DependentLib *d = new DependentLib;
256 if (!d)
257 return;
258
259 d->next = sTop;
260 d->libHandle = libHandle;
261
262 sTop = d;
263 }
264
265 static bool
266 ReadDependentCB(pathstr_t aDependentLib, bool do_preload)
267 {
268 if (do_preload) {
269 ReadAheadLib(aDependentLib);
270 }
271 LibHandleType libHandle = GetLibHandle(aDependentLib);
272 if (libHandle) {
273 AppendDependentLib(libHandle);
274 }
275
276 return libHandle;
277 }
278
279 #ifdef XP_WIN
280 static bool
281 ReadDependentCB(const char *aDependentLib, bool do_preload)
282 {
283 wchar_t wideDependentLib[MAX_PATH];
284 MultiByteToWideChar(CP_UTF8, 0, aDependentLib, -1, wideDependentLib, MAX_PATH);
285 return ReadDependentCB(wideDependentLib, do_preload);
286 }
287
288 inline FILE *TS_tfopen (const char *path, const wchar_t *mode)
289 {
290 wchar_t wPath[MAX_PATH];
291 MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, MAX_PATH);
292 return _wfopen(wPath, mode);
293 }
294 #else
295 inline FILE *TS_tfopen (const char *path, const char *mode)
296 {
297 return fopen(path, mode);
298 }
299 #endif
300
301 /* RAII wrapper for FILE descriptors */
302 struct ScopedCloseFileTraits
303 {
304 typedef FILE *type;
305 static type empty() { return nullptr; }
306 static void release(type f) {
307 if (f) {
308 fclose(f);
309 }
310 }
311 };
312 typedef Scoped<ScopedCloseFileTraits> ScopedCloseFile;
313
314 static void
315 XPCOMGlueUnload()
316 {
317 #if !defined(XP_WIN) && !defined(XP_MACOSX) && defined(NS_TRACE_MALLOC)
318 if (sTop) {
319 _malloc = __libc_malloc;
320 _calloc = __libc_calloc;
321 _realloc = __libc_realloc;
322 _free = __libc_free;
323 _memalign = __libc_memalign;
324 _valloc = __libc_valloc;
325 }
326 #endif
327
328 while (sTop) {
329 CloseLibHandle(sTop->libHandle);
330
331 DependentLib *temp = sTop;
332 sTop = sTop->next;
333
334 delete temp;
335 }
336 }
337
338 #if defined(XP_WIN)
339 // like strpbrk but finds the *last* char, not the first
340 static const char*
341 ns_strrpbrk(const char *string, const char *strCharSet)
342 {
343 const char *found = nullptr;
344 for (; *string; ++string) {
345 for (const char *search = strCharSet; *search; ++search) {
346 if (*search == *string) {
347 found = string;
348 // Since we're looking for the last char, we save "found"
349 // until we're at the end of the string.
350 }
351 }
352 }
353
354 return found;
355 }
356 #endif
357
358 static GetFrozenFunctionsFunc
359 XPCOMGlueLoad(const char *xpcomFile)
360 {
361 char xpcomDir[MAXPATHLEN];
362 #if defined(XP_WIN)
363 const char *lastSlash = ns_strrpbrk(xpcomFile, "/\\");
364 #else
365 const char *lastSlash = strrchr(xpcomFile, '/');
366 #endif
367 char *cursor;
368 if (lastSlash) {
369 size_t len = size_t(lastSlash - xpcomFile);
370
371 if (len > MAXPATHLEN - sizeof(XPCOM_FILE_PATH_SEPARATOR
372 XPCOM_DEPENDENT_LIBS_LIST)) {
373 return nullptr;
374 }
375 memcpy(xpcomDir, xpcomFile, len);
376 strcpy(xpcomDir + len, XPCOM_FILE_PATH_SEPARATOR
377 XPCOM_DEPENDENT_LIBS_LIST);
378 cursor = xpcomDir + len + 1;
379 } else {
380 strcpy(xpcomDir, XPCOM_DEPENDENT_LIBS_LIST);
381 cursor = xpcomDir;
382 }
383
384 if (getenv("MOZ_RUN_GTEST")) {
385 strcat(xpcomDir, ".gtest");
386 }
387
388 ScopedCloseFile flist;
389 flist = TS_tfopen(xpcomDir, READ_TEXTMODE);
390 if (!flist) {
391 return nullptr;
392 }
393
394 *cursor = '\0';
395
396 char buffer[MAXPATHLEN];
397
398 while (fgets(buffer, sizeof(buffer), flist)) {
399 int l = strlen(buffer);
400
401 // ignore empty lines and comments
402 if (l == 0 || *buffer == '#') {
403 continue;
404 }
405
406 // cut the trailing newline, if present
407 if (buffer[l - 1] == '\n')
408 buffer[l - 1] = '\0';
409
410 if (l + size_t(cursor - xpcomDir) > MAXPATHLEN) {
411 return nullptr;
412 }
413
414 strcpy(cursor, buffer);
415 if (!ReadDependentCB(xpcomDir, do_preload)) {
416 XPCOMGlueUnload();
417 return nullptr;
418 }
419 }
420
421 GetFrozenFunctionsFunc sym =
422 (GetFrozenFunctionsFunc) GetSymbol(sTop->libHandle, LEADING_UNDERSCORE "NS_GetFrozenFunctions");
423
424 if (!sym) { // No symbol found.
425 XPCOMGlueUnload();
426 return nullptr;
427 }
428
429 #if !defined(XP_WIN) && !defined(XP_MACOSX) && defined(NS_TRACE_MALLOC)
430 _malloc = (__ptr_t(*)(size_t)) GetSymbol(sTop->libHandle, "malloc");
431 _calloc = (__ptr_t(*)(size_t, size_t)) GetSymbol(sTop->libHandle, "calloc");
432 _realloc = (__ptr_t(*)(__ptr_t, size_t)) GetSymbol(sTop->libHandle, "realloc");
433 _free = (void(*)(__ptr_t)) GetSymbol(sTop->libHandle, "free");
434 _memalign = (__ptr_t(*)(size_t, size_t)) GetSymbol(sTop->libHandle, "memalign");
435 _valloc = (__ptr_t(*)(size_t)) GetSymbol(sTop->libHandle, "valloc");
436 #endif
437
438 return sym;
439 }
440
441 nsresult
442 XPCOMGlueLoadXULFunctions(const nsDynamicFunctionLoad *symbols)
443 {
444 // We don't null-check sXULLibHandle because this might work even
445 // if it is null (same as RTLD_DEFAULT)
446
447 nsresult rv = NS_OK;
448 while (symbols->functionName) {
449 char buffer[512];
450 snprintf(buffer, sizeof(buffer),
451 LEADING_UNDERSCORE "%s", symbols->functionName);
452
453 *symbols->function = (NSFuncPtr) GetSymbol(sTop->libHandle, buffer);
454 if (!*symbols->function)
455 rv = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
456
457 ++symbols;
458 }
459 return rv;
460 }
461
462 void XPCOMGlueEnablePreload()
463 {
464 do_preload = true;
465 }
466
467 nsresult XPCOMGlueStartup(const char* xpcomFile)
468 {
469 xpcomFunctions.version = XPCOM_GLUE_VERSION;
470 xpcomFunctions.size = sizeof(XPCOMFunctions);
471
472 if (!xpcomFile)
473 xpcomFile = XPCOM_DLL;
474
475 GetFrozenFunctionsFunc func = XPCOMGlueLoad(xpcomFile);
476 if (!func)
477 return NS_ERROR_NOT_AVAILABLE;
478
479 nsresult rv = (*func)(&xpcomFunctions, nullptr);
480 if (NS_FAILED(rv)) {
481 XPCOMGlueUnload();
482 return rv;
483 }
484
485 return NS_OK;
486 }
487
488 XPCOM_API(nsresult)
489 NS_InitXPCOM2(nsIServiceManager* *result,
490 nsIFile* binDirectory,
491 nsIDirectoryServiceProvider* appFileLocationProvider)
492 {
493 if (!xpcomFunctions.init)
494 return NS_ERROR_NOT_INITIALIZED;
495 return xpcomFunctions.init(result, binDirectory, appFileLocationProvider);
496 }
497
498 XPCOM_API(nsresult)
499 NS_ShutdownXPCOM(nsIServiceManager* servMgr)
500 {
501 if (!xpcomFunctions.shutdown)
502 return NS_ERROR_NOT_INITIALIZED;
503 return xpcomFunctions.shutdown(servMgr);
504 }
505
506 XPCOM_API(nsresult)
507 NS_GetServiceManager(nsIServiceManager* *result)
508 {
509 if (!xpcomFunctions.getServiceManager)
510 return NS_ERROR_NOT_INITIALIZED;
511 return xpcomFunctions.getServiceManager(result);
512 }
513
514 XPCOM_API(nsresult)
515 NS_GetComponentManager(nsIComponentManager* *result)
516 {
517 if (!xpcomFunctions.getComponentManager)
518 return NS_ERROR_NOT_INITIALIZED;
519 return xpcomFunctions.getComponentManager(result);
520 }
521
522 XPCOM_API(nsresult)
523 NS_GetComponentRegistrar(nsIComponentRegistrar* *result)
524 {
525 if (!xpcomFunctions.getComponentRegistrar)
526 return NS_ERROR_NOT_INITIALIZED;
527 return xpcomFunctions.getComponentRegistrar(result);
528 }
529
530 XPCOM_API(nsresult)
531 NS_GetMemoryManager(nsIMemory* *result)
532 {
533 if (!xpcomFunctions.getMemoryManager)
534 return NS_ERROR_NOT_INITIALIZED;
535 return xpcomFunctions.getMemoryManager(result);
536 }
537
538 XPCOM_API(nsresult)
539 NS_NewLocalFile(const nsAString &path, bool followLinks, nsIFile* *result)
540 {
541 if (!xpcomFunctions.newLocalFile)
542 return NS_ERROR_NOT_INITIALIZED;
543 return xpcomFunctions.newLocalFile(path, followLinks, result);
544 }
545
546 XPCOM_API(nsresult)
547 NS_NewNativeLocalFile(const nsACString &path, bool followLinks, nsIFile* *result)
548 {
549 if (!xpcomFunctions.newNativeLocalFile)
550 return NS_ERROR_NOT_INITIALIZED;
551 return xpcomFunctions.newNativeLocalFile(path, followLinks, result);
552 }
553
554 XPCOM_API(nsresult)
555 NS_GetDebug(nsIDebug* *result)
556 {
557 if (!xpcomFunctions.getDebug)
558 return NS_ERROR_NOT_INITIALIZED;
559 return xpcomFunctions.getDebug(result);
560 }
561
562
563 XPCOM_API(nsresult)
564 NS_StringContainerInit(nsStringContainer &aStr)
565 {
566 if (!xpcomFunctions.stringContainerInit)
567 return NS_ERROR_NOT_INITIALIZED;
568 return xpcomFunctions.stringContainerInit(aStr);
569 }
570
571 XPCOM_API(nsresult)
572 NS_StringContainerInit2(nsStringContainer &aStr,
573 const char16_t *aData,
574 uint32_t aDataLength,
575 uint32_t aFlags)
576 {
577 if (!xpcomFunctions.stringContainerInit2)
578 return NS_ERROR_NOT_INITIALIZED;
579 return xpcomFunctions.stringContainerInit2(aStr, aData, aDataLength, aFlags);
580 }
581
582 XPCOM_API(void)
583 NS_StringContainerFinish(nsStringContainer &aStr)
584 {
585 if (xpcomFunctions.stringContainerFinish)
586 xpcomFunctions.stringContainerFinish(aStr);
587 }
588
589 XPCOM_API(uint32_t)
590 NS_StringGetData(const nsAString &aStr, const char16_t **aBuf, bool *aTerm)
591 {
592 if (!xpcomFunctions.stringGetData) {
593 *aBuf = nullptr;
594 return 0;
595 }
596 return xpcomFunctions.stringGetData(aStr, aBuf, aTerm);
597 }
598
599 XPCOM_API(uint32_t)
600 NS_StringGetMutableData(nsAString &aStr, uint32_t aLen, char16_t **aBuf)
601 {
602 if (!xpcomFunctions.stringGetMutableData) {
603 *aBuf = nullptr;
604 return 0;
605 }
606 return xpcomFunctions.stringGetMutableData(aStr, aLen, aBuf);
607 }
608
609 XPCOM_API(char16_t*)
610 NS_StringCloneData(const nsAString &aStr)
611 {
612 if (!xpcomFunctions.stringCloneData)
613 return nullptr;
614 return xpcomFunctions.stringCloneData(aStr);
615 }
616
617 XPCOM_API(nsresult)
618 NS_StringSetData(nsAString &aStr, const char16_t *aBuf, uint32_t aCount)
619 {
620 if (!xpcomFunctions.stringSetData)
621 return NS_ERROR_NOT_INITIALIZED;
622
623 return xpcomFunctions.stringSetData(aStr, aBuf, aCount);
624 }
625
626 XPCOM_API(nsresult)
627 NS_StringSetDataRange(nsAString &aStr, uint32_t aCutStart, uint32_t aCutLength,
628 const char16_t *aBuf, uint32_t aCount)
629 {
630 if (!xpcomFunctions.stringSetDataRange)
631 return NS_ERROR_NOT_INITIALIZED;
632 return xpcomFunctions.stringSetDataRange(aStr, aCutStart, aCutLength, aBuf, aCount);
633 }
634
635 XPCOM_API(nsresult)
636 NS_StringCopy(nsAString &aDest, const nsAString &aSrc)
637 {
638 if (!xpcomFunctions.stringCopy)
639 return NS_ERROR_NOT_INITIALIZED;
640 return xpcomFunctions.stringCopy(aDest, aSrc);
641 }
642
643 XPCOM_API(void)
644 NS_StringSetIsVoid(nsAString &aStr, const bool aIsVoid)
645 {
646 if (xpcomFunctions.stringSetIsVoid)
647 xpcomFunctions.stringSetIsVoid(aStr, aIsVoid);
648 }
649
650 XPCOM_API(bool)
651 NS_StringGetIsVoid(const nsAString &aStr)
652 {
653 if (!xpcomFunctions.stringGetIsVoid)
654 return false;
655 return xpcomFunctions.stringGetIsVoid(aStr);
656 }
657
658 XPCOM_API(nsresult)
659 NS_CStringContainerInit(nsCStringContainer &aStr)
660 {
661 if (!xpcomFunctions.cstringContainerInit)
662 return NS_ERROR_NOT_INITIALIZED;
663 return xpcomFunctions.cstringContainerInit(aStr);
664 }
665
666 XPCOM_API(nsresult)
667 NS_CStringContainerInit2(nsCStringContainer &aStr,
668 const char *aData,
669 uint32_t aDataLength,
670 uint32_t aFlags)
671 {
672 if (!xpcomFunctions.cstringContainerInit2)
673 return NS_ERROR_NOT_INITIALIZED;
674 return xpcomFunctions.cstringContainerInit2(aStr, aData, aDataLength, aFlags);
675 }
676
677 XPCOM_API(void)
678 NS_CStringContainerFinish(nsCStringContainer &aStr)
679 {
680 if (xpcomFunctions.cstringContainerFinish)
681 xpcomFunctions.cstringContainerFinish(aStr);
682 }
683
684 XPCOM_API(uint32_t)
685 NS_CStringGetData(const nsACString &aStr, const char **aBuf, bool *aTerm)
686 {
687 if (!xpcomFunctions.cstringGetData) {
688 *aBuf = nullptr;
689 return 0;
690 }
691 return xpcomFunctions.cstringGetData(aStr, aBuf, aTerm);
692 }
693
694 XPCOM_API(uint32_t)
695 NS_CStringGetMutableData(nsACString &aStr, uint32_t aLen, char **aBuf)
696 {
697 if (!xpcomFunctions.cstringGetMutableData) {
698 *aBuf = nullptr;
699 return 0;
700 }
701 return xpcomFunctions.cstringGetMutableData(aStr, aLen, aBuf);
702 }
703
704 XPCOM_API(char*)
705 NS_CStringCloneData(const nsACString &aStr)
706 {
707 if (!xpcomFunctions.cstringCloneData)
708 return nullptr;
709 return xpcomFunctions.cstringCloneData(aStr);
710 }
711
712 XPCOM_API(nsresult)
713 NS_CStringSetData(nsACString &aStr, const char *aBuf, uint32_t aCount)
714 {
715 if (!xpcomFunctions.cstringSetData)
716 return NS_ERROR_NOT_INITIALIZED;
717 return xpcomFunctions.cstringSetData(aStr, aBuf, aCount);
718 }
719
720 XPCOM_API(nsresult)
721 NS_CStringSetDataRange(nsACString &aStr, uint32_t aCutStart, uint32_t aCutLength,
722 const char *aBuf, uint32_t aCount)
723 {
724 if (!xpcomFunctions.cstringSetDataRange)
725 return NS_ERROR_NOT_INITIALIZED;
726 return xpcomFunctions.cstringSetDataRange(aStr, aCutStart, aCutLength, aBuf, aCount);
727 }
728
729 XPCOM_API(nsresult)
730 NS_CStringCopy(nsACString &aDest, const nsACString &aSrc)
731 {
732 if (!xpcomFunctions.cstringCopy)
733 return NS_ERROR_NOT_INITIALIZED;
734 return xpcomFunctions.cstringCopy(aDest, aSrc);
735 }
736
737 XPCOM_API(void)
738 NS_CStringSetIsVoid(nsACString &aStr, const bool aIsVoid)
739 {
740 if (xpcomFunctions.cstringSetIsVoid)
741 xpcomFunctions.cstringSetIsVoid(aStr, aIsVoid);
742 }
743
744 XPCOM_API(bool)
745 NS_CStringGetIsVoid(const nsACString &aStr)
746 {
747 if (!xpcomFunctions.cstringGetIsVoid)
748 return false;
749 return xpcomFunctions.cstringGetIsVoid(aStr);
750 }
751
752 XPCOM_API(nsresult)
753 NS_CStringToUTF16(const nsACString &aSrc, nsCStringEncoding aSrcEncoding, nsAString &aDest)
754 {
755 if (!xpcomFunctions.cstringToUTF16)
756 return NS_ERROR_NOT_INITIALIZED;
757 return xpcomFunctions.cstringToUTF16(aSrc, aSrcEncoding, aDest);
758 }
759
760 XPCOM_API(nsresult)
761 NS_UTF16ToCString(const nsAString &aSrc, nsCStringEncoding aDestEncoding, nsACString &aDest)
762 {
763 if (!xpcomFunctions.utf16ToCString)
764 return NS_ERROR_NOT_INITIALIZED;
765 return xpcomFunctions.utf16ToCString(aSrc, aDestEncoding, aDest);
766 }
767
768 XPCOM_API(void*)
769 NS_Alloc(size_t size)
770 {
771 if (!xpcomFunctions.allocFunc)
772 return nullptr;
773 return xpcomFunctions.allocFunc(size);
774 }
775
776 XPCOM_API(void*)
777 NS_Realloc(void* ptr, size_t size)
778 {
779 if (!xpcomFunctions.reallocFunc)
780 return nullptr;
781 return xpcomFunctions.reallocFunc(ptr, size);
782 }
783
784 XPCOM_API(void)
785 NS_Free(void* ptr)
786 {
787 if (xpcomFunctions.freeFunc)
788 xpcomFunctions.freeFunc(ptr);
789 }
790
791 XPCOM_API(void)
792 NS_DebugBreak(uint32_t aSeverity, const char *aStr, const char *aExpr,
793 const char *aFile, int32_t aLine)
794 {
795 if (xpcomFunctions.debugBreakFunc)
796 xpcomFunctions.debugBreakFunc(aSeverity, aStr, aExpr, aFile, aLine);
797 }
798
799 XPCOM_API(void)
800 NS_LogInit()
801 {
802 if (xpcomFunctions.logInitFunc)
803 xpcomFunctions.logInitFunc();
804 }
805
806 XPCOM_API(void)
807 NS_LogTerm()
808 {
809 if (xpcomFunctions.logTermFunc)
810 xpcomFunctions.logTermFunc();
811 }
812
813 XPCOM_API(void)
814 NS_LogAddRef(void *aPtr, nsrefcnt aNewRefCnt,
815 const char *aTypeName, uint32_t aInstanceSize)
816 {
817 if (xpcomFunctions.logAddRefFunc)
818 xpcomFunctions.logAddRefFunc(aPtr, aNewRefCnt,
819 aTypeName, aInstanceSize);
820 }
821
822 XPCOM_API(void)
823 NS_LogRelease(void *aPtr, nsrefcnt aNewRefCnt, const char *aTypeName)
824 {
825 if (xpcomFunctions.logReleaseFunc)
826 xpcomFunctions.logReleaseFunc(aPtr, aNewRefCnt, aTypeName);
827 }
828
829 XPCOM_API(void)
830 NS_LogCtor(void *aPtr, const char *aTypeName, uint32_t aInstanceSize)
831 {
832 if (xpcomFunctions.logCtorFunc)
833 xpcomFunctions.logCtorFunc(aPtr, aTypeName, aInstanceSize);
834 }
835
836 XPCOM_API(void)
837 NS_LogDtor(void *aPtr, const char *aTypeName, uint32_t aInstanceSize)
838 {
839 if (xpcomFunctions.logDtorFunc)
840 xpcomFunctions.logDtorFunc(aPtr, aTypeName, aInstanceSize);
841 }
842
843 XPCOM_API(void)
844 NS_LogCOMPtrAddRef(void *aCOMPtr, nsISupports *aObject)
845 {
846 if (xpcomFunctions.logCOMPtrAddRefFunc)
847 xpcomFunctions.logCOMPtrAddRefFunc(aCOMPtr, aObject);
848 }
849
850 XPCOM_API(void)
851 NS_LogCOMPtrRelease(void *aCOMPtr, nsISupports *aObject)
852 {
853 if (xpcomFunctions.logCOMPtrReleaseFunc)
854 xpcomFunctions.logCOMPtrReleaseFunc(aCOMPtr, aObject);
855 }
856
857 XPCOM_API(nsresult)
858 NS_GetXPTCallStub(REFNSIID aIID, nsIXPTCProxy* aOuter,
859 nsISomeInterface* *aStub)
860 {
861 if (!xpcomFunctions.getXPTCallStubFunc)
862 return NS_ERROR_NOT_INITIALIZED;
863
864 return xpcomFunctions.getXPTCallStubFunc(aIID, aOuter, aStub);
865 }
866
867 XPCOM_API(void)
868 NS_DestroyXPTCallStub(nsISomeInterface* aStub)
869 {
870 if (xpcomFunctions.destroyXPTCallStubFunc)
871 xpcomFunctions.destroyXPTCallStubFunc(aStub);
872 }
873
874 XPCOM_API(nsresult)
875 NS_InvokeByIndex(nsISupports* that, uint32_t methodIndex,
876 uint32_t paramCount, nsXPTCVariant* params)
877 {
878 if (!xpcomFunctions.invokeByIndexFunc)
879 return NS_ERROR_NOT_INITIALIZED;
880
881 return xpcomFunctions.invokeByIndexFunc(that, methodIndex,
882 paramCount, params);
883 }
884
885 XPCOM_API(bool)
886 NS_CycleCollectorSuspect(nsISupports* obj)
887 {
888 if (!xpcomFunctions.cycleSuspectFunc)
889 return false;
890
891 return xpcomFunctions.cycleSuspectFunc(obj);
892 }
893
894 XPCOM_API(bool)
895 NS_CycleCollectorForget(nsISupports* obj)
896 {
897 if (!xpcomFunctions.cycleForgetFunc)
898 return false;
899
900 return xpcomFunctions.cycleForgetFunc(obj);
901 }
902
903 XPCOM_API(nsPurpleBufferEntry*)
904 NS_CycleCollectorSuspect2(void* obj, nsCycleCollectionParticipant *p)
905 {
906 if (!xpcomFunctions.cycleSuspect2Func)
907 return nullptr;
908
909 return xpcomFunctions.cycleSuspect2Func(obj, p);
910 }
911
912 XPCOM_API(void)
913 NS_CycleCollectorSuspect3(void* obj, nsCycleCollectionParticipant *p,
914 nsCycleCollectingAutoRefCnt* aRefCnt,
915 bool* aShouldDelete)
916 {
917 if (xpcomFunctions.cycleSuspect3Func)
918 xpcomFunctions.cycleSuspect3Func(obj, p, aRefCnt, aShouldDelete);
919 }
920
921 XPCOM_API(bool)
922 NS_CycleCollectorForget2(nsPurpleBufferEntry* e)
923 {
924 if (!xpcomFunctions.cycleForget2Func)
925 return false;
926
927 return xpcomFunctions.cycleForget2Func(e);
928 }

mercurial