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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 oldParams.params.selection_only == newParams.params.selection_only && | 127 oldParams.params.selection_only == newParams.params.selection_only && |
128 oldParams.params.supports_alpha_blend == | 128 oldParams.params.supports_alpha_blend == |
129 newParams.params.supports_alpha_blend && | 129 newParams.params.supports_alpha_blend && |
130 oldParams.pages.size() == newParams.pages.size() && | 130 oldParams.pages.size() == newParams.pages.size() && |
131 oldParams.params.print_to_pdf == newParams.params.print_to_pdf && | 131 oldParams.params.print_to_pdf == newParams.params.print_to_pdf && |
132 oldParams.params.display_header_footer == | 132 oldParams.params.display_header_footer == |
133 newParams.params.display_header_footer && | 133 newParams.params.display_header_footer && |
134 oldParams.params.date == newParams.params.date && | 134 oldParams.params.date == newParams.params.date && |
135 oldParams.params.title == newParams.params.title && | 135 oldParams.params.title == newParams.params.title && |
136 oldParams.params.url == newParams.params.url && | 136 oldParams.params.url == newParams.params.url && |
137 oldParams.params.fit_pdf_page_to_paper_size == | |
138 newParams.params.fit_pdf_page_to_paper_size && | |
137 std::equal(oldParams.pages.begin(), oldParams.pages.end(), | 139 std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
138 newParams.pages.begin()); | 140 newParams.pages.begin()); |
139 } | 141 } |
140 | 142 |
141 PrintMsg_Print_Params GetCssPrintParams( | 143 PrintMsg_Print_Params GetCssPrintParams( |
142 WebFrame* frame, | 144 WebFrame* frame, |
143 int page_index, | 145 int page_index, |
144 const PrintMsg_Print_Params& page_params) { | 146 const PrintMsg_Print_Params& page_params) { |
145 PrintMsg_Print_Params page_css_params = page_params; | 147 PrintMsg_Print_Params page_css_params = page_params; |
146 int dpi = GetDPI(&page_params); | 148 int dpi = GetDPI(&page_params); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 // Swap the |width| and |height| values. | 291 // Swap the |width| and |height| values. |
290 page_params->page_size.SetSize(page_params->page_size.height(), | 292 page_params->page_size.SetSize(page_params->page_size.height(), |
291 page_params->page_size.width()); | 293 page_params->page_size.width()); |
292 page_params->content_size.SetSize(page_params->content_size.height(), | 294 page_params->content_size.SetSize(page_params->content_size.height(), |
293 page_params->content_size.width()); | 295 page_params->content_size.width()); |
294 page_params->printable_area.set_size( | 296 page_params->printable_area.set_size( |
295 gfx::Size(page_params->printable_area.height(), | 297 gfx::Size(page_params->printable_area.height(), |
296 page_params->printable_area.width())); | 298 page_params->printable_area.width())); |
297 } | 299 } |
298 | 300 |
299 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, | 301 void ComputePrintParamsInDesiredDpi(const PrintMsg_Print_Params& print_params, |
300 gfx::Size* result) { | 302 bool print_for_preview, |
303 gfx::Size* content_size, | |
304 gfx::Rect* printable_area, | |
305 gfx::Size* paper_size, | |
306 bool* fit_pdf_page_to_paper_size) { | |
301 int dpi = GetDPI(&print_params); | 307 int dpi = GetDPI(&print_params); |
302 result->set_width(ConvertUnit(print_params.content_size.width(), dpi, | 308 content_size->set_width(ConvertUnit(print_params.content_size.width(), dpi, |
303 print_params.desired_dpi)); | 309 print_params.desired_dpi)); |
304 | 310 |
305 result->set_height(ConvertUnit(print_params.content_size.height(), dpi, | 311 content_size->set_height(ConvertUnit(print_params.content_size.height(), dpi, |
306 print_params.desired_dpi)); | 312 print_params.desired_dpi)); |
313 | |
314 printable_area->SetRect(ConvertUnit(print_params.printable_area.x(), dpi, | |
315 print_params.desired_dpi), | |
316 ConvertUnit(print_params.printable_area.y(), dpi, | |
317 print_params.desired_dpi), | |
318 ConvertUnit(print_params.printable_area.width(), dpi, | |
319 print_params.desired_dpi), | |
320 ConvertUnit(print_params.printable_area.height(), dpi, | |
321 print_params.desired_dpi)); | |
322 if (print_params.print_to_pdf || print_for_preview) { | |
323 // Don't fit to page when we save to pdf or when we print for preview. | |
vandebo (ex-Chrome)
2012/04/19 18:45:31
This is a bit weird - as we talked about, using an
kmadhusu
2012/04/20 22:29:57
Done.
| |
324 // Therefore, set the paper size to zero. | |
325 paper_size->SetSize(0, 0); | |
326 } else { | |
327 paper_size->SetSize(ConvertUnit(print_params.page_size.width(), dpi, | |
328 print_params.desired_dpi), | |
329 ConvertUnit(print_params.page_size.height(), dpi, | |
330 print_params.desired_dpi)); | |
331 } | |
332 | |
333 *fit_pdf_page_to_paper_size = print_params.fit_pdf_page_to_paper_size; | |
307 } | 334 } |
308 | 335 |
309 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { | 336 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { |
310 if (!node.isNull()) | 337 if (!node.isNull()) |
311 return true; | 338 return true; |
312 std::string mime(frame->dataSource()->response().mimeType().utf8()); | 339 std::string mime(frame->dataSource()->response().mimeType().utf8()); |
313 return mime == "application/pdf"; | 340 return mime == "application/pdf"; |
314 } | 341 } |
315 | 342 |
316 bool PrintingFrameHasPageSizeStyle(WebFrame* frame, int total_page_count) { | 343 bool PrintingFrameHasPageSizeStyle(WebFrame* frame, int total_page_count) { |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
585 } | 612 } |
586 | 613 |
587 #if defined(USE_SKIA) | 614 #if defined(USE_SKIA) |
588 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); | 615 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); |
589 #endif | 616 #endif |
590 } | 617 } |
591 | 618 |
592 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 619 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
593 const PrintMsg_Print_Params& print_params, | 620 const PrintMsg_Print_Params& print_params, |
594 WebFrame* frame, | 621 WebFrame* frame, |
595 const WebNode& node) | 622 const WebNode& node, |
623 bool print_for_preview) | |
vandebo (ex-Chrome)
2012/04/19 18:45:31
The frame and view doesn't care if we're printing
kmadhusu
2012/04/20 22:29:57
Fixed. Constructor now takes a print scaling optio
| |
596 : frame_(frame), | 624 : frame_(frame), |
597 node_to_print_(node), | 625 node_to_print_(node), |
598 web_view_(frame->view()), | 626 web_view_(frame->view()), |
599 dpi_(static_cast<int>(print_params.dpi)), | 627 dpi_(static_cast<int>(print_params.dpi)), |
600 expected_pages_count_(0), | 628 expected_pages_count_(0), |
601 use_browser_overlays_(true), | 629 use_browser_overlays_(true), |
630 fit_pdf_page_to_paper_size_(false), | |
631 print_for_preview_(print_for_preview), | |
602 finished_(false) { | 632 finished_(false) { |
603 gfx::Size canvas_size; | 633 gfx::Size canvas_size; |
604 CalculatePrintCanvasSize(print_params, &canvas_size); | 634 gfx::Rect printable_area; |
635 gfx::Size paper_size; | |
636 bool fit_pdf_page_to_paper_size; | |
637 ComputePrintParamsInDesiredDpi(print_params, print_for_preview_, &canvas_size, | |
638 &printable_area, &paper_size, | |
639 &fit_pdf_page_to_paper_size); | |
605 | 640 |
606 if (WebFrame* web_frame = web_view_->mainFrame()) | 641 if (WebFrame* web_frame = web_view_->mainFrame()) |
607 prev_scroll_offset_ = web_frame->scrollOffset(); | 642 prev_scroll_offset_ = web_frame->scrollOffset(); |
608 prev_view_size_ = web_view_->size(); | 643 prev_view_size_ = web_view_->size(); |
609 | 644 |
610 StartPrinting(canvas_size); | 645 StartPrinting(canvas_size, printable_area, paper_size, |
646 fit_pdf_page_to_paper_size); | |
611 } | 647 } |
612 | 648 |
613 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { | 649 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
614 FinishPrinting(); | 650 FinishPrinting(); |
615 } | 651 } |
616 | 652 |
617 void PrepareFrameAndViewForPrint::UpdatePrintParams( | 653 void PrepareFrameAndViewForPrint::UpdatePrintParams( |
618 const PrintMsg_Print_Params& print_params) { | 654 const PrintMsg_Print_Params& print_params) { |
619 DCHECK(!finished_); | 655 DCHECK(!finished_); |
620 gfx::Size canvas_size; | 656 gfx::Size canvas_size; |
621 CalculatePrintCanvasSize(print_params, &canvas_size); | 657 gfx::Rect printable_area; |
622 if (canvas_size == print_canvas_size_) | 658 gfx::Size paper_size; |
659 bool fit_pdf_page_to_paper_size; | |
660 ComputePrintParamsInDesiredDpi(print_params, print_for_preview_, &canvas_size, | |
661 &printable_area, &paper_size, | |
662 &fit_pdf_page_to_paper_size); | |
663 if (canvas_size == print_canvas_size_ && | |
664 printable_area == printable_area_ && | |
665 paper_size == paper_size_ && | |
666 fit_pdf_page_to_paper_size == fit_pdf_page_to_paper_size_) { | |
623 return; | 667 return; |
668 } | |
624 | 669 |
625 frame_->printEnd(); | 670 frame_->printEnd(); |
626 dpi_ = static_cast<int>(print_params.dpi); | 671 dpi_ = static_cast<int>(print_params.dpi); |
627 StartPrinting(canvas_size); | 672 StartPrinting(canvas_size, printable_area, paper_size, |
673 fit_pdf_page_to_paper_size); | |
628 } | 674 } |
629 | 675 |
630 void PrepareFrameAndViewForPrint::StartPrinting( | 676 void PrepareFrameAndViewForPrint::StartPrinting( |
631 const gfx::Size& print_canvas_size) { | 677 const gfx::Size& print_canvas_size, |
678 const gfx::Rect& printable_area, | |
679 const gfx::Size& paper_size, | |
680 bool fit_pdf_page_to_paper_size) { | |
632 print_canvas_size_ = print_canvas_size; | 681 print_canvas_size_ = print_canvas_size; |
682 printable_area_ = printable_area; | |
683 paper_size_ = paper_size; | |
684 fit_pdf_page_to_paper_size_ = fit_pdf_page_to_paper_size; | |
633 | 685 |
634 // Layout page according to printer page size. Since WebKit shrinks the | 686 // Layout page according to printer page size. Since WebKit shrinks the |
635 // size of the page automatically (from 125% to 200%) we trick it to | 687 // size of the page automatically (from 125% to 200%) we trick it to |
636 // think the page is 125% larger so the size of the page is correct for | 688 // think the page is 125% larger so the size of the page is correct for |
637 // minimum (default) scaling. | 689 // minimum (default) scaling. |
638 // This is important for sites that try to fill the page. | 690 // This is important for sites that try to fill the page. |
639 gfx::Size print_layout_size(print_canvas_size_); | 691 gfx::Size print_layout_size(print_canvas_size_); |
640 print_layout_size.set_height(static_cast<int>( | 692 print_layout_size.set_height(static_cast<int>( |
641 static_cast<double>(print_layout_size.height()) * 1.25)); | 693 static_cast<double>(print_layout_size.height()) * 1.25)); |
642 | 694 |
643 web_view_->resize(print_layout_size); | 695 web_view_->resize(print_layout_size); |
644 | 696 |
645 expected_pages_count_ = frame_->printBegin(print_canvas_size_, node_to_print_, | 697 expected_pages_count_ = frame_->printBegin(print_canvas_size_, |
646 dpi_, &use_browser_overlays_); | 698 printable_area_, |
699 paper_size_, | |
700 node_to_print_, dpi_, | |
vandebo (ex-Chrome)
2012/04/19 18:45:31
nit: you might as well put dpi on its own line.
kmadhusu
2012/04/20 22:29:57
Done.
| |
701 fit_pdf_page_to_paper_size_, | |
702 &use_browser_overlays_); | |
647 } | 703 } |
648 | 704 |
649 void PrepareFrameAndViewForPrint::FinishPrinting() { | 705 void PrepareFrameAndViewForPrint::FinishPrinting() { |
650 if (!finished_) { | 706 if (!finished_) { |
651 finished_ = true; | 707 finished_ = true; |
652 frame_->printEnd(); | 708 frame_->printEnd(); |
653 web_view_->resize(prev_view_size_); | 709 web_view_->resize(prev_view_size_); |
654 if (WebFrame* web_frame = web_view_->mainFrame()) | 710 if (WebFrame* web_frame = web_view_->mainFrame()) |
655 web_frame->setScrollOffset(prev_scroll_offset_); | 711 web_frame->setScrollOffset(prev_scroll_offset_); |
656 } | 712 } |
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1156 // actual printing. | 1212 // actual printing. |
1157 print_web_view_->mainFrame()->loadRequest(WebURLRequest(url)); | 1213 print_web_view_->mainFrame()->loadRequest(WebURLRequest(url)); |
1158 | 1214 |
1159 return true; | 1215 return true; |
1160 } | 1216 } |
1161 | 1217 |
1162 #if defined(OS_MACOSX) || defined(OS_WIN) | 1218 #if defined(OS_MACOSX) || defined(OS_WIN) |
1163 bool PrintWebViewHelper::PrintPages(WebFrame* frame, const WebNode& node) { | 1219 bool PrintWebViewHelper::PrintPages(WebFrame* frame, const WebNode& node) { |
1164 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 1220 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
1165 const PrintMsg_Print_Params& print_params = params.params; | 1221 const PrintMsg_Print_Params& print_params = params.params; |
1166 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); | 1222 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node, |
1223 print_for_preview_); | |
1167 UpdateFrameAndViewFromCssPageLayout(frame, node, &prep_frame_view, | 1224 UpdateFrameAndViewFromCssPageLayout(frame, node, &prep_frame_view, |
1168 print_params, ignore_css_margins_, | 1225 print_params, ignore_css_margins_, |
1169 fit_to_page_); | 1226 fit_to_page_); |
1170 | 1227 |
1171 int page_count = prep_frame_view.GetExpectedPageCount(); | 1228 int page_count = prep_frame_view.GetExpectedPageCount(); |
1172 if (!page_count) | 1229 if (!page_count) |
1173 return false; | 1230 return false; |
1174 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 1231 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
1175 print_params.document_cookie, | 1232 print_params.document_cookie, |
1176 page_count)); | 1233 page_count)); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1265 DCHECK(frame); | 1322 DCHECK(frame); |
1266 if (!InitPrintSettings()) { | 1323 if (!InitPrintSettings()) { |
1267 notify_browser_of_print_failure_ = false; | 1324 notify_browser_of_print_failure_ = false; |
1268 render_view()->RunModalAlertDialog( | 1325 render_view()->RunModalAlertDialog( |
1269 frame, | 1326 frame, |
1270 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | 1327 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
1271 return false; | 1328 return false; |
1272 } | 1329 } |
1273 | 1330 |
1274 DCHECK(!prepare->get()); | 1331 DCHECK(!prepare->get()); |
1275 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1332 prepare->reset(new PrepareFrameAndViewForPrint( |
1276 frame, node)); | 1333 print_pages_params_->params, frame, node, print_for_preview_)); |
vandebo (ex-Chrome)
2012/04/19 18:45:31
style: we only wrap if an argument won't fit.
kmadhusu
2012/04/20 22:29:57
Done.
| |
1277 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), | 1334 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), |
1278 print_pages_params_->params, | 1335 print_pages_params_->params, |
1279 ignore_css_margins_, fit_to_page_); | 1336 ignore_css_margins_, fit_to_page_); |
1280 Send(new PrintHostMsg_DidGetDocumentCookie( | 1337 Send(new PrintHostMsg_DidGetDocumentCookie( |
1281 routing_id(), print_pages_params_->params.document_cookie)); | 1338 routing_id(), print_pages_params_->params.document_cookie)); |
1282 return true; | 1339 return true; |
1283 } | 1340 } |
1284 | 1341 |
1285 bool PrintWebViewHelper::UpdatePrintSettings( | 1342 bool PrintWebViewHelper::UpdatePrintSettings( |
1286 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1343 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1640 | 1697 |
1641 metafile_.reset(new printing::PreviewMetafile); | 1698 metafile_.reset(new printing::PreviewMetafile); |
1642 if (!metafile_->Init()) { | 1699 if (!metafile_->Init()) { |
1643 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); | 1700 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); |
1644 LOG(ERROR) << "PreviewMetafile Init failed"; | 1701 LOG(ERROR) << "PreviewMetafile Init failed"; |
1645 return false; | 1702 return false; |
1646 } | 1703 } |
1647 | 1704 |
1648 // Need to make sure old object gets destroyed first. | 1705 // Need to make sure old object gets destroyed first. |
1649 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), | 1706 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
1650 node())); | 1707 node(), false)); |
1651 UpdateFrameAndViewFromCssPageLayout(frame_, node_, prep_frame_view_.get(), | 1708 UpdateFrameAndViewFromCssPageLayout(frame_, node_, prep_frame_view_.get(), |
1652 *print_params, ignore_css_margins, | 1709 *print_params, ignore_css_margins, |
1653 fit_to_page); | 1710 fit_to_page); |
1654 | 1711 |
1655 print_params_.reset(new PrintMsg_Print_Params(*print_params)); | 1712 print_params_.reset(new PrintMsg_Print_Params(*print_params)); |
1656 | 1713 |
1657 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1714 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
1658 if (total_page_count_ == 0) { | 1715 if (total_page_count_ == 0) { |
1659 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; | 1716 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; |
1660 set_error(PREVIEW_ERROR_ZERO_PAGES); | 1717 set_error(PREVIEW_ERROR_ZERO_PAGES); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1818 DCHECK(IsRendering()); | 1875 DCHECK(IsRendering()); |
1819 return prep_frame_view_->GetPrintCanvasSize(); | 1876 return prep_frame_view_->GetPrintCanvasSize(); |
1820 } | 1877 } |
1821 | 1878 |
1822 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1879 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1823 prep_frame_view_.reset(); | 1880 prep_frame_view_.reset(); |
1824 metafile_.reset(); | 1881 metafile_.reset(); |
1825 pages_to_render_.clear(); | 1882 pages_to_render_.clear(); |
1826 error_ = PREVIEW_ERROR_NONE; | 1883 error_ = PREVIEW_ERROR_NONE; |
1827 } | 1884 } |
OLD | NEW |