Bug 1659807 - Add nsIPrinter.systemName and nsIPrinterList.getPrinterBySystemName r=emilio

This also renames nsIPrinterList.getNamedPrinter to nsIPrinter.getPrinterByName
for consistency.

This is cheaper to lookup on platforms that use CUPS and do not show the Unix
name for printers. This only applies to OS X at the present.

Differential Revision: https://phabricator.services.mozilla.com/D90201
This commit is contained in:
Emily McDonough 2020-09-15 21:23:27 +00:00
Родитель d77b5bdf35
Коммит 5dbd0055be
12 изменённых файлов: 81 добавлений и 15 удалений

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

@ -14,6 +14,14 @@ interface nsIPrinter : nsISupports
*/
readonly attribute AString name;
/**
* The system name of the printer.
*
* This may be faster for lookup in nsIPrinterList functions, but will only
* work for functions that will accept the system name.
*/
readonly attribute AString systemName;
/**
* Creates a Promise that will resolve to an nsIPrintSettings object containing
* the default settings for this printer. For convenience, a new, mutable

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

@ -31,7 +31,15 @@ interface nsIPrinterList : nsISupports
* Returns a promise that resolves to the printer of a given name, or is
* rejected if there is no such printer.
*/
[implicit_jscontext] Promise getNamedPrinter(in AString aPrinterName);
[implicit_jscontext] Promise getPrinterByName(in AString aPrinterName);
/**
* Returns a promise that resolves to the printer of a given system name, or
* is rejected if there is no such printer.
* This may be more efficient than using getNamedPrinter, but requires the
* caller to know the system name of the printer they want to find.
*/
[implicit_jscontext] Promise getPrinterBySystemName(in AString aPrinterName);
/**
* Returns a promise that resolves to the printer of the given name, or

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

@ -137,6 +137,12 @@ nsPrinterCUPS::GetName(nsAString& aName) {
return NS_OK;
}
NS_IMETHODIMP
nsPrinterCUPS::GetSystemName(nsAString& aName) {
CopyUTF8toUTF16(MakeStringSpan(mPrinter->name), aName);
return NS_OK;
}
void nsPrinterCUPS::GetPrinterName(nsAString& aName) const {
if (mDisplayName.IsEmpty()) {
aName.Truncate();

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

@ -20,6 +20,7 @@
class nsPrinterCUPS final : public nsPrinterBase {
public:
NS_IMETHOD GetName(nsAString& aName) override;
NS_IMETHOD GetSystemName(nsAString& aName) override;
PrintSettingsInitializer DefaultSettings() const final;
bool SupportsDuplex() const final;
bool SupportsColor() const final;

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

@ -56,14 +56,21 @@ NS_IMETHODIMP nsPrinterListBase::GetPrinters(JSContext* aCx,
&nsPrinterListBase::Printers);
}
NS_IMETHODIMP nsPrinterListBase::GetNamedPrinter(const nsAString& aPrinterName,
JSContext* aCx,
Promise** aResult) {
return PrintBackgroundTaskPromise(*this, aCx, aResult, "NamedPrinter"_ns,
&nsPrinterListBase::NamedPrinter,
NS_IMETHODIMP nsPrinterListBase::GetPrinterByName(const nsAString& aPrinterName,
JSContext* aCx,
Promise** aResult) {
return PrintBackgroundTaskPromise(*this, aCx, aResult, "PrinterByName"_ns,
&nsPrinterListBase::PrinterByName,
nsString{aPrinterName});
}
NS_IMETHODIMP nsPrinterListBase::GetPrinterBySystemName(
const nsAString& aPrinterName, JSContext* aCx, Promise** aResult) {
return PrintBackgroundTaskPromise(
*this, aCx, aResult, "PrinterBySystemName"_ns,
&nsPrinterListBase::PrinterBySystemName, nsString{aPrinterName});
}
NS_IMETHODIMP nsPrinterListBase::GetNamedOrDefaultPrinter(
const nsAString& aPrinterName, JSContext* aCx, Promise** aResult) {
return PrintBackgroundTaskPromise(
@ -73,14 +80,14 @@ NS_IMETHODIMP nsPrinterListBase::GetNamedOrDefaultPrinter(
Maybe<PrinterInfo> nsPrinterListBase::NamedOrDefaultPrinter(
nsString aName) const {
if (Maybe<PrinterInfo> value = NamedPrinter(std::move(aName))) {
if (Maybe<PrinterInfo> value = PrinterByName(std::move(aName))) {
return value;
}
// Since the name had to be passed by-value, we can re-use it to fetch the
// default printer name, potentially avoiding an extra string allocation.
if (NS_SUCCEEDED(SystemDefaultPrinterName(aName))) {
return NamedPrinter(std::move(aName));
return PrinterByName(std::move(aName));
}
return Nothing();

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

@ -23,8 +23,10 @@ class nsPrinterListBase : public nsIPrinterList {
return SystemDefaultPrinterName(aName);
}
NS_IMETHOD GetPrinters(JSContext*, Promise**) final;
NS_IMETHOD GetNamedPrinter(const nsAString& aPrinterName, JSContext* aCx,
Promise** aResult) final;
NS_IMETHOD GetPrinterByName(const nsAString& aPrinterName, JSContext* aCx,
Promise** aResult) final;
NS_IMETHOD GetPrinterBySystemName(const nsAString& aPrinterName,
JSContext* aCx, Promise** aResult) final;
NS_IMETHOD GetNamedOrDefaultPrinter(const nsAString& aPrinterName,
JSContext* aCx, Promise** aResult) final;
NS_IMETHOD GetFallbackPaperList(JSContext*, Promise**) final;
@ -57,7 +59,13 @@ class nsPrinterListBase : public nsIPrinterList {
// 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;
virtual Maybe<PrinterInfo> PrinterByName(nsString aName) const = 0;
// Same as NamedPrinter, but uses the system name.
// Depending on whether or not there is a more efficient way to address the
// printer for a given backend, this may or may not be equivalent to
// NamedPrinter.
virtual Maybe<PrinterInfo> PrinterBySystemName(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.

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

@ -66,7 +66,7 @@ RefPtr<nsIPrinter> nsPrinterListCUPS::CreatePrinter(PrinterInfo aInfo) const {
static_cast<cups_dest_t*>(aInfo.mCupsHandle));
}
Maybe<PrinterInfo> nsPrinterListCUPS::NamedPrinter(
Maybe<PrinterInfo> nsPrinterListCUPS::PrinterByName(
nsString aPrinterName) const {
Maybe<PrinterInfo> rv;
if (!sCupsShim.EnsureInitialized()) {
@ -116,6 +116,20 @@ Maybe<PrinterInfo> nsPrinterListCUPS::NamedPrinter(
return rv;
}
Maybe<PrinterInfo> nsPrinterListCUPS::PrinterBySystemName(
nsString aPrinterName) const {
Maybe<PrinterInfo> rv;
if (!sCupsShim.EnsureInitialized()) {
return rv;
}
const auto printerName = NS_ConvertUTF16toUTF8(aPrinterName);
if (cups_dest_t* const printer = sCupsShim.cupsGetNamedDest(
CUPS_HTTP_DEFAULT, printerName.get(), nullptr)) {
rv.emplace(PrinterInfo{std::move(aPrinterName), printer});
}
return rv;
}
nsresult nsPrinterListCUPS::SystemDefaultPrinterName(nsAString& aName) const {
aName.Truncate();

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

@ -15,7 +15,8 @@ class nsPrinterListCUPS final : public nsPrinterListBase {
nsTArray<PrinterInfo> Printers() const final;
RefPtr<nsIPrinter> CreatePrinter(PrinterInfo) const final;
Maybe<PrinterInfo> NamedPrinter(nsString aPrinterName) const final;
Maybe<PrinterInfo> PrinterByName(nsString aPrinterName) const final;
Maybe<PrinterInfo> PrinterBySystemName(nsString aPrinterName) const final;
nsresult SystemDefaultPrinterName(nsAString&) const final;
private:

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

@ -545,7 +545,7 @@ nsTArray<nsPrinterListBase::PrinterInfo> nsPrinterListWin::Printers() const {
return list;
}
Maybe<nsPrinterListBase::PrinterInfo> nsPrinterListWin::NamedPrinter(
Maybe<nsPrinterListBase::PrinterInfo> nsPrinterListWin::PrinterByName(
nsString aName) const {
Maybe<PrinterInfo> rv;
@ -564,6 +564,11 @@ Maybe<nsPrinterListBase::PrinterInfo> nsPrinterListWin::NamedPrinter(
return rv;
}
Maybe<nsPrinterListBase::PrinterInfo> nsPrinterListWin::PrinterBySystemName(
nsString aName) const {
return PrinterByName(std::move(aName));
}
RefPtr<nsIPrinter> nsPrinterListWin::CreatePrinter(PrinterInfo aInfo) const {
return nsPrinterWin::Create(std::move(aInfo.mName));
}

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

@ -98,7 +98,8 @@ class nsPrinterListWin final : public nsPrinterListBase {
protected:
nsresult SystemDefaultPrinterName(nsAString&) const final;
Maybe<PrinterInfo> NamedPrinter(nsString) const final;
Maybe<PrinterInfo> PrinterByName(nsString) const final;
Maybe<PrinterInfo> PrinterBySystemName(nsString aPrinterName) const final;
private:
~nsPrinterListWin();

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

@ -153,6 +153,12 @@ nsPrinterWin::GetName(nsAString& aName) {
return NS_OK;
}
NS_IMETHODIMP
nsPrinterWin::GetSystemName(nsAString& aName) {
aName.Assign(mName);
return NS_OK;
}
bool nsPrinterWin::SupportsDuplex() const {
return ::DeviceCapabilitiesW(mName.get(), nullptr, DC_DUPLEX, nullptr,
nullptr) == 1;

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

@ -11,6 +11,7 @@
class nsPrinterWin final : public nsPrinterBase {
public:
NS_IMETHOD GetName(nsAString& aName) override;
NS_IMETHOD GetSystemName(nsAString& aName) override;
PrintSettingsInitializer DefaultSettings() const final;
bool SupportsDuplex() const final;
bool SupportsColor() const final;