Skip to content

Commit

Permalink
refactor: get default printer async
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jan 30, 2020
1 parent eba7511 commit e43d53f
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions shell/browser/api/atom_api_web_contents.cc
Expand Up @@ -346,6 +346,18 @@ base::Optional<base::TimeDelta> GetCursorBlinkInterval() {
return base::nullopt;
}

#if BUILDFLAG(ENABLE_PRINTING)
base::string16 GetDefaultPrinterAsync() {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);

auto print_backend = printing::PrintBackend::CreateInstance(
nullptr, g_browser_process->GetApplicationLocale());
std::string printer_name = print_backend->GetDefaultPrinterName();
return base::UTF8ToUTF16(printer_name);
}
#endif

} // namespace

WebContents::WebContents(v8::Isolate* isolate,
Expand Down Expand Up @@ -1779,14 +1791,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);
Expand Down Expand Up @@ -1866,14 +1873,31 @@ 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(
[](base::Value print_settings,
printing::CompletionCallback print_callback,
base::string16 device_name, bool silent,
content::WebContents* contents, 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(contents);
auto* focused_frame = contents->GetFocusedFrame();
auto* rfh = focused_frame && focused_frame->HasSelection()
? focused_frame
: contents->GetMainFrame();

print_view_manager->PrintNow(rfh, silent, std::move(print_settings),
std::move(print_callback));
},
std::move(settings), std::move(callback), device_name, silent,
base::Unretained(web_contents())));
}

std::vector<printing::PrinterBasicInfo> WebContents::GetPrinterList() {
Expand Down

0 comments on commit e43d53f

Please sign in to comment.