Bug 1657874 - Unify printer list implementations between Mac and GTK, and add nsPrinterListBase. r=jwatt

nsPrinterListBase is where the CC setup and promises and such will be, I
think.

Differential Revision: https://phabricator.services.mozilla.com/D86336
This commit is contained in:
Emilio Cobos Álvarez
2020-08-07 12:04:26 +00:00
parent 5a1b810975
commit eb8c32b90c
13 changed files with 171 additions and 206 deletions

View File

@@ -47,17 +47,4 @@ class nsDeviceContextSpecX : public nsIDeviceContextSpec {
#endif
};
//----------------------------------------------------------------------
// nsPrinterErnumeratorX
class nsPrinterListX final : public nsIPrinterList {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIPRINTERLIST
nsPrinterListX() = default;
private:
~nsPrinterListX() = default;
};
#endif // nsDeviceContextSpecX_h_

View File

@@ -26,7 +26,7 @@
#include "nsDirectoryServiceDefs.h"
#include "nsILocalFileMac.h"
#include "nsPaper.h"
#include "nsPrinterCUPS.h"
#include "nsPrinterListCUPS.h"
#include "nsPrintSettingsX.h"
#include "nsQueryObject.h"
#include "prenv.h"
@@ -45,16 +45,14 @@ using mozilla::gfx::SurfaceFormat;
static LazyLogModule sDeviceContextSpecXLog("DeviceContextSpecX");
static nsCUPSShim sCupsShim;
//----------------------------------------------------------------------
// nsPrinterListX
/**
* Retrieves the display name of a printer.
*/
static nsresult GetDisplayNameForPrinter(cups_dest_t& aDest, nsAString& aName) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
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.
@@ -62,101 +60,14 @@ static nsresult GetDisplayNameForPrinter(cups_dest_t& aDest, nsAString& aName) {
static_cast<CFStringRef>([NSString stringWithUTF8String:aDest.name]));
if (!corePrinter) {
return NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
return;
}
CFStringRef printerName = PMPrinterGetName(corePrinter);
nsCocoaUtils::GetStringForNSString(static_cast<NSString*>(printerName), aName);
PMRelease(corePrinter);
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMPL_ISUPPORTS(nsPrinterListX, nsIPrinterList);
NS_IMETHODIMP
nsPrinterListX::GetSystemDefaultPrinterName(nsAString& aName) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if (!sCupsShim.IsInitialized()) {
if (!sCupsShim.Init()) {
return NS_ERROR_FAILURE;
}
}
mozilla::CUPSPrinterList cupsPrinterList(sCupsShim);
cupsPrinterList.Initialize();
cups_dest_t* const dest = cupsPrinterList.GetDefaultPrinter();
if (!dest) {
return NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
}
GetDisplayNameForPrinter(*dest, aName);
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsPrinterListX::GetPrinters(nsTArray<RefPtr<nsIPrinter>>& aPrinters) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if (!sCupsShim.IsInitialized()) {
if (!sCupsShim.Init()) {
return NS_ERROR_FAILURE;
}
}
mozilla::CUPSPrinterList cupsPrinterList(sCupsShim);
cupsPrinterList.Initialize();
aPrinters.SetCapacity(cupsPrinterList.NumPrinters());
for (int i = 0; i < cupsPrinterList.NumPrinters(); i++) {
cups_dest_t* const dest = cupsPrinterList.GetPrinter(i);
nsString displayName;
GetDisplayNameForPrinter(*dest, displayName);
RefPtr<nsPrinterCUPS> cupsPrinter = nsPrinterCUPS::Create(sCupsShim, dest, displayName);
aPrinters.AppendElement(cupsPrinter);
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsPrinterListX::InitPrintSettingsFromPrinter(const nsAString& aPrinterName,
nsIPrintSettings* aPrintSettings) {
NS_ENSURE_ARG_POINTER(aPrintSettings);
// Set a default file name.
nsAutoString filename;
nsresult rv = aPrintSettings->GetToFileName(filename);
if (NS_FAILED(rv) || filename.IsEmpty()) {
const char* path = PR_GetEnv("PWD");
if (!path) {
path = PR_GetEnv("HOME");
}
if (path) {
CopyUTF8toUTF16(MakeStringSpan(path), filename);
filename.AppendLiteral("/mozilla.pdf");
} else {
filename.AssignLiteral("mozilla.pdf");
}
aPrintSettings->SetToFileName(filename);
}
aPrintSettings->SetIsInitializedFromPrinter(true);
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK;
}
//----------------------------------------------------------------------
@@ -266,11 +177,6 @@ NS_IMETHODIMP nsDeviceContextSpecX::Init(nsIWidget* aWidget, nsIPrintSettings* a
}
}
// Initialize the CUPS shim, if it wasn't already.
if (!sCupsShim.IsInitialized()) {
sCupsShim.Init();
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;

View File

@@ -33,6 +33,7 @@
#include "OSXNotificationCenter.h"
#include "nsDeviceContextSpecX.h"
#include "nsPrinterListCUPS.h"
#include "nsPrintSettingsServiceX.h"
#include "nsPrintDialogX.h"
#include "nsPrintSession.h"
@@ -62,7 +63,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsHTMLFormatConverter)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsClipboardHelper)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDragService)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceContextSpecX)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsPrinterListX)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsPrinterListCUPS)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrintSettingsServiceX, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrintDialogServiceX, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrintSession, Init)
@@ -143,7 +144,7 @@ static const mozilla::Module::CIDEntry kWidgetCIDs[] = {
{&kNS_SCREENMANAGER_CID, false, NULL, ScreenManagerConstructor,
mozilla::Module::MAIN_PROCESS_ONLY},
{&kNS_DEVICE_CONTEXT_SPEC_CID, false, NULL, nsDeviceContextSpecXConstructor},
{&kNS_PRINTER_LIST_CID, false, NULL, nsPrinterListXConstructor},
{&kNS_PRINTER_LIST_CID, false, NULL, nsPrinterListCUPSConstructor},
{&kNS_PRINTSESSION_CID, false, NULL, nsPrintSessionConstructor},
{&kNS_PRINTSETTINGSSERVICE_CID, false, NULL, nsPrintSettingsServiceXConstructor},
{&kNS_PRINTDIALOGSERVICE_CID, false, NULL, nsPrintDialogServiceXConstructor},

View File

@@ -159,7 +159,7 @@ if defined('NS_PRINTING'):
{
'cid': '{a6cf9129-15b3-11d2-932e-00805f8add32}',
'contract_ids': ['@mozilla.org/gfx/printerlist;1'],
'type': 'nsPrinterListGTK',
'headers': ['/widget/gtk/nsDeviceContextSpecG.h'],
'type': 'nsPrinterListCUPS',
'headers': ['/widget/nsPrinterListCUPS.h'],
},
]

