Chromium Code Reviews| 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 1172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1214 DCHECK(print_preview_context_.IsModifiable() || | 1215 DCHECK(print_preview_context_.IsModifiable() || |
| 1215 print_preview_context_.IsFinalPageRendered()); | 1216 print_preview_context_.IsFinalPageRendered()); |
| 1216 if (!FinalizePrintReadyDocument()) | 1217 if (!FinalizePrintReadyDocument()) |
| 1217 return false; | 1218 return false; |
| 1218 } | 1219 } |
| 1219 } | 1220 } |
| 1220 print_preview_context_.Finished(); | 1221 print_preview_context_.Finished(); |
| 1221 return true; | 1222 return true; |
| 1222 } | 1223 } |
| 1223 | 1224 |
| 1225 #if !defined(OS_MACOSX) && defined(ENABLE_PRINT_PREVIEW) | |
| 1226 bool PrintWebViewHelper::RenderPreviewPage( | |
| 1227 int page_number, | |
| 1228 const PrintMsg_Print_Params& print_params) { | |
| 1229 PrintMsg_PrintPage_Params page_params; | |
| 1230 page_params.params = print_params; | |
| 1231 page_params.page_number = page_number; | |
| 1232 scoped_ptr<PdfMetafileSkia> draft_metafile; | |
|
Vitaly Buka corp
2016/01/04 21:09:32
I'd put empty line to separate page_params and dra
| |
| 1233 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); | |
| 1234 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { | |
| 1235 draft_metafile.reset(new PdfMetafileSkia); | |
| 1236 initial_render_metafile = draft_metafile.get(); | |
| 1237 } | |
| 1238 | |
| 1239 base::TimeTicks begin_time = base::TimeTicks::Now(); | |
| 1240 PrintPageInternal(page_params, print_preview_context_.prepared_frame(), | |
| 1241 initial_render_metafile, nullptr, nullptr); | |
| 1242 print_preview_context_.RenderedPreviewPage( | |
| 1243 base::TimeTicks::Now() - begin_time); | |
| 1244 if (draft_metafile.get()) { | |
|
Vitaly Buka corp
2016/01/04 21:09:32
if (draft_metafile) {
| |
| 1245 draft_metafile->FinishDocument(); | |
| 1246 } else if (print_preview_context_.IsModifiable() && | |
| 1247 print_preview_context_.generate_draft_pages()) { | |
| 1248 DCHECK(!draft_metafile.get()); | |
|
Vitaly Buka corp
2016/01/04 21:09:32
seems DCHECK is not very useful here
| |
| 1249 draft_metafile = | |
| 1250 print_preview_context_.metafile()->GetMetafileForCurrentPage(); | |
| 1251 } | |
| 1252 return PreviewPageRendered(page_number, draft_metafile.get()); | |
| 1253 } | |
| 1254 #endif // !defined(OS_MACOSX) && defined(ENABLE_PRINT_PREVIEW) | |
| 1255 | |
| 1224 bool PrintWebViewHelper::FinalizePrintReadyDocument() { | 1256 bool PrintWebViewHelper::FinalizePrintReadyDocument() { |
| 1225 DCHECK(!is_print_ready_metafile_sent_); | 1257 DCHECK(!is_print_ready_metafile_sent_); |
| 1226 print_preview_context_.FinalizePrintReadyDocument(); | 1258 print_preview_context_.FinalizePrintReadyDocument(); |
| 1227 | 1259 |
| 1228 PdfMetafileSkia* metafile = print_preview_context_.metafile(); | 1260 PdfMetafileSkia* metafile = print_preview_context_.metafile(); |
| 1229 PrintHostMsg_DidPreviewDocument_Params preview_params; | 1261 PrintHostMsg_DidPreviewDocument_Params preview_params; |
| 1230 | 1262 |
| 1231 // Ask the browser to create the shared memory for us. | 1263 // Ask the browser to create the shared memory for us. |
| 1232 if (!CopyMetafileDataToSharedMem(*metafile, | 1264 if (!CopyMetafileDataToSharedMem(*metafile, |
| 1233 &(preview_params.metafile_data_handle))) { | 1265 &(preview_params.metafile_data_handle))) { |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1680 DCHECK(!print_pages_params_->params.selection_only || | 1712 DCHECK(!print_pages_params_->params.selection_only || |
| 1681 print_pages_params_->pages.empty()); | 1713 print_pages_params_->pages.empty()); |
| 1682 prep_frame_view_->CopySelectionIfNeeded( | 1714 prep_frame_view_->CopySelectionIfNeeded( |
| 1683 render_view()->GetWebkitPreferences(), | 1715 render_view()->GetWebkitPreferences(), |
| 1684 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, | 1716 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, |
| 1685 base::Unretained(this))); | 1717 base::Unretained(this))); |
| 1686 return true; | 1718 return true; |
| 1687 } | 1719 } |
| 1688 #endif // defined(ENABLE_BASIC_PRINTING) | 1720 #endif // defined(ENABLE_BASIC_PRINTING) |
| 1689 | 1721 |
| 1690 #if defined(OS_POSIX) | 1722 #if !defined(OS_MACOSX) |
| 1723 void PrintWebViewHelper::PrintPageInternal( | |
| 1724 const PrintMsg_PrintPage_Params& params, | |
| 1725 blink::WebFrame* frame, | |
| 1726 PdfMetafileSkia* metafile, | |
| 1727 gfx::Size* page_size_in_dpi, | |
| 1728 gfx::Rect* content_area_in_dpi) { | |
| 1729 PageSizeMargins page_layout_in_points; | |
| 1730 double css_scale_factor = 1.0f; | |
| 1731 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, | |
| 1732 ignore_css_margins_, &css_scale_factor, | |
| 1733 &page_layout_in_points); | |
| 1734 gfx::Size page_size; | |
| 1735 gfx::Rect content_area; | |
| 1736 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, | |
| 1737 &content_area); | |
| 1738 int dpi = static_cast<int>(params.params.dpi); | |
| 1739 // Calculate the actual page size and content area in dpi. | |
| 1740 if (page_size_in_dpi) { | |
| 1741 *page_size_in_dpi = | |
| 1742 gfx::Size(static_cast<int>(ConvertUnitDouble(page_size.width(), | |
| 1743 kPointsPerInch, dpi)), | |
| 1744 static_cast<int>(ConvertUnitDouble(page_size.height(), | |
| 1745 kPointsPerInch, dpi))); | |
| 1746 } | |
| 1747 | |
| 1748 if (content_area_in_dpi) { | |
| 1749 // Output PDF matches paper size and should be printer edge to edge. | |
| 1750 *content_area_in_dpi = | |
| 1751 gfx::Rect(0, 0, page_size_in_dpi->width(), page_size_in_dpi->height()); | |
| 1752 } | |
| 1753 | |
| 1754 gfx::Rect canvas_area = | |
| 1755 params.params.display_header_footer ? gfx::Rect(page_size) : content_area; | |
| 1756 | |
| 1757 #if defined(OS_WIN) || defined(ENABLE_PRINT_PREVIEW) | |
| 1758 float webkit_page_shrink_factor = | |
| 1759 frame->getPrintPageShrink(params.page_number); | |
| 1760 float scale_factor = css_scale_factor * webkit_page_shrink_factor; | |
| 1761 #endif | |
| 1762 // TODO(thestig) GetVectorCanvasForNewPage() and RenderPageContent() take a | |
| 1763 // different scale factor vs Windows. Figure out why and combine the two. | |
| 1764 #if defined(OS_WIN) | |
| 1765 float platform_scale_factor = scale_factor; | |
| 1766 #else | |
| 1767 float platform_scale_factor = css_scale_factor; | |
| 1768 #endif // defined(OS_WIN) | |
| 1769 | |
| 1770 skia::PlatformCanvas* canvas = metafile->GetVectorCanvasForNewPage( | |
| 1771 page_size, canvas_area, platform_scale_factor); | |
| 1772 if (!canvas) | |
| 1773 return; | |
| 1774 | |
| 1775 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | |
| 1776 | |
| 1777 #if defined(ENABLE_PRINT_PREVIEW) | |
| 1778 if (params.params.display_header_footer) { | |
| 1779 // |page_number| is 0-based, so 1 is added. | |
| 1780 PrintHeaderAndFooter(canvas, params.page_number + 1, | |
| 1781 print_preview_context_.total_page_count(), *frame, | |
| 1782 scale_factor, page_layout_in_points, params.params); | |
| 1783 } | |
| 1784 #endif // defined(ENABLE_PRINT_PREVIEW) | |
| 1785 | |
| 1786 float webkit_scale_factor = | |
| 1787 RenderPageContent(frame, params.page_number, canvas_area, content_area, | |
| 1788 platform_scale_factor, canvas); | |
| 1789 DCHECK_GT(webkit_scale_factor, 0.0f); | |
| 1790 | |
| 1791 // Done printing. Close the canvas to retrieve the compiled metafile. | |
| 1792 if (!metafile->FinishPage()) | |
| 1793 NOTREACHED() << "metafile failed"; | |
| 1794 } | |
| 1795 #endif // !defined(OS_MACOSX) | |
| 1796 | |
| 1691 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | 1797 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( |
| 1692 const PdfMetafileSkia& metafile, | 1798 const PdfMetafileSkia& metafile, |
| 1693 base::SharedMemoryHandle* shared_mem_handle) { | 1799 base::SharedMemoryHandle* shared_mem_handle) { |
| 1694 uint32_t buf_size = metafile.GetDataSize(); | 1800 uint32_t buf_size = metafile.GetDataSize(); |
| 1695 if (buf_size == 0) | 1801 if (buf_size == 0) |
| 1696 return false; | 1802 return false; |
| 1697 | 1803 |
| 1804 #if defined(OS_WIN) | |
| 1805 base::SharedMemory shared_buf; | |
| 1806 // Allocate a shared memory buffer to hold the generated metafile data. | |
| 1807 if (!shared_buf.CreateAndMapAnonymous(buf_size)) | |
| 1808 return false; | |
| 1809 | |
| 1810 // Copy the bits into shared memory. | |
| 1811 if (!metafile.GetData(shared_buf.memory(), buf_size)) | |
| 1812 return false; | |
| 1813 | |
| 1814 if (!shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), | |
| 1815 shared_mem_handle)) { | |
| 1816 return false; | |
| 1817 } | |
| 1818 | |
| 1819 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | |
| 1820 shared_mem_handle)); | |
| 1821 return true; | |
| 1822 #else | |
| 1698 scoped_ptr<base::SharedMemory> shared_buf( | 1823 scoped_ptr<base::SharedMemory> shared_buf( |
| 1699 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(buf_size)); | 1824 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(buf_size)); |
| 1700 if (!shared_buf) | 1825 if (!shared_buf) |
| 1701 return false; | 1826 return false; |
| 1702 | 1827 |
| 1703 if (!shared_buf->Map(buf_size)) | 1828 if (!shared_buf->Map(buf_size)) |
| 1704 return false; | 1829 return false; |
| 1705 | 1830 |
| 1706 if (!metafile.GetData(shared_buf->memory(), buf_size)) | 1831 if (!metafile.GetData(shared_buf->memory(), buf_size)) |
| 1707 return false; | 1832 return false; |
| 1708 | 1833 |
| 1709 return shared_buf->GiveToProcess(base::GetCurrentProcessHandle(), | 1834 return shared_buf->GiveToProcess(base::GetCurrentProcessHandle(), |
| 1710 shared_mem_handle); | 1835 shared_mem_handle); |
| 1836 #endif // defined(OS_WIN) | |
| 1711 } | 1837 } |
| 1712 #endif // defined(OS_POSIX) | |
| 1713 | 1838 |
| 1714 #if defined(ENABLE_PRINT_PREVIEW) | 1839 #if defined(ENABLE_PRINT_PREVIEW) |
| 1715 void PrintWebViewHelper::ShowScriptedPrintPreview() { | 1840 void PrintWebViewHelper::ShowScriptedPrintPreview() { |
| 1716 if (is_scripted_preview_delayed_) { | 1841 if (is_scripted_preview_delayed_) { |
| 1717 is_scripted_preview_delayed_ = false; | 1842 is_scripted_preview_delayed_ = false; |
| 1718 Send(new PrintHostMsg_ShowScriptedPrintPreview( | 1843 Send(new PrintHostMsg_ShowScriptedPrintPreview( |
| 1719 routing_id(), print_preview_context_.IsModifiable())); | 1844 routing_id(), print_preview_context_.IsModifiable())); |
| 1720 } | 1845 } |
| 1721 } | 1846 } |
| 1722 | 1847 |
| (...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2124 blink::WebConsoleMessage::LevelWarning, message)); | 2249 blink::WebConsoleMessage::LevelWarning, message)); |
| 2125 return false; | 2250 return false; |
| 2126 } | 2251 } |
| 2127 | 2252 |
| 2128 void PrintWebViewHelper::ScriptingThrottler::Reset() { | 2253 void PrintWebViewHelper::ScriptingThrottler::Reset() { |
| 2129 // Reset counter on successful print. | 2254 // Reset counter on successful print. |
| 2130 count_ = 0; | 2255 count_ = 0; |
| 2131 } | 2256 } |
| 2132 | 2257 |
| 2133 } // namespace printing | 2258 } // namespace printing |
| OLD | NEW |