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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 bool PrintMsg_Print_Params_IsEqual( | 119 bool PrintMsg_Print_Params_IsEqual( |
| 120 const PrintMsg_PrintPages_Params& oldParams, | 120 const PrintMsg_PrintPages_Params& oldParams, |
| 121 const PrintMsg_PrintPages_Params& newParams) { | 121 const PrintMsg_PrintPages_Params& newParams) { |
| 122 return PageLayoutIsEqual(oldParams, newParams) && | 122 return PageLayoutIsEqual(oldParams, newParams) && |
| 123 oldParams.params.max_shrink == newParams.params.max_shrink && | 123 oldParams.params.max_shrink == newParams.params.max_shrink && |
| 124 oldParams.params.min_shrink == newParams.params.min_shrink && | 124 oldParams.params.min_shrink == newParams.params.min_shrink && |
| 125 oldParams.params.selection_only == newParams.params.selection_only && | 125 oldParams.params.selection_only == newParams.params.selection_only && |
| 126 oldParams.params.supports_alpha_blend == | 126 oldParams.params.supports_alpha_blend == |
| 127 newParams.params.supports_alpha_blend && | 127 newParams.params.supports_alpha_blend && |
| 128 oldParams.pages.size() == newParams.pages.size() && | 128 oldParams.pages.size() == newParams.pages.size() && |
| 129 oldParams.params.print_to_pdf == newParams.params.print_to_pdf && | |
| 129 oldParams.params.display_header_footer == | 130 oldParams.params.display_header_footer == |
| 130 newParams.params.display_header_footer && | 131 newParams.params.display_header_footer && |
| 131 oldParams.params.date == newParams.params.date && | 132 oldParams.params.date == newParams.params.date && |
| 132 oldParams.params.title == newParams.params.title && | 133 oldParams.params.title == newParams.params.title && |
| 133 oldParams.params.url == newParams.params.url && | 134 oldParams.params.url == newParams.params.url && |
| 134 std::equal(oldParams.pages.begin(), oldParams.pages.end(), | 135 std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
| 135 newParams.pages.begin()); | 136 newParams.pages.begin()); |
| 136 } | 137 } |
| 137 | 138 |
| 138 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, | 139 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, |
| 139 gfx::Size* result) { | 140 gfx::Size* result) { |
| 140 int dpi = GetDPI(&print_params); | 141 int dpi = GetDPI(&print_params); |
| 141 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, | 142 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, |
| 142 print_params.desired_dpi)); | 143 print_params.desired_dpi)); |
| 143 | 144 |
| 144 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, | 145 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, |
| 145 print_params.desired_dpi)); | 146 print_params.desired_dpi)); |
| 146 } | 147 } |
| 147 | 148 |
| 148 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { | 149 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { |
| 149 if (!node.isNull()) | 150 if (!node.isNull()) |
| 150 return true; | 151 return true; |
| 151 std::string mime(frame->dataSource()->response().mimeType().utf8()); | 152 std::string mime(frame->dataSource()->response().mimeType().utf8()); |
| 152 return mime == "application/pdf"; | 153 return mime == "application/pdf"; |
| 153 } | 154 } |
| 154 | 155 |
| 156 bool PrintingFrameHasPageSizeStyle(WebFrame* frame) { | |
| 157 if (!frame) | |
| 158 return false; | |
| 159 return frame->hasCustomPageSizeStyle(0); | |
| 160 } | |
| 161 | |
| 155 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { | 162 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { |
| 156 if (frame->isPrintScalingDisabledForPlugin(node)) | 163 if (frame->isPrintScalingDisabledForPlugin(node)) |
| 157 return printing::NO_MARGINS; | 164 return printing::NO_MARGINS; |
| 158 else | 165 else |
| 159 return printing::PRINTABLE_AREA_MARGINS; | 166 return printing::PRINTABLE_AREA_MARGINS; |
| 160 } | 167 } |
| 161 | 168 |
| 162 // Get the (x, y) coordinate from where printing of the current text should | 169 // Get the (x, y) coordinate from where printing of the current text should |
| 163 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and | 170 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and |
| 164 // vertical alignment (TOP, BOTTOM). | 171 // vertical alignment (TOP, BOTTOM). |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 web_frame->setScrollOffset(prev_scroll_offset_); | 440 web_frame->setScrollOffset(prev_scroll_offset_); |
| 434 } | 441 } |
| 435 } | 442 } |
| 436 | 443 |
| 437 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) | 444 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) |
| 438 : content::RenderViewObserver(render_view), | 445 : content::RenderViewObserver(render_view), |
| 439 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), | 446 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), |
| 440 print_web_view_(NULL), | 447 print_web_view_(NULL), |
| 441 is_preview_enabled_(switches::IsPrintPreviewEnabled()), | 448 is_preview_enabled_(switches::IsPrintPreviewEnabled()), |
| 442 is_print_ready_metafile_sent_(false), | 449 is_print_ready_metafile_sent_(false), |
| 450 ignore_frame_margins_css_(false), | |
| 451 fit_to_page_(true), | |
| 443 user_cancelled_scripted_print_count_(0), | 452 user_cancelled_scripted_print_count_(0), |
| 444 notify_browser_of_print_failure_(true) { | 453 notify_browser_of_print_failure_(true) { |
| 445 } | 454 } |
| 446 | 455 |
| 447 PrintWebViewHelper::~PrintWebViewHelper() {} | 456 PrintWebViewHelper::~PrintWebViewHelper() {} |
| 448 | 457 |
| 449 // Prints |frame| which called window.print(). | 458 // Prints |frame| which called window.print(). |
| 450 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { | 459 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { |
| 451 DCHECK(frame); | 460 DCHECK(frame); |
| 452 | 461 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 551 void PrintWebViewHelper::OnPrintForSystemDialog() { | 560 void PrintWebViewHelper::OnPrintForSystemDialog() { |
| 552 WebFrame* frame = print_preview_context_.frame(); | 561 WebFrame* frame = print_preview_context_.frame(); |
| 553 if (!frame) { | 562 if (!frame) { |
| 554 NOTREACHED(); | 563 NOTREACHED(); |
| 555 return; | 564 return; |
| 556 } | 565 } |
| 557 | 566 |
| 558 Print(frame, print_preview_context_.node()); | 567 Print(frame, print_preview_context_.node()); |
| 559 } | 568 } |
| 560 | 569 |
| 570 void PrintWebViewHelper::UpdateFrameMarginsCssInfo( | |
| 571 const DictionaryValue& settings) { | |
| 572 int margins_type = 0; | |
| 573 if (!settings.GetInteger(printing::kSettingMarginsType, &margins_type)) | |
| 574 margins_type = printing::DEFAULT_MARGINS; | |
| 575 ignore_frame_margins_css_ = margins_type != printing::DEFAULT_MARGINS; | |
| 576 } | |
| 577 | |
| 578 bool PrintWebViewHelper::IsPrintToPdfRequested( | |
| 579 const DictionaryValue& job_settings) { | |
| 580 bool print_to_pdf = false; | |
| 581 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) | |
| 582 NOTREACHED(); | |
| 583 return print_to_pdf; | |
| 584 } | |
| 585 | |
| 561 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 586 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
| 562 DCHECK(is_preview_enabled_); | 587 DCHECK(is_preview_enabled_); |
| 563 print_preview_context_.OnPrintPreview(); | 588 print_preview_context_.OnPrintPreview(); |
| 564 | 589 |
| 565 if (!UpdatePrintSettings(print_preview_context_.frame(), | 590 if (!UpdatePrintSettings(print_preview_context_.frame(), |
| 566 print_preview_context_.node(), settings, false)) { | 591 print_preview_context_.node(), settings, false)) { |
| 567 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 592 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
| 568 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 593 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
| 569 routing_id(), print_pages_params_->params.document_cookie)); | 594 routing_id(), print_pages_params_->params.document_cookie)); |
| 570 notify_browser_of_print_failure_ = false; // Already sent. | 595 notify_browser_of_print_failure_ = false; // Already sent. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 611 } else { | 636 } else { |
| 612 if (notify_browser_of_print_failure_) | 637 if (notify_browser_of_print_failure_) |
| 613 LOG(ERROR) << "CreatePreviewDocument failed"; | 638 LOG(ERROR) << "CreatePreviewDocument failed"; |
| 614 DidFinishPrinting(FAIL_PREVIEW); | 639 DidFinishPrinting(FAIL_PREVIEW); |
| 615 } | 640 } |
| 616 } | 641 } |
| 617 | 642 |
| 618 bool PrintWebViewHelper::CreatePreviewDocument() { | 643 bool PrintWebViewHelper::CreatePreviewDocument() { |
| 619 PrintMsg_Print_Params print_params = print_pages_params_->params; | 644 PrintMsg_Print_Params print_params = print_pages_params_->params; |
| 620 const std::vector<int>& pages = print_pages_params_->pages; | 645 const std::vector<int>& pages = print_pages_params_->pages; |
| 621 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) | 646 if (!print_preview_context_.CreatePreviewDocument( |
| 647 &print_params, pages, ignore_frame_margins_css_, | |
| 648 fit_to_page_)) { | |
| 622 return false; | 649 return false; |
| 650 } | |
| 651 | |
| 652 PageSizeMargins default_page_layout; | |
| 653 GetPageSizeAndMarginsInPoints(print_preview_context_.frame(), 0, print_params, | |
| 654 ignore_frame_margins_css_, fit_to_page_, NULL, &default_page_layout); | |
| 655 if (!old_print_pages_params_.get() || | |
| 656 !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) { | |
| 657 bool has_page_size_style = PrintingFrameHasPageSizeStyle( | |
| 658 print_preview_context_.frame()); | |
| 659 // Margins: Send default page layout to browser process. | |
| 660 Send(new PrintHostMsg_DidGetDefaultPageLayout( | |
| 661 routing_id(), default_page_layout, has_page_size_style)); | |
| 662 } | |
| 663 | |
| 623 PrintHostMsg_DidGetPreviewPageCount_Params params; | 664 PrintHostMsg_DidGetPreviewPageCount_Params params; |
| 624 params.page_count = print_preview_context_.total_page_count(); | 665 params.page_count = print_preview_context_.total_page_count(); |
| 625 params.is_modifiable = print_preview_context_.IsModifiable(); | 666 params.is_modifiable = print_preview_context_.IsModifiable(); |
| 626 params.document_cookie = print_pages_params_->params.document_cookie; | 667 params.document_cookie = print_pages_params_->params.document_cookie; |
| 627 params.preview_request_id = print_pages_params_->params.preview_request_id; | 668 params.preview_request_id = print_pages_params_->params.preview_request_id; |
| 628 params.clear_preview_data = print_preview_context_.generate_draft_pages(); | 669 params.clear_preview_data = print_preview_context_.generate_draft_pages(); |
| 629 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); | 670 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); |
| 630 if (CheckForCancel()) | 671 if (CheckForCancel()) |
| 631 return false; | 672 return false; |
| 632 | 673 |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 833 return true; | 874 return true; |
| 834 } | 875 } |
| 835 | 876 |
| 836 #if defined(OS_MACOSX) || defined(OS_WIN) | 877 #if defined(OS_MACOSX) || defined(OS_WIN) |
| 837 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, | 878 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, |
| 838 WebFrame* frame, | 879 WebFrame* frame, |
| 839 const WebNode& node) { | 880 const WebNode& node) { |
| 840 PrintMsg_Print_Params print_params = params.params; | 881 PrintMsg_Print_Params print_params = params.params; |
| 841 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); | 882 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); |
| 842 UpdatePrintableSizeInPrintParameters(frame, node, &prep_frame_view, | 883 UpdatePrintableSizeInPrintParameters(frame, node, &prep_frame_view, |
| 843 &print_params); | 884 &print_params, ignore_frame_margins_css_, |
| 885 fit_to_page_); | |
| 844 | 886 |
| 845 int page_count = prep_frame_view.GetExpectedPageCount(); | 887 int page_count = prep_frame_view.GetExpectedPageCount(); |
| 846 if (!page_count) | 888 if (!page_count) |
| 847 return false; | 889 return false; |
| 848 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 890 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
| 849 print_params.document_cookie, | 891 print_params.document_cookie, |
| 850 page_count)); | 892 page_count)); |
| 851 | 893 |
| 852 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 894 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
| 853 PrintMsg_PrintPage_Params page_params; | 895 PrintMsg_PrintPage_Params page_params; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 873 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); | 915 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); |
| 874 DCHECK(params != NULL); | 916 DCHECK(params != NULL); |
| 875 PrintPages(*params, print_web_view_->mainFrame(), WebNode()); | 917 PrintPages(*params, print_web_view_->mainFrame(), WebNode()); |
| 876 } | 918 } |
| 877 | 919 |
| 878 // static - Not anonymous so that platform implementations can use it. | 920 // static - Not anonymous so that platform implementations can use it. |
| 879 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( | 921 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( |
| 880 WebFrame* frame, | 922 WebFrame* frame, |
| 881 int page_index, | 923 int page_index, |
| 882 const PrintMsg_Print_Params& default_params, | 924 const PrintMsg_Print_Params& default_params, |
| 925 bool ignore_frame_margins_css, | |
| 926 bool fit_to_page, | |
| 927 double* scale_factor, | |
| 883 PageSizeMargins* page_layout_in_points) { | 928 PageSizeMargins* page_layout_in_points) { |
| 884 int dpi = GetDPI(&default_params); | 929 int dpi = GetDPI(&default_params); |
| 885 | 930 |
| 886 WebSize page_size_in_pixels( | 931 WebSize page_size_in_pixels( |
| 887 ConvertUnit(default_params.page_size.width(), | 932 ConvertUnit(default_params.page_size.width(), |
| 888 dpi, printing::kPixelsPerInch), | 933 dpi, printing::kPixelsPerInch), |
| 889 ConvertUnit(default_params.page_size.height(), | 934 ConvertUnit(default_params.page_size.height(), |
| 890 dpi, printing::kPixelsPerInch)); | 935 dpi, printing::kPixelsPerInch)); |
| 891 int margin_top_in_pixels = ConvertUnit( | 936 int margin_top_in_pixels = ConvertUnit( |
| 892 default_params.margin_top, | 937 default_params.margin_top, |
| 893 dpi, printing::kPixelsPerInch); | 938 dpi, printing::kPixelsPerInch); |
| 894 int margin_right_in_pixels = ConvertUnit( | 939 int margin_right_in_pixels = ConvertUnit( |
| 895 default_params.page_size.width() - | 940 default_params.page_size.width() - |
| 896 default_params.printable_size.width() - default_params.margin_left, | 941 default_params.printable_size.width() - default_params.margin_left, |
| 897 dpi, printing::kPixelsPerInch); | 942 dpi, printing::kPixelsPerInch); |
| 898 int margin_bottom_in_pixels = ConvertUnit( | 943 int margin_bottom_in_pixels = ConvertUnit( |
| 899 default_params.page_size.height() - | 944 default_params.page_size.height() - |
| 900 default_params.printable_size.height() - default_params.margin_top, | 945 default_params.printable_size.height() - default_params.margin_top, |
| 901 dpi, printing::kPixelsPerInch); | 946 dpi, printing::kPixelsPerInch); |
| 902 int margin_left_in_pixels = ConvertUnit( | 947 int margin_left_in_pixels = ConvertUnit( |
| 903 default_params.margin_left, | 948 default_params.margin_left, |
| 904 dpi, printing::kPixelsPerInch); | 949 dpi, printing::kPixelsPerInch); |
| 905 | 950 |
| 906 if (frame) { | 951 if (frame) { |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
If I'm reading this correctly, if ignore_frame_mar
kmadhusu
2011/12/02 23:33:19
Done.
| |
| 952 int old_margin_top_in_pixels = margin_top_in_pixels; | |
| 953 int old_margin_bottom_in_pixels = margin_bottom_in_pixels; | |
| 954 int old_margin_left_in_pixels = margin_left_in_pixels; | |
| 955 int old_margin_right_in_pixels = margin_right_in_pixels; | |
| 956 | |
| 907 frame->pageSizeAndMarginsInPixels(page_index, | 957 frame->pageSizeAndMarginsInPixels(page_index, |
| 908 page_size_in_pixels, | 958 page_size_in_pixels, |
| 909 margin_top_in_pixels, | 959 margin_top_in_pixels, |
| 910 margin_right_in_pixels, | 960 margin_right_in_pixels, |
| 911 margin_bottom_in_pixels, | 961 margin_bottom_in_pixels, |
| 912 margin_left_in_pixels); | 962 margin_left_in_pixels); |
| 963 if (ignore_frame_margins_css) { | |
| 964 margin_top_in_pixels = old_margin_top_in_pixels; | |
| 965 margin_bottom_in_pixels = old_margin_bottom_in_pixels; | |
| 966 margin_left_in_pixels = old_margin_left_in_pixels; | |
| 967 margin_right_in_pixels = old_margin_right_in_pixels; | |
| 968 } | |
| 913 } | 969 } |
| 914 | 970 |
| 915 page_layout_in_points->content_width = | 971 double new_printable_width = |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
is this printable width, page width, or content wi
kmadhusu
2011/12/02 23:33:19
Done.
| |
| 916 ConvertPixelsToPoint(page_size_in_pixels.width - | 972 ConvertPixelsToPoint(page_size_in_pixels.width - |
| 917 margin_left_in_pixels - | 973 margin_left_in_pixels - margin_right_in_pixels); |
| 918 margin_right_in_pixels); | 974 double new_printable_height = |
| 919 page_layout_in_points->content_height = | |
| 920 ConvertPixelsToPoint(page_size_in_pixels.height - | 975 ConvertPixelsToPoint(page_size_in_pixels.height - |
| 921 margin_top_in_pixels - | 976 margin_top_in_pixels - margin_bottom_in_pixels); |
| 922 margin_bottom_in_pixels); | 977 double default_printable_width = |
| 978 ConvertUnit(default_params.printable_size.width(), | |
| 979 dpi, printing::kPointsPerInch); | |
| 980 double default_printable_height = | |
| 981 ConvertUnit(default_params.printable_size.height(), | |
| 982 dpi, printing::kPointsPerInch); | |
| 983 | |
| 984 if (ignore_frame_margins_css && fit_to_page) { | |
| 985 new_printable_width = default_printable_width; | |
| 986 new_printable_height = default_printable_height; | |
| 987 } | |
| 923 | 988 |
| 924 // Invalid page size and/or margins. We just use the default setting. | 989 // Invalid page size and/or margins. We just use the default setting. |
| 925 if (page_layout_in_points->content_width < 1.0 || | 990 if (new_printable_width < 1.0 || new_printable_height < 1.0) { |
| 926 page_layout_in_points->content_height < 1.0) { | |
| 927 CHECK(frame != NULL); | 991 CHECK(frame != NULL); |
| 928 GetPageSizeAndMarginsInPoints(NULL, page_index, default_params, | 992 GetPageSizeAndMarginsInPoints(NULL, page_index, default_params, false, |
| 929 page_layout_in_points); | 993 false, NULL, page_layout_in_points); |
| 930 return; | 994 return; |
| 931 } | 995 } |
| 996 double margin_top_in_points = | |
| 997 ConvertPixelsToPointDouble(margin_top_in_pixels); | |
| 998 double margin_right_in_points = | |
| 999 ConvertPixelsToPointDouble(margin_right_in_pixels); | |
| 1000 double margin_left_in_points = | |
| 1001 ConvertPixelsToPointDouble(margin_left_in_pixels); | |
| 1002 double margin_bottom_in_points = | |
| 1003 ConvertPixelsToPointDouble(margin_bottom_in_pixels); | |
| 1004 double content_width = new_printable_width; | |
| 1005 double content_height = new_printable_height; | |
| 932 | 1006 |
| 933 page_layout_in_points->margin_top = | 1007 if (fit_to_page && (default_printable_height != new_printable_height || |
| 934 ConvertPixelsToPointDouble(margin_top_in_pixels); | 1008 default_printable_width != new_printable_width)) { |
| 935 page_layout_in_points->margin_right = | 1009 double default_page_size_height = |
| 936 ConvertPixelsToPointDouble(margin_right_in_pixels); | 1010 ConvertUnit(default_params.page_size.height(), dpi, |
| 937 page_layout_in_points->margin_bottom = | 1011 printing::kPointsPerInch); |
| 938 ConvertPixelsToPointDouble(margin_bottom_in_pixels); | 1012 double default_page_size_width = |
| 939 page_layout_in_points->margin_left = | 1013 ConvertUnit(default_params.page_size.width(), dpi, |
| 940 ConvertPixelsToPointDouble(margin_left_in_pixels); | 1014 printing::kPointsPerInch); |
| 1015 if (default_printable_width < new_printable_width || | |
| 1016 default_printable_height < new_printable_height) { | |
| 1017 double ratio_width = default_printable_width / new_printable_width; | |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
I think we want to scale the CSS page box into the
kmadhusu
2011/12/02 23:33:19
Done.
kmadhusu
2011/12/02 23:33:19
Done.
| |
| 1018 double ratio_height = default_printable_height / new_printable_height; | |
| 1019 double factor = ratio_width < ratio_height ? ratio_width : ratio_height; | |
| 1020 if (scale_factor) | |
| 1021 *scale_factor = factor; | |
| 1022 | |
| 1023 content_width *= factor; | |
| 1024 content_height *= factor; | |
| 1025 margin_top_in_points = default_page_size_height/2 - content_height/2; | |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
(default_page_size_height - content_height) / 2;
vandebo (ex-Chrome)
2011/12/02 01:28:19
If we scale the CSS page box into the printable ar
kmadhusu
2011/12/02 23:33:19
Done.
| |
| 1026 margin_left_in_points = default_page_size_width/2 - content_width/2; | |
| 1027 margin_right_in_points = default_page_size_width - | |
| 1028 margin_left_in_points - content_width; | |
| 1029 margin_bottom_in_points = default_page_size_height - | |
| 1030 margin_top_in_points - content_height; | |
| 1031 } else { | |
| 1032 int margin_top_bottom = margin_top_in_points + margin_bottom_in_points; | |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
This doesn't seem to work out. The formula we wor
kmadhusu
2011/12/02 23:33:19
This is the formula for scaling. I have also updat
| |
| 1033 int margin_left_right = margin_left_in_points + margin_right_in_points; | |
| 1034 double ratio_y = 1.0f, ratio_x = 1.0f; | |
| 1035 if (margin_top_bottom > 0) { | |
| 1036 ratio_y = (default_page_size_height - new_printable_height) / | |
| 1037 margin_top_bottom; | |
| 1038 } | |
| 1039 if (margin_left_right > 0) { | |
| 1040 ratio_x = (default_page_size_width - new_printable_width) / | |
| 1041 margin_left_right; | |
| 1042 } | |
| 1043 margin_top_in_points *= ratio_y; | |
| 1044 margin_left_in_points *= ratio_x; | |
| 1045 margin_right_in_points *= ratio_x; | |
| 1046 margin_bottom_in_points *= ratio_y; | |
| 1047 } | |
| 1048 } | |
| 1049 page_layout_in_points->content_width = content_width; | |
| 1050 page_layout_in_points->content_height = content_height; | |
| 1051 page_layout_in_points->margin_top = margin_top_in_points; | |
| 1052 page_layout_in_points->margin_right = margin_right_in_points; | |
| 1053 page_layout_in_points->margin_bottom = margin_bottom_in_points; | |
| 1054 page_layout_in_points->margin_left = margin_left_in_points; | |
| 941 } | 1055 } |
| 942 | 1056 |
| 943 // static - Not anonymous so that platform implementations can use it. | 1057 // static - Not anonymous so that platform implementations can use it. |
| 944 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( | 1058 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
| 945 WebFrame* frame, | 1059 WebFrame* frame, |
| 946 const WebNode& node, | 1060 const WebNode& node, |
| 947 PrepareFrameAndViewForPrint* prepare, | 1061 PrepareFrameAndViewForPrint* prepare, |
| 948 PrintMsg_Print_Params* params) { | 1062 PrintMsg_Print_Params* params, |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
pass by value, since we want to change it in the b
kmadhusu
2011/12/02 23:33:19
Done.
| |
| 1063 bool ignore_frame_margins_css, | |
| 1064 bool fit_to_page) { | |
| 949 if (PrintingNodeOrPdfFrame(frame, node)) | 1065 if (PrintingNodeOrPdfFrame(frame, node)) |
| 950 return; | 1066 return; |
| 951 PageSizeMargins page_layout_in_points; | 1067 PageSizeMargins page_layout_in_points; |
| 952 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, | 1068 PrintMsg_Print_Params current_params = *params; |
| 953 &page_layout_in_points); | 1069 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, current_params, |
| 1070 ignore_frame_margins_css, false, NULL, &page_layout_in_points); | |
| 954 int dpi = GetDPI(params); | 1071 int dpi = GetDPI(params); |
| 955 params->printable_size = gfx::Size( | 1072 current_params.printable_size = gfx::Size( |
| 956 static_cast<int>(ConvertUnitDouble( | 1073 static_cast<int>(ConvertUnitDouble( |
| 957 page_layout_in_points.content_width, | 1074 page_layout_in_points.content_width, |
| 958 printing::kPointsPerInch, dpi)), | 1075 printing::kPointsPerInch, dpi)), |
| 959 static_cast<int>(ConvertUnitDouble( | 1076 static_cast<int>(ConvertUnitDouble( |
| 960 page_layout_in_points.content_height, | 1077 page_layout_in_points.content_height, |
| 961 printing::kPointsPerInch, dpi))); | 1078 printing::kPointsPerInch, dpi))); |
| 962 | |
| 963 double page_width_in_points = | 1079 double page_width_in_points = |
| 964 page_layout_in_points.content_width + | 1080 page_layout_in_points.content_width + |
| 965 page_layout_in_points.margin_left + | 1081 page_layout_in_points.margin_left + |
| 966 page_layout_in_points.margin_right; | 1082 page_layout_in_points.margin_right; |
| 967 double page_height_in_points = | 1083 double page_height_in_points = |
| 968 page_layout_in_points.content_height + | 1084 page_layout_in_points.content_height + |
| 969 page_layout_in_points.margin_top + | 1085 page_layout_in_points.margin_top + |
| 970 page_layout_in_points.margin_bottom; | 1086 page_layout_in_points.margin_bottom; |
| 971 | 1087 |
| 972 params->page_size = gfx::Size( | 1088 current_params.page_size = gfx::Size( |
| 973 static_cast<int>(ConvertUnitDouble( | 1089 static_cast<int>(ConvertUnitDouble( |
| 974 page_width_in_points, printing::kPointsPerInch, dpi)), | 1090 page_width_in_points, printing::kPointsPerInch, dpi)), |
| 975 static_cast<int>(ConvertUnitDouble( | 1091 static_cast<int>(ConvertUnitDouble( |
| 976 page_height_in_points, printing::kPointsPerInch, dpi))); | 1092 page_height_in_points, printing::kPointsPerInch, dpi))); |
| 977 | 1093 current_params.margin_top = |
| 978 params->margin_top = static_cast<int>(ConvertUnitDouble( | 1094 static_cast<int>(ConvertUnitDouble( |
| 979 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); | 1095 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); |
| 980 params->margin_left = static_cast<int>(ConvertUnitDouble( | 1096 current_params.margin_left = static_cast<int>(ConvertUnitDouble( |
| 981 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); | 1097 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); |
| 982 | 1098 prepare->UpdatePrintParams(current_params); |
| 983 prepare->UpdatePrintParams(*params); | |
| 984 } | 1099 } |
| 985 | 1100 |
| 986 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, | 1101 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, |
| 987 const WebKit::WebNode& node) { | 1102 const WebKit::WebNode& node) { |
| 988 DCHECK(frame); | 1103 DCHECK(frame); |
| 989 PrintMsg_PrintPages_Params settings; | 1104 PrintMsg_PrintPages_Params settings; |
| 990 | 1105 |
| 991 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1106 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
| 992 &settings.params)); | 1107 &settings.params)); |
| 993 // Check if the printer returned any settings, if the settings is empty, we | 1108 // Check if the printer returned any settings, if the settings is empty, we |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 1017 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1132 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
| 1018 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1133 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
| 1019 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | 1134 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
| 1020 if (!InitPrintSettings(frame, node)) | 1135 if (!InitPrintSettings(frame, node)) |
| 1021 return false; | 1136 return false; |
| 1022 | 1137 |
| 1023 DCHECK(!prepare->get()); | 1138 DCHECK(!prepare->get()); |
| 1024 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1139 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
| 1025 frame, node)); | 1140 frame, node)); |
| 1026 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), | 1141 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), |
| 1027 &print_pages_params_->params); | 1142 &print_pages_params_->params, |
| 1143 ignore_frame_margins_css_, | |
| 1144 fit_to_page_); | |
| 1028 Send(new PrintHostMsg_DidGetDocumentCookie( | 1145 Send(new PrintHostMsg_DidGetDocumentCookie( |
| 1029 routing_id(), print_pages_params_->params.document_cookie)); | 1146 routing_id(), print_pages_params_->params.document_cookie)); |
| 1030 return true; | 1147 return true; |
| 1031 } | 1148 } |
| 1032 | 1149 |
| 1033 bool PrintWebViewHelper::UpdatePrintSettings( | 1150 bool PrintWebViewHelper::UpdatePrintSettings( |
| 1034 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1151 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
| 1035 const DictionaryValue& passed_job_settings, bool print_for_preview) { | 1152 const DictionaryValue& passed_job_settings, bool print_for_preview) { |
| 1036 DCHECK(is_preview_enabled_); | 1153 DCHECK(is_preview_enabled_); |
| 1037 const DictionaryValue* job_settings = &passed_job_settings; | 1154 const DictionaryValue* job_settings = &passed_job_settings; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1071 bool get_margins_from_pdf = false; | 1188 bool get_margins_from_pdf = false; |
| 1072 #endif | 1189 #endif |
| 1073 | 1190 |
| 1074 printing::MarginType margin_type = printing::NO_MARGINS; | 1191 printing::MarginType margin_type = printing::NO_MARGINS; |
| 1075 if (get_margins_from_pdf) | 1192 if (get_margins_from_pdf) |
| 1076 margin_type = GetMarginsForPdf(frame, node); | 1193 margin_type = GetMarginsForPdf(frame, node); |
| 1077 modified_job_settings.SetInteger(printing::kSettingMarginsType, | 1194 modified_job_settings.SetInteger(printing::kSettingMarginsType, |
| 1078 margin_type); | 1195 margin_type); |
| 1079 job_settings = &modified_job_settings; | 1196 job_settings = &modified_job_settings; |
| 1080 } | 1197 } |
| 1198 UpdateFrameMarginsCssInfo(*job_settings); | |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
You can put both of these lines in a !print_for_pr
kmadhusu
2011/12/02 23:33:19
Done.
| |
| 1199 fit_to_page_ = !print_for_preview && source_is_html && | |
| 1200 !IsPrintToPdfRequested(*job_settings); | |
| 1081 | 1201 |
| 1082 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when | 1202 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when |
| 1083 // possible. | 1203 // possible. |
| 1084 int cookie = print_pages_params_.get() ? | 1204 int cookie = print_pages_params_.get() ? |
| 1085 print_pages_params_->params.document_cookie : 0; | 1205 print_pages_params_->params.document_cookie : 0; |
| 1086 PrintMsg_PrintPages_Params settings; | 1206 PrintMsg_PrintPages_Params settings; |
| 1087 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 1207 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
| 1088 cookie, *job_settings, &settings)); | 1208 cookie, *job_settings, &settings)); |
| 1089 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1209 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 1090 | 1210 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 1118 &(settings.params.preview_ui_addr)) || | 1238 &(settings.params.preview_ui_addr)) || |
| 1119 !job_settings->GetInteger(printing::kPreviewRequestID, | 1239 !job_settings->GetInteger(printing::kPreviewRequestID, |
| 1120 &(settings.params.preview_request_id)) || | 1240 &(settings.params.preview_request_id)) || |
| 1121 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1241 !job_settings->GetBoolean(printing::kIsFirstRequest, |
| 1122 &(settings.params.is_first_request))) { | 1242 &(settings.params.is_first_request))) { |
| 1123 NOTREACHED(); | 1243 NOTREACHED(); |
| 1124 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1244 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
| 1125 return false; | 1245 return false; |
| 1126 } | 1246 } |
| 1127 | 1247 |
| 1128 // Margins: Send default page layout to browser process. | 1248 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
|
vandebo (ex-Chrome)
2011/12/02 01:28:19
I think this can go inside the !print_for_preview
kmadhusu
2011/12/02 23:33:19
It is already within that block.
| |
| 1129 PageSizeMargins default_page_layout; | |
| 1130 GetPageSizeAndMarginsInPoints(NULL, -1, settings.params, | |
| 1131 &default_page_layout); | |
| 1132 if (!old_print_pages_params_.get() || | |
| 1133 !PageLayoutIsEqual(*old_print_pages_params_, settings)) { | |
| 1134 Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(), | |
| 1135 default_page_layout)); | |
| 1136 } | |
| 1137 | 1249 |
| 1138 // Header/Footer: Set |header_footer_info_|. | 1250 // Header/Footer: Set |header_footer_info_|. |
| 1139 if (settings.params.display_header_footer) { | 1251 if (settings.params.display_header_footer) { |
| 1140 header_footer_info_.reset(new DictionaryValue()); | 1252 header_footer_info_.reset(new DictionaryValue()); |
| 1141 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1253 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
| 1142 settings.params.date); | 1254 settings.params.date); |
| 1143 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1255 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
| 1144 settings.params.url); | 1256 settings.params.url); |
| 1145 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1257 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
| 1146 settings.params.title); | 1258 settings.params.title); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1364 node_ = web_node; | 1476 node_ = web_node; |
| 1365 } | 1477 } |
| 1366 | 1478 |
| 1367 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 1479 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
| 1368 DCHECK_EQ(INITIALIZED, state_); | 1480 DCHECK_EQ(INITIALIZED, state_); |
| 1369 ClearContext(); | 1481 ClearContext(); |
| 1370 } | 1482 } |
| 1371 | 1483 |
| 1372 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( | 1484 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
| 1373 PrintMsg_Print_Params* print_params, | 1485 PrintMsg_Print_Params* print_params, |
| 1374 const std::vector<int>& pages) { | 1486 const std::vector<int>& pages, |
| 1487 bool ignore_frame_margins_css, | |
| 1488 bool fit_to_page) { | |
| 1375 DCHECK_EQ(INITIALIZED, state_); | 1489 DCHECK_EQ(INITIALIZED, state_); |
| 1376 state_ = RENDERING; | 1490 state_ = RENDERING; |
| 1377 | 1491 |
| 1378 metafile_.reset(new printing::PreviewMetafile); | 1492 metafile_.reset(new printing::PreviewMetafile); |
| 1379 if (!metafile_->Init()) { | 1493 if (!metafile_->Init()) { |
| 1380 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); | 1494 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); |
| 1381 LOG(ERROR) << "PreviewMetafile Init failed"; | 1495 LOG(ERROR) << "PreviewMetafile Init failed"; |
| 1382 return false; | 1496 return false; |
| 1383 } | 1497 } |
| 1384 | 1498 |
| 1385 // Need to make sure old object gets destroyed first. | 1499 // Need to make sure old object gets destroyed first. |
| 1386 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), | 1500 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
| 1387 node())); | 1501 node())); |
| 1388 UpdatePrintableSizeInPrintParameters(frame_, node_, | 1502 UpdatePrintableSizeInPrintParameters(frame_, node_, |
| 1389 prep_frame_view_.get(), print_params); | 1503 prep_frame_view_.get(), print_params, |
| 1504 ignore_frame_margins_css, | |
| 1505 fit_to_page); | |
| 1390 | 1506 |
| 1391 print_params_.reset(new PrintMsg_Print_Params(*print_params)); | 1507 print_params_.reset(new PrintMsg_Print_Params(*print_params)); |
| 1392 | 1508 |
| 1393 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1509 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
| 1394 if (total_page_count_ == 0) { | 1510 if (total_page_count_ == 0) { |
| 1395 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; | 1511 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; |
| 1396 set_error(PREVIEW_ERROR_ZERO_PAGES); | 1512 set_error(PREVIEW_ERROR_ZERO_PAGES); |
| 1397 return false; | 1513 return false; |
| 1398 } | 1514 } |
| 1399 | 1515 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1555 DCHECK(IsRendering()); | 1671 DCHECK(IsRendering()); |
| 1556 return prep_frame_view_->GetPrintCanvasSize(); | 1672 return prep_frame_view_->GetPrintCanvasSize(); |
| 1557 } | 1673 } |
| 1558 | 1674 |
| 1559 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1675 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1560 prep_frame_view_.reset(); | 1676 prep_frame_view_.reset(); |
| 1561 metafile_.reset(); | 1677 metafile_.reset(); |
| 1562 pages_to_render_.clear(); | 1678 pages_to_render_.clear(); |
| 1563 error_ = PREVIEW_ERROR_NONE; | 1679 error_ = PREVIEW_ERROR_NONE; |
| 1564 } | 1680 } |
| OLD | NEW |