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:
@@ -70,11 +70,6 @@ NS_IMETHODIMP nsPrinterListBase::GetNamedOrDefaultPrinter(
|
||||
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(
|
||||
nsString aName) const {
|
||||
if (Maybe<PrinterInfo> value = NamedPrinter(std::move(aName))) {
|
||||
|
||||
@@ -52,10 +52,11 @@ class nsPrinterListBase : public nsIPrinterList {
|
||||
nsPrinterListBase();
|
||||
virtual ~nsPrinterListBase();
|
||||
|
||||
// Implemented in terms of Printers() and then searching the returned printer
|
||||
// info for a printer of the given name. Backends might have more efficient
|
||||
// methods of implementing this.
|
||||
virtual Maybe<PrinterInfo> NamedPrinter(nsString aName) const;
|
||||
// This could be implemented in terms of Printers() and then searching the
|
||||
// returned printer info for a printer of the given name, but we expect
|
||||
// backends to have more efficient methods of implementing this.
|
||||
virtual Maybe<PrinterInfo> NamedPrinter(nsString aName) const = 0;
|
||||
|
||||
// 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.
|
||||
virtual nsresult SystemDefaultPrinterName(nsAString&) const = 0;
|
||||
|
||||
@@ -484,12 +484,10 @@ nsresult nsDeviceContextSpecWin::GetDataFromPrinter(const nsAString& aName,
|
||||
|
||||
nsPrinterListWin::~nsPrinterListWin() = default;
|
||||
|
||||
nsTArray<nsPrinterListBase::PrinterInfo> nsPrinterListWin::Printers() const {
|
||||
PR_PL(("EnumerateNativePrinters\n"));
|
||||
|
||||
// Helper to get the array of PRINTER_INFO_4 records from the OS into a
|
||||
// caller-supplied byte array; returns the number of records present.
|
||||
static unsigned GetPrinterInfo4(nsTArray<BYTE>& aBuffer) {
|
||||
const DWORD kLevel = 4;
|
||||
using RecType = PRINTER_INFO_4;
|
||||
|
||||
DWORD needed = 0;
|
||||
DWORD count = 0;
|
||||
const DWORD kFlags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS;
|
||||
@@ -500,20 +498,30 @@ nsTArray<nsPrinterListBase::PrinterInfo> nsPrinterListWin::Printers() const {
|
||||
0, // cbBuf (buffer size)
|
||||
&needed, // Bytes needed in buffer
|
||||
&count);
|
||||
|
||||
AutoTArray<BYTE, 1024> buffer;
|
||||
if (needed > 0) {
|
||||
buffer.SetLength(needed);
|
||||
ok = EnumPrinters(kFlags, nullptr, kLevel, buffer.Elements(),
|
||||
buffer.Length(), &needed, &count);
|
||||
aBuffer.SetLength(needed);
|
||||
ok = EnumPrinters(kFlags, nullptr, kLevel, aBuffer.Elements(),
|
||||
aBuffer.Length(), &needed, &count);
|
||||
}
|
||||
if (!ok || !count) {
|
||||
return 0;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
if (!ok || !count) {
|
||||
nsTArray<nsPrinterListBase::PrinterInfo> nsPrinterListWin::Printers() const {
|
||||
PR_PL(("nsPrinterListWin::Printers\n"));
|
||||
|
||||
AutoTArray<BYTE, 1024> buffer;
|
||||
unsigned count = GetPrinterInfo4(buffer);
|
||||
|
||||
if (!count) {
|
||||
PR_PL(("[No printers found]\n"));
|
||||
return {};
|
||||
}
|
||||
|
||||
auto* printers = reinterpret_cast<const RecType*>(buffer.Elements());
|
||||
const auto* printers =
|
||||
reinterpret_cast<const PRINTER_INFO_4*>(buffer.Elements());
|
||||
nsTArray<PrinterInfo> list;
|
||||
for (unsigned i = 0; i < count; i++) {
|
||||
list.AppendElement(PrinterInfo{nsString(printers[i].pPrinterName)});
|
||||
@@ -524,6 +532,25 @@ nsTArray<nsPrinterListBase::PrinterInfo> nsPrinterListWin::Printers() const {
|
||||
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 {
|
||||
return nsPrinterWin::Create(std::move(aInfo.mName));
|
||||
}
|
||||
|
||||
@@ -96,6 +96,8 @@ class nsPrinterListWin final : public nsPrinterListBase {
|
||||
protected:
|
||||
nsresult SystemDefaultPrinterName(nsAString&) const final;
|
||||
|
||||
Maybe<PrinterInfo> NamedPrinter(nsString) const final;
|
||||
|
||||
private:
|
||||
~nsPrinterListWin();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user