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

Side by Side Diff: chrome/renderer/print_web_view_helper.cc

Issue 10168013: [Print Preview] Modified PrepareFrameAndViewPrint class to call the new printBegin function to supp… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed nits Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | webkit/glue/webkit_glue.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
14 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
15 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/print_messages.h" 16 #include "chrome/common/print_messages.h"
17 #include "chrome/common/render_messages.h" 17 #include "chrome/common/render_messages.h"
18 #include "chrome/common/url_constants.h" 18 #include "chrome/common/url_constants.h"
19 #include "chrome/renderer/prerender/prerender_helper.h" 19 #include "chrome/renderer/prerender/prerender_helper.h"
20 #include "content/public/renderer/render_thread.h" 20 #include "content/public/renderer/render_thread.h"
21 #include "content/public/renderer/render_view.h" 21 #include "content/public/renderer/render_view.h"
22 #include "grit/generated_resources.h" 22 #include "grit/generated_resources.h"
23 #include "printing/metafile_impl.h" 23 #include "printing/metafile_impl.h"
24 #include "printing/page_size_margins.h" 24 #include "printing/page_size_margins.h"
25 #include "printing/print_job_constants.h" 25 #include "printing/print_job_constants.h"
26 #include "printing/units.h" 26 #include "printing/units.h"
27 #include "third_party/skia/include/core/SkRect.h" 27 #include "third_party/skia/include/core/SkRect.h"
28 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCanvas.h" 28 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCanvas.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRespon se.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginDocument.h" 37 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginDocument.h"
35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h" 38 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h"
36 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" 39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPrintScalingOption .h"
37 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h"
38 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRespon se.h"
39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 40 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
40 #include "ui/base/l10n/l10n_util.h" 41 #include "ui/base/l10n/l10n_util.h"
41 #include "ui/gfx/rect.h" 42 #include "ui/gfx/rect.h"
42 #include "webkit/glue/webpreferences.h" 43 #include "webkit/glue/webpreferences.h"
43 44
44 #if defined(OS_POSIX) 45 #if defined(OS_POSIX)
45 #include "base/process_util.h" 46 #include "base/process_util.h"
46 #endif 47 #endif
47 48
48 #if defined(USE_SKIA) 49 #if defined(USE_SKIA)
(...skipping 22 matching lines...) Expand all
71 using printing::ConvertUnitDouble; 72 using printing::ConvertUnitDouble;
72 using printing::GetHeaderFooterSegmentWidth; 73 using printing::GetHeaderFooterSegmentWidth;
73 using printing::PageSizeMargins; 74 using printing::PageSizeMargins;
74 using WebKit::WebConsoleMessage; 75 using WebKit::WebConsoleMessage;
75 using WebKit::WebDocument; 76 using WebKit::WebDocument;
76 using WebKit::WebElement; 77 using WebKit::WebElement;
77 using WebKit::WebFrame; 78 using WebKit::WebFrame;
78 using WebKit::WebNode; 79 using WebKit::WebNode;
79 using WebKit::WebPlugin; 80 using WebKit::WebPlugin;
80 using WebKit::WebPluginDocument; 81 using WebKit::WebPluginDocument;
82 using WebKit::WebPrintParams;
81 using WebKit::WebSize; 83 using WebKit::WebSize;
82 using WebKit::WebString; 84 using WebKit::WebString;
83 using WebKit::WebURLRequest; 85 using WebKit::WebURLRequest;
84 using WebKit::WebView; 86 using WebKit::WebView;
85 87
86 namespace { 88 namespace {
87 89
88 #if defined(USE_SKIA) 90 #if defined(USE_SKIA)
89 typedef SkPaint HeaderFooterPaint; 91 typedef SkPaint HeaderFooterPaint;
90 #elif defined(OS_MACOSX) 92 #elif defined(OS_MACOSX)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 const PrintMsg_PrintPages_Params& oldParams, 132 const PrintMsg_PrintPages_Params& oldParams,
131 const PrintMsg_PrintPages_Params& newParams) { 133 const PrintMsg_PrintPages_Params& newParams) {
132 return PageLayoutIsEqual(oldParams, newParams) && 134 return PageLayoutIsEqual(oldParams, newParams) &&
133 oldParams.params.max_shrink == newParams.params.max_shrink && 135 oldParams.params.max_shrink == newParams.params.max_shrink &&
134 oldParams.params.min_shrink == newParams.params.min_shrink && 136 oldParams.params.min_shrink == newParams.params.min_shrink &&
135 oldParams.params.selection_only == newParams.params.selection_only && 137 oldParams.params.selection_only == newParams.params.selection_only &&
136 oldParams.params.supports_alpha_blend == 138 oldParams.params.supports_alpha_blend ==
137 newParams.params.supports_alpha_blend && 139 newParams.params.supports_alpha_blend &&
138 oldParams.pages.size() == newParams.pages.size() && 140 oldParams.pages.size() == newParams.pages.size() &&
139 oldParams.params.print_to_pdf == newParams.params.print_to_pdf && 141 oldParams.params.print_to_pdf == newParams.params.print_to_pdf &&
140 oldParams.params.fit_to_paper_size == 142 oldParams.params.print_scaling_option ==
141 newParams.params.fit_to_paper_size && 143 newParams.params.print_scaling_option &&
142 oldParams.params.display_header_footer == 144 oldParams.params.display_header_footer ==
143 newParams.params.display_header_footer && 145 newParams.params.display_header_footer &&
144 oldParams.params.date == newParams.params.date && 146 oldParams.params.date == newParams.params.date &&
145 oldParams.params.title == newParams.params.title && 147 oldParams.params.title == newParams.params.title &&
146 oldParams.params.url == newParams.params.url && 148 oldParams.params.url == newParams.params.url &&
147 std::equal(oldParams.pages.begin(), oldParams.pages.end(), 149 std::equal(oldParams.pages.begin(), oldParams.pages.end(),
148 newParams.pages.begin()); 150 newParams.pages.begin());
149 } 151 }
150 152
151 PrintMsg_Print_Params GetCssPrintParams( 153 PrintMsg_Print_Params GetCssPrintParams(
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 // Swap the |width| and |height| values. 301 // Swap the |width| and |height| values.
300 page_params->page_size.SetSize(page_params->page_size.height(), 302 page_params->page_size.SetSize(page_params->page_size.height(),
301 page_params->page_size.width()); 303 page_params->page_size.width());
302 page_params->content_size.SetSize(page_params->content_size.height(), 304 page_params->content_size.SetSize(page_params->content_size.height(),
303 page_params->content_size.width()); 305 page_params->content_size.width());
304 page_params->printable_area.set_size( 306 page_params->printable_area.set_size(
305 gfx::Size(page_params->printable_area.height(), 307 gfx::Size(page_params->printable_area.height(),
306 page_params->printable_area.width())); 308 page_params->printable_area.width()));
307 } 309 }
308 310
309 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, 311 void ComputeWebKitPrintParamsInDesiredDpi(
310 gfx::Size* result) { 312 const PrintMsg_Print_Params& print_params,
313 WebPrintParams* webkit_print_params) {
311 int dpi = GetDPI(&print_params); 314 int dpi = GetDPI(&print_params);
312 result->set_width(ConvertUnit(print_params.content_size.width(), dpi, 315 webkit_print_params->printerDPI = dpi;
313 print_params.desired_dpi)); 316 webkit_print_params->printScalingOption = print_params.print_scaling_option;
314 317
315 result->set_height(ConvertUnit(print_params.content_size.height(), dpi, 318 webkit_print_params->printContentArea.width =
316 print_params.desired_dpi)); 319 ConvertUnit(print_params.content_size.width(), dpi,
320 print_params.desired_dpi);
321 webkit_print_params->printContentArea.height =
322 ConvertUnit(print_params.content_size.height(), dpi,
323 print_params.desired_dpi);
324
325 webkit_print_params->printableArea.x =
326 ConvertUnit(print_params.printable_area.x(), dpi,
327 print_params.desired_dpi);
328 webkit_print_params->printableArea.y =
329 ConvertUnit(print_params.printable_area.y(), dpi,
330 print_params.desired_dpi);
331 webkit_print_params->printableArea.width =
332 ConvertUnit(print_params.printable_area.width(), dpi,
333 print_params.desired_dpi);
334 webkit_print_params->printableArea.height =
335 ConvertUnit(print_params.printable_area.height(),
336 dpi, print_params.desired_dpi);
337
338 webkit_print_params->paperSize.width =
339 ConvertUnit(print_params.page_size.width(), dpi,
340 print_params.desired_dpi);
341 webkit_print_params->paperSize.height =
342 ConvertUnit(print_params.page_size.height(), dpi,
343 print_params.desired_dpi);
317 } 344 }
318 345
319 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { 346 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) {
320 if (!node.isNull()) 347 if (!node.isNull())
321 return true; 348 return true;
322 if (!frame->document().isPluginDocument()) 349 if (!frame->document().isPluginDocument())
323 return false; 350 return false;
324 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin(); 351 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin();
325 return plugin && plugin->supportsPaginatedPrint(); 352 return plugin && plugin->supportsPaginatedPrint();
326 } 353 }
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 #endif 699 #endif
673 } 700 }
674 701
675 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( 702 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
676 const PrintMsg_Print_Params& print_params, 703 const PrintMsg_Print_Params& print_params,
677 WebFrame* frame, 704 WebFrame* frame,
678 const WebNode& node) 705 const WebNode& node)
679 : frame_(frame), 706 : frame_(frame),
680 node_to_print_(node), 707 node_to_print_(node),
681 web_view_(frame->view()), 708 web_view_(frame->view()),
682 dpi_(static_cast<int>(print_params.dpi)),
683 expected_pages_count_(0), 709 expected_pages_count_(0),
684 use_browser_overlays_(true), 710 use_browser_overlays_(true),
685 finished_(false) { 711 finished_(false) {
686 gfx::Size canvas_size; 712 WebPrintParams webkit_print_params;
687 CalculatePrintCanvasSize(print_params, &canvas_size); 713 ComputeWebKitPrintParamsInDesiredDpi(print_params, &webkit_print_params);
688 714
689 if (WebFrame* web_frame = web_view_->mainFrame()) 715 if (WebFrame* web_frame = web_view_->mainFrame())
690 prev_scroll_offset_ = web_frame->scrollOffset(); 716 prev_scroll_offset_ = web_frame->scrollOffset();
691 prev_view_size_ = web_view_->size(); 717 prev_view_size_ = web_view_->size();
692 718
693 StartPrinting(canvas_size); 719 StartPrinting(webkit_print_params);
694 } 720 }
695 721
696 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { 722 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
697 FinishPrinting(); 723 FinishPrinting();
698 } 724 }
699 725
700 void PrepareFrameAndViewForPrint::UpdatePrintParams( 726 void PrepareFrameAndViewForPrint::UpdatePrintParams(
701 const PrintMsg_Print_Params& print_params) { 727 const PrintMsg_Print_Params& print_params) {
702 DCHECK(!finished_); 728 DCHECK(!finished_);
703 gfx::Size canvas_size; 729 WebPrintParams webkit_print_params;
704 CalculatePrintCanvasSize(print_params, &canvas_size); 730 ComputeWebKitPrintParamsInDesiredDpi(print_params, &webkit_print_params);
705 if (canvas_size == print_canvas_size_) 731
732 if (webkit_print_params.printContentArea ==
733 web_print_params_.printContentArea &&
734 webkit_print_params.printableArea == web_print_params_.printableArea &&
735 webkit_print_params.paperSize == web_print_params_.paperSize &&
736 webkit_print_params.printScalingOption ==
737 web_print_params_.printScalingOption) {
706 return; 738 return;
739 }
707 740
708 frame_->printEnd(); 741 frame_->printEnd();
709 dpi_ = static_cast<int>(print_params.dpi); 742 StartPrinting(webkit_print_params);
710 StartPrinting(canvas_size);
711 } 743 }
712 744
713 void PrepareFrameAndViewForPrint::StartPrinting( 745 void PrepareFrameAndViewForPrint::StartPrinting(
714 const gfx::Size& print_canvas_size) { 746 const WebPrintParams& webkit_print_params) {
715 print_canvas_size_ = print_canvas_size; 747 web_print_params_ = webkit_print_params;
716 748
717 // Layout page according to printer page size. Since WebKit shrinks the 749 // Layout page according to printer page size. Since WebKit shrinks the
718 // size of the page automatically (from 125% to 200%) we trick it to 750 // size of the page automatically (from 125% to 200%) we trick it to
719 // think the page is 125% larger so the size of the page is correct for 751 // think the page is 125% larger so the size of the page is correct for
720 // minimum (default) scaling. 752 // minimum (default) scaling.
721 // This is important for sites that try to fill the page. 753 // This is important for sites that try to fill the page.
722 gfx::Size print_layout_size(print_canvas_size_); 754 gfx::Size print_layout_size(web_print_params_.printContentArea.width,
755 web_print_params_.printContentArea.height);
723 print_layout_size.set_height(static_cast<int>( 756 print_layout_size.set_height(static_cast<int>(
724 static_cast<double>(print_layout_size.height()) * 1.25)); 757 static_cast<double>(print_layout_size.height()) * 1.25));
725 758
726 web_view_->resize(print_layout_size); 759 web_view_->resize(print_layout_size);
727 760
728 expected_pages_count_ = frame_->printBegin(print_canvas_size_, node_to_print_, 761 expected_pages_count_ = frame_->printBegin(web_print_params_,
729 dpi_, &use_browser_overlays_); 762 node_to_print_,
763 &use_browser_overlays_);
730 } 764 }
731 765
732 void PrepareFrameAndViewForPrint::FinishPrinting() { 766 void PrepareFrameAndViewForPrint::FinishPrinting() {
733 if (!finished_) { 767 if (!finished_) {
734 finished_ = true; 768 finished_ = true;
735 frame_->printEnd(); 769 frame_->printEnd();
736 web_view_->resize(prev_view_size_); 770 web_view_->resize(prev_view_size_);
737 if (WebFrame* web_frame = web_view_->mainFrame()) 771 if (WebFrame* web_frame = web_view_->mainFrame())
738 web_frame->setScrollOffset(prev_scroll_offset_); 772 web_frame->setScrollOffset(prev_scroll_offset_);
739 } 773 }
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
901 } 935 }
902 936
903 bool PrintWebViewHelper::IsPrintToPdfRequested( 937 bool PrintWebViewHelper::IsPrintToPdfRequested(
904 const DictionaryValue& job_settings) { 938 const DictionaryValue& job_settings) {
905 bool print_to_pdf = false; 939 bool print_to_pdf = false;
906 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) 940 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf))
907 NOTREACHED(); 941 NOTREACHED();
908 return print_to_pdf; 942 return print_to_pdf;
909 } 943 }
910 944
911 bool PrintWebViewHelper::IsFitToPaperSizeRequested( 945 WebKit::WebPrintScalingOption PrintWebViewHelper::GetPrintScalingOption(
912 bool source_is_html, const DictionaryValue& job_settings, 946 bool source_is_html, const DictionaryValue& job_settings,
913 const PrintMsg_Print_Params& params) { 947 const PrintMsg_Print_Params& params) {
914 DCHECK(!print_for_preview_); 948 DCHECK(!print_for_preview_);
915 949
916 // Do not fit to paper size when the user is saving the print contents as pdf.
917 if (params.print_to_pdf) 950 if (params.print_to_pdf)
918 return false; 951 return WebKit::WebPrintScalingOptionSourceSize;
919 952
920 if (!source_is_html) { 953 if (!source_is_html) {
921 if (!FitToPageEnabled(job_settings)) 954 if (!FitToPageEnabled(job_settings))
922 return false; 955 return WebKit::WebPrintScalingOptionNone;
923 956
924 // Get the print scaling option for the initiator renderer pdf. 957 bool no_plugin_scaling =
925 bool print_scaling_disabled_for_plugin =
926 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( 958 print_preview_context_.frame()->isPrintScalingDisabledForPlugin(
927 print_preview_context_.node()); 959 print_preview_context_.node());
928 960
929 // If this is the first preview request, UI doesn't know about the print 961 if (params.is_first_request && no_plugin_scaling)
930 // scaling option of the plugin. Therefore, check the print scaling option 962 return WebKit::WebPrintScalingOptionNone;
931 // and update the print params accordingly.
932 //
933 // If this is not the first preview request, update print params based on
934 // preview job settings.
935 if (params.is_first_request && print_scaling_disabled_for_plugin)
936 return false;
937 } 963 }
938 return true; 964 return WebKit::WebPrintScalingOptionFitToPrintableArea;
939 } 965 }
940 966
941 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 967 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
942 DCHECK(is_preview_enabled_); 968 DCHECK(is_preview_enabled_);
943 print_preview_context_.OnPrintPreview(); 969 print_preview_context_.OnPrintPreview();
944 970
945 if (!UpdatePrintSettings(print_preview_context_.frame(), 971 if (!UpdatePrintSettings(print_preview_context_.frame(),
946 print_preview_context_.node(), settings)) { 972 print_preview_context_.node(), settings)) {
947 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { 973 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
948 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( 974 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 // static - Not anonymous so that platform implementations can use it. 1348 // static - Not anonymous so that platform implementations can use it.
1323 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( 1349 void PrintWebViewHelper::ComputePageLayoutInPointsForCss(
1324 WebFrame* frame, 1350 WebFrame* frame,
1325 int page_index, 1351 int page_index,
1326 const PrintMsg_Print_Params& page_params, 1352 const PrintMsg_Print_Params& page_params,
1327 bool ignore_css_margins, 1353 bool ignore_css_margins,
1328 double* scale_factor, 1354 double* scale_factor,
1329 PageSizeMargins* page_layout_in_points) { 1355 PageSizeMargins* page_layout_in_points) {
1330 PrintMsg_Print_Params params = CalculatePrintParamsForCss( 1356 PrintMsg_Print_Params params = CalculatePrintParamsForCss(
1331 frame, page_index, page_params, ignore_css_margins, 1357 frame, page_index, page_params, ignore_css_margins,
1332 page_params.fit_to_paper_size, scale_factor); 1358 page_params.print_scaling_option ==
1359 WebKit::WebPrintScalingOptionFitToPrintableArea,
1360 scale_factor);
1333 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); 1361 CalculatePageLayoutFromPrintParams(params, page_layout_in_points);
1334 } 1362 }
1335 1363
1336 // static - Not anonymous so that platform implementations can use it. 1364 // static - Not anonymous so that platform implementations can use it.
1337 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( 1365 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout(
1338 WebFrame* frame, 1366 WebFrame* frame,
1339 const WebNode& node, 1367 const WebNode& node,
1340 PrepareFrameAndViewForPrint* prepare, 1368 PrepareFrameAndViewForPrint* prepare,
1341 const PrintMsg_Print_Params& params, 1369 const PrintMsg_Print_Params& params,
1342 bool ignore_css_margins) { 1370 bool ignore_css_margins) {
1343 if (PrintingNodeOrPdfFrame(frame, node)) 1371 if (PrintingNodeOrPdfFrame(frame, node))
1344 return; 1372 return;
1373 bool fit_to_page = ignore_css_margins &&
1374 params.print_scaling_option ==
1375 WebKit::WebPrintScalingOptionFitToPrintableArea;
1345 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( 1376 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss(
1346 frame, 0, params, ignore_css_margins, 1377 frame, 0, params, ignore_css_margins, fit_to_page, NULL);
1347 ignore_css_margins && params.fit_to_paper_size, NULL);
1348 prepare->UpdatePrintParams(print_params); 1378 prepare->UpdatePrintParams(print_params);
1349 } 1379 }
1350 1380
1351 bool PrintWebViewHelper::InitPrintSettings() { 1381 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) {
1352 PrintMsg_PrintPages_Params settings; 1382 PrintMsg_PrintPages_Params settings;
1353 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), 1383 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
1354 &settings.params)); 1384 &settings.params));
1355 // Check if the printer returned any settings, if the settings is empty, we 1385 // Check if the printer returned any settings, if the settings is empty, we
1356 // can safely assume there are no printer drivers configured. So we safely 1386 // can safely assume there are no printer drivers configured. So we safely
1357 // terminate. 1387 // terminate.
1358 bool result = true; 1388 bool result = true;
1359 if (!PrintMsg_Print_Params_IsValid(settings.params)) 1389 if (!PrintMsg_Print_Params_IsValid(settings.params))
1360 result = false; 1390 result = false;
1361 1391
1362 if (result && 1392 if (result &&
1363 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { 1393 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) {
1364 // Invalid print page settings. 1394 // Invalid print page settings.
1365 NOTREACHED(); 1395 NOTREACHED();
1366 result = false; 1396 result = false;
1367 } 1397 }
1368 1398
1369 // Reset to default values. 1399 // Reset to default values.
1370 ignore_css_margins_ = false; 1400 ignore_css_margins_ = false;
1371 settings.params.fit_to_paper_size = true;
1372 settings.pages.clear(); 1401 settings.pages.clear();
1373 1402
1403 settings.params.print_scaling_option =
1404 WebKit::WebPrintScalingOptionSourceSize;
1405 if (fit_to_paper_size) {
1406 settings.params.print_scaling_option =
1407 WebKit::WebPrintScalingOptionFitToPrintableArea;
1408 }
1409
1374 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1410 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1375 return result; 1411 return result;
1376 } 1412 }
1377 1413
1378 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( 1414 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame(
1379 WebKit::WebFrame* frame, const WebKit::WebNode& node, 1415 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1380 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { 1416 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
1381 DCHECK(frame); 1417 DCHECK(frame);
1382 if (!InitPrintSettings()) { 1418
1419 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node));
1420 if (!InitPrintSettings(fit_to_paper_size)) {
1383 notify_browser_of_print_failure_ = false; 1421 notify_browser_of_print_failure_ = false;
1384 render_view()->RunModalAlertDialog( 1422 render_view()->RunModalAlertDialog(
1385 frame, 1423 frame,
1386 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); 1424 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1387 return false; 1425 return false;
1388 } 1426 }
1389 1427
1390 DCHECK(!prepare->get()); 1428 DCHECK(!prepare->get());
1391 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1429 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1392 frame, node)); 1430 frame, node));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1469 !job_settings->GetInteger(printing::kPreviewRequestID, 1507 !job_settings->GetInteger(printing::kPreviewRequestID,
1470 &(settings.params.preview_request_id)) || 1508 &(settings.params.preview_request_id)) ||
1471 !job_settings->GetBoolean(printing::kIsFirstRequest, 1509 !job_settings->GetBoolean(printing::kIsFirstRequest,
1472 &(settings.params.is_first_request))) { 1510 &(settings.params.is_first_request))) {
1473 NOTREACHED(); 1511 NOTREACHED();
1474 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1512 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1475 return false; 1513 return false;
1476 } 1514 }
1477 1515
1478 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); 1516 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings);
1479 settings.params.fit_to_paper_size = IsFitToPaperSizeRequested( 1517 UpdateFrameMarginsCssInfo(*job_settings);
1518 settings.params.print_scaling_option = GetPrintScalingOption(
1480 source_is_html, *job_settings, settings.params); 1519 source_is_html, *job_settings, settings.params);
1481 UpdateFrameMarginsCssInfo(*job_settings);
1482 1520
1483 // Header/Footer: Set |header_footer_info_|. 1521 // Header/Footer: Set |header_footer_info_|.
1484 if (settings.params.display_header_footer) { 1522 if (settings.params.display_header_footer) {
1485 header_footer_info_.reset(new DictionaryValue()); 1523 header_footer_info_.reset(new DictionaryValue());
1486 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, 1524 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
1487 settings.params.date); 1525 settings.params.date);
1488 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, 1526 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
1489 settings.params.url); 1527 settings.params.url);
1490 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, 1528 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
1491 settings.params.title); 1529 settings.params.title);
(...skipping 22 matching lines...) Expand all
1514 params.expected_pages_count = expected_pages_count; 1552 params.expected_pages_count = expected_pages_count;
1515 printing::MarginType margin_type = printing::DEFAULT_MARGINS; 1553 printing::MarginType margin_type = printing::DEFAULT_MARGINS;
1516 if (PrintingNodeOrPdfFrame(frame, node)) 1554 if (PrintingNodeOrPdfFrame(frame, node))
1517 margin_type = GetMarginsForPdf(frame, node); 1555 margin_type = GetMarginsForPdf(frame, node);
1518 params.margin_type = margin_type; 1556 params.margin_type = margin_type;
1519 1557
1520 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); 1558 Send(new PrintHostMsg_DidShowPrintDialog(routing_id()));
1521 1559
1522 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the 1560 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the
1523 // value before and restore it afterwards. 1561 // value before and restore it afterwards.
1524 bool fit_to_paper_size = print_pages_params_->params.fit_to_paper_size; 1562 WebKit::WebPrintScalingOption scaling_option =
1563 print_pages_params_->params.print_scaling_option;
1525 1564
1526 print_pages_params_.reset(); 1565 print_pages_params_.reset();
1527 IPC::SyncMessage* msg = 1566 IPC::SyncMessage* msg =
1528 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); 1567 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings);
1529 msg->EnableMessagePumping(); 1568 msg->EnableMessagePumping();
1530 Send(msg); 1569 Send(msg);
1531 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); 1570 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings));
1532 1571
1533 print_pages_params_->params.fit_to_paper_size = fit_to_paper_size; 1572 print_pages_params_->params.print_scaling_option = scaling_option;
1534 return (print_settings.params.dpi && print_settings.params.document_cookie); 1573 return (print_settings.params.dpi && print_settings.params.document_cookie);
1535 } 1574 }
1536 1575
1537 bool PrintWebViewHelper::RenderPagesForPrint( 1576 bool PrintWebViewHelper::RenderPagesForPrint(
1538 WebKit::WebFrame* frame, 1577 WebKit::WebFrame* frame,
1539 const WebKit::WebNode& node) { 1578 const WebKit::WebNode& node) {
1540 if (print_pages_params_->params.selection_only) 1579 if (print_pages_params_->params.selection_only)
1541 return CopyAndPrint(frame); 1580 return CopyAndPrint(frame);
1542 return PrintPages(frame, node); 1581 return PrintPages(frame, node);
1543 } 1582 }
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
1909 const PrintMsg_Print_Params& 1948 const PrintMsg_Print_Params&
1910 PrintWebViewHelper::PrintPreviewContext::print_params() const { 1949 PrintWebViewHelper::PrintPreviewContext::print_params() const {
1911 DCHECK(state_ != UNINITIALIZED); 1950 DCHECK(state_ != UNINITIALIZED);
1912 return *print_params_; 1951 return *print_params_;
1913 } 1952 }
1914 1953
1915 int PrintWebViewHelper::PrintPreviewContext::last_error() const { 1954 int PrintWebViewHelper::PrintPreviewContext::last_error() const {
1916 return error_; 1955 return error_;
1917 } 1956 }
1918 1957
1919 const gfx::Size& 1958 gfx::Size PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1920 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1921 DCHECK(IsRendering()); 1959 DCHECK(IsRendering());
1922 return prep_frame_view_->GetPrintCanvasSize(); 1960 return prep_frame_view_->GetPrintCanvasSize();
1923 } 1961 }
1924 1962
1925 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1963 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1926 prep_frame_view_.reset(); 1964 prep_frame_view_.reset();
1927 metafile_.reset(); 1965 metafile_.reset();
1928 pages_to_render_.clear(); 1966 pages_to_render_.clear();
1929 error_ = PREVIEW_ERROR_NONE; 1967 error_ = PREVIEW_ERROR_NONE;
1930 } 1968 }
OLDNEW
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | webkit/glue/webkit_glue.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698