2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2020-08-07 23:23:21 +03:00
|
|
|
/* ex: set tabstop=8 softtabstop=2 shiftwidth=2 expandtab: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-09-07 21:55:27 +04:00
|
|
|
|
|
|
|
#include "nsDebug.h"
|
2005-05-08 19:01:20 +04:00
|
|
|
#include "nsString.h"
|
2004-09-07 21:55:27 +04:00
|
|
|
#include "nsCUPSShim.h"
|
2014-01-30 22:26:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2004-09-07 21:55:27 +04:00
|
|
|
#include "prlink.h"
|
|
|
|
|
2020-08-11 03:25:18 +03:00
|
|
|
#ifdef CUPS_SHIM_RUNTIME_LINK
|
|
|
|
|
|
|
|
// TODO: This is currently pointless as we always use the compile-time linked
|
|
|
|
// version of CUPS, but in the future this may become a configure option.
|
|
|
|
// We also cannot use NSPR's library suffix support, since that cannot handle
|
|
|
|
// version number suffixes.
|
|
|
|
# ifdef XP_MACOSX
|
2020-07-17 20:34:20 +03:00
|
|
|
static const char gCUPSLibraryName[] = "libcups.2.dylib";
|
2020-08-11 03:25:18 +03:00
|
|
|
# else
|
2020-07-17 20:34:20 +03:00
|
|
|
static const char gCUPSLibraryName[] = "libcups.so.2";
|
2020-08-11 03:25:18 +03:00
|
|
|
# endif
|
2020-07-17 20:34:20 +03:00
|
|
|
|
2020-07-20 23:07:18 +03:00
|
|
|
template <typename FuncT>
|
2020-08-06 20:01:21 +03:00
|
|
|
static bool LoadCupsFunc(PRLibrary*& lib, FuncT*& dest,
|
2020-07-20 23:07:18 +03:00
|
|
|
const char* const name) {
|
|
|
|
dest = (FuncT*)PR_FindSymbol(lib, name);
|
|
|
|
if (MOZ_UNLIKELY(!dest)) {
|
2020-08-11 03:25:18 +03:00
|
|
|
# ifdef DEBUG
|
2020-07-20 23:07:18 +03:00
|
|
|
nsAutoCString msg(name);
|
|
|
|
msg.AppendLiteral(" not found in CUPS library");
|
|
|
|
NS_WARNING(msg.get());
|
2020-08-11 03:25:18 +03:00
|
|
|
# endif
|
|
|
|
# ifndef MOZ_TSAN
|
2020-08-06 20:01:21 +03:00
|
|
|
// With TSan, we cannot unload libcups once we have loaded it because
|
|
|
|
// TSan does not support unloading libraries that are matched from its
|
|
|
|
// suppression list. Hence we just keep the library loaded in TSan builds.
|
|
|
|
PR_UnloadLibrary(lib);
|
2020-08-11 03:25:18 +03:00
|
|
|
# endif
|
2020-08-06 20:01:21 +03:00
|
|
|
lib = nullptr;
|
2020-07-20 23:07:18 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:56:13 +03:00
|
|
|
nsCUPSShim::nsCUPSShim() {
|
2020-07-20 23:07:18 +03:00
|
|
|
mCupsLib = PR_LoadLibrary(gCUPSLibraryName);
|
|
|
|
if (!mCupsLib) {
|
2020-11-04 21:56:13 +03:00
|
|
|
return;
|
2020-07-20 23:07:18 +03:00
|
|
|
}
|
2020-02-04 17:38:49 +03:00
|
|
|
|
2020-11-04 21:56:13 +03:00
|
|
|
// This is a macro so that it could also load from libcups if we are
|
|
|
|
// configured to use it as a compile-time dependency.
|
2020-08-11 03:25:18 +03:00
|
|
|
# define CUPS_SHIM_LOAD(NAME) \
|
2020-11-04 21:56:13 +03:00
|
|
|
if (!LoadCupsFunc(mCupsLib, NAME, #NAME)) return;
|
2020-08-06 20:01:21 +03:00
|
|
|
CUPS_SHIM_ALL_FUNCS(CUPS_SHIM_LOAD)
|
2020-08-11 03:25:18 +03:00
|
|
|
# undef CUPS_SHIM_LOAD
|
|
|
|
|
2020-11-04 21:56:13 +03:00
|
|
|
// Set mInitOkay only if all cups functions are loaded successfully.
|
|
|
|
mInitOkay = true;
|
2004-09-07 21:55:27 +04:00
|
|
|
}
|
2020-08-11 03:25:18 +03:00
|
|
|
|
|
|
|
#endif
|