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/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 986 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
997 | 997 |
998 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, | 998 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, |
999 const WebKit::WebNode& node) { | 999 const WebKit::WebNode& node) { |
1000 // If still not finished with earlier print request simply ignore. | 1000 // If still not finished with earlier print request simply ignore. |
1001 if (print_web_view_) | 1001 if (print_web_view_) |
1002 return; | 1002 return; |
1003 | 1003 |
1004 // Initialize print settings. | 1004 // Initialize print settings. |
1005 scoped_ptr<PrepareFrameAndViewForPrint> prepare; | 1005 scoped_ptr<PrepareFrameAndViewForPrint> prepare; |
1006 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) { | 1006 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) { |
1007 DidFinishPrinting(FAIL_PRINT); | 1007 DidFinishPrinting(FAIL_PRINT_INIT); |
1008 return; // Failed to init print page settings. | 1008 return; // Failed to init print page settings. |
1009 } | 1009 } |
1010 | 1010 |
1011 int expected_page_count = 0; | 1011 int expected_page_count = 0; |
1012 bool use_browser_overlays = true; | 1012 bool use_browser_overlays = true; |
1013 | 1013 |
1014 expected_page_count = prepare->GetExpectedPageCount(); | 1014 expected_page_count = prepare->GetExpectedPageCount(); |
1015 if (expected_page_count) | 1015 if (expected_page_count) |
1016 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); | 1016 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); |
1017 | 1017 |
(...skipping 17 matching lines...) Expand all Loading... |
1035 // Render Pages for printing. | 1035 // Render Pages for printing. |
1036 if (!RenderPagesForPrint(frame, node)) { | 1036 if (!RenderPagesForPrint(frame, node)) { |
1037 LOG(ERROR) << "RenderPagesForPrint failed"; | 1037 LOG(ERROR) << "RenderPagesForPrint failed"; |
1038 DidFinishPrinting(FAIL_PRINT); | 1038 DidFinishPrinting(FAIL_PRINT); |
1039 } | 1039 } |
1040 ResetScriptedPrintCount(); | 1040 ResetScriptedPrintCount(); |
1041 } | 1041 } |
1042 | 1042 |
1043 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { | 1043 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { |
1044 bool store_print_pages_params = true; | 1044 bool store_print_pages_params = true; |
1045 if (result == FAIL_PRINT) { | 1045 switch (result) { |
1046 DisplayPrintJobError(); | 1046 case OK: |
| 1047 break; |
1047 | 1048 |
1048 if (notify_browser_of_print_failure_ && print_pages_params_.get()) { | 1049 case FAIL_PRINT_INIT: |
1049 int cookie = print_pages_params_->params.document_cookie; | 1050 DCHECK(!notify_browser_of_print_failure_); |
1050 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); | 1051 break; |
1051 } | 1052 |
1052 } else if (result == FAIL_PREVIEW) { | 1053 case FAIL_PRINT: |
1053 DCHECK(is_preview_enabled_); | 1054 DisplayPrintJobError(); |
1054 store_print_pages_params = false; | 1055 |
1055 int cookie = print_pages_params_.get() ? | 1056 if (notify_browser_of_print_failure_ && print_pages_params_.get()) { |
1056 print_pages_params_->params.document_cookie : 0; | 1057 int cookie = print_pages_params_->params.document_cookie; |
1057 if (notify_browser_of_print_failure_) | 1058 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); |
1058 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); | 1059 } |
1059 else | 1060 break; |
1060 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); | 1061 |
1061 print_preview_context_.Failed(notify_browser_of_print_failure_); | 1062 case FAIL_PREVIEW: |
| 1063 DCHECK(is_preview_enabled_); |
| 1064 store_print_pages_params = false; |
| 1065 int cookie = print_pages_params_.get() ? |
| 1066 print_pages_params_->params.document_cookie : 0; |
| 1067 if (notify_browser_of_print_failure_) |
| 1068 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); |
| 1069 else |
| 1070 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); |
| 1071 print_preview_context_.Failed(notify_browser_of_print_failure_); |
| 1072 break; |
1062 } | 1073 } |
1063 | 1074 |
1064 if (print_web_view_) { | 1075 if (print_web_view_) { |
1065 print_web_view_->close(); | 1076 print_web_view_->close(); |
1066 print_web_view_ = NULL; | 1077 print_web_view_ = NULL; |
1067 } | 1078 } |
1068 | 1079 |
1069 if (store_print_pages_params) { | 1080 if (store_print_pages_params) { |
1070 old_print_pages_params_.reset(print_pages_params_.release()); | 1081 old_print_pages_params_.reset(print_pages_params_.release()); |
1071 } else { | 1082 } else { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1168 bool ignore_css_margins, | 1179 bool ignore_css_margins, |
1169 bool fit_to_page) { | 1180 bool fit_to_page) { |
1170 if (PrintingNodeOrPdfFrame(frame, node)) | 1181 if (PrintingNodeOrPdfFrame(frame, node)) |
1171 return; | 1182 return; |
1172 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( | 1183 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( |
1173 frame, 0, params, ignore_css_margins, ignore_css_margins && fit_to_page, | 1184 frame, 0, params, ignore_css_margins, ignore_css_margins && fit_to_page, |
1174 NULL); | 1185 NULL); |
1175 prepare->UpdatePrintParams(print_params); | 1186 prepare->UpdatePrintParams(print_params); |
1176 } | 1187 } |
1177 | 1188 |
1178 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, | 1189 bool PrintWebViewHelper::InitPrintSettings() { |
1179 const WebKit::WebNode& node) { | |
1180 DCHECK(frame); | |
1181 PrintMsg_PrintPages_Params settings; | 1190 PrintMsg_PrintPages_Params settings; |
1182 | |
1183 // Reset to default values. | |
1184 ignore_css_margins_ = false; | |
1185 fit_to_page_ = true; | |
1186 | |
1187 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1191 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
1188 &settings.params)); | 1192 &settings.params)); |
1189 // Check if the printer returned any settings, if the settings is empty, we | 1193 // Check if the printer returned any settings, if the settings is empty, we |
1190 // can safely assume there are no printer drivers configured. So we safely | 1194 // can safely assume there are no printer drivers configured. So we safely |
1191 // terminate. | 1195 // terminate. |
1192 bool result = true; | 1196 bool result = true; |
1193 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { | 1197 if (PrintMsg_Print_Params_IsEmpty(settings.params)) |
1194 render_view()->RunModalAlertDialog( | |
1195 frame, | |
1196 l10n_util::GetStringUTF16( | |
1197 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | |
1198 result = false; | 1198 result = false; |
1199 } | |
1200 | 1199 |
1201 if (result && | 1200 if (result && |
1202 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { | 1201 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { |
1203 // Invalid print page settings. | 1202 // Invalid print page settings. |
1204 NOTREACHED(); | 1203 NOTREACHED(); |
1205 result = false; | 1204 result = false; |
1206 } | 1205 } |
1207 | 1206 |
| 1207 // Reset to default values. |
| 1208 ignore_css_margins_ = false; |
| 1209 fit_to_page_ = true; |
1208 settings.pages.clear(); | 1210 settings.pages.clear(); |
| 1211 |
1209 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1212 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
1210 return result; | 1213 return result; |
1211 } | 1214 } |
1212 | 1215 |
1213 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1216 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
1214 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1217 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
1215 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | 1218 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
1216 if (!InitPrintSettings(frame, node)) | 1219 DCHECK(frame); |
| 1220 if (!InitPrintSettings()) { |
| 1221 notify_browser_of_print_failure_ = false; |
| 1222 render_view()->RunModalAlertDialog( |
| 1223 frame, |
| 1224 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
1217 return false; | 1225 return false; |
| 1226 } |
1218 | 1227 |
1219 DCHECK(!prepare->get()); | 1228 DCHECK(!prepare->get()); |
1220 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1229 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
1221 frame, node)); | 1230 frame, node)); |
1222 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), | 1231 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), |
1223 print_pages_params_->params, | 1232 print_pages_params_->params, |
1224 ignore_css_margins_, fit_to_page_); | 1233 ignore_css_margins_, fit_to_page_); |
1225 Send(new PrintHostMsg_DidGetDocumentCookie( | 1234 Send(new PrintHostMsg_DidGetDocumentCookie( |
1226 routing_id(), print_pages_params_->params.document_cookie)); | 1235 routing_id(), print_pages_params_->params.document_cookie)); |
1227 return true; | 1236 return true; |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1766 DCHECK(IsRendering()); | 1775 DCHECK(IsRendering()); |
1767 return prep_frame_view_->GetPrintCanvasSize(); | 1776 return prep_frame_view_->GetPrintCanvasSize(); |
1768 } | 1777 } |
1769 | 1778 |
1770 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1779 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1771 prep_frame_view_.reset(); | 1780 prep_frame_view_.reset(); |
1772 metafile_.reset(); | 1781 metafile_.reset(); |
1773 pages_to_render_.clear(); | 1782 pages_to_render_.clear(); |
1774 error_ = PREVIEW_ERROR_NONE; | 1783 error_ = PREVIEW_ERROR_NONE; |
1775 } | 1784 } |
OLD | NEW |