2020-07-29 21:02:21 +03:00
|
|
|
/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "nsPrinterCUPS.h"
|
2020-09-10 02:56:13 +03:00
|
|
|
|
2021-11-15 15:39:38 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2020-09-10 02:56:13 +03:00
|
|
|
#include "mozilla/GkRustUtils.h"
|
2021-11-15 15:39:38 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2020-10-23 14:58:42 +03:00
|
|
|
#include "mozilla/StaticPrefs_print.h"
|
2020-09-30 21:37:50 +03:00
|
|
|
#include "nsTHashtable.h"
|
2020-08-05 04:24:49 +03:00
|
|
|
#include "nsPaper.h"
|
|
|
|
#include "nsPrinterBase.h"
|
2020-08-20 09:11:08 +03:00
|
|
|
#include "nsPrintSettingsImpl.h"
|
2020-08-05 14:26:13 +03:00
|
|
|
|
2020-09-07 21:58:48 +03:00
|
|
|
using namespace mozilla;
|
2021-11-15 15:39:38 +03:00
|
|
|
using MarginDouble = mozilla::gfx::MarginDouble;
|
2020-09-07 21:58:48 +03:00
|
|
|
|
2020-09-10 02:56:13 +03:00
|
|
|
// Requested attributes for IPP requests, just the CUPS version now.
|
|
|
|
static constexpr Array<const char* const, 1> requestedAttributes{
|
|
|
|
"cups-version"};
|
|
|
|
|
2020-10-14 00:52:04 +03:00
|
|
|
static constexpr double kPointsPerHundredthMillimeter = 72.0 / 2540.0;
|
|
|
|
|
|
|
|
static PaperInfo MakePaperInfo(const nsAString& aName,
|
|
|
|
const cups_size_t& aMedia) {
|
|
|
|
// XXX Do we actually have the guarantee that this is utf-8?
|
2020-09-17 23:05:08 +03:00
|
|
|
NS_ConvertUTF8toUTF16 paperId(aMedia.media); // internal paper name/ID
|
2020-08-20 09:11:08 +03:00
|
|
|
return PaperInfo(
|
2020-10-14 00:52:04 +03:00
|
|
|
paperId, aName,
|
2020-08-20 09:11:08 +03:00
|
|
|
{aMedia.width * kPointsPerHundredthMillimeter,
|
|
|
|
aMedia.length * kPointsPerHundredthMillimeter},
|
2020-09-07 21:58:48 +03:00
|
|
|
Some(gfx::MarginDouble{aMedia.top * kPointsPerHundredthMillimeter,
|
|
|
|
aMedia.right * kPointsPerHundredthMillimeter,
|
|
|
|
aMedia.bottom * kPointsPerHundredthMillimeter,
|
|
|
|
aMedia.left * kPointsPerHundredthMillimeter}));
|
2020-08-20 09:11:08 +03:00
|
|
|
}
|
|
|
|
|
2020-09-10 02:56:13 +03:00
|
|
|
// Fetches the CUPS version for the print server controlling the printer. This
|
|
|
|
// will only modify the output arguments if the fetch succeeds.
|
|
|
|
static void FetchCUPSVersionForPrinter(const nsCUPSShim& aShim,
|
|
|
|
const cups_dest_t* const aDest,
|
|
|
|
uint64_t& aOutMajor, uint64_t& aOutMinor,
|
|
|
|
uint64_t& aOutPatch) {
|
|
|
|
// Make an IPP request to the server for the printer.
|
|
|
|
const char* const uri = aShim.cupsGetOption(
|
|
|
|
"printer-uri-supported", aDest->num_options, aDest->options);
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipp_t* const ippRequest = aShim.ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
|
|
|
|
|
|
|
|
// Set the URI we want to use.
|
|
|
|
aShim.ippAddString(ippRequest, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
|
|
|
|
nullptr, uri);
|
|
|
|
|
|
|
|
// Set the attributes to request.
|
|
|
|
aShim.ippAddStrings(ippRequest, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
|
|
|
|
"requested-attributes", requestedAttributes.Length,
|
|
|
|
nullptr, &(requestedAttributes[0]));
|
|
|
|
|
|
|
|
// Use the default HTTP connection to query the CUPS server itself to get
|
|
|
|
// the CUPS version.
|
|
|
|
// Note that cupsDoRequest will delete the request whether it succeeds or
|
|
|
|
// fails, so we should not use ippDelete on it.
|
|
|
|
if (ipp_t* const ippResponse =
|
|
|
|
aShim.cupsDoRequest(CUPS_HTTP_DEFAULT, ippRequest, "/")) {
|
|
|
|
ipp_attribute_t* const versionAttrib =
|
|
|
|
aShim.ippFindAttribute(ippResponse, "cups-version", IPP_TAG_TEXT);
|
|
|
|
if (versionAttrib && aShim.ippGetCount(versionAttrib) == 1) {
|
|
|
|
const char* versionString = aShim.ippGetString(versionAttrib, 0, nullptr);
|
|
|
|
MOZ_ASSERT(versionString);
|
|
|
|
// On error, GkRustUtils::ParseSemVer will not modify its arguments.
|
|
|
|
GkRustUtils::ParseSemVer(
|
|
|
|
nsDependentCSubstring{MakeStringSpan(versionString)}, aOutMajor,
|
|
|
|
aOutMinor, aOutPatch);
|
|
|
|
}
|
|
|
|
aShim.ippDelete(ippResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 21:02:21 +03:00
|
|
|
nsPrinterCUPS::~nsPrinterCUPS() {
|
2023-04-11 00:11:28 +03:00
|
|
|
PrinterInfoLock lock = mPrinterInfoMutex.Lock();
|
|
|
|
if (lock->mPrinterInfo) {
|
|
|
|
mShim.cupsFreeDestInfo(lock->mPrinterInfo);
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
2023-04-11 00:11:28 +03:00
|
|
|
if (lock->mPrinter) {
|
|
|
|
mShim.cupsFreeDests(1, lock->mPrinter);
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrinterCUPS::GetName(nsAString& aName) {
|
2020-08-20 09:11:08 +03:00
|
|
|
GetPrinterName(aName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-09-16 00:23:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrinterCUPS::GetSystemName(nsAString& aName) {
|
2023-04-11 00:11:28 +03:00
|
|
|
PrinterInfoLock lock = mPrinterInfoMutex.Lock();
|
|
|
|
CopyUTF8toUTF16(MakeStringSpan(lock->mPrinter->name), aName);
|
2020-09-16 00:23:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:11:08 +03:00
|
|
|
void nsPrinterCUPS::GetPrinterName(nsAString& aName) const {
|
2020-08-06 04:42:13 +03:00
|
|
|
if (mDisplayName.IsEmpty()) {
|
2020-08-10 22:22:27 +03:00
|
|
|
aName.Truncate();
|
2023-04-11 00:11:28 +03:00
|
|
|
PrinterInfoLock lock = mPrinterInfoMutex.Lock();
|
|
|
|
CopyUTF8toUTF16(MakeStringSpan(lock->mPrinter->name), aName);
|
2020-08-06 04:42:13 +03:00
|
|
|
} else {
|
|
|
|
aName = mDisplayName;
|
|
|
|
}
|
2020-08-20 09:11:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* nsPrinterCUPS::LocalizeMediaName(http_t& aConnection,
|
|
|
|
cups_size_t& aMedia) const {
|
|
|
|
// The returned string is owned by mPrinterInfo.
|
|
|
|
// https://www.cups.org/doc/cupspm.html#cupsLocalizeDestMedia
|
2021-03-29 13:08:44 +03:00
|
|
|
if (!mShim.cupsLocalizeDestMedia) {
|
|
|
|
return aMedia.media;
|
|
|
|
}
|
2023-04-11 00:11:28 +03:00
|
|
|
PrinterInfoLock lock = TryEnsurePrinterInfo();
|
2023-04-11 00:11:28 +03:00
|
|
|
return mShim.cupsLocalizeDestMedia(&aConnection, lock->mPrinter,
|
|
|
|
lock->mPrinterInfo,
|
2020-08-20 09:11:08 +03:00
|
|
|
CUPS_MEDIA_FLAGS_DEFAULT, &aMedia);
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
|
|
|
|
2020-08-04 15:06:37 +03:00
|
|
|
bool nsPrinterCUPS::SupportsDuplex() const {
|
|
|
|
return Supports(CUPS_SIDES, CUPS_SIDES_TWO_SIDED_PORTRAIT);
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
|
|
|
|
2020-09-07 21:58:48 +03:00
|
|
|
bool nsPrinterCUPS::SupportsMonochrome() const {
|
2020-10-23 14:58:42 +03:00
|
|
|
if (!SupportsColor()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-27 19:23:26 +03:00
|
|
|
return StaticPrefs::print_cups_monochrome_enabled();
|
2020-09-07 21:58:48 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 23:57:43 +03:00
|
|
|
bool nsPrinterCUPS::SupportsColor() const {
|
2020-11-11 22:00:00 +03:00
|
|
|
// CUPS 2.1 (particularly as used in Ubuntu 16) is known to have inaccurate
|
2020-09-03 02:49:08 +03:00
|
|
|
// results for CUPS_PRINT_COLOR_MODE.
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1660658#c15
|
|
|
|
if (!IsCUPSVersionAtLeast(2, 2, 0)) {
|
2020-09-17 15:14:25 +03:00
|
|
|
return true; // See comment for PrintSettingsInitializer.mPrintInColor
|
2020-09-03 02:49:08 +03:00
|
|
|
}
|
2020-09-17 15:14:25 +03:00
|
|
|
return Supports(CUPS_PRINT_COLOR_MODE, CUPS_PRINT_COLOR_MODE_AUTO) ||
|
|
|
|
Supports(CUPS_PRINT_COLOR_MODE, CUPS_PRINT_COLOR_MODE_COLOR) ||
|
|
|
|
!Supports(CUPS_PRINT_COLOR_MODE, CUPS_PRINT_COLOR_MODE_MONOCHROME);
|
2020-08-06 23:57:43 +03:00
|
|
|
}
|
2020-08-03 14:45:35 +03:00
|
|
|
|
2020-08-14 23:41:59 +03:00
|
|
|
bool nsPrinterCUPS::SupportsCollation() const {
|
|
|
|
// We can't depend on cupsGetIntegerOption existing.
|
2023-04-11 00:11:28 +03:00
|
|
|
PrinterInfoLock lock = mPrinterInfoMutex.Lock();
|
|
|
|
const char* const value = FindCUPSOption(lock, "printer-type");
|
2020-08-14 23:41:59 +03:00
|
|
|
if (!value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If the value is non-numeric, then atoi will return 0, which will still
|
|
|
|
// cause this function to return false.
|
|
|
|
const int type = atoi(value);
|
|
|
|
return type & CUPS_PRINTER_COLLATE;
|
|
|
|
}
|
|
|
|
|
2020-11-03 23:35:38 +03:00
|
|
|
nsPrinterBase::PrinterInfo nsPrinterCUPS::CreatePrinterInfo() const {
|
|
|
|
Connection connection{mShim};
|
|
|
|
return PrinterInfo{PaperList(connection), DefaultSettings(connection)};
|
|
|
|
}
|
|
|
|
|
2020-09-03 02:49:08 +03:00
|
|
|
bool nsPrinterCUPS::Supports(const char* aOption, const char* aValue) const {
|
2023-04-11 00:11:28 +03:00
|
|
|
PrinterInfoLock lock = TryEnsurePrinterInfo();
|
2023-04-11 00:11:28 +03:00
|
|
|
return mShim.cupsCheckDestSupported(CUPS_HTTP_DEFAULT, lock->mPrinter,
|
2023-04-11 00:11:28 +03:00
|
|
|
lock->mPrinterInfo, aOption, aValue);
|
2020-09-03 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nsPrinterCUPS::IsCUPSVersionAtLeast(uint64_t aCUPSMajor,
|
|
|
|
uint64_t aCUPSMinor,
|
|
|
|
uint64_t aCUPSPatch) const {
|
2023-04-11 00:11:28 +03:00
|
|
|
PrinterInfoLock lock = TryEnsurePrinterInfo();
|
2020-09-03 02:49:08 +03:00
|
|
|
// Compare major version.
|
2023-04-11 00:11:28 +03:00
|
|
|
if (lock->mCUPSMajor > aCUPSMajor) {
|
2020-09-03 02:49:08 +03:00
|
|
|
return true;
|
|
|
|
}
|
2023-04-11 00:11:28 +03:00
|
|
|
if (lock->mCUPSMajor < aCUPSMajor) {
|
2020-09-03 02:49:08 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare minor version.
|
2023-04-11 00:11:28 +03:00
|
|
|
if (lock->mCUPSMinor > aCUPSMinor) {
|
2020-09-03 02:49:08 +03:00
|
|
|
return true;
|
|
|
|
}
|
2023-04-11 00:11:28 +03:00
|
|
|
if (lock->mCUPSMinor < aCUPSMinor) {
|
2020-09-03 02:49:08 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare patch.
|
2023-04-11 00:11:28 +03:00
|
|
|
return aCUPSPatch <= lock->mCUPSPatch;
|
2020-07-29 21:02:21 +03:00
|
|
|
}
|
2020-08-05 04:24:49 +03:00
|
|
|
|
2020-11-03 23:35:38 +03:00
|
|
|
http_t* nsPrinterCUPS::Connection::GetConnection(cups_dest_t* aDest) {
|
|
|
|
if (mWasInited) {
|
|
|
|
return mConnection;
|
|
|
|
}
|
|
|
|
mWasInited = true;
|
|
|
|
|
2020-08-05 04:24:49 +03:00
|
|
|
// blocking call
|
2020-11-03 23:35:38 +03:00
|
|
|
http_t* const connection = mShim.cupsConnectDest(aDest, CUPS_DEST_FLAGS_NONE,
|
|
|
|
/* timeout(ms) */ 5000,
|
|
|
|
/* cancel */ nullptr,
|
|
|
|
/* resource */ nullptr,
|
|
|
|
/* resourcesize */ 0,
|
|
|
|
/* callback */ nullptr,
|
|
|
|
/* user_data */ nullptr);
|
|
|
|
if (connection) {
|
|
|
|
mConnection = connection;
|
2020-09-25 01:01:43 +03:00
|
|
|
}
|
2020-11-03 23:35:38 +03:00
|
|
|
return mConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrinterCUPS::Connection::~Connection() {
|
|
|
|
if (mWasInited && mConnection) {
|
|
|
|
mShim.httpClose(mConnection);
|
|
|
|
}
|
|
|
|
}
|
2020-09-25 01:01:43 +03:00
|
|
|
|
2020-11-03 23:35:38 +03:00
|
|
|
PrintSettingsInitializer nsPrinterCUPS::DefaultSettings(
|
|
|
|
Connection& aConnection) const {
|
|
|
|
nsString printerName;
|
|
|
|
GetPrinterName(printerName);
|
2023-04-11 00:11:28 +03:00
|
|
|
PrinterInfoLock lock = TryEnsurePrinterInfo();
|
2020-11-03 23:35:38 +03:00
|
|
|
|
|
|
|
cups_size_t media;
|
|
|
|
|
2021-03-29 13:08:44 +03:00
|
|
|
bool hasDefaultMedia = false;
|
2020-11-03 23:35:38 +03:00
|
|
|
// cupsGetDestMediaDefault appears to return more accurate defaults on macOS,
|
|
|
|
// and the IPP attribute appears to return more accurate defaults on Linux.
|
|
|
|
#ifdef XP_MACOSX
|
2023-04-11 00:11:28 +03:00
|
|
|
hasDefaultMedia = mShim.cupsGetDestMediaDefault(
|
2023-04-11 00:11:28 +03:00
|
|
|
CUPS_HTTP_DEFAULT, lock->mPrinter, lock->mPrinterInfo,
|
|
|
|
CUPS_MEDIA_FLAGS_DEFAULT, &media);
|
2020-11-03 23:35:38 +03:00
|
|
|
#else
|
2021-03-29 13:08:44 +03:00
|
|
|
{
|
|
|
|
ipp_attribute_t* defaultMediaIPP =
|
|
|
|
mShim.cupsFindDestDefault
|
2023-04-11 00:11:28 +03:00
|
|
|
? mShim.cupsFindDestDefault(CUPS_HTTP_DEFAULT, lock->mPrinter,
|
2023-04-11 00:11:28 +03:00
|
|
|
lock->mPrinterInfo, "media")
|
2021-03-29 13:08:44 +03:00
|
|
|
: nullptr;
|
|
|
|
|
|
|
|
const char* defaultMediaName =
|
|
|
|
defaultMediaIPP ? mShim.ippGetString(defaultMediaIPP, 0, nullptr)
|
|
|
|
: nullptr;
|
|
|
|
|
|
|
|
hasDefaultMedia = defaultMediaName &&
|
|
|
|
mShim.cupsGetDestMediaByName(
|
2023-04-11 00:11:28 +03:00
|
|
|
CUPS_HTTP_DEFAULT, lock->mPrinter, lock->mPrinterInfo,
|
2021-03-29 13:08:44 +03:00
|
|
|
defaultMediaName, CUPS_MEDIA_FLAGS_DEFAULT, &media);
|
|
|
|
}
|
2020-11-03 23:35:38 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!hasDefaultMedia) {
|
|
|
|
return PrintSettingsInitializer{
|
|
|
|
std::move(printerName),
|
|
|
|
PaperInfo(),
|
|
|
|
SupportsColor(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this is a localized fallback paper size, in which case we can
|
|
|
|
// avoid using the CUPS localization methods.
|
|
|
|
const gfx::SizeDouble sizeDouble{
|
|
|
|
media.width * kPointsPerHundredthMillimeter,
|
|
|
|
media.length * kPointsPerHundredthMillimeter};
|
|
|
|
if (const PaperInfo* const paperInfo = FindCommonPaperSize(sizeDouble)) {
|
|
|
|
return PrintSettingsInitializer{
|
|
|
|
std::move(printerName),
|
|
|
|
MakePaperInfo(paperInfo->mName, media),
|
|
|
|
SupportsColor(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-11 00:11:28 +03:00
|
|
|
http_t* const connection = aConnection.GetConnection(lock->mPrinter);
|
2020-11-03 23:35:38 +03:00
|
|
|
// XXX Do we actually have the guarantee that this is utf-8?
|
|
|
|
NS_ConvertUTF8toUTF16 localizedName{
|
|
|
|
connection ? LocalizeMediaName(*connection, media) : ""};
|
|
|
|
|
|
|
|
return PrintSettingsInitializer{
|
|
|
|
std::move(printerName),
|
|
|
|
MakePaperInfo(localizedName, media),
|
|
|
|
SupportsColor(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<mozilla::PaperInfo> nsPrinterCUPS::PaperList(
|
|
|
|
Connection& aConnection) const {
|
2023-04-11 00:11:28 +03:00
|
|
|
PrinterInfoLock lock = mPrinterInfoMutex.Lock();
|
|
|
|
http_t* const connection = aConnection.GetConnection(lock->mPrinter);
|
|
|
|
TryEnsurePrinterInfo(lock, connection);
|
2020-09-25 01:01:43 +03:00
|
|
|
|
2023-04-11 00:11:28 +03:00
|
|
|
if (!lock->mPrinterInfo) {
|
2020-08-05 04:24:49 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-04-11 00:11:28 +03:00
|
|
|
const int paperCount = mShim.cupsGetDestMediaCount
|
|
|
|
? mShim.cupsGetDestMediaCount(
|
2023-04-11 00:11:28 +03:00
|
|
|
connection, lock->mPrinter,
|
|
|
|
lock->mPrinterInfo, CUPS_MEDIA_FLAGS_DEFAULT)
|
2023-04-11 00:11:28 +03:00
|
|
|
: 0;
|
2020-08-05 14:26:13 +03:00
|
|
|
nsTArray<PaperInfo> paperList;
|
2020-09-30 21:37:50 +03:00
|
|
|
nsTHashtable<nsCharPtrHashKey> paperSet(std::max(paperCount, 0));
|
|
|
|
|
2020-10-14 00:52:04 +03:00
|
|
|
paperList.SetCapacity(paperCount);
|
2020-08-05 04:24:49 +03:00
|
|
|
for (int i = 0; i < paperCount; ++i) {
|
2020-08-20 09:11:08 +03:00
|
|
|
cups_size_t media;
|
2023-04-11 00:11:28 +03:00
|
|
|
const int getInfoSucceeded = mShim.cupsGetDestMediaByIndex(
|
|
|
|
connection, lock->mPrinter, lock->mPrinterInfo, i,
|
|
|
|
CUPS_MEDIA_FLAGS_DEFAULT, &media);
|
2020-08-05 04:24:49 +03:00
|
|
|
|
2020-09-30 21:37:50 +03:00
|
|
|
if (!getInfoSucceeded || !paperSet.EnsureInserted(media.media)) {
|
2020-08-05 04:24:49 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-10-14 00:52:04 +03:00
|
|
|
// Check if this is a PWG paper size, in which case we can avoid using the
|
|
|
|
// CUPS localization methods.
|
|
|
|
const gfx::SizeDouble sizeDouble{
|
|
|
|
media.width * kPointsPerHundredthMillimeter,
|
|
|
|
media.length * kPointsPerHundredthMillimeter};
|
|
|
|
if (const PaperInfo* const paperInfo = FindCommonPaperSize(sizeDouble)) {
|
|
|
|
paperList.AppendElement(MakePaperInfo(paperInfo->mName, media));
|
|
|
|
} else {
|
|
|
|
const char* const mediaName =
|
|
|
|
connection ? LocalizeMediaName(*connection, media) : media.media;
|
|
|
|
paperList.AppendElement(
|
|
|
|
MakePaperInfo(NS_ConvertUTF8toUTF16(mediaName), media));
|
|
|
|
}
|
2020-09-25 01:01:43 +03:00
|
|
|
}
|
|
|
|
|
2020-08-05 04:24:49 +03:00
|
|
|
return paperList;
|
|
|
|
}
|
2020-09-10 02:56:13 +03:00
|
|
|
|
2023-04-11 00:11:28 +03:00
|
|
|
void nsPrinterCUPS::TryEnsurePrinterInfo(PrinterInfoLock& aLock,
|
|
|
|
http_t* const aConnection) const {
|
|
|
|
if (aLock->mPrinterInfo ||
|
|
|
|
(aConnection == CUPS_HTTP_DEFAULT ? aLock->mTriedInitWithDefault
|
|
|
|
: aLock->mTriedInitWithConnection)) {
|
|
|
|
return;
|
2020-09-10 02:56:13 +03:00
|
|
|
}
|
|
|
|
|
2020-09-25 01:01:43 +03:00
|
|
|
if (aConnection == CUPS_HTTP_DEFAULT) {
|
2023-04-11 00:11:28 +03:00
|
|
|
aLock->mTriedInitWithDefault = true;
|
2020-09-25 01:01:43 +03:00
|
|
|
} else {
|
2023-04-11 00:11:28 +03:00
|
|
|
aLock->mTriedInitWithConnection = true;
|
2020-09-25 01:01:43 +03:00
|
|
|
}
|
2020-09-11 03:36:17 +03:00
|
|
|
|
2023-04-11 00:11:28 +03:00
|
|
|
MOZ_ASSERT(aLock->mPrinter);
|
2021-10-23 01:55:25 +03:00
|
|
|
|
|
|
|
// httpGetAddress was only added in CUPS 2.0, and some systems still use
|
|
|
|
// CUPS 1.7.
|
|
|
|
if (aConnection && MOZ_LIKELY(mShim.httpGetAddress && mShim.httpAddrPort)) {
|
|
|
|
// This is a workaround for the CUPS Bug seen in bug 1691347.
|
|
|
|
// This is to avoid a null string being passed to strstr in CUPS. The path
|
|
|
|
// in CUPS that leads to this is as follows:
|
|
|
|
//
|
|
|
|
// In cupsCopyDestInfo, CUPS_DEST_FLAG_DEVICE is set when the connection is
|
|
|
|
// not null (same as CUPS_HTTP_DEFAULT), the print server is not the same
|
|
|
|
// as our hostname and is not path-based (starts with a '/'), or the IPP
|
|
|
|
// port is different than the global server IPP port.
|
|
|
|
//
|
|
|
|
// https://github.com/apple/cups/blob/c9da6f63b263faef5d50592fe8cf8056e0a58aa2/cups/dest-options.c#L718-L722
|
|
|
|
//
|
|
|
|
// In _cupsGetDestResource, CUPS fetches the IPP options "device-uri" and
|
|
|
|
// "printer-uri-supported". Note that IPP options are returned as null when
|
|
|
|
// missing.
|
|
|
|
//
|
|
|
|
// https://github.com/apple/cups/blob/23c45db76a8520fd6c3b1d9164dbe312f1ab1481/cups/dest.c#L1138-L1141
|
|
|
|
//
|
|
|
|
// If the CUPS_DEST_FLAG_DEVICE is set or the "printer-uri-supported"
|
|
|
|
// option is not set, CUPS checks for "._tcp" in the "device-uri" option
|
|
|
|
// without doing a NULL-check first.
|
|
|
|
//
|
|
|
|
// https://github.com/apple/cups/blob/23c45db76a8520fd6c3b1d9164dbe312f1ab1481/cups/dest.c#L1144
|
|
|
|
//
|
|
|
|
// If we find that those branches will be taken, don't actually fetch the
|
|
|
|
// CUPS data and instead just return an empty printer info.
|
|
|
|
|
|
|
|
const char* const serverNameBytes = mShim.cupsServer();
|
|
|
|
|
|
|
|
if (MOZ_LIKELY(serverNameBytes)) {
|
|
|
|
const nsDependentCString serverName{serverNameBytes};
|
|
|
|
|
|
|
|
// We only need enough characters to determine equality with serverName.
|
|
|
|
// + 2 because we need one byte for the null-character, and we also want
|
|
|
|
// to get more characters of the host name than the server name if
|
|
|
|
// possible. Otherwise, if the hostname starts with the same text as the
|
|
|
|
// entire server name, it would compare equal when it's not.
|
|
|
|
const size_t hostnameMemLength = serverName.Length() + 2;
|
|
|
|
auto hostnameMem = MakeUnique<char[]>(hostnameMemLength);
|
|
|
|
|
|
|
|
// We don't expect httpGetHostname to return null when a connection is
|
|
|
|
// passed, but it's better not to make assumptions.
|
|
|
|
const char* const hostnameBytes = mShim.httpGetHostname(
|
|
|
|
aConnection, hostnameMem.get(), hostnameMemLength);
|
|
|
|
|
|
|
|
if (MOZ_LIKELY(hostnameBytes)) {
|
|
|
|
const nsDependentCString hostname{hostnameBytes};
|
|
|
|
|
2022-02-05 03:39:04 +03:00
|
|
|
// Attempt to match the condional at
|
2021-10-23 01:55:25 +03:00
|
|
|
// https://github.com/apple/cups/blob/c9da6f63b263faef5d50592fe8cf8056e0a58aa2/cups/dest-options.c#L718
|
2022-02-05 03:39:04 +03:00
|
|
|
//
|
|
|
|
// To find the result of the comparison CUPS performs of
|
|
|
|
// `strcmp(http->hostname, cg->server)`, we use httpGetHostname to try
|
|
|
|
// to get the value of `http->hostname`, but this isn't quite the same.
|
|
|
|
// For local addresses, httpGetHostName will normalize the result to be
|
|
|
|
// localhost", rather than the actual value of `http->hostname`.
|
|
|
|
//
|
|
|
|
// https://github.com/apple/cups/blob/2201569857f225c9874bfae19713ffb2f4bdfdeb/cups/http-addr.c#L794-L818
|
|
|
|
//
|
|
|
|
// Because of this, if both serverName and hostname equal "localhost",
|
|
|
|
// then the actual hostname might be a different local address that CUPS
|
|
|
|
// normalized in httpGetHostName, and `http->hostname` won't be equal to
|
|
|
|
// `cg->server` in CUPS.
|
|
|
|
const bool namesMightNotMatch =
|
|
|
|
hostname != serverName || hostname == "localhost";
|
2021-10-23 01:55:25 +03:00
|
|
|
const bool portsDiffer =
|
|
|
|
mShim.httpAddrPort(mShim.httpGetAddress(aConnection)) !=
|
|
|
|
mShim.ippPort();
|
|
|
|
const bool cupsDestDeviceFlag =
|
2022-02-05 03:39:04 +03:00
|
|
|
(namesMightNotMatch && serverName[0] != '/') || portsDiffer;
|
2021-10-23 01:55:25 +03:00
|
|
|
|
|
|
|
// Match the conditional at
|
|
|
|
// https://github.com/apple/cups/blob/23c45db76a8520fd6c3b1d9164dbe312f1ab1481/cups/dest.c#L1144
|
|
|
|
// but if device-uri is null do not call into CUPS.
|
2023-04-11 00:11:28 +03:00
|
|
|
if ((cupsDestDeviceFlag ||
|
|
|
|
!FindCUPSOption(aLock, "printer-uri-supported")) &&
|
|
|
|
!FindCUPSOption(aLock, "device-uri")) {
|
|
|
|
return;
|
2021-10-23 01:55:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 03:36:17 +03:00
|
|
|
// All CUPS calls that take the printer info do null-checks internally, so we
|
|
|
|
// can fetch this info and only worry about the result of the later CUPS
|
|
|
|
// functions.
|
2023-04-11 00:11:28 +03:00
|
|
|
aLock->mPrinterInfo = mShim.cupsCopyDestInfo(aConnection, aLock->mPrinter);
|
2020-09-11 03:36:17 +03:00
|
|
|
|
|
|
|
// Even if we failed to fetch printer info, it is still possible we can talk
|
|
|
|
// to the print server and get its CUPS version.
|
2023-04-11 00:11:28 +03:00
|
|
|
FetchCUPSVersionForPrinter(mShim, aLock->mPrinter, aLock->mCUPSMajor,
|
|
|
|
aLock->mCUPSMinor, aLock->mCUPSPatch);
|
2020-09-10 02:56:13 +03:00
|
|
|
}
|
2020-11-11 22:00:00 +03:00
|
|
|
|
|
|
|
void nsPrinterCUPS::ForEachExtraMonochromeSetting(
|
|
|
|
FunctionRef<void(const nsACString&, const nsACString&)> aCallback) {
|
|
|
|
nsAutoCString pref;
|
|
|
|
Preferences::GetCString("print.cups.monochrome.extra_settings", pref);
|
|
|
|
if (pref.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& pair : pref.Split(',')) {
|
|
|
|
auto splitter = pair.Split(':');
|
|
|
|
auto end = splitter.end();
|
|
|
|
|
|
|
|
auto key = splitter.begin();
|
|
|
|
if (key == end) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto value = ++splitter.begin();
|
|
|
|
if (value == end) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback(*key, *value);
|
|
|
|
}
|
|
|
|
}
|