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 "printing/page_size_margins.h" | 15 #include "printing/page_size_margins.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
17 | 17 |
18 #if defined(USE_SKIA) | 18 #if defined(USE_SKIA) |
19 #include "printing/metafile_skia_wrapper.h" | 19 #include "printing/metafile_skia_wrapper.h" |
| 20 #include "skia/ext/platform_device.h" |
20 #include "skia/ext/vector_canvas.h" | 21 #include "skia/ext/vector_canvas.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" |
22 #endif | 23 #endif |
23 | 24 |
24 using WebKit::WebFrame; | 25 using WebKit::WebFrame; |
25 | 26 |
26 void PrintWebViewHelper::PrintPageInternal( | 27 void PrintWebViewHelper::PrintPageInternal( |
27 const PrintMsg_PrintPage_Params& params, | 28 const PrintMsg_PrintPage_Params& params, |
28 const gfx::Size& canvas_size, | 29 const gfx::Size& canvas_size, |
29 WebFrame* frame) { | 30 WebFrame* frame) { |
30 printing::NativeMetafile metafile; | 31 printing::NativeMetafile metafile; |
31 if (!metafile.Init()) | 32 if (!metafile.Init()) |
32 return; | 33 return; |
33 | 34 |
34 float scale_factor = frame->getPrintPageShrink(params.page_number); | 35 float scale_factor = frame->getPrintPageShrink(params.page_number); |
35 int page_number = params.page_number; | 36 int page_number = params.page_number; |
36 | 37 |
37 // Render page for printing. | 38 // Render page for printing. |
38 gfx::Rect content_area(params.params.printable_size); | 39 gfx::Rect content_area(params.params.printable_size); |
39 RenderPage(params.params.printable_size, content_area, scale_factor, | 40 RenderPage(params.params.printable_size, content_area, scale_factor, |
40 page_number, frame, &metafile); | 41 page_number, frame, false, &metafile); |
41 metafile.FinishDocument(); | 42 metafile.FinishDocument(); |
42 | 43 |
43 PrintHostMsg_DidPrintPage_Params page_params; | 44 PrintHostMsg_DidPrintPage_Params page_params; |
44 page_params.data_size = metafile.GetDataSize(); | 45 page_params.data_size = metafile.GetDataSize(); |
45 page_params.page_number = page_number; | 46 page_params.page_number = page_number; |
46 page_params.document_cookie = params.params.document_cookie; | 47 page_params.document_cookie = params.params.document_cookie; |
47 page_params.actual_shrink = scale_factor; | 48 page_params.actual_shrink = scale_factor; |
48 page_params.page_size = params.params.page_size; | 49 page_params.page_size = params.params.page_size; |
49 page_params.content_area = gfx::Rect(params.params.margin_left, | 50 page_params.content_area = gfx::Rect(params.params.margin_left, |
50 params.params.margin_top, | 51 params.params.margin_top, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 print_preview_context_.set_error( | 90 print_preview_context_.set_error( |
90 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); | 91 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); |
91 LOG(ERROR) << "Draft PreviewMetafile Init failed"; | 92 LOG(ERROR) << "Draft PreviewMetafile Init failed"; |
92 return false; | 93 return false; |
93 } | 94 } |
94 initial_render_metafile = draft_metafile.get(); | 95 initial_render_metafile = draft_metafile.get(); |
95 } | 96 } |
96 | 97 |
97 base::TimeTicks begin_time = base::TimeTicks::Now(); | 98 base::TimeTicks begin_time = base::TimeTicks::Now(); |
98 RenderPage(printParams.page_size, content_area, scale_factor, page_number, | 99 RenderPage(printParams.page_size, content_area, scale_factor, page_number, |
99 print_preview_context_.frame(), initial_render_metafile); | 100 print_preview_context_.frame(), true, initial_render_metafile); |
100 print_preview_context_.RenderedPreviewPage( | 101 print_preview_context_.RenderedPreviewPage( |
101 base::TimeTicks::Now() - begin_time); | 102 base::TimeTicks::Now() - begin_time); |
102 | 103 |
103 if (draft_metafile.get()) { | 104 if (draft_metafile.get()) { |
104 draft_metafile->FinishDocument(); | 105 draft_metafile->FinishDocument(); |
105 #if !defined(USE_SKIA) | 106 #if !defined(USE_SKIA) |
106 if (!is_print_ready_metafile_sent_) { | 107 if (!is_print_ready_metafile_sent_) { |
107 // With CG, we rendered into a new metafile so we could get it as a draft | 108 // With CG, we rendered into a new metafile so we could get it as a draft |
108 // document. Now we need to add it to print ready document. But the | 109 // document. Now we need to add it to print ready document. But the |
109 // document has already been scaled and adjusted for margins, so do a 1:1 | 110 // document has already been scaled and adjusted for margins, so do a 1:1 |
(...skipping 27 matching lines...) Expand all Loading... |
137 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | 138 print_preview_context_.metafile()->GetMetafileForCurrentPage()); |
138 } | 139 } |
139 #endif | 140 #endif |
140 } | 141 } |
141 return PreviewPageRendered(page_number, draft_metafile.get()); | 142 return PreviewPageRendered(page_number, draft_metafile.get()); |
142 } | 143 } |
143 | 144 |
144 void PrintWebViewHelper::RenderPage( | 145 void PrintWebViewHelper::RenderPage( |
145 const gfx::Size& page_size, const gfx::Rect& content_area, | 146 const gfx::Size& page_size, const gfx::Rect& content_area, |
146 const float& scale_factor, int page_number, WebFrame* frame, | 147 const float& scale_factor, int page_number, WebFrame* frame, |
147 printing::Metafile* metafile) { | 148 bool is_preview, printing::Metafile* metafile) { |
148 | 149 |
149 { | 150 { |
150 #if defined(USE_SKIA) | 151 #if defined(USE_SKIA) |
151 SkDevice* device = metafile->StartPageForVectorCanvas( | 152 SkDevice* device = metafile->StartPageForVectorCanvas( |
152 page_size, content_area, scale_factor); | 153 page_size, content_area, scale_factor); |
153 if (!device) | 154 if (!device) |
154 return; | 155 return; |
155 | 156 |
156 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 157 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
157 canvas->unref(); // SkRefPtr and new both took a reference. | 158 canvas->unref(); // SkRefPtr and new both took a reference. |
158 WebKit::WebCanvas* canvas_ptr = canvas.get(); | 159 WebKit::WebCanvas* canvas_ptr = canvas.get(); |
159 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); | 160 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); |
160 printing::MetafileSkiaWrapper::SetDraftMode(canvas.get(), | 161 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); |
161 is_print_ready_metafile_sent_); | 162 skia::SetIsPreviewMetafile(*canvas, is_preview); |
162 #else | 163 #else |
163 bool success = metafile->StartPage(page_size, content_area, scale_factor); | 164 bool success = metafile->StartPage(page_size, content_area, scale_factor); |
164 DCHECK(success); | 165 DCHECK(success); |
165 // printPage can create autoreleased references to |context|. PDF contexts | 166 // printPage can create autoreleased references to |context|. PDF contexts |
166 // don't write all their data until they are destroyed, so we need to make | 167 // don't write all their data until they are destroyed, so we need to make |
167 // certain that there are no lingering references. | 168 // certain that there are no lingering references. |
168 base::mac::ScopedNSAutoreleasePool pool; | 169 base::mac::ScopedNSAutoreleasePool pool; |
169 CGContextRef cgContext = metafile->context(); | 170 CGContextRef cgContext = metafile->context(); |
170 CGContextRef canvas_ptr = cgContext; | 171 CGContextRef canvas_ptr = cgContext; |
171 #endif | 172 #endif |
(...skipping 26 matching lines...) Expand all Loading... |
198 print_preview_context_.total_page_count(), | 199 print_preview_context_.total_page_count(), |
199 scale_factor, page_layout_in_points, | 200 scale_factor, page_layout_in_points, |
200 *header_footer_info_); | 201 *header_footer_info_); |
201 } | 202 } |
202 #endif // defined(USE_SKIA) | 203 #endif // defined(USE_SKIA) |
203 } | 204 } |
204 | 205 |
205 // Done printing. Close the device context to retrieve the compiled metafile. | 206 // Done printing. Close the device context to retrieve the compiled metafile. |
206 metafile->FinishPage(); | 207 metafile->FinishPage(); |
207 } | 208 } |
OLD | NEW |