Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: chrome/renderer/print_web_view_helper.cc

Issue 9762004: Block scripted printing. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/print_web_view_helper.h" 5 #include "chrome/renderer/print_web_view_helper.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 650
651 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) 651 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
652 : content::RenderViewObserver(render_view), 652 : content::RenderViewObserver(render_view),
653 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), 653 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
654 print_web_view_(NULL), 654 print_web_view_(NULL),
655 is_preview_enabled_(IsPrintPreviewEnabled()), 655 is_preview_enabled_(IsPrintPreviewEnabled()),
656 is_print_ready_metafile_sent_(false), 656 is_print_ready_metafile_sent_(false),
657 ignore_css_margins_(false), 657 ignore_css_margins_(false),
658 fit_to_page_(true), 658 fit_to_page_(true),
659 user_cancelled_scripted_print_count_(0), 659 user_cancelled_scripted_print_count_(0),
660 is_scripted_printing_blocked_(false),
660 notify_browser_of_print_failure_(true) { 661 notify_browser_of_print_failure_(true) {
661 } 662 }
662 663
663 PrintWebViewHelper::~PrintWebViewHelper() {} 664 PrintWebViewHelper::~PrintWebViewHelper() {}
664 665
666 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed(
667 WebKit::WebFrame* frame) {
668 if (is_scripted_printing_blocked_)
669 return false;
670 return !IsScriptInitiatedPrintTooFrequent(frame);
671 }
672
665 // Prints |frame| which called window.print(). 673 // Prints |frame| which called window.print().
666 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { 674 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) {
667 DCHECK(frame); 675 DCHECK(frame);
668 676
669 // Allow Prerendering to cancel this print request if necessary. 677 // Allow Prerendering to cancel this print request if necessary.
670 if (prerender::PrerenderHelper::IsPrerendering(render_view())) { 678 if (prerender::PrerenderHelper::IsPrerendering(render_view())) {
671 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); 679 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id()));
672 return; 680 return;
673 } 681 }
674 682
675 if (IsScriptInitiatedPrintTooFrequent(frame)) 683 if (!IsScriptInitiatedPrintAllowed(frame))
676 return; 684 return;
677 IncrementScriptedPrintCount(); 685 IncrementScriptedPrintCount();
678 686
679 if (is_preview_enabled_) { 687 if (is_preview_enabled_) {
680 print_preview_context_.InitWithFrame(frame); 688 print_preview_context_.InitWithFrame(frame);
681 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); 689 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED);
682 } else { 690 } else {
683 Print(frame, WebNode()); 691 Print(frame, WebNode());
684 } 692 }
685 } 693 }
686 694
687 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { 695 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) {
688 bool handled = true; 696 bool handled = true;
689 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) 697 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message)
690 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) 698 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages)
691 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) 699 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog)
692 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) 700 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview)
693 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, 701 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu,
694 OnPrintNodeUnderContextMenu) 702 OnPrintNodeUnderContextMenu)
695 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) 703 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
696 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) 704 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview)
697 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) 705 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
698 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, 706 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount,
699 ResetScriptedPrintCount) 707 ResetScriptedPrintCount)
700 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, 708 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled,
701 DisplayPrintJobError) 709 DisplayPrintJobError)
710 IPC_MESSAGE_HANDLER(PrintMsg_SetScriptedPrintingBlocked,
711 SetScriptedPrintBlocked)
702 IPC_MESSAGE_UNHANDLED(handled = false) 712 IPC_MESSAGE_UNHANDLED(handled = false)
703 IPC_END_MESSAGE_MAP() 713 IPC_END_MESSAGE_MAP()
704 return handled; 714 return handled;
705 } 715 }
706 716
707 void PrintWebViewHelper::OnPrintForPrintPreview( 717 void PrintWebViewHelper::OnPrintForPrintPreview(
708 const DictionaryValue& job_settings) { 718 const DictionaryValue& job_settings) {
709 DCHECK(is_preview_enabled_); 719 DCHECK(is_preview_enabled_);
710 // If still not finished with earlier print request simply ignore. 720 // If still not finished with earlier print request simply ignore.
711 if (print_web_view_) 721 if (print_web_view_)
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
969 return true; 979 return true;
970 } 980 }
971 981
972 void PrintWebViewHelper::OnPrintingDone(bool success) { 982 void PrintWebViewHelper::OnPrintingDone(bool success) {
973 notify_browser_of_print_failure_ = false; 983 notify_browser_of_print_failure_ = false;
974 if (!success) 984 if (!success)
975 LOG(ERROR) << "Failure in OnPrintingDone"; 985 LOG(ERROR) << "Failure in OnPrintingDone";
976 DidFinishPrinting(success ? OK : FAIL_PRINT); 986 DidFinishPrinting(success ? OK : FAIL_PRINT);
977 } 987 }
978 988
989 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) {
990 is_scripted_printing_blocked_ = blocked;
991 }
992
979 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() { 993 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() {
980 const WebNode& context_menu_node = render_view()->GetContextMenuNode(); 994 const WebNode& context_menu_node = render_view()->GetContextMenuNode();
981 PrintNode(context_menu_node); 995 PrintNode(context_menu_node);
982 } 996 }
983 997
984 void PrintWebViewHelper::OnInitiatePrintPreview() { 998 void PrintWebViewHelper::OnInitiatePrintPreview() {
985 DCHECK(is_preview_enabled_); 999 DCHECK(is_preview_enabled_);
986 WebFrame* frame; 1000 WebFrame* frame;
987 if (GetPrintFrame(&frame)) { 1001 if (GetPrintFrame(&frame)) {
988 print_preview_context_.InitWithFrame(frame); 1002 print_preview_context_.InitWithFrame(frame);
(...skipping 802 matching lines...) Expand 10 before | Expand all | Expand 10 after
1791 DCHECK(IsRendering()); 1805 DCHECK(IsRendering());
1792 return prep_frame_view_->GetPrintCanvasSize(); 1806 return prep_frame_view_->GetPrintCanvasSize();
1793 } 1807 }
1794 1808
1795 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1809 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1796 prep_frame_view_.reset(); 1810 prep_frame_view_.reset();
1797 metafile_.reset(); 1811 metafile_.reset();
1798 pages_to_render_.clear(); 1812 pages_to_render_.clear();
1799 error_ = PREVIEW_ERROR_NONE; 1813 error_ = PREVIEW_ERROR_NONE;
1800 } 1814 }
OLDNEW
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698