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

Side by Side Diff: components/printing/renderer/print_web_view_helper.cc

Issue 2829973002: add customized printing setting for headless (Closed)
Patch Set: add skia as public_deps instead Created 3 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
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 "components/printing/renderer/print_web_view_helper.h" 5 #include "components/printing/renderer/print_web_view_helper.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 PREVIEW_EVENT_MAX, 82 PREVIEW_EVENT_MAX,
83 }; 83 };
84 84
85 const double kMinDpi = 1.0; 85 const double kMinDpi = 1.0;
86 86
87 // Also set in third_party/WebKit/Source/core/page/PrintContext.h 87 // Also set in third_party/WebKit/Source/core/page/PrintContext.h
88 const float kPrintingMinimumShrinkFactor = 1.33333333f; 88 const float kPrintingMinimumShrinkFactor = 1.33333333f;
89 89
90 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) 90 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
91 bool g_is_preview_enabled = true; 91 bool g_is_preview_enabled = true;
92 #else
93 bool g_is_preview_enabled = false;
94 #endif
92 95
93 const char kPageLoadScriptFormat[] = 96 const char kPageLoadScriptFormat[] =
94 "document.open(); document.write(%s); document.close();"; 97 "document.open(); document.write(%s); document.close();";
95 98
96 const char kPageSetupScriptFormat[] = "setup(%s);"; 99 const char kPageSetupScriptFormat[] = "setup(%s);";
97 100
98 void ExecuteScript(blink::WebFrame* frame, 101 void ExecuteScript(blink::WebFrame* frame,
99 const char* script_format, 102 const char* script_format,
100 const base::Value& parameters) { 103 const base::Value& parameters) {
101 std::string json; 104 std::string json;
102 base::JSONWriter::Write(parameters, &json); 105 base::JSONWriter::Write(parameters, &json);
103 std::string script = base::StringPrintf(script_format, json.c_str()); 106 std::string script = base::StringPrintf(script_format, json.c_str());
104 frame->ExecuteScript(blink::WebString::FromUTF8(script)); 107 frame->ExecuteScript(blink::WebString::FromUTF8(script));
105 } 108 }
106 #else
107 bool g_is_preview_enabled = false;
108 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
109 109
110 int GetDPI(const PrintMsg_Print_Params* print_params) { 110 int GetDPI(const PrintMsg_Print_Params* print_params) {
111 #if defined(OS_MACOSX) 111 #if defined(OS_MACOSX)
112 // On the Mac, the printable area is in points, don't do any scaling based 112 // On the Mac, the printable area is in points, don't do any scaling based
113 // on dpi. 113 // on dpi.
114 return kPointsPerInch; 114 return kPointsPerInch;
115 #else 115 #else
116 return static_cast<int>(print_params->dpi); 116 return static_cast<int>(print_params->dpi);
117 #endif // defined(OS_MACOSX) 117 #endif // defined(OS_MACOSX)
118 } 118 }
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 if (frame == frame_) 552 if (frame == frame_)
553 return frame_; 553 return frame_;
554 } 554 }
555 return NULL; 555 return NULL;
556 } 556 }
557 557
558 blink::WebView* FrameReference::view() { 558 blink::WebView* FrameReference::view() {
559 return view_; 559 return view_;
560 } 560 }
561 561
562 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
563 // static - Not anonymous so that platform implementations can use it. 562 // static - Not anonymous so that platform implementations can use it.
564 void PrintWebViewHelper::PrintHeaderAndFooter( 563 void PrintWebViewHelper::PrintHeaderAndFooter(
565 blink::WebCanvas* canvas, 564 blink::WebCanvas* canvas,
566 int page_number, 565 int page_number,
567 int total_pages, 566 int total_pages,
568 const blink::WebFrame& source_frame, 567 const blink::WebFrame& source_frame,
569 float webkit_scale_factor, 568 float webkit_scale_factor,
570 const PageSizeMargins& page_layout, 569 const PageSizeMargins& page_layout,
571 const PrintMsg_Print_Params& params) { 570 const PrintMsg_Print_Params& params) {
572 cc::PaintCanvasAutoRestore auto_restore(canvas, true); 571 cc::PaintCanvasAutoRestore auto_restore(canvas, true);
573 canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor); 572 canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor);
574 573
575 blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right + 574 blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right +
576 page_layout.content_width, 575 page_layout.content_width,
577 page_layout.margin_top + page_layout.margin_bottom + 576 page_layout.margin_top + page_layout.margin_bottom +
578 page_layout.content_height); 577 page_layout.content_height);
579 578
580 blink::WebView* web_view = 579 blink::WebView* web_view =
581 blink::WebView::Create(nullptr, blink::kWebPageVisibilityStateVisible); 580 blink::WebView::Create(nullptr, blink::kWebPageVisibilityStateVisible);
582 web_view->GetSettings()->SetJavaScriptEnabled(true); 581 web_view->GetSettings()->SetJavaScriptEnabled(true);
583 582
584 blink::WebFrameClient frame_client; 583 blink::WebFrameClient frame_client;
585 blink::WebLocalFrame* frame = blink::WebLocalFrame::Create( 584 blink::WebLocalFrame* frame = blink::WebLocalFrame::Create(
586 blink::WebTreeScopeType::kDocument, &frame_client, nullptr, nullptr); 585 blink::WebTreeScopeType::kDocument, &frame_client, nullptr, nullptr);
587 web_view->SetMainFrame(frame); 586 web_view->SetMainFrame(frame);
588 blink::WebWidgetClient web_widget_client; 587 blink::WebWidgetClient web_widget_client;
589 blink::WebFrameWidget::Create(&web_widget_client, web_view, frame); 588 blink::WebFrameWidget::Create(&web_widget_client, web_view, frame);
590 589
591 base::Value html(ResourceBundle::GetSharedInstance().GetLocalizedString( 590 base::Value html(
592 IDR_PRINT_PREVIEW_PAGE)); 591 base::UTF8ToUTF16(ResourceBundle::GetSharedInstance().GetRawDataResource(
592 IDR_PRINT_PREVIEW_PAGE)));
593 // Load page with script to avoid async operations. 593 // Load page with script to avoid async operations.
594 ExecuteScript(frame, kPageLoadScriptFormat, html); 594 ExecuteScript(frame, kPageLoadScriptFormat, html);
595 595
596 auto options = base::MakeUnique<base::DictionaryValue>(); 596 auto options = base::MakeUnique<base::DictionaryValue>();
597 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); 597 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime());
598 options->SetDouble("width", page_size.width); 598 options->SetDouble("width", page_size.width);
599 options->SetDouble("height", page_size.height); 599 options->SetDouble("height", page_size.height);
600 options->SetDouble("topMargin", page_layout.margin_top); 600 options->SetDouble("topMargin", page_layout.margin_top);
601 options->SetDouble("bottomMargin", page_layout.margin_bottom); 601 options->SetDouble("bottomMargin", page_layout.margin_bottom);
602 options->SetString("pageNumber", 602 options->SetString("pageNumber",
603 base::StringPrintf("%d/%d", page_number, total_pages)); 603 base::StringPrintf("%d/%d", page_number, total_pages));
604 604
605 options->SetString("url", params.url); 605 options->SetString("url", params.url);
606 base::string16 title = source_frame.GetDocument().Title().Utf16(); 606 base::string16 title = source_frame.GetDocument().Title().Utf16();
607 options->SetString("title", title.empty() ? params.title : title); 607 options->SetString("title", title.empty() ? params.title : title);
608 608
609 ExecuteScript(frame, kPageSetupScriptFormat, *options); 609 ExecuteScript(frame, kPageSetupScriptFormat, *options);
610 610
611 blink::WebPrintParams webkit_params(page_size); 611 blink::WebPrintParams webkit_params(page_size);
612 webkit_params.printer_dpi = GetDPI(&params); 612 webkit_params.printer_dpi = GetDPI(&params);
613 613
614 frame->PrintBegin(webkit_params); 614 frame->PrintBegin(webkit_params);
615 frame->PrintPage(0, canvas); 615 frame->PrintPage(0, canvas);
616 frame->PrintEnd(); 616 frame->PrintEnd();
617 617
618 web_view->Close(); 618 web_view->Close();
619 } 619 }
620 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
621 620
622 // static - Not anonymous so that platform implementations can use it. 621 // static - Not anonymous so that platform implementations can use it.
623 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame, 622 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame,
624 int page_number, 623 int page_number,
625 const gfx::Rect& canvas_area, 624 const gfx::Rect& canvas_area,
626 const gfx::Rect& content_area, 625 const gfx::Rect& content_area,
627 double scale_factor, 626 double scale_factor,
628 blink::WebCanvas* canvas) { 627 blink::WebCanvas* canvas) {
629 cc::PaintCanvasAutoRestore auto_restore(canvas, true); 628 cc::PaintCanvasAutoRestore auto_restore(canvas, true);
630 canvas->translate((content_area.x() - canvas_area.x()) / scale_factor, 629 canvas->translate((content_area.x() - canvas_area.x()) / scale_factor,
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 is_printing_started_(false), 716 is_printing_started_(false),
718 weak_ptr_factory_(this) { 717 weak_ptr_factory_(this) {
719 PrintMsg_Print_Params print_params = params; 718 PrintMsg_Print_Params print_params = params;
720 if (!should_print_selection_only_ || 719 if (!should_print_selection_only_ ||
721 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { 720 !PrintingNodeOrPdfFrame(frame, node_to_print_)) {
722 bool fit_to_page = 721 bool fit_to_page =
723 ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params); 722 ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params);
724 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_); 723 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_);
725 frame->PrintBegin(web_print_params_, node_to_print_); 724 frame->PrintBegin(web_print_params_, node_to_print_);
726 double scale_factor = 1.0f; 725 double scale_factor = 1.0f;
727 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
728 if (print_params.scale_factor >= PrintWebViewHelper::kEpsilon) 726 if (print_params.scale_factor >= PrintWebViewHelper::kEpsilon)
729 scale_factor = print_params.scale_factor; 727 scale_factor = print_params.scale_factor;
730 #endif
731 print_params = CalculatePrintParamsForCss( 728 print_params = CalculatePrintParamsForCss(
732 frame, 0, print_params, ignore_css_margins, fit_to_page, &scale_factor); 729 frame, 0, print_params, ignore_css_margins, fit_to_page, &scale_factor);
733 frame->PrintEnd(); 730 frame->PrintEnd();
734 } 731 }
735 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_); 732 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_);
736 } 733 }
737 734
738 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { 735 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
739 FinishPrinting(); 736 FinishPrinting();
740 } 737 }
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after
1343 const PrintMsg_Print_Params& print_params) { 1340 const PrintMsg_Print_Params& print_params) {
1344 std::unique_ptr<PdfMetafileSkia> draft_metafile; 1341 std::unique_ptr<PdfMetafileSkia> draft_metafile;
1345 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); 1342 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
1346 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { 1343 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) {
1347 draft_metafile = base::MakeUnique<PdfMetafileSkia>(PDF_SKIA_DOCUMENT_TYPE); 1344 draft_metafile = base::MakeUnique<PdfMetafileSkia>(PDF_SKIA_DOCUMENT_TYPE);
1348 initial_render_metafile = draft_metafile.get(); 1345 initial_render_metafile = draft_metafile.get();
1349 } 1346 }
1350 1347
1351 base::TimeTicks begin_time = base::TimeTicks::Now(); 1348 base::TimeTicks begin_time = base::TimeTicks::Now();
1352 PrintPageInternal(print_params, page_number, 1349 PrintPageInternal(print_params, page_number,
1350 print_preview_context_.total_page_count(),
1353 print_preview_context_.prepared_frame(), 1351 print_preview_context_.prepared_frame(),
1354 initial_render_metafile, nullptr, nullptr, nullptr); 1352 initial_render_metafile, nullptr, nullptr, nullptr);
1355 print_preview_context_.RenderedPreviewPage( 1353 print_preview_context_.RenderedPreviewPage(
1356 base::TimeTicks::Now() - begin_time); 1354 base::TimeTicks::Now() - begin_time);
1357 if (draft_metafile.get()) { 1355 if (draft_metafile.get()) {
1358 draft_metafile->FinishDocument(); 1356 draft_metafile->FinishDocument();
1359 } else if (print_preview_context_.IsModifiable() && 1357 } else if (print_preview_context_.IsModifiable() &&
1360 print_preview_context_.generate_draft_pages()) { 1358 print_preview_context_.generate_draft_pages()) {
1361 DCHECK(!draft_metafile.get()); 1359 DCHECK(!draft_metafile.get());
1362 draft_metafile = 1360 draft_metafile =
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
1843 render_frame()->GetWebkitPreferences(), 1841 render_frame()->GetWebkitPreferences(),
1844 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, 1842 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages,
1845 weak_ptr_factory_.GetWeakPtr())); 1843 weak_ptr_factory_.GetWeakPtr()));
1846 return true; 1844 return true;
1847 } 1845 }
1848 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) 1846 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
1849 1847
1850 #if !defined(OS_MACOSX) 1848 #if !defined(OS_MACOSX)
1851 void PrintWebViewHelper::PrintPageInternal(const PrintMsg_Print_Params& params, 1849 void PrintWebViewHelper::PrintPageInternal(const PrintMsg_Print_Params& params,
1852 int page_number, 1850 int page_number,
1851 int page_count,
1853 blink::WebLocalFrame* frame, 1852 blink::WebLocalFrame* frame,
1854 PdfMetafileSkia* metafile, 1853 PdfMetafileSkia* metafile,
1855 gfx::Size* page_size_in_dpi, 1854 gfx::Size* page_size_in_dpi,
1856 gfx::Rect* content_area_in_dpi, 1855 gfx::Rect* content_area_in_dpi,
1857 gfx::Rect* printable_area_in_dpi) { 1856 gfx::Rect* printable_area_in_dpi) {
1858 PageSizeMargins page_layout_in_points; 1857 PageSizeMargins page_layout_in_points;
1859 1858
1860 double css_scale_factor = 1.0f; 1859 double css_scale_factor = 1.0f;
1861 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1862 if (params.scale_factor >= kEpsilon) 1860 if (params.scale_factor >= kEpsilon)
1863 css_scale_factor = params.scale_factor; 1861 css_scale_factor = params.scale_factor;
1864 #endif
1865 1862
1866 // Save the original page size here to avoid rounding errors incurred by 1863 // Save the original page size here to avoid rounding errors incurred by
1867 // converting to pixels and back and by scaling the page for reflow and 1864 // converting to pixels and back and by scaling the page for reflow and
1868 // scaling back. Windows uses |page_size_in_dpi| for the actual page size 1865 // scaling back. Windows uses |page_size_in_dpi| for the actual page size
1869 // so requires an accurate value. 1866 // so requires an accurate value.
1870 gfx::Size original_page_size = params.page_size; 1867 gfx::Size original_page_size = params.page_size;
1871 ComputePageLayoutInPointsForCss(frame, page_number, params, 1868 ComputePageLayoutInPointsForCss(frame, page_number, params,
1872 ignore_css_margins_, &css_scale_factor, 1869 ignore_css_margins_, &css_scale_factor,
1873 &page_layout_in_points); 1870 &page_layout_in_points);
1874 gfx::Size page_size; 1871 gfx::Size page_size;
(...skipping 26 matching lines...) Expand all
1901 float scale_factor = css_scale_factor; 1898 float scale_factor = css_scale_factor;
1902 #endif 1899 #endif
1903 1900
1904 cc::PaintCanvas* canvas = 1901 cc::PaintCanvas* canvas =
1905 metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor); 1902 metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor);
1906 if (!canvas) 1903 if (!canvas)
1907 return; 1904 return;
1908 1905
1909 MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile); 1906 MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile);
1910 1907
1911 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1912 if (params.display_header_footer) { 1908 if (params.display_header_footer) {
1913 #if defined(OS_WIN) 1909 #if defined(OS_WIN)
1914 const float fudge_factor = 1; 1910 const float fudge_factor = 1;
1915 #else 1911 #else
1916 // TODO(thestig): Figure out why Linux needs this. It is almost certainly 1912 // TODO(thestig): Figure out why Linux needs this. It is almost certainly
1917 // |kPrintingMinimumShrinkFactor| from Blink. 1913 // |kPrintingMinimumShrinkFactor| from Blink.
1918 const float fudge_factor = kPrintingMinimumShrinkFactor; 1914 const float fudge_factor = kPrintingMinimumShrinkFactor;
1919 #endif 1915 #endif
1920 // |page_number| is 0-based, so 1 is added. 1916 // |page_number| is 0-based, so 1 is added.
1921 PrintHeaderAndFooter( 1917 PrintHeaderAndFooter(canvas, page_number + 1, page_count, *frame,
1922 canvas, page_number + 1, print_preview_context_.total_page_count(), 1918 scale_factor / fudge_factor, page_layout_in_points,
1923 *frame, scale_factor / fudge_factor, page_layout_in_points, params); 1919 params);
1924 } 1920 }
1925 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
1926 1921
1927 float webkit_scale_factor = RenderPageContent( 1922 float webkit_scale_factor = RenderPageContent(
1928 frame, page_number, canvas_area, content_area, scale_factor, canvas); 1923 frame, page_number, canvas_area, content_area, scale_factor, canvas);
1929 DCHECK_GT(webkit_scale_factor, 0.0f); 1924 DCHECK_GT(webkit_scale_factor, 0.0f);
1930 1925
1931 // Done printing. Close the canvas to retrieve the compiled metafile. 1926 // Done printing. Close the canvas to retrieve the compiled metafile.
1932 if (!metafile->FinishPage()) 1927 if (!metafile->FinishPage())
1933 NOTREACHED() << "metafile failed"; 1928 NOTREACHED() << "metafile failed";
1934 } 1929 }
1935 #endif // !defined(OS_MACOSX) 1930 #endif // !defined(OS_MACOSX)
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after
2377 blink::WebConsoleMessage::kLevelWarning, message)); 2372 blink::WebConsoleMessage::kLevelWarning, message));
2378 return false; 2373 return false;
2379 } 2374 }
2380 2375
2381 void PrintWebViewHelper::ScriptingThrottler::Reset() { 2376 void PrintWebViewHelper::ScriptingThrottler::Reset() {
2382 // Reset counter on successful print. 2377 // Reset counter on successful print.
2383 count_ = 0; 2378 count_ = 0;
2384 } 2379 }
2385 2380
2386 } // namespace printing 2381 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698