widget/gtk/nsCUPSShim.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gtk/nsCUPSShim.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,59 @@
     1.4 +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* ex: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsDebug.h"
    1.11 +#include "nsString.h"
    1.12 +#include "nsCUPSShim.h"
    1.13 +#include "mozilla/ArrayUtils.h"
    1.14 +#include "prlink.h"
    1.15 +
    1.16 +
    1.17 +// List of symbols to find in libcups. Must match symAddr[] defined in Init().
    1.18 +// Making this an array of arrays instead of pointers allows storing the
    1.19 +// whole thing in read-only memory.
    1.20 +static const char gSymName[][sizeof("cupsPrintFile")] = {
    1.21 +    { "cupsAddOption" },
    1.22 +    { "cupsFreeDests" },
    1.23 +    { "cupsGetDest" },
    1.24 +    { "cupsGetDests" },
    1.25 +    { "cupsPrintFile" },
    1.26 +    { "cupsTempFd" },
    1.27 +};
    1.28 +static const int gSymNameCt = mozilla::ArrayLength(gSymName);
    1.29 +
    1.30 +
    1.31 +bool
    1.32 +nsCUPSShim::Init()
    1.33 +{
    1.34 +    mCupsLib = PR_LoadLibrary("libcups.so.2");
    1.35 +    if (!mCupsLib)
    1.36 +        return false;
    1.37 +
    1.38 +    // List of symbol pointers. Must match gSymName[] defined above.
    1.39 +    void **symAddr[] = {
    1.40 +        (void **)&mCupsAddOption,
    1.41 +        (void **)&mCupsFreeDests,
    1.42 +        (void **)&mCupsGetDest,
    1.43 +        (void **)&mCupsGetDests,
    1.44 +        (void **)&mCupsPrintFile,
    1.45 +        (void **)&mCupsTempFd,
    1.46 +    };
    1.47 +
    1.48 +    for (int i = gSymNameCt; i--; ) {
    1.49 +        *(symAddr[i]) = PR_FindSymbol(mCupsLib, gSymName[i]);
    1.50 +        if (! *(symAddr[i])) {
    1.51 +#ifdef DEBUG
    1.52 +            nsAutoCString msg(gSymName[i]);
    1.53 +            msg.Append(" not found in CUPS library");
    1.54 +            NS_WARNING(msg.get());
    1.55 +#endif
    1.56 +            PR_UnloadLibrary(mCupsLib);
    1.57 +            mCupsLib = nullptr;
    1.58 +            return false;
    1.59 +        }
    1.60 +    }
    1.61 +    return true;
    1.62 +}

mercurial