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

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

Issue 8201027: Move margin processing code to the browser process. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address nits Created 9 years, 2 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
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | printing/page_setup.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 print_params.desired_dpi)); 145 print_params.desired_dpi));
146 } 146 }
147 147
148 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { 148 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) {
149 if (!node.isNull()) 149 if (!node.isNull())
150 return true; 150 return true;
151 std::string mime(frame->dataSource()->response().mimeType().utf8()); 151 std::string mime(frame->dataSource()->response().mimeType().utf8());
152 return mime == "application/pdf"; 152 return mime == "application/pdf";
153 } 153 }
154 154
155 void SetMarginsForPDF(PrintMsg_Print_Params* settings) { 155 void SetMarginsForPdf(DictionaryValue* job_settings, bool force_no_margins) {
156 // This is the wrong way to do this. But the pipeline for the right way is 156 // TODO(vandebo) When it's plumbed through, check if the plugin wants us to
157 // too long. This will be removed soon. http://crbug.com/92000 157 // scale or not. For now, assume the answer is yes.
158 settings->margin_top = 0; 158 if (force_no_margins) {
159 settings->margin_left = 0; 159 job_settings->SetInteger(printing::kSettingMarginsType,
160 settings->printable_size.set_width(settings->page_size.width()); 160 printing::NO_MARGINS);
161 settings->printable_size.set_height(settings->page_size.height()); 161 } else {
162 } 162 job_settings->SetInteger(printing::kSettingMarginsType,
163 163 printing::PRINTABLE_AREA_MARGINS);
164 // Get the margins option selected and set custom margins appropriately.
165 void SetCustomMarginsIfSelected(const DictionaryValue& job_settings,
166 PrintMsg_PrintPages_Params* settings) {
167 int margin_type = printing::DEFAULT_MARGINS;
168 if (!job_settings.GetInteger(printing::kSettingMarginsType, &margin_type)) {
169 NOTREACHED();
170 } 164 }
171 165
172 if (margin_type == printing::DEFAULT_MARGINS)
173 return;
174
175 double custom_margin_top_in_points = 0;
176 double custom_margin_left_in_points = 0;
177 double custom_margin_right_in_points = 0;
178 double custom_margin_bottom_in_points = 0;
179 if (margin_type == printing::CUSTOM_MARGINS) {
180 DictionaryValue* custom_margins;
181 if (!job_settings.GetDictionary(printing::kSettingMarginsCustom,
182 &custom_margins)) {
183 NOTREACHED();
184 return;
185 }
186 if (!custom_margins->GetDouble(printing::kSettingMarginTop,
187 &custom_margin_top_in_points) ||
188 !custom_margins->GetDouble(printing::kSettingMarginLeft,
189 &custom_margin_left_in_points) ||
190 !custom_margins->GetDouble(printing::kSettingMarginRight,
191 &custom_margin_right_in_points) ||
192 !custom_margins->GetDouble(printing::kSettingMarginBottom,
193 &custom_margin_bottom_in_points)) {
194 NOTREACHED();
195 return;
196 }
197 }
198
199 int dpi = GetDPI(&settings->params);
200 double custom_margin_top_in_dots = ConvertUnitDouble(
201 custom_margin_top_in_points, printing::kPointsPerInch, dpi);
202 double custom_margin_left_in_dots = ConvertUnitDouble(
203 custom_margin_left_in_points, printing::kPointsPerInch, dpi);
204 double custom_margin_right_in_dots = ConvertUnitDouble(
205 custom_margin_right_in_points, printing::kPointsPerInch, dpi);
206 double custom_margin_bottom_in_dots = ConvertUnitDouble(
207 custom_margin_bottom_in_points, printing::kPointsPerInch, dpi);
208
209
210 if (custom_margin_left_in_dots < 0 || custom_margin_right_in_dots < 0 ||
211 custom_margin_top_in_dots < 0 || custom_margin_bottom_in_dots < 0) {
212 NOTREACHED();
213 return;
214 }
215
216 if (settings->params.page_size.width() < custom_margin_left_in_dots +
217 custom_margin_right_in_dots ||
218 settings->params.page_size.height() < custom_margin_top_in_dots +
219 custom_margin_bottom_in_dots) {
220 NOTREACHED();
221 return;
222 }
223
224 settings->params.margin_top = custom_margin_top_in_dots;
225 settings->params.margin_left = custom_margin_left_in_dots;
226 settings->params.printable_size.set_width(
227 settings->params.page_size.width() - custom_margin_right_in_dots -
228 custom_margin_left_in_dots);
229 settings->params.printable_size.set_height(
230 settings->params.page_size.height() - custom_margin_bottom_in_dots -
231 custom_margin_top_in_dots);
232 } 166 }
233 167
234 // Get the (x, y) coordinate from where printing of the current text should 168 // Get the (x, y) coordinate from where printing of the current text should
235 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and 169 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
236 // vertical alignment (TOP, BOTTOM). 170 // vertical alignment (TOP, BOTTOM).
237 SkPoint GetHeaderFooterPosition( 171 SkPoint GetHeaderFooterPosition(
238 float webkit_scale_factor, 172 float webkit_scale_factor,
239 const PageSizeMargins& page_layout, 173 const PageSizeMargins& page_layout,
240 printing::HorizontalHeaderFooterPosition horizontal_position, 174 printing::HorizontalHeaderFooterPosition horizontal_position,
241 printing::VerticalHeaderFooterPosition vertical_position, 175 printing::VerticalHeaderFooterPosition vertical_position,
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 509
576 WebDocument document = main_frame->document(); 510 WebDocument document = main_frame->document();
577 // <object> with id="pdf-viewer" is created in 511 // <object> with id="pdf-viewer" is created in
578 // chrome/browser/resources/print_preview/print_preview.js 512 // chrome/browser/resources/print_preview/print_preview.js
579 WebElement pdf_element = document.getElementById("pdf-viewer"); 513 WebElement pdf_element = document.getElementById("pdf-viewer");
580 if (pdf_element.isNull()) { 514 if (pdf_element.isNull()) {
581 NOTREACHED(); 515 NOTREACHED();
582 return; 516 return;
583 } 517 }
584 518
585 if (!UpdatePrintSettings(job_settings, false)) { 519 WebFrame* pdf_frame = pdf_element.document().frame();
520 if (!UpdatePrintSettings(pdf_frame, pdf_element, job_settings, false)) {
586 LOG(ERROR) << "UpdatePrintSettings failed"; 521 LOG(ERROR) << "UpdatePrintSettings failed";
587 DidFinishPrinting(FAIL_PRINT); 522 DidFinishPrinting(FAIL_PRINT);
588 return; 523 return;
589 } 524 }
590 525
591 WebFrame* pdf_frame = pdf_element.document().frame();
592 scoped_ptr<PrepareFrameAndViewForPrint> prepare; 526 scoped_ptr<PrepareFrameAndViewForPrint> prepare;
593 prepare.reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 527 prepare.reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
594 pdf_frame, pdf_element)); 528 pdf_frame, pdf_element));
595 UpdatePrintableSizeInPrintParameters(pdf_frame, pdf_element, prepare.get(), 529 UpdatePrintableSizeInPrintParameters(pdf_frame, pdf_element, prepare.get(),
596 &print_pages_params_->params); 530 &print_pages_params_->params);
597 531
598 // Render Pages for printing. 532 // Render Pages for printing.
599 if (!RenderPagesForPrint(pdf_frame, pdf_element, prepare.get())) { 533 if (!RenderPagesForPrint(pdf_frame, pdf_element, prepare.get())) {
600 LOG(ERROR) << "RenderPagesForPrint failed"; 534 LOG(ERROR) << "RenderPagesForPrint failed";
601 DidFinishPrinting(FAIL_PRINT); 535 DidFinishPrinting(FAIL_PRINT);
(...skipping 27 matching lines...) Expand all
629 return; 563 return;
630 } 564 }
631 565
632 Print(frame, print_preview_context_.node()); 566 Print(frame, print_preview_context_.node());
633 } 567 }
634 568
635 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 569 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
636 DCHECK(is_preview_); 570 DCHECK(is_preview_);
637 print_preview_context_.OnPrintPreview(); 571 print_preview_context_.OnPrintPreview();
638 572
639 if (!UpdatePrintSettings(settings, true)) { 573 if (!UpdatePrintSettings(print_preview_context_.frame(),
574 print_preview_context_.node(), settings, true)) {
640 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { 575 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
641 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( 576 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
642 routing_id(), print_pages_params_->params.document_cookie)); 577 routing_id(), print_pages_params_->params.document_cookie));
643 notify_browser_of_print_failure_ = false; // Already sent. 578 notify_browser_of_print_failure_ = false; // Already sent.
644 } 579 }
645 DidFinishPrinting(FAIL_PREVIEW); 580 DidFinishPrinting(FAIL_PREVIEW);
646 return; 581 return;
647 } 582 }
648 583
649 if (!print_pages_params_->params.is_first_request && 584 if (!print_pages_params_->params.is_first_request &&
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1037 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1103 frame, node)); 1038 frame, node));
1104 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), 1039 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(),
1105 &print_pages_params_->params); 1040 &print_pages_params_->params);
1106 Send(new PrintHostMsg_DidGetDocumentCookie( 1041 Send(new PrintHostMsg_DidGetDocumentCookie(
1107 routing_id(), print_pages_params_->params.document_cookie)); 1042 routing_id(), print_pages_params_->params.document_cookie));
1108 return true; 1043 return true;
1109 } 1044 }
1110 1045
1111 bool PrintWebViewHelper::UpdatePrintSettings( 1046 bool PrintWebViewHelper::UpdatePrintSettings(
1112 const DictionaryValue& job_settings, bool generating_preview) { 1047 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1113 if (job_settings.empty()) { 1048 const DictionaryValue& passed_job_settings, bool generating_preview) {
1049 DCHECK(is_preview_);
1050 const DictionaryValue* job_settings = &passed_job_settings;
1051 DictionaryValue modified_job_settings;
1052 if (job_settings->empty()) {
1114 if (generating_preview) 1053 if (generating_preview)
1115 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1054 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1116 return false; 1055 return false;
1117 } 1056 }
1118 1057
1058 bool is_pdf = PrintingNodeOrPdfFrame(frame, node);
1059 if (is_pdf || !generating_preview) {
1060 modified_job_settings.MergeDictionary(job_settings);
1061 SetMarginsForPdf(&modified_job_settings, !generating_preview);
1062 if (is_pdf) {
1063 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled,
1064 false);
1065 }
1066 job_settings = &modified_job_settings;
1067 }
1068
1119 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when 1069 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
1120 // possible. 1070 // possible.
1121 int cookie = print_pages_params_.get() ? 1071 int cookie = print_pages_params_.get() ?
1122 print_pages_params_->params.document_cookie : 0; 1072 print_pages_params_->params.document_cookie : 0;
1123 PrintMsg_PrintPages_Params settings; 1073 PrintMsg_PrintPages_Params settings;
1124 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), 1074 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
1125 cookie, job_settings, &settings)); 1075 cookie, *job_settings, &settings));
1126 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1076 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1127 1077
1128 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { 1078 if (PrintMsg_Print_Params_IsEmpty(settings.params)) {
1129 if (generating_preview) { 1079 if (generating_preview) {
1130 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); 1080 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
1131 } else { 1081 } else {
1132 WebKit::WebFrame* frame = print_preview_context_.frame(); 1082 WebKit::WebFrame* frame = print_preview_context_.frame();
1133 if (!frame) { 1083 if (!frame) {
1134 GetPrintFrame(&frame); 1084 GetPrintFrame(&frame);
1135 } 1085 }
1136 if (frame) { 1086 if (frame) {
1137 render_view()->RunModalAlertDialog( 1087 render_view()->RunModalAlertDialog(
1138 frame, 1088 frame,
1139 l10n_util::GetStringUTF16( 1089 l10n_util::GetStringUTF16(
1140 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); 1090 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1141 } 1091 }
1142 } 1092 }
1143 return false; 1093 return false;
1144 } 1094 }
1145 1095
1146 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) { 1096 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) {
1147 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS); 1097 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS);
1148 return false; 1098 return false;
1149 } 1099 }
1150 1100
1151 if (generating_preview) { 1101 if (generating_preview) {
1152 // Validate expected print preview settings. 1102 // Validate expected print preview settings.
1153 if (!job_settings.GetString(printing::kPreviewUIAddr, 1103 if (!job_settings->GetString(printing::kPreviewUIAddr,
1154 &(settings.params.preview_ui_addr)) || 1104 &(settings.params.preview_ui_addr)) ||
1155 !job_settings.GetInteger(printing::kPreviewRequestID, 1105 !job_settings->GetInteger(printing::kPreviewRequestID,
1156 &(settings.params.preview_request_id)) || 1106 &(settings.params.preview_request_id)) ||
1157 !job_settings.GetBoolean(printing::kIsFirstRequest, 1107 !job_settings->GetBoolean(printing::kIsFirstRequest,
1158 &(settings.params.is_first_request))) { 1108 &(settings.params.is_first_request))) {
1159 NOTREACHED(); 1109 NOTREACHED();
1160 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1110 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1161 return false; 1111 return false;
1162 } 1112 }
1163 1113
1164 if (settings.params.is_first_request &&
1165 !print_preview_context_.IsModifiable()) {
1166 settings.params.display_header_footer = false;
1167 }
1168
1169 // Margins: Send default page layout to browser process. 1114 // Margins: Send default page layout to browser process.
1170 PageSizeMargins default_page_layout; 1115 PageSizeMargins default_page_layout;
1171 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, 1116 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params,
1172 &default_page_layout); 1117 &default_page_layout);
1173 if (!old_print_pages_params_.get() || 1118 if (!old_print_pages_params_.get() ||
1174 !PageLayoutIsEqual(*old_print_pages_params_, settings)) { 1119 !PageLayoutIsEqual(*old_print_pages_params_, settings)) {
1175 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(), 1120 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(),
1176 default_page_layout)); 1121 default_page_layout));
1177 } 1122 }
1178 SetCustomMarginsIfSelected(job_settings, &settings);
1179 1123
1180 // Header/Footer: Set |header_footer_info_|. 1124 // Header/Footer: Set |header_footer_info_|.
1181 if (settings.params.display_header_footer) { 1125 if (settings.params.display_header_footer) {
1182 header_footer_info_.reset(new DictionaryValue()); 1126 header_footer_info_.reset(new DictionaryValue());
1183 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, 1127 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
1184 settings.params.date); 1128 settings.params.date);
1185 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, 1129 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
1186 settings.params.url); 1130 settings.params.url);
1187 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, 1131 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
1188 settings.params.title); 1132 settings.params.title);
1189 } 1133 }
1190 } 1134 }
1191 1135
1192 if ((is_preview_ && !generating_preview) ||
1193 PrintingNodeOrPdfFrame(print_preview_context_.frame(),
1194 print_preview_context_.node())) {
1195 SetMarginsForPDF(&settings.params);
1196 }
1197
1198 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1136 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1199 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), 1137 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
1200 settings.params.document_cookie)); 1138 settings.params.document_cookie));
1201 return true; 1139 return true;
1202 } 1140 }
1203 1141
1204 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, 1142 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame,
1205 int expected_pages_count, 1143 int expected_pages_count,
1206 bool use_browser_overlays) { 1144 bool use_browser_overlays) {
1207 PrintHostMsg_ScriptedPrint_Params params; 1145 PrintHostMsg_ScriptedPrint_Params params;
1208 PrintMsg_PrintPages_Params print_settings; 1146 PrintMsg_PrintPages_Params print_settings;
1209 1147
1210 // The routing id is sent across as it is needed to look up the 1148 // The routing id is sent across as it is needed to look up the
1211 // corresponding RenderViewHost instance to signal and reset the 1149 // corresponding RenderViewHost instance to signal and reset the
1212 // pump messages event. 1150 // pump messages event.
1213 params.routing_id = render_view()->GetRoutingId(); 1151 params.routing_id = render_view()->GetRoutingId();
1214 // host_window_ may be NULL at this point if the current window is a 1152 // host_window_ may be NULL at this point if the current window is a
1215 // popup and the print() command has been issued from the parent. The 1153 // popup and the print() command has been issued from the parent. The
1216 // receiver of this message has to deal with this. 1154 // receiver of this message has to deal with this.
1217 params.host_window_id = render_view()->GetHostWindow(); 1155 params.host_window_id = render_view()->GetHostWindow();
1218 params.cookie = print_pages_params_->params.document_cookie; 1156 params.cookie = print_pages_params_->params.document_cookie;
1219 params.has_selection = frame->hasSelection(); 1157 params.has_selection = frame->hasSelection();
1220 params.expected_pages_count = expected_pages_count; 1158 params.expected_pages_count = expected_pages_count;
1221 params.use_overlays = use_browser_overlays; 1159 params.margin_type = printing::DEFAULT_MARGINS;
1222 1160
1223 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); 1161 Send(new PrintHostMsg_DidShowPrintDialog(routing_id()));
1224 1162
1225 print_pages_params_.reset(); 1163 print_pages_params_.reset();
1226 IPC::SyncMessage* msg = 1164 IPC::SyncMessage* msg =
1227 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); 1165 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings);
1228 msg->EnableMessagePumping(); 1166 msg->EnableMessagePumping();
1229 Send(msg); 1167 Send(msg);
1230 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); 1168 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings));
1231 return (print_settings.params.dpi && print_settings.params.document_cookie); 1169 return (print_settings.params.dpi && print_settings.params.document_cookie);
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
1587 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { 1525 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1588 return prep_frame_view_->GetPrintCanvasSize(); 1526 return prep_frame_view_->GetPrintCanvasSize();
1589 } 1527 }
1590 1528
1591 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1529 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1592 prep_frame_view_.reset(); 1530 prep_frame_view_.reset();
1593 metafile_.reset(); 1531 metafile_.reset();
1594 pages_to_render_.clear(); 1532 pages_to_render_.clear();
1595 error_ = PREVIEW_ERROR_NONE; 1533 error_ = PREVIEW_ERROR_NONE;
1596 } 1534 }
OLDNEW
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | printing/page_setup.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698