From 312dcbe844f65afe1fedcfc1ae2a47a28a0b7f3e Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 29 Jan 2020 10:50:41 -0800 Subject: [PATCH] refactor: get default printer async --- shell/browser/api/atom_api_web_contents.cc | 59 +++++++++++++++------- shell/browser/api/atom_api_web_contents.h | 7 +++ 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index 0d4e46715bae4..94755f9f95c01 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -113,11 +113,6 @@ #include "ui/gfx/font_render_params.h" #endif -#if BUILDFLAG(ENABLE_PRINTING) -#include "chrome/browser/printing/print_view_manager_basic.h" -#include "components/printing/common/print_messages.h" -#endif - #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) #include "shell/browser/extensions/atom_extension_web_contents_observer.h" #endif @@ -346,6 +341,19 @@ base::Optional GetCursorBlinkInterval() { return base::nullopt; } +#if BUILDFLAG(ENABLE_PRINTING) +base::string16 GetDefaultPrinterAsync() { + base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, + base::BlockingType::MAY_BLOCK); + + scoped_refptr backend = + printing::PrintBackend::CreateInstance( + nullptr, g_browser_process->GetApplicationLocale()); + std::string printer_name = backend->GetDefaultPrinterName(); + return base::UTF8ToUTF16(printer_name); +} +#endif + } // namespace WebContents::WebContents(v8::Isolate* isolate, @@ -1713,6 +1721,25 @@ bool WebContents::IsCurrentlyAudible() { } #if BUILDFLAG(ENABLE_PRINTING) +void WebContents::OnGetDefaultPrinter( + base::Value print_settings, + printing::CompletionCallback print_callback, + base::string16 device_name, + bool silent, + base::string16 default_printer) { + base::string16 printer_name = + device_name.empty() ? default_printer : device_name; + print_settings.SetStringKey(printing::kSettingDeviceName, printer_name); + auto* print_view_manager = + printing::PrintViewManagerBasic::FromWebContents(web_contents()); + auto* focused_frame = web_contents()->GetFocusedFrame(); + auto* rfh = focused_frame && focused_frame->HasSelection() + ? focused_frame + : web_contents()->GetMainFrame(); + print_view_manager->PrintNow(rfh, silent, std::move(print_settings), + std::move(print_callback)); +} + void WebContents::Print(gin_helper::Arguments* args) { gin_helper::Dictionary options = gin::Dictionary::CreateEmpty(args->isolate()); @@ -1779,14 +1806,9 @@ void WebContents::Print(gin_helper::Arguments* args) { // We set the default to the system's default printer and only update // if at the Chromium level if the user overrides. - auto print_backend = printing::PrintBackend::CreateInstance( - nullptr, g_browser_process->GetApplicationLocale()); - std::string default_printer = print_backend->GetDefaultPrinterName(); - base::string16 device_name = base::UTF8ToUTF16(default_printer); - // Printer device name as opened by the OS. + base::string16 device_name; options.Get("deviceName", &device_name); - settings.SetStringKey(printing::kSettingDeviceName, device_name); int scale_factor = 100; options.Get("scaleFactor", &scale_factor); @@ -1866,14 +1888,13 @@ void WebContents::Print(gin_helper::Arguments* args) { settings.SetIntKey(printing::kSettingDpiVertical, dpi); } - auto* print_view_manager = - printing::PrintViewManagerBasic::FromWebContents(web_contents()); - auto* focused_frame = web_contents()->GetFocusedFrame(); - auto* rfh = focused_frame && focused_frame->HasSelection() - ? focused_frame - : web_contents()->GetMainFrame(); - print_view_manager->PrintNow(rfh, silent, std::move(settings), - std::move(callback)); + base::PostTaskAndReplyWithResult( + FROM_HERE, + {base::ThreadPool(), base::MayBlock(), base::TaskPriority::USER_BLOCKING}, + base::BindOnce(&GetDefaultPrinterAsync), + base::BindOnce(&WebContents::OnGetDefaultPrinter, + weak_factory_.GetWeakPtr(), std::move(settings), + std::move(callback), device_name, silent)); } std::vector WebContents::GetPrinterList() { diff --git a/shell/browser/api/atom_api_web_contents.h b/shell/browser/api/atom_api_web_contents.h index e949682c1fc69..9d57b141d312d 100644 --- a/shell/browser/api/atom_api_web_contents.h +++ b/shell/browser/api/atom_api_web_contents.h @@ -32,6 +32,8 @@ #include "ui/gfx/image/image.h" #if BUILDFLAG(ENABLE_PRINTING) +#include "chrome/browser/printing/print_view_manager_basic.h" +#include "components/printing/common/print_messages.h" #include "printing/backend/print_backend.h" #include "shell/browser/printing/print_preview_message_handler.h" #endif @@ -190,6 +192,11 @@ class WebContents : public gin_helper::TrackableObject, bool IsBeingCaptured(); #if BUILDFLAG(ENABLE_PRINTING) + void OnGetDefaultPrinter(base::Value print_settings, + printing::CompletionCallback print_callback, + base::string16 device_name, + bool silent, + base::string16 default_printer); void Print(gin_helper::Arguments* args); std::vector GetPrinterList(); // Print current page as PDF.