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 b31cd851cfe5213c2219515b3dde4681cd01e5f7..75b3af19e1e6a1fa9a48a3b8bcc40be1400a7e49 100644 |
| --- a/chrome/renderer/print_web_view_helper.cc |
| +++ b/chrome/renderer/print_web_view_helper.cc |
| @@ -302,14 +302,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) { |
| @@ -600,22 +615,27 @@ void PrintWebViewHelper::PrintHeaderAndFooter( |
| PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
| const PrintMsg_Print_Params& print_params, |
| WebFrame* frame, |
| - const WebNode& node) |
| + const WebNode& node, |
| + WebKit::WebPrintScalingOption scaling_option) |
| : 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), |
| + print_scaling_option_(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() { |
| @@ -626,18 +646,28 @@ 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_) { |
| return; |
| + } |
| frame_->printEnd(); |
| dpi_ = static_cast<int>(print_params.dpi); |
| - StartPrinting(canvas_size); |
| + 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 |
| @@ -650,8 +680,13 @@ void PrepareFrameAndViewForPrint::StartPrinting( |
| 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(print_canvas_size_, |
| + printable_area_, |
| + paper_size_, |
| + node_to_print_, |
| + dpi_, |
| + print_scaling_option_, |
| + &use_browser_overlays_); |
| } |
| void PrepareFrameAndViewForPrint::FinishPrinting() { |
| @@ -674,7 +709,8 @@ PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) |
| user_cancelled_scripted_print_count_(0), |
| is_scripted_printing_blocked_(false), |
| notify_browser_of_print_failure_(true), |
| - print_for_preview_(false) { |
| + print_for_preview_(false), |
| + print_scaling_option_(WebKit::WebPrintFitToPrintableArea) { |
| } |
| PrintWebViewHelper::~PrintWebViewHelper() {} |
| @@ -894,7 +930,8 @@ bool PrintWebViewHelper::CreatePreviewDocument() { |
| PrintMsg_Print_Params print_params = print_pages_params_->params; |
| const std::vector<int>& pages = print_pages_params_->pages; |
| if (!print_preview_context_.CreatePreviewDocument(&print_params, pages, |
| - ignore_css_margins_)) { |
| + ignore_css_margins_, |
| + print_scaling_option_)) { |
| return false; |
| } |
| @@ -1169,7 +1206,8 @@ bool PrintWebViewHelper::CopyAndPrint(WebKit::WebFrame* web_frame) { |
| bool PrintWebViewHelper::PrintPages(WebFrame* frame, const WebNode& node) { |
| const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
| const PrintMsg_Print_Params& print_params = params.params; |
| - PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); |
| + PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node, |
| + print_scaling_option_); |
| UpdateFrameAndViewFromCssPageLayout(frame, node, &prep_frame_view, |
| print_params, ignore_css_margins_); |
| @@ -1233,7 +1271,7 @@ void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( |
| 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)); |
| @@ -1253,8 +1291,12 @@ bool PrintWebViewHelper::InitPrintSettings() { |
| // Reset to default values. |
| ignore_css_margins_ = false; |
| - settings.params.fit_to_paper_size = true; |
| settings.pages.clear(); |
| + settings.params.fit_to_paper_size = fit_to_paper_size; |
| + |
| + // Update the print scaling option. |
| + print_scaling_option_ = static_cast<WebKit::WebPrintScalingOption>( |
|
vandebo (ex-Chrome)
2012/04/20 23:26:11
This cast is pretty ugly. A conditional or trinar
kmadhusu
2012/04/23 17:39:10
Done... Removed print_scaling_option_ from PrintWe
|
| + fit_to_paper_size); |
| print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| return result; |
| @@ -1264,7 +1306,9 @@ bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
| WebKit::WebFrame* frame, const WebKit::WebNode& node, |
| scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
| DCHECK(frame); |
| - if (!InitPrintSettings()) { |
| + bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node) && |
| + frame->isPrintScalingDisabledForPlugin(node)); |
| + if (!InitPrintSettings(fit_to_paper_size)) { |
| notify_browser_of_print_failure_ = false; |
| render_view()->RunModalAlertDialog( |
| frame, |
| @@ -1274,7 +1318,8 @@ bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
| DCHECK(!prepare->get()); |
| prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
| - frame, node)); |
| + frame, node, |
| + print_scaling_option_)); |
| UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), |
| print_pages_params_->params, |
| ignore_css_margins_); |
| @@ -1382,8 +1427,17 @@ bool PrintWebViewHelper::UpdatePrintSettings( |
| UpdateFrameMarginsCssInfo(*job_settings); |
| // Fit to paper size. |
| - settings.params.fit_to_paper_size = source_is_html && |
| - !IsPrintToPdfRequested(*job_settings); |
| + settings.params.fit_to_paper_size = |
|
vandebo (ex-Chrome)
2012/04/20 23:26:11
At this point, it might be clearer to use an if or
kmadhusu
2012/04/23 17:39:10
Done.
|
| + !(print_for_preview_ || settings.params.print_to_pdf || |
| + (PrintingNodeOrPdfFrame(frame, node) && |
| + frame->isPrintScalingDisabledForPlugin(node))); |
| + |
| + // Update the print scaling option. |
| + print_scaling_option_ = (print_for_preview_ || |
| + settings.params.print_to_pdf) ? |
| + WebKit::WebPrintActualSize : |
| + static_cast<WebKit::WebPrintScalingOption>( |
| + settings.params.fit_to_paper_size); |
| // Header/Footer: Set |header_footer_info_|. |
| if (settings.params.display_header_footer) { |
| @@ -1637,7 +1691,8 @@ void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
| bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
| PrintMsg_Print_Params* print_params, |
| const std::vector<int>& pages, |
| - bool ignore_css_margins) { |
| + bool ignore_css_margins, |
| + WebKit::WebPrintScalingOption print_scaling_option) { |
| DCHECK_EQ(INITIALIZED, state_); |
| state_ = RENDERING; |
| @@ -1650,7 +1705,8 @@ bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
| // Need to make sure old object gets destroyed first. |
| prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
| - node())); |
| + node(), |
| + print_scaling_option)); |
| UpdateFrameAndViewFromCssPageLayout(frame_, node_, prep_frame_view_.get(), |
| *print_params, ignore_css_margins); |