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

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: rebase + fix conflicts Created 8 years, 12 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 void GetPageCssParams(
vandebo (ex-Chrome) 2012/01/03 21:55:12 nit: GetCssPrintParams ? nit: return the page_css_
kmadhusu 2012/01/04 16:55:35 Done. PrintMsg_Print_Params GetCssPageParams(...)
141 WebFrame* frame,
142 int page_index,
143 const PrintMsg_Print_Params& default_params,
144 PrintMsg_Print_Params* page_css_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 GetPageCssParams(NULL, page_index, default_params, page_css_params);
184 return;
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 }
207
208 void IgnoreCssMarginsAndUpdateParams(
209 const PrintMsg_Print_Params& default_params,
210 bool fit_to_page,
211 PrintMsg_Print_Params* page_css_params) {
212 page_css_params->margin_top = default_params.margin_top;
213 page_css_params->margin_left = default_params.margin_left;
214
215 // Since we are ignoring the margins, the css page size is no longer
216 // valid.
217 if (fit_to_page) {
218 page_css_params->content_size = default_params.content_size;
219 page_css_params->page_size = default_params.page_size;
220 } else {
221 int default_margin_right= default_params.page_size.width() -
222 default_params.content_size.width() - default_params.margin_left;
223 int default_margin_bottom = default_params.page_size.height() -
224 default_params.content_size.height() - default_params.margin_top;
225 page_css_params->content_size = gfx::Size(
vandebo (ex-Chrome) 2012/01/03 21:55:12 Is the value calculated here just default_params.c
kmadhusu 2012/01/04 16:55:35 No. page_css_params->page_size != default_params.p
226 page_css_params->page_size.width() - page_css_params->margin_left -
227 default_margin_right,
228 page_css_params->page_size.height() - page_css_params->margin_top -
229 default_margin_bottom);
230 }
231 }
232
233 void UpdateCssParamsToFitPaperSize(
vandebo (ex-Chrome) 2012/01/03 21:55:12 double/*scale_factor*/ FitPrintParamsToPage(const
kmadhusu 2012/01/04 16:55:35 Done.
234 const PrintMsg_Print_Params& default_params,
235 PrintMsg_Print_Params* page_css_params,
236 double* scale_factor) {
237 double content_width = (double)page_css_params->content_size.width();
vandebo (ex-Chrome) 2012/01/03 21:55:12 I don't think a cast is needed, but if it is, use
kmadhusu 2012/01/04 16:55:35 Done. (double) => static_cast<double>()
238 double content_height = (double)page_css_params->content_size.height();
239 int default_page_size_height = default_params.page_size.height();
vandebo (ex-Chrome) 2012/01/03 21:55:12 It looks like you don't need the variables introdu
kmadhusu 2012/01/04 16:55:35 Removed |margin_top| and |margin_left|. |new_page_
240 int default_page_size_width = default_params.page_size.width();
241 int new_page_box_height = page_css_params->page_size.height();
242 int new_page_box_width = page_css_params->page_size.width();
243 int margin_top = page_css_params->margin_top;
244 int margin_left = page_css_params->margin_left;
245
246 if (default_params.page_size != page_css_params->page_size) {
vandebo (ex-Chrome) 2012/01/03 21:55:12 negate this and push it up to the top: if (default
kmadhusu 2012/01/04 16:55:35 Done.
247 double factor = 1.0f;
248 if (default_page_size_width < new_page_box_width ||
249 default_page_size_height < new_page_box_height) {
250 double ratio_width = (double) default_params.printable_area.width() /
251 new_page_box_width;
252 double ratio_height = (double) default_params.printable_area.height() /
253 new_page_box_height;
254 factor = ratio_width < ratio_height ? ratio_width : ratio_height;
255 if (scale_factor)
256 *scale_factor = factor;
257
258 content_width *= factor;
259 content_height *= factor;
260 }
261 page_css_params->margin_top = static_cast<int>(
262 (default_page_size_height - new_page_box_height * factor)/2 +
263 (margin_top * factor));
264 page_css_params->margin_left = static_cast<int>(
265 (default_page_size_width - new_page_box_width * factor)/2 +
266 (margin_left * factor));
267 page_css_params->content_size = gfx::Size(
268 static_cast<int>(content_width), static_cast<int>(content_height));
269 page_css_params->page_size = default_params.page_size;
270 }
271 }
272
273 void CalculatePageLayoutFromCssParams(
vandebo (ex-Chrome) 2012/01/03 21:55:12 CalculatePageLayoutFromPrintParams (this isn't CSS
kmadhusu 2012/01/04 16:55:35 Done.
274 const PrintMsg_Print_Params& page_css_params,
275 PageSizeMargins* page_layout_in_points) {
276 int dpi = GetDPI(&page_css_params);
277 int content_width = page_css_params.content_size.width();
278 int content_height = page_css_params.content_size.height();
279
280 int margin_bottom = page_css_params.page_size.height() -
281 content_height - page_css_params.margin_top;
282 int margin_right = page_css_params.page_size.width() -
283 content_width - page_css_params.margin_left;
284
285 page_layout_in_points->content_width = ConvertUnit(
286 content_width, dpi, printing::kPointsPerInch);
287 page_layout_in_points->content_height = ConvertUnit(
288 content_height, dpi, printing::kPointsPerInch);
289 page_layout_in_points->margin_top = ConvertUnit(
290 page_css_params.margin_top, dpi, printing::kPointsPerInch);
291 page_layout_in_points->margin_right = ConvertUnit(
292 margin_right, dpi, printing::kPointsPerInch);
293 page_layout_in_points->margin_bottom = ConvertUnit(
294 margin_bottom, dpi, printing::kPointsPerInch);
295 page_layout_in_points->margin_left = ConvertUnit(
296 page_css_params.margin_left, dpi, printing::kPointsPerInch);
297 }
298
138 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, 299 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params,
139 gfx::Size* result) { 300 gfx::Size* result) {
140 int dpi = GetDPI(&print_params); 301 int dpi = GetDPI(&print_params);
141 result->set_width(ConvertUnit(print_params.content_size.width(), dpi, 302 result->set_width(ConvertUnit(print_params.content_size.width(), dpi,
142 print_params.desired_dpi)); 303 print_params.desired_dpi));
143 304
144 result->set_height(ConvertUnit(print_params.content_size.height(), dpi, 305 result->set_height(ConvertUnit(print_params.content_size.height(), dpi,
145 print_params.desired_dpi)); 306 print_params.desired_dpi));
146 } 307 }
147 308
148 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { 309 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) {
149 if (!node.isNull()) 310 if (!node.isNull())
150 return true; 311 return true;
151 std::string mime(frame->dataSource()->response().mimeType().utf8()); 312 std::string mime(frame->dataSource()->response().mimeType().utf8());
152 return mime == "application/pdf"; 313 return mime == "application/pdf";
153 } 314 }
154 315
316 bool PrintingFrameHasPageSizeStyle(WebFrame* frame, int total_page_count) {
317 if (!frame)
318 return false;
319 bool frame_has_custom_page_size_style = false;
320 for (int i = 0; i < total_page_count; ++i) {
321 if (frame->hasCustomPageSizeStyle(i)) {
322 frame_has_custom_page_size_style = true;
323 break;
324 }
325 }
326 return frame_has_custom_page_size_style;
327 }
328
155 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { 329 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) {
156 if (frame->isPrintScalingDisabledForPlugin(node)) 330 if (frame->isPrintScalingDisabledForPlugin(node))
157 return printing::NO_MARGINS; 331 return printing::NO_MARGINS;
158 else 332 else
159 return printing::PRINTABLE_AREA_MARGINS; 333 return printing::PRINTABLE_AREA_MARGINS;
160 } 334 }
161 335
162 // Get the (x, y) coordinate from where printing of the current text should 336 // Get the (x, y) coordinate from where printing of the current text should
163 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and 337 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
164 // vertical alignment (TOP, BOTTOM). 338 // vertical alignment (TOP, BOTTOM).
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 web_frame->setScrollOffset(prev_scroll_offset_); 607 web_frame->setScrollOffset(prev_scroll_offset_);
434 } 608 }
435 } 609 }
436 610
437 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) 611 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
438 : content::RenderViewObserver(render_view), 612 : content::RenderViewObserver(render_view),
439 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), 613 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
440 print_web_view_(NULL), 614 print_web_view_(NULL),
441 is_preview_enabled_(switches::IsPrintPreviewEnabled()), 615 is_preview_enabled_(switches::IsPrintPreviewEnabled()),
442 is_print_ready_metafile_sent_(false), 616 is_print_ready_metafile_sent_(false),
617 ignore_css_margins_(false),
618 fit_to_page_(true),
443 user_cancelled_scripted_print_count_(0), 619 user_cancelled_scripted_print_count_(0),
444 notify_browser_of_print_failure_(true) { 620 notify_browser_of_print_failure_(true) {
445 } 621 }
446 622
447 PrintWebViewHelper::~PrintWebViewHelper() {} 623 PrintWebViewHelper::~PrintWebViewHelper() {}
448 624
449 // Prints |frame| which called window.print(). 625 // Prints |frame| which called window.print().
450 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { 626 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) {
451 DCHECK(frame); 627 DCHECK(frame);
452 628
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 void PrintWebViewHelper::OnPrintForSystemDialog() { 729 void PrintWebViewHelper::OnPrintForSystemDialog() {
554 WebFrame* frame = print_preview_context_.frame(); 730 WebFrame* frame = print_preview_context_.frame();
555 if (!frame) { 731 if (!frame) {
556 NOTREACHED(); 732 NOTREACHED();
557 return; 733 return;
558 } 734 }
559 735
560 Print(frame, print_preview_context_.node()); 736 Print(frame, print_preview_context_.node());
561 } 737 }
562 738
739 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout(
740 const printing::PageSizeMargins& page_layout_in_points,
741 gfx::Size* page_size,
742 gfx::Rect* content_area) {
743 *page_size = gfx::Size(
744 page_layout_in_points.content_width +
745 page_layout_in_points.margin_right +
746 page_layout_in_points.margin_left,
747 page_layout_in_points.content_height +
748 page_layout_in_points.margin_top +
749 page_layout_in_points.margin_bottom);
750 *content_area = gfx::Rect(page_layout_in_points.margin_left,
751 page_layout_in_points.margin_top,
752 page_layout_in_points.content_width,
753 page_layout_in_points.content_height);
754 }
755
756 void PrintWebViewHelper::UpdateFrameMarginsCssInfo(
757 const DictionaryValue& settings) {
758 int margins_type = 0;
759 if (!settings.GetInteger(printing::kSettingMarginsType, &margins_type))
760 margins_type = printing::DEFAULT_MARGINS;
761 ignore_css_margins_ = margins_type != printing::DEFAULT_MARGINS;
762 }
763
764 bool PrintWebViewHelper::IsPrintToPdfRequested(
765 const DictionaryValue& job_settings) {
766 bool print_to_pdf = false;
767 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf))
768 NOTREACHED();
769 return print_to_pdf;
770 }
771
563 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 772 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
564 DCHECK(is_preview_enabled_); 773 DCHECK(is_preview_enabled_);
565 print_preview_context_.OnPrintPreview(); 774 print_preview_context_.OnPrintPreview();
566 775
567 if (!UpdatePrintSettings(print_preview_context_.frame(), 776 if (!UpdatePrintSettings(print_preview_context_.frame(),
568 print_preview_context_.node(), settings, false)) { 777 print_preview_context_.node(), settings, false)) {
569 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { 778 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
570 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( 779 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
571 routing_id(), print_pages_params_->params.document_cookie)); 780 routing_id(), print_pages_params_->params.document_cookie));
572 notify_browser_of_print_failure_ = false; // Already sent. 781 notify_browser_of_print_failure_ = false; // Already sent.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 } else { 822 } else {
614 if (notify_browser_of_print_failure_) 823 if (notify_browser_of_print_failure_)
615 LOG(ERROR) << "CreatePreviewDocument failed"; 824 LOG(ERROR) << "CreatePreviewDocument failed";
616 DidFinishPrinting(FAIL_PREVIEW); 825 DidFinishPrinting(FAIL_PREVIEW);
617 } 826 }
618 } 827 }
619 828
620 bool PrintWebViewHelper::CreatePreviewDocument() { 829 bool PrintWebViewHelper::CreatePreviewDocument() {
621 PrintMsg_Print_Params print_params = print_pages_params_->params; 830 PrintMsg_Print_Params print_params = print_pages_params_->params;
622 const std::vector<int>& pages = print_pages_params_->pages; 831 const std::vector<int>& pages = print_pages_params_->pages;
623 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) 832 if (!print_preview_context_.CreatePreviewDocument(
833 &print_params, pages, ignore_css_margins_,
vandebo (ex-Chrome) 2012/01/03 21:55:12 nit: don't need to indent four - will fit equally
kmadhusu 2012/01/04 16:55:35 Done.
834 fit_to_page_)) {
624 return false; 835 return false;
836 }
837
838 PageSizeMargins default_page_layout;
839 ComputePageLayout(print_preview_context_.frame(), 0, print_params,
840 ignore_css_margins_, fit_to_page_, NULL,
841 &default_page_layout);
842 if (!old_print_pages_params_.get() ||
843 !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) {
844 bool has_page_size_style = PrintingFrameHasPageSizeStyle(
845 print_preview_context_.frame(),
846 print_preview_context_.total_page_count());
847 // Margins: Send default page layout to browser process.
848 Send(new PrintHostMsg_DidGetDefaultPageLayout(
849 routing_id(), default_page_layout, has_page_size_style));
850 }
851
625 PrintHostMsg_DidGetPreviewPageCount_Params params; 852 PrintHostMsg_DidGetPreviewPageCount_Params params;
626 params.page_count = print_preview_context_.total_page_count(); 853 params.page_count = print_preview_context_.total_page_count();
627 params.is_modifiable = print_preview_context_.IsModifiable(); 854 params.is_modifiable = print_preview_context_.IsModifiable();
628 params.document_cookie = print_pages_params_->params.document_cookie; 855 params.document_cookie = print_pages_params_->params.document_cookie;
629 params.preview_request_id = print_pages_params_->params.preview_request_id; 856 params.preview_request_id = print_pages_params_->params.preview_request_id;
630 params.clear_preview_data = print_preview_context_.generate_draft_pages(); 857 params.clear_preview_data = print_preview_context_.generate_draft_pages();
631 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); 858 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params));
632 if (CheckForCancel()) 859 if (CheckForCancel())
633 return false; 860 return false;
634 861
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 1061
835 return true; 1062 return true;
836 } 1063 }
837 1064
838 #if defined(OS_MACOSX) || defined(OS_WIN) 1065 #if defined(OS_MACOSX) || defined(OS_WIN)
839 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, 1066 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params,
840 WebFrame* frame, 1067 WebFrame* frame,
841 const WebNode& node) { 1068 const WebNode& node) {
842 PrintMsg_Print_Params print_params = params.params; 1069 PrintMsg_Print_Params print_params = params.params;
843 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); 1070 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node);
844 UpdatePrintableSizeInPrintParameters(frame, node, &prep_frame_view, 1071 UpdateFrameAndViewFromCssPageLayout(frame, node, &prep_frame_view,
845 &print_params); 1072 print_params, ignore_css_margins_,
1073 fit_to_page_);
846 1074
847 int page_count = prep_frame_view.GetExpectedPageCount(); 1075 int page_count = prep_frame_view.GetExpectedPageCount();
848 if (!page_count) 1076 if (!page_count)
849 return false; 1077 return false;
850 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), 1078 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(),
851 print_params.document_cookie, 1079 print_params.document_cookie,
852 page_count)); 1080 page_count));
853 1081
854 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); 1082 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize();
855 PrintMsg_PrintPage_Params page_params; 1083 PrintMsg_PrintPage_Params page_params;
(...skipping 15 matching lines...) Expand all
871 } 1099 }
872 #endif // OS_MACOSX || OS_WIN 1100 #endif // OS_MACOSX || OS_WIN
873 1101
874 void PrintWebViewHelper::didStopLoading() { 1102 void PrintWebViewHelper::didStopLoading() {
875 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); 1103 PrintMsg_PrintPages_Params* params = print_pages_params_.get();
876 DCHECK(params != NULL); 1104 DCHECK(params != NULL);
877 PrintPages(*params, print_web_view_->mainFrame(), WebNode()); 1105 PrintPages(*params, print_web_view_->mainFrame(), WebNode());
878 } 1106 }
879 1107
880 // static - Not anonymous so that platform implementations can use it. 1108 // static - Not anonymous so that platform implementations can use it.
881 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( 1109 void PrintWebViewHelper::ComputePageLayout(
882 WebFrame* frame, 1110 WebFrame* frame,
883 int page_index, 1111 int page_index,
884 const PrintMsg_Print_Params& default_params, 1112 const PrintMsg_Print_Params& default_params,
1113 bool ignore_css_margins,
1114 bool fit_to_page,
1115 double* scale_factor,
885 PageSizeMargins* page_layout_in_points) { 1116 PageSizeMargins* page_layout_in_points) {
886 int dpi = GetDPI(&default_params); 1117 PrintMsg_Print_Params page_css_params = default_params;
vandebo (ex-Chrome) 2012/01/03 21:55:12 I don't think we need to copy it here, since GetPa
vandebo (ex-Chrome) 2012/01/03 21:55:12 Instead of calling it page_css_params (which is on
kmadhusu 2012/01/04 16:55:35 Done. page_css_params => params
kmadhusu 2012/01/04 16:55:35 GetPageCssParams only fills "page_size, content_si
887 1118 GetPageCssParams(frame, page_index, default_params, &page_css_params);
vandebo (ex-Chrome) 2012/01/03 21:55:12 Is it correct that we don't need to call this if i
kmadhusu 2012/01/04 16:55:35 Done.
888 WebSize page_size_in_pixels( 1119 if (ignore_css_margins) {
889 ConvertUnit(default_params.page_size.width(), 1120 IgnoreCssMarginsAndUpdateParams(default_params, fit_to_page,
vandebo (ex-Chrome) 2012/01/03 21:55:12 nit: I'd probably put the contents of this method
kmadhusu 2012/01/04 16:55:35 Done.
890 dpi, printing::kPixelsPerInch), 1121 &page_css_params);
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
908 if (frame) {
909 frame->pageSizeAndMarginsInPixels(page_index,
910 page_size_in_pixels,
911 margin_top_in_pixels,
912 margin_right_in_pixels,
913 margin_bottom_in_pixels,
914 margin_left_in_pixels);
915 } 1122 }
916 1123
917 page_layout_in_points->content_width = 1124 if (fit_to_page) {
918 ConvertPixelsToPoint(page_size_in_pixels.width - 1125 UpdateCssParamsToFitPaperSize(default_params, &page_css_params,
919 margin_left_in_pixels - 1126 scale_factor);
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 } 1127 }
934 1128 CalculatePageLayoutFromCssParams(page_css_params, page_layout_in_points);
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 } 1129 }
944 1130
945 // static - Not anonymous so that platform implementations can use it. 1131 // static - Not anonymous so that platform implementations can use it.
946 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( 1132 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout(
947 WebFrame* frame, 1133 WebFrame* frame,
948 const WebNode& node, 1134 const WebNode& node,
949 PrepareFrameAndViewForPrint* prepare, 1135 PrepareFrameAndViewForPrint* prepare,
950 PrintMsg_Print_Params* params) { 1136 const PrintMsg_Print_Params& params,
1137 bool ignore_css_margins,
1138 bool fit_to_page) {
951 if (PrintingNodeOrPdfFrame(frame, node)) 1139 if (PrintingNodeOrPdfFrame(frame, node))
952 return; 1140 return;
953 PageSizeMargins page_layout_in_points; 1141 PageSizeMargins page_layout_in_points;
954 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, 1142 PrintMsg_Print_Params current_params = params;
955 &page_layout_in_points); 1143 PrintWebViewHelper::ComputePageLayout(frame, 0, current_params,
vandebo (ex-Chrome) 2012/01/03 21:55:12 It seems that this caller of ComputePageLayout act
kmadhusu 2012/01/04 16:55:35 ComputePageLayout is divided into two different fu
956 int dpi = GetDPI(params); 1144 ignore_css_margins, ignore_css_margins && fit_to_page, NULL,
957 params->content_size = gfx::Size( 1145 &page_layout_in_points);
1146 int dpi = GetDPI(&current_params);
1147 current_params.content_size = gfx::Size(
958 static_cast<int>(ConvertUnitDouble( 1148 static_cast<int>(ConvertUnitDouble(
959 page_layout_in_points.content_width, 1149 page_layout_in_points.content_width,
960 printing::kPointsPerInch, dpi)), 1150 printing::kPointsPerInch, dpi)),
961 static_cast<int>(ConvertUnitDouble( 1151 static_cast<int>(ConvertUnitDouble(
962 page_layout_in_points.content_height, 1152 page_layout_in_points.content_height,
963 printing::kPointsPerInch, dpi))); 1153 printing::kPointsPerInch, dpi)));
964
965 double page_width_in_points = 1154 double page_width_in_points =
966 page_layout_in_points.content_width + 1155 page_layout_in_points.content_width +
967 page_layout_in_points.margin_left + 1156 page_layout_in_points.margin_left +
968 page_layout_in_points.margin_right; 1157 page_layout_in_points.margin_right;
969 double page_height_in_points = 1158 double page_height_in_points =
970 page_layout_in_points.content_height + 1159 page_layout_in_points.content_height +
971 page_layout_in_points.margin_top + 1160 page_layout_in_points.margin_top +
972 page_layout_in_points.margin_bottom; 1161 page_layout_in_points.margin_bottom;
973 1162
974 params->page_size = gfx::Size( 1163 current_params.page_size = gfx::Size(
975 static_cast<int>(ConvertUnitDouble( 1164 static_cast<int>(ConvertUnitDouble(
976 page_width_in_points, printing::kPointsPerInch, dpi)), 1165 page_width_in_points, printing::kPointsPerInch, dpi)),
977 static_cast<int>(ConvertUnitDouble( 1166 static_cast<int>(ConvertUnitDouble(
978 page_height_in_points, printing::kPointsPerInch, dpi))); 1167 page_height_in_points, printing::kPointsPerInch, dpi)));
979 1168 current_params.margin_top =
980 params->margin_top = static_cast<int>(ConvertUnitDouble( 1169 static_cast<int>(ConvertUnitDouble(
981 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); 1170 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi));
982 params->margin_left = static_cast<int>(ConvertUnitDouble( 1171 current_params.margin_left = static_cast<int>(ConvertUnitDouble(
983 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); 1172 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi));
984 1173 prepare->UpdatePrintParams(current_params);
985 prepare->UpdatePrintParams(*params);
986 } 1174 }
987 1175
988 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, 1176 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame,
989 const WebKit::WebNode& node) { 1177 const WebKit::WebNode& node) {
990 DCHECK(frame); 1178 DCHECK(frame);
991 PrintMsg_PrintPages_Params settings; 1179 PrintMsg_PrintPages_Params settings;
992 1180
1181 // Reset to default values.
1182 ignore_css_margins_ = false;
1183 fit_to_page_ = true;
1184
993 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), 1185 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
994 &settings.params)); 1186 &settings.params));
995 // Check if the printer returned any settings, if the settings is empty, we 1187 // 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 1188 // can safely assume there are no printer drivers configured. So we safely
997 // terminate. 1189 // terminate.
998 bool result = true; 1190 bool result = true;
999 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { 1191 if (PrintMsg_Print_Params_IsEmpty(settings.params)) {
1000 render_view()->RunModalAlertDialog( 1192 render_view()->RunModalAlertDialog(
1001 frame, 1193 frame,
1002 l10n_util::GetStringUTF16( 1194 l10n_util::GetStringUTF16(
(...skipping 15 matching lines...) Expand all
1018 1210
1019 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( 1211 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame(
1020 WebKit::WebFrame* frame, const WebKit::WebNode& node, 1212 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1021 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { 1213 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
1022 if (!InitPrintSettings(frame, node)) 1214 if (!InitPrintSettings(frame, node))
1023 return false; 1215 return false;
1024 1216
1025 DCHECK(!prepare->get()); 1217 DCHECK(!prepare->get());
1026 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1218 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1027 frame, node)); 1219 frame, node));
1028 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), 1220 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(),
1029 &print_pages_params_->params); 1221 print_pages_params_->params,
1222 ignore_css_margins_, fit_to_page_);
1030 Send(new PrintHostMsg_DidGetDocumentCookie( 1223 Send(new PrintHostMsg_DidGetDocumentCookie(
1031 routing_id(), print_pages_params_->params.document_cookie)); 1224 routing_id(), print_pages_params_->params.document_cookie));
1032 return true; 1225 return true;
1033 } 1226 }
1034 1227
1035 bool PrintWebViewHelper::UpdatePrintSettings( 1228 bool PrintWebViewHelper::UpdatePrintSettings(
1036 WebKit::WebFrame* frame, const WebKit::WebNode& node, 1229 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1037 const DictionaryValue& passed_job_settings, bool print_for_preview) { 1230 const DictionaryValue& passed_job_settings, bool print_for_preview) {
1038 DCHECK(is_preview_enabled_); 1231 DCHECK(is_preview_enabled_);
1039 const DictionaryValue* job_settings = &passed_job_settings; 1232 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)) || 1313 &(settings.params.preview_ui_addr)) ||
1121 !job_settings->GetInteger(printing::kPreviewRequestID, 1314 !job_settings->GetInteger(printing::kPreviewRequestID,
1122 &(settings.params.preview_request_id)) || 1315 &(settings.params.preview_request_id)) ||
1123 !job_settings->GetBoolean(printing::kIsFirstRequest, 1316 !job_settings->GetBoolean(printing::kIsFirstRequest,
1124 &(settings.params.is_first_request))) { 1317 &(settings.params.is_first_request))) {
1125 NOTREACHED(); 1318 NOTREACHED();
1126 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1319 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1127 return false; 1320 return false;
1128 } 1321 }
1129 1322
1130 // Margins: Send default page layout to browser process. 1323 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings);
1131 PageSizeMargins default_page_layout; 1324 UpdateFrameMarginsCssInfo(*job_settings);
1132 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, 1325 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 1326
1140 // Header/Footer: Set |header_footer_info_|. 1327 // Header/Footer: Set |header_footer_info_|.
1141 if (settings.params.display_header_footer) { 1328 if (settings.params.display_header_footer) {
1142 header_footer_info_.reset(new DictionaryValue()); 1329 header_footer_info_.reset(new DictionaryValue());
1143 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, 1330 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
1144 settings.params.date); 1331 settings.params.date);
1145 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, 1332 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
1146 settings.params.url); 1333 settings.params.url);
1147 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, 1334 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
1148 settings.params.title); 1335 settings.params.title);
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
1366 node_ = web_node; 1553 node_ = web_node;
1367 } 1554 }
1368 1555
1369 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { 1556 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() {
1370 DCHECK_EQ(INITIALIZED, state_); 1557 DCHECK_EQ(INITIALIZED, state_);
1371 ClearContext(); 1558 ClearContext();
1372 } 1559 }
1373 1560
1374 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( 1561 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument(
1375 PrintMsg_Print_Params* print_params, 1562 PrintMsg_Print_Params* print_params,
1376 const std::vector<int>& pages) { 1563 const std::vector<int>& pages,
1564 bool ignore_css_margins,
1565 bool fit_to_page) {
1377 DCHECK_EQ(INITIALIZED, state_); 1566 DCHECK_EQ(INITIALIZED, state_);
1378 state_ = RENDERING; 1567 state_ = RENDERING;
1379 1568
1380 metafile_.reset(new printing::PreviewMetafile); 1569 metafile_.reset(new printing::PreviewMetafile);
1381 if (!metafile_->Init()) { 1570 if (!metafile_->Init()) {
1382 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); 1571 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED);
1383 LOG(ERROR) << "PreviewMetafile Init failed"; 1572 LOG(ERROR) << "PreviewMetafile Init failed";
1384 return false; 1573 return false;
1385 } 1574 }
1386 1575
1387 // Need to make sure old object gets destroyed first. 1576 // Need to make sure old object gets destroyed first.
1388 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), 1577 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(),
1389 node())); 1578 node()));
1390 UpdatePrintableSizeInPrintParameters(frame_, node_, 1579 UpdateFrameAndViewFromCssPageLayout(frame_, node_,
1391 prep_frame_view_.get(), print_params); 1580 prep_frame_view_.get(), *print_params,
1581 ignore_css_margins, fit_to_page);
1392 1582
1393 print_params_.reset(new PrintMsg_Print_Params(*print_params)); 1583 print_params_.reset(new PrintMsg_Print_Params(*print_params));
1394 1584
1395 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); 1585 total_page_count_ = prep_frame_view_->GetExpectedPageCount();
1396 if (total_page_count_ == 0) { 1586 if (total_page_count_ == 0) {
1397 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; 1587 LOG(ERROR) << "CreatePreviewDocument got 0 page count";
1398 set_error(PREVIEW_ERROR_ZERO_PAGES); 1588 set_error(PREVIEW_ERROR_ZERO_PAGES);
1399 return false; 1589 return false;
1400 } 1590 }
1401 1591
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 DCHECK(IsRendering()); 1747 DCHECK(IsRendering());
1558 return prep_frame_view_->GetPrintCanvasSize(); 1748 return prep_frame_view_->GetPrintCanvasSize();
1559 } 1749 }
1560 1750
1561 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1751 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1562 prep_frame_view_.reset(); 1752 prep_frame_view_.reset();
1563 metafile_.reset(); 1753 metafile_.reset();
1564 pages_to_render_.clear(); 1754 pages_to_render_.clear();
1565 error_ = PREVIEW_ERROR_NONE; 1755 error_ = PREVIEW_ERROR_NONE;
1566 } 1756 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698