2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2004-09-07 21:55:27 +04:00
|
|
|
/* ex: set tabstop=8 softtabstop=4 shiftwidth=4 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-07-17 20:34:20 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// TODO: On OS X we are guaranteed to have CUPS, so it would be nice to just
|
|
|
|
// assign the members from the header directly instead of dlopen'ing.
|
|
|
|
// Alternatively, we could just do some #define's on OS X, but we don't use
|
|
|
|
// CUPS all that much, so there really isn't too much overhead in storing this
|
|
|
|
// table of functions even on OS X.
|
|
|
|
static const char gCUPSLibraryName[] = "libcups.2.dylib";
|
|
|
|
#else
|
|
|
|
static const char gCUPSLibraryName[] = "libcups.so.2";
|
|
|
|
#endif
|
|
|
|
|
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)) {
|
2005-05-08 19:01:20 +04: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());
|
2005-05-08 19:01:20 +04:00
|
|
|
#endif
|
2020-08-06 20:01:21 +03:00
|
|
|
#ifndef MOZ_TSAN
|
|
|
|
// 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);
|
|
|
|
#endif
|
|
|
|
lib = nullptr;
|
2020-07-20 23:07:18 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsCUPSShim::Init() {
|
|
|
|
MOZ_ASSERT(!mInited);
|
|
|
|
mCupsLib = PR_LoadLibrary(gCUPSLibraryName);
|
|
|
|
if (!mCupsLib) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-04 17:38:49 +03:00
|
|
|
|
2020-08-06 20:01:21 +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.
|
|
|
|
#define CUPS_SHIM_LOAD(NAME) \
|
|
|
|
if (!LoadCupsFunc(mCupsLib, NAME, #NAME)) return false;
|
|
|
|
CUPS_SHIM_ALL_FUNCS(CUPS_SHIM_LOAD)
|
|
|
|
#undef CUPS_SHIM_LOAD
|
2020-07-20 23:07:18 +03:00
|
|
|
mInited = true;
|
2011-10-03 11:56:21 +04:00
|
|
|
return true;
|
2004-09-07 21:55:27 +04:00
|
|
|
}
|