| 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 800 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 | 811 |
| 812 PrintWebViewHelper::~PrintWebViewHelper() {} | 812 PrintWebViewHelper::~PrintWebViewHelper() {} |
| 813 | 813 |
| 814 // static | 814 // static |
| 815 void PrintWebViewHelper::DisablePreview() { | 815 void PrintWebViewHelper::DisablePreview() { |
| 816 g_is_preview_enabled_ = false; | 816 g_is_preview_enabled_ = false; |
| 817 } | 817 } |
| 818 | 818 |
| 819 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( | 819 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( |
| 820 blink::WebFrame* frame, bool user_initiated) { | 820 blink::WebFrame* frame, bool user_initiated) { |
| 821 #if defined(OS_ANDROID) | |
| 822 return false; | |
| 823 #endif // defined(OS_ANDROID) | |
| 824 // If preview is enabled, then the print dialog is tab modal, and the user | 821 // If preview is enabled, then the print dialog is tab modal, and the user |
| 825 // can always close the tab on a mis-behaving page (the system print dialog | 822 // can always close the tab on a mis-behaving page (the system print dialog |
| 826 // is app modal). If the print was initiated through user action, don't | 823 // is app modal). If the print was initiated through user action, don't |
| 827 // throttle. Or, if the command line flag to skip throttling has been set. | 824 // throttle. Or, if the command line flag to skip throttling has been set. |
| 828 return !is_scripted_printing_blocked_ && | 825 return !is_scripted_printing_blocked_ && |
| 829 (user_initiated || g_is_preview_enabled_ || | 826 (user_initiated || g_is_preview_enabled_ || |
| 830 scripting_throttler_.IsAllowed(frame)); | 827 scripting_throttler_.IsAllowed(frame)); |
| 831 } | 828 } |
| 832 | 829 |
| 833 void PrintWebViewHelper::DidStartLoading() { | 830 void PrintWebViewHelper::DidStartLoading() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 851 if (prerender::PrerenderHelper::IsPrerendering( | 848 if (prerender::PrerenderHelper::IsPrerendering( |
| 852 render_view()->GetMainRenderFrame())) { | 849 render_view()->GetMainRenderFrame())) { |
| 853 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); | 850 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); |
| 854 return; | 851 return; |
| 855 } | 852 } |
| 856 | 853 |
| 857 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) | 854 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) |
| 858 return; | 855 return; |
| 859 | 856 |
| 860 if (!g_is_preview_enabled_) { | 857 if (!g_is_preview_enabled_) { |
| 861 Print(frame, blink::WebNode()); | 858 Print(frame, blink::WebNode(), true); |
| 862 } else { | 859 } else { |
| 863 print_preview_context_.InitWithFrame(frame); | 860 print_preview_context_.InitWithFrame(frame); |
| 864 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); | 861 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); |
| 865 } | 862 } |
| 866 } | 863 } |
| 867 | 864 |
| 868 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { | 865 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { |
| 869 bool handled = true; | 866 bool handled = true; |
| 870 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) | 867 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) |
| 871 #if defined(ENABLE_BASIC_PRINTING) | 868 #if defined(ENABLE_BASIC_PRINTING) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 967 } | 964 } |
| 968 | 965 |
| 969 #if defined(ENABLE_BASIC_PRINTING) | 966 #if defined(ENABLE_BASIC_PRINTING) |
| 970 void PrintWebViewHelper::OnPrintPages() { | 967 void PrintWebViewHelper::OnPrintPages() { |
| 971 blink::WebLocalFrame* frame; | 968 blink::WebLocalFrame* frame; |
| 972 if (!GetPrintFrame(&frame)) | 969 if (!GetPrintFrame(&frame)) |
| 973 return; | 970 return; |
| 974 // If we are printing a PDF extension frame, find the plugin node and print | 971 // If we are printing a PDF extension frame, find the plugin node and print |
| 975 // that instead. | 972 // that instead. |
| 976 auto plugin = GetPdfElement(frame); | 973 auto plugin = GetPdfElement(frame); |
| 977 Print(frame, plugin); | 974 Print(frame, plugin, false); |
| 978 } | 975 } |
| 979 | 976 |
| 980 void PrintWebViewHelper::OnPrintForSystemDialog() { | 977 void PrintWebViewHelper::OnPrintForSystemDialog() { |
| 981 blink::WebLocalFrame* frame = print_preview_context_.source_frame(); | 978 blink::WebLocalFrame* frame = print_preview_context_.source_frame(); |
| 982 if (!frame) { | 979 if (!frame) { |
| 983 NOTREACHED(); | 980 NOTREACHED(); |
| 984 return; | 981 return; |
| 985 } | 982 } |
| 986 Print(frame, print_preview_context_.source_node()); | 983 Print(frame, print_preview_context_.source_node(), false); |
| 987 } | 984 } |
| 988 #endif // ENABLE_BASIC_PRINTING | 985 #endif // ENABLE_BASIC_PRINTING |
| 989 | 986 |
| 990 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( | 987 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( |
| 991 const PageSizeMargins& page_layout_in_points, | 988 const PageSizeMargins& page_layout_in_points, |
| 992 gfx::Size* page_size, | 989 gfx::Size* page_size, |
| 993 gfx::Rect* content_area) { | 990 gfx::Rect* content_area) { |
| 994 *page_size = gfx::Size( | 991 *page_size = gfx::Size( |
| 995 page_layout_in_points.content_width + | 992 page_layout_in_points.content_width + |
| 996 page_layout_in_points.margin_right + | 993 page_layout_in_points.margin_right + |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1258 // See http://crbug.com/159165. | 1255 // See http://crbug.com/159165. |
| 1259 return; | 1256 return; |
| 1260 } | 1257 } |
| 1261 | 1258 |
| 1262 print_node_in_progress_ = true; | 1259 print_node_in_progress_ = true; |
| 1263 | 1260 |
| 1264 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets | 1261 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets |
| 1265 // its |context_menu_node_|. | 1262 // its |context_menu_node_|. |
| 1266 if (!g_is_preview_enabled_) { | 1263 if (!g_is_preview_enabled_) { |
| 1267 blink::WebNode duplicate_node(node); | 1264 blink::WebNode duplicate_node(node); |
| 1268 Print(duplicate_node.document().frame(), duplicate_node); | 1265 Print(duplicate_node.document().frame(), duplicate_node, false); |
| 1269 } else { | 1266 } else { |
| 1270 print_preview_context_.InitWithNode(node); | 1267 print_preview_context_.InitWithNode(node); |
| 1271 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE); | 1268 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE); |
| 1272 } | 1269 } |
| 1273 | 1270 |
| 1274 print_node_in_progress_ = false; | 1271 print_node_in_progress_ = false; |
| 1275 } | 1272 } |
| 1276 | 1273 |
| 1277 void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, | 1274 void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, |
| 1278 const blink::WebNode& node) { | 1275 const blink::WebNode& node, |
| 1276 bool is_scripted) { |
| 1279 // If still not finished with earlier print request simply ignore. | 1277 // If still not finished with earlier print request simply ignore. |
| 1280 if (prep_frame_view_) | 1278 if (prep_frame_view_) |
| 1281 return; | 1279 return; |
| 1282 | 1280 |
| 1283 FrameReference frame_ref(frame); | 1281 FrameReference frame_ref(frame); |
| 1284 | 1282 |
| 1285 int expected_page_count = 0; | 1283 int expected_page_count = 0; |
| 1286 if (!CalculateNumberOfPages(frame, node, &expected_page_count)) { | 1284 if (!CalculateNumberOfPages(frame, node, &expected_page_count)) { |
| 1287 DidFinishPrinting(FAIL_PRINT_INIT); | 1285 DidFinishPrinting(FAIL_PRINT_INIT); |
| 1288 return; // Failed to init print page settings. | 1286 return; // Failed to init print page settings. |
| 1289 } | 1287 } |
| 1290 | 1288 |
| 1291 // Some full screen plugins can say they don't want to print. | 1289 // Some full screen plugins can say they don't want to print. |
| 1292 if (!expected_page_count) { | 1290 if (!expected_page_count) { |
| 1293 DidFinishPrinting(FAIL_PRINT); | 1291 DidFinishPrinting(FAIL_PRINT); |
| 1294 return; | 1292 return; |
| 1295 } | 1293 } |
| 1296 | 1294 |
| 1297 // Ask the browser to show UI to retrieve the final print settings. | 1295 // Ask the browser to show UI to retrieve the final print settings. |
| 1298 if (!GetPrintSettingsFromUser(frame_ref.GetFrame(), node, | 1296 if (!GetPrintSettingsFromUser(frame_ref.GetFrame(), node, |
| 1299 expected_page_count)) { | 1297 expected_page_count, |
| 1298 is_scripted)) { |
| 1300 DidFinishPrinting(OK); // Release resources and fail silently. | 1299 DidFinishPrinting(OK); // Release resources and fail silently. |
| 1301 return; | 1300 return; |
| 1302 } | 1301 } |
| 1303 | 1302 |
| 1304 // Render Pages for printing. | 1303 // Render Pages for printing. |
| 1305 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { | 1304 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { |
| 1306 LOG(ERROR) << "RenderPagesForPrint failed"; | 1305 LOG(ERROR) << "RenderPagesForPrint failed"; |
| 1307 DidFinishPrinting(FAIL_PRINT); | 1306 DidFinishPrinting(FAIL_PRINT); |
| 1308 } | 1307 } |
| 1309 scripting_throttler_.Reset(); | 1308 scripting_throttler_.Reset(); |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1566 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); | 1565 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); |
| 1567 | 1566 |
| 1568 return false; | 1567 return false; |
| 1569 } | 1568 } |
| 1570 | 1569 |
| 1571 return true; | 1570 return true; |
| 1572 } | 1571 } |
| 1573 | 1572 |
| 1574 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebFrame* frame, | 1573 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebFrame* frame, |
| 1575 const blink::WebNode& node, | 1574 const blink::WebNode& node, |
| 1576 int expected_pages_count) { | 1575 int expected_pages_count, |
| 1576 bool is_scripted) { |
| 1577 PrintHostMsg_ScriptedPrint_Params params; | 1577 PrintHostMsg_ScriptedPrint_Params params; |
| 1578 PrintMsg_PrintPages_Params print_settings; | 1578 PrintMsg_PrintPages_Params print_settings; |
| 1579 | 1579 |
| 1580 params.cookie = print_pages_params_->params.document_cookie; | 1580 params.cookie = print_pages_params_->params.document_cookie; |
| 1581 params.has_selection = frame->hasSelection(); | 1581 params.has_selection = frame->hasSelection(); |
| 1582 params.expected_pages_count = expected_pages_count; | 1582 params.expected_pages_count = expected_pages_count; |
| 1583 MarginType margin_type = DEFAULT_MARGINS; | 1583 MarginType margin_type = DEFAULT_MARGINS; |
| 1584 if (PrintingNodeOrPdfFrame(frame, node)) | 1584 if (PrintingNodeOrPdfFrame(frame, node)) |
| 1585 margin_type = GetMarginsForPdf(frame, node); | 1585 margin_type = GetMarginsForPdf(frame, node); |
| 1586 params.margin_type = margin_type; | 1586 params.margin_type = margin_type; |
| 1587 params.is_scripted = is_scripted; |
| 1587 | 1588 |
| 1588 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); | 1589 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); |
| 1589 | 1590 |
| 1590 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the | 1591 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the |
| 1591 // value before and restore it afterwards. | 1592 // value before and restore it afterwards. |
| 1592 blink::WebPrintScalingOption scaling_option = | 1593 blink::WebPrintScalingOption scaling_option = |
| 1593 print_pages_params_->params.print_scaling_option; | 1594 print_pages_params_->params.print_scaling_option; |
| 1594 | 1595 |
| 1595 print_pages_params_.reset(); | 1596 print_pages_params_.reset(); |
| 1596 IPC::SyncMessage* msg = | 1597 IPC::SyncMessage* msg = |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2055 blink::WebConsoleMessage::LevelWarning, message)); | 2056 blink::WebConsoleMessage::LevelWarning, message)); |
| 2056 return false; | 2057 return false; |
| 2057 } | 2058 } |
| 2058 | 2059 |
| 2059 void PrintWebViewHelper::ScriptingThrottler::Reset() { | 2060 void PrintWebViewHelper::ScriptingThrottler::Reset() { |
| 2060 // Reset counter on successful print. | 2061 // Reset counter on successful print. |
| 2061 count_ = 0; | 2062 count_ = 0; |
| 2062 } | 2063 } |
| 2063 | 2064 |
| 2064 } // namespace printing | 2065 } // namespace printing |
| OLD | NEW |