fix: default printer if none is provided (#21956)

This commit is contained in:
Shelley Vohr 2020-01-31 19:20:11 +00:00 committed by GitHub
parent 1c6d8f5af6
commit 08b1f0e25d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 41 deletions

View file

@ -113,15 +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"
#if defined(OS_WIN)
#include "printing/backend/win_helper.h"
#endif
#endif
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
#include "shell/browser/extensions/atom_extension_web_contents_observer.h"
#endif
@ -368,6 +359,17 @@ bool IsDeviceNameValid(const base::string16& device_name) {
#endif
return true;
}
base::string16 GetDefaultPrinterAsync() {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
scoped_refptr<printing::PrintBackend> backend =
printing::PrintBackend::CreateInstance(
nullptr, g_browser_process->GetApplicationLocale());
std::string printer_name = backend->GetDefaultPrinterName();
return base::UTF8ToUTF16(printer_name);
}
#endif
} // namespace
@ -1737,6 +1739,27 @@ 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());
@ -1801,8 +1824,8 @@ void WebContents::Print(gin_helper::Arguments* args) {
options.Get("landscape", &landscape);
settings.SetBoolKey(printing::kSettingLandscape, landscape);
// We set the default to empty string here and only update
// if at the Chromium level if it's non-empty
// We set the default to the system's default printer and only update
// if at the Chromium level if the user overrides.
// Printer device name as opened by the OS.
base::string16 device_name;
options.Get("deviceName", &device_name);
@ -1810,7 +1833,6 @@ void WebContents::Print(gin_helper::Arguments* args) {
args->ThrowError("webContents.print(): Invalid deviceName provided.");
return;
}
settings.SetStringKey(printing::kSettingDeviceName, device_name);
int scale_factor = 100;
options.Get("scaleFactor", &scale_factor);
@ -1890,14 +1912,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<printing::PrinterBasicInfo> WebContents::GetPrinterList() {

View file

@ -32,8 +32,14 @@
#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"
#if defined(OS_WIN)
#include "printing/backend/win_helper.h"
#endif
#endif
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
@ -190,6 +196,11 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
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<printing::PrinterBasicInfo> GetPrinterList();
// Print current page as PDF.