| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/print_web_view_helper.h" | 5 #include "chrome/renderer/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/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 const gfx::Size& canvas_size, | 77 const gfx::Size& canvas_size, |
| 78 WebFrame* frame) { | 78 WebFrame* frame) { |
| 79 // Generate a memory-based metafile. It will use the current screen's DPI. | 79 // Generate a memory-based metafile. It will use the current screen's DPI. |
| 80 // Each metafile contains a single page. | 80 // Each metafile contains a single page. |
| 81 scoped_ptr<Metafile> metafile(new printing::NativeMetafile); | 81 scoped_ptr<Metafile> metafile(new printing::NativeMetafile); |
| 82 metafile->Init(); | 82 metafile->Init(); |
| 83 DCHECK(metafile->context()); | 83 DCHECK(metafile->context()); |
| 84 skia::InitializeDC(metafile->context()); | 84 skia::InitializeDC(metafile->context()); |
| 85 | 85 |
| 86 int page_number = params.page_number; | 86 int page_number = params.page_number; |
| 87 int page_slot = params.page_slot; | |
| 88 | 87 |
| 89 // Calculate the dpi adjustment. | 88 // Calculate the dpi adjustment. |
| 90 float scale_factor = static_cast<float>(params.params.desired_dpi / | 89 float scale_factor = static_cast<float>(params.params.desired_dpi / |
| 91 params.params.dpi); | 90 params.params.dpi); |
| 92 | 91 |
| 93 // Render page for printing. | 92 // Render page for printing. |
| 94 RenderPage(params.params, &scale_factor, page_number, page_slot, false, frame, | 93 RenderPage(params.params, &scale_factor, page_number, false, frame, |
| 95 &metafile); | 94 &metafile); |
| 96 | 95 |
| 97 // Close the device context to retrieve the compiled metafile. | 96 // Close the device context to retrieve the compiled metafile. |
| 98 if (!metafile->FinishDocument()) | 97 if (!metafile->FinishDocument()) |
| 99 NOTREACHED(); | 98 NOTREACHED(); |
| 100 | 99 |
| 101 // Get the size of the compiled metafile. | 100 // Get the size of the compiled metafile. |
| 102 uint32 buf_size = metafile->GetDataSize(); | 101 uint32 buf_size = metafile->GetDataSize(); |
| 103 DCHECK_GT(buf_size, 128u); | 102 DCHECK_GT(buf_size, 128u); |
| 104 | 103 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 126 PrintMsg_Print_Params print_params = print_preview_context_.print_params(); | 125 PrintMsg_Print_Params print_params = print_preview_context_.print_params(); |
| 127 // Calculate the dpi adjustment. | 126 // Calculate the dpi adjustment. |
| 128 float scale_factor = static_cast<float>(print_params.desired_dpi / | 127 float scale_factor = static_cast<float>(print_params.desired_dpi / |
| 129 print_params.dpi); | 128 print_params.dpi); |
| 130 | 129 |
| 131 // |metafile| is needed for RenderPage() below. |metafile| will not take the | 130 // |metafile| is needed for RenderPage() below. |metafile| will not take the |
| 132 // ownership of |print_preview_context_| metafile. | 131 // ownership of |print_preview_context_| metafile. |
| 133 scoped_ptr<Metafile> metafile(print_preview_context_.metafile()); | 132 scoped_ptr<Metafile> metafile(print_preview_context_.metafile()); |
| 134 | 133 |
| 135 base::TimeTicks begin_time = base::TimeTicks::Now(); | 134 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 136 RenderPage(print_params, &scale_factor, page_number, | 135 RenderPage(print_params, &scale_factor, page_number, true, |
| 137 print_preview_context_.GetPageSlotForPage(page_number), true, | |
| 138 print_preview_context_.frame(), &metafile); | 136 print_preview_context_.frame(), &metafile); |
| 139 | 137 |
| 140 print_preview_context_.RenderedPreviewPage( | 138 print_preview_context_.RenderedPreviewPage( |
| 141 base::TimeTicks::Now() - begin_time); | 139 base::TimeTicks::Now() - begin_time); |
| 142 | 140 |
| 143 // Release since |print_preview_context_| is the real owner. | 141 // Release since |print_preview_context_| is the real owner. |
| 144 metafile.release(); | 142 metafile.release(); |
| 145 printing::Metafile* page_metafile = NULL; | 143 printing::Metafile* page_metafile = NULL; |
| 146 if (print_preview_context_.IsModifiable()) { | 144 if (print_preview_context_.IsModifiable()) { |
| 147 page_metafile = reinterpret_cast<printing::PreviewMetafile*>( | 145 page_metafile = reinterpret_cast<printing::PreviewMetafile*>( |
| 148 print_preview_context_.metafile())->GetMetafileForCurrentPage(); | 146 print_preview_context_.metafile())->GetMetafileForCurrentPage(); |
| 149 } | 147 } |
| 150 PreviewPageRendered(page_number, page_metafile); | 148 PreviewPageRendered(page_number, page_metafile); |
| 151 } | 149 } |
| 152 | 150 |
| 153 void PrintWebViewHelper::RenderPage( | 151 void PrintWebViewHelper::RenderPage( |
| 154 const PrintMsg_Print_Params& params, float* scale_factor, int page_number, | 152 const PrintMsg_Print_Params& params, float* scale_factor, int page_number, |
| 155 int page_slot, bool is_preview, WebFrame* frame, | 153 bool is_preview, WebFrame* frame, scoped_ptr<Metafile>* metafile) { |
| 156 scoped_ptr<Metafile>* metafile) { | |
| 157 PageSizeMargins page_layout_in_points; | 154 PageSizeMargins page_layout_in_points; |
| 158 GetPageSizeAndMarginsInPoints(frame, page_number, params, | 155 GetPageSizeAndMarginsInPoints(frame, page_number, params, |
| 159 &page_layout_in_points); | 156 &page_layout_in_points); |
| 160 | 157 |
| 161 int width; | 158 int width; |
| 162 int height; | 159 int height; |
| 163 if (is_preview) { | 160 if (is_preview) { |
| 164 int dpi = static_cast<int>(params.dpi); | 161 int dpi = static_cast<int>(params.dpi); |
| 165 int desired_dpi = printing::kPointsPerInch; | 162 int desired_dpi = printing::kPointsPerInch; |
| 166 width = ConvertUnit(params.page_size.width(), dpi, desired_dpi); | 163 width = ConvertUnit(params.page_size.width(), dpi, desired_dpi); |
| 167 height = ConvertUnit(params.page_size.height(), dpi, desired_dpi); | 164 height = ConvertUnit(params.page_size.height(), dpi, desired_dpi); |
| 168 } else { | 165 } else { |
| 169 // Since WebKit extends the page width depending on the magical scale factor | 166 // Since WebKit extends the page width depending on the magical scale factor |
| 170 // we make sure the canvas covers the worst case scenario (x2.0 currently). | 167 // we make sure the canvas covers the worst case scenario (x2.0 currently). |
| 171 // PrintContext will then set the correct clipping region. | 168 // PrintContext will then set the correct clipping region. |
| 172 width = static_cast<int>(page_layout_in_points.content_width * | 169 width = static_cast<int>(page_layout_in_points.content_width * |
| 173 params.max_shrink); | 170 params.max_shrink); |
| 174 height = static_cast<int>(page_layout_in_points.content_height * | 171 height = static_cast<int>(page_layout_in_points.content_height * |
| 175 params.max_shrink); | 172 params.max_shrink); |
| 176 } | 173 } |
| 177 | 174 |
| 178 gfx::Size page_size(width, height); | 175 gfx::Size page_size(width, height); |
| 179 gfx::Rect content_area( | 176 gfx::Rect content_area( |
| 180 static_cast<int>(page_layout_in_points.margin_left), | 177 static_cast<int>(page_layout_in_points.margin_left), |
| 181 static_cast<int>(page_layout_in_points.margin_top), | 178 static_cast<int>(page_layout_in_points.margin_top), |
| 182 static_cast<int>(page_layout_in_points.content_width), | 179 static_cast<int>(page_layout_in_points.content_width), |
| 183 static_cast<int>(page_layout_in_points.content_height)); | 180 static_cast<int>(page_layout_in_points.content_height)); |
| 184 SkDevice* device = (*metafile)->StartPageForVectorCanvas( | 181 SkDevice* device = (*metafile)->StartPageForVectorCanvas( |
| 185 page_slot, page_size, content_area, | 182 page_size, content_area, frame->getPrintPageShrink(page_number)); |
| 186 frame->getPrintPageShrink(page_number)); | |
| 187 DCHECK(device); | 183 DCHECK(device); |
| 188 // The printPage method may take a reference to the canvas we pass down, so it | 184 // The printPage method may take a reference to the canvas we pass down, so it |
| 189 // can't be a stack object. | 185 // can't be a stack object. |
| 190 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 186 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
| 191 canvas->unref(); // SkRefPtr and new both took a reference. | 187 canvas->unref(); // SkRefPtr and new both took a reference. |
| 192 if (is_preview) { | 188 if (is_preview) { |
| 193 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), | 189 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), |
| 194 metafile->get()); | 190 metafile->get()); |
| 195 } | 191 } |
| 196 | 192 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 shared_buf.Unmap(); | 282 shared_buf.Unmap(); |
| 287 return false; | 283 return false; |
| 288 } | 284 } |
| 289 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 285 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
| 290 shared_buf.Unmap(); | 286 shared_buf.Unmap(); |
| 291 | 287 |
| 292 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | 288 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, |
| 293 shared_mem_handle)); | 289 shared_mem_handle)); |
| 294 return true; | 290 return true; |
| 295 } | 291 } |
| OLD | NEW |