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 "base/utf_string_conversions.h" | |
11 #include "chrome/common/print_messages.h" | 12 #include "chrome/common/print_messages.h" |
12 #include "content/common/view_messages.h" | 13 #include "content/common/view_messages.h" |
13 #include "printing/metafile.h" | 14 #include "printing/metafile.h" |
14 #include "printing/metafile_impl.h" | 15 #include "printing/metafile_impl.h" |
15 #include "printing/metafile_skia_wrapper.h" | 16 #include "printing/metafile_skia_wrapper.h" |
16 #include "skia/ext/vector_canvas.h" | 17 #include "skia/ext/vector_canvas.h" |
18 #include "skia/ext/vector_platform_device_skia.h" | |
19 #include "third_party/skia/include/core/SkColor.h" | |
20 #include "third_party/skia/include/core/SkDraw.h" | |
17 #include "third_party/skia/include/core/SkRefCnt.h" | 21 #include "third_party/skia/include/core/SkRefCnt.h" |
22 #include "third_party/skia/include/core/SkTypeface.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
19 #include "ui/gfx/point.h" | 24 #include "ui/gfx/point.h" |
20 | 25 |
21 #if !defined(OS_CHROMEOS) | 26 #if !defined(OS_CHROMEOS) |
22 #include "base/process_util.h" | 27 #include "base/process_util.h" |
23 #endif // !defined(OS_CHROMEOS) | 28 #endif // !defined(OS_CHROMEOS) |
24 | 29 |
25 using WebKit::WebFrame; | 30 using WebKit::WebFrame; |
26 using WebKit::WebNode; | 31 using WebKit::WebNode; |
27 | 32 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 return false; | 162 return false; |
158 if (is_preview) { | 163 if (is_preview) { |
159 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), *page_count)); | 164 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), *page_count)); |
160 } else { | 165 } else { |
161 #if !defined(OS_CHROMEOS) | 166 #if !defined(OS_CHROMEOS) |
162 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 167 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
163 printParams.document_cookie, | 168 printParams.document_cookie, |
164 *page_count)); | 169 *page_count)); |
165 #endif | 170 #endif |
166 } | 171 } |
172 int total_pages = *page_count; | |
kmadhusu
2011/07/14 01:50:15
|total_pages| is not required. At this point, you
Aayush Kumar
2011/07/19 01:20:30
Done.
| |
167 | 173 |
168 base::TimeTicks begin_time = base::TimeTicks::Now(); | 174 base::TimeTicks begin_time = base::TimeTicks::Now(); |
169 base::TimeTicks page_begin_time = begin_time; | 175 base::TimeTicks page_begin_time = begin_time; |
170 | 176 |
171 PrintMsg_PrintPage_Params page_params; | 177 PrintMsg_PrintPage_Params page_params; |
172 page_params.params = printParams; | 178 page_params.params = printParams; |
173 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 179 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
174 if (params.pages.empty()) { | 180 if (params.pages.empty()) { |
175 for (int i = 0; i < *page_count; ++i) { | 181 for (int i = 0; i < *page_count; ++i) { |
176 page_params.page_number = i; | 182 page_params.page_number = i; |
177 PrintPageInternal(page_params, canvas_size, frame, metafile); | 183 PrintPageInternal(page_params, canvas_size, frame, metafile, is_preview, |
184 total_pages); | |
178 if (is_preview) { | 185 if (is_preview) { |
179 page_begin_time = ReportPreviewPageRenderTime(page_begin_time); | 186 page_begin_time = ReportPreviewPageRenderTime(page_begin_time); |
180 if (!PreviewPageRendered(i)) | 187 if (!PreviewPageRendered(i)) |
181 return false; | 188 return false; |
182 } | 189 } |
183 } | 190 } |
184 } else { | 191 } else { |
185 for (size_t i = 0; i < params.pages.size(); ++i) { | 192 for (size_t i = 0; i < params.pages.size(); ++i) { |
186 page_params.page_number = params.pages[i]; | 193 page_params.page_number = params.pages[i]; |
187 PrintPageInternal(page_params, canvas_size, frame, metafile); | 194 PrintPageInternal(page_params, canvas_size, frame, metafile, is_preview, |
195 total_pages); | |
188 if (is_preview) { | 196 if (is_preview) { |
189 page_begin_time = ReportPreviewPageRenderTime(page_begin_time); | 197 page_begin_time = ReportPreviewPageRenderTime(page_begin_time); |
190 if (!PreviewPageRendered(params.pages[i])) | 198 if (!PreviewPageRendered(params.pages[i])) |
191 return false; | 199 return false; |
192 } | 200 } |
193 } | 201 } |
194 } | 202 } |
195 | 203 |
196 base::TimeDelta render_time = base::TimeTicks::Now() - begin_time; | 204 base::TimeDelta render_time = base::TimeTicks::Now() - begin_time; |
197 | 205 |
198 prep_frame_view.FinishPrinting(); | 206 prep_frame_view.FinishPrinting(); |
199 metafile->FinishDocument(); | 207 metafile->FinishDocument(); |
200 | 208 |
201 if (is_preview) { | 209 if (is_preview) { |
202 ReportTotalPreviewGenerationTime(params.pages.size(), *page_count, | 210 ReportTotalPreviewGenerationTime(params.pages.size(), *page_count, |
203 render_time, | 211 render_time, |
204 base::TimeTicks::Now() - begin_time); | 212 base::TimeTicks::Now() - begin_time); |
205 } | 213 } |
206 return true; | 214 return true; |
207 } | 215 } |
208 | 216 |
209 void PrintWebViewHelper::PrintPageInternal( | 217 void PrintWebViewHelper::PrintPageInternal( |
210 const PrintMsg_PrintPage_Params& params, | 218 const PrintMsg_PrintPage_Params& params, |
211 const gfx::Size& canvas_size, | 219 const gfx::Size& canvas_size, |
212 WebFrame* frame, | 220 WebFrame* frame, |
213 printing::Metafile* metafile) { | 221 printing::Metafile* metafile, |
214 double content_width_in_points; | 222 bool is_preview, |
215 double content_height_in_points; | 223 int total_pages) { |
kmadhusu
2011/07/14 01:50:15
You don't need |total_pages|. Just use |preview_pa
Aayush Kumar
2011/07/19 01:20:30
Done.
| |
216 double margin_top_in_points; | |
217 double margin_right_in_points; | |
218 double margin_bottom_in_points; | |
219 double margin_left_in_points; | |
220 GetPageSizeAndMarginsInPoints(frame, | 224 GetPageSizeAndMarginsInPoints(frame, |
221 params.page_number, | 225 params.page_number, |
222 params.params, | 226 params.params); |
223 &content_width_in_points, | |
224 &content_height_in_points, | |
225 &margin_top_in_points, | |
226 &margin_right_in_points, | |
227 &margin_bottom_in_points, | |
228 &margin_left_in_points); | |
229 | 227 |
230 gfx::Size page_size( | 228 gfx::Size page_size( |
231 content_width_in_points + margin_right_in_points + | 229 content_width_in_points + margin_right_in_points + |
232 margin_left_in_points, | 230 margin_left_in_points, |
233 content_height_in_points + margin_top_in_points + | 231 content_height_in_points + margin_top_in_points + |
234 margin_bottom_in_points); | 232 margin_bottom_in_points); |
235 gfx::Rect content_area(margin_left_in_points, margin_top_in_points, | 233 gfx::Rect content_area(margin_left_in_points, margin_top_in_points, |
236 content_width_in_points, content_height_in_points); | 234 content_width_in_points, content_height_in_points); |
237 | 235 |
238 SkDevice* device = metafile->StartPageForVectorCanvas( | 236 SkDevice* device = metafile->StartPageForVectorCanvas( |
239 page_size, content_area, 1.0f); | 237 page_size, content_area, 1.0f); |
240 if (!device) | 238 if (!device) |
241 return; | 239 return; |
242 | 240 |
243 // The printPage method take a reference to the canvas we pass down, so it | 241 // The printPage method take a reference to the canvas we pass down, so it |
244 // can't be a stack object. | 242 // can't be a stack object. |
245 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); | 243 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
246 canvas->unref(); // SkRefPtr and new both took a reference. | 244 canvas->unref(); // SkRefPtr and new both took a reference. |
247 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); | 245 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); |
246 | |
247 if (is_preview && params.params.header_footer) { | |
248 // The page_number count starts from '0', so, we add by '1'. | |
249 // The scale factor on Linux is '1' which is the last parameter. | |
250 PrintHeaderAndFooter(device, canvas, params.page_number+1, total_pages, 1); | |
kmadhusu
2011/07/14 01:50:15
Why the scale factor on linux is '1'?
Aayush Kumar
2011/07/19 01:20:30
The scale factor on linux is not used and is not r
| |
251 } | |
252 | |
248 frame->printPage(params.page_number, canvas.get()); | 253 frame->printPage(params.page_number, canvas.get()); |
249 | 254 |
250 // TODO(myhuang): We should render the header and the footer. | 255 // TODO(myhuang): We should render the header and the footer. |
251 | 256 |
252 // Done printing. Close the device context to retrieve the compiled metafile. | 257 // Done printing. Close the device context to retrieve the compiled metafile. |
253 if (!metafile->FinishPage()) | 258 if (!metafile->FinishPage()) |
254 NOTREACHED() << "metafile failed"; | 259 NOTREACHED() << "metafile failed"; |
255 } | 260 } |
OLD | NEW |