| 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 "chrome/renderer/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/print_web_view_helper.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/process/process_handle.h" | 9 #include "base/process/process_handle.h" |
| 10 #include "chrome/common/print_messages.h" | 10 #include "chrome/common/print_messages.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 page_params.params = print_params; | 30 page_params.params = print_params; |
| 31 page_params.page_number = page_number; | 31 page_params.page_number = page_number; |
| 32 scoped_ptr<Metafile> draft_metafile; | 32 scoped_ptr<Metafile> draft_metafile; |
| 33 Metafile* initial_render_metafile = print_preview_context_.metafile(); | 33 Metafile* initial_render_metafile = print_preview_context_.metafile(); |
| 34 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { | 34 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { |
| 35 draft_metafile.reset(new PreviewMetafile); | 35 draft_metafile.reset(new PreviewMetafile); |
| 36 initial_render_metafile = draft_metafile.get(); | 36 initial_render_metafile = draft_metafile.get(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 base::TimeTicks begin_time = base::TimeTicks::Now(); | 39 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 40 double actual_shrink = | |
| 41 static_cast<float>(print_params.desired_dpi / print_params.dpi); | |
| 42 PrintPageInternal(page_params, | 40 PrintPageInternal(page_params, |
| 43 print_preview_context_.GetPrintCanvasSize(), | 41 print_preview_context_.GetPrintCanvasSize(), |
| 44 print_preview_context_.prepared_frame(), | 42 print_preview_context_.prepared_frame(), |
| 45 initial_render_metafile, | 43 initial_render_metafile, |
| 46 true, | 44 true, |
| 47 &actual_shrink, | |
| 48 NULL, | 45 NULL, |
| 49 NULL); | 46 NULL); |
| 50 print_preview_context_.RenderedPreviewPage( | 47 print_preview_context_.RenderedPreviewPage( |
| 51 base::TimeTicks::Now() - begin_time); | 48 base::TimeTicks::Now() - begin_time); |
| 52 if (draft_metafile.get()) { | 49 if (draft_metafile.get()) { |
| 53 draft_metafile->FinishDocument(); | 50 draft_metafile->FinishDocument(); |
| 54 } else if (print_preview_context_.IsModifiable() && | 51 } else if (print_preview_context_.IsModifiable() && |
| 55 print_preview_context_.generate_draft_pages()) { | 52 print_preview_context_.generate_draft_pages()) { |
| 56 DCHECK(!draft_metafile.get()); | 53 DCHECK(!draft_metafile.get()); |
| 57 draft_metafile.reset( | 54 draft_metafile.reset( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 95 |
| 99 PrintMsg_PrintPage_Params page_params; | 96 PrintMsg_PrintPage_Params page_params; |
| 100 page_params.params = params.params; | 97 page_params.params = params.params; |
| 101 for (size_t i = 0; i < printed_pages.size(); ++i) { | 98 for (size_t i = 0; i < printed_pages.size(); ++i) { |
| 102 page_params.page_number = printed_pages[i]; | 99 page_params.page_number = printed_pages[i]; |
| 103 PrintPageInternal(page_params, | 100 PrintPageInternal(page_params, |
| 104 canvas_size, | 101 canvas_size, |
| 105 frame, | 102 frame, |
| 106 &metafile, | 103 &metafile, |
| 107 false, | 104 false, |
| 108 &shrink[i], | |
| 109 &page_size_in_dpi[i], | 105 &page_size_in_dpi[i], |
| 110 &content_area_in_dpi[i]); | 106 &content_area_in_dpi[i]); |
| 111 } | 107 } |
| 112 | 108 |
| 113 // blink::printEnd() for PDF should be called before metafile is closed. | 109 // blink::printEnd() for PDF should be called before metafile is closed. |
| 114 FinishFramePrinting(); | 110 FinishFramePrinting(); |
| 115 | 111 |
| 116 metafile.FinishDocument(); | 112 metafile.FinishDocument(); |
| 117 | 113 |
| 118 // Get the size of the resulting metafile. | 114 // Get the size of the resulting metafile. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 145 | 141 |
| 146 printed_page_params.data_size = buf_size; | 142 printed_page_params.data_size = buf_size; |
| 147 Send(new PrintHostMsg_DuplicateSection( | 143 Send(new PrintHostMsg_DuplicateSection( |
| 148 routing_id(), | 144 routing_id(), |
| 149 printed_page_params.metafile_data_handle, | 145 printed_page_params.metafile_data_handle, |
| 150 &printed_page_params.metafile_data_handle)); | 146 &printed_page_params.metafile_data_handle)); |
| 151 } | 147 } |
| 152 | 148 |
| 153 for (size_t i = 0; i < printed_pages.size(); ++i) { | 149 for (size_t i = 0; i < printed_pages.size(); ++i) { |
| 154 printed_page_params.page_number = printed_pages[i]; | 150 printed_page_params.page_number = printed_pages[i]; |
| 155 printed_page_params.actual_shrink = shrink[i]; | |
| 156 printed_page_params.page_size = page_size_in_dpi[i]; | 151 printed_page_params.page_size = page_size_in_dpi[i]; |
| 157 printed_page_params.content_area = content_area_in_dpi[i]; | 152 printed_page_params.content_area = content_area_in_dpi[i]; |
| 158 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); | 153 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); |
| 159 printed_page_params.metafile_data_handle = INVALID_HANDLE_VALUE; | 154 printed_page_params.metafile_data_handle = INVALID_HANDLE_VALUE; |
| 160 } | 155 } |
| 161 return true; | 156 return true; |
| 162 } | 157 } |
| 163 | 158 |
| 164 void PrintWebViewHelper::PrintPageInternal( | 159 void PrintWebViewHelper::PrintPageInternal( |
| 165 const PrintMsg_PrintPage_Params& params, | 160 const PrintMsg_PrintPage_Params& params, |
| 166 const gfx::Size& canvas_size, | 161 const gfx::Size& canvas_size, |
| 167 WebFrame* frame, | 162 WebFrame* frame, |
| 168 Metafile* metafile, | 163 Metafile* metafile, |
| 169 bool is_preview, | 164 bool is_preview, |
| 170 double* actual_shrink, | |
| 171 gfx::Size* page_size_in_dpi, | 165 gfx::Size* page_size_in_dpi, |
| 172 gfx::Rect* content_area_in_dpi) { | 166 gfx::Rect* content_area_in_dpi) { |
| 173 PageSizeMargins page_layout_in_points; | 167 PageSizeMargins page_layout_in_points; |
| 174 double css_scale_factor = 1.0f; | 168 double css_scale_factor = 1.0f; |
| 175 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, | 169 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, |
| 176 ignore_css_margins_, &css_scale_factor, | 170 ignore_css_margins_, &css_scale_factor, |
| 177 &page_layout_in_points); | 171 &page_layout_in_points); |
| 178 gfx::Size page_size; | 172 gfx::Size page_size; |
| 179 gfx::Rect content_area; | 173 gfx::Rect content_area; |
| 180 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, | 174 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 | 218 |
| 225 // The printPage method take a reference to the canvas we pass down, so it | 219 // The printPage method take a reference to the canvas we pass down, so it |
| 226 // can't be a stack object. | 220 // can't be a stack object. |
| 227 skia::RefPtr<skia::VectorCanvas> canvas = | 221 skia::RefPtr<skia::VectorCanvas> canvas = |
| 228 skia::AdoptRef(new skia::VectorCanvas(device)); | 222 skia::AdoptRef(new skia::VectorCanvas(device)); |
| 229 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | 223 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
| 230 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | 224 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); |
| 231 | 225 |
| 232 if (params.params.display_header_footer) { | 226 if (params.params.display_header_footer) { |
| 233 // |page_number| is 0-based, so 1 is added. | 227 // |page_number| is 0-based, so 1 is added. |
| 234 PrintHeaderAndFooter(canvas.get(), params.page_number + 1, | 228 PrintHeaderAndFooter(canvas.get(), |
| 235 print_preview_context_.total_page_count(), | 229 params.page_number + 1, |
| 236 scale_factor, | 230 print_preview_context_.total_page_count(), |
| 237 page_layout_in_points, *header_footer_info_, | 231 scale_factor, |
| 238 params.params); | 232 page_layout_in_points, |
| 233 *header_footer_info_, |
| 234 params.params); |
| 239 } | 235 } |
| 240 | 236 |
| 241 float webkit_scale_factor = RenderPageContent(frame, | 237 float webkit_scale_factor = RenderPageContent(frame, |
| 242 params.page_number, | 238 params.page_number, |
| 243 canvas_area, | 239 canvas_area, |
| 244 content_area, | 240 content_area, |
| 245 scale_factor, | 241 scale_factor, |
| 246 canvas.get()); | 242 canvas.get()); |
| 247 | 243 DCHECK_GT(webkit_scale_factor, 0.0f); |
| 248 if (*actual_shrink <= 0 || webkit_scale_factor <= 0) { | |
| 249 NOTREACHED() << "Printing page " << params.page_number << " failed."; | |
| 250 } else { | |
| 251 // While rendering certain plugins (PDF) to metafile, we might need to | |
| 252 // set custom scale factor. Update |actual_shrink| with custom scale | |
| 253 // if it is set on canvas. | |
| 254 // TODO(gene): We should revisit this solution for the next versions. | |
| 255 // Consider creating metafile of the right size (or resizable) | |
| 256 // https://code.google.com/p/chromium/issues/detail?id=126037 | |
| 257 if (!MetafileSkiaWrapper::GetCustomScaleOnCanvas( | |
| 258 *canvas, actual_shrink)) { | |
| 259 // Update the dpi adjustment with the "page |actual_shrink|" calculated in | |
| 260 // webkit. | |
| 261 *actual_shrink /= (webkit_scale_factor * css_scale_factor); | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 // Done printing. Close the device context to retrieve the compiled metafile. | 244 // Done printing. Close the device context to retrieve the compiled metafile. |
| 266 if (!metafile->FinishPage()) | 245 if (!metafile->FinishPage()) |
| 267 NOTREACHED() << "metafile failed"; | 246 NOTREACHED() << "metafile failed"; |
| 268 } | 247 } |
| 269 | 248 |
| 270 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | 249 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( |
| 271 Metafile* metafile, base::SharedMemoryHandle* shared_mem_handle) { | 250 Metafile* metafile, base::SharedMemoryHandle* shared_mem_handle) { |
| 272 uint32 buf_size = metafile->GetDataSize(); | 251 uint32 buf_size = metafile->GetDataSize(); |
| 273 base::SharedMemory shared_buf; | 252 base::SharedMemory shared_buf; |
| 274 // Allocate a shared memory buffer to hold the generated metafile data. | 253 // Allocate a shared memory buffer to hold the generated metafile data. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 285 } | 264 } |
| 286 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 265 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
| 287 shared_buf.Unmap(); | 266 shared_buf.Unmap(); |
| 288 | 267 |
| 289 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 268 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
| 290 shared_mem_handle)); | 269 shared_mem_handle)); |
| 291 return true; | 270 return true; |
| 292 } | 271 } |
| 293 | 272 |
| 294 } // namespace printing | 273 } // namespace printing |
| OLD | NEW |