Index: chrome/renderer/print_web_view_helper.cc |
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc |
index 771cff2f3611b3084c694ffe709d1d713bbe8f83..bc4f9af6640ea698fa54c40491a3ad551bb0e5e8 100644 |
--- a/chrome/renderer/print_web_view_helper.cc |
+++ b/chrome/renderer/print_web_view_helper.cc |
@@ -33,6 +33,7 @@ |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginDocument.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h" |
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebPrintScalingOption.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRequest.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLResponse.h" |
@@ -137,8 +138,8 @@ bool PrintMsg_Print_Params_IsEqual( |
newParams.params.supports_alpha_blend && |
oldParams.pages.size() == newParams.pages.size() && |
oldParams.params.print_to_pdf == newParams.params.print_to_pdf && |
- oldParams.params.fit_to_paper_size == |
- newParams.params.fit_to_paper_size && |
+ oldParams.params.print_scaling_option == |
+ newParams.params.print_scaling_option && |
oldParams.params.display_header_footer == |
newParams.params.display_header_footer && |
oldParams.params.date == newParams.params.date && |
@@ -306,14 +307,39 @@ void EnsureOrientationMatches(const PrintMsg_Print_Params& css_params, |
page_params->printable_area.width())); |
} |
-void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, |
- gfx::Size* result) { |
+void ComputeWebKitPrintParamsInDesiredDpi( |
+ const PrintMsg_Print_Params& print_params, |
+ WebKit::WebPrintParams* webkit_print_params) { |
int dpi = GetDPI(&print_params); |
- result->set_width(ConvertUnit(print_params.content_size.width(), dpi, |
- print_params.desired_dpi)); |
- |
- result->set_height(ConvertUnit(print_params.content_size.height(), dpi, |
- print_params.desired_dpi)); |
+ webkit_print_params->printerDPI = dpi; |
+ webkit_print_params->printScalingOption = print_params.print_scaling_option; |
+ |
+ webkit_print_params->printContentArea.width = |
+ ConvertUnit(print_params.content_size.width(), dpi, |
+ print_params.desired_dpi); |
+ webkit_print_params->printContentArea.height = |
+ ConvertUnit(print_params.content_size.height(), dpi, |
+ print_params.desired_dpi); |
+ |
+ webkit_print_params->printableArea.x = |
+ ConvertUnit(print_params.printable_area.x(), dpi, |
+ print_params.desired_dpi); |
+ webkit_print_params->printableArea.y = |
+ ConvertUnit(print_params.printable_area.y(), dpi, |
+ print_params.desired_dpi); |
+ webkit_print_params->printableArea.width = |
+ ConvertUnit(print_params.printable_area.width(), dpi, |
+ print_params.desired_dpi); |
+ webkit_print_params->printableArea.height = |
+ ConvertUnit(print_params.printable_area.height(), |
+ dpi, print_params.desired_dpi); |
+ |
+ webkit_print_params->paperSize.width = |
+ ConvertUnit(print_params.page_size.width(), dpi, |
+ print_params.desired_dpi); |
+ webkit_print_params->paperSize.height = |
+ ConvertUnit(print_params.page_size.height(), dpi, |
+ print_params.desired_dpi); |
} |
bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { |
@@ -345,6 +371,15 @@ printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { |
return printing::PRINTABLE_AREA_MARGINS; |
} |
+bool FitToPageEnabled(const DictionaryValue& job_settings) { |
+ bool fit_to_paper_size = false; |
+ if (!job_settings.GetBoolean(printing::kSettingFitToPageEnabled, |
+ &fit_to_paper_size)) { |
+ NOTREACHED(); |
+ } |
+ return fit_to_paper_size; |
+} |
+ |
// Get the (x, y) coordinate from where printing of the current text should |
// start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and |
// vertical alignment (TOP, BOTTOM). |
@@ -674,18 +709,17 @@ PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
: frame_(frame), |
node_to_print_(node), |
web_view_(frame->view()), |
- dpi_(static_cast<int>(print_params.dpi)), |
expected_pages_count_(0), |
use_browser_overlays_(true), |
finished_(false) { |
- gfx::Size canvas_size; |
- CalculatePrintCanvasSize(print_params, &canvas_size); |
+ WebKit::WebPrintParams webkit_print_params; |
+ ComputeWebKitPrintParamsInDesiredDpi(print_params, &webkit_print_params); |
if (WebFrame* web_frame = web_view_->mainFrame()) |
prev_scroll_offset_ = web_frame->scrollOffset(); |
prev_view_size_ = web_view_->size(); |
- StartPrinting(canvas_size); |
+ StartPrinting(webkit_print_params); |
} |
PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
@@ -695,33 +729,41 @@ PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
void PrepareFrameAndViewForPrint::UpdatePrintParams( |
const PrintMsg_Print_Params& print_params) { |
DCHECK(!finished_); |
- gfx::Size canvas_size; |
- CalculatePrintCanvasSize(print_params, &canvas_size); |
- if (canvas_size == print_canvas_size_) |
+ WebKit::WebPrintParams webkit_print_params; |
+ ComputeWebKitPrintParamsInDesiredDpi(print_params, &webkit_print_params); |
+ |
+ if (webkit_print_params.printContentArea == |
+ web_print_params_.printContentArea && |
+ webkit_print_params.printableArea == web_print_params_.printableArea && |
+ webkit_print_params.paperSize == web_print_params_.paperSize && |
+ webkit_print_params.printScalingOption == |
+ web_print_params_.printScalingOption) { |
return; |
+ } |
frame_->printEnd(); |
- dpi_ = static_cast<int>(print_params.dpi); |
- StartPrinting(canvas_size); |
+ StartPrinting(webkit_print_params); |
} |
void PrepareFrameAndViewForPrint::StartPrinting( |
- const gfx::Size& print_canvas_size) { |
- print_canvas_size_ = print_canvas_size; |
+ const WebKit::WebPrintParams& webkit_print_params) { |
+ web_print_params_ = webkit_print_params; |
// Layout page according to printer page size. Since WebKit shrinks the |
// size of the page automatically (from 125% to 200%) we trick it to |
// think the page is 125% larger so the size of the page is correct for |
// minimum (default) scaling. |
// This is important for sites that try to fill the page. |
- gfx::Size print_layout_size(print_canvas_size_); |
+ gfx::Size print_layout_size(web_print_params_.printContentArea.width, |
+ web_print_params_.printContentArea.height); |
print_layout_size.set_height(static_cast<int>( |
static_cast<double>(print_layout_size.height()) * 1.25)); |
web_view_->resize(print_layout_size); |
- expected_pages_count_ = frame_->printBegin(print_canvas_size_, node_to_print_, |
- dpi_, &use_browser_overlays_); |
+ expected_pages_count_ = frame_->printBegin(web_print_params_, |
+ node_to_print_, |
+ &use_browser_overlays_); |
} |
void PrepareFrameAndViewForPrint::FinishPrinting() { |
@@ -903,6 +945,36 @@ bool PrintWebViewHelper::IsPrintToPdfRequested( |
return print_to_pdf; |
} |
+WebKit::WebPrintScalingOption PrintWebViewHelper::GetPrintScalingOption( |
+ bool source_is_html, const DictionaryValue& job_settings, |
+ const PrintMsg_Print_Params& params) { |
+ DCHECK(!print_for_preview_); |
+ |
+ // 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.
|
+ if (params.print_to_pdf) |
+ return WebKit::WebPrintScalingOptionSourceSize; |
+ |
+ if (!source_is_html) { |
+ if (!FitToPageEnabled(job_settings)) |
+ return WebKit::WebPrintScalingOptionNone; |
+ |
+ // 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.
|
+ 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.
|
+ print_preview_context_.frame()->isPrintScalingDisabledForPlugin( |
vandebo (ex-Chrome)
2012/05/16 17:41:32
nit: indent
kmadhusu
2012/05/16 18:22:41
Done.
|
+ print_preview_context_.node()); |
+ |
+ // 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.
|
+ // scaling option of the plugin. Therefore, check the print scaling option |
+ // and update the print params accordingly. |
+ // |
+ // If this is not the first preview request, update print params based on |
+ // preview job settings. |
+ if (params.is_first_request && print_scaling_disabled_for_plugin) |
+ return WebKit::WebPrintScalingOptionNone; |
+ } |
+ return WebKit::WebPrintScalingOptionFitToPrintableArea; |
+} |
+ |
void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
DCHECK(is_preview_enabled_); |
print_preview_context_.OnPrintPreview(); |
@@ -1284,7 +1356,9 @@ void PrintWebViewHelper::ComputePageLayoutInPointsForCss( |
PageSizeMargins* page_layout_in_points) { |
PrintMsg_Print_Params params = CalculatePrintParamsForCss( |
frame, page_index, page_params, ignore_css_margins, |
- page_params.fit_to_paper_size, scale_factor); |
+ page_params.print_scaling_option == |
+ WebKit::WebPrintScalingOptionFitToPrintableArea, |
+ scale_factor); |
CalculatePageLayoutFromPrintParams(params, page_layout_in_points); |
} |
@@ -1299,11 +1373,14 @@ void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( |
return; |
PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( |
frame, 0, params, ignore_css_margins, |
- ignore_css_margins && params.fit_to_paper_size, NULL); |
+ 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.
|
+ params.print_scaling_option == |
+ WebKit::WebPrintScalingOptionFitToPrintableArea, |
+ NULL); |
prepare->UpdatePrintParams(print_params); |
} |
-bool PrintWebViewHelper::InitPrintSettings() { |
+bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) { |
PrintMsg_PrintPages_Params settings; |
Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
&settings.params)); |
@@ -1323,8 +1400,10 @@ bool PrintWebViewHelper::InitPrintSettings() { |
// Reset to default values. |
ignore_css_margins_ = false; |
- settings.params.fit_to_paper_size = true; |
settings.pages.clear(); |
+ 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.
|
+ WebKit::WebPrintScalingOptionFitToPrintableArea : |
+ WebKit::WebPrintScalingOptionSourceSize; |
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
return result; |
@@ -1334,7 +1413,10 @@ bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
WebKit::WebFrame* frame, const WebKit::WebNode& node, |
scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
DCHECK(frame); |
- if (!InitPrintSettings()) { |
+ |
+ // 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.
|
+ bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); |
+ if (!InitPrintSettings(fit_to_paper_size)) { |
notify_browser_of_print_failure_ = false; |
render_view()->RunModalAlertDialog( |
frame, |
@@ -1450,10 +1532,8 @@ bool PrintWebViewHelper::UpdatePrintSettings( |
settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
UpdateFrameMarginsCssInfo(*job_settings); |
- |
- // Fit to paper size. |
- settings.params.fit_to_paper_size = source_is_html && |
- !IsPrintToPdfRequested(*job_settings); |
+ settings.params.print_scaling_option = GetPrintScalingOption( |
+ source_is_html, *job_settings, settings.params); |
// Header/Footer: Set |header_footer_info_|. |
if (settings.params.display_header_footer) { |
@@ -1494,11 +1574,21 @@ bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); |
+ // 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.
|
+ WebKit::WebPrintScalingOption scaling_option = |
+ print_pages_params_->params.print_scaling_option; |
print_pages_params_.reset(); |
IPC::SyncMessage* msg = |
new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); |
msg->EnableMessagePumping(); |
Send(msg); |
+ |
+ // When we get new |print_settings| from native print dialog we will have |
+ // |print_scaling_option| value set to |
+ // WebKit::WebPrintScalingOptionSourceSize. So we should restore the |
+ // calculated value in new |print_settings|. |
+ print_settings.params.print_scaling_option = scaling_option; |
+ |
print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); |
return (print_settings.params.dpi && print_settings.params.document_cookie); |
} |
@@ -1885,8 +1975,7 @@ int PrintWebViewHelper::PrintPreviewContext::last_error() const { |
return error_; |
} |
-const gfx::Size& |
-PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
+gfx::Size PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
DCHECK(IsRendering()); |
return prep_frame_view_->GetPrintCanvasSize(); |
} |