View File

@@ -48,8 +48,6 @@ using mozilla::gfx::PrintTargetPS;
static LazyLogModule sDeviceContextSpecGTKLog("DeviceContextSpecGTK");
static nsCUPSShim sCupsShim;
nsDeviceContextSpecGTK::nsDeviceContextSpecGTK()
: mGtkPrintSettings(nullptr), mGtkPageSetup(nullptr) {}
@@ -316,73 +314,3 @@ NS_IMETHODIMP nsDeviceContextSpecGTK::EndDocument() {
}
return NS_OK;
}
// Printer List
NS_IMPL_ISUPPORTS(nsPrinterListGTK, nsIPrinterList)
NS_IMETHODIMP
nsPrinterListGTK::GetPrinters(nsTArray<RefPtr<nsIPrinter>>& aPrinters) {
if (!sCupsShim.IsInitialized()) {
if (!sCupsShim.Init()) {
return NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
}
}
// Build the CUPS printer list.
mozilla::CUPSPrinterList cupsPrinterList{sCupsShim};
cupsPrinterList.Initialize();
aPrinters.SetCapacity(cupsPrinterList.NumPrinters());
for (int i = 0; i < cupsPrinterList.NumPrinters(); i++) {
cups_dest_t* const dest = cupsPrinterList.GetPrinter(i);
aPrinters.AppendElement(nsPrinterCUPS::Create(sCupsShim, dest));
}
return NS_OK;
}
NS_IMETHODIMP
nsPrinterListGTK::GetSystemDefaultPrinterName(nsAString& aName) {
if (!sCupsShim.IsInitialized()) {
if (!sCupsShim.Init()) {
return NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
}
}
mozilla::CUPSPrinterList cupsPrinterList{sCupsShim};
cupsPrinterList.Initialize();
// TODO: Once the CUPSPrinterList has default printer support, use that.
if (cupsPrinterList.NumPrinters() != 0) {
CopyUTF8toUTF16(
mozilla::MakeStringSpan(cupsPrinterList.GetPrinter(0)->name), aName);
return NS_OK;
}
// No printers.
return NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
}
NS_IMETHODIMP
nsPrinterListGTK::InitPrintSettingsFromPrinter(
const nsAString& aPrinterName, nsIPrintSettings* aPrintSettings) {
NS_ENSURE_ARG_POINTER(aPrintSettings);
// Set a default file name.
nsAutoString filename;
nsresult rv = aPrintSettings->GetToFileName(filename);
if (NS_FAILED(rv) || filename.IsEmpty()) {
const char* path = PR_GetEnv("PWD");
if (!path) {
path = PR_GetEnv("HOME");
}
if (path) {
CopyUTF8toUTF16(MakeStringSpan(path), filename);
filename.AppendLiteral("/mozilla.pdf");
} else {
filename.AssignLiteral("mozilla.pdf");
}
aPrintSettings->SetToFileName(filename);
}
aPrintSettings->SetIsInitializedFromPrinter(true);
return NS_OK;
}

