Chromium Code Reviews| 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 19 matching lines...) Expand all Loading... | |
| 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( |
| 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); |
| 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_content_width = | |
| 986 ConvertUnit(default_params.printable_size.width(), | |
| 987 dpi, printing::kPointsPerInch); | |
| 988 double default_content_height = | |
| 989 ConvertUnit(default_params.printable_size.height(), | |
| 990 dpi, printing::kPointsPerInch); | |
| 991 | |
| 992 if (ignore_frame_margins_css && fit_to_page) { | |
| 993 new_content_width = default_content_width; | |
|
vandebo (ex-Chrome)
2011/12/03 02:01:45
I think you don't need these anymore, since you ad
kmadhusu
2011/12/04 01:55:38
Done.
| |
| 994 new_content_height = default_content_height; | |
| 995 new_page_box_width = default_content_width; | |
| 996 new_page_box_height = default_content_height; | |
| 997 } | |
| 925 | 998 |
| 926 // Invalid page size and/or margins. We just use the default setting. | 999 // Invalid page size and/or margins. We just use the default setting. |
| 927 if (page_layout_in_points->content_width < 1.0 || | 1000 if (new_content_width < 1.0 || new_content_height < 1.0) { |
| 928 page_layout_in_points->content_height < 1.0) { | |
| 929 CHECK(frame != NULL); | 1001 CHECK(frame != NULL); |
| 930 GetPageSizeAndMarginsInPoints(NULL, page_index, default_params, | 1002 GetPageSizeAndMarginsInPoints(NULL, page_index, default_params, false, |
| 931 page_layout_in_points); | 1003 false, NULL, page_layout_in_points); |
| 932 return; | 1004 return; |
| 933 } | 1005 } |
| 1006 double margin_top_in_points = | |
| 1007 ConvertPixelsToPointDouble(margin_top_in_pixels); | |
| 1008 double margin_right_in_points = | |
| 1009 ConvertPixelsToPointDouble(margin_right_in_pixels); | |
| 1010 double margin_left_in_points = | |
| 1011 ConvertPixelsToPointDouble(margin_left_in_pixels); | |
| 1012 double margin_bottom_in_points = | |
| 1013 ConvertPixelsToPointDouble(margin_bottom_in_pixels); | |
| 934 | 1014 |
| 935 page_layout_in_points->margin_top = | 1015 if (fit_to_page && (default_content_height != new_page_box_height || |
|
vandebo (ex-Chrome)
2011/12/03 02:01:45
I don't think we want to compare the content heigh
kmadhusu
2011/12/04 01:55:38
Done.
| |
| 936 ConvertPixelsToPointDouble(margin_top_in_pixels); | 1016 default_content_width != new_page_box_width)) { |
| 937 page_layout_in_points->margin_right = | 1017 double default_page_size_height = |
| 938 ConvertPixelsToPointDouble(margin_right_in_pixels); | 1018 ConvertUnit(default_params.page_size.height(), dpi, |
| 939 page_layout_in_points->margin_bottom = | 1019 printing::kPointsPerInch); |
| 940 ConvertPixelsToPointDouble(margin_bottom_in_pixels); | 1020 double default_page_size_width = |
| 941 page_layout_in_points->margin_left = | 1021 ConvertUnit(default_params.page_size.width(), dpi, |
| 942 ConvertPixelsToPointDouble(margin_left_in_pixels); | 1022 printing::kPointsPerInch); |
| 1023 if (default_content_width < new_page_box_width || | |
| 1024 default_content_height < new_page_box_height) { | |
| 1025 double minimum_printable_width = | |
| 1026 ConvertUnit(default_params.printable_area.width(), dpi, | |
| 1027 printing::kPointsPerInch); | |
| 1028 double minimum_printable_height = | |
| 1029 ConvertUnit(default_params.printable_area.height(), dpi, | |
| 1030 printing::kPointsPerInch); | |
| 1031 double ratio_width = minimum_printable_width / new_page_box_width; | |
| 1032 double ratio_height = minimum_printable_height / new_page_box_height; | |
| 1033 double factor = ratio_width < ratio_height ? ratio_width : ratio_height; | |
| 1034 if (scale_factor) | |
|
vandebo (ex-Chrome)
2011/12/03 02:01:45
There are probably some edge cases here where scal
kmadhusu
2011/12/04 01:55:38
When you say "scale factor ends up being greater t
| |
| 1035 *scale_factor = factor; | |
| 1036 | |
| 1037 new_content_width *= factor; | |
| 1038 new_content_height *= factor; | |
| 1039 margin_top_in_points = | |
| 1040 (default_page_size_height - new_page_box_height * factor)/2 + | |
| 1041 (margin_top_in_points * factor); | |
| 1042 margin_bottom_in_points = | |
| 1043 (default_page_size_height - new_page_box_height * factor)/2 + | |
| 1044 (margin_bottom_in_points * factor); | |
| 1045 margin_right_in_points = | |
| 1046 (default_page_size_width - new_page_box_width * factor)/2 + | |
| 1047 (margin_right_in_points * factor); | |
| 1048 margin_left_in_points = | |
| 1049 (default_page_size_width - new_page_box_width * factor)/2 + | |
| 1050 (margin_left_in_points * factor); | |
| 1051 } else { | |
| 1052 double default_margin_left_in_points = | |
| 1053 ConvertUnit(default_params.margin_left, dpi, | |
| 1054 printing::kPointsPerInch); | |
| 1055 double default_margin_top_in_points = | |
| 1056 ConvertUnit(default_params.margin_top, dpi, | |
| 1057 printing::kPointsPerInch); | |
| 1058 double default_margin_right_in_points = | |
| 1059 default_page_size_width - default_content_width - | |
| 1060 default_margin_left_in_points; | |
| 1061 double default_margin_bottom_in_points = | |
| 1062 default_page_size_height - default_content_height - | |
| 1063 default_margin_top_in_points; | |
| 1064 | |
| 1065 margin_top_in_points = | |
| 1066 (default_content_height - new_page_box_height)/2 + | |
|
vandebo (ex-Chrome)
2011/12/03 02:01:45
Looking at this in code, it looks like just a vari
kmadhusu
2011/12/04 01:55:38
Done.
| |
| 1067 margin_top_in_points + default_margin_top_in_points; | |
| 1068 margin_bottom_in_points = | |
| 1069 (default_content_height - new_page_box_height)/2 + | |
| 1070 margin_bottom_in_points + default_margin_bottom_in_points; | |
| 1071 margin_right_in_points = | |
| 1072 (default_content_width - new_page_box_width)/2 + | |
| 1073 margin_right_in_points + default_margin_right_in_points; | |
| 1074 margin_left_in_points = | |
| 1075 (default_content_width - new_page_box_width)/2 + | |
| 1076 margin_left_in_points + default_margin_left_in_points; | |
| 1077 } | |
| 1078 } | |
| 1079 page_layout_in_points->content_width = new_content_width; | |
| 1080 page_layout_in_points->content_height = new_content_height; | |
| 1081 page_layout_in_points->margin_top = margin_top_in_points; | |
| 1082 page_layout_in_points->margin_right = margin_right_in_points; | |
| 1083 page_layout_in_points->margin_bottom = margin_bottom_in_points; | |
| 1084 page_layout_in_points->margin_left = margin_left_in_points; | |
| 943 } | 1085 } |
| 944 | 1086 |
| 945 // static - Not anonymous so that platform implementations can use it. | 1087 // static - Not anonymous so that platform implementations can use it. |
| 946 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( | 1088 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
| 947 WebFrame* frame, | 1089 WebFrame* frame, |
| 948 const WebNode& node, | 1090 const WebNode& node, |
| 949 PrepareFrameAndViewForPrint* prepare, | 1091 PrepareFrameAndViewForPrint* prepare, |
| 950 PrintMsg_Print_Params* params) { | 1092 const PrintMsg_Print_Params& params, |
| 1093 bool ignore_frame_margins_css, | |
| 1094 bool fit_to_page) { | |
| 951 if (PrintingNodeOrPdfFrame(frame, node)) | 1095 if (PrintingNodeOrPdfFrame(frame, node)) |
| 952 return; | 1096 return; |
| 953 PageSizeMargins page_layout_in_points; | 1097 PageSizeMargins page_layout_in_points; |
| 954 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, | 1098 PrintMsg_Print_Params current_params = params; |
| 955 &page_layout_in_points); | 1099 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, current_params, |
| 956 int dpi = GetDPI(params); | 1100 ignore_frame_margins_css, false, NULL, &page_layout_in_points); |
| 957 params->printable_size = gfx::Size( | 1101 int dpi = GetDPI(¤t_params); |
| 1102 current_params.printable_size = gfx::Size( | |
| 958 static_cast<int>(ConvertUnitDouble( | 1103 static_cast<int>(ConvertUnitDouble( |
| 959 page_layout_in_points.content_width, | 1104 page_layout_in_points.content_width, |
| 960 printing::kPointsPerInch, dpi)), | 1105 printing::kPointsPerInch, dpi)), |
| 961 static_cast<int>(ConvertUnitDouble( | 1106 static_cast<int>(ConvertUnitDouble( |
| 962 page_layout_in_points.content_height, | 1107 page_layout_in_points.content_height, |
| 963 printing::kPointsPerInch, dpi))); | 1108 printing::kPointsPerInch, dpi))); |
| 964 | |
| 965 double page_width_in_points = | 1109 double page_width_in_points = |
| 966 page_layout_in_points.content_width + | 1110 page_layout_in_points.content_width + |
| 967 page_layout_in_points.margin_left + | 1111 page_layout_in_points.margin_left + |
| 968 page_layout_in_points.margin_right; | 1112 page_layout_in_points.margin_right; |
| 969 double page_height_in_points = | 1113 double page_height_in_points = |
| 970 page_layout_in_points.content_height + | 1114 page_layout_in_points.content_height + |
| 971 page_layout_in_points.margin_top + | 1115 page_layout_in_points.margin_top + |
| 972 page_layout_in_points.margin_bottom; | 1116 page_layout_in_points.margin_bottom; |
| 973 | 1117 |
| 974 params->page_size = gfx::Size( | 1118 current_params.page_size = gfx::Size( |
| 975 static_cast<int>(ConvertUnitDouble( | 1119 static_cast<int>(ConvertUnitDouble( |
| 976 page_width_in_points, printing::kPointsPerInch, dpi)), | 1120 page_width_in_points, printing::kPointsPerInch, dpi)), |
| 977 static_cast<int>(ConvertUnitDouble( | 1121 static_cast<int>(ConvertUnitDouble( |
| 978 page_height_in_points, printing::kPointsPerInch, dpi))); | 1122 page_height_in_points, printing::kPointsPerInch, dpi))); |
| 979 | 1123 current_params.margin_top = |
| 980 params->margin_top = static_cast<int>(ConvertUnitDouble( | 1124 static_cast<int>(ConvertUnitDouble( |
| 981 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); | 1125 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); |
| 982 params->margin_left = static_cast<int>(ConvertUnitDouble( | 1126 current_params.margin_left = static_cast<int>(ConvertUnitDouble( |
| 983 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); | 1127 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); |
| 984 | 1128 prepare->UpdatePrintParams(current_params); |
| 985 prepare->UpdatePrintParams(*params); | |
| 986 } | 1129 } |
| 987 | 1130 |
| 988 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, | 1131 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, |
| 989 const WebKit::WebNode& node) { | 1132 const WebKit::WebNode& node) { |
| 990 DCHECK(frame); | 1133 DCHECK(frame); |
| 991 PrintMsg_PrintPages_Params settings; | 1134 PrintMsg_PrintPages_Params settings; |
| 992 | 1135 |
| 993 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1136 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
| 994 &settings.params)); | 1137 &settings.params)); |
| 995 // Check if the printer returned any settings, if the settings is empty, we | 1138 // Check if the printer returned any settings, if the settings is empty, we |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 1019 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1162 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
| 1020 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1163 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
| 1021 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | 1164 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
| 1022 if (!InitPrintSettings(frame, node)) | 1165 if (!InitPrintSettings(frame, node)) |
| 1023 return false; | 1166 return false; |
| 1024 | 1167 |
| 1025 DCHECK(!prepare->get()); | 1168 DCHECK(!prepare->get()); |
| 1026 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1169 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
| 1027 frame, node)); | 1170 frame, node)); |
| 1028 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), | 1171 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), |
| 1029 &print_pages_params_->params); | 1172 print_pages_params_->params, |
| 1173 ignore_frame_margins_css_, | |
| 1174 fit_to_page_); | |
| 1030 Send(new PrintHostMsg_DidGetDocumentCookie( | 1175 Send(new PrintHostMsg_DidGetDocumentCookie( |
| 1031 routing_id(), print_pages_params_->params.document_cookie)); | 1176 routing_id(), print_pages_params_->params.document_cookie)); |
| 1032 return true; | 1177 return true; |
| 1033 } | 1178 } |
| 1034 | 1179 |
| 1035 bool PrintWebViewHelper::UpdatePrintSettings( | 1180 bool PrintWebViewHelper::UpdatePrintSettings( |
| 1036 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1181 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
| 1037 const DictionaryValue& passed_job_settings, bool print_for_preview) { | 1182 const DictionaryValue& passed_job_settings, bool print_for_preview) { |
| 1038 DCHECK(is_preview_enabled_); | 1183 DCHECK(is_preview_enabled_); |
| 1039 const DictionaryValue* job_settings = &passed_job_settings; | 1184 const DictionaryValue* job_settings = &passed_job_settings; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1074 #endif | 1219 #endif |
| 1075 | 1220 |
| 1076 printing::MarginType margin_type = printing::NO_MARGINS; | 1221 printing::MarginType margin_type = printing::NO_MARGINS; |
| 1077 if (get_margins_from_pdf) | 1222 if (get_margins_from_pdf) |
| 1078 margin_type = GetMarginsForPdf(frame, node); | 1223 margin_type = GetMarginsForPdf(frame, node); |
| 1079 modified_job_settings.SetInteger(printing::kSettingMarginsType, | 1224 modified_job_settings.SetInteger(printing::kSettingMarginsType, |
| 1080 margin_type); | 1225 margin_type); |
| 1081 job_settings = &modified_job_settings; | 1226 job_settings = &modified_job_settings; |
| 1082 } | 1227 } |
| 1083 | 1228 |
| 1229 | |
| 1084 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when | 1230 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when |
| 1085 // possible. | 1231 // possible. |
| 1086 int cookie = print_pages_params_.get() ? | 1232 int cookie = print_pages_params_.get() ? |
| 1087 print_pages_params_->params.document_cookie : 0; | 1233 print_pages_params_->params.document_cookie : 0; |
| 1088 PrintMsg_PrintPages_Params settings; | 1234 PrintMsg_PrintPages_Params settings; |
| 1089 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 1235 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
| 1090 cookie, *job_settings, &settings)); | 1236 cookie, *job_settings, &settings)); |
| 1091 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1237 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 1092 | 1238 |
| 1093 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { | 1239 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 1120 &(settings.params.preview_ui_addr)) || | 1266 &(settings.params.preview_ui_addr)) || |
| 1121 !job_settings->GetInteger(printing::kPreviewRequestID, | 1267 !job_settings->GetInteger(printing::kPreviewRequestID, |
| 1122 &(settings.params.preview_request_id)) || | 1268 &(settings.params.preview_request_id)) || |
| 1123 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1269 !job_settings->GetBoolean(printing::kIsFirstRequest, |
| 1124 &(settings.params.is_first_request))) { | 1270 &(settings.params.is_first_request))) { |
| 1125 NOTREACHED(); | 1271 NOTREACHED(); |
| 1126 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1272 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
| 1127 return false; | 1273 return false; |
| 1128 } | 1274 } |
| 1129 | 1275 |
| 1130 // Margins: Send default page layout to browser process. | 1276 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
| 1131 PageSizeMargins default_page_layout; | 1277 UpdateFrameMarginsCssInfo(*job_settings); |
| 1132 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, | 1278 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 | 1279 |
| 1140 // Header/Footer: Set |header_footer_info_|. | 1280 // Header/Footer: Set |header_footer_info_|. |
| 1141 if (settings.params.display_header_footer) { | 1281 if (settings.params.display_header_footer) { |
| 1142 header_footer_info_.reset(new DictionaryValue()); | 1282 header_footer_info_.reset(new DictionaryValue()); |
| 1143 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1283 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
| 1144 settings.params.date); | 1284 settings.params.date); |
| 1145 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1285 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
| 1146 settings.params.url); | 1286 settings.params.url); |
| 1147 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1287 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
| 1148 settings.params.title); | 1288 settings.params.title); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1366 node_ = web_node; | 1506 node_ = web_node; |
| 1367 } | 1507 } |
| 1368 | 1508 |
| 1369 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 1509 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
| 1370 DCHECK_EQ(INITIALIZED, state_); | 1510 DCHECK_EQ(INITIALIZED, state_); |
| 1371 ClearContext(); | 1511 ClearContext(); |
| 1372 } | 1512 } |
| 1373 | 1513 |
| 1374 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( | 1514 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
| 1375 PrintMsg_Print_Params* print_params, | 1515 PrintMsg_Print_Params* print_params, |
| 1376 const std::vector<int>& pages) { | 1516 const std::vector<int>& pages, |
| 1517 bool ignore_frame_margins_css, | |
| 1518 bool fit_to_page) { | |
| 1377 DCHECK_EQ(INITIALIZED, state_); | 1519 DCHECK_EQ(INITIALIZED, state_); |
| 1378 state_ = RENDERING; | 1520 state_ = RENDERING; |
| 1379 | 1521 |
| 1380 metafile_.reset(new printing::PreviewMetafile); | 1522 metafile_.reset(new printing::PreviewMetafile); |
| 1381 if (!metafile_->Init()) { | 1523 if (!metafile_->Init()) { |
| 1382 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); | 1524 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); |
| 1383 LOG(ERROR) << "PreviewMetafile Init failed"; | 1525 LOG(ERROR) << "PreviewMetafile Init failed"; |
| 1384 return false; | 1526 return false; |
| 1385 } | 1527 } |
| 1386 | 1528 |
| 1387 // Need to make sure old object gets destroyed first. | 1529 // Need to make sure old object gets destroyed first. |
| 1388 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), | 1530 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
| 1389 node())); | 1531 node())); |
| 1390 UpdatePrintableSizeInPrintParameters(frame_, node_, | 1532 UpdatePrintableSizeInPrintParameters(frame_, node_, |
| 1391 prep_frame_view_.get(), print_params); | 1533 prep_frame_view_.get(), *print_params, |
| 1534 ignore_frame_margins_css, | |
| 1535 fit_to_page); | |
| 1392 | 1536 |
| 1393 print_params_.reset(new PrintMsg_Print_Params(*print_params)); | 1537 print_params_.reset(new PrintMsg_Print_Params(*print_params)); |
| 1394 | 1538 |
| 1395 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1539 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
| 1396 if (total_page_count_ == 0) { | 1540 if (total_page_count_ == 0) { |
| 1397 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; | 1541 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; |
| 1398 set_error(PREVIEW_ERROR_ZERO_PAGES); | 1542 set_error(PREVIEW_ERROR_ZERO_PAGES); |
| 1399 return false; | 1543 return false; |
| 1400 } | 1544 } |
| 1401 | 1545 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1557 DCHECK(IsRendering()); | 1701 DCHECK(IsRendering()); |
| 1558 return prep_frame_view_->GetPrintCanvasSize(); | 1702 return prep_frame_view_->GetPrintCanvasSize(); |
| 1559 } | 1703 } |
| 1560 | 1704 |
| 1561 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1705 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1562 prep_frame_view_.reset(); | 1706 prep_frame_view_.reset(); |
| 1563 metafile_.reset(); | 1707 metafile_.reset(); |
| 1564 pages_to_render_.clear(); | 1708 pages_to_render_.clear(); |
| 1565 error_ = PREVIEW_ERROR_NONE; | 1709 error_ = PREVIEW_ERROR_NONE; |
| 1566 } | 1710 } |
| OLD | NEW |