Skip to content

Commit

Permalink
Bug 1659807 - Add nsIPrinter.systemName and nsIPrinterList.getPrinter…
Browse files Browse the repository at this point in the history
…BySystemName 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
  • Loading branch information
Emily McDonough committed Sep 15, 2020
1 parent 7358603 commit d929f6c
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 15 deletions.
8 changes: 8 additions & 0 deletions widget/nsIPrinter.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion widget/nsIPrinterList.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions widget/nsPrinterCUPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions widget/nsPrinterCUPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 14 additions & 7 deletions widget/nsPrinterListBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand Down
14 changes: 11 additions & 3 deletions widget/nsPrinterListBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion widget/nsPrinterListCUPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion widget/nsPrinterListCUPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion widget/windows/nsDeviceContextSpecWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
Expand Down
3 changes: 2 additions & 1 deletion widget/windows/nsDeviceContextSpecWin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions widget/windows/nsPrinterWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions widget/windows/nsPrinterWin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d929f6c

Please sign in to comment.