From e43b3309af9b5e03a7a4189522937e907c1816c6 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 22 Aug 2014 15:01:07 +0800 Subject: [PATCH] Add "silent" and "print_background" option for printing. --- atom/browser/api/atom_api_window.cc | 33 +++++++++++++++++-- atom/browser/api/atom_api_window.h | 2 +- atom/browser/native_window.cc | 4 +-- atom/browser/native_window.h | 2 +- .../printing/print_view_manager_base.cc | 5 +-- .../printing/print_view_manager_base.h | 2 +- chromium_src/chrome/common/print_messages.cc | 2 -- chromium_src/chrome/common/print_messages.h | 8 ++--- .../printing/print_web_view_helper.cc | 22 +++++-------- .../renderer/printing/print_web_view_helper.h | 10 +++--- 10 files changed, 56 insertions(+), 34 deletions(-) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index da6a113aa5c..65f7841f765 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -16,8 +16,31 @@ #include "atom/common/node_includes.h" +namespace { + +struct PrintSettings { + bool silent; + bool print_backgournd; +}; + +} // namespace + namespace mate { +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, v8::Handle val, + PrintSettings* out) { + mate::Dictionary dict; + if (!ConvertFromV8(isolate, val, &dict)) + return false; + if (!dict.Get("silent", &(out->silent)) || + !dict.Get("printBackground", &(out->print_backgournd))) + return false; + return true; + } +}; + template<> struct Converter { static bool FromV8(v8::Isolate* isolate, @@ -334,8 +357,14 @@ void Window::CapturePage(mate::Arguments* args) { rect, base::Bind(&OnCapturePageDone, args->isolate(), callback)); } -void Window::Print() { - window_->Print(); +void Window::Print(mate::Arguments* args) { + PrintSettings settings = { false, false };; + if (args->Length() == 1 && !args->GetNext(&settings)) { + args->ThrowError(); + return; + } + + window_->Print(settings.silent, settings.print_backgournd); } mate::Handle Window::GetWebContents(v8::Isolate* isolate) const { diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 1a8bdb1dec6..e558b5ce00d 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -102,7 +102,7 @@ class Window : public mate::EventEmitter, void SetDocumentEdited(bool edited); bool IsDocumentEdited(); void CapturePage(mate::Arguments* args); - void Print(); + void Print(mate::Arguments* args); // APIs for WebContents. mate::Handle GetWebContents(v8::Isolate* isolate) const; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index de1d2a18a9f..94d308b355f 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -202,9 +202,9 @@ bool NativeWindow::IsDocumentEdited() { void NativeWindow::SetMenu(ui::MenuModel* menu) { } -void NativeWindow::Print() { +void NativeWindow::Print(bool silent, bool print_background) { printing::PrintViewManagerBasic::FromWebContents(GetWebContents())-> - PrintNow(); + PrintNow(silent, print_background); } bool NativeWindow::HasModalDialog() { diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index cafccc17a5b..bc052a8e105 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -157,7 +157,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, const CapturePageCallback& callback); // Print current page. - virtual void Print(); + virtual void Print(bool silent, bool print_background); // The same with closing a tab in a real browser. // diff --git a/chromium_src/chrome/browser/printing/print_view_manager_base.cc b/chromium_src/chrome/browser/printing/print_view_manager_base.cc index 3569cde4697..c987d8e0d1c 100644 --- a/chromium_src/chrome/browser/printing/print_view_manager_base.cc +++ b/chromium_src/chrome/browser/printing/print_view_manager_base.cc @@ -60,8 +60,9 @@ PrintViewManagerBase::~PrintViewManagerBase() { DisconnectFromCurrentPrintJob(); } -bool PrintViewManagerBase::PrintNow() { - return PrintNowInternal(new PrintMsg_PrintPages(routing_id())); +bool PrintViewManagerBase::PrintNow(bool silent, bool print_background) { + return PrintNowInternal(new PrintMsg_PrintPages( + routing_id(), silent, print_background)); } void PrintViewManagerBase::NavigationStopped() { diff --git a/chromium_src/chrome/browser/printing/print_view_manager_base.h b/chromium_src/chrome/browser/printing/print_view_manager_base.h index e74174736ff..17cc5809294 100644 --- a/chromium_src/chrome/browser/printing/print_view_manager_base.h +++ b/chromium_src/chrome/browser/printing/print_view_manager_base.h @@ -37,7 +37,7 @@ class PrintViewManagerBase : public content::NotificationObserver, // Prints the current document immediately. Since the rendering is // asynchronous, the actual printing will not be completed on the return of // this function. Returns false if printing is impossible at the moment. - virtual bool PrintNow(); + virtual bool PrintNow(bool silent, bool print_background); // PrintedPagesSource implementation. virtual base::string16 RenderSourceName() OVERRIDE; diff --git a/chromium_src/chrome/common/print_messages.cc b/chromium_src/chrome/common/print_messages.cc index d3d911572fc..c17b8450821 100644 --- a/chromium_src/chrome/common/print_messages.cc +++ b/chromium_src/chrome/common/print_messages.cc @@ -22,7 +22,6 @@ PrintMsg_Print_Params::PrintMsg_Print_Params() selection_only(false), supports_alpha_blend(false), print_scaling_option(blink::WebPrintScalingOptionSourceSize), - print_to_pdf(false), title(), url(), should_print_backgrounds(false) { @@ -44,7 +43,6 @@ void PrintMsg_Print_Params::Reset() { selection_only = false; supports_alpha_blend = false; print_scaling_option = blink::WebPrintScalingOptionSourceSize; - print_to_pdf = false; title.clear(); url.clear(); should_print_backgrounds = false; diff --git a/chromium_src/chrome/common/print_messages.h b/chromium_src/chrome/common/print_messages.h index b52103e38cb..f98c3f7b6a8 100644 --- a/chromium_src/chrome/common/print_messages.h +++ b/chromium_src/chrome/common/print_messages.h @@ -40,7 +40,6 @@ struct PrintMsg_Print_Params { bool selection_only; bool supports_alpha_blend; blink::WebPrintScalingOption print_scaling_option; - bool print_to_pdf; base::string16 title; base::string16 url; bool should_print_backgrounds; @@ -108,9 +107,6 @@ IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params) // Specifies the page scaling option for preview printing. IPC_STRUCT_TRAITS_MEMBER(print_scaling_option) - // True if print to pdf is requested. - IPC_STRUCT_TRAITS_MEMBER(print_to_pdf) - // Title string to be printed as header if requested by the user. IPC_STRUCT_TRAITS_MEMBER(title) @@ -187,7 +183,9 @@ IPC_STRUCT_END() // Tells the render view to switch the CSS to print media type, renders every // requested pages and switch back the CSS to display media type. -IPC_MESSAGE_ROUTED0(PrintMsg_PrintPages) +IPC_MESSAGE_ROUTED2(PrintMsg_PrintPages, + bool /* silent print */, + bool /* print page's background */) // Tells the render view that printing is done so it can clean up. IPC_MESSAGE_ROUTED1(PrintMsg_PrintingDone, diff --git a/chromium_src/chrome/renderer/printing/print_web_view_helper.cc b/chromium_src/chrome/renderer/printing/print_web_view_helper.cc index 43c408a7787..b962f45112f 100644 --- a/chromium_src/chrome/renderer/printing/print_web_view_helper.cc +++ b/chromium_src/chrome/renderer/printing/print_web_view_helper.cc @@ -672,10 +672,10 @@ bool PrintWebViewHelper::GetPrintFrame(blink::WebLocalFrame** frame) { return true; } -void PrintWebViewHelper::OnPrintPages() { +void PrintWebViewHelper::OnPrintPages(bool silent, bool print_background) { blink::WebLocalFrame* frame; if (GetPrintFrame(&frame)) - Print(frame, blink::WebNode()); + Print(frame, blink::WebNode(), silent, print_background); } void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( @@ -703,14 +703,6 @@ void PrintWebViewHelper::UpdateFrameMarginsCssInfo( ignore_css_margins_ = (margins_type != DEFAULT_MARGINS); } -bool PrintWebViewHelper::IsPrintToPdfRequested( - const base::DictionaryValue& job_settings) { - bool print_to_pdf = false; - if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf)) - NOTREACHED(); - return print_to_pdf; -} - void PrintWebViewHelper::OnPrintingDone(bool success) { notify_browser_of_print_failure_ = false; if (!success) @@ -743,7 +735,9 @@ void PrintWebViewHelper::PrintNode(const blink::WebNode& node) { } void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, - const blink::WebNode& node) { + const blink::WebNode& node, + bool silent, + bool print_background) { // If still not finished with earlier print request simply ignore. if (prep_frame_view_) return; @@ -763,12 +757,14 @@ void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, } // Ask the browser to show UI to retrieve the final print settings. - if (!GetPrintSettingsFromUser(frame_ref.GetFrame(), node, - expected_page_count)) { + if (!silent && !GetPrintSettingsFromUser(frame_ref.GetFrame(), node, + expected_page_count)) { DidFinishPrinting(OK); // Release resources and fail silently. return; } + print_pages_params_->params.should_print_backgrounds = print_background; + // Render Pages for printing. if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { LOG(ERROR) << "RenderPagesForPrint failed"; diff --git a/chromium_src/chrome/renderer/printing/print_web_view_helper.h b/chromium_src/chrome/renderer/printing/print_web_view_helper.h index 75093915974..9ae64be20b7 100644 --- a/chromium_src/chrome/renderer/printing/print_web_view_helper.h +++ b/chromium_src/chrome/renderer/printing/print_web_view_helper.h @@ -81,7 +81,7 @@ class PrintWebViewHelper bool user_initiated) OVERRIDE; // Message handlers --------------------------------------------------------- - void OnPrintPages(); + void OnPrintPages(bool silent, bool print_background); void OnPrintingDone(bool success); // Get |page_size| and |content_area| information from @@ -94,12 +94,12 @@ class PrintWebViewHelper // Update |ignore_css_margins_| based on settings. void UpdateFrameMarginsCssInfo(const base::DictionaryValue& settings); - // Returns true if the current destination printer is PRINT_TO_PDF. - bool IsPrintToPdfRequested(const base::DictionaryValue& settings); - // Main printing code ------------------------------------------------------- - void Print(blink::WebLocalFrame* frame, const blink::WebNode& node); + void Print(blink::WebLocalFrame* frame, + const blink::WebNode& node, + bool silent = false, + bool print_background = false); // Notification when printing is done - signal tear-down/free resources. void DidFinishPrinting(PrintingResult result);