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 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/scoped_nsautorelease_pool.h" | 10 #include "base/mac/scoped_nsautorelease_pool.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "chrome/common/print_messages.h" | 12 #include "chrome/common/print_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 "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
16 | 16 |
17 #if defined(USE_SKIA) | 17 #if defined(USE_SKIA) |
18 #include "printing/metafile_skia_wrapper.h" | 18 #include "printing/metafile_skia_wrapper.h" |
19 #include "skia/ext/vector_canvas.h" | 19 #include "skia/ext/vector_canvas.h" |
| 20 #include "skia/ext/vector_platform_device_skia.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" |
21 #endif | 22 #endif |
22 | 23 |
23 using WebKit::WebFrame; | 24 using WebKit::WebFrame; |
24 | 25 |
25 void PrintWebViewHelper::PrintPageInternal( | 26 void PrintWebViewHelper::PrintPageInternal( |
26 const PrintMsg_PrintPage_Params& params, | 27 const PrintMsg_PrintPage_Params& params, |
27 const gfx::Size& canvas_size, | 28 const gfx::Size& canvas_size, |
28 WebFrame* frame) { | 29 WebFrame* frame) { |
29 printing::NativeMetafile metafile; | 30 printing::NativeMetafile metafile; |
30 if (!metafile.Init()) | 31 if (!metafile.Init()) |
31 return; | 32 return; |
32 | 33 |
33 float scale_factor = frame->getPrintPageShrink(params.page_number); | 34 float scale_factor = frame->getPrintPageShrink(params.page_number); |
34 int page_number = params.page_number; | 35 int page_number = params.page_number; |
35 | 36 |
36 // Render page for printing. | 37 // Render page for printing. |
37 gfx::Rect content_area(params.params.printable_size); | 38 gfx::Rect content_area(params.params.printable_size); |
38 RenderPage(params.params.printable_size, content_area, scale_factor, | 39 RenderPage(params.params.printable_size, content_area, scale_factor, |
39 page_number, frame, &metafile); | 40 page_number, frame, &metafile, false); |
40 metafile.FinishDocument(); | 41 metafile.FinishDocument(); |
41 | 42 |
42 PrintHostMsg_DidPrintPage_Params page_params; | 43 PrintHostMsg_DidPrintPage_Params page_params; |
43 page_params.data_size = metafile.GetDataSize(); | 44 page_params.data_size = metafile.GetDataSize(); |
44 page_params.page_number = page_number; | 45 page_params.page_number = page_number; |
45 page_params.document_cookie = params.params.document_cookie; | 46 page_params.document_cookie = params.params.document_cookie; |
46 page_params.actual_shrink = scale_factor; | 47 page_params.actual_shrink = scale_factor; |
47 page_params.page_size = params.params.page_size; | 48 page_params.page_size = params.params.page_size; |
48 page_params.content_area = gfx::Rect(params.params.margin_left, | 49 page_params.content_area = gfx::Rect(params.params.margin_left, |
49 params.params.margin_top, | 50 params.params.margin_top, |
(...skipping 11 matching lines...) Expand all Loading... |
61 | 62 |
62 void PrintWebViewHelper::RenderPreviewPage(int page_number) { | 63 void PrintWebViewHelper::RenderPreviewPage(int page_number) { |
63 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); | 64 float scale_factor = print_preview_context_.frame()->getPrintPageShrink(0); |
64 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); | 65 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); |
65 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, | 66 gfx::Rect content_area(printParams.margin_left, printParams.margin_top, |
66 printParams.printable_size.width(), | 67 printParams.printable_size.width(), |
67 printParams.printable_size.height()); | 68 printParams.printable_size.height()); |
68 | 69 |
69 base::TimeTicks begin_time = base::TimeTicks::Now(); | 70 base::TimeTicks begin_time = base::TimeTicks::Now(); |
70 RenderPage(printParams.page_size, content_area, scale_factor, page_number, | 71 RenderPage(printParams.page_size, content_area, scale_factor, page_number, |
71 print_preview_context_.frame(), print_preview_context_.metafile()); | 72 print_preview_context_.frame(), print_preview_context_.metafile(), |
| 73 true); |
72 print_preview_context_.RenderedPreviewPage( | 74 print_preview_context_.RenderedPreviewPage( |
73 base::TimeTicks::Now() - begin_time); | 75 base::TimeTicks::Now() - begin_time); |
74 PreviewPageRendered(page_number); | 76 PreviewPageRendered(page_number); |
75 } | 77 } |
76 | 78 |
77 void PrintWebViewHelper::RenderPage( | 79 void PrintWebViewHelper::RenderPage( |
78 const gfx::Size& page_size, const gfx::Rect& content_area, | 80 const gfx::Size& page_size, const gfx::Rect& content_area, |
79 const float& scale_factor, int page_number, WebFrame* frame, | 81 const float& scale_factor, int page_number, WebFrame* frame, |
80 printing::Metafile* metafile) { | 82 printing::Metafile* metafile, bool is_preview) { |
81 | 83 |
82 { | 84 { |
83 #if defined(USE_SKIA) | 85 #if defined(USE_SKIA) |
84 SkDevice* device = metafile->StartPageForVectorCanvas( | 86 SkDevice* device = metafile->StartPageForVectorCanvas( |
85 page_size, content_area, scale_factor); | 87 page_size, content_area, scale_factor); |
86 if (!device) | 88 if (!device) |
87 return; | 89 return; |
88 | 90 |
89 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 91 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
90 canvas->unref(); // SkRefPtr and new both took a reference. | 92 canvas->unref(); // SkRefPtr and new both took a reference. |
91 WebKit::WebCanvas* canvasPtr = canvas.get(); | 93 WebKit::WebCanvas* canvasPtr = canvas.get(); |
92 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvasPtr, metafile); | 94 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvasPtr, metafile); |
93 #else | 95 #else |
94 bool success = metafile->StartPage(page_size, content_area, scale_factor); | 96 bool success = metafile->StartPage(page_size, content_area, scale_factor); |
95 DCHECK(success); | 97 DCHECK(success); |
96 // printPage can create autoreleased references to |context|. PDF contexts | 98 // printPage can create autoreleased references to |context|. PDF contexts |
97 // don't write all their data until they are destroyed, so we need to make | 99 // don't write all their data until they are destroyed, so we need to make |
98 // certain that there are no lingering references. | 100 // certain that there are no lingering references. |
99 base::mac::ScopedNSAutoreleasePool pool; | 101 base::mac::ScopedNSAutoreleasePool pool; |
100 CGContextRef cgContext = metafile->context(); | 102 CGContextRef cgContext = metafile->context(); |
101 CGContextRef canvasPtr = cgContext; | 103 CGContextRef canvasPtr = cgContext; |
102 #endif | 104 #endif |
103 frame->printPage(page_number, canvasPtr); | 105 float webkit_scale_factor = frame->printPage(page_number, canvasPtr); |
| 106 #if defined(USE_SKIA) |
| 107 PrintMsg_Print_Params printParams = print_preview_context_.print_params(); |
| 108 if (is_preview && printParams.display_header_footer) { |
| 109 PageSizeMargins page_layout_in_points; |
| 110 GetPageSizeAndMarginsInPoints(frame, page_number, printParams, |
| 111 &page_layout_in_points); |
| 112 // The page_number count starts from 0, so we add 1. |
| 113 PrintHeaderAndFooter(static_cast<skia::VectorPlatformDeviceSkia*>(device), |
| 114 canvas, page_number + 1, |
| 115 print_preview_context_.total_page_count(), |
| 116 webkit_scale_factor, page_layout_in_points, |
| 117 header_footer_info_); |
| 118 } |
| 119 #endif |
104 } | 120 } |
105 | 121 |
106 // Done printing. Close the device context to retrieve the compiled metafile. | 122 // Done printing. Close the device context to retrieve the compiled metafile. |
107 metafile->FinishPage(); | 123 metafile->FinishPage(); |
108 } | 124 } |
OLD | NEW |