Bug 1658300 - Implement NamedPrinter() for nsPrinterListWin, and remove the base implementation. r=bobowen

Differential Revision: https://phabricator.services.mozilla.com/D87450
This commit is contained in:
Jonathan Kew
2020-08-20 11:21:34 +00:00
parent 3b7d4b07a4
commit a666c43683
4 changed files with 46 additions and 21 deletions

View File

@@ -70,11 +70,6 @@ NS_IMETHODIMP nsPrinterListBase::GetNamedOrDefaultPrinter(
nsString{aPrinterName}); nsString{aPrinterName});
} }
Maybe<PrinterInfo> nsPrinterListBase::NamedPrinter(nsString aName) const {
// TODO: This should be removed once the Win32 backend lands.
return Nothing();
}
Maybe<PrinterInfo> nsPrinterListBase::NamedOrDefaultPrinter( Maybe<PrinterInfo> nsPrinterListBase::NamedOrDefaultPrinter(
nsString aName) const { nsString aName) const {
if (Maybe<PrinterInfo> value = NamedPrinter(std::move(aName))) { if (Maybe<PrinterInfo> value = NamedPrinter(std::move(aName))) {

View File

@@ -52,10 +52,11 @@ class nsPrinterListBase : public nsIPrinterList {
nsPrinterListBase(); nsPrinterListBase();
virtual ~nsPrinterListBase(); virtual ~nsPrinterListBase();
// Implemented in terms of Printers() and then searching the returned printer // This could be implemented in terms of Printers() and then searching the
// info for a printer of the given name. Backends might have more efficient // returned printer info for a printer of the given name, but we expect
// methods of implementing this. // backends to have more efficient methods of implementing this.
virtual Maybe<PrinterInfo> NamedPrinter(nsString aName) const; virtual Maybe<PrinterInfo> NamedPrinter(nsString aName) const = 0;
// This is implemented separately from the IDL interface version so that it // This is implemented separately from the IDL interface version so that it
// can be made const, which allows it to be used while resolving promises. // can be made const, which allows it to be used while resolving promises.
virtual nsresult SystemDefaultPrinterName(nsAString&) const = 0; virtual nsresult SystemDefaultPrinterName(nsAString&) const = 0;

View File

@@ -484,12 +484,10 @@ nsresult nsDeviceContextSpecWin::GetDataFromPrinter(const nsAString& aName,
nsPrinterListWin::~nsPrinterListWin() = default; nsPrinterListWin::~nsPrinterListWin() = default;
nsTArray<nsPrinterListBase::PrinterInfo> nsPrinterListWin::Printers() const { // Helper to get the array of PRINTER_INFO_4 records from the OS into a
PR_PL(("EnumerateNativePrinters\n")); // caller-supplied byte array; returns the number of records present.
static unsigned GetPrinterInfo4(nsTArray<BYTE>& aBuffer) {
const DWORD kLevel = 4; const DWORD kLevel = 4;
using RecType = PRINTER_INFO_4;
DWORD needed = 0; DWORD needed = 0;
DWORD count = 0; DWORD count = 0;
const DWORD kFlags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS; const DWORD kFlags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS;
@@ -500,20 +498,30 @@ nsTArray<nsPrinterListBase::PrinterInfo> nsPrinterListWin::Printers() const {
0, // cbBuf (buffer size) 0, // cbBuf (buffer size)
&needed, // Bytes needed in buffer &needed, // Bytes needed in buffer
&count); &count);
if (needed > 0) {
aBuffer.SetLength(needed);
ok = EnumPrinters(kFlags, nullptr, kLevel, aBuffer.Elements(),
aBuffer.Length(), &needed, &count);
}
if (!ok || !count) {
return 0;
}
return count;
}
nsTArray<nsPrinterListBase::PrinterInfo> nsPrinterListWin::Printers() const {
PR_PL(("nsPrinterListWin::Printers\n"));
AutoTArray<BYTE, 1024> buffer; AutoTArray<BYTE, 1024> buffer;
if (needed > 0) { unsigned count = GetPrinterInfo4(buffer);
buffer.SetLength(needed);
ok = EnumPrinters(kFlags, nullptr, kLevel, buffer.Elements(),
buffer.Length(), &needed, &count);
}
if (!ok || !count) { if (!count) {
PR_PL(("[No printers found]\n")); PR_PL(("[No printers found]\n"));
return {}; return {};
} }
auto* printers = reinterpret_cast<const RecType*>(buffer.Elements()); const auto* printers =
reinterpret_cast<const PRINTER_INFO_4*>(buffer.Elements());
nsTArray<PrinterInfo> list; nsTArray<PrinterInfo> list;
for (unsigned i = 0; i < count; i++) { for (unsigned i = 0; i < count; i++) {
list.AppendElement(PrinterInfo{nsString(printers[i].pPrinterName)}); list.AppendElement(PrinterInfo{nsString(printers[i].pPrinterName)});
@@ -524,6 +532,25 @@ nsTArray<nsPrinterListBase::PrinterInfo> nsPrinterListWin::Printers() const {
return list; return list;
} }
Maybe<nsPrinterListBase::PrinterInfo> nsPrinterListWin::NamedPrinter(
nsString aName) const {
Maybe<PrinterInfo> rv;
AutoTArray<BYTE, 1024> buffer;
unsigned count = GetPrinterInfo4(buffer);
const auto* printers =
reinterpret_cast<const PRINTER_INFO_4*>(buffer.Elements());
for (unsigned i = 0; i < count; ++i) {
if (aName.Equals(nsString(printers[i].pPrinterName))) {
rv.emplace(PrinterInfo{aName});
break;
}
}
return rv;
}
RefPtr<nsIPrinter> nsPrinterListWin::CreatePrinter(PrinterInfo aInfo) const { RefPtr<nsIPrinter> nsPrinterListWin::CreatePrinter(PrinterInfo aInfo) const {
return nsPrinterWin::Create(std::move(aInfo.mName)); return nsPrinterWin::Create(std::move(aInfo.mName));
} }

View File

@@ -96,6 +96,8 @@ class nsPrinterListWin final : public nsPrinterListBase {
protected: protected:
nsresult SystemDefaultPrinterName(nsAString&) const final; nsresult SystemDefaultPrinterName(nsAString&) const final;
Maybe<PrinterInfo> NamedPrinter(nsString) const final;
private: private:
~nsPrinterListWin(); ~nsPrinterListWin();
}; };