Skip to content

Commit

Permalink
chore: add logging for print_backend failures
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Sep 8, 2021
1 parent ee33374 commit ed6c90f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
5 changes: 4 additions & 1 deletion shell/browser/api/electron_api_printing.cc
Expand Up @@ -47,7 +47,10 @@ printing::PrinterList GetPrinterList() {
// TODO(deepak1556): Deprecate this api in favor of an
// async version and post a non blocing task call.
base::ThreadRestrictions::ScopedAllowIO allow_io;
print_backend->EnumeratePrinters(&printers);
printing::mojom::ResultCode code =
print_backend->EnumeratePrinters(&printers);
if (code != printing::mojom::ResultCode::kSuccess)
LOG(INFO) << "Failed to enumerate printers";
}
return printers;
}
Expand Down
26 changes: 19 additions & 7 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -424,7 +424,7 @@ bool IsDeviceNameValid(const std::u16string& device_name) {
#endif
}

std::u16string GetDefaultPrinterAsync() {
std::pair<std::string, std::u16string> GetDefaultPrinterAsync() {
#if defined(OS_WIN)
// Blocking is needed here because Windows printer drivers are oftentimes
// not thread-safe and have to be accessed on the UI thread.
Expand All @@ -435,18 +435,26 @@ std::u16string GetDefaultPrinterAsync() {
printing::PrintBackend::CreateInstance(
g_browser_process->GetApplicationLocale());
std::string printer_name;
print_backend->GetDefaultPrinterName(printer_name);
printing::mojom::ResultCode code =
print_backend->GetDefaultPrinterName(printer_name);
if (code != printing::mojom::ResultCode::kSuccess)
return std::make_pair("Failed to get default printer name",
std::u16string());

// Some devices won't have a default printer, so we should
// also check for existing printers and pick the first
// one should it exist.
if (printer_name.empty()) {
printing::PrinterList printers;
print_backend->EnumeratePrinters(&printers);
printing::mojom::ResultCode code =
print_backend->EnumeratePrinters(&printers);
if (code != printing::mojom::ResultCode::kSuccess)
return std::make_pair("Failed to enumerate printers", std::u16string());
if (!printers.empty())
printer_name = printers.front().printer_name;
}
return base::UTF8ToUTF16(printer_name);

return std::make_pair(std::string(), base::UTF8ToUTF16(printer_name));
}

// Copied from
Expand Down Expand Up @@ -2409,7 +2417,7 @@ void WebContents::OnGetDefaultPrinter(
printing::CompletionCallback print_callback,
std::u16string device_name,
bool silent,
std::u16string default_printer) {
std::pair<std::string, std::u16string> info) {
// The content::WebContents might be already deleted at this point, and the
// PrintViewManagerElectron class does not do null check.
if (!web_contents()) {
Expand All @@ -2418,8 +2426,12 @@ void WebContents::OnGetDefaultPrinter(
return;
}

std::u16string printer_name =
device_name.empty() ? default_printer : device_name;
if (!info.first.empty()) {
std::move(print_callback).Run(false, info.first);
return;
}

std::u16string printer_name = info.second.empty() ? info.second : device_name;

// If there are no valid printers available on the network, we bail.
if (printer_name.empty() || !IsDeviceNameValid(printer_name)) {
Expand Down
3 changes: 2 additions & 1 deletion shell/browser/api/electron_api_web_contents.h
Expand Up @@ -215,7 +215,8 @@ class WebContents : public gin::Wrappable<WebContents>,
printing::CompletionCallback print_callback,
std::u16string device_name,
bool silent,
std::u16string default_printer);
// <error, default_printer_name>
std::pair<std::string, std::u16string> info);
void Print(gin::Arguments* args);
// Print current page as PDF.
v8::Local<v8::Promise> PrintToPDF(base::DictionaryValue settings);
Expand Down

0 comments on commit ed6c90f

Please sign in to comment.