| 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 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/thread_task_runner_handle.h" | 23 #include "base/thread_task_runner_handle.h" |
| 24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 25 #include "components/printing/common/print_messages.h" | 25 #include "components/printing/common/print_messages.h" |
| 26 #include "content/public/common/web_preferences.h" | 26 #include "content/public/common/web_preferences.h" |
| 27 #include "content/public/renderer/render_frame.h" | 27 #include "content/public/renderer/render_frame.h" |
| 28 #include "content/public/renderer/render_thread.h" | 28 #include "content/public/renderer/render_thread.h" |
| 29 #include "content/public/renderer/render_view.h" | 29 #include "content/public/renderer/render_view.h" |
| 30 #include "grit/components_resources.h" | 30 #include "grit/components_resources.h" |
| 31 #include "net/base/escape.h" | 31 #include "net/base/escape.h" |
| 32 #include "printing/metafile_skia_wrapper.h" |
| 32 #include "printing/pdf_metafile_skia.h" | 33 #include "printing/pdf_metafile_skia.h" |
| 33 #include "printing/units.h" | 34 #include "printing/units.h" |
| 34 #include "third_party/WebKit/public/platform/WebSize.h" | 35 #include "third_party/WebKit/public/platform/WebSize.h" |
| 35 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 36 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 36 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 37 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
| 37 #include "third_party/WebKit/public/web/WebDocument.h" | 38 #include "third_party/WebKit/public/web/WebDocument.h" |
| 38 #include "third_party/WebKit/public/web/WebElement.h" | 39 #include "third_party/WebKit/public/web/WebElement.h" |
| 39 #include "third_party/WebKit/public/web/WebFrameClient.h" | 40 #include "third_party/WebKit/public/web/WebFrameClient.h" |
| 40 #include "third_party/WebKit/public/web/WebFrameOwnerProperties.h" | 41 #include "third_party/WebKit/public/web/WebFrameOwnerProperties.h" |
| 41 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 42 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| (...skipping 1175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1217 DCHECK(print_preview_context_.IsModifiable() || | 1218 DCHECK(print_preview_context_.IsModifiable() || |
| 1218 print_preview_context_.IsFinalPageRendered()); | 1219 print_preview_context_.IsFinalPageRendered()); |
| 1219 if (!FinalizePrintReadyDocument()) | 1220 if (!FinalizePrintReadyDocument()) |
| 1220 return false; | 1221 return false; |
| 1221 } | 1222 } |
| 1222 } | 1223 } |
| 1223 print_preview_context_.Finished(); | 1224 print_preview_context_.Finished(); |
| 1224 return true; | 1225 return true; |
| 1225 } | 1226 } |
| 1226 | 1227 |
| 1228 #if !defined(OS_MACOSX) && defined(ENABLE_PRINT_PREVIEW) |
| 1229 bool PrintWebViewHelper::RenderPreviewPage( |
| 1230 int page_number, |
| 1231 const PrintMsg_Print_Params& print_params) { |
| 1232 PrintMsg_PrintPage_Params page_params; |
| 1233 page_params.params = print_params; |
| 1234 page_params.page_number = page_number; |
| 1235 scoped_ptr<PdfMetafileSkia> draft_metafile; |
| 1236 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); |
| 1237 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { |
| 1238 draft_metafile.reset(new PdfMetafileSkia); |
| 1239 initial_render_metafile = draft_metafile.get(); |
| 1240 } |
| 1241 |
| 1242 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 1243 PrintPageInternal(page_params, print_preview_context_.prepared_frame(), |
| 1244 initial_render_metafile, nullptr, nullptr); |
| 1245 print_preview_context_.RenderedPreviewPage( |
| 1246 base::TimeTicks::Now() - begin_time); |
| 1247 if (draft_metafile.get()) { |
| 1248 draft_metafile->FinishDocument(); |
| 1249 } else if (print_preview_context_.IsModifiable() && |
| 1250 print_preview_context_.generate_draft_pages()) { |
| 1251 DCHECK(!draft_metafile.get()); |
| 1252 draft_metafile = |
| 1253 print_preview_context_.metafile()->GetMetafileForCurrentPage(); |
| 1254 } |
| 1255 return PreviewPageRendered(page_number, draft_metafile.get()); |
| 1256 } |
| 1257 #endif // !defined(OS_MACOSX) && defined(ENABLE_PRINT_PREVIEW) |
| 1258 |
| 1227 bool PrintWebViewHelper::FinalizePrintReadyDocument() { | 1259 bool PrintWebViewHelper::FinalizePrintReadyDocument() { |
| 1228 DCHECK(!is_print_ready_metafile_sent_); | 1260 DCHECK(!is_print_ready_metafile_sent_); |
| 1229 print_preview_context_.FinalizePrintReadyDocument(); | 1261 print_preview_context_.FinalizePrintReadyDocument(); |
| 1230 | 1262 |
| 1231 PdfMetafileSkia* metafile = print_preview_context_.metafile(); | 1263 PdfMetafileSkia* metafile = print_preview_context_.metafile(); |
| 1232 PrintHostMsg_DidPreviewDocument_Params preview_params; | 1264 PrintHostMsg_DidPreviewDocument_Params preview_params; |
| 1233 | 1265 |
| 1234 // Ask the browser to create the shared memory for us. | 1266 // Ask the browser to create the shared memory for us. |
| 1235 if (!CopyMetafileDataToSharedMem(*metafile, | 1267 if (!CopyMetafileDataToSharedMem(*metafile, |
| 1236 &(preview_params.metafile_data_handle))) { | 1268 &(preview_params.metafile_data_handle))) { |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1685 DCHECK(!print_pages_params_->params.selection_only || | 1717 DCHECK(!print_pages_params_->params.selection_only || |
| 1686 print_pages_params_->pages.empty()); | 1718 print_pages_params_->pages.empty()); |
| 1687 prep_frame_view_->CopySelectionIfNeeded( | 1719 prep_frame_view_->CopySelectionIfNeeded( |
| 1688 render_view()->GetWebkitPreferences(), | 1720 render_view()->GetWebkitPreferences(), |
| 1689 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, | 1721 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, |
| 1690 base::Unretained(this))); | 1722 base::Unretained(this))); |
| 1691 return true; | 1723 return true; |
| 1692 } | 1724 } |
| 1693 #endif // defined(ENABLE_BASIC_PRINTING) | 1725 #endif // defined(ENABLE_BASIC_PRINTING) |
| 1694 | 1726 |
| 1695 #if defined(OS_POSIX) | 1727 #if !defined(OS_MACOSX) |
| 1728 void PrintWebViewHelper::PrintPageInternal( |
| 1729 const PrintMsg_PrintPage_Params& params, |
| 1730 blink::WebFrame* frame, |
| 1731 PdfMetafileSkia* metafile, |
| 1732 gfx::Size* page_size_in_dpi, |
| 1733 gfx::Rect* content_area_in_dpi) { |
| 1734 PageSizeMargins page_layout_in_points; |
| 1735 double css_scale_factor = 1.0f; |
| 1736 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, |
| 1737 ignore_css_margins_, &css_scale_factor, |
| 1738 &page_layout_in_points); |
| 1739 gfx::Size page_size; |
| 1740 gfx::Rect content_area; |
| 1741 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, |
| 1742 &content_area); |
| 1743 int dpi = static_cast<int>(params.params.dpi); |
| 1744 // Calculate the actual page size and content area in dpi. |
| 1745 if (page_size_in_dpi) { |
| 1746 *page_size_in_dpi = |
| 1747 gfx::Size(static_cast<int>(ConvertUnitDouble(page_size.width(), |
| 1748 kPointsPerInch, dpi)), |
| 1749 static_cast<int>(ConvertUnitDouble(page_size.height(), |
| 1750 kPointsPerInch, dpi))); |
| 1751 } |
| 1752 |
| 1753 if (content_area_in_dpi) { |
| 1754 // Output PDF matches paper size and should be printer edge to edge. |
| 1755 *content_area_in_dpi = |
| 1756 gfx::Rect(0, 0, page_size_in_dpi->width(), page_size_in_dpi->height()); |
| 1757 } |
| 1758 |
| 1759 gfx::Rect canvas_area = |
| 1760 params.params.display_header_footer ? gfx::Rect(page_size) : content_area; |
| 1761 |
| 1762 #if defined(OS_WIN) || defined(ENABLE_PRINT_PREVIEW) |
| 1763 float webkit_page_shrink_factor = |
| 1764 frame->getPrintPageShrink(params.page_number); |
| 1765 float scale_factor = css_scale_factor * webkit_page_shrink_factor; |
| 1766 #endif |
| 1767 // TODO(thestig) GetVectorCanvasForNewPage() and RenderPageContent() take a |
| 1768 // different scale factor vs Windows. Figure out why and combine the two. |
| 1769 #if defined(OS_WIN) |
| 1770 float platform_scale_factor = scale_factor; |
| 1771 #else |
| 1772 float platform_scale_factor = css_scale_factor; |
| 1773 #endif // defined(OS_WIN) |
| 1774 |
| 1775 skia::PlatformCanvas* canvas = metafile->GetVectorCanvasForNewPage( |
| 1776 page_size, canvas_area, platform_scale_factor); |
| 1777 if (!canvas) |
| 1778 return; |
| 1779 |
| 1780 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
| 1781 |
| 1782 #if defined(ENABLE_PRINT_PREVIEW) |
| 1783 if (params.params.display_header_footer) { |
| 1784 // |page_number| is 0-based, so 1 is added. |
| 1785 PrintHeaderAndFooter(canvas, params.page_number + 1, |
| 1786 print_preview_context_.total_page_count(), *frame, |
| 1787 scale_factor, page_layout_in_points, params.params); |
| 1788 } |
| 1789 #endif // defined(ENABLE_PRINT_PREVIEW) |
| 1790 |
| 1791 float webkit_scale_factor = |
| 1792 RenderPageContent(frame, params.page_number, canvas_area, content_area, |
| 1793 platform_scale_factor, canvas); |
| 1794 DCHECK_GT(webkit_scale_factor, 0.0f); |
| 1795 |
| 1796 // Done printing. Close the canvas to retrieve the compiled metafile. |
| 1797 if (!metafile->FinishPage()) |
| 1798 NOTREACHED() << "metafile failed"; |
| 1799 } |
| 1800 #endif // !defined(OS_MACOSX) |
| 1801 |
| 1696 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | 1802 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( |
| 1697 const PdfMetafileSkia& metafile, | 1803 const PdfMetafileSkia& metafile, |
| 1698 base::SharedMemoryHandle* shared_mem_handle) { | 1804 base::SharedMemoryHandle* shared_mem_handle) { |
| 1699 uint32_t buf_size = metafile.GetDataSize(); | 1805 uint32_t buf_size = metafile.GetDataSize(); |
| 1700 if (buf_size == 0) | 1806 if (buf_size == 0) |
| 1701 return false; | 1807 return false; |
| 1702 | 1808 |
| 1809 #if defined(OS_WIN) |
| 1810 base::SharedMemory shared_buf; |
| 1811 // Allocate a shared memory buffer to hold the generated metafile data. |
| 1812 if (!shared_buf.CreateAndMapAnonymous(buf_size)) |
| 1813 return false; |
| 1814 |
| 1815 // Copy the bits into shared memory. |
| 1816 if (!metafile.GetData(shared_buf.memory(), buf_size)) |
| 1817 return false; |
| 1818 |
| 1819 if (!shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), |
| 1820 shared_mem_handle)) { |
| 1821 return false; |
| 1822 } |
| 1823 |
| 1824 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
| 1825 shared_mem_handle)); |
| 1826 return true; |
| 1827 #else |
| 1703 scoped_ptr<base::SharedMemory> shared_buf( | 1828 scoped_ptr<base::SharedMemory> shared_buf( |
| 1704 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(buf_size)); | 1829 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(buf_size)); |
| 1705 if (!shared_buf) | 1830 if (!shared_buf) |
| 1706 return false; | 1831 return false; |
| 1707 | 1832 |
| 1708 if (!shared_buf->Map(buf_size)) | 1833 if (!shared_buf->Map(buf_size)) |
| 1709 return false; | 1834 return false; |
| 1710 | 1835 |
| 1711 if (!metafile.GetData(shared_buf->memory(), buf_size)) | 1836 if (!metafile.GetData(shared_buf->memory(), buf_size)) |
| 1712 return false; | 1837 return false; |
| 1713 | 1838 |
| 1714 return shared_buf->GiveToProcess(base::GetCurrentProcessHandle(), | 1839 return shared_buf->GiveToProcess(base::GetCurrentProcessHandle(), |
| 1715 shared_mem_handle); | 1840 shared_mem_handle); |
| 1841 #endif // defined(OS_WIN) |
| 1716 } | 1842 } |
| 1717 #endif // defined(OS_POSIX) | |
| 1718 | 1843 |
| 1719 #if defined(ENABLE_PRINT_PREVIEW) | 1844 #if defined(ENABLE_PRINT_PREVIEW) |
| 1720 void PrintWebViewHelper::ShowScriptedPrintPreview() { | 1845 void PrintWebViewHelper::ShowScriptedPrintPreview() { |
| 1721 if (is_scripted_preview_delayed_) { | 1846 if (is_scripted_preview_delayed_) { |
| 1722 is_scripted_preview_delayed_ = false; | 1847 is_scripted_preview_delayed_ = false; |
| 1723 Send(new PrintHostMsg_ShowScriptedPrintPreview( | 1848 Send(new PrintHostMsg_ShowScriptedPrintPreview( |
| 1724 routing_id(), print_preview_context_.IsModifiable())); | 1849 routing_id(), print_preview_context_.IsModifiable())); |
| 1725 } | 1850 } |
| 1726 } | 1851 } |
| 1727 | 1852 |
| (...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2129 blink::WebConsoleMessage::LevelWarning, message)); | 2254 blink::WebConsoleMessage::LevelWarning, message)); |
| 2130 return false; | 2255 return false; |
| 2131 } | 2256 } |
| 2132 | 2257 |
| 2133 void PrintWebViewHelper::ScriptingThrottler::Reset() { | 2258 void PrintWebViewHelper::ScriptingThrottler::Reset() { |
| 2134 // Reset counter on successful print. | 2259 // Reset counter on successful print. |
| 2135 count_ = 0; | 2260 count_ = 0; |
| 2136 } | 2261 } |
| 2137 | 2262 |
| 2138 } // namespace printing | 2263 } // namespace printing |
| OLD | NEW |