]> khamidou — GitHub mirrors - electron.git/commitdiff
Add "silent" and "print_background" option for printing.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 22 Aug 2014 07:01:07 +0000 (15:01 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 22 Aug 2014 07:01:07 +0000 (15:01 +0800)
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/native_window.cc
atom/browser/native_window.h
chromium_src/chrome/browser/printing/print_view_manager_base.cc
chromium_src/chrome/browser/printing/print_view_manager_base.h
chromium_src/chrome/common/print_messages.cc
chromium_src/chrome/common/print_messages.h
chromium_src/chrome/renderer/printing/print_web_view_helper.cc
chromium_src/chrome/renderer/printing/print_web_view_helper.h

index da6a113aa5c3d7cc644846addf4e59a58f13d076..65f7841f7658c859ac6d43d4b3b4b36020dd7696 100644 (file)
 
 #include "atom/common/node_includes.h"
 
+namespace {
+
+struct PrintSettings {
+  bool silent;
+  bool print_backgournd;
+};
+
+}  // namespace
+
 namespace mate {
 
+template<>
+struct Converter<PrintSettings> {
+  static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> 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<gfx::Rect> {
   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<WebContents> Window::GetWebContents(v8::Isolate* isolate) const {
index 1a8bdb1dec654eb6ee1d9defda980e34812c1969..e558b5ce00d39f0d78c1590175139034e5aaf123 100644 (file)
@@ -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<WebContents> GetWebContents(v8::Isolate* isolate) const;
index de1d2a18a9f183ef9c599ee5b638c7e5decbb439..94d308b355f18d4407769e5f3acbeede8af3bcb6 100644 (file)
@@ -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() {
index cafccc17a5bf74764f55543321cd53a681f90beb..bc052a8e105d3923d350a3b7a55ee07fb9f5d13b 100644 (file)
@@ -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.
   //
index 3569cde4697eadb945859dcf3b86428b64b49e0e..c987d8e0d1c4acbf65fd046e7fc241bf42792cd4 100644 (file)
@@ -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() {
index e74174736ff84bf04cd772bcfc3356f02ed6343c..17cc58092941a0740fa6d402c93f036e3ab34684 100644 (file)
@@ -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;
index d3d911572fc76885c859f3c90cb0175e0a2875b5..c17b8450821471db81b333a9cd87bb04727fdb79 100644 (file)
@@ -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;
index b52103e38cb322bfbe8579c4a076ffc0dda78f9c..f98c3f7b6a8e83106095d733d56c151966d04c6d 100644 (file)
@@ -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,
index 43c408a7787462586eb3106c4ad2d80777750bbc..b962f45112fb84f19fefd367721b662097f7691e 100644 (file)
@@ -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";
index 7509391597463b9e6e7cba441a53e91eb3178d26..9ae64be20b70cf541afeb8783b1149e35387530e 100644 (file)
@@ -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);