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.printable_size.IsEmpty() && | 103 !params.min_shrink && !params.dpi && params.printable_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.printable_size == newParams.params.printable_size && | 111 return oldParams.params.printable_size == newParams.params.printable_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 |
138 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, | 140 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, |
139 gfx::Size* result) { | 141 gfx::Size* result) { |
140 int dpi = GetDPI(&print_params); | 142 int dpi = GetDPI(&print_params); |
141 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, | 143 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, |
142 print_params.desired_dpi)); | 144 print_params.desired_dpi)); |
143 | 145 |
144 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, | 146 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, |
145 print_params.desired_dpi)); | 147 print_params.desired_dpi)); |
146 } | 148 } |
147 | 149 |
148 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { | 150 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { |
149 if (!node.isNull()) | 151 if (!node.isNull()) |
150 return true; | 152 return true; |
151 std::string mime(frame->dataSource()->response().mimeType().utf8()); | 153 std::string mime(frame->dataSource()->response().mimeType().utf8()); |
152 return mime == "application/pdf"; | 154 return mime == "application/pdf"; |
153 } | 155 } |
154 | 156 |
157 bool PrintingFrameHasPageSizeStyle(WebFrame* frame) { | |
158 if (!frame) | |
159 return false; | |
160 return frame->hasCustomPageSizeStyle(0); | |
161 } | |
162 | |
155 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { | 163 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { |
156 if (frame->isPrintScalingDisabledForPlugin(node)) | 164 if (frame->isPrintScalingDisabledForPlugin(node)) |
157 return printing::NO_MARGINS; | 165 return printing::NO_MARGINS; |
158 else | 166 else |
159 return printing::PRINTABLE_AREA_MARGINS; | 167 return printing::PRINTABLE_AREA_MARGINS; |
160 } | 168 } |
161 | 169 |
162 // Get the (x, y) coordinate from where printing of the current text should | 170 // Get the (x, y) coordinate from where printing of the current text should |
163 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and | 171 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and |
164 // vertical alignment (TOP, BOTTOM). | 172 // vertical alignment (TOP, BOTTOM). |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
433 web_frame->setScrollOffset(prev_scroll_offset_); | 441 web_frame->setScrollOffset(prev_scroll_offset_); |
434 } | 442 } |
435 } | 443 } |
436 | 444 |
437 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) | 445 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) |
438 : content::RenderViewObserver(render_view), | 446 : content::RenderViewObserver(render_view), |
439 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), | 447 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), |
440 print_web_view_(NULL), | 448 print_web_view_(NULL), |
441 is_preview_enabled_(switches::IsPrintPreviewEnabled()), | 449 is_preview_enabled_(switches::IsPrintPreviewEnabled()), |
442 is_print_ready_metafile_sent_(false), | 450 is_print_ready_metafile_sent_(false), |
451 ignore_frame_margins_css_(false), | |
452 fit_to_page_(true), | |
443 user_cancelled_scripted_print_count_(0), | 453 user_cancelled_scripted_print_count_(0), |
444 notify_browser_of_print_failure_(true) { | 454 notify_browser_of_print_failure_(true) { |
445 } | 455 } |
446 | 456 |
447 PrintWebViewHelper::~PrintWebViewHelper() {} | 457 PrintWebViewHelper::~PrintWebViewHelper() {} |
448 | 458 |
449 // Prints |frame| which called window.print(). | 459 // Prints |frame| which called window.print(). |
450 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { | 460 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { |
451 DCHECK(frame); | 461 DCHECK(frame); |
452 | 462 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
553 void PrintWebViewHelper::OnPrintForSystemDialog() { | 563 void PrintWebViewHelper::OnPrintForSystemDialog() { |
554 WebFrame* frame = print_preview_context_.frame(); | 564 WebFrame* frame = print_preview_context_.frame(); |
555 if (!frame) { | 565 if (!frame) { |
556 NOTREACHED(); | 566 NOTREACHED(); |
557 return; | 567 return; |
558 } | 568 } |
559 | 569 |
560 Print(frame, print_preview_context_.node()); | 570 Print(frame, print_preview_context_.node()); |
561 } | 571 } |
562 | 572 |
573 void PrintWebViewHelper::UpdateFrameMarginsCssInfo( | |
574 const DictionaryValue& settings) { | |
575 int margins_type = 0; | |
576 if (!settings.GetInteger(printing::kSettingMarginsType, &margins_type)) | |
577 margins_type = printing::DEFAULT_MARGINS; | |
578 ignore_frame_margins_css_ = margins_type != printing::DEFAULT_MARGINS; | |
579 } | |
580 | |
581 bool PrintWebViewHelper::IsPrintToPdfRequested( | |
582 const DictionaryValue& job_settings) { | |
583 bool print_to_pdf = false; | |
584 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) | |
585 NOTREACHED(); | |
586 return print_to_pdf; | |
587 } | |
588 | |
563 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 589 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
564 DCHECK(is_preview_enabled_); | 590 DCHECK(is_preview_enabled_); |
565 print_preview_context_.OnPrintPreview(); | 591 print_preview_context_.OnPrintPreview(); |
566 | 592 |
567 if (!UpdatePrintSettings(print_preview_context_.frame(), | 593 if (!UpdatePrintSettings(print_preview_context_.frame(), |
568 print_preview_context_.node(), settings, false)) { | 594 print_preview_context_.node(), settings, false)) { |
569 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 595 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
570 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 596 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
571 routing_id(), print_pages_params_->params.document_cookie)); | 597 routing_id(), print_pages_params_->params.document_cookie)); |
572 notify_browser_of_print_failure_ = false; // Already sent. | 598 notify_browser_of_print_failure_ = false; // Already sent. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
613 } else { | 639 } else { |
614 if (notify_browser_of_print_failure_) | 640 if (notify_browser_of_print_failure_) |
615 LOG(ERROR) << "CreatePreviewDocument failed"; | 641 LOG(ERROR) << "CreatePreviewDocument failed"; |
616 DidFinishPrinting(FAIL_PREVIEW); | 642 DidFinishPrinting(FAIL_PREVIEW); |
617 } | 643 } |
618 } | 644 } |
619 | 645 |
620 bool PrintWebViewHelper::CreatePreviewDocument() { | 646 bool PrintWebViewHelper::CreatePreviewDocument() { |
621 PrintMsg_Print_Params print_params = print_pages_params_->params; | 647 PrintMsg_Print_Params print_params = print_pages_params_->params; |
622 const std::vector<int>& pages = print_pages_params_->pages; | 648 const std::vector<int>& pages = print_pages_params_->pages; |
623 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) | 649 if (!print_preview_context_.CreatePreviewDocument( |
650 &print_params, pages, ignore_frame_margins_css_, | |
651 fit_to_page_)) { | |
624 return false; | 652 return false; |
653 } | |
654 | |
655 PageSizeMargins default_page_layout; | |
656 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), 0, print_params, | |
657 ignore_frame_margins_css_, fit_to_page_, NULL, &default_page_layout); | |
658 if (!old_print_pages_params_.get() || | |
659 !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) { | |
660 bool has_page_size_style = PrintingFrameHasPageSizeStyle( | |
661 print_preview_context_.frame()); | |
662 // Margins: Send default page layout to browser process. | |
663 Send(new PrintHostMsg_DidGetDefaultPageLayout( | |
664 routing_id(), default_page_layout, has_page_size_style)); | |
665 } | |
666 | |
625 PrintHostMsg_DidGetPreviewPageCount_Params params; | 667 PrintHostMsg_DidGetPreviewPageCount_Params params; |
626 params.page_count = print_preview_context_.total_page_count(); | 668 params.page_count = print_preview_context_.total_page_count(); |
627 params.is_modifiable = print_preview_context_.IsModifiable(); | 669 params.is_modifiable = print_preview_context_.IsModifiable(); |
628 params.document_cookie = print_pages_params_->params.document_cookie; | 670 params.document_cookie = print_pages_params_->params.document_cookie; |
629 params.preview_request_id = print_pages_params_->params.preview_request_id; | 671 params.preview_request_id = print_pages_params_->params.preview_request_id; |
630 params.clear_preview_data = print_preview_context_.generate_draft_pages(); | 672 params.clear_preview_data = print_preview_context_.generate_draft_pages(); |
631 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); | 673 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); |
632 if (CheckForCancel()) | 674 if (CheckForCancel()) |
633 return false; | 675 return false; |
634 | 676 |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
835 return true; | 877 return true; |
836 } | 878 } |
837 | 879 |
838 #if defined(OS_MACOSX) || defined(OS_WIN) | 880 #if defined(OS_MACOSX) || defined(OS_WIN) |
839 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, | 881 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, |
840 WebFrame* frame, | 882 WebFrame* frame, |
841 const WebNode& node) { | 883 const WebNode& node) { |
842 PrintMsg_Print_Params print_params = params.params; | 884 PrintMsg_Print_Params print_params = params.params; |
843 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); | 885 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); |
844 UpdatePrintableSizeInPrintParameters(frame, node, &prep_frame_view, | 886 UpdatePrintableSizeInPrintParameters(frame, node, &prep_frame_view, |
845 &print_params); | 887 print_params, ignore_frame_margins_css_, |
888 fit_to_page_); | |
846 | 889 |
847 int page_count = prep_frame_view.GetExpectedPageCount(); | 890 int page_count = prep_frame_view.GetExpectedPageCount(); |
848 if (!page_count) | 891 if (!page_count) |
849 return false; | 892 return false; |
850 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 893 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
851 print_params.document_cookie, | 894 print_params.document_cookie, |
852 page_count)); | 895 page_count)); |
853 | 896 |
854 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 897 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
855 PrintMsg_PrintPage_Params page_params; | 898 PrintMsg_PrintPage_Params page_params; |
(...skipping 15 matching lines...) Expand all Loading... | |
871 } | 914 } |
872 #endif // OS_MACOSX || OS_WIN | 915 #endif // OS_MACOSX || OS_WIN |
873 | 916 |
874 void PrintWebViewHelper::didStopLoading() { | 917 void PrintWebViewHelper::didStopLoading() { |
875 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); | 918 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); |
876 DCHECK(params != NULL); | 919 DCHECK(params != NULL); |
877 PrintPages(*params, print_web_view_->mainFrame(), WebNode()); | 920 PrintPages(*params, print_web_view_->mainFrame(), WebNode()); |
878 } | 921 } |
879 | 922 |
880 // static - Not anonymous so that platform implementations can use it. | 923 // static - Not anonymous so that platform implementations can use it. |
881 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( | 924 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( |
vandebo (ex-Chrome)
2011/12/04 22:20:34
The variable names in this method get confusing be
kmadhusu
2011/12/05 09:06:54
Done.
| |
882 WebFrame* frame, | 925 WebFrame* frame, |
883 int page_index, | 926 int page_index, |
884 const PrintMsg_Print_Params& default_params, | 927 const PrintMsg_Print_Params& default_params, |
928 bool ignore_frame_margins_css, | |
929 bool fit_to_page, | |
930 double* scale_factor, | |
885 PageSizeMargins* page_layout_in_points) { | 931 PageSizeMargins* page_layout_in_points) { |
886 int dpi = GetDPI(&default_params); | 932 int dpi = GetDPI(&default_params); |
887 | 933 |
888 WebSize page_size_in_pixels( | 934 WebSize page_size_in_pixels( |
889 ConvertUnit(default_params.page_size.width(), | 935 ConvertUnit(default_params.page_size.width(), |
890 dpi, printing::kPixelsPerInch), | 936 dpi, printing::kPixelsPerInch), |
891 ConvertUnit(default_params.page_size.height(), | 937 ConvertUnit(default_params.page_size.height(), |
892 dpi, printing::kPixelsPerInch)); | 938 dpi, printing::kPixelsPerInch)); |
893 int margin_top_in_pixels = ConvertUnit( | 939 int margin_top_in_pixels = ConvertUnit( |
894 default_params.margin_top, | 940 default_params.margin_top, |
895 dpi, printing::kPixelsPerInch); | 941 dpi, printing::kPixelsPerInch); |
896 int margin_right_in_pixels = ConvertUnit( | 942 int margin_right_in_pixels = ConvertUnit( |
897 default_params.page_size.width() - | 943 default_params.page_size.width() - |
898 default_params.printable_size.width() - default_params.margin_left, | 944 default_params.printable_size.width() - default_params.margin_left, |
899 dpi, printing::kPixelsPerInch); | 945 dpi, printing::kPixelsPerInch); |
900 int margin_bottom_in_pixels = ConvertUnit( | 946 int margin_bottom_in_pixels = ConvertUnit( |
901 default_params.page_size.height() - | 947 default_params.page_size.height() - |
902 default_params.printable_size.height() - default_params.margin_top, | 948 default_params.printable_size.height() - default_params.margin_top, |
903 dpi, printing::kPixelsPerInch); | 949 dpi, printing::kPixelsPerInch); |
904 int margin_left_in_pixels = ConvertUnit( | 950 int margin_left_in_pixels = ConvertUnit( |
905 default_params.margin_left, | 951 default_params.margin_left, |
906 dpi, printing::kPixelsPerInch); | 952 dpi, printing::kPixelsPerInch); |
907 | 953 |
908 if (frame) { | 954 if (frame && !(ignore_frame_margins_css && fit_to_page)) { |
955 int old_margin_top_in_pixels = margin_top_in_pixels; | |
956 int old_margin_bottom_in_pixels = margin_bottom_in_pixels; | |
957 int old_margin_left_in_pixels = margin_left_in_pixels; | |
958 int old_margin_right_in_pixels = margin_right_in_pixels; | |
959 | |
909 frame->pageSizeAndMarginsInPixels(page_index, | 960 frame->pageSizeAndMarginsInPixels(page_index, |
910 page_size_in_pixels, | 961 page_size_in_pixels, |
911 margin_top_in_pixels, | 962 margin_top_in_pixels, |
912 margin_right_in_pixels, | 963 margin_right_in_pixels, |
913 margin_bottom_in_pixels, | 964 margin_bottom_in_pixels, |
914 margin_left_in_pixels); | 965 margin_left_in_pixels); |
966 if (ignore_frame_margins_css) { | |
967 margin_top_in_pixels = old_margin_top_in_pixels; | |
968 margin_bottom_in_pixels = old_margin_bottom_in_pixels; | |
969 margin_left_in_pixels = old_margin_left_in_pixels; | |
970 margin_right_in_pixels = old_margin_right_in_pixels; | |
971 } | |
915 } | 972 } |
916 | 973 |
917 page_layout_in_points->content_width = | 974 double new_content_width = |
918 ConvertPixelsToPoint(page_size_in_pixels.width - | 975 ConvertPixelsToPoint(page_size_in_pixels.width - |
919 margin_left_in_pixels - | 976 margin_left_in_pixels - margin_right_in_pixels); |
vandebo (ex-Chrome)
2011/12/04 22:20:34
When we are ignoring the css margins, I think thes
kmadhusu
2011/12/05 09:06:54
Done.
When the selected printer is "PRINT_TO_PDF
| |
920 margin_right_in_pixels); | 977 double new_content_height = |
921 page_layout_in_points->content_height = | |
922 ConvertPixelsToPoint(page_size_in_pixels.height - | 978 ConvertPixelsToPoint(page_size_in_pixels.height - |
923 margin_top_in_pixels - | 979 margin_top_in_pixels - margin_bottom_in_pixels); |
924 margin_bottom_in_pixels); | 980 double new_page_box_width = |
981 ConvertPixelsToPoint(page_size_in_pixels.width); | |
982 double new_page_box_height= | |
983 ConvertPixelsToPoint(page_size_in_pixels.height); | |
984 | |
985 double default_page_size_height = | |
986 ConvertUnit(default_params.page_size.height(), dpi, | |
987 printing::kPointsPerInch); | |
988 double default_page_size_width = | |
989 ConvertUnit(default_params.page_size.width(), dpi, | |
990 printing::kPointsPerInch); | |
925 | 991 |
926 // Invalid page size and/or margins. We just use the default setting. | 992 // Invalid page size and/or margins. We just use the default setting. |
927 if (page_layout_in_points->content_width < 1.0 || | 993 if (new_content_width < 1.0 || new_content_height < 1.0) { |
928 page_layout_in_points->content_height < 1.0) { | |
929 CHECK(frame != NULL); | 994 CHECK(frame != NULL); |
930 GetPageSizeAndMarginsInPoints(NULL, page_index, default_params, | 995 GetPageSizeAndMarginsInPoints(NULL, page_index, default_params, false, |
931 page_layout_in_points); | 996 false, NULL, page_layout_in_points); |
932 return; | 997 return; |
933 } | 998 } |
999 double margin_top_in_points = | |
1000 ConvertPixelsToPointDouble(margin_top_in_pixels); | |
1001 double margin_right_in_points = | |
1002 ConvertPixelsToPointDouble(margin_right_in_pixels); | |
1003 double margin_left_in_points = | |
1004 ConvertPixelsToPointDouble(margin_left_in_pixels); | |
1005 double margin_bottom_in_points = | |
1006 ConvertPixelsToPointDouble(margin_bottom_in_pixels); | |
934 | 1007 |
935 page_layout_in_points->margin_top = | 1008 if (fit_to_page && (default_page_size_height != new_page_box_height || |
936 ConvertPixelsToPointDouble(margin_top_in_pixels); | 1009 default_page_size_width != new_page_box_width)) { |
937 page_layout_in_points->margin_right = | 1010 double factor = 1.0f; |
938 ConvertPixelsToPointDouble(margin_right_in_pixels); | 1011 if (default_page_size_width < new_page_box_width || |
939 page_layout_in_points->margin_bottom = | 1012 default_page_size_height < new_page_box_height) { |
940 ConvertPixelsToPointDouble(margin_bottom_in_pixels); | 1013 double minimum_printable_width = |
941 page_layout_in_points->margin_left = | 1014 ConvertUnit(default_params.printable_area.width(), dpi, |
942 ConvertPixelsToPointDouble(margin_left_in_pixels); | 1015 printing::kPointsPerInch); |
1016 double minimum_printable_height = | |
1017 ConvertUnit(default_params.printable_area.height(), dpi, | |
1018 printing::kPointsPerInch); | |
1019 double ratio_width = minimum_printable_width / new_page_box_width; | |
1020 double ratio_height = minimum_printable_height / new_page_box_height; | |
1021 factor = ratio_width < ratio_height ? ratio_width : ratio_height; | |
1022 if (scale_factor) | |
1023 *scale_factor = factor; | |
1024 | |
1025 new_content_width *= factor; | |
1026 new_content_height *= factor; | |
1027 } | |
1028 margin_top_in_points = | |
1029 (default_page_size_height - new_page_box_height * factor)/2 + | |
1030 (margin_top_in_points * factor); | |
1031 margin_bottom_in_points = | |
1032 (default_page_size_height - new_page_box_height * factor)/2 + | |
1033 (margin_bottom_in_points * factor); | |
1034 margin_right_in_points = | |
1035 (default_page_size_width - new_page_box_width * factor)/2 + | |
1036 (margin_right_in_points * factor); | |
1037 margin_left_in_points = | |
1038 (default_page_size_width - new_page_box_width * factor)/2 + | |
1039 (margin_left_in_points * factor); | |
1040 } | |
1041 page_layout_in_points->content_width = new_content_width; | |
1042 page_layout_in_points->content_height = new_content_height; | |
1043 page_layout_in_points->margin_top = margin_top_in_points; | |
1044 page_layout_in_points->margin_right = margin_right_in_points; | |
1045 page_layout_in_points->margin_bottom = margin_bottom_in_points; | |
1046 page_layout_in_points->margin_left = margin_left_in_points; | |
943 } | 1047 } |
944 | 1048 |
945 // static - Not anonymous so that platform implementations can use it. | 1049 // static - Not anonymous so that platform implementations can use it. |
946 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( | 1050 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
947 WebFrame* frame, | 1051 WebFrame* frame, |
948 const WebNode& node, | 1052 const WebNode& node, |
949 PrepareFrameAndViewForPrint* prepare, | 1053 PrepareFrameAndViewForPrint* prepare, |
950 PrintMsg_Print_Params* params) { | 1054 const PrintMsg_Print_Params& params, |
1055 bool ignore_frame_margins_css, | |
1056 bool fit_to_page) { | |
951 if (PrintingNodeOrPdfFrame(frame, node)) | 1057 if (PrintingNodeOrPdfFrame(frame, node)) |
952 return; | 1058 return; |
953 PageSizeMargins page_layout_in_points; | 1059 PageSizeMargins page_layout_in_points; |
954 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, | 1060 PrintMsg_Print_Params current_params = params; |
955 &page_layout_in_points); | 1061 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, current_params, |
956 int dpi = GetDPI(params); | 1062 ignore_frame_margins_css, false, NULL, &page_layout_in_points); |
957 params->printable_size = gfx::Size( | 1063 int dpi = GetDPI(¤t_params); |
1064 current_params.printable_size = gfx::Size( | |
958 static_cast<int>(ConvertUnitDouble( | 1065 static_cast<int>(ConvertUnitDouble( |
959 page_layout_in_points.content_width, | 1066 page_layout_in_points.content_width, |
960 printing::kPointsPerInch, dpi)), | 1067 printing::kPointsPerInch, dpi)), |
961 static_cast<int>(ConvertUnitDouble( | 1068 static_cast<int>(ConvertUnitDouble( |
962 page_layout_in_points.content_height, | 1069 page_layout_in_points.content_height, |
963 printing::kPointsPerInch, dpi))); | 1070 printing::kPointsPerInch, dpi))); |
964 | |
965 double page_width_in_points = | 1071 double page_width_in_points = |
966 page_layout_in_points.content_width + | 1072 page_layout_in_points.content_width + |
967 page_layout_in_points.margin_left + | 1073 page_layout_in_points.margin_left + |
968 page_layout_in_points.margin_right; | 1074 page_layout_in_points.margin_right; |
969 double page_height_in_points = | 1075 double page_height_in_points = |
970 page_layout_in_points.content_height + | 1076 page_layout_in_points.content_height + |
971 page_layout_in_points.margin_top + | 1077 page_layout_in_points.margin_top + |
972 page_layout_in_points.margin_bottom; | 1078 page_layout_in_points.margin_bottom; |
973 | 1079 |
974 params->page_size = gfx::Size( | 1080 current_params.page_size = gfx::Size( |
975 static_cast<int>(ConvertUnitDouble( | 1081 static_cast<int>(ConvertUnitDouble( |
976 page_width_in_points, printing::kPointsPerInch, dpi)), | 1082 page_width_in_points, printing::kPointsPerInch, dpi)), |
977 static_cast<int>(ConvertUnitDouble( | 1083 static_cast<int>(ConvertUnitDouble( |
978 page_height_in_points, printing::kPointsPerInch, dpi))); | 1084 page_height_in_points, printing::kPointsPerInch, dpi))); |
979 | 1085 current_params.margin_top = |
980 params->margin_top = static_cast<int>(ConvertUnitDouble( | 1086 static_cast<int>(ConvertUnitDouble( |
981 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); | 1087 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); |
982 params->margin_left = static_cast<int>(ConvertUnitDouble( | 1088 current_params.margin_left = static_cast<int>(ConvertUnitDouble( |
983 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); | 1089 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); |
984 | 1090 prepare->UpdatePrintParams(current_params); |
985 prepare->UpdatePrintParams(*params); | |
986 } | 1091 } |
987 | 1092 |
988 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, | 1093 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, |
989 const WebKit::WebNode& node) { | 1094 const WebKit::WebNode& node) { |
990 DCHECK(frame); | 1095 DCHECK(frame); |
991 PrintMsg_PrintPages_Params settings; | 1096 PrintMsg_PrintPages_Params settings; |
992 | 1097 |
993 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1098 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
994 &settings.params)); | 1099 &settings.params)); |
995 // Check if the printer returned any settings, if the settings is empty, we | 1100 // Check if the printer returned any settings, if the settings is empty, we |
(...skipping 23 matching lines...) Expand all Loading... | |
1019 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1124 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
1020 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1125 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
1021 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | 1126 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
1022 if (!InitPrintSettings(frame, node)) | 1127 if (!InitPrintSettings(frame, node)) |
1023 return false; | 1128 return false; |
1024 | 1129 |
1025 DCHECK(!prepare->get()); | 1130 DCHECK(!prepare->get()); |
1026 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1131 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
1027 frame, node)); | 1132 frame, node)); |
1028 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), | 1133 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), |
1029 &print_pages_params_->params); | 1134 print_pages_params_->params, |
1135 ignore_frame_margins_css_, | |
1136 fit_to_page_); | |
1030 Send(new PrintHostMsg_DidGetDocumentCookie( | 1137 Send(new PrintHostMsg_DidGetDocumentCookie( |
1031 routing_id(), print_pages_params_->params.document_cookie)); | 1138 routing_id(), print_pages_params_->params.document_cookie)); |
1032 return true; | 1139 return true; |
1033 } | 1140 } |
1034 | 1141 |
1035 bool PrintWebViewHelper::UpdatePrintSettings( | 1142 bool PrintWebViewHelper::UpdatePrintSettings( |
1036 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1143 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
1037 const DictionaryValue& passed_job_settings, bool print_for_preview) { | 1144 const DictionaryValue& passed_job_settings, bool print_for_preview) { |
1038 DCHECK(is_preview_enabled_); | 1145 DCHECK(is_preview_enabled_); |
1039 const DictionaryValue* job_settings = &passed_job_settings; | 1146 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)) || | 1227 &(settings.params.preview_ui_addr)) || |
1121 !job_settings->GetInteger(printing::kPreviewRequestID, | 1228 !job_settings->GetInteger(printing::kPreviewRequestID, |
1122 &(settings.params.preview_request_id)) || | 1229 &(settings.params.preview_request_id)) || |
1123 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1230 !job_settings->GetBoolean(printing::kIsFirstRequest, |
1124 &(settings.params.is_first_request))) { | 1231 &(settings.params.is_first_request))) { |
1125 NOTREACHED(); | 1232 NOTREACHED(); |
1126 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1233 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1127 return false; | 1234 return false; |
1128 } | 1235 } |
1129 | 1236 |
1130 // Margins: Send default page layout to browser process. | 1237 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
1131 PageSizeMargins default_page_layout; | 1238 UpdateFrameMarginsCssInfo(*job_settings); |
1132 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, | 1239 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 | 1240 |
1140 // Header/Footer: Set |header_footer_info_|. | 1241 // Header/Footer: Set |header_footer_info_|. |
1141 if (settings.params.display_header_footer) { | 1242 if (settings.params.display_header_footer) { |
1142 header_footer_info_.reset(new DictionaryValue()); | 1243 header_footer_info_.reset(new DictionaryValue()); |
1143 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1244 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
1144 settings.params.date); | 1245 settings.params.date); |
1145 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1246 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
1146 settings.params.url); | 1247 settings.params.url); |
1147 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1248 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
1148 settings.params.title); | 1249 settings.params.title); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1366 node_ = web_node; | 1467 node_ = web_node; |
1367 } | 1468 } |
1368 | 1469 |
1369 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 1470 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
1370 DCHECK_EQ(INITIALIZED, state_); | 1471 DCHECK_EQ(INITIALIZED, state_); |
1371 ClearContext(); | 1472 ClearContext(); |
1372 } | 1473 } |
1373 | 1474 |
1374 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( | 1475 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
1375 PrintMsg_Print_Params* print_params, | 1476 PrintMsg_Print_Params* print_params, |
1376 const std::vector<int>& pages) { | 1477 const std::vector<int>& pages, |
1478 bool ignore_frame_margins_css, | |
1479 bool fit_to_page) { | |
1377 DCHECK_EQ(INITIALIZED, state_); | 1480 DCHECK_EQ(INITIALIZED, state_); |
1378 state_ = RENDERING; | 1481 state_ = RENDERING; |
1379 | 1482 |
1380 metafile_.reset(new printing::PreviewMetafile); | 1483 metafile_.reset(new printing::PreviewMetafile); |
1381 if (!metafile_->Init()) { | 1484 if (!metafile_->Init()) { |
1382 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); | 1485 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); |
1383 LOG(ERROR) << "PreviewMetafile Init failed"; | 1486 LOG(ERROR) << "PreviewMetafile Init failed"; |
1384 return false; | 1487 return false; |
1385 } | 1488 } |
1386 | 1489 |
1387 // Need to make sure old object gets destroyed first. | 1490 // Need to make sure old object gets destroyed first. |
1388 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), | 1491 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
1389 node())); | 1492 node())); |
1390 UpdatePrintableSizeInPrintParameters(frame_, node_, | 1493 UpdatePrintableSizeInPrintParameters(frame_, node_, |
1391 prep_frame_view_.get(), print_params); | 1494 prep_frame_view_.get(), *print_params, |
1495 ignore_frame_margins_css, | |
1496 fit_to_page); | |
1392 | 1497 |
1393 print_params_.reset(new PrintMsg_Print_Params(*print_params)); | 1498 print_params_.reset(new PrintMsg_Print_Params(*print_params)); |
1394 | 1499 |
1395 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1500 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
1396 if (total_page_count_ == 0) { | 1501 if (total_page_count_ == 0) { |
1397 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; | 1502 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; |
1398 set_error(PREVIEW_ERROR_ZERO_PAGES); | 1503 set_error(PREVIEW_ERROR_ZERO_PAGES); |
1399 return false; | 1504 return false; |
1400 } | 1505 } |
1401 | 1506 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1557 DCHECK(IsRendering()); | 1662 DCHECK(IsRendering()); |
1558 return prep_frame_view_->GetPrintCanvasSize(); | 1663 return prep_frame_view_->GetPrintCanvasSize(); |
1559 } | 1664 } |
1560 | 1665 |
1561 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1666 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1562 prep_frame_view_.reset(); | 1667 prep_frame_view_.reset(); |
1563 metafile_.reset(); | 1668 metafile_.reset(); |
1564 pages_to_render_.clear(); | 1669 pages_to_render_.clear(); |
1565 error_ = PREVIEW_ERROR_NONE; | 1670 error_ = PREVIEW_ERROR_NONE; |
1566 } | 1671 } |
OLD | NEW |