OLD | NEW |
---|---|
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" |
(...skipping 15 matching lines...) Expand all Loading... | |
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/WebConsoleMessage.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginDocument.h" | 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginDocument.h" |
35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h" | 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h" |
36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPrintParams.h" | |
36 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" | 37 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
37 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" | 38 #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/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" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 const PrintMsg_PrintPages_Params& oldParams, | 131 const PrintMsg_PrintPages_Params& oldParams, |
131 const PrintMsg_PrintPages_Params& newParams) { | 132 const PrintMsg_PrintPages_Params& newParams) { |
132 return PageLayoutIsEqual(oldParams, newParams) && | 133 return PageLayoutIsEqual(oldParams, newParams) && |
133 oldParams.params.max_shrink == newParams.params.max_shrink && | 134 oldParams.params.max_shrink == newParams.params.max_shrink && |
134 oldParams.params.min_shrink == newParams.params.min_shrink && | 135 oldParams.params.min_shrink == newParams.params.min_shrink && |
135 oldParams.params.selection_only == newParams.params.selection_only && | 136 oldParams.params.selection_only == newParams.params.selection_only && |
136 oldParams.params.supports_alpha_blend == | 137 oldParams.params.supports_alpha_blend == |
137 newParams.params.supports_alpha_blend && | 138 newParams.params.supports_alpha_blend && |
138 oldParams.pages.size() == newParams.pages.size() && | 139 oldParams.pages.size() == newParams.pages.size() && |
139 oldParams.params.print_to_pdf == newParams.params.print_to_pdf && | 140 oldParams.params.print_to_pdf == newParams.params.print_to_pdf && |
140 oldParams.params.fit_to_paper_size == | 141 oldParams.params.print_scaling_option == |
141 newParams.params.fit_to_paper_size && | 142 newParams.params.print_scaling_option && |
142 oldParams.params.display_header_footer == | 143 oldParams.params.display_header_footer == |
143 newParams.params.display_header_footer && | 144 newParams.params.display_header_footer && |
144 oldParams.params.date == newParams.params.date && | 145 oldParams.params.date == newParams.params.date && |
145 oldParams.params.title == newParams.params.title && | 146 oldParams.params.title == newParams.params.title && |
146 oldParams.params.url == newParams.params.url && | 147 oldParams.params.url == newParams.params.url && |
147 std::equal(oldParams.pages.begin(), oldParams.pages.end(), | 148 std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
148 newParams.pages.begin()); | 149 newParams.pages.begin()); |
149 } | 150 } |
150 | 151 |
151 PrintMsg_Print_Params GetCssPrintParams( | 152 PrintMsg_Print_Params GetCssPrintParams( |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 // Swap the |width| and |height| values. | 300 // Swap the |width| and |height| values. |
300 page_params->page_size.SetSize(page_params->page_size.height(), | 301 page_params->page_size.SetSize(page_params->page_size.height(), |
301 page_params->page_size.width()); | 302 page_params->page_size.width()); |
302 page_params->content_size.SetSize(page_params->content_size.height(), | 303 page_params->content_size.SetSize(page_params->content_size.height(), |
303 page_params->content_size.width()); | 304 page_params->content_size.width()); |
304 page_params->printable_area.set_size( | 305 page_params->printable_area.set_size( |
305 gfx::Size(page_params->printable_area.height(), | 306 gfx::Size(page_params->printable_area.height(), |
306 page_params->printable_area.width())); | 307 page_params->printable_area.width())); |
307 } | 308 } |
308 | 309 |
309 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, | 310 void ComputePrintParamsInDesiredDpi(const PrintMsg_Print_Params& print_params, |
310 gfx::Size* result) { | 311 gfx::Size* content_size, |
312 gfx::Rect* printable_area, | |
313 gfx::Size* paper_size) { | |
311 int dpi = GetDPI(&print_params); | 314 int dpi = GetDPI(&print_params); |
312 result->set_width(ConvertUnit(print_params.content_size.width(), dpi, | 315 content_size->set_width(ConvertUnit(print_params.content_size.width(), dpi, |
313 print_params.desired_dpi)); | 316 print_params.desired_dpi)); |
314 | 317 |
315 result->set_height(ConvertUnit(print_params.content_size.height(), dpi, | 318 content_size->set_height(ConvertUnit(print_params.content_size.height(), dpi, |
316 print_params.desired_dpi)); | 319 print_params.desired_dpi)); |
320 | |
321 printable_area->SetRect(ConvertUnit(print_params.printable_area.x(), dpi, | |
322 print_params.desired_dpi), | |
323 ConvertUnit(print_params.printable_area.y(), dpi, | |
324 print_params.desired_dpi), | |
325 ConvertUnit(print_params.printable_area.width(), dpi, | |
326 print_params.desired_dpi), | |
327 ConvertUnit(print_params.printable_area.height(), dpi, | |
328 print_params.desired_dpi)); | |
329 paper_size->SetSize(ConvertUnit(print_params.page_size.width(), dpi, | |
330 print_params.desired_dpi), | |
331 ConvertUnit(print_params.page_size.height(), dpi, | |
332 print_params.desired_dpi)); | |
317 } | 333 } |
318 | 334 |
319 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { | 335 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { |
320 if (!node.isNull()) | 336 if (!node.isNull()) |
321 return true; | 337 return true; |
322 if (!frame->document().isPluginDocument()) | 338 if (!frame->document().isPluginDocument()) |
323 return false; | 339 return false; |
324 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin(); | 340 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin(); |
325 return plugin && plugin->supportsPaginatedPrint(); | 341 return plugin && plugin->supportsPaginatedPrint(); |
326 } | 342 } |
(...skipping 11 matching lines...) Expand all Loading... | |
338 return frame_has_custom_page_size_style; | 354 return frame_has_custom_page_size_style; |
339 } | 355 } |
340 | 356 |
341 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { | 357 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { |
342 if (frame->isPrintScalingDisabledForPlugin(node)) | 358 if (frame->isPrintScalingDisabledForPlugin(node)) |
343 return printing::NO_MARGINS; | 359 return printing::NO_MARGINS; |
344 else | 360 else |
345 return printing::PRINTABLE_AREA_MARGINS; | 361 return printing::PRINTABLE_AREA_MARGINS; |
346 } | 362 } |
347 | 363 |
364 bool FitToPageEnabled(const DictionaryValue& job_settings) { | |
365 bool fit_to_paper_size = false; | |
366 if (!job_settings.GetBoolean(printing::kSettingFitToPageEnabled, | |
367 &fit_to_paper_size)) { | |
368 NOTREACHED(); | |
369 } | |
370 return fit_to_paper_size; | |
371 } | |
372 | |
348 // Get the (x, y) coordinate from where printing of the current text should | 373 // Get the (x, y) coordinate from where printing of the current text should |
349 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and | 374 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and |
350 // vertical alignment (TOP, BOTTOM). | 375 // vertical alignment (TOP, BOTTOM). |
351 SkPoint GetHeaderFooterPosition( | 376 SkPoint GetHeaderFooterPosition( |
352 float webkit_scale_factor, | 377 float webkit_scale_factor, |
353 const PageSizeMargins& page_layout, | 378 const PageSizeMargins& page_layout, |
354 printing::HorizontalHeaderFooterPosition horizontal_position, | 379 printing::HorizontalHeaderFooterPosition horizontal_position, |
355 printing::VerticalHeaderFooterPosition vertical_position, | 380 printing::VerticalHeaderFooterPosition vertical_position, |
356 double offset_to_baseline, | 381 double offset_to_baseline, |
357 double text_width_in_points) { | 382 double text_width_in_points) { |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
670 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 695 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
671 const PrintMsg_Print_Params& print_params, | 696 const PrintMsg_Print_Params& print_params, |
672 WebFrame* frame, | 697 WebFrame* frame, |
673 const WebNode& node) | 698 const WebNode& node) |
674 : frame_(frame), | 699 : frame_(frame), |
675 node_to_print_(node), | 700 node_to_print_(node), |
676 web_view_(frame->view()), | 701 web_view_(frame->view()), |
677 dpi_(static_cast<int>(print_params.dpi)), | 702 dpi_(static_cast<int>(print_params.dpi)), |
678 expected_pages_count_(0), | 703 expected_pages_count_(0), |
679 use_browser_overlays_(true), | 704 use_browser_overlays_(true), |
705 print_scaling_option_(print_params.print_scaling_option), | |
680 finished_(false) { | 706 finished_(false) { |
681 gfx::Size canvas_size; | 707 gfx::Size canvas_size; |
682 CalculatePrintCanvasSize(print_params, &canvas_size); | 708 gfx::Rect printable_area; |
709 gfx::Size paper_size; | |
710 ComputePrintParamsInDesiredDpi(print_params, &canvas_size, &printable_area, | |
711 &paper_size); | |
683 | 712 |
684 if (WebFrame* web_frame = web_view_->mainFrame()) | 713 if (WebFrame* web_frame = web_view_->mainFrame()) |
685 prev_scroll_offset_ = web_frame->scrollOffset(); | 714 prev_scroll_offset_ = web_frame->scrollOffset(); |
686 prev_view_size_ = web_view_->size(); | 715 prev_view_size_ = web_view_->size(); |
687 | 716 |
688 StartPrinting(canvas_size); | 717 StartPrinting(canvas_size, printable_area, paper_size); |
689 } | 718 } |
690 | 719 |
691 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { | 720 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
692 FinishPrinting(); | 721 FinishPrinting(); |
693 } | 722 } |
694 | 723 |
695 void PrepareFrameAndViewForPrint::UpdatePrintParams( | 724 void PrepareFrameAndViewForPrint::UpdatePrintParams( |
696 const PrintMsg_Print_Params& print_params) { | 725 const PrintMsg_Print_Params& print_params) { |
697 DCHECK(!finished_); | 726 DCHECK(!finished_); |
698 gfx::Size canvas_size; | 727 gfx::Size canvas_size; |
699 CalculatePrintCanvasSize(print_params, &canvas_size); | 728 gfx::Rect printable_area; |
700 if (canvas_size == print_canvas_size_) | 729 gfx::Size paper_size; |
730 ComputePrintParamsInDesiredDpi(print_params, &canvas_size, &printable_area, | |
731 &paper_size); | |
732 if (canvas_size == print_canvas_size_ && | |
733 printable_area == printable_area_ && | |
734 paper_size == paper_size_ && | |
735 print_params.print_scaling_option == print_scaling_option_) { | |
701 return; | 736 return; |
737 } | |
702 | 738 |
703 frame_->printEnd(); | 739 frame_->printEnd(); |
704 dpi_ = static_cast<int>(print_params.dpi); | 740 dpi_ = static_cast<int>(print_params.dpi); |
705 StartPrinting(canvas_size); | 741 print_scaling_option_ = print_params.print_scaling_option; |
742 StartPrinting(canvas_size, printable_area, paper_size); | |
706 } | 743 } |
707 | 744 |
708 void PrepareFrameAndViewForPrint::StartPrinting( | 745 void PrepareFrameAndViewForPrint::StartPrinting( |
709 const gfx::Size& print_canvas_size) { | 746 const gfx::Size& print_canvas_size, |
747 const gfx::Rect& printable_area, | |
748 const gfx::Size& paper_size) { | |
710 print_canvas_size_ = print_canvas_size; | 749 print_canvas_size_ = print_canvas_size; |
750 printable_area_ = printable_area; | |
751 paper_size_ = paper_size; | |
711 | 752 |
712 // Layout page according to printer page size. Since WebKit shrinks the | 753 // Layout page according to printer page size. Since WebKit shrinks the |
713 // size of the page automatically (from 125% to 200%) we trick it to | 754 // size of the page automatically (from 125% to 200%) we trick it to |
714 // think the page is 125% larger so the size of the page is correct for | 755 // think the page is 125% larger so the size of the page is correct for |
715 // minimum (default) scaling. | 756 // minimum (default) scaling. |
716 // This is important for sites that try to fill the page. | 757 // This is important for sites that try to fill the page. |
717 gfx::Size print_layout_size(print_canvas_size_); | 758 gfx::Size print_layout_size(print_canvas_size_); |
718 print_layout_size.set_height(static_cast<int>( | 759 print_layout_size.set_height(static_cast<int>( |
719 static_cast<double>(print_layout_size.height()) * 1.25)); | 760 static_cast<double>(print_layout_size.height()) * 1.25)); |
720 | 761 |
721 web_view_->resize(print_layout_size); | 762 web_view_->resize(print_layout_size); |
722 | 763 |
723 expected_pages_count_ = frame_->printBegin(print_canvas_size_, node_to_print_, | 764 WebKit::WebPrintParams webkit_print_params; |
724 dpi_, &use_browser_overlays_); | 765 webkit_print_params.printContentArea = gfx::Rect(print_canvas_size); |
766 webkit_print_params.printableArea = printable_area_; | |
767 webkit_print_params.paperSize = paper_size_; | |
768 webkit_print_params.printerDPI = dpi_; | |
769 webkit_print_params.printScalingOption = print_scaling_option_; | |
770 | |
771 expected_pages_count_ = frame_->printBegin(webkit_print_params, | |
kmadhusu
2012/05/11 21:36:03
I constructed the |webkit_print_params| right befo
| |
772 node_to_print_, | |
773 &use_browser_overlays_); | |
725 } | 774 } |
726 | 775 |
727 void PrepareFrameAndViewForPrint::FinishPrinting() { | 776 void PrepareFrameAndViewForPrint::FinishPrinting() { |
728 if (!finished_) { | 777 if (!finished_) { |
729 finished_ = true; | 778 finished_ = true; |
730 frame_->printEnd(); | 779 frame_->printEnd(); |
731 web_view_->resize(prev_view_size_); | 780 web_view_->resize(prev_view_size_); |
732 if (WebFrame* web_frame = web_view_->mainFrame()) | 781 if (WebFrame* web_frame = web_view_->mainFrame()) |
733 web_frame->setScrollOffset(prev_scroll_offset_); | 782 web_frame->setScrollOffset(prev_scroll_offset_); |
734 } | 783 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
896 } | 945 } |
897 | 946 |
898 bool PrintWebViewHelper::IsPrintToPdfRequested( | 947 bool PrintWebViewHelper::IsPrintToPdfRequested( |
899 const DictionaryValue& job_settings) { | 948 const DictionaryValue& job_settings) { |
900 bool print_to_pdf = false; | 949 bool print_to_pdf = false; |
901 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) | 950 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) |
902 NOTREACHED(); | 951 NOTREACHED(); |
903 return print_to_pdf; | 952 return print_to_pdf; |
904 } | 953 } |
905 | 954 |
955 WebKit::WebPrintScalingOption PrintWebViewHelper::GetPrintScalingOption( | |
956 bool source_is_html, const DictionaryValue& job_settings, | |
957 const PrintMsg_Print_Params& params) { | |
958 DCHECK(!print_for_preview_); | |
959 | |
960 // Do not fit to paper size when the user is saving the print contents as pdf. | |
961 if (params.print_to_pdf) | |
962 return WebKit::WebPrintScalingOptionSourceSize; | |
963 | |
964 if (!source_is_html) { | |
965 if (!FitToPageEnabled(job_settings)) | |
966 return WebKit::WebPrintScalingOptionNone; | |
967 | |
968 // Get the print scaling option for the initiator renderer pdf. | |
969 bool print_scaling_disabled_for_plugin = | |
970 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( | |
971 print_preview_context_.node()); | |
972 | |
973 // If this is the first preview request, UI doesn't know about the print | |
974 // scaling option of the plugin. Therefore, check the print scaling option | |
975 // and update the print params accordingly. | |
976 // | |
977 // If this is not the first preview request, update print params based on | |
978 // preview job settings. | |
979 if (params.is_first_request && print_scaling_disabled_for_plugin) | |
980 return WebKit::WebPrintScalingOptionNone; | |
981 } | |
982 return WebKit::WebPrintScalingOptionFitToPrintableArea; | |
983 } | |
984 | |
906 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 985 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
907 DCHECK(is_preview_enabled_); | 986 DCHECK(is_preview_enabled_); |
908 print_preview_context_.OnPrintPreview(); | 987 print_preview_context_.OnPrintPreview(); |
909 | 988 |
910 if (!UpdatePrintSettings(print_preview_context_.frame(), | 989 if (!UpdatePrintSettings(print_preview_context_.frame(), |
911 print_preview_context_.node(), settings)) { | 990 print_preview_context_.node(), settings)) { |
912 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 991 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
913 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 992 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
914 routing_id(), print_pages_params_->params.document_cookie)); | 993 routing_id(), print_pages_params_->params.document_cookie)); |
915 notify_browser_of_print_failure_ = false; // Already sent. | 994 notify_browser_of_print_failure_ = false; // Already sent. |
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1277 // static - Not anonymous so that platform implementations can use it. | 1356 // static - Not anonymous so that platform implementations can use it. |
1278 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( | 1357 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( |
1279 WebFrame* frame, | 1358 WebFrame* frame, |
1280 int page_index, | 1359 int page_index, |
1281 const PrintMsg_Print_Params& page_params, | 1360 const PrintMsg_Print_Params& page_params, |
1282 bool ignore_css_margins, | 1361 bool ignore_css_margins, |
1283 double* scale_factor, | 1362 double* scale_factor, |
1284 PageSizeMargins* page_layout_in_points) { | 1363 PageSizeMargins* page_layout_in_points) { |
1285 PrintMsg_Print_Params params = CalculatePrintParamsForCss( | 1364 PrintMsg_Print_Params params = CalculatePrintParamsForCss( |
1286 frame, page_index, page_params, ignore_css_margins, | 1365 frame, page_index, page_params, ignore_css_margins, |
1287 page_params.fit_to_paper_size, scale_factor); | 1366 page_params.print_scaling_option == |
1367 WebKit::WebPrintScalingOptionFitToPrintableArea, | |
1368 scale_factor); | |
1288 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); | 1369 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); |
1289 } | 1370 } |
1290 | 1371 |
1291 // static - Not anonymous so that platform implementations can use it. | 1372 // static - Not anonymous so that platform implementations can use it. |
1292 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( | 1373 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( |
1293 WebFrame* frame, | 1374 WebFrame* frame, |
1294 const WebNode& node, | 1375 const WebNode& node, |
1295 PrepareFrameAndViewForPrint* prepare, | 1376 PrepareFrameAndViewForPrint* prepare, |
1296 const PrintMsg_Print_Params& params, | 1377 const PrintMsg_Print_Params& params, |
1297 bool ignore_css_margins) { | 1378 bool ignore_css_margins) { |
1298 if (PrintingNodeOrPdfFrame(frame, node)) | 1379 if (PrintingNodeOrPdfFrame(frame, node)) |
1299 return; | 1380 return; |
1300 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( | 1381 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( |
1301 frame, 0, params, ignore_css_margins, | 1382 frame, 0, params, ignore_css_margins, |
1302 ignore_css_margins && params.fit_to_paper_size, NULL); | 1383 ignore_css_margins && |
1384 params.print_scaling_option == | |
1385 WebKit::WebPrintScalingOptionFitToPrintableArea, | |
1386 NULL); | |
1303 prepare->UpdatePrintParams(print_params); | 1387 prepare->UpdatePrintParams(print_params); |
1304 } | 1388 } |
1305 | 1389 |
1306 bool PrintWebViewHelper::InitPrintSettings() { | 1390 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) { |
1307 PrintMsg_PrintPages_Params settings; | 1391 PrintMsg_PrintPages_Params settings; |
1308 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1392 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
1309 &settings.params)); | 1393 &settings.params)); |
1310 // Check if the printer returned any settings, if the settings is empty, we | 1394 // Check if the printer returned any settings, if the settings is empty, we |
1311 // can safely assume there are no printer drivers configured. So we safely | 1395 // can safely assume there are no printer drivers configured. So we safely |
1312 // terminate. | 1396 // terminate. |
1313 bool result = true; | 1397 bool result = true; |
1314 if (!PrintMsg_Print_Params_IsValid(settings.params)) | 1398 if (!PrintMsg_Print_Params_IsValid(settings.params)) |
1315 result = false; | 1399 result = false; |
1316 | 1400 |
1317 if (result && | 1401 if (result && |
1318 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { | 1402 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { |
1319 // Invalid print page settings. | 1403 // Invalid print page settings. |
1320 NOTREACHED(); | 1404 NOTREACHED(); |
1321 result = false; | 1405 result = false; |
1322 } | 1406 } |
1323 | 1407 |
1324 // Reset to default values. | 1408 // Reset to default values. |
1325 ignore_css_margins_ = false; | 1409 ignore_css_margins_ = false; |
1326 settings.params.fit_to_paper_size = true; | |
1327 settings.pages.clear(); | 1410 settings.pages.clear(); |
1411 settings.params.print_scaling_option = fit_to_paper_size ? | |
1412 WebKit::WebPrintScalingOptionFitToPrintableArea : | |
1413 WebKit::WebPrintScalingOptionSourceSize; | |
1328 | 1414 |
1329 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1415 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
1330 return result; | 1416 return result; |
1331 } | 1417 } |
1332 | 1418 |
1333 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1419 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
1334 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1420 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
1335 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | 1421 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
1336 DCHECK(frame); | 1422 DCHECK(frame); |
1337 if (!InitPrintSettings()) { | 1423 |
1424 // If the source is html, fit to paper size else print the source pdf size. | |
1425 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); | |
1426 if (!InitPrintSettings(fit_to_paper_size)) { | |
1338 notify_browser_of_print_failure_ = false; | 1427 notify_browser_of_print_failure_ = false; |
1339 render_view()->RunModalAlertDialog( | 1428 render_view()->RunModalAlertDialog( |
1340 frame, | 1429 frame, |
1341 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | 1430 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
1342 return false; | 1431 return false; |
1343 } | 1432 } |
1344 | 1433 |
1345 DCHECK(!prepare->get()); | 1434 DCHECK(!prepare->get()); |
1346 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1435 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
1347 frame, node)); | 1436 frame, node)); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1443 &(settings.params.preview_request_id)) || | 1532 &(settings.params.preview_request_id)) || |
1444 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1533 !job_settings->GetBoolean(printing::kIsFirstRequest, |
1445 &(settings.params.is_first_request))) { | 1534 &(settings.params.is_first_request))) { |
1446 NOTREACHED(); | 1535 NOTREACHED(); |
1447 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1536 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1448 return false; | 1537 return false; |
1449 } | 1538 } |
1450 | 1539 |
1451 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); | 1540 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
1452 UpdateFrameMarginsCssInfo(*job_settings); | 1541 UpdateFrameMarginsCssInfo(*job_settings); |
1453 | 1542 settings.params.print_scaling_option = GetPrintScalingOption( |
1454 // Fit to paper size. | 1543 source_is_html, *job_settings, settings.params); |
1455 settings.params.fit_to_paper_size = source_is_html && | |
1456 !IsPrintToPdfRequested(*job_settings); | |
1457 | 1544 |
1458 // Header/Footer: Set |header_footer_info_|. | 1545 // Header/Footer: Set |header_footer_info_|. |
1459 if (settings.params.display_header_footer) { | 1546 if (settings.params.display_header_footer) { |
1460 header_footer_info_.reset(new DictionaryValue()); | 1547 header_footer_info_.reset(new DictionaryValue()); |
1461 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1548 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
1462 settings.params.date); | 1549 settings.params.date); |
1463 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1550 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
1464 settings.params.url); | 1551 settings.params.url); |
1465 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1552 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
1466 settings.params.title); | 1553 settings.params.title); |
(...skipping 20 matching lines...) Expand all Loading... | |
1487 params.cookie = print_pages_params_->params.document_cookie; | 1574 params.cookie = print_pages_params_->params.document_cookie; |
1488 params.has_selection = frame->hasSelection(); | 1575 params.has_selection = frame->hasSelection(); |
1489 params.expected_pages_count = expected_pages_count; | 1576 params.expected_pages_count = expected_pages_count; |
1490 printing::MarginType margin_type = printing::DEFAULT_MARGINS; | 1577 printing::MarginType margin_type = printing::DEFAULT_MARGINS; |
1491 if (PrintingNodeOrPdfFrame(frame, node)) | 1578 if (PrintingNodeOrPdfFrame(frame, node)) |
1492 margin_type = GetMarginsForPdf(frame, node); | 1579 margin_type = GetMarginsForPdf(frame, node); |
1493 params.margin_type = margin_type; | 1580 params.margin_type = margin_type; |
1494 | 1581 |
1495 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); | 1582 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); |
1496 | 1583 |
1584 // Store the calculated print scaling option value. | |
1585 WebKit::WebPrintScalingOption scaling_option = | |
1586 print_pages_params_->params.print_scaling_option; | |
1497 print_pages_params_.reset(); | 1587 print_pages_params_.reset(); |
1498 IPC::SyncMessage* msg = | 1588 IPC::SyncMessage* msg = |
1499 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); | 1589 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); |
1500 msg->EnableMessagePumping(); | 1590 msg->EnableMessagePumping(); |
1501 Send(msg); | 1591 Send(msg); |
1592 | |
1593 // When we get new |print_settings| from native print dialog we will have | |
1594 // |print_scaling_option| value set to | |
1595 // WebKit::WebPrintScalingOptionSourceSize. So we should restore the | |
1596 // calculated value in new |print_settings|. | |
1597 print_settings.params.print_scaling_option = scaling_option; | |
1598 | |
1502 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); | 1599 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); |
1503 return (print_settings.params.dpi && print_settings.params.document_cookie); | 1600 return (print_settings.params.dpi && print_settings.params.document_cookie); |
1504 } | 1601 } |
1505 | 1602 |
1506 bool PrintWebViewHelper::RenderPagesForPrint( | 1603 bool PrintWebViewHelper::RenderPagesForPrint( |
1507 WebKit::WebFrame* frame, | 1604 WebKit::WebFrame* frame, |
1508 const WebKit::WebNode& node) { | 1605 const WebKit::WebNode& node) { |
1509 if (print_pages_params_->params.selection_only) | 1606 if (print_pages_params_->params.selection_only) |
1510 return CopyAndPrint(frame); | 1607 return CopyAndPrint(frame); |
1511 return PrintPages(frame, node); | 1608 return PrintPages(frame, node); |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1890 DCHECK(IsRendering()); | 1987 DCHECK(IsRendering()); |
1891 return prep_frame_view_->GetPrintCanvasSize(); | 1988 return prep_frame_view_->GetPrintCanvasSize(); |
1892 } | 1989 } |
1893 | 1990 |
1894 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1991 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1895 prep_frame_view_.reset(); | 1992 prep_frame_view_.reset(); |
1896 metafile_.reset(); | 1993 metafile_.reset(); |
1897 pages_to_render_.clear(); | 1994 pages_to_render_.clear(); |
1898 error_ = PREVIEW_ERROR_NONE; | 1995 error_ = PREVIEW_ERROR_NONE; |
1899 } | 1996 } |
OLD | NEW |