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 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 12 #include "components/printing/common/print_messages.h" | 11 #include "components/printing/common/print_messages.h" |
| 13 #include "content/public/renderer/render_thread.h" | |
| 14 #include "printing/metafile_skia_wrapper.h" | 12 #include "printing/metafile_skia_wrapper.h" |
| 15 #include "printing/page_size_margins.h" | |
| 16 #include "printing/pdf_metafile_skia.h" | |
| 17 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 18 | 13 |
| 19 #if defined(OS_ANDROID) | 14 #if defined(OS_ANDROID) |
| 20 #include "base/file_descriptor_posix.h" | 15 #include "base/file_descriptor_posix.h" |
| 21 #else | 16 #else |
| 22 #include "base/process/process_handle.h" | 17 #include "base/process/process_handle.h" |
| 23 #endif // defined(OS_ANDROID) | 18 #endif // defined(OS_ANDROID) |
| 24 | 19 |
| 25 namespace printing { | 20 namespace printing { |
| 26 | 21 |
| 27 #if defined(ENABLE_PRINT_PREVIEW) | |
| 28 bool PrintWebViewHelper::RenderPreviewPage( | |
| 29 int page_number, | |
| 30 const PrintMsg_Print_Params& print_params) { | |
| 31 PrintMsg_PrintPage_Params page_params; | |
| 32 page_params.params = print_params; | |
| 33 page_params.page_number = page_number; | |
| 34 scoped_ptr<PdfMetafileSkia> draft_metafile; | |
| 35 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); | |
| 36 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { | |
| 37 draft_metafile.reset(new PdfMetafileSkia); | |
| 38 initial_render_metafile = draft_metafile.get(); | |
| 39 } | |
| 40 | |
| 41 base::TimeTicks begin_time = base::TimeTicks::Now(); | |
| 42 PrintPageInternal(page_params, | |
| 43 print_preview_context_.prepared_frame(), | |
| 44 initial_render_metafile); | |
| 45 print_preview_context_.RenderedPreviewPage( | |
| 46 base::TimeTicks::Now() - begin_time); | |
| 47 if (draft_metafile.get()) { | |
| 48 draft_metafile->FinishDocument(); | |
| 49 } else if (print_preview_context_.IsModifiable() && | |
| 50 print_preview_context_.generate_draft_pages()) { | |
| 51 DCHECK(!draft_metafile.get()); | |
| 52 draft_metafile = | |
| 53 print_preview_context_.metafile()->GetMetafileForCurrentPage(); | |
| 54 } | |
| 55 return PreviewPageRendered(page_number, draft_metafile.get()); | |
| 56 } | |
| 57 #endif | |
| 58 | |
| 59 #if defined(ENABLE_BASIC_PRINTING) | 22 #if defined(ENABLE_BASIC_PRINTING) |
| 60 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame, | 23 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame, |
| 61 int page_count) { | 24 int page_count) { |
| 62 PdfMetafileSkia metafile; | 25 PdfMetafileSkia metafile; |
| 63 if (!metafile.Init()) | 26 if (!metafile.Init()) |
| 64 return false; | 27 return false; |
| 65 | 28 |
| 66 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 29 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
| 67 std::vector<int> printed_pages = GetPrintedPages(params, page_count); | 30 std::vector<int> printed_pages = GetPrintedPages(params, page_count); |
| 68 if (printed_pages.empty()) | 31 if (printed_pages.empty()) |
| 69 return false; | 32 return false; |
| 70 | 33 |
| 71 PrintMsg_PrintPage_Params page_params; | 34 PrintMsg_PrintPage_Params page_params; |
| 72 page_params.params = params.params; | 35 page_params.params = params.params; |
| 73 for (int page_number : printed_pages) { | 36 for (int page_number : printed_pages) { |
| 74 page_params.page_number = page_number; | 37 page_params.page_number = page_number; |
| 75 PrintPageInternal(page_params, frame, &metafile); | 38 PrintPageInternal(page_params, frame, &metafile, nullptr, nullptr); |
| 76 } | 39 } |
| 77 | 40 |
| 78 // blink::printEnd() for PDF should be called before metafile is closed. | 41 // blink::printEnd() for PDF should be called before metafile is closed. |
| 79 FinishFramePrinting(); | 42 FinishFramePrinting(); |
| 80 | 43 |
| 81 metafile.FinishDocument(); | 44 metafile.FinishDocument(); |
| 82 | 45 |
| 83 #if defined(OS_ANDROID) | 46 #if defined(OS_ANDROID) |
| 84 int sequence_number = -1; | 47 int sequence_number = -1; |
| 85 base::FileDescriptor fd; | 48 base::FileDescriptor fd; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 110 printed_page_params.page_number = printed_pages[i]; | 73 printed_page_params.page_number = printed_pages[i]; |
| 111 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); | 74 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); |
| 112 // Send the rest of the pages with an invalid metafile handle. | 75 // Send the rest of the pages with an invalid metafile handle. |
| 113 printed_page_params.metafile_data_handle.fd = -1; | 76 printed_page_params.metafile_data_handle.fd = -1; |
| 114 } | 77 } |
| 115 return true; | 78 return true; |
| 116 #endif // defined(OS_ANDROID) | 79 #endif // defined(OS_ANDROID) |
| 117 } | 80 } |
| 118 #endif // defined(ENABLE_BASIC_PRINTING) | 81 #endif // defined(ENABLE_BASIC_PRINTING) |
| 119 | 82 |
| 120 void PrintWebViewHelper::PrintPageInternal( | |
| 121 const PrintMsg_PrintPage_Params& params, | |
| 122 blink::WebFrame* frame, | |
| 123 PdfMetafileSkia* metafile) { | |
| 124 PageSizeMargins page_layout_in_points; | |
| 125 double scale_factor = 1.0f; | |
| 126 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, | |
| 127 ignore_css_margins_, &scale_factor, | |
| 128 &page_layout_in_points); | |
| 129 gfx::Size page_size; | |
| 130 gfx::Rect content_area; | |
| 131 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, | |
| 132 &content_area); | |
| 133 gfx::Rect canvas_area = | |
| 134 params.params.display_header_footer ? gfx::Rect(page_size) : content_area; | |
| 135 | |
| 136 skia::PlatformCanvas* canvas = | |
| 137 metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor); | |
| 138 if (!canvas) | |
| 139 return; | |
| 140 | |
| 141 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | |
| 142 | |
| 143 #if defined(ENABLE_PRINT_PREVIEW) | |
| 144 if (params.params.display_header_footer) { | |
| 145 // |page_number| is 0-based, so 1 is added. | |
| 146 // TODO(vitalybuka) : why does it work only with 1.25? | |
|
Lei Zhang
2015/12/30 01:53:48
Because 1.25 is the WK scaling factor.
Vitaly Buka corp
2016/01/04 21:09:32
Quite possible, but seems to simple to be true :-)
| |
| 147 PrintHeaderAndFooter(canvas, params.page_number + 1, | |
| 148 print_preview_context_.total_page_count(), *frame, | |
| 149 scale_factor / 1.25, page_layout_in_points, | |
| 150 params.params); | |
| 151 } | |
| 152 #endif // defined(ENABLE_PRINT_PREVIEW) | |
| 153 | |
| 154 RenderPageContent(frame, params.page_number, canvas_area, content_area, | |
| 155 scale_factor, canvas); | |
| 156 | |
| 157 // Done printing. Close the canvas to retrieve the compiled metafile. | |
| 158 if (!metafile->FinishPage()) | |
| 159 NOTREACHED() << "metafile failed"; | |
| 160 } | |
| 161 | |
| 162 } // namespace printing | 83 } // namespace printing |
| OLD | NEW |