View File

@@ -59,17 +59,4 @@ class nsDeviceContextSpecGTK : public nsIDeviceContextSpec {
static gboolean PrinterEnumerator(GtkPrinter* aPrinter, gpointer aData);
};
//-------------------------------------------------------------------------
// Printer Enumerator
//-------------------------------------------------------------------------
class nsPrinterListGTK final : public nsIPrinterList {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIPRINTERLIST
nsPrinterListGTK() = default;
private:
~nsPrinterListGTK() = default;
};
#endif /* !nsDeviceContextSpecGTK_h___ */

View File

@@ -144,6 +144,7 @@ EXPORTS += [
'nsIRollupListener.h',
'nsIWidget.h',
'nsIWidgetListener.h',
'nsPrinterListBase.h',
'nsUserIdleService.h',
'nsWidgetInitData.h',
'nsWidgetsCID.h',
@@ -240,6 +241,7 @@ if CONFIG['MOZ_XUL'] and CONFIG['NS_PRINTING']:
'nsPaper.cpp',
'nsPaperMargin.cpp',
'nsPrinterBase.cpp',
'nsPrinterListBase.cpp',
'nsPrintSession.cpp',
'nsPrintSettingsService.cpp',
]
@@ -249,6 +251,7 @@ if CONFIG['MOZ_XUL'] and CONFIG['NS_PRINTING']:
'CUPSPrinterList.cpp',
'nsCUPSShim.cpp',
'nsPrinterCUPS.cpp',
'nsPrinterListCUPS.cpp',
]
# nsBaseWidget.cpp needs to be built separately because of name clashes in the OS X headers

View File

@@ -0,0 +1,11 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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 "nsPrinterListBase.h"
nsPrinterListBase::nsPrinterListBase() = default;
nsPrinterListBase::~nsPrinterListBase() = default;
NS_IMPL_ISUPPORTS(nsPrinterListBase, nsIPrinterList)

View File

@@ -0,0 +1,25 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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/. */
#ifndef nsPrinterListBase_h__
#define nsPrinterListBase_h__
#include "nsIPrinterList.h"
#include "nsISupportsImpl.h"
class nsPrinterListBase : public nsIPrinterList {
public:
NS_DECL_ISUPPORTS
// No copy or move, we're an identity.
nsPrinterListBase(const nsPrinterListBase&) = delete;
nsPrinterListBase(nsPrinterListBase&&) = delete;
protected:
nsPrinterListBase();
virtual ~nsPrinterListBase();
};
#endif

View File

