0

Add a helper to open Windows printer handles.

Use it in PrintBackendWin to simplify callers that do exactly the same
thing.

Change-Id: I8ffad95b1581d01f18b00122280c4dac5d0cfe5e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1769593
Reviewed-by: Rebekah Potter <rbpotter@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#691838}
This commit is contained in:
Lei Zhang
2019-08-29 22:47:47 +00:00
committed by Commit Bot
parent 9c6af4b5cf
commit f20ddd184e

@ -26,6 +26,12 @@ namespace printing {
namespace {
ScopedPrinterHandle GetPrinterHandle(const std::string& printer_name) {
ScopedPrinterHandle handle;
handle.OpenPrinterWithName(base::UTF8ToWide(printer_name).c_str());
return handle;
}
HRESULT StreamOnHGlobalToString(IStream* stream, std::string* out) {
DCHECK(stream);
DCHECK(out);
@ -223,11 +229,9 @@ std::string PrintBackendWin::GetDefaultPrinterName() {
bool PrintBackendWin::GetPrinterBasicInfo(const std::string& printer_name,
PrinterBasicInfo* printer_info) {
ScopedPrinterHandle printer_handle;
if (!printer_handle.OpenPrinterWithName(
base::UTF8ToWide(printer_name).c_str())) {
ScopedPrinterHandle printer_handle = GetPrinterHandle(printer_name);
if (!printer_handle.IsValid())
return false;
}
if (!InitBasicPrinterInfo(printer_handle.Get(), printer_info))
return false;
@ -240,9 +244,8 @@ bool PrintBackendWin::GetPrinterBasicInfo(const std::string& printer_name,
bool PrintBackendWin::GetPrinterSemanticCapsAndDefaults(
const std::string& printer_name,
PrinterSemanticCapsAndDefaults* printer_info) {
ScopedPrinterHandle printer_handle;
if (!printer_handle.OpenPrinterWithName(
base::UTF8ToWide(printer_name).c_str())) {
ScopedPrinterHandle printer_handle = GetPrinterHandle(printer_name);
if (!printer_handle.IsValid()) {
LOG(WARNING) << "Failed to open printer, error = " << GetLastError();
return false;
}
@ -378,16 +381,13 @@ bool PrintBackendWin::GetPrinterCapsAndDefaults(
// Gets the information about driver for a specific printer.
std::string PrintBackendWin::GetPrinterDriverInfo(
const std::string& printer_name) {
ScopedPrinterHandle printer;
if (!printer.OpenPrinterWithName(base::UTF8ToWide(printer_name).c_str()))
return std::string();
return GetDriverInfo(printer.Get());
ScopedPrinterHandle printer = GetPrinterHandle(printer_name);
return printer.IsValid() ? GetDriverInfo(printer.Get()) : std::string();
}
bool PrintBackendWin::IsValidPrinter(const std::string& printer_name) {
ScopedPrinterHandle printer_handle;
return printer_handle.OpenPrinterWithName(
base::UTF8ToWide(printer_name).c_str());
ScopedPrinterHandle printer_handle = GetPrinterHandle(printer_name);
return printer_handle.IsValid();
}
// static