Bug 1839316: part 5) Guard the "fetchpriority" attribute behind a pref. r=kershaw...
[gecko.git] / widget / nsCUPSShim.cpp
blobd999a1be1770ed8da2277e1f8df4632d2de68321
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ex: set tabstop=8 softtabstop=2 shiftwidth=2 expandtab: */
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/. */
7 #include "nsDebug.h"
8 #include "nsString.h"
9 #include "nsCUPSShim.h"
10 #include "mozilla/ArrayUtils.h"
11 #include "mozilla/Logging.h"
12 #include "prlink.h"
14 #ifdef CUPS_SHIM_RUNTIME_LINK
16 mozilla::LazyLogModule gCupsLinkLog("CupsLink");
18 # define DEBUG_LOG(...) \
19 MOZ_LOG(gCupsLinkLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
21 // TODO: This is currently pointless as we always use the compile-time linked
22 // version of CUPS, but in the future this may become a configure option.
23 // We also cannot use NSPR's library suffix support, since that cannot handle
24 // version number suffixes.
25 # ifdef XP_MACOSX
26 static const char gCUPSLibraryName[] = "libcups.2.dylib";
27 # else
28 static const char gCUPSLibraryName[] = "libcups.so.2";
29 # endif
31 static bool LoadCupsFunc(PRLibrary* aLib, void** aDest, const char* const aName,
32 nsCUPSShim::Optional aOptional) {
33 *aDest = PR_FindSymbol(aLib, aName);
34 if (!*aDest) {
35 DEBUG_LOG("%s not found in CUPS library", aName);
36 return bool(aOptional);
38 return true;
41 nsCUPSShim::nsCUPSShim() {
42 mCupsLib = PR_LoadLibrary(gCUPSLibraryName);
43 if (!mCupsLib) {
44 DEBUG_LOG("CUPS library not found");
45 return;
48 bool success = true;
50 // This is a macro so that it could also load from libcups if we are
51 // configured to use it as a compile-time dependency.
53 // We try to load all functions even if some fail so that we get the debug log
54 // unconditionally.
55 # define CUPS_SHIM_LOAD(opt_, fn_) \
56 success |= \
57 LoadCupsFunc(mCupsLib, reinterpret_cast<void**>(&fn_), #fn_, opt_);
58 CUPS_SHIM_ALL_FUNCS(CUPS_SHIM_LOAD)
59 # undef CUPS_SHIM_LOAD
61 if (!success) {
62 # ifndef MOZ_TSAN
63 // With TSan, we cannot unload libcups once we have loaded it because
64 // TSan does not support unloading libraries that are matched from its
65 // suppression list. Hence we just keep the library loaded in TSan builds.
66 PR_UnloadLibrary(mCupsLib);
67 # endif
68 mCupsLib = nullptr;
69 return;
72 // Set mInitOkay only if all cups functions are loaded successfully.
73 mInitOkay = true;
76 #endif