@@ -0,0 +1,90 @@
/* 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 "nsPrinterListCUPS.h"
#include "CUPSPrinterList.h"
#include "nsCUPSShim.h"
#include "nsPrinterCUPS.h"
#include "prenv.h"
static nsCUPSShim sCupsShim;
NS_IMETHODIMP
nsPrinterListCUPS::GetPrinters(nsTArray<RefPtr<nsIPrinter>>& aPrinters) {
if (!sCupsShim.IsInitialized()) {
if (!sCupsShim.Init()) {
return NS_ERROR_FAILURE;
}
}
mozilla::CUPSPrinterList cupsPrinterList(sCupsShim);
cupsPrinterList.Initialize();
aPrinters.SetCapacity(cupsPrinterList.NumPrinters());
for (int i = 0; i < cupsPrinterList.NumPrinters(); i++) {
cups_dest_t* const dest = cupsPrinterList.GetPrinter(i);
nsString displayName;
GetDisplayNameForPrinter(*dest, displayName);
RefPtr<nsPrinterCUPS> cupsPrinter =
nsPrinterCUPS::Create(sCupsShim, dest, displayName);
aPrinters.AppendElement(cupsPrinter);
}
return NS_OK;
}
NS_IMETHODIMP
nsPrinterListCUPS::GetSystemDefaultPrinterName(nsAString& aName) {
if (!sCupsShim.IsInitialized()) {
if (!sCupsShim.Init()) {
return NS_ERROR_FAILURE;
}
}
mozilla::CUPSPrinterList cupsPrinterList(sCupsShim);
cupsPrinterList.Initialize();
cups_dest_t* const dest = cupsPrinterList.GetDefaultPrinter();
if (!dest) {
return NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
}
GetDisplayNameForPrinter(*dest, aName);
if (aName.IsEmpty()) {
CopyUTF8toUTF16(mozilla::MakeStringSpan(dest->name), aName);
}
return NS_OK;
}
NS_IMETHODIMP
nsPrinterListCUPS::InitPrintSettingsFromPrinter(
const nsAString& aPrinterName, nsIPrintSettings* aPrintSettings) {
MOZ_ASSERT(aPrintSettings);
// Set a default file name.
nsAutoString filename;
nsresult rv = aPrintSettings->GetToFileName(filename);
if (NS_FAILED(rv) || filename.IsEmpty()) {
const char* path = PR_GetEnv("PWD");
if (!path) {
path = PR_GetEnv("HOME");
}
if (path) {
CopyUTF8toUTF16(mozilla::MakeStringSpan(path), filename);
filename.AppendLiteral("/mozilla.pdf");
} else {
filename.AssignLiteral("mozilla.pdf");
}
aPrintSettings->SetToFileName(filename);
}
aPrintSettings->SetIsInitializedFromPrinter(true);
return NS_OK;
}

View File

@@ -0,0 +1,30 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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/. */
#ifndef nsPrinterListCUPS_h__
#define nsPrinterListCUPS_h__
#include "nsPrinterListBase.h"
#include "nsStringFwd.h"
struct cups_dest_s;
class nsPrinterListCUPS final : public nsPrinterListBase {
NS_DECL_NSIPRINTERLIST
#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;
};
#endif

View File

@@ -516,8 +516,6 @@ nsPrinterListWin::~nsPrinterListWin() {
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
}
NS_IMPL_ISUPPORTS(nsPrinterListWin, nsIPrinterList)
NS_IMETHODIMP
nsPrinterListWin::GetSystemDefaultPrinterName(nsAString& aName) {
GlobalPrinters::GetInstance()->GetDefaultPrinterName(aName);

View File

@@ -8,7 +8,7 @@
#include "nsCOMPtr.h"
#include "nsIDeviceContextSpec.h"
#include "nsIPrinterList.h"
#include "nsPrinterListBase.h"
#include "nsIPrintSettings.h"
#include <windows.h>
#include "mozilla/Attributes.h"
@@ -83,9 +83,8 @@ class nsDeviceContextSpecWin : public nsIDeviceContextSpec {
//-------------------------------------------------------------------------
// Printer List
//-------------------------------------------------------------------------
class nsPrinterListWin final : public nsIPrinterList {
class nsPrinterListWin final : public nsPrinterListBase {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIPRINTERLIST
nsPrinterListWin() = default;