| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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> | |
| 9 | 8 |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
| 13 #include "components/printing/common/print_messages.h" | 11 #include "components/printing/common/print_messages.h" |
| 14 #include "content/public/renderer/render_thread.h" | |
| 15 #include "printing/metafile_skia_wrapper.h" | 12 #include "printing/metafile_skia_wrapper.h" |
| 16 #include "printing/page_size_margins.h" | |
| 17 #include "printing/pdf_metafile_skia.h" | |
| 18 #include "printing/units.h" | |
| 19 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 20 | 13 |
| 21 namespace printing { | 14 namespace printing { |
| 22 | 15 |
| 23 #if defined(ENABLE_PRINT_PREVIEW) | |
| 24 bool PrintWebViewHelper::RenderPreviewPage( | |
| 25 int page_number, | |
| 26 const PrintMsg_Print_Params& print_params) { | |
| 27 PrintMsg_PrintPage_Params page_params; | |
| 28 page_params.params = print_params; | |
| 29 page_params.page_number = page_number; | |
| 30 scoped_ptr<PdfMetafileSkia> draft_metafile; | |
| 31 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); | |
| 32 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { | |
| 33 draft_metafile.reset(new PdfMetafileSkia); | |
| 34 initial_render_metafile = draft_metafile.get(); | |
| 35 } | |
| 36 | |
| 37 base::TimeTicks begin_time = base::TimeTicks::Now(); | |
| 38 PrintPageInternal(page_params, | |
| 39 print_preview_context_.prepared_frame(), | |
| 40 initial_render_metafile, | |
| 41 NULL, | |
| 42 NULL); | |
| 43 print_preview_context_.RenderedPreviewPage( | |
| 44 base::TimeTicks::Now() - begin_time); | |
| 45 if (draft_metafile.get()) { | |
| 46 draft_metafile->FinishDocument(); | |
| 47 } else if (print_preview_context_.IsModifiable() && | |
| 48 print_preview_context_.generate_draft_pages()) { | |
| 49 DCHECK(!draft_metafile.get()); | |
| 50 draft_metafile = | |
| 51 print_preview_context_.metafile()->GetMetafileForCurrentPage(); | |
| 52 } | |
| 53 return PreviewPageRendered(page_number, draft_metafile.get()); | |
| 54 } | |
| 55 #endif // defined(ENABLE_PRINT_PREVIEW) | |
| 56 | |
| 57 #if defined(ENABLE_BASIC_PRINTING) | 16 #if defined(ENABLE_BASIC_PRINTING) |
| 58 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame, | 17 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame, |
| 59 int page_count) { | 18 int page_count) { |
| 60 PdfMetafileSkia metafile; | 19 PdfMetafileSkia metafile; |
| 61 if (!metafile.Init()) | 20 if (!metafile.Init()) |
| 62 return false; | 21 return false; |
| 63 | 22 |
| 64 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 23 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
| 65 std::vector<int> printed_pages = GetPrintedPages(params, page_count); | 24 std::vector<int> printed_pages = GetPrintedPages(params, page_count); |
| 66 if (printed_pages.empty()) | 25 if (printed_pages.empty()) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 printed_page_params.page_size = page_size_in_dpi[i]; | 60 printed_page_params.page_size = page_size_in_dpi[i]; |
| 102 printed_page_params.content_area = content_area_in_dpi[i]; | 61 printed_page_params.content_area = content_area_in_dpi[i]; |
| 103 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); | 62 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); |
| 104 // Send the rest of the pages with an invalid metafile handle. | 63 // Send the rest of the pages with an invalid metafile handle. |
| 105 printed_page_params.metafile_data_handle = base::SharedMemoryHandle(); | 64 printed_page_params.metafile_data_handle = base::SharedMemoryHandle(); |
| 106 } | 65 } |
| 107 return true; | 66 return true; |
| 108 } | 67 } |
| 109 #endif // defined(ENABLE_BASIC_PRINTING) | 68 #endif // defined(ENABLE_BASIC_PRINTING) |
| 110 | 69 |
| 111 void PrintWebViewHelper::PrintPageInternal( | |
| 112 const PrintMsg_PrintPage_Params& params, | |
| 113 blink::WebFrame* frame, | |
| 114 PdfMetafileSkia* metafile, | |
| 115 gfx::Size* page_size_in_dpi, | |
| 116 gfx::Rect* content_area_in_dpi) { | |
| 117 PageSizeMargins page_layout_in_points; | |
| 118 double css_scale_factor = 1.0f; | |
| 119 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, | |
| 120 ignore_css_margins_, &css_scale_factor, | |
| 121 &page_layout_in_points); | |
| 122 gfx::Size page_size; | |
| 123 gfx::Rect content_area; | |
| 124 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, | |
| 125 &content_area); | |
| 126 int dpi = static_cast<int>(params.params.dpi); | |
| 127 // Calculate the actual page size and content area in dpi. | |
| 128 if (page_size_in_dpi) { | |
| 129 *page_size_in_dpi = | |
| 130 gfx::Size(static_cast<int>(ConvertUnitDouble(page_size.width(), | |
| 131 kPointsPerInch, dpi)), | |
| 132 static_cast<int>(ConvertUnitDouble(page_size.height(), | |
| 133 kPointsPerInch, dpi))); | |
| 134 } | |
| 135 | |
| 136 if (content_area_in_dpi) { | |
| 137 // Output PDF matches paper size and should be printer edge to edge. | |
| 138 *content_area_in_dpi = | |
| 139 gfx::Rect(0, 0, page_size_in_dpi->width(), page_size_in_dpi->height()); | |
| 140 } | |
| 141 | |
| 142 gfx::Rect canvas_area = | |
| 143 params.params.display_header_footer ? gfx::Rect(page_size) : content_area; | |
| 144 | |
| 145 float webkit_page_shrink_factor = | |
| 146 frame->getPrintPageShrink(params.page_number); | |
| 147 float scale_factor = css_scale_factor * webkit_page_shrink_factor; | |
| 148 | |
| 149 skia::PlatformCanvas* canvas = | |
| 150 metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor); | |
| 151 if (!canvas) | |
| 152 return; | |
| 153 | |
| 154 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | |
| 155 | |
| 156 #if defined(ENABLE_PRINT_PREVIEW) | |
| 157 if (params.params.display_header_footer) { | |
| 158 // |page_number| is 0-based, so 1 is added. | |
| 159 PrintHeaderAndFooter(canvas, params.page_number + 1, | |
| 160 print_preview_context_.total_page_count(), *frame, | |
| 161 scale_factor, page_layout_in_points, params.params); | |
| 162 } | |
| 163 #endif // defined(ENABLE_PRINT_PREVIEW) | |
| 164 | |
| 165 float webkit_scale_factor = | |
| 166 RenderPageContent(frame, params.page_number, canvas_area, content_area, | |
| 167 scale_factor, canvas); | |
| 168 DCHECK_GT(webkit_scale_factor, 0.0f); | |
| 169 // Done printing. Close the canvas to retrieve the compiled metafile. | |
| 170 if (!metafile->FinishPage()) | |
| 171 NOTREACHED() << "metafile failed"; | |
| 172 } | |
| 173 | |
| 174 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | |
| 175 const PdfMetafileSkia& metafile, | |
| 176 base::SharedMemoryHandle* shared_mem_handle) { | |
| 177 uint32_t buf_size = metafile.GetDataSize(); | |
| 178 if (buf_size == 0) | |
| 179 return false; | |
| 180 | |
| 181 base::SharedMemory shared_buf; | |
| 182 // Allocate a shared memory buffer to hold the generated metafile data. | |
| 183 if (!shared_buf.CreateAndMapAnonymous(buf_size)) | |
| 184 return false; | |
| 185 | |
| 186 // Copy the bits into shared memory. | |
| 187 if (!metafile.GetData(shared_buf.memory(), buf_size)) | |
| 188 return false; | |
| 189 | |
| 190 if (!shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), | |
| 191 shared_mem_handle)) { | |
| 192 return false; | |
| 193 } | |
| 194 | |
| 195 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | |
| 196 shared_mem_handle)); | |
| 197 return true; | |
| 198 } | |
| 199 | |
| 200 } // namespace printing | 70 } // namespace printing |
| OLD | NEW |