OLD | NEW |
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 Loading... |
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& page_params) { |
| 144 PrintMsg_Print_Params page_css_params = page_params; |
| 145 int dpi = GetDPI(&page_params); |
| 146 WebSize page_size_in_pixels( |
| 147 ConvertUnit(page_params.page_size.width(), |
| 148 dpi, printing::kPixelsPerInch), |
| 149 ConvertUnit(page_params.page_size.height(), |
| 150 dpi, printing::kPixelsPerInch)); |
| 151 int margin_top_in_pixels = ConvertUnit( |
| 152 page_params.margin_top, |
| 153 dpi, printing::kPixelsPerInch); |
| 154 int margin_right_in_pixels = ConvertUnit( |
| 155 page_params.page_size.width() - |
| 156 page_params.content_size.width() - page_params.margin_left, |
| 157 dpi, printing::kPixelsPerInch); |
| 158 int margin_bottom_in_pixels = ConvertUnit( |
| 159 page_params.page_size.height() - |
| 160 page_params.content_size.height() - page_params.margin_top, |
| 161 dpi, printing::kPixelsPerInch); |
| 162 int margin_left_in_pixels = ConvertUnit( |
| 163 page_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, page_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& page_params, |
| 210 PrintMsg_Print_Params* params_to_fit) { |
| 211 double content_width = |
| 212 static_cast<double>(params_to_fit->content_size.width()); |
| 213 double content_height = |
| 214 static_cast<double>(params_to_fit->content_size.height()); |
| 215 int default_page_size_height = page_params.page_size.height(); |
| 216 int default_page_size_width = page_params.page_size.width(); |
| 217 int css_page_size_height = params_to_fit->page_size.height(); |
| 218 int css_page_size_width = params_to_fit->page_size.width(); |
| 219 |
| 220 double scale_factor = 1.0f; |
| 221 if (page_params.page_size == params_to_fit->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>(page_params.printable_area.width()) / |
| 228 css_page_size_width; |
| 229 double ratio_height = |
| 230 static_cast<double>(page_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 params_to_fit->margin_top = static_cast<int>( |
| 237 (default_page_size_height - css_page_size_height * scale_factor) / 2 + |
| 238 (params_to_fit->margin_top * scale_factor)); |
| 239 params_to_fit->margin_left = static_cast<int>( |
| 240 (default_page_size_width - css_page_size_width * scale_factor) / 2 + |
| 241 (params_to_fit->margin_left * scale_factor)); |
| 242 params_to_fit->content_size = gfx::Size( |
| 243 static_cast<int>(content_width), static_cast<int>(content_height)); |
| 244 params_to_fit->page_size = page_params.page_size; |
| 245 return scale_factor; |
| 246 } |
| 247 |
| 248 void CalculatePageLayoutFromPrintParams( |
| 249 const PrintMsg_Print_Params& params, |
| 250 PageSizeMargins* page_layout_in_points) { |
| 251 int dpi = GetDPI(¶ms); |
| 252 int content_width = params.content_size.width(); |
| 253 int content_height = params.content_size.height(); |
| 254 |
| 255 int margin_bottom = params.page_size.height() - |
| 256 content_height - params.margin_top; |
| 257 int margin_right = params.page_size.width() - |
| 258 content_width - 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 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 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, | 395 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, |
247 page_layout, horizontal_position, | 396 page_layout, horizontal_position, |
248 vertical_position, offset_to_baseline, | 397 vertical_position, offset_to_baseline, |
249 text_width_in_points); | 398 text_width_in_points); |
250 CGContextSetTextPosition(canvas, SkScalarToDouble(point.x()), | 399 CGContextSetTextPosition(canvas, SkScalarToDouble(point.x()), |
251 SkScalarToDouble(point.y())); | 400 SkScalarToDouble(point.y())); |
252 CTLineDraw(line, canvas); | 401 CTLineDraw(line, canvas); |
253 #endif | 402 #endif |
254 } | 403 } |
255 | 404 |
| 405 PrintMsg_Print_Params CalculatePrintParamsForCss( |
| 406 WebFrame* frame, |
| 407 int page_index, |
| 408 const PrintMsg_Print_Params& page_params, |
| 409 bool ignore_css_margins, |
| 410 bool fit_to_page, |
| 411 double* scale_factor) { |
| 412 if (ignore_css_margins && fit_to_page) |
| 413 return page_params; |
| 414 |
| 415 PrintMsg_Print_Params params = GetCssPrintParams(frame, page_index, |
| 416 page_params); |
| 417 |
| 418 if (ignore_css_margins) { |
| 419 params.margin_top = page_params.margin_top; |
| 420 params.margin_left = page_params.margin_left; |
| 421 |
| 422 DCHECK(!fit_to_page); |
| 423 // Since we are ignoring the margins, the css page size is no longer |
| 424 // valid. |
| 425 int default_margin_right = page_params.page_size.width() - |
| 426 page_params.content_size.width() - page_params.margin_left; |
| 427 int default_margin_bottom = page_params.page_size.height() - |
| 428 page_params.content_size.height() - page_params.margin_top; |
| 429 params.content_size = gfx::Size( |
| 430 params.page_size.width() - params.margin_left - default_margin_right, |
| 431 params.page_size.height() - params.margin_top - default_margin_bottom); |
| 432 } |
| 433 |
| 434 if (fit_to_page) { |
| 435 double factor = FitPrintParamsToPage(page_params, ¶ms); |
| 436 if (scale_factor) |
| 437 *scale_factor = factor; |
| 438 } |
| 439 return params; |
| 440 } |
| 441 |
256 } // namespace | 442 } // namespace |
257 | 443 |
258 // static - Not anonymous so that platform implementations can use it. | 444 // static - Not anonymous so that platform implementations can use it. |
259 void PrintWebViewHelper::PrintHeaderAndFooter( | 445 void PrintWebViewHelper::PrintHeaderAndFooter( |
260 WebKit::WebCanvas* canvas, | 446 WebKit::WebCanvas* canvas, |
261 int page_number, | 447 int page_number, |
262 int total_pages, | 448 int total_pages, |
263 float webkit_scale_factor, | 449 float webkit_scale_factor, |
264 const PageSizeMargins& page_layout, | 450 const PageSizeMargins& page_layout, |
265 const DictionaryValue& header_footer_info) { | 451 const DictionaryValue& header_footer_info) { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 web_frame->setScrollOffset(prev_scroll_offset_); | 619 web_frame->setScrollOffset(prev_scroll_offset_); |
434 } | 620 } |
435 } | 621 } |
436 | 622 |
437 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) | 623 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) |
438 : content::RenderViewObserver(render_view), | 624 : content::RenderViewObserver(render_view), |
439 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), | 625 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), |
440 print_web_view_(NULL), | 626 print_web_view_(NULL), |
441 is_preview_enabled_(switches::IsPrintPreviewEnabled()), | 627 is_preview_enabled_(switches::IsPrintPreviewEnabled()), |
442 is_print_ready_metafile_sent_(false), | 628 is_print_ready_metafile_sent_(false), |
| 629 ignore_css_margins_(false), |
| 630 fit_to_page_(true), |
443 user_cancelled_scripted_print_count_(0), | 631 user_cancelled_scripted_print_count_(0), |
444 notify_browser_of_print_failure_(true) { | 632 notify_browser_of_print_failure_(true) { |
445 } | 633 } |
446 | 634 |
447 PrintWebViewHelper::~PrintWebViewHelper() {} | 635 PrintWebViewHelper::~PrintWebViewHelper() {} |
448 | 636 |
449 // Prints |frame| which called window.print(). | 637 // Prints |frame| which called window.print(). |
450 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { | 638 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { |
451 DCHECK(frame); | 639 DCHECK(frame); |
452 | 640 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 void PrintWebViewHelper::OnPrintForSystemDialog() { | 741 void PrintWebViewHelper::OnPrintForSystemDialog() { |
554 WebFrame* frame = print_preview_context_.frame(); | 742 WebFrame* frame = print_preview_context_.frame(); |
555 if (!frame) { | 743 if (!frame) { |
556 NOTREACHED(); | 744 NOTREACHED(); |
557 return; | 745 return; |
558 } | 746 } |
559 | 747 |
560 Print(frame, print_preview_context_.node()); | 748 Print(frame, print_preview_context_.node()); |
561 } | 749 } |
562 | 750 |
| 751 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( |
| 752 const printing::PageSizeMargins& page_layout_in_points, |
| 753 gfx::Size* page_size, |
| 754 gfx::Rect* content_area) { |
| 755 *page_size = gfx::Size( |
| 756 page_layout_in_points.content_width + |
| 757 page_layout_in_points.margin_right + |
| 758 page_layout_in_points.margin_left, |
| 759 page_layout_in_points.content_height + |
| 760 page_layout_in_points.margin_top + |
| 761 page_layout_in_points.margin_bottom); |
| 762 *content_area = gfx::Rect(page_layout_in_points.margin_left, |
| 763 page_layout_in_points.margin_top, |
| 764 page_layout_in_points.content_width, |
| 765 page_layout_in_points.content_height); |
| 766 } |
| 767 |
| 768 void PrintWebViewHelper::UpdateFrameMarginsCssInfo( |
| 769 const DictionaryValue& settings) { |
| 770 int margins_type = 0; |
| 771 if (!settings.GetInteger(printing::kSettingMarginsType, &margins_type)) |
| 772 margins_type = printing::DEFAULT_MARGINS; |
| 773 ignore_css_margins_ = margins_type != printing::DEFAULT_MARGINS; |
| 774 } |
| 775 |
| 776 bool PrintWebViewHelper::IsPrintToPdfRequested( |
| 777 const DictionaryValue& job_settings) { |
| 778 bool print_to_pdf = false; |
| 779 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) |
| 780 NOTREACHED(); |
| 781 return print_to_pdf; |
| 782 } |
| 783 |
563 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 784 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
564 DCHECK(is_preview_enabled_); | 785 DCHECK(is_preview_enabled_); |
565 print_preview_context_.OnPrintPreview(); | 786 print_preview_context_.OnPrintPreview(); |
566 | 787 |
567 if (!UpdatePrintSettings(print_preview_context_.frame(), | 788 if (!UpdatePrintSettings(print_preview_context_.frame(), |
568 print_preview_context_.node(), settings, false)) { | 789 print_preview_context_.node(), settings, false)) { |
569 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 790 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
570 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 791 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
571 routing_id(), print_pages_params_->params.document_cookie)); | 792 routing_id(), print_pages_params_->params.document_cookie)); |
572 notify_browser_of_print_failure_ = false; // Already sent. | 793 notify_browser_of_print_failure_ = false; // Already sent. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 } else { | 834 } else { |
614 if (notify_browser_of_print_failure_) | 835 if (notify_browser_of_print_failure_) |
615 LOG(ERROR) << "CreatePreviewDocument failed"; | 836 LOG(ERROR) << "CreatePreviewDocument failed"; |
616 DidFinishPrinting(FAIL_PREVIEW); | 837 DidFinishPrinting(FAIL_PREVIEW); |
617 } | 838 } |
618 } | 839 } |
619 | 840 |
620 bool PrintWebViewHelper::CreatePreviewDocument() { | 841 bool PrintWebViewHelper::CreatePreviewDocument() { |
621 PrintMsg_Print_Params print_params = print_pages_params_->params; | 842 PrintMsg_Print_Params print_params = print_pages_params_->params; |
622 const std::vector<int>& pages = print_pages_params_->pages; | 843 const std::vector<int>& pages = print_pages_params_->pages; |
623 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) | 844 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages, |
| 845 ignore_css_margins_, |
| 846 fit_to_page_)) { |
624 return false; | 847 return false; |
| 848 } |
| 849 |
| 850 PageSizeMargins default_page_layout; |
| 851 ComputePageLayoutInPointsForCss(print_preview_context_.frame(), 0, |
| 852 print_params, ignore_css_margins_, |
| 853 fit_to_page_, NULL, &default_page_layout); |
| 854 if (!old_print_pages_params_.get() || |
| 855 !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) { |
| 856 bool has_page_size_style = PrintingFrameHasPageSizeStyle( |
| 857 print_preview_context_.frame(), |
| 858 print_preview_context_.total_page_count()); |
| 859 // Margins: Send default page layout to browser process. |
| 860 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(), |
| 861 default_page_layout, |
| 862 has_page_size_style)); |
| 863 } |
| 864 |
625 PrintHostMsg_DidGetPreviewPageCount_Params params; | 865 PrintHostMsg_DidGetPreviewPageCount_Params params; |
626 params.page_count = print_preview_context_.total_page_count(); | 866 params.page_count = print_preview_context_.total_page_count(); |
627 params.is_modifiable = print_preview_context_.IsModifiable(); | 867 params.is_modifiable = print_preview_context_.IsModifiable(); |
628 params.document_cookie = print_pages_params_->params.document_cookie; | 868 params.document_cookie = print_pages_params_->params.document_cookie; |
629 params.preview_request_id = print_pages_params_->params.preview_request_id; | 869 params.preview_request_id = print_pages_params_->params.preview_request_id; |
630 params.clear_preview_data = print_preview_context_.generate_draft_pages(); | 870 params.clear_preview_data = print_preview_context_.generate_draft_pages(); |
631 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); | 871 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); |
632 if (CheckForCancel()) | 872 if (CheckForCancel()) |
633 return false; | 873 return false; |
634 | 874 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 | 1074 |
835 return true; | 1075 return true; |
836 } | 1076 } |
837 | 1077 |
838 #if defined(OS_MACOSX) || defined(OS_WIN) | 1078 #if defined(OS_MACOSX) || defined(OS_WIN) |
839 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, | 1079 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, |
840 WebFrame* frame, | 1080 WebFrame* frame, |
841 const WebNode& node) { | 1081 const WebNode& node) { |
842 PrintMsg_Print_Params print_params = params.params; | 1082 PrintMsg_Print_Params print_params = params.params; |
843 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); | 1083 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); |
844 UpdatePrintableSizeInPrintParameters(frame, node, &prep_frame_view, | 1084 UpdateFrameAndViewFromCssPageLayout(frame, node, &prep_frame_view, |
845 &print_params); | 1085 print_params, ignore_css_margins_, |
| 1086 fit_to_page_); |
846 | 1087 |
847 int page_count = prep_frame_view.GetExpectedPageCount(); | 1088 int page_count = prep_frame_view.GetExpectedPageCount(); |
848 if (!page_count) | 1089 if (!page_count) |
849 return false; | 1090 return false; |
850 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 1091 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
851 print_params.document_cookie, | 1092 print_params.document_cookie, |
852 page_count)); | 1093 page_count)); |
853 | 1094 |
854 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 1095 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
855 PrintMsg_PrintPage_Params page_params; | 1096 PrintMsg_PrintPage_Params page_params; |
(...skipping 15 matching lines...) Expand all Loading... |
871 } | 1112 } |
872 #endif // OS_MACOSX || OS_WIN | 1113 #endif // OS_MACOSX || OS_WIN |
873 | 1114 |
874 void PrintWebViewHelper::didStopLoading() { | 1115 void PrintWebViewHelper::didStopLoading() { |
875 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); | 1116 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); |
876 DCHECK(params != NULL); | 1117 DCHECK(params != NULL); |
877 PrintPages(*params, print_web_view_->mainFrame(), WebNode()); | 1118 PrintPages(*params, print_web_view_->mainFrame(), WebNode()); |
878 } | 1119 } |
879 | 1120 |
880 // static - Not anonymous so that platform implementations can use it. | 1121 // static - Not anonymous so that platform implementations can use it. |
881 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( | 1122 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( |
882 WebFrame* frame, | 1123 WebFrame* frame, |
883 int page_index, | 1124 int page_index, |
884 const PrintMsg_Print_Params& default_params, | 1125 const PrintMsg_Print_Params& page_params, |
| 1126 bool ignore_css_margins, |
| 1127 bool fit_to_page, |
| 1128 double* scale_factor, |
885 PageSizeMargins* page_layout_in_points) { | 1129 PageSizeMargins* page_layout_in_points) { |
886 int dpi = GetDPI(&default_params); | 1130 PrintMsg_Print_Params params = CalculatePrintParamsForCss(frame, page_index, |
887 | 1131 page_params, |
888 WebSize page_size_in_pixels( | 1132 ignore_css_margins, |
889 ConvertUnit(default_params.page_size.width(), | 1133 fit_to_page, |
890 dpi, printing::kPixelsPerInch), | 1134 scale_factor); |
891 ConvertUnit(default_params.page_size.height(), | 1135 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); |
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 } | |
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 } | 1136 } |
944 | 1137 |
945 // static - Not anonymous so that platform implementations can use it. | 1138 // static - Not anonymous so that platform implementations can use it. |
946 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( | 1139 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( |
947 WebFrame* frame, | 1140 WebFrame* frame, |
948 const WebNode& node, | 1141 const WebNode& node, |
949 PrepareFrameAndViewForPrint* prepare, | 1142 PrepareFrameAndViewForPrint* prepare, |
950 PrintMsg_Print_Params* params) { | 1143 const PrintMsg_Print_Params& params, |
| 1144 bool ignore_css_margins, |
| 1145 bool fit_to_page) { |
951 if (PrintingNodeOrPdfFrame(frame, node)) | 1146 if (PrintingNodeOrPdfFrame(frame, node)) |
952 return; | 1147 return; |
953 PageSizeMargins page_layout_in_points; | 1148 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( |
954 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, | 1149 frame, 0, params, ignore_css_margins, ignore_css_margins && fit_to_page, |
955 &page_layout_in_points); | 1150 NULL); |
956 int dpi = GetDPI(params); | 1151 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 } | 1152 } |
987 | 1153 |
988 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, | 1154 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, |
989 const WebKit::WebNode& node) { | 1155 const WebKit::WebNode& node) { |
990 DCHECK(frame); | 1156 DCHECK(frame); |
991 PrintMsg_PrintPages_Params settings; | 1157 PrintMsg_PrintPages_Params settings; |
992 | 1158 |
| 1159 // Reset to default values. |
| 1160 ignore_css_margins_ = false; |
| 1161 fit_to_page_ = true; |
| 1162 |
993 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1163 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
994 &settings.params)); | 1164 &settings.params)); |
995 // Check if the printer returned any settings, if the settings is empty, we | 1165 // 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 | 1166 // can safely assume there are no printer drivers configured. So we safely |
997 // terminate. | 1167 // terminate. |
998 bool result = true; | 1168 bool result = true; |
999 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { | 1169 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { |
1000 render_view()->RunModalAlertDialog( | 1170 render_view()->RunModalAlertDialog( |
1001 frame, | 1171 frame, |
1002 l10n_util::GetStringUTF16( | 1172 l10n_util::GetStringUTF16( |
(...skipping 15 matching lines...) Expand all Loading... |
1018 | 1188 |
1019 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1189 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
1020 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1190 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
1021 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | 1191 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
1022 if (!InitPrintSettings(frame, node)) | 1192 if (!InitPrintSettings(frame, node)) |
1023 return false; | 1193 return false; |
1024 | 1194 |
1025 DCHECK(!prepare->get()); | 1195 DCHECK(!prepare->get()); |
1026 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1196 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
1027 frame, node)); | 1197 frame, node)); |
1028 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), | 1198 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), |
1029 &print_pages_params_->params); | 1199 print_pages_params_->params, |
| 1200 ignore_css_margins_, fit_to_page_); |
1030 Send(new PrintHostMsg_DidGetDocumentCookie( | 1201 Send(new PrintHostMsg_DidGetDocumentCookie( |
1031 routing_id(), print_pages_params_->params.document_cookie)); | 1202 routing_id(), print_pages_params_->params.document_cookie)); |
1032 return true; | 1203 return true; |
1033 } | 1204 } |
1034 | 1205 |
1035 bool PrintWebViewHelper::UpdatePrintSettings( | 1206 bool PrintWebViewHelper::UpdatePrintSettings( |
1036 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1207 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
1037 const DictionaryValue& passed_job_settings, bool print_for_preview) { | 1208 const DictionaryValue& passed_job_settings, bool print_for_preview) { |
1038 DCHECK(is_preview_enabled_); | 1209 DCHECK(is_preview_enabled_); |
1039 const DictionaryValue* job_settings = &passed_job_settings; | 1210 const DictionaryValue* job_settings = &passed_job_settings; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1120 &(settings.params.preview_ui_addr)) || | 1291 &(settings.params.preview_ui_addr)) || |
1121 !job_settings->GetInteger(printing::kPreviewRequestID, | 1292 !job_settings->GetInteger(printing::kPreviewRequestID, |
1122 &(settings.params.preview_request_id)) || | 1293 &(settings.params.preview_request_id)) || |
1123 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1294 !job_settings->GetBoolean(printing::kIsFirstRequest, |
1124 &(settings.params.is_first_request))) { | 1295 &(settings.params.is_first_request))) { |
1125 NOTREACHED(); | 1296 NOTREACHED(); |
1126 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1297 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1127 return false; | 1298 return false; |
1128 } | 1299 } |
1129 | 1300 |
1130 // Margins: Send default page layout to browser process. | 1301 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
1131 PageSizeMargins default_page_layout; | 1302 UpdateFrameMarginsCssInfo(*job_settings); |
1132 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, | 1303 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 | 1304 |
1140 // Header/Footer: Set |header_footer_info_|. | 1305 // Header/Footer: Set |header_footer_info_|. |
1141 if (settings.params.display_header_footer) { | 1306 if (settings.params.display_header_footer) { |
1142 header_footer_info_.reset(new DictionaryValue()); | 1307 header_footer_info_.reset(new DictionaryValue()); |
1143 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1308 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
1144 settings.params.date); | 1309 settings.params.date); |
1145 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1310 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
1146 settings.params.url); | 1311 settings.params.url); |
1147 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1312 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
1148 settings.params.title); | 1313 settings.params.title); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1366 node_ = web_node; | 1531 node_ = web_node; |
1367 } | 1532 } |
1368 | 1533 |
1369 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 1534 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
1370 DCHECK_EQ(INITIALIZED, state_); | 1535 DCHECK_EQ(INITIALIZED, state_); |
1371 ClearContext(); | 1536 ClearContext(); |
1372 } | 1537 } |
1373 | 1538 |
1374 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( | 1539 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
1375 PrintMsg_Print_Params* print_params, | 1540 PrintMsg_Print_Params* print_params, |
1376 const std::vector<int>& pages) { | 1541 const std::vector<int>& pages, |
| 1542 bool ignore_css_margins, |
| 1543 bool fit_to_page) { |
1377 DCHECK_EQ(INITIALIZED, state_); | 1544 DCHECK_EQ(INITIALIZED, state_); |
1378 state_ = RENDERING; | 1545 state_ = RENDERING; |
1379 | 1546 |
1380 metafile_.reset(new printing::PreviewMetafile); | 1547 metafile_.reset(new printing::PreviewMetafile); |
1381 if (!metafile_->Init()) { | 1548 if (!metafile_->Init()) { |
1382 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); | 1549 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); |
1383 LOG(ERROR) << "PreviewMetafile Init failed"; | 1550 LOG(ERROR) << "PreviewMetafile Init failed"; |
1384 return false; | 1551 return false; |
1385 } | 1552 } |
1386 | 1553 |
1387 // Need to make sure old object gets destroyed first. | 1554 // Need to make sure old object gets destroyed first. |
1388 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), | 1555 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
1389 node())); | 1556 node())); |
1390 UpdatePrintableSizeInPrintParameters(frame_, node_, | 1557 UpdateFrameAndViewFromCssPageLayout(frame_, node_, prep_frame_view_.get(), |
1391 prep_frame_view_.get(), print_params); | 1558 *print_params, ignore_css_margins, |
| 1559 fit_to_page); |
1392 | 1560 |
1393 print_params_.reset(new PrintMsg_Print_Params(*print_params)); | 1561 print_params_.reset(new PrintMsg_Print_Params(*print_params)); |
1394 | 1562 |
1395 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1563 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
1396 if (total_page_count_ == 0) { | 1564 if (total_page_count_ == 0) { |
1397 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; | 1565 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; |
1398 set_error(PREVIEW_ERROR_ZERO_PAGES); | 1566 set_error(PREVIEW_ERROR_ZERO_PAGES); |
1399 return false; | 1567 return false; |
1400 } | 1568 } |
1401 | 1569 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1557 DCHECK(IsRendering()); | 1725 DCHECK(IsRendering()); |
1558 return prep_frame_view_->GetPrintCanvasSize(); | 1726 return prep_frame_view_->GetPrintCanvasSize(); |
1559 } | 1727 } |
1560 | 1728 |
1561 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1729 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1562 prep_frame_view_.reset(); | 1730 prep_frame_view_.reset(); |
1563 metafile_.reset(); | 1731 metafile_.reset(); |
1564 pages_to_render_.clear(); | 1732 pages_to_render_.clear(); |
1565 error_ = PREVIEW_ERROR_NONE; | 1733 error_ = PREVIEW_ERROR_NONE; |
1566 } | 1734 } |
OLD | NEW |