Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/print_web_view_helper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 767 if (owns_web_view_) { | 767 if (owns_web_view_) { |
| 768 DCHECK(!frame->isLoading()); | 768 DCHECK(!frame->isLoading()); |
| 769 owns_web_view_ = false; | 769 owns_web_view_ = false; |
| 770 web_view->close(); | 770 web_view->close(); |
| 771 } | 771 } |
| 772 } | 772 } |
| 773 frame_.Reset(NULL); | 773 frame_.Reset(NULL); |
| 774 on_ready_.Reset(); | 774 on_ready_.Reset(); |
| 775 } | 775 } |
| 776 | 776 |
| 777 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) | 777 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view, |
| 778 bool preview_enabled, | |
|
Vitaly Buka (NO REVIEWS)
2014/09/08 09:43:59
preview_enabled is always false because you build
Marshall
2014/09/09 16:24:51
Building without ENABLE_FULL_PRINTING is problemat
| |
| 779 bool scripted_print_throttling_disabled) | |
| 778 : content::RenderViewObserver(render_view), | 780 : content::RenderViewObserver(render_view), |
| 779 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), | 781 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), |
| 780 reset_prep_frame_view_(false), | 782 reset_prep_frame_view_(false), |
| 783 is_preview_enabled_(preview_enabled), | |
| 784 is_scripted_print_throttling_disabled_(scripted_print_throttling_disabled) , | |
| 781 is_print_ready_metafile_sent_(false), | 785 is_print_ready_metafile_sent_(false), |
| 782 ignore_css_margins_(false), | 786 ignore_css_margins_(false), |
| 787 user_cancelled_scripted_print_count_(0), | |
| 783 is_scripted_printing_blocked_(false), | 788 is_scripted_printing_blocked_(false), |
| 784 notify_browser_of_print_failure_(true), | 789 notify_browser_of_print_failure_(true), |
| 785 print_for_preview_(false), | 790 print_for_preview_(false), |
| 786 print_node_in_progress_(false), | 791 print_node_in_progress_(false), |
| 787 is_loading_(false), | 792 is_loading_(false), |
| 788 is_scripted_preview_delayed_(false), | 793 is_scripted_preview_delayed_(false), |
| 789 weak_ptr_factory_(this) { | 794 weak_ptr_factory_(this) { |
| 790 } | 795 } |
| 791 | 796 |
| 792 PrintWebViewHelper::~PrintWebViewHelper() {} | 797 PrintWebViewHelper::~PrintWebViewHelper() {} |
| 793 | 798 |
| 794 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( | 799 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( |
| 795 blink::WebFrame* frame, bool user_initiated) { | 800 blink::WebFrame* frame, bool user_initiated) { |
| 796 #if defined(OS_ANDROID) | 801 #if defined(OS_ANDROID) |
| 797 return false; | 802 return false; |
| 798 #endif // defined(OS_ANDROID) | 803 #endif // defined(OS_ANDROID) |
| 799 if (is_scripted_printing_blocked_) | 804 if (is_scripted_printing_blocked_) |
| 800 return false; | 805 return false; |
| 806 // If preview is enabled, then the print dialog is tab modal, and the user | |
| 807 // can always close the tab on a mis-behaving page (the system print dialog | |
| 808 // is app modal). If the print was initiated through user action, don't | |
| 809 // throttle. Or, if the command line flag to skip throttling has been set. | |
| 810 if (!is_scripted_print_throttling_disabled_ && | |
| 811 !is_preview_enabled_ && | |
| 812 !user_initiated) | |
| 813 return !IsScriptInitiatedPrintTooFrequent(frame); | |
| 801 return true; | 814 return true; |
| 802 } | 815 } |
| 803 | 816 |
| 804 void PrintWebViewHelper::DidStartLoading() { | 817 void PrintWebViewHelper::DidStartLoading() { |
| 805 is_loading_ = true; | 818 is_loading_ = true; |
| 806 } | 819 } |
| 807 | 820 |
| 808 void PrintWebViewHelper::DidStopLoading() { | 821 void PrintWebViewHelper::DidStopLoading() { |
| 809 is_loading_ = false; | 822 is_loading_ = false; |
| 810 if (!on_stop_loading_closure_.is_null()) { | 823 if (!on_stop_loading_closure_.is_null()) { |
| 811 on_stop_loading_closure_.Run(); | 824 on_stop_loading_closure_.Run(); |
| 812 on_stop_loading_closure_.Reset(); | 825 on_stop_loading_closure_.Reset(); |
| 813 } | 826 } |
| 814 } | 827 } |
| 815 | 828 |
| 816 // Prints |frame| which called window.print(). | 829 // Prints |frame| which called window.print(). |
| 817 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame, | 830 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame, |
| 818 bool user_initiated) { | 831 bool user_initiated) { |
| 819 DCHECK(frame); | 832 DCHECK(frame); |
| 820 | 833 |
| 821 // Allow Prerendering to cancel this print request if necessary. | 834 // Allow Prerendering to cancel this print request if necessary. |
| 822 if (prerender::PrerenderHelper::IsPrerendering( | 835 if (prerender::PrerenderHelper::IsPrerendering( |
| 823 render_view()->GetMainRenderFrame())) { | 836 render_view()->GetMainRenderFrame())) { |
| 824 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); | 837 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); |
| 825 return; | 838 return; |
| 826 } | 839 } |
| 827 | 840 |
| 828 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) | 841 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) |
| 829 return; | 842 return; |
| 830 print_preview_context_.InitWithFrame(frame); | 843 IncrementScriptedPrintCount(); |
| 831 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); | 844 |
| 845 if (is_preview_enabled_) { | |
| 846 print_preview_context_.InitWithFrame(frame); | |
| 847 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); | |
| 848 } else { | |
| 849 Print(frame, blink::WebNode()); | |
| 850 } | |
| 832 } | 851 } |
| 833 | 852 |
| 834 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { | 853 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { |
| 835 bool handled = true; | 854 bool handled = true; |
| 836 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) | 855 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) |
| 837 #if !defined(DISABLE_BASIC_PRINTING) | 856 #if !defined(DISABLE_BASIC_PRINTING) |
| 838 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) | 857 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) |
| 839 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) | 858 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) |
| 840 #endif // !DISABLE_BASIC_PRINTING | 859 #endif // !DISABLE_BASIC_PRINTING |
| 841 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) | 860 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) |
| 842 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) | 861 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) |
| 843 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) | 862 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) |
| 844 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) | 863 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) |
| 845 IPC_MESSAGE_HANDLER(PrintMsg_SetScriptedPrintingBlocked, | 864 IPC_MESSAGE_HANDLER(PrintMsg_SetScriptedPrintingBlocked, |
| 846 SetScriptedPrintBlocked) | 865 SetScriptedPrintBlocked) |
| 847 IPC_MESSAGE_UNHANDLED(handled = false) | 866 IPC_MESSAGE_UNHANDLED(handled = false) |
| 848 IPC_END_MESSAGE_MAP() | 867 IPC_END_MESSAGE_MAP() |
| 849 return handled; | 868 return handled; |
| 850 } | 869 } |
| 851 | 870 |
| 852 void PrintWebViewHelper::OnPrintForPrintPreview( | 871 void PrintWebViewHelper::OnPrintForPrintPreview( |
| 853 const base::DictionaryValue& job_settings) { | 872 const base::DictionaryValue& job_settings) { |
| 873 DCHECK(is_preview_enabled_); | |
| 854 // If still not finished with earlier print request simply ignore. | 874 // If still not finished with earlier print request simply ignore. |
| 855 if (prep_frame_view_) | 875 if (prep_frame_view_) |
| 856 return; | 876 return; |
| 857 | 877 |
| 858 if (!render_view()->GetWebView()) | 878 if (!render_view()->GetWebView()) |
| 859 return; | 879 return; |
| 860 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); | 880 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); |
| 861 if (!main_frame) | 881 if (!main_frame) |
| 862 return; | 882 return; |
| 863 | 883 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 978 | 998 |
| 979 bool PrintWebViewHelper::IsPrintToPdfRequested( | 999 bool PrintWebViewHelper::IsPrintToPdfRequested( |
| 980 const base::DictionaryValue& job_settings) { | 1000 const base::DictionaryValue& job_settings) { |
| 981 bool print_to_pdf = false; | 1001 bool print_to_pdf = false; |
| 982 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf)) | 1002 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf)) |
| 983 NOTREACHED(); | 1003 NOTREACHED(); |
| 984 return print_to_pdf; | 1004 return print_to_pdf; |
| 985 } | 1005 } |
| 986 | 1006 |
| 987 void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) { | 1007 void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) { |
| 1008 DCHECK(is_preview_enabled_); | |
| 988 print_preview_context_.OnPrintPreview(); | 1009 print_preview_context_.OnPrintPreview(); |
| 989 | 1010 |
| 990 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", | 1011 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", |
| 991 PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX); | 1012 PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX); |
| 992 | 1013 |
| 993 if (!print_preview_context_.source_frame()) { | 1014 if (!print_preview_context_.source_frame()) { |
| 994 DidFinishPrinting(FAIL_PREVIEW); | 1015 DidFinishPrinting(FAIL_PREVIEW); |
| 995 return; | 1016 return; |
| 996 } | 1017 } |
| 997 | 1018 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1180 if (!success) | 1201 if (!success) |
| 1181 LOG(ERROR) << "Failure in OnPrintingDone"; | 1202 LOG(ERROR) << "Failure in OnPrintingDone"; |
| 1182 DidFinishPrinting(success ? OK : FAIL_PRINT); | 1203 DidFinishPrinting(success ? OK : FAIL_PRINT); |
| 1183 } | 1204 } |
| 1184 | 1205 |
| 1185 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { | 1206 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { |
| 1186 is_scripted_printing_blocked_ = blocked; | 1207 is_scripted_printing_blocked_ = blocked; |
| 1187 } | 1208 } |
| 1188 | 1209 |
| 1189 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { | 1210 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { |
| 1211 DCHECK(is_preview_enabled_); | |
| 1190 blink::WebLocalFrame* frame = NULL; | 1212 blink::WebLocalFrame* frame = NULL; |
| 1191 GetPrintFrame(&frame); | 1213 GetPrintFrame(&frame); |
| 1192 DCHECK(frame); | 1214 DCHECK(frame); |
| 1193 print_preview_context_.InitWithFrame(frame); | 1215 print_preview_context_.InitWithFrame(frame); |
| 1194 RequestPrintPreview(selection_only ? | 1216 RequestPrintPreview(selection_only ? |
| 1195 PRINT_PREVIEW_USER_INITIATED_SELECTION : | 1217 PRINT_PREVIEW_USER_INITIATED_SELECTION : |
| 1196 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME); | 1218 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME); |
| 1197 } | 1219 } |
| 1198 | 1220 |
| 1199 bool PrintWebViewHelper::IsPrintingEnabled() { | 1221 bool PrintWebViewHelper::IsPrintingEnabled() { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 1213 // This can happen as a result of processing sync messages when printing | 1235 // This can happen as a result of processing sync messages when printing |
| 1214 // from ppapi plugins. It's a rare case, so its OK to just fail here. | 1236 // from ppapi plugins. It's a rare case, so its OK to just fail here. |
| 1215 // See http://crbug.com/159165. | 1237 // See http://crbug.com/159165. |
| 1216 return; | 1238 return; |
| 1217 } | 1239 } |
| 1218 | 1240 |
| 1219 print_node_in_progress_ = true; | 1241 print_node_in_progress_ = true; |
| 1220 | 1242 |
| 1221 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets | 1243 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets |
| 1222 // its |context_menu_node_|. | 1244 // its |context_menu_node_|. |
| 1223 print_preview_context_.InitWithNode(node); | 1245 if (is_preview_enabled_) { |
| 1224 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE); | 1246 print_preview_context_.InitWithNode(node); |
| 1247 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE); | |
| 1248 } else { | |
| 1249 blink::WebNode duplicate_node(node); | |
| 1250 Print(duplicate_node.document().frame(), duplicate_node); | |
| 1251 } | |
| 1225 | 1252 |
| 1226 print_node_in_progress_ = false; | 1253 print_node_in_progress_ = false; |
| 1227 } | 1254 } |
| 1228 | 1255 |
| 1229 #if !defined(DISABLE_BASIC_PRINTING) | 1256 #if !defined(DISABLE_BASIC_PRINTING) |
| 1230 void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, | 1257 void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, |
| 1231 const blink::WebNode& node) { | 1258 const blink::WebNode& node) { |
| 1232 // If still not finished with earlier print request simply ignore. | 1259 // If still not finished with earlier print request simply ignore. |
| 1233 if (prep_frame_view_) | 1260 if (prep_frame_view_) |
| 1234 return; | 1261 return; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 1252 expected_page_count)) { | 1279 expected_page_count)) { |
| 1253 DidFinishPrinting(OK); // Release resources and fail silently. | 1280 DidFinishPrinting(OK); // Release resources and fail silently. |
| 1254 return; | 1281 return; |
| 1255 } | 1282 } |
| 1256 | 1283 |
| 1257 // Render Pages for printing. | 1284 // Render Pages for printing. |
| 1258 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { | 1285 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { |
| 1259 LOG(ERROR) << "RenderPagesForPrint failed"; | 1286 LOG(ERROR) << "RenderPagesForPrint failed"; |
| 1260 DidFinishPrinting(FAIL_PRINT); | 1287 DidFinishPrinting(FAIL_PRINT); |
| 1261 } | 1288 } |
| 1289 ResetScriptedPrintCount(); | |
| 1262 } | 1290 } |
| 1263 #endif // !DISABLE_BASIC_PRINTING | 1291 #endif // !DISABLE_BASIC_PRINTING |
| 1264 | 1292 |
| 1265 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { | 1293 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { |
| 1266 switch (result) { | 1294 switch (result) { |
| 1267 case OK: | 1295 case OK: |
| 1268 break; | 1296 break; |
| 1269 | 1297 |
| 1270 case FAIL_PRINT_INIT: | 1298 case FAIL_PRINT_INIT: |
| 1271 DCHECK(!notify_browser_of_print_failure_); | 1299 DCHECK(!notify_browser_of_print_failure_); |
| 1272 break; | 1300 break; |
| 1273 | 1301 |
| 1274 case FAIL_PRINT: | 1302 case FAIL_PRINT: |
| 1275 if (notify_browser_of_print_failure_ && print_pages_params_) { | 1303 if (notify_browser_of_print_failure_ && print_pages_params_) { |
| 1276 int cookie = print_pages_params_->params.document_cookie; | 1304 int cookie = print_pages_params_->params.document_cookie; |
| 1277 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); | 1305 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); |
| 1278 } | 1306 } |
| 1279 break; | 1307 break; |
| 1280 | 1308 |
| 1281 case FAIL_PREVIEW: | 1309 case FAIL_PREVIEW: |
| 1310 DCHECK(is_preview_enabled_); | |
| 1282 int cookie = print_pages_params_ ? | 1311 int cookie = print_pages_params_ ? |
| 1283 print_pages_params_->params.document_cookie : 0; | 1312 print_pages_params_->params.document_cookie : 0; |
| 1284 if (notify_browser_of_print_failure_) { | 1313 if (notify_browser_of_print_failure_) { |
| 1285 LOG(ERROR) << "CreatePreviewDocument failed"; | 1314 LOG(ERROR) << "CreatePreviewDocument failed"; |
| 1286 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); | 1315 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); |
| 1287 } else { | 1316 } else { |
| 1288 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); | 1317 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); |
| 1289 } | 1318 } |
| 1290 print_preview_context_.Failed(notify_browser_of_print_failure_); | 1319 print_preview_context_.Failed(notify_browser_of_print_failure_); |
| 1291 break; | 1320 break; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1439 const blink::WebNode& source_node = print_preview_context_.source_node(); | 1468 const blink::WebNode& source_node = print_preview_context_.source_node(); |
| 1440 | 1469 |
| 1441 params.is_scaling_disabled = | 1470 params.is_scaling_disabled = |
| 1442 source_frame->isPrintScalingDisabledForPlugin(source_node); | 1471 source_frame->isPrintScalingDisabledForPlugin(source_node); |
| 1443 } | 1472 } |
| 1444 | 1473 |
| 1445 bool PrintWebViewHelper::UpdatePrintSettings( | 1474 bool PrintWebViewHelper::UpdatePrintSettings( |
| 1446 blink::WebLocalFrame* frame, | 1475 blink::WebLocalFrame* frame, |
| 1447 const blink::WebNode& node, | 1476 const blink::WebNode& node, |
| 1448 const base::DictionaryValue& passed_job_settings) { | 1477 const base::DictionaryValue& passed_job_settings) { |
| 1478 DCHECK(is_preview_enabled_); | |
| 1449 const base::DictionaryValue* job_settings = &passed_job_settings; | 1479 const base::DictionaryValue* job_settings = &passed_job_settings; |
| 1450 base::DictionaryValue modified_job_settings; | 1480 base::DictionaryValue modified_job_settings; |
| 1451 if (job_settings->empty()) { | 1481 if (job_settings->empty()) { |
| 1452 if (!print_for_preview_) | 1482 if (!print_for_preview_) |
| 1453 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1483 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
| 1454 return false; | 1484 return false; |
| 1455 } | 1485 } |
| 1456 | 1486 |
| 1457 bool source_is_html = true; | 1487 bool source_is_html = true; |
| 1458 if (print_for_preview_) { | 1488 if (print_for_preview_) { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1581 if (shared_buf->Map(buf_size)) { | 1611 if (shared_buf->Map(buf_size)) { |
| 1582 metafile->GetData(shared_buf->memory(), buf_size); | 1612 metafile->GetData(shared_buf->memory(), buf_size); |
| 1583 return shared_buf->GiveToProcess(base::GetCurrentProcessHandle(), | 1613 return shared_buf->GiveToProcess(base::GetCurrentProcessHandle(), |
| 1584 shared_mem_handle); | 1614 shared_mem_handle); |
| 1585 } | 1615 } |
| 1586 } | 1616 } |
| 1587 return false; | 1617 return false; |
| 1588 } | 1618 } |
| 1589 #endif // defined(OS_POSIX) | 1619 #endif // defined(OS_POSIX) |
| 1590 | 1620 |
| 1621 bool PrintWebViewHelper::IsScriptInitiatedPrintTooFrequent( | |
| 1622 blink::WebFrame* frame) { | |
| 1623 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; | |
| 1624 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 32; | |
| 1625 bool too_frequent = false; | |
| 1626 | |
| 1627 // Check if there is script repeatedly trying to print and ignore it if too | |
| 1628 // frequent. The first 3 times, we use a constant wait time, but if this | |
| 1629 // gets excessive, we switch to exponential wait time. So for a page that | |
| 1630 // calls print() in a loop the user will need to cancel the print dialog | |
| 1631 // after: [2, 2, 2, 4, 8, 16, 32, 32, ...] seconds. | |
| 1632 // This gives the user time to navigate from the page. | |
| 1633 if (user_cancelled_scripted_print_count_ > 0) { | |
| 1634 base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_; | |
| 1635 int min_wait_seconds = kMinSecondsToIgnoreJavascriptInitiatedPrint; | |
| 1636 if (user_cancelled_scripted_print_count_ > 3) { | |
| 1637 min_wait_seconds = std::min( | |
| 1638 kMinSecondsToIgnoreJavascriptInitiatedPrint << | |
| 1639 (user_cancelled_scripted_print_count_ - 3), | |
| 1640 kMaxSecondsToIgnoreJavascriptInitiatedPrint); | |
| 1641 } | |
| 1642 if (diff.InSeconds() < min_wait_seconds) { | |
| 1643 too_frequent = true; | |
| 1644 } | |
| 1645 } | |
| 1646 | |
| 1647 if (!too_frequent) | |
| 1648 return false; | |
| 1649 | |
| 1650 blink::WebString message( | |
| 1651 blink::WebString::fromUTF8("Ignoring too frequent calls to print().")); | |
| 1652 frame->addMessageToConsole( | |
| 1653 blink::WebConsoleMessage( | |
| 1654 blink::WebConsoleMessage::LevelWarning, message)); | |
| 1655 return true; | |
| 1656 } | |
| 1657 | |
| 1658 void PrintWebViewHelper::ResetScriptedPrintCount() { | |
| 1659 // Reset cancel counter on successful print. | |
| 1660 user_cancelled_scripted_print_count_ = 0; | |
| 1661 } | |
| 1662 | |
| 1663 void PrintWebViewHelper::IncrementScriptedPrintCount() { | |
| 1664 ++user_cancelled_scripted_print_count_; | |
| 1665 last_cancelled_script_print_ = base::Time::Now(); | |
| 1666 } | |
| 1667 | |
| 1668 | |
| 1591 void PrintWebViewHelper::ShowScriptedPrintPreview() { | 1669 void PrintWebViewHelper::ShowScriptedPrintPreview() { |
| 1592 if (is_scripted_preview_delayed_) { | 1670 if (is_scripted_preview_delayed_) { |
| 1593 is_scripted_preview_delayed_ = false; | 1671 is_scripted_preview_delayed_ = false; |
| 1594 Send(new PrintHostMsg_ShowScriptedPrintPreview(routing_id(), | 1672 Send(new PrintHostMsg_ShowScriptedPrintPreview(routing_id(), |
| 1595 print_preview_context_.IsModifiable())); | 1673 print_preview_context_.IsModifiable())); |
| 1596 } | 1674 } |
| 1597 } | 1675 } |
| 1598 | 1676 |
| 1599 void PrintWebViewHelper::RequestPrintPreview(PrintPreviewRequestType type) { | 1677 void PrintWebViewHelper::RequestPrintPreview(PrintPreviewRequestType type) { |
| 1600 const bool is_modifiable = print_preview_context_.IsModifiable(); | 1678 const bool is_modifiable = print_preview_context_.IsModifiable(); |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1964 } | 2042 } |
| 1965 | 2043 |
| 1966 void PrintWebViewHelper::SetPrintPagesParams( | 2044 void PrintWebViewHelper::SetPrintPagesParams( |
| 1967 const PrintMsg_PrintPages_Params& settings) { | 2045 const PrintMsg_PrintPages_Params& settings) { |
| 1968 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 2046 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 1969 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 2047 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
| 1970 settings.params.document_cookie)); | 2048 settings.params.document_cookie)); |
| 1971 } | 2049 } |
| 1972 | 2050 |
| 1973 } // namespace printing | 2051 } // namespace printing |
| OLD | NEW |