| 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 "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 1319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1330 } | 1330 } |
| 1331 } | 1331 } |
| 1332 print_preview_context_.Finished(); | 1332 print_preview_context_.Finished(); |
| 1333 return true; | 1333 return true; |
| 1334 } | 1334 } |
| 1335 | 1335 |
| 1336 #if !defined(OS_MACOSX) && BUILDFLAG(ENABLE_PRINT_PREVIEW) | 1336 #if !defined(OS_MACOSX) && BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 1337 bool PrintWebViewHelper::RenderPreviewPage( | 1337 bool PrintWebViewHelper::RenderPreviewPage( |
| 1338 int page_number, | 1338 int page_number, |
| 1339 const PrintMsg_Print_Params& print_params) { | 1339 const PrintMsg_Print_Params& print_params) { |
| 1340 PrintMsg_PrintPage_Params page_params; | |
| 1341 page_params.params = print_params; | |
| 1342 page_params.page_number = page_number; | |
| 1343 std::unique_ptr<PdfMetafileSkia> draft_metafile; | 1340 std::unique_ptr<PdfMetafileSkia> draft_metafile; |
| 1344 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); | 1341 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); |
| 1345 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { | 1342 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { |
| 1346 draft_metafile = base::MakeUnique<PdfMetafileSkia>(PDF_SKIA_DOCUMENT_TYPE); | 1343 draft_metafile = base::MakeUnique<PdfMetafileSkia>(PDF_SKIA_DOCUMENT_TYPE); |
| 1347 initial_render_metafile = draft_metafile.get(); | 1344 initial_render_metafile = draft_metafile.get(); |
| 1348 } | 1345 } |
| 1349 | 1346 |
| 1350 base::TimeTicks begin_time = base::TimeTicks::Now(); | 1347 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 1351 PrintPageInternal(page_params, print_preview_context_.prepared_frame(), | 1348 PrintPageInternal(print_params, page_number, |
| 1349 print_preview_context_.prepared_frame(), |
| 1352 initial_render_metafile, nullptr, nullptr, nullptr); | 1350 initial_render_metafile, nullptr, nullptr, nullptr); |
| 1353 print_preview_context_.RenderedPreviewPage( | 1351 print_preview_context_.RenderedPreviewPage( |
| 1354 base::TimeTicks::Now() - begin_time); | 1352 base::TimeTicks::Now() - begin_time); |
| 1355 if (draft_metafile.get()) { | 1353 if (draft_metafile.get()) { |
| 1356 draft_metafile->FinishDocument(); | 1354 draft_metafile->FinishDocument(); |
| 1357 } else if (print_preview_context_.IsModifiable() && | 1355 } else if (print_preview_context_.IsModifiable() && |
| 1358 print_preview_context_.generate_draft_pages()) { | 1356 print_preview_context_.generate_draft_pages()) { |
| 1359 DCHECK(!draft_metafile.get()); | 1357 DCHECK(!draft_metafile.get()); |
| 1360 draft_metafile = | 1358 draft_metafile = |
| 1361 print_preview_context_.metafile()->GetMetafileForCurrentPage( | 1359 print_preview_context_.metafile()->GetMetafileForCurrentPage( |
| (...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1839 print_pages_params_->pages.empty()); | 1837 print_pages_params_->pages.empty()); |
| 1840 prep_frame_view_->CopySelectionIfNeeded( | 1838 prep_frame_view_->CopySelectionIfNeeded( |
| 1841 render_frame()->GetWebkitPreferences(), | 1839 render_frame()->GetWebkitPreferences(), |
| 1842 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, | 1840 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, |
| 1843 base::Unretained(this))); | 1841 base::Unretained(this))); |
| 1844 return true; | 1842 return true; |
| 1845 } | 1843 } |
| 1846 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) | 1844 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 1847 | 1845 |
| 1848 #if !defined(OS_MACOSX) | 1846 #if !defined(OS_MACOSX) |
| 1849 void PrintWebViewHelper::PrintPageInternal( | 1847 void PrintWebViewHelper::PrintPageInternal(const PrintMsg_Print_Params& params, |
| 1850 const PrintMsg_PrintPage_Params& params, | 1848 int page_number, |
| 1851 blink::WebLocalFrame* frame, | 1849 blink::WebLocalFrame* frame, |
| 1852 PdfMetafileSkia* metafile, | 1850 PdfMetafileSkia* metafile, |
| 1853 gfx::Size* page_size_in_dpi, | 1851 gfx::Size* page_size_in_dpi, |
| 1854 gfx::Rect* content_area_in_dpi, | 1852 gfx::Rect* content_area_in_dpi, |
| 1855 gfx::Rect* printable_area_in_dpi) { | 1853 gfx::Rect* printable_area_in_dpi) { |
| 1856 PageSizeMargins page_layout_in_points; | 1854 PageSizeMargins page_layout_in_points; |
| 1857 | 1855 |
| 1858 double css_scale_factor = 1.0f; | 1856 double css_scale_factor = 1.0f; |
| 1859 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 1857 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 1860 if (params.params.scale_factor >= kEpsilon) | 1858 if (params.scale_factor >= kEpsilon) |
| 1861 css_scale_factor = params.params.scale_factor; | 1859 css_scale_factor = params.scale_factor; |
| 1862 #endif | 1860 #endif |
| 1863 | 1861 |
| 1864 // Save the original page size here to avoid rounding errors incurred by | 1862 // Save the original page size here to avoid rounding errors incurred by |
| 1865 // converting to pixels and back and by scaling the page for reflow and | 1863 // converting to pixels and back and by scaling the page for reflow and |
| 1866 // scaling back. Windows uses |page_size_in_dpi| for the actual page size | 1864 // scaling back. Windows uses |page_size_in_dpi| for the actual page size |
| 1867 // so requires an accurate value. | 1865 // so requires an accurate value. |
| 1868 gfx::Size original_page_size = params.params.page_size; | 1866 gfx::Size original_page_size = params.page_size; |
| 1869 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, | 1867 ComputePageLayoutInPointsForCss(frame, page_number, params, |
| 1870 ignore_css_margins_, &css_scale_factor, | 1868 ignore_css_margins_, &css_scale_factor, |
| 1871 &page_layout_in_points); | 1869 &page_layout_in_points); |
| 1872 gfx::Size page_size; | 1870 gfx::Size page_size; |
| 1873 gfx::Rect content_area; | 1871 gfx::Rect content_area; |
| 1874 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, | 1872 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, |
| 1875 &content_area); | 1873 &content_area); |
| 1876 | 1874 |
| 1877 // Calculate the actual page size and content area in dpi. | 1875 // Calculate the actual page size and content area in dpi. |
| 1878 if (page_size_in_dpi) { | 1876 if (page_size_in_dpi) { |
| 1879 *page_size_in_dpi = original_page_size; | 1877 *page_size_in_dpi = original_page_size; |
| 1880 } | 1878 } |
| 1881 | 1879 |
| 1882 if (content_area_in_dpi) { | 1880 if (content_area_in_dpi) { |
| 1883 // Output PDF matches paper size and should be printer edge to edge. | 1881 // Output PDF matches paper size and should be printer edge to edge. |
| 1884 *content_area_in_dpi = | 1882 *content_area_in_dpi = |
| 1885 gfx::Rect(0, 0, page_size_in_dpi->width(), page_size_in_dpi->height()); | 1883 gfx::Rect(0, 0, page_size_in_dpi->width(), page_size_in_dpi->height()); |
| 1886 } | 1884 } |
| 1887 if (printable_area_in_dpi) { | 1885 if (printable_area_in_dpi) { |
| 1888 *printable_area_in_dpi = printer_printable_area_; | 1886 *printable_area_in_dpi = printer_printable_area_; |
| 1889 } | 1887 } |
| 1890 | 1888 |
| 1891 gfx::Rect canvas_area = | 1889 gfx::Rect canvas_area = |
| 1892 params.params.display_header_footer ? gfx::Rect(page_size) : content_area; | 1890 params.display_header_footer ? gfx::Rect(page_size) : content_area; |
| 1893 | 1891 |
| 1894 // TODO(thestig): Figure out why Linux is different. | 1892 // TODO(thestig): Figure out why Linux is different. |
| 1895 #if defined(OS_WIN) | 1893 #if defined(OS_WIN) |
| 1896 float webkit_page_shrink_factor = | 1894 float webkit_page_shrink_factor = frame->GetPrintPageShrink(page_number); |
| 1897 frame->GetPrintPageShrink(params.page_number); | |
| 1898 float scale_factor = css_scale_factor * webkit_page_shrink_factor; | 1895 float scale_factor = css_scale_factor * webkit_page_shrink_factor; |
| 1899 #else | 1896 #else |
| 1900 float scale_factor = css_scale_factor; | 1897 float scale_factor = css_scale_factor; |
| 1901 #endif | 1898 #endif |
| 1902 | 1899 |
| 1903 cc::PaintCanvas* canvas = | 1900 cc::PaintCanvas* canvas = |
| 1904 metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor); | 1901 metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor); |
| 1905 if (!canvas) | 1902 if (!canvas) |
| 1906 return; | 1903 return; |
| 1907 | 1904 |
| 1908 MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile); | 1905 MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile); |
| 1909 | 1906 |
| 1910 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 1907 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 1911 if (params.params.display_header_footer) { | 1908 if (params.display_header_footer) { |
| 1912 // TODO(thestig): Figure out why Linux needs this. It is almost certainly | 1909 // TODO(thestig): Figure out why Linux needs this. It is almost certainly |
| 1913 // |printingMinimumShrinkFactor| from Blink. | 1910 // |printingMinimumShrinkFactor| from Blink. |
| 1914 #if defined(OS_WIN) | 1911 #if defined(OS_WIN) |
| 1915 const float fudge_factor = 1; | 1912 const float fudge_factor = 1; |
| 1916 #else | 1913 #else |
| 1917 const float fudge_factor = kPrintingMinimumShrinkFactor; | 1914 const float fudge_factor = kPrintingMinimumShrinkFactor; |
| 1918 #endif | 1915 #endif |
| 1919 // |page_number| is 0-based, so 1 is added. | 1916 // |page_number| is 0-based, so 1 is added. |
| 1920 PrintHeaderAndFooter(canvas, params.page_number + 1, | 1917 PrintHeaderAndFooter( |
| 1921 print_preview_context_.total_page_count(), *frame, | 1918 canvas, page_number + 1, print_preview_context_.total_page_count(), |
| 1922 scale_factor / fudge_factor, page_layout_in_points, | 1919 *frame, scale_factor / fudge_factor, page_layout_in_points, params); |
| 1923 params.params); | |
| 1924 } | 1920 } |
| 1925 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) | 1921 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 1926 | 1922 |
| 1927 float webkit_scale_factor = | 1923 float webkit_scale_factor = RenderPageContent( |
| 1928 RenderPageContent(frame, params.page_number, canvas_area, content_area, | 1924 frame, page_number, canvas_area, content_area, scale_factor, canvas); |
| 1929 scale_factor, canvas); | |
| 1930 DCHECK_GT(webkit_scale_factor, 0.0f); | 1925 DCHECK_GT(webkit_scale_factor, 0.0f); |
| 1931 | 1926 |
| 1932 // Done printing. Close the canvas to retrieve the compiled metafile. | 1927 // Done printing. Close the canvas to retrieve the compiled metafile. |
| 1933 if (!metafile->FinishPage()) | 1928 if (!metafile->FinishPage()) |
| 1934 NOTREACHED() << "metafile failed"; | 1929 NOTREACHED() << "metafile failed"; |
| 1935 } | 1930 } |
| 1936 #endif // !defined(OS_MACOSX) | 1931 #endif // !defined(OS_MACOSX) |
| 1937 | 1932 |
| 1938 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | 1933 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( |
| 1939 const PdfMetafileSkia& metafile, | 1934 const PdfMetafileSkia& metafile, |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2371 blink::WebConsoleMessage::kLevelWarning, message)); | 2366 blink::WebConsoleMessage::kLevelWarning, message)); |
| 2372 return false; | 2367 return false; |
| 2373 } | 2368 } |
| 2374 | 2369 |
| 2375 void PrintWebViewHelper::ScriptingThrottler::Reset() { | 2370 void PrintWebViewHelper::ScriptingThrottler::Reset() { |
| 2376 // Reset counter on successful print. | 2371 // Reset counter on successful print. |
| 2377 count_ = 0; | 2372 count_ = 0; |
| 2378 } | 2373 } |
| 2379 | 2374 |
| 2380 } // namespace printing | 2375 } // namespace printing |
| OLD | NEW |