| 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/file_descriptor_posix.h" | 7 #include "base/file_descriptor_posix.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "chrome/common/print_messages.h" | 11 #include "chrome/common/print_messages.h" |
| 12 #include "content/common/view_messages.h" | 12 #include "content/common/view_messages.h" |
| 13 #include "printing/metafile.h" | 13 #include "printing/metafile.h" |
| 14 #include "printing/metafile_impl.h" | 14 #include "printing/metafile_impl.h" |
| 15 #include "printing/metafile_skia_wrapper.h" | 15 #include "printing/metafile_skia_wrapper.h" |
| 16 #include "skia/ext/vector_canvas.h" | 16 #include "skia/ext/vector_canvas.h" |
| 17 #include "skia/ext/vector_platform_device_skia.h" |
| 17 #include "third_party/skia/include/core/SkRefCnt.h" | 18 #include "third_party/skia/include/core/SkRefCnt.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 19 #include "ui/gfx/point.h" | 20 #include "ui/gfx/point.h" |
| 20 | 21 |
| 21 #if !defined(OS_CHROMEOS) | 22 #if !defined(OS_CHROMEOS) |
| 22 #include "base/process_util.h" | 23 #include "base/process_util.h" |
| 23 #endif // !defined(OS_CHROMEOS) | 24 #endif // !defined(OS_CHROMEOS) |
| 24 | 25 |
| 25 using WebKit::WebFrame; | 26 using WebKit::WebFrame; |
| 26 using WebKit::WebNode; | 27 using WebKit::WebNode; |
| 27 | 28 |
| 28 void PrintWebViewHelper::RenderPreviewPage(int page_number) { | 29 void PrintWebViewHelper::RenderPreviewPage(int page_number) { |
| 29 PrintMsg_PrintPage_Params page_params; | 30 PrintMsg_PrintPage_Params page_params; |
| 30 page_params.params = print_preview_context_.print_params(); | 31 page_params.params = print_preview_context_.print_params(); |
| 31 page_params.page_number = page_number; | 32 page_params.page_number = page_number; |
| 32 | 33 |
| 33 base::TimeTicks begin_time = base::TimeTicks::Now(); | 34 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 34 PrintPageInternal(page_params, | 35 PrintPageInternal(page_params, |
| 35 print_preview_context_.GetPrintCanvasSize(), | 36 print_preview_context_.GetPrintCanvasSize(), |
| 36 print_preview_context_.frame(), | 37 print_preview_context_.frame(), |
| 37 print_preview_context_.metafile()); | 38 print_preview_context_.metafile(), |
| 39 true); |
| 38 print_preview_context_.RenderedPreviewPage( | 40 print_preview_context_.RenderedPreviewPage( |
| 39 base::TimeTicks::Now() - begin_time); | 41 base::TimeTicks::Now() - begin_time); |
| 40 PreviewPageRendered(page_number); | 42 PreviewPageRendered(page_number); |
| 41 } | 43 } |
| 42 | 44 |
| 43 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, | 45 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, |
| 44 WebFrame* frame, | 46 WebFrame* frame, |
| 45 WebNode* node) { | 47 WebNode* node) { |
| 46 printing::NativeMetafile metafile; | 48 printing::NativeMetafile metafile; |
| 47 if (!metafile.Init()) | 49 if (!metafile.Init()) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 | 143 |
| 142 base::TimeTicks begin_time = base::TimeTicks::Now(); | 144 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 143 base::TimeTicks page_begin_time = begin_time; | 145 base::TimeTicks page_begin_time = begin_time; |
| 144 | 146 |
| 145 PrintMsg_PrintPage_Params page_params; | 147 PrintMsg_PrintPage_Params page_params; |
| 146 page_params.params = printParams; | 148 page_params.params = printParams; |
| 147 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 149 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
| 148 if (params.pages.empty()) { | 150 if (params.pages.empty()) { |
| 149 for (int i = 0; i < *page_count; ++i) { | 151 for (int i = 0; i < *page_count; ++i) { |
| 150 page_params.page_number = i; | 152 page_params.page_number = i; |
| 151 PrintPageInternal(page_params, canvas_size, frame, metafile); | 153 PrintPageInternal(page_params, canvas_size, frame, metafile, false); |
| 152 } | 154 } |
| 153 } else { | 155 } else { |
| 154 for (size_t i = 0; i < params.pages.size(); ++i) { | 156 for (size_t i = 0; i < params.pages.size(); ++i) { |
| 155 page_params.page_number = params.pages[i]; | 157 page_params.page_number = params.pages[i]; |
| 156 PrintPageInternal(page_params, canvas_size, frame, metafile); | 158 PrintPageInternal(page_params, canvas_size, frame, metafile, false); |
| 157 } | 159 } |
| 158 } | 160 } |
| 159 | 161 |
| 160 base::TimeDelta render_time = base::TimeTicks::Now() - begin_time; | 162 base::TimeDelta render_time = base::TimeTicks::Now() - begin_time; |
| 161 | 163 |
| 162 prep_frame_view.FinishPrinting(); | 164 prep_frame_view.FinishPrinting(); |
| 163 metafile->FinishDocument(); | 165 metafile->FinishDocument(); |
| 164 return true; | 166 return true; |
| 165 } | 167 } |
| 166 | 168 |
| 167 void PrintWebViewHelper::PrintPageInternal( | 169 void PrintWebViewHelper::PrintPageInternal( |
| 168 const PrintMsg_PrintPage_Params& params, | 170 const PrintMsg_PrintPage_Params& params, |
| 169 const gfx::Size& canvas_size, | 171 const gfx::Size& canvas_size, |
| 170 WebFrame* frame, | 172 WebFrame* frame, |
| 171 printing::Metafile* metafile) { | 173 printing::Metafile* metafile, |
| 174 bool is_preview) { |
| 172 PageSizeMargins page_layout_in_points; | 175 PageSizeMargins page_layout_in_points; |
| 173 GetPageSizeAndMarginsInPoints(frame, params.page_number, params.params, | 176 GetPageSizeAndMarginsInPoints(frame, params.page_number, params.params, |
| 174 &page_layout_in_points); | 177 &page_layout_in_points); |
| 175 | 178 |
| 176 gfx::Size page_size( | 179 gfx::Size page_size( |
| 177 page_layout_in_points.content_width + | 180 page_layout_in_points.content_width + |
| 178 page_layout_in_points.margin_right + | 181 page_layout_in_points.margin_right + |
| 179 page_layout_in_points.margin_left, | 182 page_layout_in_points.margin_left, |
| 180 page_layout_in_points.content_height + | 183 page_layout_in_points.content_height + |
| 181 page_layout_in_points.margin_top + | 184 page_layout_in_points.margin_top + |
| 182 page_layout_in_points.margin_bottom); | 185 page_layout_in_points.margin_bottom); |
| 183 gfx::Rect content_area(page_layout_in_points.margin_left, | 186 gfx::Rect content_area(page_layout_in_points.margin_left, |
| 184 page_layout_in_points.margin_top, | 187 page_layout_in_points.margin_top, |
| 185 page_layout_in_points.content_width, | 188 page_layout_in_points.content_width, |
| 186 page_layout_in_points.content_height); | 189 page_layout_in_points.content_height); |
| 187 | 190 |
| 188 SkDevice* device = metafile->StartPageForVectorCanvas( | 191 SkDevice* device = metafile->StartPageForVectorCanvas( |
| 189 page_size, content_area, 1.0f); | 192 page_size, content_area, 1.0f); |
| 190 if (!device) | 193 if (!device) |
| 191 return; | 194 return; |
| 192 | 195 |
| 193 // The printPage method take a reference to the canvas we pass down, so it | 196 // The printPage method take a reference to the canvas we pass down, so it |
| 194 // can't be a stack object. | 197 // can't be a stack object. |
| 195 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 198 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
| 196 canvas->unref(); // SkRefPtr and new both took a reference. | 199 canvas->unref(); // SkRefPtr and new both took a reference. |
| 197 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); | 200 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); |
| 198 frame->printPage(params.page_number, canvas.get()); | 201 frame->printPage(params.page_number, canvas.get()); |
| 199 | 202 |
| 200 // TODO(myhuang): We should render the header and the footer. | 203 if (is_preview && params.params.display_header_footer) { |
| 204 // The page_number count starts from 0, so we add 1. |
| 205 // The scale factor on Linux is 1. |
| 206 PrintHeaderAndFooter(static_cast<skia::VectorPlatformDeviceSkia*>(device), |
| 207 canvas, params.page_number + 1, |
| 208 print_preview_context_.total_page_count(), 1, |
| 209 page_layout_in_points, header_footer_info_); |
| 210 } |
| 201 | 211 |
| 202 // Done printing. Close the device context to retrieve the compiled metafile. | 212 // Done printing. Close the device context to retrieve the compiled metafile. |
| 203 if (!metafile->FinishPage()) | 213 if (!metafile->FinishPage()) |
| 204 NOTREACHED() << "metafile failed"; | 214 NOTREACHED() << "metafile failed"; |
| 205 } | 215 } |
| OLD | NEW |