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

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

Issue 8585017: PrintPreview: Honor the print media page size and margin values. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix win and mac issues Created 8 years, 11 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 return !params.document_cookie && !params.desired_dpi && !params.max_shrink && 102 return !params.document_cookie && !params.desired_dpi && !params.max_shrink &&
103 !params.min_shrink && !params.dpi && params.content_size.IsEmpty() && 103 !params.min_shrink && !params.dpi && params.content_size.IsEmpty() &&
104 !params.selection_only && params.page_size.IsEmpty() && 104 !params.selection_only && params.page_size.IsEmpty() &&
105 !params.margin_top && !params.margin_left && 105 !params.margin_top && !params.margin_left &&
106 !params.supports_alpha_blend; 106 !params.supports_alpha_blend;
107 } 107 }
108 108
109 bool PageLayoutIsEqual(const PrintMsg_PrintPages_Params& oldParams, 109 bool PageLayoutIsEqual(const PrintMsg_PrintPages_Params& oldParams,
110 const PrintMsg_PrintPages_Params& newParams) { 110 const PrintMsg_PrintPages_Params& newParams) {
111 return oldParams.params.content_size == newParams.params.content_size && 111 return oldParams.params.content_size == newParams.params.content_size &&
112 oldParams.params.printable_area == newParams.params.printable_area &&
112 oldParams.params.page_size == newParams.params.page_size && 113 oldParams.params.page_size == newParams.params.page_size &&
113 oldParams.params.margin_top == newParams.params.margin_top && 114 oldParams.params.margin_top == newParams.params.margin_top &&
114 oldParams.params.margin_left == newParams.params.margin_left && 115 oldParams.params.margin_left == newParams.params.margin_left &&
115 oldParams.params.desired_dpi == newParams.params.desired_dpi && 116 oldParams.params.desired_dpi == newParams.params.desired_dpi &&
116 oldParams.params.dpi == newParams.params.dpi; 117 oldParams.params.dpi == newParams.params.dpi;
117 } 118 }
118 119
119 bool PrintMsg_Print_Params_IsEqual( 120 bool PrintMsg_Print_Params_IsEqual(
120 const PrintMsg_PrintPages_Params& oldParams, 121 const PrintMsg_PrintPages_Params& oldParams,
121 const PrintMsg_PrintPages_Params& newParams) { 122 const PrintMsg_PrintPages_Params& newParams) {
122 return PageLayoutIsEqual(oldParams, newParams) && 123 return PageLayoutIsEqual(oldParams, newParams) &&
123 oldParams.params.max_shrink == newParams.params.max_shrink && 124 oldParams.params.max_shrink == newParams.params.max_shrink &&
124 oldParams.params.min_shrink == newParams.params.min_shrink && 125 oldParams.params.min_shrink == newParams.params.min_shrink &&
125 oldParams.params.selection_only == newParams.params.selection_only && 126 oldParams.params.selection_only == newParams.params.selection_only &&
126 oldParams.params.supports_alpha_blend == 127 oldParams.params.supports_alpha_blend ==
127 newParams.params.supports_alpha_blend && 128 newParams.params.supports_alpha_blend &&
128 oldParams.pages.size() == newParams.pages.size() && 129 oldParams.pages.size() == newParams.pages.size() &&
130 oldParams.params.print_to_pdf == newParams.params.print_to_pdf &&
129 oldParams.params.display_header_footer == 131 oldParams.params.display_header_footer ==
130 newParams.params.display_header_footer && 132 newParams.params.display_header_footer &&
131 oldParams.params.date == newParams.params.date && 133 oldParams.params.date == newParams.params.date &&
132 oldParams.params.title == newParams.params.title && 134 oldParams.params.title == newParams.params.title &&
133 oldParams.params.url == newParams.params.url && 135 oldParams.params.url == newParams.params.url &&
134 std::equal(oldParams.pages.begin(), oldParams.pages.end(), 136 std::equal(oldParams.pages.begin(), oldParams.pages.end(),
135 newParams.pages.begin()); 137 newParams.pages.begin());
136 } 138 }
137 139
140 PrintMsg_Print_Params GetCssPrintParams(
141 WebFrame* frame,
142 int page_index,
143 const PrintMsg_Print_Params& default_params) {
144 PrintMsg_Print_Params page_css_params = default_params;
145 int dpi = GetDPI(&default_params);
146 WebSize page_size_in_pixels(
147 ConvertUnit(default_params.page_size.width(),
148 dpi, printing::kPixelsPerInch),
149 ConvertUnit(default_params.page_size.height(),
150 dpi, printing::kPixelsPerInch));
151 int margin_top_in_pixels = ConvertUnit(
152 default_params.margin_top,
153 dpi, printing::kPixelsPerInch);
154 int margin_right_in_pixels = ConvertUnit(
155 default_params.page_size.width() -
156 default_params.content_size.width() - default_params.margin_left,
157 dpi, printing::kPixelsPerInch);
158 int margin_bottom_in_pixels = ConvertUnit(
159 default_params.page_size.height() -
160 default_params.content_size.height() - default_params.margin_top,
161 dpi, printing::kPixelsPerInch);
162 int margin_left_in_pixels = ConvertUnit(
163 default_params.margin_left,
164 dpi, printing::kPixelsPerInch);
165
166 if (frame) {
167 frame->pageSizeAndMarginsInPixels(page_index,
168 page_size_in_pixels,
169 margin_top_in_pixels,
170 margin_right_in_pixels,
171 margin_bottom_in_pixels,
172 margin_left_in_pixels);
173 }
174
175 int new_content_width = page_size_in_pixels.width -
176 margin_left_in_pixels - margin_right_in_pixels;
177 int new_content_height = page_size_in_pixels.height -
178 margin_top_in_pixels - margin_bottom_in_pixels;
179
180 // Invalid page size and/or margins. We just use the default setting.
181 if (new_content_width < 1 || new_content_height < 1) {
182 CHECK(frame != NULL);
183 page_css_params = GetCssPrintParams(NULL, page_index, default_params);
184 return page_css_params;
185 }
186
187 page_css_params.content_size = gfx::Size(
188 static_cast<int>(ConvertUnit(new_content_width,
189 printing::kPixelsPerInch, dpi)),
190 static_cast<int>(ConvertUnit(new_content_height,
191 printing::kPixelsPerInch, dpi)));
192
193 page_css_params.page_size = gfx::Size(
194 static_cast<int>(ConvertUnit(page_size_in_pixels.width,
195 printing::kPixelsPerInch, dpi)),
196 static_cast<int>(ConvertUnit(page_size_in_pixels.height,
197 printing::kPixelsPerInch, dpi)));
198
199 page_css_params.margin_top =
200 static_cast<int>(ConvertUnit(margin_top_in_pixels,
201 printing::kPixelsPerInch, dpi));
202
203 page_css_params.margin_left =
204 static_cast<int>(ConvertUnit(margin_left_in_pixels,
205 printing::kPixelsPerInch, dpi));
206 return page_css_params;
207 }
208
209 double FitPrintParamsToPage(const PrintMsg_Print_Params& default_params,
210 PrintMsg_Print_Params* page_css_params) {
vandebo (ex-Chrome) 2012/01/07 00:23:19 nit: default_params -> page_params ? nit: page_css
kmadhusu 2012/01/09 17:15:55 Done.
211 double content_width =
212 static_cast<double>(page_css_params->content_size.width());
213 double content_height =
214 static_cast<double>(page_css_params->content_size.height());
215 int default_page_size_height = default_params.page_size.height();
216 int default_page_size_width = default_params.page_size.width();
217 int css_page_size_height = page_css_params->page_size.height();
218 int css_page_size_width = page_css_params->page_size.width();
219
220 double scale_factor = 1.0f;
221 if (default_params.page_size == page_css_params->page_size)
222 return scale_factor;
223
224 if (default_page_size_width < css_page_size_width ||
225 default_page_size_height < css_page_size_height) {
226 double ratio_width =
227 static_cast<double>(default_params.printable_area.width()) /
228 css_page_size_width;
229 double ratio_height =
230 static_cast<double>(default_params.printable_area.height()) /
231 css_page_size_height;
232 scale_factor = ratio_width < ratio_height ? ratio_width : ratio_height;
233 content_width *= scale_factor;
234 content_height *= scale_factor;
235 }
236 page_css_params->margin_top = static_cast<int>(
237 (default_page_size_height - css_page_size_height * scale_factor) / 2 +
238 (page_css_params->margin_top * scale_factor));
239 page_css_params->margin_left = static_cast<int>(
240 (default_page_size_width - css_page_size_width * scale_factor) / 2 +
241 (page_css_params->margin_left * scale_factor));
242 page_css_params->content_size = gfx::Size(
243 static_cast<int>(content_width), static_cast<int>(content_height));
244 page_css_params->page_size = default_params.page_size;
245 return scale_factor;
246 }
247
248 void CalculatePageLayoutFromPrintParams(
249 const PrintMsg_Print_Params& page_css_params,
250 PageSizeMargins* page_layout_in_points) {
251 int dpi = GetDPI(&page_css_params);
252 int content_width = page_css_params.content_size.width();
253 int content_height = page_css_params.content_size.height();
254
255 int margin_bottom = page_css_params.page_size.height() -
256 content_height - page_css_params.margin_top;
257 int margin_right = page_css_params.page_size.width() -
258 content_width - page_css_params.margin_left;
259
260 page_layout_in_points->content_width = ConvertUnit(
261 content_width, dpi, printing::kPointsPerInch);
262 page_layout_in_points->content_height = ConvertUnit(
263 content_height, dpi, printing::kPointsPerInch);
264 page_layout_in_points->margin_top = ConvertUnit(
265 page_css_params.margin_top, dpi, printing::kPointsPerInch);
266 page_layout_in_points->margin_right = ConvertUnit(
267 margin_right, dpi, printing::kPointsPerInch);
268 page_layout_in_points->margin_bottom = ConvertUnit(
269 margin_bottom, dpi, printing::kPointsPerInch);
270 page_layout_in_points->margin_left = ConvertUnit(
271 page_css_params.margin_left, dpi, printing::kPointsPerInch);
272 }
273
138 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, 274 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params,
139 gfx::Size* result) { 275 gfx::Size* result) {
140 int dpi = GetDPI(&print_params); 276 int dpi = GetDPI(&print_params);
141 result->set_width(ConvertUnit(print_params.content_size.width(), dpi, 277 result->set_width(ConvertUnit(print_params.content_size.width(), dpi,
142 print_params.desired_dpi)); 278 print_params.desired_dpi));
143 279
144 result->set_height(ConvertUnit(print_params.content_size.height(), dpi, 280 result->set_height(ConvertUnit(print_params.content_size.height(), dpi,
145 print_params.desired_dpi)); 281 print_params.desired_dpi));
146 } 282 }
147 283
148 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { 284 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) {
149 if (!node.isNull()) 285 if (!node.isNull())
150 return true; 286 return true;
151 std::string mime(frame->dataSource()->response().mimeType().utf8()); 287 std::string mime(frame->dataSource()->response().mimeType().utf8());
152 return mime == "application/pdf"; 288 return mime == "application/pdf";
153 } 289 }
154 290
291 bool PrintingFrameHasPageSizeStyle(WebFrame* frame, int total_page_count) {
292 if (!frame)
293 return false;
294 bool frame_has_custom_page_size_style = false;
295 for (int i = 0; i < total_page_count; ++i) {
296 if (frame->hasCustomPageSizeStyle(i)) {
297 frame_has_custom_page_size_style = true;
298 break;
299 }
300 }
301 return frame_has_custom_page_size_style;
302 }
303
155 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { 304 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) {
156 if (frame->isPrintScalingDisabledForPlugin(node)) 305 if (frame->isPrintScalingDisabledForPlugin(node))
157 return printing::NO_MARGINS; 306 return printing::NO_MARGINS;
158 else 307 else
159 return printing::PRINTABLE_AREA_MARGINS; 308 return printing::PRINTABLE_AREA_MARGINS;
160 } 309 }
161 310
162 // Get the (x, y) coordinate from where printing of the current text should 311 // Get the (x, y) coordinate from where printing of the current text should
163 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and 312 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
164 // vertical alignment (TOP, BOTTOM). 313 // vertical alignment (TOP, BOTTOM).
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 web_frame->setScrollOffset(prev_scroll_offset_); 582 web_frame->setScrollOffset(prev_scroll_offset_);
434 } 583 }
435 } 584 }
436 585
437 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) 586 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
438 : content::RenderViewObserver(render_view), 587 : content::RenderViewObserver(render_view),
439 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), 588 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
440 print_web_view_(NULL), 589 print_web_view_(NULL),
441 is_preview_enabled_(switches::IsPrintPreviewEnabled()), 590 is_preview_enabled_(switches::IsPrintPreviewEnabled()),
442 is_print_ready_metafile_sent_(false), 591 is_print_ready_metafile_sent_(false),
592 ignore_css_margins_(false),
593 fit_to_page_(true),
443 user_cancelled_scripted_print_count_(0), 594 user_cancelled_scripted_print_count_(0),
444 notify_browser_of_print_failure_(true) { 595 notify_browser_of_print_failure_(true) {
445 } 596 }
446 597
447 PrintWebViewHelper::~PrintWebViewHelper() {} 598 PrintWebViewHelper::~PrintWebViewHelper() {}
448 599
449 // Prints |frame| which called window.print(). 600 // Prints |frame| which called window.print().
450 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { 601 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) {
451 DCHECK(frame); 602 DCHECK(frame);
452 603
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 void PrintWebViewHelper::OnPrintForSystemDialog() { 704 void PrintWebViewHelper::OnPrintForSystemDialog() {
554 WebFrame* frame = print_preview_context_.frame(); 705 WebFrame* frame = print_preview_context_.frame();
555 if (!frame) { 706 if (!frame) {
556 NOTREACHED(); 707 NOTREACHED();
557 return; 708 return;
558 } 709 }
559 710
560 Print(frame, print_preview_context_.node()); 711 Print(frame, print_preview_context_.node());
561 } 712 }
562 713
714 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout(
715 const printing::PageSizeMargins& page_layout_in_points,
716 gfx::Size* page_size,
717 gfx::Rect* content_area) {
718 *page_size = gfx::Size(
719 page_layout_in_points.content_width +
720 page_layout_in_points.margin_right +
721 page_layout_in_points.margin_left,
722 page_layout_in_points.content_height +
723 page_layout_in_points.margin_top +
724 page_layout_in_points.margin_bottom);
725 *content_area = gfx::Rect(page_layout_in_points.margin_left,
726 page_layout_in_points.margin_top,
727 page_layout_in_points.content_width,
728 page_layout_in_points.content_height);
729 }
730
731 void PrintWebViewHelper::UpdateFrameMarginsCssInfo(
732 const DictionaryValue& settings) {
733 int margins_type = 0;
734 if (!settings.GetInteger(printing::kSettingMarginsType, &margins_type))
735 margins_type = printing::DEFAULT_MARGINS;
736 ignore_css_margins_ = margins_type != printing::DEFAULT_MARGINS;
737 }
738
739 bool PrintWebViewHelper::IsPrintToPdfRequested(
740 const DictionaryValue& job_settings) {
741 bool print_to_pdf = false;
742 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf))
743 NOTREACHED();
744 return print_to_pdf;
745 }
746
563 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 747 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
564 DCHECK(is_preview_enabled_); 748 DCHECK(is_preview_enabled_);
565 print_preview_context_.OnPrintPreview(); 749 print_preview_context_.OnPrintPreview();
566 750
567 if (!UpdatePrintSettings(print_preview_context_.frame(), 751 if (!UpdatePrintSettings(print_preview_context_.frame(),
568 print_preview_context_.node(), settings, false)) { 752 print_preview_context_.node(), settings, false)) {
569 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { 753 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
570 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( 754 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
571 routing_id(), print_pages_params_->params.document_cookie)); 755 routing_id(), print_pages_params_->params.document_cookie));
572 notify_browser_of_print_failure_ = false; // Already sent. 756 notify_browser_of_print_failure_ = false; // Already sent.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 } else { 797 } else {
614 if (notify_browser_of_print_failure_) 798 if (notify_browser_of_print_failure_)
615 LOG(ERROR) << "CreatePreviewDocument failed"; 799 LOG(ERROR) << "CreatePreviewDocument failed";
616 DidFinishPrinting(FAIL_PREVIEW); 800 DidFinishPrinting(FAIL_PREVIEW);
617 } 801 }
618 } 802 }
619 803
620 bool PrintWebViewHelper::CreatePreviewDocument() { 804 bool PrintWebViewHelper::CreatePreviewDocument() {
621 PrintMsg_Print_Params print_params = print_pages_params_->params; 805 PrintMsg_Print_Params print_params = print_pages_params_->params;
622 const std::vector<int>& pages = print_pages_params_->pages; 806 const std::vector<int>& pages = print_pages_params_->pages;
623 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) 807 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages,
808 ignore_css_margins_,
809 fit_to_page_)) {
624 return false; 810 return false;
811 }
812
813 PageSizeMargins default_page_layout;
814 ComputePageLayoutInPoints(print_preview_context_.frame(), 0, print_params,
815 ignore_css_margins_, fit_to_page_, NULL,
816 &default_page_layout);
817 if (!old_print_pages_params_.get() ||
818 !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) {
819 bool has_page_size_style = PrintingFrameHasPageSizeStyle(
820 print_preview_context_.frame(),
821 print_preview_context_.total_page_count());
822 // Margins: Send default page layout to browser process.
823 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(),
824 default_page_layout,
825 has_page_size_style));
826 }
827
625 PrintHostMsg_DidGetPreviewPageCount_Params params; 828 PrintHostMsg_DidGetPreviewPageCount_Params params;
626 params.page_count = print_preview_context_.total_page_count(); 829 params.page_count = print_preview_context_.total_page_count();
627 params.is_modifiable = print_preview_context_.IsModifiable(); 830 params.is_modifiable = print_preview_context_.IsModifiable();
628 params.document_cookie = print_pages_params_->params.document_cookie; 831 params.document_cookie = print_pages_params_->params.document_cookie;
629 params.preview_request_id = print_pages_params_->params.preview_request_id; 832 params.preview_request_id = print_pages_params_->params.preview_request_id;
630 params.clear_preview_data = print_preview_context_.generate_draft_pages(); 833 params.clear_preview_data = print_preview_context_.generate_draft_pages();
631 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); 834 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params));
632 if (CheckForCancel()) 835 if (CheckForCancel())
633 return false; 836 return false;
634 837
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 1037
835 return true; 1038 return true;
836 } 1039 }
837 1040
838 #if defined(OS_MACOSX) || defined(OS_WIN) 1041 #if defined(OS_MACOSX) || defined(OS_WIN)
839 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, 1042 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params,
840 WebFrame* frame, 1043 WebFrame* frame,
841 const WebNode& node) { 1044 const WebNode& node) {
842 PrintMsg_Print_Params print_params = params.params; 1045 PrintMsg_Print_Params print_params = params.params;
843 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); 1046 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node);
844 UpdatePrintableSizeInPrintParameters(frame, node, &prep_frame_view, 1047 UpdateFrameAndViewFromCssPageLayout(frame, node, &prep_frame_view,
845 &print_params); 1048 print_params, ignore_css_margins_,
1049 fit_to_page_);
846 1050
847 int page_count = prep_frame_view.GetExpectedPageCount(); 1051 int page_count = prep_frame_view.GetExpectedPageCount();
848 if (!page_count) 1052 if (!page_count)
849 return false; 1053 return false;
850 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), 1054 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(),
851 print_params.document_cookie, 1055 print_params.document_cookie,
852 page_count)); 1056 page_count));
853 1057
854 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); 1058 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize();
855 PrintMsg_PrintPage_Params page_params; 1059 PrintMsg_PrintPage_Params page_params;
(...skipping 14 matching lines...) Expand all
870 return true; 1074 return true;
871 } 1075 }
872 #endif // OS_MACOSX || OS_WIN 1076 #endif // OS_MACOSX || OS_WIN
873 1077
874 void PrintWebViewHelper::didStopLoading() { 1078 void PrintWebViewHelper::didStopLoading() {
875 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); 1079 PrintMsg_PrintPages_Params* params = print_pages_params_.get();
876 DCHECK(params != NULL); 1080 DCHECK(params != NULL);
877 PrintPages(*params, print_web_view_->mainFrame(), WebNode()); 1081 PrintPages(*params, print_web_view_->mainFrame(), WebNode());
878 } 1082 }
879 1083
880 // static - Not anonymous so that platform implementations can use it. 1084 PrintMsg_Print_Params CalculatePrintParams(
vandebo (ex-Chrome) 2012/01/07 00:23:19 So when we have no CSS, this is a no-op, right? S
vandebo (ex-Chrome) 2012/01/07 00:23:19 Move this to the anonymous namespace.
kmadhusu 2012/01/09 17:15:55 Done.
kmadhusu 2012/01/09 17:15:55 Done.
881 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints(
882 WebFrame* frame, 1085 WebFrame* frame,
883 int page_index, 1086 int page_index,
884 const PrintMsg_Print_Params& default_params, 1087 const PrintMsg_Print_Params& default_params,
885 PageSizeMargins* page_layout_in_points) { 1088 bool ignore_css_margins,
886 int dpi = GetDPI(&default_params); 1089 bool fit_to_page,
1090 double* scale_factor) {
1091 if (ignore_css_margins && fit_to_page) {
1092 return default_params;
1093 } else {
vandebo (ex-Chrome) 2012/01/07 00:23:19 Since the if body returns, don't use an else, just
kmadhusu 2012/01/09 17:15:55 Done.
1094 PrintMsg_Print_Params params = GetCssPrintParams(frame, page_index,
1095 default_params);
887 1096
888 WebSize page_size_in_pixels( 1097 if (ignore_css_margins) {
889 ConvertUnit(default_params.page_size.width(), 1098 params.margin_top = default_params.margin_top;
890 dpi, printing::kPixelsPerInch), 1099 params.margin_left = default_params.margin_left;
891 ConvertUnit(default_params.page_size.height(),
892 dpi, printing::kPixelsPerInch));
893 int margin_top_in_pixels = ConvertUnit(
894 default_params.margin_top,
895 dpi, printing::kPixelsPerInch);
896 int margin_right_in_pixels = ConvertUnit(
897 default_params.page_size.width() -
898 default_params.content_size.width() - default_params.margin_left,
899 dpi, printing::kPixelsPerInch);
900 int margin_bottom_in_pixels = ConvertUnit(
901 default_params.page_size.height() -
902 default_params.content_size.height() - default_params.margin_top,
903 dpi, printing::kPixelsPerInch);
904 int margin_left_in_pixels = ConvertUnit(
905 default_params.margin_left,
906 dpi, printing::kPixelsPerInch);
907 1100
908 if (frame) { 1101 // Since we are ignoring the margins, the css page size is no longer
909 frame->pageSizeAndMarginsInPixels(page_index, 1102 // valid.
910 page_size_in_pixels, 1103 if (fit_to_page) {
vandebo (ex-Chrome) 2012/01/07 00:23:19 This if isn't needed, because you handeld the igno
kmadhusu 2012/01/09 17:15:55 Done.
911 margin_top_in_pixels, 1104 params.content_size = default_params.content_size;
912 margin_right_in_pixels, 1105 params.page_size = default_params.page_size;
913 margin_bottom_in_pixels, 1106 } else {
914 margin_left_in_pixels); 1107 int default_margin_right= default_params.page_size.width() -
vandebo (ex-Chrome) 2012/01/07 00:23:19 nit space: default_margin_right =
kmadhusu 2012/01/09 17:15:55 Done.
1108 default_params.content_size.width() - default_params.margin_left;
1109 int default_margin_bottom = default_params.page_size.height() -
1110 default_params.content_size.height() - default_params.margin_top;
1111 params.content_size = gfx::Size(
1112 params.page_size.width() - params.margin_left -
1113 default_margin_right,
1114 params.page_size.height() - params.margin_top -
1115 default_margin_bottom);
1116 }
1117 }
1118
1119 if (fit_to_page) {
vandebo (ex-Chrome) 2012/01/07 00:23:19 note: This could be an else if because the case wh
kmadhusu 2012/01/09 17:15:55 ok
1120 double factor = FitPrintParamsToPage(default_params, &params);
1121 if (scale_factor)
1122 *scale_factor = factor;
1123 }
1124 return params;
915 } 1125 }
916
917 page_layout_in_points->content_width =
918 ConvertPixelsToPoint(page_size_in_pixels.width -
919 margin_left_in_pixels -
920 margin_right_in_pixels);
921 page_layout_in_points->content_height =
922 ConvertPixelsToPoint(page_size_in_pixels.height -
923 margin_top_in_pixels -
924 margin_bottom_in_pixels);
925
926 // Invalid page size and/or margins. We just use the default setting.
927 if (page_layout_in_points->content_width < 1.0 ||
928 page_layout_in_points->content_height < 1.0) {
929 CHECK(frame != NULL);
930 GetPageSizeAndMarginsInPoints(NULL, page_index, default_params,
931 page_layout_in_points);
932 return;
933 }
934
935 page_layout_in_points->margin_top =
936 ConvertPixelsToPointDouble(margin_top_in_pixels);
937 page_layout_in_points->margin_right =
938 ConvertPixelsToPointDouble(margin_right_in_pixels);
939 page_layout_in_points->margin_bottom =
940 ConvertPixelsToPointDouble(margin_bottom_in_pixels);
941 page_layout_in_points->margin_left =
942 ConvertPixelsToPointDouble(margin_left_in_pixels);
943 } 1126 }
944 1127
945 // static - Not anonymous so that platform implementations can use it. 1128 // static - Not anonymous so that platform implementations can use it.
946 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( 1129 void PrintWebViewHelper::ComputePageLayoutInPoints(
vandebo (ex-Chrome) 2012/01/07 00:23:19 ComputePageLayoutInPointsForCss
kmadhusu 2012/01/09 17:15:55 Done.
1130 WebFrame* frame,
1131 int page_index,
1132 const PrintMsg_Print_Params& default_params,
1133 bool ignore_css_margins,
1134 bool fit_to_page,
1135 double* scale_factor,
1136 PageSizeMargins* page_layout_in_points) {
1137 PrintMsg_Print_Params params = CalculatePrintParams(frame, page_index,
1138 default_params,
1139 ignore_css_margins,
1140 fit_to_page,
1141 scale_factor);
1142 CalculatePageLayoutFromPrintParams(params, page_layout_in_points);
1143 }
1144
1145 // static - Not anonymous so that platform implementations can use it.
1146 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout(
947 WebFrame* frame, 1147 WebFrame* frame,
948 const WebNode& node, 1148 const WebNode& node,
949 PrepareFrameAndViewForPrint* prepare, 1149 PrepareFrameAndViewForPrint* prepare,
950 PrintMsg_Print_Params* params) { 1150 const PrintMsg_Print_Params& params,
1151 bool ignore_css_margins,
1152 bool fit_to_page) {
951 if (PrintingNodeOrPdfFrame(frame, node)) 1153 if (PrintingNodeOrPdfFrame(frame, node))
952 return; 1154 return;
953 PageSizeMargins page_layout_in_points; 1155 PrintMsg_Print_Params print_params = CalculatePrintParams(
954 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, 1156 frame, 0, params, ignore_css_margins, ignore_css_margins && fit_to_page,
955 &page_layout_in_points); 1157 NULL);
956 int dpi = GetDPI(params); 1158 prepare->UpdatePrintParams(print_params);
957 params->content_size = gfx::Size(
958 static_cast<int>(ConvertUnitDouble(
959 page_layout_in_points.content_width,
960 printing::kPointsPerInch, dpi)),
961 static_cast<int>(ConvertUnitDouble(
962 page_layout_in_points.content_height,
963 printing::kPointsPerInch, dpi)));
964
965 double page_width_in_points =
966 page_layout_in_points.content_width +
967 page_layout_in_points.margin_left +
968 page_layout_in_points.margin_right;
969 double page_height_in_points =
970 page_layout_in_points.content_height +
971 page_layout_in_points.margin_top +
972 page_layout_in_points.margin_bottom;
973
974 params->page_size = gfx::Size(
975 static_cast<int>(ConvertUnitDouble(
976 page_width_in_points, printing::kPointsPerInch, dpi)),
977 static_cast<int>(ConvertUnitDouble(
978 page_height_in_points, printing::kPointsPerInch, dpi)));
979
980 params->margin_top = static_cast<int>(ConvertUnitDouble(
981 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi));
982 params->margin_left = static_cast<int>(ConvertUnitDouble(
983 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi));
984
985 prepare->UpdatePrintParams(*params);
986 } 1159 }
987 1160
988 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, 1161 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame,
989 const WebKit::WebNode& node) { 1162 const WebKit::WebNode& node) {
990 DCHECK(frame); 1163 DCHECK(frame);
991 PrintMsg_PrintPages_Params settings; 1164 PrintMsg_PrintPages_Params settings;
992 1165
1166 // Reset to default values.
1167 ignore_css_margins_ = false;
1168 fit_to_page_ = true;
1169
993 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), 1170 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
994 &settings.params)); 1171 &settings.params));
995 // Check if the printer returned any settings, if the settings is empty, we 1172 // Check if the printer returned any settings, if the settings is empty, we
996 // can safely assume there are no printer drivers configured. So we safely 1173 // can safely assume there are no printer drivers configured. So we safely
997 // terminate. 1174 // terminate.
998 bool result = true; 1175 bool result = true;
999 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { 1176 if (PrintMsg_Print_Params_IsEmpty(settings.params)) {
1000 render_view()->RunModalAlertDialog( 1177 render_view()->RunModalAlertDialog(
1001 frame, 1178 frame,
1002 l10n_util::GetStringUTF16( 1179 l10n_util::GetStringUTF16(
(...skipping 15 matching lines...) Expand all
1018 1195
1019 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( 1196 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame(
1020 WebKit::WebFrame* frame, const WebKit::WebNode& node, 1197 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1021 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { 1198 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
1022 if (!InitPrintSettings(frame, node)) 1199 if (!InitPrintSettings(frame, node))
1023 return false; 1200 return false;
1024 1201
1025 DCHECK(!prepare->get()); 1202 DCHECK(!prepare->get());
1026 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1203 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1027 frame, node)); 1204 frame, node));
1028 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), 1205 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(),
1029 &print_pages_params_->params); 1206 print_pages_params_->params,
1207 ignore_css_margins_, fit_to_page_);
1030 Send(new PrintHostMsg_DidGetDocumentCookie( 1208 Send(new PrintHostMsg_DidGetDocumentCookie(
1031 routing_id(), print_pages_params_->params.document_cookie)); 1209 routing_id(), print_pages_params_->params.document_cookie));
1032 return true; 1210 return true;
1033 } 1211 }
1034 1212
1035 bool PrintWebViewHelper::UpdatePrintSettings( 1213 bool PrintWebViewHelper::UpdatePrintSettings(
1036 WebKit::WebFrame* frame, const WebKit::WebNode& node, 1214 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1037 const DictionaryValue& passed_job_settings, bool print_for_preview) { 1215 const DictionaryValue& passed_job_settings, bool print_for_preview) {
1038 DCHECK(is_preview_enabled_); 1216 DCHECK(is_preview_enabled_);
1039 const DictionaryValue* job_settings = &passed_job_settings; 1217 const DictionaryValue* job_settings = &passed_job_settings;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 &(settings.params.preview_ui_addr)) || 1298 &(settings.params.preview_ui_addr)) ||
1121 !job_settings->GetInteger(printing::kPreviewRequestID, 1299 !job_settings->GetInteger(printing::kPreviewRequestID,
1122 &(settings.params.preview_request_id)) || 1300 &(settings.params.preview_request_id)) ||
1123 !job_settings->GetBoolean(printing::kIsFirstRequest, 1301 !job_settings->GetBoolean(printing::kIsFirstRequest,
1124 &(settings.params.is_first_request))) { 1302 &(settings.params.is_first_request))) {
1125 NOTREACHED(); 1303 NOTREACHED();
1126 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1304 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1127 return false; 1305 return false;
1128 } 1306 }
1129 1307
1130 // Margins: Send default page layout to browser process. 1308 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings);
1131 PageSizeMargins default_page_layout; 1309 UpdateFrameMarginsCssInfo(*job_settings);
1132 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, 1310 fit_to_page_ = source_is_html && !IsPrintToPdfRequested(*job_settings);
1133 &default_page_layout);
1134 if (!old_print_pages_params_.get() ||
1135 !PageLayoutIsEqual(*old_print_pages_params_, settings)) {
1136 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(),
1137 default_page_layout));
1138 }
1139 1311
1140 // Header/Footer: Set |header_footer_info_|. 1312 // Header/Footer: Set |header_footer_info_|.
1141 if (settings.params.display_header_footer) { 1313 if (settings.params.display_header_footer) {
1142 header_footer_info_.reset(new DictionaryValue()); 1314 header_footer_info_.reset(new DictionaryValue());
1143 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, 1315 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
1144 settings.params.date); 1316 settings.params.date);
1145 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, 1317 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
1146 settings.params.url); 1318 settings.params.url);
1147 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, 1319 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
1148 settings.params.title); 1320 settings.params.title);
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
1366 node_ = web_node; 1538 node_ = web_node;
1367 } 1539 }
1368 1540
1369 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { 1541 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() {
1370 DCHECK_EQ(INITIALIZED, state_); 1542 DCHECK_EQ(INITIALIZED, state_);
1371 ClearContext(); 1543 ClearContext();
1372 } 1544 }
1373 1545
1374 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( 1546 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument(
1375 PrintMsg_Print_Params* print_params, 1547 PrintMsg_Print_Params* print_params,
1376 const std::vector<int>& pages) { 1548 const std::vector<int>& pages,
1549 bool ignore_css_margins,
1550 bool fit_to_page) {
1377 DCHECK_EQ(INITIALIZED, state_); 1551 DCHECK_EQ(INITIALIZED, state_);
1378 state_ = RENDERING; 1552 state_ = RENDERING;
1379 1553
1380 metafile_.reset(new printing::PreviewMetafile); 1554 metafile_.reset(new printing::PreviewMetafile);
1381 if (!metafile_->Init()) { 1555 if (!metafile_->Init()) {
1382 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); 1556 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED);
1383 LOG(ERROR) << "PreviewMetafile Init failed"; 1557 LOG(ERROR) << "PreviewMetafile Init failed";
1384 return false; 1558 return false;
1385 } 1559 }
1386 1560
1387 // Need to make sure old object gets destroyed first. 1561 // Need to make sure old object gets destroyed first.
1388 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), 1562 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(),
1389 node())); 1563 node()));
1390 UpdatePrintableSizeInPrintParameters(frame_, node_, 1564 UpdateFrameAndViewFromCssPageLayout(frame_, node_, prep_frame_view_.get(),
1391 prep_frame_view_.get(), print_params); 1565 *print_params, ignore_css_margins,
1566 fit_to_page);
1392 1567
1393 print_params_.reset(new PrintMsg_Print_Params(*print_params)); 1568 print_params_.reset(new PrintMsg_Print_Params(*print_params));
1394 1569
1395 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); 1570 total_page_count_ = prep_frame_view_->GetExpectedPageCount();
1396 if (total_page_count_ == 0) { 1571 if (total_page_count_ == 0) {
1397 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; 1572 LOG(ERROR) << "CreatePreviewDocument got 0 page count";
1398 set_error(PREVIEW_ERROR_ZERO_PAGES); 1573 set_error(PREVIEW_ERROR_ZERO_PAGES);
1399 return false; 1574 return false;
1400 } 1575 }
1401 1576
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 DCHECK(IsRendering()); 1732 DCHECK(IsRendering());
1558 return prep_frame_view_->GetPrintCanvasSize(); 1733 return prep_frame_view_->GetPrintCanvasSize();
1559 } 1734 }
1560 1735
1561 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1736 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1562 prep_frame_view_.reset(); 1737 prep_frame_view_.reset();
1563 metafile_.reset(); 1738 metafile_.reset();
1564 pages_to_render_.clear(); 1739 pages_to_render_.clear();
1565 error_ = PREVIEW_ERROR_NONE; 1740 error_ = PREVIEW_ERROR_NONE;
1566 } 1741 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698