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