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

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: '' 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') | no next file » | 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"
(...skipping 15 matching lines...) Expand all
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"
38 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
37 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" 39 #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" 40 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRespon se.h"
39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 41 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
40 #include "ui/base/l10n/l10n_util.h" 42 #include "ui/base/l10n/l10n_util.h"
41 #include "ui/gfx/rect.h" 43 #include "ui/gfx/rect.h"
42 #include "webkit/glue/webpreferences.h" 44 #include "webkit/glue/webpreferences.h"
43 45
44 #if defined(OS_POSIX) 46 #if defined(OS_POSIX)
45 #include "base/process_util.h" 47 #include "base/process_util.h"
46 #endif 48 #endif
(...skipping 83 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 ComputePrintParamsInDesiredDpi(const PrintMsg_Print_Params& print_params,
310 gfx::Size* result) { 312 WebKit::WebRect* content_size,
313 WebKit::WebRect* printable_area,
314 WebKit::WebSize* paper_size) {
311 int dpi = GetDPI(&print_params); 315 int dpi = GetDPI(&print_params);
312 result->set_width(ConvertUnit(print_params.content_size.width(), dpi, 316 content_size->width = ConvertUnit(print_params.content_size.width(), dpi,
313 print_params.desired_dpi)); 317 print_params.desired_dpi);
314 318
315 result->set_height(ConvertUnit(print_params.content_size.height(), dpi, 319 content_size->height = ConvertUnit(print_params.content_size.height(), dpi,
316 print_params.desired_dpi)); 320 print_params.desired_dpi);
321
322 printable_area->x = ConvertUnit(print_params.printable_area.x(), dpi,
323 print_params.desired_dpi);
324 printable_area->y = ConvertUnit(print_params.printable_area.y(), dpi,
325 print_params.desired_dpi);
326 printable_area->width = ConvertUnit(print_params.printable_area.width(), dpi,
327 print_params.desired_dpi);
328 printable_area->height = ConvertUnit(print_params.printable_area.height(),
329 dpi, print_params.desired_dpi);
330 paper_size->width = ConvertUnit(print_params.page_size.width(), dpi,
331 print_params.desired_dpi);
332 paper_size->height = ConvertUnit(print_params.page_size.height(), dpi,
333 print_params.desired_dpi);
317 } 334 }
318 335
319 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { 336 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) {
320 if (!node.isNull()) 337 if (!node.isNull())
321 return true; 338 return true;
322 if (!frame->document().isPluginDocument()) 339 if (!frame->document().isPluginDocument())
323 return false; 340 return false;
324 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin(); 341 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin();
325 return plugin && plugin->supportsPaginatedPrint(); 342 return plugin && plugin->supportsPaginatedPrint();
326 } 343 }
(...skipping 11 matching lines...) Expand all
338 return frame_has_custom_page_size_style; 355 return frame_has_custom_page_size_style;
339 } 356 }
340 357
341 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { 358 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) {
342 if (frame->isPrintScalingDisabledForPlugin(node)) 359 if (frame->isPrintScalingDisabledForPlugin(node))
343 return printing::NO_MARGINS; 360 return printing::NO_MARGINS;
344 else 361 else
345 return printing::PRINTABLE_AREA_MARGINS; 362 return printing::PRINTABLE_AREA_MARGINS;
346 } 363 }
347 364
365 bool FitToPageEnabled(const DictionaryValue& job_settings) {
366 bool fit_to_paper_size = false;
367 if (!job_settings.GetBoolean(printing::kSettingFitToPageEnabled,
368 &fit_to_paper_size)) {
369 NOTREACHED();
370 }
371 return fit_to_paper_size;
372 }
373
348 // Get the (x, y) coordinate from where printing of the current text should 374 // Get the (x, y) coordinate from where printing of the current text should
349 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and 375 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
350 // vertical alignment (TOP, BOTTOM). 376 // vertical alignment (TOP, BOTTOM).
351 SkPoint GetHeaderFooterPosition( 377 SkPoint GetHeaderFooterPosition(
352 float webkit_scale_factor, 378 float webkit_scale_factor,
353 const PageSizeMargins& page_layout, 379 const PageSizeMargins& page_layout,
354 printing::HorizontalHeaderFooterPosition horizontal_position, 380 printing::HorizontalHeaderFooterPosition horizontal_position,
355 printing::VerticalHeaderFooterPosition vertical_position, 381 printing::VerticalHeaderFooterPosition vertical_position,
356 double offset_to_baseline, 382 double offset_to_baseline,
357 double text_width_in_points) { 383 double text_width_in_points) {
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 #endif 693 #endif
668 } 694 }
669 695
670 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( 696 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
671 const PrintMsg_Print_Params& print_params, 697 const PrintMsg_Print_Params& print_params,
672 WebFrame* frame, 698 WebFrame* frame,
673 const WebNode& node) 699 const WebNode& node)
674 : frame_(frame), 700 : frame_(frame),
675 node_to_print_(node), 701 node_to_print_(node),
676 web_view_(frame->view()), 702 web_view_(frame->view()),
677 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),
680 finished_(false) { 705 finished_(false) {
681 gfx::Size canvas_size; 706 WebKit::WebRect canvas_area;
682 CalculatePrintCanvasSize(print_params, &canvas_size); 707 WebKit::WebRect printable_area;
708 WebKit::WebSize paper_size;
709 ComputePrintParamsInDesiredDpi(print_params, &canvas_area, &printable_area,
710 &paper_size);
683 711
684 if (WebFrame* web_frame = web_view_->mainFrame()) 712 if (WebFrame* web_frame = web_view_->mainFrame())
685 prev_scroll_offset_ = web_frame->scrollOffset(); 713 prev_scroll_offset_ = web_frame->scrollOffset();
686 prev_view_size_ = web_view_->size(); 714 prev_view_size_ = web_view_->size();
687 715
688 StartPrinting(canvas_size); 716 StartPrinting(canvas_area, printable_area, paper_size,
717 static_cast<int>(print_params.dpi),
718 print_params.print_scaling_option);
689 } 719 }
690 720
691 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { 721 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
692 FinishPrinting(); 722 FinishPrinting();
693 } 723 }
694 724
695 void PrepareFrameAndViewForPrint::UpdatePrintParams( 725 void PrepareFrameAndViewForPrint::UpdatePrintParams(
696 const PrintMsg_Print_Params& print_params) { 726 const PrintMsg_Print_Params& print_params) {
697 DCHECK(!finished_); 727 DCHECK(!finished_);
698 gfx::Size canvas_size; 728 WebKit::WebRect canvas_area;
699 CalculatePrintCanvasSize(print_params, &canvas_size); 729 WebKit::WebRect printable_area;
700 if (canvas_size == print_canvas_size_) 730 WebKit::WebSize paper_size;
731 ComputePrintParamsInDesiredDpi(print_params, &canvas_area, &printable_area,
732 &paper_size);
733 if (canvas_area == web_print_params_.printContentArea &&
734 printable_area == web_print_params_.printableArea &&
735 paper_size == web_print_params_.paperSize &&
736 print_params.print_scaling_option ==
737 web_print_params_.printScalingOption) {
701 return; 738 return;
739 }
702 740
703 frame_->printEnd(); 741 frame_->printEnd();
704 dpi_ = static_cast<int>(print_params.dpi); 742 StartPrinting(canvas_area, printable_area, paper_size,
705 StartPrinting(canvas_size); 743 static_cast<int>(print_params.dpi),
744 print_params.print_scaling_option);
706 } 745 }
707 746
708 void PrepareFrameAndViewForPrint::StartPrinting( 747 void PrepareFrameAndViewForPrint::StartPrinting(
vandebo (ex-Chrome) 2012/05/14 17:45:46 It looks like this should take a web print params.
kmadhusu 2012/05/14 18:50:43 Done.
709 const gfx::Size& print_canvas_size) { 748 const WebKit::WebRect& print_canvas_area,
710 print_canvas_size_ = print_canvas_size; 749 const WebKit::WebRect& printable_area,
750 const WebKit::WebSize& paper_size,
751 int dpi,
752 const WebKit::WebPrintScalingOption& scaling_option) {
753 web_print_params_.printContentArea = print_canvas_area;
754 web_print_params_.printableArea = printable_area;
755 web_print_params_.paperSize = paper_size;
756 web_print_params_.printerDPI = dpi;
757 web_print_params_.printScalingOption = scaling_option;
711 758
712 // Layout page according to printer page size. Since WebKit shrinks the 759 // 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 760 // 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 761 // think the page is 125% larger so the size of the page is correct for
715 // minimum (default) scaling. 762 // minimum (default) scaling.
716 // This is important for sites that try to fill the page. 763 // This is important for sites that try to fill the page.
717 gfx::Size print_layout_size(print_canvas_size_); 764 gfx::Size print_layout_size(print_canvas_area.width,
765 print_canvas_area.height);
718 print_layout_size.set_height(static_cast<int>( 766 print_layout_size.set_height(static_cast<int>(
719 static_cast<double>(print_layout_size.height()) * 1.25)); 767 static_cast<double>(print_layout_size.height()) * 1.25));
720 768
721 web_view_->resize(print_layout_size); 769 web_view_->resize(print_layout_size);
722 770
723 expected_pages_count_ = frame_->printBegin(print_canvas_size_, node_to_print_, 771 expected_pages_count_ = frame_->printBegin(web_print_params_,
724 dpi_, &use_browser_overlays_); 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
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
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
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
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 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
1878 const PrintMsg_Print_Params& 1975 const PrintMsg_Print_Params&
1879 PrintWebViewHelper::PrintPreviewContext::print_params() const { 1976 PrintWebViewHelper::PrintPreviewContext::print_params() const {
1880 DCHECK(state_ != UNINITIALIZED); 1977 DCHECK(state_ != UNINITIALIZED);
1881 return *print_params_; 1978 return *print_params_;
1882 } 1979 }
1883 1980
1884 int PrintWebViewHelper::PrintPreviewContext::last_error() const { 1981 int PrintWebViewHelper::PrintPreviewContext::last_error() const {
1885 return error_; 1982 return error_;
1886 } 1983 }
1887 1984
1888 const gfx::Size& 1985 gfx::Size PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1889 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1890 DCHECK(IsRendering()); 1986 DCHECK(IsRendering());
1891 return prep_frame_view_->GetPrintCanvasSize(); 1987 return prep_frame_view_->GetPrintCanvasSize();
1892 } 1988 }
1893 1989
1894 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1990 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1895 prep_frame_view_.reset(); 1991 prep_frame_view_.reset();
1896 metafile_.reset(); 1992 metafile_.reset();
1897 pages_to_render_.clear(); 1993 pages_to_render_.clear();
1898 error_ = PREVIEW_ERROR_NONE; 1994 error_ = PREVIEW_ERROR_NONE;
1899 } 1995 }
OLDNEW
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698