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

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

Issue 8173016: Patch up excessive margins until we can get the real fix plumbed through. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix headers and footers 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
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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, 136 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params,
137 gfx::Size* result) { 137 gfx::Size* result) {
138 int dpi = GetDPI(&print_params); 138 int dpi = GetDPI(&print_params);
139 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, 139 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi,
140 print_params.desired_dpi)); 140 print_params.desired_dpi));
141 141
142 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, 142 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi,
143 print_params.desired_dpi)); 143 print_params.desired_dpi));
144 } 144 }
145 145
146 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) {
147 if (!node.isNull())
148 return true;
149 std::string mime(frame->dataSource()->response().mimeType().utf8());
150 return mime == "application/pdf";
151 }
152
153 void SetMarginsForPDF(PrintMsg_Print_Params* settings) {
154 // This is the wrong way to do this. But the pipeline for the right way is
155 // too long. This will be removed soon. http://crbug.com/92000
156 settings->margin_top = 0;
157 settings->margin_left = 0;
158 settings->printable_size.set_width(settings->page_size.width());
159 settings->printable_size.set_height(settings->page_size.height());
160 }
161
146 // Get the margins option selected and set custom margins appropriately. 162 // Get the margins option selected and set custom margins appropriately.
147 void SetCustomMarginsIfSelected(const DictionaryValue& job_settings, 163 void SetCustomMarginsIfSelected(const DictionaryValue& job_settings,
148 PrintMsg_PrintPages_Params* settings) { 164 PrintMsg_PrintPages_Params* settings) {
149 bool default_margins_selected; 165 bool default_margins_selected;
150 if (!job_settings.GetBoolean(printing::kSettingDefaultMarginsSelected, 166 if (!job_settings.GetBoolean(printing::kSettingDefaultMarginsSelected,
151 &default_margins_selected)) { 167 &default_margins_selected)) {
152 NOTREACHED(); 168 NOTREACHED();
153 default_margins_selected = true; 169 default_margins_selected = true;
154 } 170 }
155 171
(...skipping 845 matching lines...) Expand 10 before | Expand all | Expand 10 after
1001 page_layout_in_points->margin_left = 1017 page_layout_in_points->margin_left =
1002 ConvertPixelsToPointDouble(margin_left_in_pixels); 1018 ConvertPixelsToPointDouble(margin_left_in_pixels);
1003 } 1019 }
1004 1020
1005 // static - Not anonymous so that platform implementations can use it. 1021 // static - Not anonymous so that platform implementations can use it.
1006 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( 1022 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters(
1007 WebFrame* frame, 1023 WebFrame* frame,
1008 const WebNode& node, 1024 const WebNode& node,
1009 PrepareFrameAndViewForPrint* prepare, 1025 PrepareFrameAndViewForPrint* prepare,
1010 PrintMsg_Print_Params* params) { 1026 PrintMsg_Print_Params* params) {
1027 if (PrintingNodeOrPdfFrame(frame, node))
1028 return;
1011 PageSizeMargins page_layout_in_points; 1029 PageSizeMargins page_layout_in_points;
1012 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, 1030 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params,
1013 &page_layout_in_points); 1031 &page_layout_in_points);
1014 int dpi = GetDPI(params); 1032 int dpi = GetDPI(params);
1015 params->printable_size = gfx::Size( 1033 params->printable_size = gfx::Size(
1016 static_cast<int>(ConvertUnitDouble( 1034 static_cast<int>(ConvertUnitDouble(
1017 page_layout_in_points.content_width, 1035 page_layout_in_points.content_width,
1018 printing::kPointsPerInch, dpi)), 1036 printing::kPointsPerInch, dpi)),
1019 static_cast<int>(ConvertUnitDouble( 1037 static_cast<int>(ConvertUnitDouble(
1020 page_layout_in_points.content_height, 1038 page_layout_in_points.content_height,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1084 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1102 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1085 frame, node)); 1103 frame, node));
1086 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), 1104 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(),
1087 &print_pages_params_->params); 1105 &print_pages_params_->params);
1088 Send(new PrintHostMsg_DidGetDocumentCookie( 1106 Send(new PrintHostMsg_DidGetDocumentCookie(
1089 routing_id(), print_pages_params_->params.document_cookie)); 1107 routing_id(), print_pages_params_->params.document_cookie));
1090 return true; 1108 return true;
1091 } 1109 }
1092 1110
1093 bool PrintWebViewHelper::UpdatePrintSettings( 1111 bool PrintWebViewHelper::UpdatePrintSettings(
1094 const DictionaryValue& job_settings, bool is_preview) { 1112 const DictionaryValue& job_settings, bool generating_preview) {
1095 if (job_settings.empty()) { 1113 if (job_settings.empty()) {
1096 if (is_preview) 1114 if (generating_preview)
1097 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1115 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1098 return false; 1116 return false;
1099 } 1117 }
1100 1118
1101 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when 1119 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
1102 // possible. 1120 // possible.
1103 int cookie = print_pages_params_.get() ? 1121 int cookie = print_pages_params_.get() ?
1104 print_pages_params_->params.document_cookie : 0; 1122 print_pages_params_->params.document_cookie : 0;
1105 PrintMsg_PrintPages_Params settings; 1123 PrintMsg_PrintPages_Params settings;
1106 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), 1124 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
1107 cookie, job_settings, &settings)); 1125 cookie, job_settings, &settings));
1108 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1126 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1109 1127
1110 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { 1128 if (PrintMsg_Print_Params_IsEmpty(settings.params)) {
1111 if (is_preview) { 1129 if (generating_preview) {
1112 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); 1130 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
1113 } else { 1131 } else {
1114 WebKit::WebFrame* frame = print_preview_context_.frame(); 1132 WebKit::WebFrame* frame = print_preview_context_.frame();
1115 if (!frame) { 1133 if (!frame) {
1116 GetPrintFrame(&frame); 1134 GetPrintFrame(&frame);
1117 } 1135 }
1118 if (frame) { 1136 if (frame) {
1119 render_view()->runModalAlertDialog( 1137 render_view()->runModalAlertDialog(
1120 frame, 1138 frame,
1121 l10n_util::GetStringUTF16( 1139 l10n_util::GetStringUTF16(
1122 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); 1140 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1123 } 1141 }
1124 } 1142 }
1125 return false; 1143 return false;
1126 } 1144 }
1127 1145
1128 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) { 1146 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) {
1129 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS); 1147 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS);
1130 return false; 1148 return false;
1131 } 1149 }
1132 1150
1133 if (is_preview) { 1151 if (generating_preview) {
1134 // Validate expected print preview settings. 1152 // Validate expected print preview settings.
1135 if (!job_settings.GetString(printing::kPreviewUIAddr, 1153 if (!job_settings.GetString(printing::kPreviewUIAddr,
1136 &(settings.params.preview_ui_addr)) || 1154 &(settings.params.preview_ui_addr)) ||
1137 !job_settings.GetInteger(printing::kPreviewRequestID, 1155 !job_settings.GetInteger(printing::kPreviewRequestID,
1138 &(settings.params.preview_request_id)) || 1156 &(settings.params.preview_request_id)) ||
1139 !job_settings.GetBoolean(printing::kIsFirstRequest, 1157 !job_settings.GetBoolean(printing::kIsFirstRequest,
1140 &(settings.params.is_first_request))) { 1158 &(settings.params.is_first_request))) {
1141 NOTREACHED(); 1159 NOTREACHED();
1142 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1160 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1143 return false; 1161 return false;
(...skipping 20 matching lines...) Expand all
1164 header_footer_info_.reset(new DictionaryValue()); 1182 header_footer_info_.reset(new DictionaryValue());
1165 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, 1183 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
1166 settings.params.date); 1184 settings.params.date);
1167 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, 1185 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
1168 settings.params.url); 1186 settings.params.url);
1169 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, 1187 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
1170 settings.params.title); 1188 settings.params.title);
1171 } 1189 }
1172 } 1190 }
1173 1191
1192 if ((is_preview_ && !generating_preview) ||
1193 PrintingNodeOrPdfFrame(print_preview_context_.frame(),
1194 print_preview_context_.node())) {
1195 SetMarginsForPDF(&settings.params);
1196 }
1197
1174 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1198 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1175 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), 1199 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
1176 settings.params.document_cookie)); 1200 settings.params.document_cookie));
1177 return true; 1201 return true;
1178 } 1202 }
1179 1203
1180 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, 1204 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame,
1181 int expected_pages_count, 1205 int expected_pages_count,
1182 bool use_browser_overlays) { 1206 bool use_browser_overlays) {
1183 PrintHostMsg_ScriptedPrint_Params params; 1207 PrintHostMsg_ScriptedPrint_Params params;
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
1498 if (IsFinalPageRendered()) 1522 if (IsFinalPageRendered())
1499 return -1; 1523 return -1;
1500 return pages_to_render_[current_page_index_++]; 1524 return pages_to_render_[current_page_index_++];
1501 } 1525 }
1502 1526
1503 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { 1527 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const {
1504 return state_ != UNINITIALIZED; 1528 return state_ != UNINITIALIZED;
1505 } 1529 }
1506 1530
1507 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { 1531 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const {
1508 // TODO(vandebo) I think this should only return false if the content is a 1532 // The only kind of node we can print right now is a PDF node.
1509 // PDF, just because we are printing a particular node does not mean it's 1533 return !PrintingNodeOrPdfFrame(frame(), node());
1510 // a PDF (right?), we should check the mime type of the node.
1511 if (!node().isNull())
1512 return false;
1513 std::string mime(frame()->dataSource()->response().mimeType().utf8());
1514 return mime != "application/pdf";
1515 } 1534 }
1516 1535
1517 bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile() 1536 bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile()
1518 const { 1537 const {
1519 return current_page_index_ == print_ready_metafile_page_count_; 1538 return current_page_index_ == print_ready_metafile_page_count_;
1520 } 1539 }
1521 1540
1522 bool PrintWebViewHelper::PrintPreviewContext::IsFinalPageRendered() const { 1541 bool PrintWebViewHelper::PrintPreviewContext::IsFinalPageRendered() const {
1523 return static_cast<size_t>(current_page_index_) == pages_to_render_.size(); 1542 return static_cast<size_t>(current_page_index_) == pages_to_render_.size();
1524 } 1543 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1568 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { 1587 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1569 return prep_frame_view_->GetPrintCanvasSize(); 1588 return prep_frame_view_->GetPrintCanvasSize();
1570 } 1589 }
1571 1590
1572 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1591 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1573 prep_frame_view_.reset(); 1592 prep_frame_view_.reset();
1574 metafile_.reset(); 1593 metafile_.reset();
1575 pages_to_render_.clear(); 1594 pages_to_render_.clear();
1576 error_ = PREVIEW_ERROR_NONE; 1595 error_ = PREVIEW_ERROR_NONE;
1577 } 1596 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698