Skip to content

Commit

Permalink
fix: default printer if none is provided (#21956)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Feb 11, 2020
1 parent 0b00ff6 commit 612b4db
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
18 changes: 0 additions & 18 deletions patches/chromium/printing.patch
Expand Up @@ -600,24 +600,6 @@ index 71c0c15217b62cd7a6087c6d9ae50481f9041d5f..18d853d7f808aaf816de86e8c5b82317

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Set options for print preset from source PDF document.
diff --git a/printing/print_settings_conversion.cc b/printing/print_settings_conversion.cc
index 17c363ff9aa2e2262cacd0c9baea3820334bf67b..5b02461c2e9afe254405ddacd904e4bdbddd0b8b 100644
--- a/printing/print_settings_conversion.cc
+++ b/printing/print_settings_conversion.cc
@@ -184,11 +184,12 @@ bool PrintSettingsFromJobSettings(const base::Value& job_settings,

settings->set_dpi_xy(dpi_horizontal.value(), dpi_vertical.value());
#endif
+ if (!device_name->empty())
+ settings->set_device_name(base::UTF8ToUTF16(*device_name));

settings->set_collate(collate.value());
settings->set_copies(copies.value());
settings->SetOrientation(landscape.value());
- settings->set_device_name(base::UTF8ToUTF16(*device_name));
settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode.value()));
settings->set_color(static_cast<ColorModel>(color.value()));
settings->set_scale_factor(static_cast<double>(scale_factor.value()) / 100.0);
diff --git a/printing/printing_context.cc b/printing/printing_context.cc
index cd5c27c87df175676504a06b4e1904f6b836dc90..c4f6acf66bc69f1e7db633aa5b3b03a913ffb666 100644
--- a/printing/printing_context.cc
Expand Down
66 changes: 44 additions & 22 deletions shell/browser/api/atom_api_web_contents.cc
Expand Up @@ -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
Expand Down Expand Up @@ -335,6 +326,16 @@ 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);
std::string printer_name = backend->GetDefaultPrinterName();
return base::UTF8ToUTF16(printer_name);
}
#endif
} // namespace

Expand Down Expand Up @@ -1670,6 +1671,30 @@ bool WebContents::IsCurrentlyAudible() {
}

#if BUILDFLAG(ENABLE_PRINTING)
void WebContents::OnGetDefaultPrinter(
base::DictionaryValue 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,
std::make_unique<PrintMsg_PrintPages>(rfh->GetRoutingID(), silent,
std::move(print_settings)),
std::move(print_callback));
}

void WebContents::Print(mate::Arguments* args) {
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
base::DictionaryValue settings;
Expand Down Expand Up @@ -1733,15 +1758,15 @@ void WebContents::Print(mate::Arguments* args) {
options.Get("landscape", &landscape);
settings.SetBoolean(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);
if (!device_name.empty() && !IsDeviceNameValid(device_name)) {
args->ThrowError("webContents.print(): Invalid deviceName provided.");
return;
}
settings.SetString(printing::kSettingDeviceName, device_name);

int scale_factor = 100;
options.Get("scaleFactor", &scale_factor);
Expand Down Expand Up @@ -1808,16 +1833,13 @@ void WebContents::Print(mate::Arguments* args) {
settings.SetInteger(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,
std::make_unique<PrintMsg_PrintPages>(
rfh->GetRoutingID(), silent, 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() {
Expand Down
11 changes: 11 additions & 0 deletions shell/browser/api/atom_api_web_contents.h
Expand Up @@ -31,8 +31,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

namespace blink {
Expand Down Expand Up @@ -181,6 +187,11 @@ class WebContents : public mate::TrackableObject<WebContents>,
v8::Local<v8::Value> GetNativeView() const;

#if BUILDFLAG(ENABLE_PRINTING)
void OnGetDefaultPrinter(base::DictionaryValue print_settings,
printing::CompletionCallback print_callback,
base::string16 device_name,
bool silent,
base::string16 default_printer);
void Print(mate::Arguments* args);
std::vector<printing::PrinterBasicInfo> GetPrinterList();
// Print current page as PDF.
Expand Down

0 comments on commit 612b4db

Please sign in to comment.