| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 #include "ui/base/l10n/l10n_util.h" | 48 #include "ui/base/l10n/l10n_util.h" |
| 49 #include "ui/base/resource/resource_bundle.h" | 49 #include "ui/base/resource/resource_bundle.h" |
| 50 #include "webkit/common/webpreferences.h" | 50 #include "webkit/common/webpreferences.h" |
| 51 | 51 |
| 52 namespace printing { | 52 namespace printing { |
| 53 | 53 |
| 54 namespace { | 54 namespace { |
| 55 | 55 |
| 56 enum PrintPreviewHelperEvents { | 56 enum PrintPreviewHelperEvents { |
| 57 PREVIEW_EVENT_REQUESTED, | 57 PREVIEW_EVENT_REQUESTED, |
| 58 PREVIEW_EVENT_CACHE_HIT, | 58 PREVIEW_EVENT_CACHE_HIT, // Unused |
| 59 PREVIEW_EVENT_CREATE_DOCUMENT, | 59 PREVIEW_EVENT_CREATE_DOCUMENT, |
| 60 PREVIEW_EVENT_NEW_SETTINGS, | 60 PREVIEW_EVENT_NEW_SETTINGS, // Unused |
| 61 PREVIEW_EVENT_MAX, | 61 PREVIEW_EVENT_MAX, |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 const double kMinDpi = 1.0; | 64 const double kMinDpi = 1.0; |
| 65 | 65 |
| 66 const char kPageLoadScriptFormat[] = | 66 const char kPageLoadScriptFormat[] = |
| 67 "document.open(); document.write(%s); document.close();"; | 67 "document.open(); document.write(%s); document.close();"; |
| 68 | 68 |
| 69 const char kPageSetupScriptFormat[] = "setup(%s);"; | 69 const char kPageSetupScriptFormat[] = "setup(%s);"; |
| 70 | 70 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 87 #endif // defined(OS_MACOSX) | 87 #endif // defined(OS_MACOSX) |
| 88 } | 88 } |
| 89 | 89 |
| 90 bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) { | 90 bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) { |
| 91 return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() && | 91 return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() && |
| 92 !params.printable_area.IsEmpty() && params.document_cookie && | 92 !params.printable_area.IsEmpty() && params.document_cookie && |
| 93 params.desired_dpi && params.max_shrink && params.min_shrink && | 93 params.desired_dpi && params.max_shrink && params.min_shrink && |
| 94 params.dpi && (params.margin_top >= 0) && (params.margin_left >= 0); | 94 params.dpi && (params.margin_top >= 0) && (params.margin_left >= 0); |
| 95 } | 95 } |
| 96 | 96 |
| 97 bool PageLayoutIsEqual(const PrintMsg_PrintPages_Params& oldParams, | |
| 98 const PrintMsg_PrintPages_Params& newParams) { | |
| 99 return oldParams.params.content_size == newParams.params.content_size && | |
| 100 oldParams.params.printable_area == newParams.params.printable_area && | |
| 101 oldParams.params.page_size == newParams.params.page_size && | |
| 102 oldParams.params.margin_top == newParams.params.margin_top && | |
| 103 oldParams.params.margin_left == newParams.params.margin_left && | |
| 104 oldParams.params.desired_dpi == newParams.params.desired_dpi && | |
| 105 oldParams.params.dpi == newParams.params.dpi; | |
| 106 } | |
| 107 | |
| 108 bool PrintMsg_Print_Params_IsEqual( | |
| 109 const PrintMsg_PrintPages_Params& oldParams, | |
| 110 const PrintMsg_PrintPages_Params& newParams) { | |
| 111 return PageLayoutIsEqual(oldParams, newParams) && | |
| 112 oldParams.params.max_shrink == newParams.params.max_shrink && | |
| 113 oldParams.params.min_shrink == newParams.params.min_shrink && | |
| 114 oldParams.params.selection_only == newParams.params.selection_only && | |
| 115 oldParams.params.supports_alpha_blend == | |
| 116 newParams.params.supports_alpha_blend && | |
| 117 oldParams.pages.size() == newParams.pages.size() && | |
| 118 oldParams.params.print_to_pdf == newParams.params.print_to_pdf && | |
| 119 oldParams.params.print_scaling_option == | |
| 120 newParams.params.print_scaling_option && | |
| 121 oldParams.params.display_header_footer == | |
| 122 newParams.params.display_header_footer && | |
| 123 oldParams.params.date == newParams.params.date && | |
| 124 oldParams.params.title == newParams.params.title && | |
| 125 oldParams.params.url == newParams.params.url && | |
| 126 std::equal(oldParams.pages.begin(), oldParams.pages.end(), | |
| 127 newParams.pages.begin()) && | |
| 128 oldParams.params.should_print_backgrounds == | |
| 129 newParams.params.should_print_backgrounds; | |
| 130 } | |
| 131 | |
| 132 PrintMsg_Print_Params GetCssPrintParams( | 97 PrintMsg_Print_Params GetCssPrintParams( |
| 133 WebKit::WebFrame* frame, | 98 WebKit::WebFrame* frame, |
| 134 int page_index, | 99 int page_index, |
| 135 const PrintMsg_Print_Params& page_params) { | 100 const PrintMsg_Print_Params& page_params) { |
| 136 PrintMsg_Print_Params page_css_params = page_params; | 101 PrintMsg_Print_Params page_css_params = page_params; |
| 137 int dpi = GetDPI(&page_params); | 102 int dpi = GetDPI(&page_params); |
| 138 | 103 |
| 139 WebKit::WebSize page_size_in_pixels( | 104 WebKit::WebSize page_size_in_pixels( |
| 140 ConvertUnit(page_params.page_size.width(), dpi, kPixelsPerInch), | 105 ConvertUnit(page_params.page_size.width(), dpi, kPixelsPerInch), |
| 141 ConvertUnit(page_params.page_size.height(), dpi, kPixelsPerInch)); | 106 ConvertUnit(page_params.page_size.height(), dpi, kPixelsPerInch)); |
| (...skipping 835 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 977 print_preview_context_.source_node(), settings)) { | 942 print_preview_context_.source_node(), settings)) { |
| 978 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 943 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
| 979 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 944 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
| 980 routing_id(), print_pages_params_->params.document_cookie)); | 945 routing_id(), print_pages_params_->params.document_cookie)); |
| 981 notify_browser_of_print_failure_ = false; // Already sent. | 946 notify_browser_of_print_failure_ = false; // Already sent. |
| 982 } | 947 } |
| 983 DidFinishPrinting(FAIL_PREVIEW); | 948 DidFinishPrinting(FAIL_PREVIEW); |
| 984 return; | 949 return; |
| 985 } | 950 } |
| 986 | 951 |
| 987 if (!print_pages_params_->params.is_first_request && | |
| 988 old_print_pages_params_.get() && | |
| 989 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_, | |
| 990 *print_pages_params_)) { | |
| 991 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", | |
| 992 PREVIEW_EVENT_CACHE_HIT, PREVIEW_EVENT_MAX); | |
| 993 PrintHostMsg_DidPreviewDocument_Params preview_params; | |
| 994 preview_params.reuse_existing_data = true; | |
| 995 preview_params.data_size = 0; | |
| 996 preview_params.document_cookie = | |
| 997 print_pages_params_->params.document_cookie; | |
| 998 preview_params.expected_pages_count = | |
| 999 print_preview_context_.total_page_count(); | |
| 1000 preview_params.modifiable = print_preview_context_.IsModifiable(); | |
| 1001 preview_params.preview_request_id = | |
| 1002 print_pages_params_->params.preview_request_id; | |
| 1003 | |
| 1004 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), | |
| 1005 preview_params)); | |
| 1006 return; | |
| 1007 } | |
| 1008 | |
| 1009 // If we are previewing a pdf and the print scaling is disabled, send a | 952 // If we are previewing a pdf and the print scaling is disabled, send a |
| 1010 // message to browser. | 953 // message to browser. |
| 1011 if (print_pages_params_->params.is_first_request && | 954 if (print_pages_params_->params.is_first_request && |
| 1012 !print_preview_context_.IsModifiable() && | 955 !print_preview_context_.IsModifiable() && |
| 1013 print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin( | 956 print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin( |
| 1014 print_preview_context_.source_node())) { | 957 print_preview_context_.source_node())) { |
| 1015 Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id())); | 958 Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id())); |
| 1016 } | 959 } |
| 1017 | 960 |
| 1018 // Always clear |old_print_pages_params_| before rendering the pages. | |
| 1019 old_print_pages_params_.reset(); | |
| 1020 is_print_ready_metafile_sent_ = false; | 961 is_print_ready_metafile_sent_ = false; |
| 1021 | 962 |
| 1022 // PDF printer device supports alpha blending. | 963 // PDF printer device supports alpha blending. |
| 1023 print_pages_params_->params.supports_alpha_blend = true; | 964 print_pages_params_->params.supports_alpha_blend = true; |
| 1024 | 965 |
| 1025 bool generate_draft_pages = false; | 966 bool generate_draft_pages = false; |
| 1026 if (!settings.GetBoolean(kSettingGenerateDraftData, | 967 if (!settings.GetBoolean(kSettingGenerateDraftData, |
| 1027 &generate_draft_pages)) { | 968 &generate_draft_pages)) { |
| 1028 NOTREACHED(); | 969 NOTREACHED(); |
| 1029 } | 970 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1080 if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(), | 1021 if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(), |
| 1081 pages)) { | 1022 pages)) { |
| 1082 return false; | 1023 return false; |
| 1083 } | 1024 } |
| 1084 | 1025 |
| 1085 PageSizeMargins default_page_layout; | 1026 PageSizeMargins default_page_layout; |
| 1086 ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0, | 1027 ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0, |
| 1087 print_params, ignore_css_margins_, NULL, | 1028 print_params, ignore_css_margins_, NULL, |
| 1088 &default_page_layout); | 1029 &default_page_layout); |
| 1089 | 1030 |
| 1090 if (!old_print_pages_params_.get() || | 1031 bool has_page_size_style = PrintingFrameHasPageSizeStyle( |
| 1091 !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) { | 1032 print_preview_context_.prepared_frame(), |
| 1092 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", | 1033 print_preview_context_.total_page_count()); |
| 1093 PREVIEW_EVENT_NEW_SETTINGS, PREVIEW_EVENT_MAX); | 1034 int dpi = GetDPI(&print_params); |
| 1094 bool has_page_size_style = PrintingFrameHasPageSizeStyle( | |
| 1095 print_preview_context_.prepared_frame(), | |
| 1096 print_preview_context_.total_page_count()); | |
| 1097 int dpi = GetDPI(&print_params); | |
| 1098 | 1035 |
| 1099 gfx::Rect printable_area_in_points( | 1036 gfx::Rect printable_area_in_points( |
| 1100 ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch), | 1037 ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch), |
| 1101 ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch), | 1038 ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch), |
| 1102 ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch), | 1039 ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch), |
| 1103 ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch)); | 1040 ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch)); |
| 1104 | 1041 |
| 1105 // Margins: Send default page layout to browser process. | 1042 // Margins: Send default page layout to browser process. |
| 1106 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(), | 1043 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(), |
| 1107 default_page_layout, | 1044 default_page_layout, |
| 1108 printable_area_in_points, | 1045 printable_area_in_points, |
| 1109 has_page_size_style)); | 1046 has_page_size_style)); |
| 1110 } | |
| 1111 | 1047 |
| 1112 PrintHostMsg_DidGetPreviewPageCount_Params params; | 1048 PrintHostMsg_DidGetPreviewPageCount_Params params; |
| 1113 params.page_count = print_preview_context_.total_page_count(); | 1049 params.page_count = print_preview_context_.total_page_count(); |
| 1114 params.is_modifiable = print_preview_context_.IsModifiable(); | 1050 params.is_modifiable = print_preview_context_.IsModifiable(); |
| 1115 params.document_cookie = print_params.document_cookie; | 1051 params.document_cookie = print_params.document_cookie; |
| 1116 params.preview_request_id = print_params.preview_request_id; | 1052 params.preview_request_id = print_params.preview_request_id; |
| 1117 params.clear_preview_data = print_preview_context_.generate_draft_pages(); | 1053 params.clear_preview_data = print_preview_context_.generate_draft_pages(); |
| 1118 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); | 1054 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); |
| 1119 if (CheckForCancel()) | 1055 if (CheckForCancel()) |
| 1120 return false; | 1056 return false; |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1271 | 1207 |
| 1272 // Render Pages for printing. | 1208 // Render Pages for printing. |
| 1273 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { | 1209 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { |
| 1274 LOG(ERROR) << "RenderPagesForPrint failed"; | 1210 LOG(ERROR) << "RenderPagesForPrint failed"; |
| 1275 DidFinishPrinting(FAIL_PRINT); | 1211 DidFinishPrinting(FAIL_PRINT); |
| 1276 } | 1212 } |
| 1277 ResetScriptedPrintCount(); | 1213 ResetScriptedPrintCount(); |
| 1278 } | 1214 } |
| 1279 | 1215 |
| 1280 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { | 1216 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { |
| 1281 bool store_print_pages_params = true; | |
| 1282 switch (result) { | 1217 switch (result) { |
| 1283 case OK: | 1218 case OK: |
| 1284 break; | 1219 break; |
| 1285 | 1220 |
| 1286 case FAIL_PRINT_INIT: | 1221 case FAIL_PRINT_INIT: |
| 1287 DCHECK(!notify_browser_of_print_failure_); | 1222 DCHECK(!notify_browser_of_print_failure_); |
| 1288 break; | 1223 break; |
| 1289 | 1224 |
| 1290 case FAIL_PRINT: | 1225 case FAIL_PRINT: |
| 1291 if (notify_browser_of_print_failure_ && print_pages_params_.get()) { | 1226 if (notify_browser_of_print_failure_ && print_pages_params_.get()) { |
| 1292 int cookie = print_pages_params_->params.document_cookie; | 1227 int cookie = print_pages_params_->params.document_cookie; |
| 1293 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); | 1228 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); |
| 1294 } | 1229 } |
| 1295 break; | 1230 break; |
| 1296 | 1231 |
| 1297 case FAIL_PREVIEW: | 1232 case FAIL_PREVIEW: |
| 1298 DCHECK(is_preview_enabled_); | 1233 DCHECK(is_preview_enabled_); |
| 1299 store_print_pages_params = false; | |
| 1300 int cookie = print_pages_params_.get() ? | 1234 int cookie = print_pages_params_.get() ? |
| 1301 print_pages_params_->params.document_cookie : 0; | 1235 print_pages_params_->params.document_cookie : 0; |
| 1302 if (notify_browser_of_print_failure_) { | 1236 if (notify_browser_of_print_failure_) { |
| 1303 LOG(ERROR) << "CreatePreviewDocument failed"; | 1237 LOG(ERROR) << "CreatePreviewDocument failed"; |
| 1304 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); | 1238 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); |
| 1305 } else { | 1239 } else { |
| 1306 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); | 1240 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); |
| 1307 } | 1241 } |
| 1308 print_preview_context_.Failed(notify_browser_of_print_failure_); | 1242 print_preview_context_.Failed(notify_browser_of_print_failure_); |
| 1309 break; | 1243 break; |
| 1310 } | 1244 } |
| 1311 | |
| 1312 prep_frame_view_.reset(); | 1245 prep_frame_view_.reset(); |
| 1313 | 1246 print_pages_params_.reset(); |
| 1314 if (store_print_pages_params) { | |
| 1315 old_print_pages_params_.reset(print_pages_params_.release()); | |
| 1316 } else { | |
| 1317 print_pages_params_.reset(); | |
| 1318 old_print_pages_params_.reset(); | |
| 1319 } | |
| 1320 | |
| 1321 notify_browser_of_print_failure_ = true; | 1247 notify_browser_of_print_failure_ = true; |
| 1322 } | 1248 } |
| 1323 | 1249 |
| 1324 void PrintWebViewHelper::OnFramePreparedForPrintPages() { | 1250 void PrintWebViewHelper::OnFramePreparedForPrintPages() { |
| 1325 PrintPages(); | 1251 PrintPages(); |
| 1326 FinishFramePrinting(); | 1252 FinishFramePrinting(); |
| 1327 } | 1253 } |
| 1328 | 1254 |
| 1329 void PrintWebViewHelper::PrintPages() { | 1255 void PrintWebViewHelper::PrintPages() { |
| 1330 if (!prep_frame_view_) // Printing is already canceled or failed. | 1256 if (!prep_frame_view_) // Printing is already canceled or failed. |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1697 if (is_scripted_preview_delayed_) { | 1623 if (is_scripted_preview_delayed_) { |
| 1698 is_scripted_preview_delayed_ = false; | 1624 is_scripted_preview_delayed_ = false; |
| 1699 Send(new PrintHostMsg_ShowScriptedPrintPreview(routing_id(), | 1625 Send(new PrintHostMsg_ShowScriptedPrintPreview(routing_id(), |
| 1700 print_preview_context_.IsModifiable())); | 1626 print_preview_context_.IsModifiable())); |
| 1701 } | 1627 } |
| 1702 } | 1628 } |
| 1703 | 1629 |
| 1704 void PrintWebViewHelper::RequestPrintPreview(PrintPreviewRequestType type) { | 1630 void PrintWebViewHelper::RequestPrintPreview(PrintPreviewRequestType type) { |
| 1705 const bool is_modifiable = print_preview_context_.IsModifiable(); | 1631 const bool is_modifiable = print_preview_context_.IsModifiable(); |
| 1706 const bool has_selection = print_preview_context_.HasSelection(); | 1632 const bool has_selection = print_preview_context_.HasSelection(); |
| 1707 old_print_pages_params_.reset(); | |
| 1708 PrintHostMsg_RequestPrintPreview_Params params; | 1633 PrintHostMsg_RequestPrintPreview_Params params; |
| 1709 params.is_modifiable = is_modifiable; | 1634 params.is_modifiable = is_modifiable; |
| 1710 params.has_selection = has_selection; | 1635 params.has_selection = has_selection; |
| 1711 switch (type) { | 1636 switch (type) { |
| 1712 case PRINT_PREVIEW_SCRIPTED: { | 1637 case PRINT_PREVIEW_SCRIPTED: { |
| 1713 // Shows scripted print preview in two stages. | 1638 // Shows scripted print preview in two stages. |
| 1714 // 1. PrintHostMsg_SetupScriptedPrintPreview blocks this call and JS by | 1639 // 1. PrintHostMsg_SetupScriptedPrintPreview blocks this call and JS by |
| 1715 // pumping messages here. | 1640 // pumping messages here. |
| 1716 // 2. PrintHostMsg_ShowScriptedPrintPreview shows preview once the | 1641 // 2. PrintHostMsg_ShowScriptedPrintPreview shows preview once the |
| 1717 // document has been loaded. | 1642 // document has been loaded. |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2039 } | 1964 } |
| 2040 | 1965 |
| 2041 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1966 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 2042 prep_frame_view_.reset(); | 1967 prep_frame_view_.reset(); |
| 2043 metafile_.reset(); | 1968 metafile_.reset(); |
| 2044 pages_to_render_.clear(); | 1969 pages_to_render_.clear(); |
| 2045 error_ = PREVIEW_ERROR_NONE; | 1970 error_ = PREVIEW_ERROR_NONE; |
| 2046 } | 1971 } |
| 2047 | 1972 |
| 2048 } // namespace printing | 1973 } // namespace printing |
| OLD | NEW |