]> khamidou — GitHub mirrors - electron.git/commitdiff
Allow script initialized window.print().
authorCheng Zhao <zcbenz@gmail.com>
Thu, 21 Aug 2014 08:25:04 +0000 (16:25 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 21 Aug 2014 08:32:41 +0000 (16:32 +0800)
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.h
chromium_src/chrome/renderer/printing/print_web_view_helper.cc
chromium_src/chrome/renderer/printing/print_web_view_helper.h

index c47797e46c6b09e63eccf5f4c9de2f2c63a8c1df..e9a76ba714ef56c59295eaedb2c51d421cda6511 100644 (file)
@@ -73,12 +73,6 @@ bool PrintViewManagerBase::PrintNow() {
   return PrintNowInternal(new PrintMsg_PrintPages(routing_id()));
 }
 
-void PrintViewManagerBase::UpdateScriptedPrintingBlocked() {
-  Send(new PrintMsg_SetScriptedPrintingBlocked(
-       routing_id(),
-       !printing_enabled_));
-}
-
 void PrintViewManagerBase::NavigationStopped() {
   // Cancel the current job, wait for the worker to finish.
   TerminatePrintJob(true);
@@ -206,7 +200,6 @@ void PrintViewManagerBase::OnShowInvalidPrinterSettingsError() {
 
 void PrintViewManagerBase::DidStartLoading(
     content::RenderViewHost* render_view_host) {
-  UpdateScriptedPrintingBlocked();
 }
 
 bool PrintViewManagerBase::OnMessageReceived(const IPC::Message& message) {
index e3f94c2784f64b726dfe36878720c6b1c09ff9a2..e74174736ff84bf04cd772bcfc3356f02ed6343c 100644 (file)
@@ -39,9 +39,6 @@ class PrintViewManagerBase : public content::NotificationObserver,
   // this function. Returns false if printing is impossible at the moment.
   virtual bool PrintNow();
 
-  // Whether to block scripted printing for our tab or not.
-  void UpdateScriptedPrintingBlocked();
-
   // PrintedPagesSource implementation.
   virtual base::string16 RenderSourceName() OVERRIDE;
 
index cf91871b5867b1c0a2594f4a47be6532a2fd5150..d095417a96aec0c1e8a0e1c9c6a3578aa1839ff4 100644 (file)
@@ -295,10 +295,6 @@ IPC_MESSAGE_ROUTED0(PrintMsg_PrintPages)
 IPC_MESSAGE_ROUTED1(PrintMsg_PrintingDone,
                     bool /* success */)
 
-// Tells the render view whether scripted printing is blocked or not.
-IPC_MESSAGE_ROUTED1(PrintMsg_SetScriptedPrintingBlocked,
-                    bool /* blocked */)
-
 // Tells the render view to switch the CSS to print media type, renders every
 // requested pages for print preview using the given |settings|. This gets
 // called multiple times as the user updates settings.
@@ -308,9 +304,6 @@ IPC_MESSAGE_ROUTED1(PrintMsg_PrintPreview,
 // Like PrintMsg_PrintPages, but using the print preview document's frame/node.
 IPC_MESSAGE_ROUTED0(PrintMsg_PrintForSystemDialog)
 
-// Tells a renderer to stop blocking script initiated printing.
-IPC_MESSAGE_ROUTED0(PrintMsg_ResetScriptedPrintCount)
-
 // Messages sent from the renderer to the browser.
 
 #if defined(OS_WIN)
index 4b14f207927b66c55df3866244cbbc9612594887..7d63d446ee83578e99d448eb1a05bb579d721808 100644 (file)
@@ -398,10 +398,6 @@ bool IsPrintPreviewEnabled() {
   return true;
 }
 
-bool IsPrintThrottlingDisabled() {
-  return false;
-}
-
 }  // namespace
 
 FrameReference::FrameReference(blink::WebLocalFrame* frame) {
@@ -772,11 +768,8 @@ PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
       content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
       reset_prep_frame_view_(false),
       is_preview_enabled_(IsPrintPreviewEnabled()),
-      is_scripted_print_throttling_disabled_(IsPrintThrottlingDisabled()),
       is_print_ready_metafile_sent_(false),
       ignore_css_margins_(false),
-      user_cancelled_scripted_print_count_(0),
-      is_scripted_printing_blocked_(false),
       notify_browser_of_print_failure_(true),
       print_for_preview_(false),
       print_node_in_progress_(false),
@@ -787,24 +780,6 @@ PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
 
 PrintWebViewHelper::~PrintWebViewHelper() {}
 
-bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed(
-    blink::WebFrame* frame, bool user_initiated) {
-#if defined(OS_ANDROID)
-  return false;
-#endif  // defined(OS_ANDROID)
-  if (is_scripted_printing_blocked_)
-    return false;
-  // If preview is enabled, then the print dialog is tab modal, and the user
-  // can always close the tab on a mis-behaving page (the system print dialog
-  // is app modal). If the print was initiated through user action, don't
-  // throttle. Or, if the command line flag to skip throttling has been set.
-  if (!is_scripted_print_throttling_disabled_ &&
-      !is_preview_enabled_ &&
-      !user_initiated)
-    return !IsScriptInitiatedPrintTooFrequent(frame);
-  return true;
-}
-
 void PrintWebViewHelper::DidStartLoading() {
   is_loading_ = true;
 }
@@ -819,10 +794,6 @@ void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame,
                                    bool user_initiated) {
   DCHECK(frame);
 
-  if (!IsScriptInitiatedPrintAllowed(frame, user_initiated))
-    return;
-  IncrementScriptedPrintCount();
-
   if (is_preview_enabled_) {
     print_preview_context_.InitWithFrame(frame);
     RequestPrintPreview(PRINT_PREVIEW_SCRIPTED);
@@ -840,10 +811,6 @@ bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) {
     IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
     IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview)
     IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
-    IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount,
-                        ResetScriptedPrintCount)
-    IPC_MESSAGE_HANDLER(PrintMsg_SetScriptedPrintingBlocked,
-                        SetScriptedPrintBlocked)
     IPC_MESSAGE_UNHANDLED(handled = false)
     IPC_END_MESSAGE_MAP()
   return handled;
@@ -1170,10 +1137,6 @@ void PrintWebViewHelper::OnPrintingDone(bool success) {
   DidFinishPrinting(success ? OK : FAIL_PRINT);
 }
 
-void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) {
-  is_scripted_printing_blocked_ = blocked;
-}
-
 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) {
   DCHECK(is_preview_enabled_);
   blink::WebLocalFrame* frame = NULL;
@@ -1252,7 +1215,6 @@ void PrintWebViewHelper::Print(blink::WebLocalFrame* frame,
     LOG(ERROR) << "RenderPagesForPrint failed";
     DidFinishPrinting(FAIL_PRINT);
   }
-  ResetScriptedPrintCount();
 }
 
 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) {
@@ -1601,54 +1563,6 @@ bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
 }
 #endif  // defined(OS_POSIX)
 
-bool PrintWebViewHelper::IsScriptInitiatedPrintTooFrequent(
-    blink::WebFrame* frame) {
-  const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2;
-  const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 32;
-  bool too_frequent = false;
-
-  // Check if there is script repeatedly trying to print and ignore it if too
-  // frequent.  The first 3 times, we use a constant wait time, but if this
-  // gets excessive, we switch to exponential wait time. So for a page that
-  // calls print() in a loop the user will need to cancel the print dialog
-  // after: [2, 2, 2, 4, 8, 16, 32, 32, ...] seconds.
-  // This gives the user time to navigate from the page.
-  if (user_cancelled_scripted_print_count_ > 0) {
-    base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_;
-    int min_wait_seconds = kMinSecondsToIgnoreJavascriptInitiatedPrint;
-    if (user_cancelled_scripted_print_count_ > 3) {
-      min_wait_seconds = std::min(
-          kMinSecondsToIgnoreJavascriptInitiatedPrint <<
-              (user_cancelled_scripted_print_count_ - 3),
-          kMaxSecondsToIgnoreJavascriptInitiatedPrint);
-    }
-    if (diff.InSeconds() < min_wait_seconds) {
-      too_frequent = true;
-    }
-  }
-
-  if (!too_frequent)
-    return false;
-
-  blink::WebString message(
-      blink::WebString::fromUTF8("Ignoring too frequent calls to print()."));
-  frame->addMessageToConsole(
-      blink::WebConsoleMessage(
-          blink::WebConsoleMessage::LevelWarning, message));
-  return true;
-}
-
-void PrintWebViewHelper::ResetScriptedPrintCount() {
-  // Reset cancel counter on successful print.
-  user_cancelled_scripted_print_count_ = 0;
-}
-
-void PrintWebViewHelper::IncrementScriptedPrintCount() {
-  ++user_cancelled_scripted_print_count_;
-  last_cancelled_script_print_ = base::Time::Now();
-}
-
-
 void PrintWebViewHelper::ShowScriptedPrintPreview() {
   if (is_scripted_preview_delayed_) {
     is_scripted_preview_delayed_ = false;
index e63a0a382160e671b13fc91203b023f31be90dd0..c478272f9af41e2550e0fd9989ec639f81800ba6 100644 (file)
@@ -276,23 +276,6 @@ class PrintWebViewHelper
 
   // Script Initiated Printing ------------------------------------------------
 
-  // Return true if script initiated printing is currently
-  // allowed. |user_initiated| should be true when a user event triggered the
-  // script, most likely by pressing a print button on the page.
-  bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame,
-                                     bool user_initiated);
-
-  // Returns true if script initiated printing occurs too often.
-  bool IsScriptInitiatedPrintTooFrequent(blink::WebFrame* frame);
-
-  // Reset the counter for script initiated printing.
-  // Scripted printing will be allowed to continue.
-  void ResetScriptedPrintCount();
-
-  // Increment the counter for script initiated printing.
-  // Scripted printing will be blocked for a limited amount of time.
-  void IncrementScriptedPrintCount();
-
   // Shows scripted print preview when options from plugin are availible.
   void ShowScriptedPrintPreview();
 
@@ -319,11 +302,6 @@ class PrintWebViewHelper
   bool is_print_ready_metafile_sent_;
   bool ignore_css_margins_;
 
-  // Used for scripted initiated printing blocking.
-  base::Time last_cancelled_script_print_;
-  int user_cancelled_scripted_print_count_;
-  bool is_scripted_printing_blocked_;
-
   // Let the browser process know of a printing failure. Only set to false when
   // the failure came from the browser in the first place.
   bool notify_browser_of_print_failure_;