Chromium Code Reviews| 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 1a06ad66eb0866ecd413aa2de5f1fa2a7240cf96..962dfd68a46a166f1293dc71b399c7a5eadc38e0 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/WebPrintParams.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,29 @@ 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 ComputePrintParamsInDesiredDpi(const PrintMsg_Print_Params& print_params, |
| + gfx::Size* content_size, |
| + gfx::Rect* printable_area, |
| + gfx::Size* paper_size) { |
| 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)); |
| + content_size->set_width(ConvertUnit(print_params.content_size.width(), dpi, |
| + print_params.desired_dpi)); |
| + |
| + content_size->set_height(ConvertUnit(print_params.content_size.height(), dpi, |
| + print_params.desired_dpi)); |
| + |
| + printable_area->SetRect(ConvertUnit(print_params.printable_area.x(), dpi, |
| + print_params.desired_dpi), |
| + ConvertUnit(print_params.printable_area.y(), dpi, |
| + print_params.desired_dpi), |
| + ConvertUnit(print_params.printable_area.width(), dpi, |
| + print_params.desired_dpi), |
| + ConvertUnit(print_params.printable_area.height(), dpi, |
| + print_params.desired_dpi)); |
| + paper_size->SetSize(ConvertUnit(print_params.page_size.width(), dpi, |
| + print_params.desired_dpi), |
| + ConvertUnit(print_params.page_size.height(), dpi, |
| + print_params.desired_dpi)); |
| } |
| bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { |
| @@ -345,6 +361,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). |
| @@ -677,15 +702,19 @@ PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
| dpi_(static_cast<int>(print_params.dpi)), |
| expected_pages_count_(0), |
| use_browser_overlays_(true), |
| + print_scaling_option_(print_params.print_scaling_option), |
| finished_(false) { |
| gfx::Size canvas_size; |
| - CalculatePrintCanvasSize(print_params, &canvas_size); |
| + gfx::Rect printable_area; |
| + gfx::Size paper_size; |
| + ComputePrintParamsInDesiredDpi(print_params, &canvas_size, &printable_area, |
| + &paper_size); |
| if (WebFrame* web_frame = web_view_->mainFrame()) |
| prev_scroll_offset_ = web_frame->scrollOffset(); |
| prev_view_size_ = web_view_->size(); |
| - StartPrinting(canvas_size); |
| + StartPrinting(canvas_size, printable_area, paper_size); |
| } |
| PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
| @@ -696,18 +725,30 @@ 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_) |
| + gfx::Rect printable_area; |
| + gfx::Size paper_size; |
| + ComputePrintParamsInDesiredDpi(print_params, &canvas_size, &printable_area, |
| + &paper_size); |
| + if (canvas_size == print_canvas_size_ && |
| + printable_area == printable_area_ && |
| + paper_size == paper_size_ && |
| + print_params.print_scaling_option == print_scaling_option_) { |
| return; |
| + } |
| frame_->printEnd(); |
| dpi_ = static_cast<int>(print_params.dpi); |
| - StartPrinting(canvas_size); |
| + print_scaling_option_ = print_params.print_scaling_option; |
| + StartPrinting(canvas_size, printable_area, paper_size); |
| } |
| void PrepareFrameAndViewForPrint::StartPrinting( |
| - const gfx::Size& print_canvas_size) { |
| + const gfx::Size& print_canvas_size, |
| + const gfx::Rect& printable_area, |
| + const gfx::Size& paper_size) { |
| print_canvas_size_ = print_canvas_size; |
| + printable_area_ = printable_area; |
| + paper_size_ = paper_size; |
| // Layout page according to printer page size. Since WebKit shrinks the |
| // size of the page automatically (from 125% to 200%) we trick it to |
| @@ -720,8 +761,16 @@ void PrepareFrameAndViewForPrint::StartPrinting( |
| web_view_->resize(print_layout_size); |
| - expected_pages_count_ = frame_->printBegin(print_canvas_size_, node_to_print_, |
| - dpi_, &use_browser_overlays_); |
| + WebKit::WebPrintParams webkit_print_params; |
| + webkit_print_params.printContentArea = gfx::Rect(print_canvas_size); |
| + webkit_print_params.printableArea = printable_area_; |
| + webkit_print_params.paperSize = paper_size_; |
| + webkit_print_params.printerDPI = dpi_; |
| + webkit_print_params.printScalingOption = print_scaling_option_; |
| + |
| + expected_pages_count_ = frame_->printBegin(webkit_print_params, |
|
kmadhusu
2012/05/11 21:36:03
I constructed the |webkit_print_params| right befo
|
| + node_to_print_, |
| + &use_browser_overlays_); |
| } |
| void PrepareFrameAndViewForPrint::FinishPrinting() { |
| @@ -903,6 +952,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. |
| + 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. |
| + bool print_scaling_disabled_for_plugin = |
| + print_preview_context_.frame()->isPrintScalingDisabledForPlugin( |
| + print_preview_context_.node()); |
| + |
| + // If this is the first preview request, UI doesn't know about the print |
| + // 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 +1363,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 +1380,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 && |
| + 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 +1407,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 ? |
| + WebKit::WebPrintScalingOptionFitToPrintableArea : |
| + WebKit::WebPrintScalingOptionSourceSize; |
| print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| return result; |
| @@ -1334,7 +1420,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. |
| + 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 +1539,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 +1581,21 @@ bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
| Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); |
| + // Store the calculated print scaling option value. |
| + 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); |
| } |