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/WebPrintScalingOption .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 ComputeWebKitPrintParamsInDesiredDpi( |
310 gfx::Size* result) { | 311 const PrintMsg_Print_Params& print_params, |
312 WebKit::WebPrintParams* webkit_print_params) { | |
311 int dpi = GetDPI(&print_params); | 313 int dpi = GetDPI(&print_params); |
312 result->set_width(ConvertUnit(print_params.content_size.width(), dpi, | 314 webkit_print_params->printerDPI = dpi; |
313 print_params.desired_dpi)); | 315 webkit_print_params->printScalingOption = print_params.print_scaling_option; |
314 | 316 |
315 result->set_height(ConvertUnit(print_params.content_size.height(), dpi, | 317 webkit_print_params->printContentArea.width = |
316 print_params.desired_dpi)); | 318 ConvertUnit(print_params.content_size.width(), dpi, |
319 print_params.desired_dpi); | |
320 webkit_print_params->printContentArea.height = | |
321 ConvertUnit(print_params.content_size.height(), dpi, | |
322 print_params.desired_dpi); | |
323 | |
324 webkit_print_params->printableArea.x = | |
325 ConvertUnit(print_params.printable_area.x(), dpi, | |
326 print_params.desired_dpi); | |
327 webkit_print_params->printableArea.y = | |
328 ConvertUnit(print_params.printable_area.y(), dpi, | |
329 print_params.desired_dpi); | |
330 webkit_print_params->printableArea.width = | |
331 ConvertUnit(print_params.printable_area.width(), dpi, | |
332 print_params.desired_dpi); | |
333 webkit_print_params->printableArea.height = | |
334 ConvertUnit(print_params.printable_area.height(), | |
335 dpi, print_params.desired_dpi); | |
336 | |
337 webkit_print_params->paperSize.width = | |
338 ConvertUnit(print_params.page_size.width(), dpi, | |
339 print_params.desired_dpi); | |
340 webkit_print_params->paperSize.height = | |
341 ConvertUnit(print_params.page_size.height(), dpi, | |
342 print_params.desired_dpi); | |
317 } | 343 } |
318 | 344 |
319 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { | 345 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { |
320 if (!node.isNull()) | 346 if (!node.isNull()) |
321 return true; | 347 return true; |
322 if (!frame->document().isPluginDocument()) | 348 if (!frame->document().isPluginDocument()) |
323 return false; | 349 return false; |
324 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin(); | 350 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin(); |
325 return plugin && plugin->supportsPaginatedPrint(); | 351 return plugin && plugin->supportsPaginatedPrint(); |
326 } | 352 } |
(...skipping 11 matching lines...) Expand all Loading... | |
338 return frame_has_custom_page_size_style; | 364 return frame_has_custom_page_size_style; |
339 } | 365 } |
340 | 366 |
341 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { | 367 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { |
342 if (frame->isPrintScalingDisabledForPlugin(node)) | 368 if (frame->isPrintScalingDisabledForPlugin(node)) |
343 return printing::NO_MARGINS; | 369 return printing::NO_MARGINS; |
344 else | 370 else |
345 return printing::PRINTABLE_AREA_MARGINS; | 371 return printing::PRINTABLE_AREA_MARGINS; |
346 } | 372 } |
347 | 373 |
374 bool FitToPageEnabled(const DictionaryValue& job_settings) { | |
375 bool fit_to_paper_size = false; | |
376 if (!job_settings.GetBoolean(printing::kSettingFitToPageEnabled, | |
377 &fit_to_paper_size)) { | |
378 NOTREACHED(); | |
379 } | |
380 return fit_to_paper_size; | |
381 } | |
382 | |
348 // Get the (x, y) coordinate from where printing of the current text should | 383 // Get the (x, y) coordinate from where printing of the current text should |
349 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and | 384 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and |
350 // vertical alignment (TOP, BOTTOM). | 385 // vertical alignment (TOP, BOTTOM). |
351 SkPoint GetHeaderFooterPosition( | 386 SkPoint GetHeaderFooterPosition( |
352 float webkit_scale_factor, | 387 float webkit_scale_factor, |
353 const PageSizeMargins& page_layout, | 388 const PageSizeMargins& page_layout, |
354 printing::HorizontalHeaderFooterPosition horizontal_position, | 389 printing::HorizontalHeaderFooterPosition horizontal_position, |
355 printing::VerticalHeaderFooterPosition vertical_position, | 390 printing::VerticalHeaderFooterPosition vertical_position, |
356 double offset_to_baseline, | 391 double offset_to_baseline, |
357 double text_width_in_points) { | 392 double text_width_in_points) { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
667 #endif | 702 #endif |
668 } | 703 } |
669 | 704 |
670 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 705 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
671 const PrintMsg_Print_Params& print_params, | 706 const PrintMsg_Print_Params& print_params, |
672 WebFrame* frame, | 707 WebFrame* frame, |
673 const WebNode& node) | 708 const WebNode& node) |
674 : frame_(frame), | 709 : frame_(frame), |
675 node_to_print_(node), | 710 node_to_print_(node), |
676 web_view_(frame->view()), | 711 web_view_(frame->view()), |
677 dpi_(static_cast<int>(print_params.dpi)), | |
678 expected_pages_count_(0), | 712 expected_pages_count_(0), |
679 use_browser_overlays_(true), | 713 use_browser_overlays_(true), |
680 finished_(false) { | 714 finished_(false) { |
681 gfx::Size canvas_size; | 715 WebKit::WebPrintParams webkit_print_params; |
682 CalculatePrintCanvasSize(print_params, &canvas_size); | 716 ComputeWebKitPrintParamsInDesiredDpi(print_params, &webkit_print_params); |
683 | 717 |
684 if (WebFrame* web_frame = web_view_->mainFrame()) | 718 if (WebFrame* web_frame = web_view_->mainFrame()) |
685 prev_scroll_offset_ = web_frame->scrollOffset(); | 719 prev_scroll_offset_ = web_frame->scrollOffset(); |
686 prev_view_size_ = web_view_->size(); | 720 prev_view_size_ = web_view_->size(); |
687 | 721 |
688 StartPrinting(canvas_size); | 722 StartPrinting(webkit_print_params); |
689 } | 723 } |
690 | 724 |
691 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { | 725 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
692 FinishPrinting(); | 726 FinishPrinting(); |
693 } | 727 } |
694 | 728 |
695 void PrepareFrameAndViewForPrint::UpdatePrintParams( | 729 void PrepareFrameAndViewForPrint::UpdatePrintParams( |
696 const PrintMsg_Print_Params& print_params) { | 730 const PrintMsg_Print_Params& print_params) { |
697 DCHECK(!finished_); | 731 DCHECK(!finished_); |
698 gfx::Size canvas_size; | 732 WebKit::WebPrintParams webkit_print_params; |
699 CalculatePrintCanvasSize(print_params, &canvas_size); | 733 ComputeWebKitPrintParamsInDesiredDpi(print_params, &webkit_print_params); |
700 if (canvas_size == print_canvas_size_) | 734 |
735 if (webkit_print_params.printContentArea == | |
736 web_print_params_.printContentArea && | |
737 webkit_print_params.printableArea == web_print_params_.printableArea && | |
738 webkit_print_params.paperSize == web_print_params_.paperSize && | |
739 webkit_print_params.printScalingOption == | |
740 web_print_params_.printScalingOption) { | |
701 return; | 741 return; |
742 } | |
702 | 743 |
703 frame_->printEnd(); | 744 frame_->printEnd(); |
704 dpi_ = static_cast<int>(print_params.dpi); | 745 StartPrinting(webkit_print_params); |
705 StartPrinting(canvas_size); | |
706 } | 746 } |
707 | 747 |
708 void PrepareFrameAndViewForPrint::StartPrinting( | 748 void PrepareFrameAndViewForPrint::StartPrinting( |
709 const gfx::Size& print_canvas_size) { | 749 const WebKit::WebPrintParams& webkit_print_params) { |
710 print_canvas_size_ = print_canvas_size; | 750 web_print_params_ = webkit_print_params; |
711 | 751 |
712 // Layout page according to printer page size. Since WebKit shrinks the | 752 // 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 | 753 // 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 | 754 // think the page is 125% larger so the size of the page is correct for |
715 // minimum (default) scaling. | 755 // minimum (default) scaling. |
716 // This is important for sites that try to fill the page. | 756 // This is important for sites that try to fill the page. |
717 gfx::Size print_layout_size(print_canvas_size_); | 757 gfx::Size print_layout_size(web_print_params_.printContentArea.width, |
758 web_print_params_.printContentArea.height); | |
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 expected_pages_count_ = frame_->printBegin(web_print_params_, |
724 dpi_, &use_browser_overlays_); | 765 node_to_print_, |
766 &use_browser_overlays_); | |
725 } | 767 } |
726 | 768 |
727 void PrepareFrameAndViewForPrint::FinishPrinting() { | 769 void PrepareFrameAndViewForPrint::FinishPrinting() { |
728 if (!finished_) { | 770 if (!finished_) { |
729 finished_ = true; | 771 finished_ = true; |
730 frame_->printEnd(); | 772 frame_->printEnd(); |
731 web_view_->resize(prev_view_size_); | 773 web_view_->resize(prev_view_size_); |
732 if (WebFrame* web_frame = web_view_->mainFrame()) | 774 if (WebFrame* web_frame = web_view_->mainFrame()) |
733 web_frame->setScrollOffset(prev_scroll_offset_); | 775 web_frame->setScrollOffset(prev_scroll_offset_); |
734 } | 776 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
896 } | 938 } |
897 | 939 |
898 bool PrintWebViewHelper::IsPrintToPdfRequested( | 940 bool PrintWebViewHelper::IsPrintToPdfRequested( |
899 const DictionaryValue& job_settings) { | 941 const DictionaryValue& job_settings) { |
900 bool print_to_pdf = false; | 942 bool print_to_pdf = false; |
901 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) | 943 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) |
902 NOTREACHED(); | 944 NOTREACHED(); |
903 return print_to_pdf; | 945 return print_to_pdf; |
904 } | 946 } |
905 | 947 |
948 WebKit::WebPrintScalingOption PrintWebViewHelper::GetPrintScalingOption( | |
949 bool source_is_html, const DictionaryValue& job_settings, | |
950 const PrintMsg_Print_Params& params) { | |
951 DCHECK(!print_for_preview_); | |
952 | |
953 // Do not fit to paper size when the user is saving the print contents as pdf. | |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: redundant comment.
kmadhusu
2012/05/16 18:22:41
Done.
| |
954 if (params.print_to_pdf) | |
955 return WebKit::WebPrintScalingOptionSourceSize; | |
956 | |
957 if (!source_is_html) { | |
958 if (!FitToPageEnabled(job_settings)) | |
959 return WebKit::WebPrintScalingOptionNone; | |
960 | |
961 // Get the print scaling option for the initiator renderer pdf. | |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: redundant comment
kmadhusu
2012/05/16 18:22:41
Done.
| |
962 bool print_scaling_disabled_for_plugin = | |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: you might be able to shorten this variable na
kmadhusu
2012/05/16 18:22:41
Done.
vandebo (ex-Chrome)
2012/05/16 18:30:47
I guess it didn't help.
| |
963 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( | |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: indent
kmadhusu
2012/05/16 18:22:41
Done.
| |
964 print_preview_context_.node()); | |
965 | |
966 // If this is the first preview request, UI doesn't know about the print | |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: It looks like this comment just describes WHA
kmadhusu
2012/05/16 18:22:41
Removed.
| |
967 // scaling option of the plugin. Therefore, check the print scaling option | |
968 // and update the print params accordingly. | |
969 // | |
970 // If this is not the first preview request, update print params based on | |
971 // preview job settings. | |
972 if (params.is_first_request && print_scaling_disabled_for_plugin) | |
973 return WebKit::WebPrintScalingOptionNone; | |
974 } | |
975 return WebKit::WebPrintScalingOptionFitToPrintableArea; | |
976 } | |
977 | |
906 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 978 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
907 DCHECK(is_preview_enabled_); | 979 DCHECK(is_preview_enabled_); |
908 print_preview_context_.OnPrintPreview(); | 980 print_preview_context_.OnPrintPreview(); |
909 | 981 |
910 if (!UpdatePrintSettings(print_preview_context_.frame(), | 982 if (!UpdatePrintSettings(print_preview_context_.frame(), |
911 print_preview_context_.node(), settings)) { | 983 print_preview_context_.node(), settings)) { |
912 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 984 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
913 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 985 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
914 routing_id(), print_pages_params_->params.document_cookie)); | 986 routing_id(), print_pages_params_->params.document_cookie)); |
915 notify_browser_of_print_failure_ = false; // Already sent. | 987 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. | 1349 // static - Not anonymous so that platform implementations can use it. |
1278 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( | 1350 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( |
1279 WebFrame* frame, | 1351 WebFrame* frame, |
1280 int page_index, | 1352 int page_index, |
1281 const PrintMsg_Print_Params& page_params, | 1353 const PrintMsg_Print_Params& page_params, |
1282 bool ignore_css_margins, | 1354 bool ignore_css_margins, |
1283 double* scale_factor, | 1355 double* scale_factor, |
1284 PageSizeMargins* page_layout_in_points) { | 1356 PageSizeMargins* page_layout_in_points) { |
1285 PrintMsg_Print_Params params = CalculatePrintParamsForCss( | 1357 PrintMsg_Print_Params params = CalculatePrintParamsForCss( |
1286 frame, page_index, page_params, ignore_css_margins, | 1358 frame, page_index, page_params, ignore_css_margins, |
1287 page_params.fit_to_paper_size, scale_factor); | 1359 page_params.print_scaling_option == |
1360 WebKit::WebPrintScalingOptionFitToPrintableArea, | |
1361 scale_factor); | |
1288 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); | 1362 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); |
1289 } | 1363 } |
1290 | 1364 |
1291 // static - Not anonymous so that platform implementations can use it. | 1365 // static - Not anonymous so that platform implementations can use it. |
1292 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( | 1366 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( |
1293 WebFrame* frame, | 1367 WebFrame* frame, |
1294 const WebNode& node, | 1368 const WebNode& node, |
1295 PrepareFrameAndViewForPrint* prepare, | 1369 PrepareFrameAndViewForPrint* prepare, |
1296 const PrintMsg_Print_Params& params, | 1370 const PrintMsg_Print_Params& params, |
1297 bool ignore_css_margins) { | 1371 bool ignore_css_margins) { |
1298 if (PrintingNodeOrPdfFrame(frame, node)) | 1372 if (PrintingNodeOrPdfFrame(frame, node)) |
1299 return; | 1373 return; |
1300 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( | 1374 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( |
1301 frame, 0, params, ignore_css_margins, | 1375 frame, 0, params, ignore_css_margins, |
1302 ignore_css_margins && params.fit_to_paper_size, NULL); | 1376 ignore_css_margins && |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: because of formatting, you might want to pull
kmadhusu
2012/05/16 18:22:41
Done.
| |
1377 params.print_scaling_option == | |
1378 WebKit::WebPrintScalingOptionFitToPrintableArea, | |
1379 NULL); | |
1303 prepare->UpdatePrintParams(print_params); | 1380 prepare->UpdatePrintParams(print_params); |
1304 } | 1381 } |
1305 | 1382 |
1306 bool PrintWebViewHelper::InitPrintSettings() { | 1383 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) { |
1307 PrintMsg_PrintPages_Params settings; | 1384 PrintMsg_PrintPages_Params settings; |
1308 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1385 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
1309 &settings.params)); | 1386 &settings.params)); |
1310 // Check if the printer returned any settings, if the settings is empty, we | 1387 // 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 | 1388 // can safely assume there are no printer drivers configured. So we safely |
1312 // terminate. | 1389 // terminate. |
1313 bool result = true; | 1390 bool result = true; |
1314 if (!PrintMsg_Print_Params_IsValid(settings.params)) | 1391 if (!PrintMsg_Print_Params_IsValid(settings.params)) |
1315 result = false; | 1392 result = false; |
1316 | 1393 |
1317 if (result && | 1394 if (result && |
1318 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { | 1395 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { |
1319 // Invalid print page settings. | 1396 // Invalid print page settings. |
1320 NOTREACHED(); | 1397 NOTREACHED(); |
1321 result = false; | 1398 result = false; |
1322 } | 1399 } |
1323 | 1400 |
1324 // Reset to default values. | 1401 // Reset to default values. |
1325 ignore_css_margins_ = false; | 1402 ignore_css_margins_ = false; |
1326 settings.params.fit_to_paper_size = true; | |
1327 settings.pages.clear(); | 1403 settings.pages.clear(); |
1404 settings.params.print_scaling_option = fit_to_paper_size ? | |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: just use an if. assign SourceSize, if (fit_to
kmadhusu
2012/05/16 18:22:41
Done.
| |
1405 WebKit::WebPrintScalingOptionFitToPrintableArea : | |
1406 WebKit::WebPrintScalingOptionSourceSize; | |
1328 | 1407 |
1329 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1408 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
1330 return result; | 1409 return result; |
1331 } | 1410 } |
1332 | 1411 |
1333 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1412 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
1334 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1413 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
1335 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | 1414 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
1336 DCHECK(frame); | 1415 DCHECK(frame); |
1337 if (!InitPrintSettings()) { | 1416 |
1417 // If the source is html, fit to paper size else print the source pdf size. | |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: redundant comment.
kmadhusu
2012/05/16 18:22:41
Done.
| |
1418 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); | |
1419 if (!InitPrintSettings(fit_to_paper_size)) { | |
1338 notify_browser_of_print_failure_ = false; | 1420 notify_browser_of_print_failure_ = false; |
1339 render_view()->RunModalAlertDialog( | 1421 render_view()->RunModalAlertDialog( |
1340 frame, | 1422 frame, |
1341 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | 1423 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
1342 return false; | 1424 return false; |
1343 } | 1425 } |
1344 | 1426 |
1345 DCHECK(!prepare->get()); | 1427 DCHECK(!prepare->get()); |
1346 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1428 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
1347 frame, node)); | 1429 frame, node)); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1443 &(settings.params.preview_request_id)) || | 1525 &(settings.params.preview_request_id)) || |
1444 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1526 !job_settings->GetBoolean(printing::kIsFirstRequest, |
1445 &(settings.params.is_first_request))) { | 1527 &(settings.params.is_first_request))) { |
1446 NOTREACHED(); | 1528 NOTREACHED(); |
1447 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1529 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1448 return false; | 1530 return false; |
1449 } | 1531 } |
1450 | 1532 |
1451 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); | 1533 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
1452 UpdateFrameMarginsCssInfo(*job_settings); | 1534 UpdateFrameMarginsCssInfo(*job_settings); |
1453 | 1535 settings.params.print_scaling_option = GetPrintScalingOption( |
1454 // Fit to paper size. | 1536 source_is_html, *job_settings, settings.params); |
1455 settings.params.fit_to_paper_size = source_is_html && | |
1456 !IsPrintToPdfRequested(*job_settings); | |
1457 | 1537 |
1458 // Header/Footer: Set |header_footer_info_|. | 1538 // Header/Footer: Set |header_footer_info_|. |
1459 if (settings.params.display_header_footer) { | 1539 if (settings.params.display_header_footer) { |
1460 header_footer_info_.reset(new DictionaryValue()); | 1540 header_footer_info_.reset(new DictionaryValue()); |
1461 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1541 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
1462 settings.params.date); | 1542 settings.params.date); |
1463 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1543 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
1464 settings.params.url); | 1544 settings.params.url); |
1465 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1545 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
1466 settings.params.title); | 1546 settings.params.title); |
(...skipping 20 matching lines...) Expand all Loading... | |
1487 params.cookie = print_pages_params_->params.document_cookie; | 1567 params.cookie = print_pages_params_->params.document_cookie; |
1488 params.has_selection = frame->hasSelection(); | 1568 params.has_selection = frame->hasSelection(); |
1489 params.expected_pages_count = expected_pages_count; | 1569 params.expected_pages_count = expected_pages_count; |
1490 printing::MarginType margin_type = printing::DEFAULT_MARGINS; | 1570 printing::MarginType margin_type = printing::DEFAULT_MARGINS; |
1491 if (PrintingNodeOrPdfFrame(frame, node)) | 1571 if (PrintingNodeOrPdfFrame(frame, node)) |
1492 margin_type = GetMarginsForPdf(frame, node); | 1572 margin_type = GetMarginsForPdf(frame, node); |
1493 params.margin_type = margin_type; | 1573 params.margin_type = margin_type; |
1494 | 1574 |
1495 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); | 1575 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); |
1496 | 1576 |
1577 // Store the calculated print scaling option value. | |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: Move the below comment up here as:
"PrintHost
kmadhusu
2012/05/16 18:22:41
Done.
| |
1578 WebKit::WebPrintScalingOption scaling_option = | |
1579 print_pages_params_->params.print_scaling_option; | |
1497 print_pages_params_.reset(); | 1580 print_pages_params_.reset(); |
1498 IPC::SyncMessage* msg = | 1581 IPC::SyncMessage* msg = |
1499 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); | 1582 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); |
1500 msg->EnableMessagePumping(); | 1583 msg->EnableMessagePumping(); |
1501 Send(msg); | 1584 Send(msg); |
1585 | |
1586 // When we get new |print_settings| from native print dialog we will have | |
1587 // |print_scaling_option| value set to | |
1588 // WebKit::WebPrintScalingOptionSourceSize. So we should restore the | |
1589 // calculated value in new |print_settings|. | |
1590 print_settings.params.print_scaling_option = scaling_option; | |
1591 | |
1502 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); | 1592 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); |
1503 return (print_settings.params.dpi && print_settings.params.document_cookie); | 1593 return (print_settings.params.dpi && print_settings.params.document_cookie); |
1504 } | 1594 } |
1505 | 1595 |
1506 bool PrintWebViewHelper::RenderPagesForPrint( | 1596 bool PrintWebViewHelper::RenderPagesForPrint( |
1507 WebKit::WebFrame* frame, | 1597 WebKit::WebFrame* frame, |
1508 const WebKit::WebNode& node) { | 1598 const WebKit::WebNode& node) { |
1509 if (print_pages_params_->params.selection_only) | 1599 if (print_pages_params_->params.selection_only) |
1510 return CopyAndPrint(frame); | 1600 return CopyAndPrint(frame); |
1511 return PrintPages(frame, node); | 1601 return PrintPages(frame, node); |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1878 const PrintMsg_Print_Params& | 1968 const PrintMsg_Print_Params& |
1879 PrintWebViewHelper::PrintPreviewContext::print_params() const { | 1969 PrintWebViewHelper::PrintPreviewContext::print_params() const { |
1880 DCHECK(state_ != UNINITIALIZED); | 1970 DCHECK(state_ != UNINITIALIZED); |
1881 return *print_params_; | 1971 return *print_params_; |
1882 } | 1972 } |
1883 | 1973 |
1884 int PrintWebViewHelper::PrintPreviewContext::last_error() const { | 1974 int PrintWebViewHelper::PrintPreviewContext::last_error() const { |
1885 return error_; | 1975 return error_; |
1886 } | 1976 } |
1887 | 1977 |
1888 const gfx::Size& | 1978 gfx::Size PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
1889 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | |
1890 DCHECK(IsRendering()); | 1979 DCHECK(IsRendering()); |
1891 return prep_frame_view_->GetPrintCanvasSize(); | 1980 return prep_frame_view_->GetPrintCanvasSize(); |
1892 } | 1981 } |
1893 | 1982 |
1894 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1983 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1895 prep_frame_view_.reset(); | 1984 prep_frame_view_.reset(); |
1896 metafile_.reset(); | 1985 metafile_.reset(); |
1897 pages_to_render_.clear(); | 1986 pages_to_render_.clear(); |
1898 error_ = PREVIEW_ERROR_NONE; | 1987 error_ = PREVIEW_ERROR_NONE; |
1899 } | 1988 } |
OLD | NEW |