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

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

Issue 9703013: Printing: Catch more error conditions and remove more cruft. (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
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 986 matching lines...) Expand 10 before | Expand all | Expand 10 after
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);
Lei Zhang 2012/03/14 05:40:05 InitPrintSettingsAndPrepareFrame() already display
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
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_);
1062 } 1072 }
1063 1073
1064 if (print_web_view_) { 1074 if (print_web_view_) {
1065 print_web_view_->close(); 1075 print_web_view_->close();
1066 print_web_view_ = NULL; 1076 print_web_view_ = NULL;
1067 } 1077 }
1068 1078
1069 if (store_print_pages_params) { 1079 if (store_print_pages_params) {
1070 old_print_pages_params_.reset(print_pages_params_.release()); 1080 old_print_pages_params_.reset(print_pages_params_.release());
1071 } else { 1081 } else {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
1168 bool ignore_css_margins, 1178 bool ignore_css_margins,
1169 bool fit_to_page) { 1179 bool fit_to_page) {
1170 if (PrintingNodeOrPdfFrame(frame, node)) 1180 if (PrintingNodeOrPdfFrame(frame, node))
1171 return; 1181 return;
1172 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( 1182 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss(
1173 frame, 0, params, ignore_css_margins, ignore_css_margins && fit_to_page, 1183 frame, 0, params, ignore_css_margins, ignore_css_margins && fit_to_page,
1174 NULL); 1184 NULL);
1175 prepare->UpdatePrintParams(print_params); 1185 prepare->UpdatePrintParams(print_params);
1176 } 1186 }
1177 1187
1178 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, 1188 bool PrintWebViewHelper::InitPrintSettings() {
1179 const WebKit::WebNode& node) {
1180 DCHECK(frame);
1181 PrintMsg_PrintPages_Params settings; 1189 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(), 1190 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
1188 &settings.params)); 1191 &settings.params));
1189 // Check if the printer returned any settings, if the settings is empty, we 1192 // 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 1193 // can safely assume there are no printer drivers configured. So we safely
1191 // terminate. 1194 // terminate.
1192 bool result = true; 1195 bool result = true;
1193 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { 1196 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; 1197 result = false;
1199 }
1200 1198
1201 if (result && 1199 if (result &&
1202 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { 1200 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) {
1203 // Invalid print page settings. 1201 // Invalid print page settings.
1204 NOTREACHED(); 1202 NOTREACHED();
1205 result = false; 1203 result = false;
1206 } 1204 }
1207 1205
1206 // Reset to default values.
1207 ignore_css_margins_ = false;
1208 fit_to_page_ = true;
1208 settings.pages.clear(); 1209 settings.pages.clear();
1210
1209 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1211 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1210 return result; 1212 return result;
1211 } 1213 }
1212 1214
1213 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( 1215 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame(
1214 WebKit::WebFrame* frame, const WebKit::WebNode& node, 1216 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1215 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { 1217 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
1216 if (!InitPrintSettings(frame, node)) 1218 DCHECK(frame);
1219 if (!InitPrintSettings()) {
1220 notify_browser_of_print_failure_ = false;
1221 render_view()->RunModalAlertDialog(
1222 frame,
1223 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1217 return false; 1224 return false;
1225 }
1218 1226
1219 DCHECK(!prepare->get()); 1227 DCHECK(!prepare->get());
1220 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1228 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1221 frame, node)); 1229 frame, node));
1222 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), 1230 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(),
1223 print_pages_params_->params, 1231 print_pages_params_->params,
1224 ignore_css_margins_, fit_to_page_); 1232 ignore_css_margins_, fit_to_page_);
1225 Send(new PrintHostMsg_DidGetDocumentCookie( 1233 Send(new PrintHostMsg_DidGetDocumentCookie(
1226 routing_id(), print_pages_params_->params.document_cookie)); 1234 routing_id(), print_pages_params_->params.document_cookie));
1227 return true; 1235 return true;
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after
1766 DCHECK(IsRendering()); 1774 DCHECK(IsRendering());
1767 return prep_frame_view_->GetPrintCanvasSize(); 1775 return prep_frame_view_->GetPrintCanvasSize();
1768 } 1776 }
1769 1777
1770 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1778 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1771 prep_frame_view_.reset(); 1779 prep_frame_view_.reset();
1772 metafile_.reset(); 1780 metafile_.reset();
1773 pages_to_render_.clear(); 1781 pages_to_render_.clear();
1774 error_ = PREVIEW_ERROR_NONE; 1782 error_ = PREVIEW_ERROR_NONE;
1775 } 1783 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698