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 "chrome/renderer/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/print_web_view_helper.h" |
| 6 | 6 |
| 7 #if !PRINTING_USE_EMF_METAFILE | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/process/process_handle.h" | |
| 12 #include "chrome/common/print_messages.h" | |
| 13 #include "content/public/renderer/render_thread.h" | |
| 14 #include "printing/metafile.h" | |
| 15 #include "printing/metafile_impl.h" | |
| 16 #include "printing/metafile_skia_wrapper.h" | |
| 17 #include "printing/page_size_margins.h" | |
| 18 #include "skia/ext/platform_device.h" | |
| 19 #include "skia/ext/vector_canvas.h" | |
| 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 21 | |
| 22 | |
|
Vitaly Buka (NO REVIEWS)
2014/04/24 22:47:58
Maybe it's worth additional file, like *helper_pdf
| |
| 23 namespace printing { | |
| 24 | |
| 25 using blink::WebFrame; | |
| 26 | |
| 27 bool PrintWebViewHelper::RenderPreviewPage( | |
| 28 int page_number, | |
| 29 const PrintMsg_Print_Params& print_params) { | |
| 30 PrintMsg_PrintPage_Params page_params; | |
| 31 page_params.params = print_params; | |
| 32 page_params.page_number = page_number; | |
| 33 scoped_ptr<Metafile> draft_metafile; | |
| 34 Metafile* initial_render_metafile = print_preview_context_.metafile(); | |
| 35 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { | |
| 36 draft_metafile.reset(new PreviewMetafile); | |
| 37 initial_render_metafile = draft_metafile.get(); | |
| 38 } | |
| 39 | |
| 40 base::TimeTicks begin_time = base::TimeTicks::Now(); | |
| 41 PrintPageInternal(page_params, | |
| 42 print_preview_context_.GetPrintCanvasSize(), | |
| 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.reset( | |
| 53 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | |
| 54 } | |
| 55 return PreviewPageRendered(page_number, draft_metafile.get()); | |
| 56 } | |
| 57 | |
| 58 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame, | |
| 59 int page_count, | |
| 60 const gfx::Size& canvas_size) { | |
| 61 NativeMetafile metafile; | |
| 62 if (!metafile.Init()) | |
| 63 return false; | |
| 64 | |
| 65 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | |
| 66 std::vector<int> printed_pages; | |
| 67 | |
| 68 if (params.pages.empty()) { | |
| 69 for (int i = 0; i < page_count; ++i) { | |
| 70 printed_pages.push_back(i); | |
| 71 } | |
| 72 } else { | |
| 73 // TODO(vitalybuka): redesign to make more code cross platform. | |
| 74 for (size_t i = 0; i < params.pages.size(); ++i) { | |
| 75 if (params.pages[i] >= 0 && params.pages[i] < page_count) { | |
| 76 printed_pages.push_back(params.pages[i]); | |
| 77 } | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 if (printed_pages.empty()) | |
| 82 return false; | |
| 83 | |
| 84 PrintMsg_PrintPage_Params page_params; | |
| 85 page_params.params = params.params; | |
| 86 for (size_t i = 0; i < printed_pages.size(); ++i) { | |
| 87 page_params.page_number = printed_pages[i]; | |
| 88 PrintPageInternal(page_params, canvas_size, frame, &metafile); | |
| 89 } | |
| 90 | |
| 91 // blink::printEnd() for PDF should be called before metafile is closed. | |
| 92 FinishFramePrinting(); | |
| 93 | |
| 94 metafile.FinishDocument(); | |
| 95 | |
| 96 // Get the size of the resulting metafile. | |
| 97 uint32 buf_size = metafile.GetDataSize(); | |
| 98 DCHECK_GT(buf_size, 0u); | |
| 99 | |
| 100 PrintHostMsg_DidPrintPage_Params printed_page_params; | |
| 101 printed_page_params.data_size = 0; | |
| 102 printed_page_params.document_cookie = params.params.document_cookie; | |
| 103 | |
| 104 { | |
| 105 base::SharedMemory shared_buf; | |
| 106 // Allocate a shared memory buffer to hold the generated metafile data. | |
| 107 if (!shared_buf.CreateAndMapAnonymous(buf_size)) { | |
| 108 NOTREACHED() << "Buffer allocation failed"; | |
| 109 return false; | |
| 110 } | |
| 111 | |
| 112 // Copy the bits into shared memory. | |
| 113 if (!metafile.GetData(shared_buf.memory(), buf_size)) { | |
| 114 NOTREACHED() << "GetData() failed"; | |
| 115 shared_buf.Unmap(); | |
| 116 return false; | |
| 117 } | |
| 118 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), | |
| 119 &printed_page_params.metafile_data_handle); | |
| 120 shared_buf.Unmap(); | |
| 121 | |
| 122 printed_page_params.data_size = buf_size; | |
| 123 Send(new PrintHostMsg_DuplicateSection( | |
| 124 routing_id(), | |
| 125 printed_page_params.metafile_data_handle, | |
| 126 &printed_page_params.metafile_data_handle)); | |
| 127 } | |
| 128 | |
| 129 for (size_t i = 0; i < printed_pages.size(); ++i) { | |
| 130 printed_page_params.page_number = printed_pages[i]; | |
| 131 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); | |
| 132 // Send the rest of the pages with an invalid metafile handle. | |
| 133 printed_page_params.metafile_data_handle = INVALID_HANDLE_VALUE; | |
| 134 } | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 void PrintWebViewHelper::PrintPageInternal( | |
| 139 const PrintMsg_PrintPage_Params& params, | |
| 140 const gfx::Size& canvas_size, | |
| 141 WebFrame* frame, | |
| 142 Metafile* metafile) { | |
| 143 PageSizeMargins page_layout_in_points; | |
| 144 double scale_factor = 1.0f; | |
| 145 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, | |
| 146 ignore_css_margins_, &scale_factor, | |
| 147 &page_layout_in_points); | |
| 148 gfx::Size page_size; | |
| 149 gfx::Rect content_area; | |
| 150 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, | |
| 151 &content_area); | |
| 152 gfx::Rect canvas_area = | |
| 153 params.params.display_header_footer ? gfx::Rect(page_size) : content_area; | |
| 154 | |
| 155 SkBaseDevice* device = metafile->StartPageForVectorCanvas(page_size, | |
| 156 canvas_area, | |
| 157 scale_factor); | |
| 158 if (!device) | |
| 159 return; | |
| 160 | |
| 161 // The printPage method take a reference to the canvas we pass down, so it | |
| 162 // can't be a stack object. | |
| 163 skia::RefPtr<skia::VectorCanvas> canvas = | |
| 164 skia::AdoptRef(new skia::VectorCanvas(device)); | |
| 165 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | |
| 166 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | |
| 167 | |
| 168 if (params.params.display_header_footer) { | |
| 169 // |page_number| is 0-based, so 1 is added. | |
| 170 // TODO(vitalybuka) : why does it work only with 1.25? | |
| 171 PrintHeaderAndFooter(canvas.get(), params.page_number + 1, | |
| 172 print_preview_context_.total_page_count(), | |
| 173 scale_factor / 1.25, | |
| 174 page_layout_in_points, *header_footer_info_, | |
| 175 params.params); | |
| 176 } | |
| 177 RenderPageContent(frame, params.page_number, canvas_area, content_area, | |
| 178 scale_factor, canvas.get()); | |
| 179 | |
| 180 // Done printing. Close the device context to retrieve the compiled metafile. | |
| 181 if (!metafile->FinishPage()) | |
| 182 NOTREACHED() << "metafile failed"; | |
| 183 } | |
| 184 | |
| 185 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | |
| 186 Metafile* metafile, base::SharedMemoryHandle* shared_mem_handle) { | |
| 187 uint32 buf_size = metafile->GetDataSize(); | |
| 188 base::SharedMemory shared_buf; | |
| 189 // Allocate a shared memory buffer to hold the generated metafile data. | |
| 190 if (!shared_buf.CreateAndMapAnonymous(buf_size)) { | |
| 191 NOTREACHED() << "Buffer allocation failed"; | |
| 192 return false; | |
| 193 } | |
| 194 | |
| 195 // Copy the bits into shared memory. | |
| 196 if (!metafile->GetData(shared_buf.memory(), buf_size)) { | |
| 197 NOTREACHED() << "GetData() failed"; | |
| 198 shared_buf.Unmap(); | |
| 199 return false; | |
| 200 } | |
| 201 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | |
| 202 shared_buf.Unmap(); | |
| 203 | |
| 204 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | |
| 205 shared_mem_handle)); | |
| 206 return true; | |
| 207 } | |
| 208 | |
| 209 } // namespace printing | |
| 210 | |
| 211 | |
| 212 #else // !PRINTING_USE_EMF_METAFILE | |
| 213 | |
| 7 #include "base/logging.h" | 214 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 215 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/metrics/histogram.h" | 216 #include "base/metrics/histogram.h" |
| 10 #include "base/process/process_handle.h" | 217 #include "base/process/process_handle.h" |
| 11 #include "base/win/scoped_gdi_object.h" | 218 #include "base/win/scoped_gdi_object.h" |
| 12 #include "base/win/scoped_hdc.h" | 219 #include "base/win/scoped_hdc.h" |
| 13 #include "base/win/scoped_select_object.h" | 220 #include "base/win/scoped_select_object.h" |
| 14 #include "chrome/common/print_messages.h" | 221 #include "chrome/common/print_messages.h" |
| 15 #include "printing/metafile.h" | 222 #include "printing/metafile.h" |
| 16 #include "printing/metafile_impl.h" | 223 #include "printing/metafile_impl.h" |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 } | 447 } |
| 241 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 448 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
| 242 shared_buf.Unmap(); | 449 shared_buf.Unmap(); |
| 243 | 450 |
| 244 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 451 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
| 245 shared_mem_handle)); | 452 shared_mem_handle)); |
| 246 return true; | 453 return true; |
| 247 } | 454 } |
| 248 | 455 |
| 249 } // namespace printing | 456 } // namespace printing |
| 457 | |
| 458 #endif // !PRINTING_USE_EMF_METAFILE | |
| OLD | NEW |