Bug 1657518 - Clean up nsCUPSPrinter GetName() Functionality r=emilio

- Add cupsGetOption function support to CUPSShim
- Improve performance on GetName() function to not copy string twice.
- Unify implementation of `GetDisplayNameForPrinter()`

Differential Revision: https://phabricator.services.mozilla.com/D86114
This commit is contained in:
Erik Nordin 2020-08-10 19:22:27 +00:00
Родитель 0dc0ced176
Коммит b80fa4c694
5 изменённых файлов: 20 добавлений и 37 удалений

Просмотреть файл

@ -44,31 +44,6 @@ using mozilla::gfx::SurfaceFormat;
static LazyLogModule sDeviceContextSpecXLog("DeviceContextSpecX");
//----------------------------------------------------------------------
// nsPrinterListX
/**
* Retrieves the display name of a printer.
*/
void nsPrinterListCUPS::GetDisplayNameForPrinter(const cups_dest_t& aDest, nsAString& aName) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
// CUPS does not appear to have a native call to retrieve a display name for
// a printer, so we need to use cocoa to find a display name for the printer.
PMPrinter corePrinter = PMPrinterCreateFromPrinterID(
static_cast<CFStringRef>([NSString stringWithUTF8String:aDest.name]));
if (!corePrinter) {
return;
}
CFStringRef printerName = PMPrinterGetName(corePrinter);
nsCocoaUtils::GetStringForNSString(static_cast<NSString*>(printerName), aName);
PMRelease(corePrinter);
NS_OBJC_END_TRY_ABORT_BLOCK;
}
//----------------------------------------------------------------------
// nsDeviceContentSpecX

Просмотреть файл

@ -37,6 +37,7 @@ class nsCUPSShim {
X(cupsGetDest) \
X(cupsGetDests) \
X(cupsGetNamedDest) \
X(cupsGetOption) \
X(cupsLocalizeDestMedia) \
X(cupsPrintFile) \
X(cupsTempFd) \

Просмотреть файл

@ -23,7 +23,8 @@ nsPrinterCUPS::~nsPrinterCUPS() {
NS_IMETHODIMP
nsPrinterCUPS::GetName(nsAString& aName) {
if (mDisplayName.IsEmpty()) {
aName = NS_ConvertUTF8toUTF16(mPrinter->name);
aName.Truncate();
CopyUTF8toUTF16(MakeStringSpan(mPrinter->name), aName);
} else {
aName = mDisplayName;
}

Просмотреть файл

@ -13,6 +13,23 @@
static nsCUPSShim sCupsShim;
using PrinterInfo = nsPrinterListBase::PrinterInfo;
/**
* Retrieves a human-readable name for the printer from CUPS.
* https://www.cups.org/doc/cupspm.html#basic-destination-information
*/
static void GetDisplayNameForPrinter(const cups_dest_t& aDest,
nsAString& aName) {
// macOS clients expect prettified printer names
// while GTK clients expect non-prettified names.
#ifdef XP_MACOSX
const char* displayName =
sCupsShim.cupsGetOption("printer-info", aDest.num_options, aDest.options);
if (displayName) {
CopyUTF8toUTF16(MakeStringSpan(displayName), aName);
}
#endif
}
nsTArray<PrinterInfo> nsPrinterListCUPS::Printers() const {
if (!sCupsShim.EnsureInitialized()) {
return {};

Просмотреть файл

@ -9,8 +9,6 @@
#include "nsPrinterListBase.h"
#include "nsStringFwd.h"
struct cups_dest_s;
class nsPrinterListCUPS final : public nsPrinterListBase {
NS_IMETHOD InitPrintSettingsFromPrinter(const nsAString&,
nsIPrintSettings*) final;
@ -19,15 +17,6 @@ class nsPrinterListCUPS final : public nsPrinterListBase {
nsTArray<PrinterInfo> Printers() const final;
RefPtr<nsIPrinter> CreatePrinter(PrinterInfo) const final;
#ifdef XP_MACOSX
// This is implemented in nsDeviceContextSpecX. We could add a new class to
// the class hierarchy instead and make this virtual, but it seems overkill
// just for this.
static void GetDisplayNameForPrinter(const cups_dest_s&, nsAString& aName);
#else
static void GetDisplayNameForPrinter(const cups_dest_s&, nsAString& aName) {}
#endif
private:
~nsPrinterListCUPS() override = default;
};