From cb7196a9c12c5baad89fda56e79a6e1dfe257e35 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 21 Aug 2014 16:25:04 +0800 Subject: [PATCH] Allow script initialized window.print(). --- .../printing/print_view_manager_base.cc | 7 -- .../printing/print_view_manager_base.h | 3 - chromium_src/chrome/common/print_messages.h | 7 -- .../printing/print_web_view_helper.cc | 86 ------------------- .../renderer/printing/print_web_view_helper.h | 22 ----- 5 files changed, 125 deletions(-) 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 c47797e46c6b..e9a76ba714ef 100644 --- a/chromium_src/chrome/browser/printing/print_view_manager_base.cc +++ b/chromium_src/chrome/browser/printing/print_view_manager_base.cc @@ -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) { 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 e3f94c2784f6..e74174736ff8 100644 --- a/chromium_src/chrome/browser/printing/print_view_manager_base.h +++ b/chromium_src/chrome/browser/printing/print_view_manager_base.h @@ -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; diff --git a/chromium_src/chrome/common/print_messages.h b/chromium_src/chrome/common/print_messages.h index cf91871b5867..d095417a96ae 100644 --- a/chromium_src/chrome/common/print_messages.h +++ b/chromium_src/chrome/common/print_messages.h @@ -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) 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 4b14f207927b..7d63d446ee83 100644 --- a/chromium_src/chrome/renderer/printing/print_web_view_helper.cc +++ b/chromium_src/chrome/renderer/printing/print_web_view_helper.cc @@ -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(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; 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 e63a0a382160..c478272f9af4 100644 --- a/chromium_src/chrome/renderer/printing/print_web_view_helper.h +++ b/chromium_src/chrome/renderer/printing/print_web_view_helper.h @@ -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_;