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 "third_party/skia/include/core/SkRefCnt.h" | 17 #include "third_party/skia/include/core/SkRefCnt.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
19 #include "ui/gfx/point.h" | 19 #include "ui/gfx/point.h" |
20 | 20 |
21 #if !defined(OS_CHROMEOS) | 21 #if !defined(OS_CHROMEOS) |
22 #include "base/process_util.h" | 22 #include "base/process_util.h" |
23 #endif // !defined(OS_CHROMEOS) | 23 #endif // !defined(OS_CHROMEOS) |
24 | 24 |
25 using WebKit::WebFrame; | 25 using WebKit::WebFrame; |
26 using WebKit::WebNode; | 26 using WebKit::WebNode; |
27 | 27 |
28 bool PrintWebViewHelper::CreatePreviewDocument( | 28 void PrintWebViewHelper::RenderPreviewPage(int page_number) { |
29 const PrintMsg_PrintPages_Params& params, WebKit::WebFrame* frame, | 29 PrintMsg_PrintPage_Params page_params; |
30 WebKit::WebNode* node) { | 30 page_params.params = print_preview_context_.print_params(); |
31 if (!PreviewPageRendered(-1)) | 31 page_params.page_number = page_number; |
32 return false; | |
33 | 32 |
34 printing::PreviewMetafile metafile; | 33 base::TimeTicks begin_time = base::TimeTicks::Now(); |
35 if (!metafile.Init()) | 34 PrintPageInternal(page_params, |
36 return false; | 35 print_preview_context_.GetPrintCanvasSize(), |
37 | 36 print_preview_context_.frame(), |
38 preview_page_count_ = 0; | 37 print_preview_context_.metafile()); |
39 if (!RenderPages(params, frame, node, &preview_page_count_, &metafile, true)) | 38 print_preview_context_.RenderedPreviewPage( |
40 return false; | 39 base::TimeTicks::Now() - begin_time); |
41 | 40 PreviewPageRendered(page_number); |
42 // Get the size of the resulting metafile. | |
43 uint32 buf_size = metafile.GetDataSize(); | |
44 DCHECK_GT(buf_size, 0u); | |
45 | |
46 PrintHostMsg_DidPreviewDocument_Params preview_params; | |
47 preview_params.reuse_existing_data = false; | |
48 preview_params.data_size = buf_size; | |
49 preview_params.document_cookie = params.params.document_cookie; | |
50 preview_params.expected_pages_count = preview_page_count_; | |
51 preview_params.modifiable = IsModifiable(frame, node); | |
52 preview_params.preview_request_id = params.params.preview_request_id; | |
53 | |
54 if (!CopyMetafileDataToSharedMem(&metafile, | |
55 &(preview_params.metafile_data_handle))) { | |
56 return false; | |
57 } | |
58 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | |
59 return true; | |
60 } | 41 } |
61 | 42 |
62 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, | 43 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, |
63 WebFrame* frame, | 44 WebFrame* frame, |
64 WebNode* node) { | 45 WebNode* node) { |
65 printing::NativeMetafile metafile; | 46 printing::NativeMetafile metafile; |
66 if (!metafile.Init()) | 47 if (!metafile.Init()) |
67 return false; | 48 return false; |
68 | 49 |
69 int page_count = 0; | 50 int page_count = 0; |
70 if (!RenderPages(params, frame, node, &page_count, &metafile, false)) | 51 if (!RenderPages(params, frame, node, &page_count, &metafile)) |
71 return false; | 52 return false; |
72 | 53 |
73 // Get the size of the resulting metafile. | 54 // Get the size of the resulting metafile. |
74 uint32 buf_size = metafile.GetDataSize(); | 55 uint32 buf_size = metafile.GetDataSize(); |
75 DCHECK_GT(buf_size, 0u); | 56 DCHECK_GT(buf_size, 0u); |
76 | 57 |
77 #if defined(OS_CHROMEOS) | 58 #if defined(OS_CHROMEOS) |
78 int sequence_number = -1; | 59 int sequence_number = -1; |
79 base::FileDescriptor fd; | 60 base::FileDescriptor fd; |
80 | 61 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 } | 116 } |
136 } | 117 } |
137 return true; | 118 return true; |
138 #endif // defined(OS_CHROMEOS) | 119 #endif // defined(OS_CHROMEOS) |
139 } | 120 } |
140 | 121 |
141 bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, | 122 bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, |
142 WebKit::WebFrame* frame, | 123 WebKit::WebFrame* frame, |
143 WebKit::WebNode* node, | 124 WebKit::WebNode* node, |
144 int* page_count, | 125 int* page_count, |
145 printing::Metafile* metafile, | 126 printing::Metafile* metafile) { |
146 bool is_preview) { | |
147 PrintMsg_Print_Params printParams = params.params; | 127 PrintMsg_Print_Params printParams = params.params; |
148 | 128 |
149 UpdatePrintableSizeInPrintParameters(frame, node, &printParams); | 129 UpdatePrintableSizeInPrintParameters(frame, node, &printParams); |
150 | 130 |
151 PrepareFrameAndViewForPrint prep_frame_view(printParams, frame, node, | 131 PrepareFrameAndViewForPrint prep_frame_view(printParams, frame, node); |
152 frame->view()); | |
153 if (is_preview && !PreviewPageRendered(-1)) | |
154 return false; | |
155 | |
156 *page_count = prep_frame_view.GetExpectedPageCount(); | 132 *page_count = prep_frame_view.GetExpectedPageCount(); |
157 if (!*page_count) | 133 if (!*page_count) |
158 return false; | 134 return false; |
159 if (is_preview) { | 135 |
160 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), *page_count)); | |
161 } else { | |
162 #if !defined(OS_CHROMEOS) | 136 #if !defined(OS_CHROMEOS) |
163 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 137 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
164 printParams.document_cookie, | 138 printParams.document_cookie, |
165 *page_count)); | 139 *page_count)); |
166 #endif | 140 #endif |
167 } | |
168 | 141 |
169 base::TimeTicks begin_time = base::TimeTicks::Now(); | 142 base::TimeTicks begin_time = base::TimeTicks::Now(); |
170 base::TimeTicks page_begin_time = begin_time; | 143 base::TimeTicks page_begin_time = begin_time; |
171 | 144 |
172 PrintMsg_PrintPage_Params page_params; | 145 PrintMsg_PrintPage_Params page_params; |
173 page_params.params = printParams; | 146 page_params.params = printParams; |
174 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 147 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
175 if (params.pages.empty()) { | 148 if (params.pages.empty()) { |
176 for (int i = 0; i < *page_count; ++i) { | 149 for (int i = 0; i < *page_count; ++i) { |
177 page_params.page_number = i; | 150 page_params.page_number = i; |
178 PrintPageInternal(page_params, canvas_size, frame, metafile); | 151 PrintPageInternal(page_params, canvas_size, frame, metafile); |
179 if (is_preview) { | |
180 page_begin_time = ReportPreviewPageRenderTime(page_begin_time); | |
181 if (!PreviewPageRendered(i)) | |
182 return false; | |
183 } | |
184 } | 152 } |
185 } else { | 153 } else { |
186 for (size_t i = 0; i < params.pages.size(); ++i) { | 154 for (size_t i = 0; i < params.pages.size(); ++i) { |
187 page_params.page_number = params.pages[i]; | 155 page_params.page_number = params.pages[i]; |
188 PrintPageInternal(page_params, canvas_size, frame, metafile); | 156 PrintPageInternal(page_params, canvas_size, frame, metafile); |
189 if (is_preview) { | |
190 page_begin_time = ReportPreviewPageRenderTime(page_begin_time); | |
191 if (!PreviewPageRendered(params.pages[i])) | |
192 return false; | |
193 } | |
194 } | 157 } |
195 } | 158 } |
196 | 159 |
197 base::TimeDelta render_time = base::TimeTicks::Now() - begin_time; | 160 base::TimeDelta render_time = base::TimeTicks::Now() - begin_time; |
198 | 161 |
199 prep_frame_view.FinishPrinting(); | 162 prep_frame_view.FinishPrinting(); |
200 metafile->FinishDocument(); | 163 metafile->FinishDocument(); |
201 | |
202 if (is_preview) { | |
203 ReportTotalPreviewGenerationTime(params.pages.size(), *page_count, | |
204 render_time, | |
205 base::TimeTicks::Now() - begin_time); | |
206 } | |
207 return true; | 164 return true; |
208 } | 165 } |
209 | 166 |
210 void PrintWebViewHelper::PrintPageInternal( | 167 void PrintWebViewHelper::PrintPageInternal( |
211 const PrintMsg_PrintPage_Params& params, | 168 const PrintMsg_PrintPage_Params& params, |
212 const gfx::Size& canvas_size, | 169 const gfx::Size& canvas_size, |
213 WebFrame* frame, | 170 WebFrame* frame, |
214 printing::Metafile* metafile) { | 171 printing::Metafile* metafile) { |
215 double content_width_in_points; | 172 double content_width_in_points; |
216 double content_height_in_points; | 173 double content_height_in_points; |
(...skipping 30 matching lines...) Expand all Loading... |
247 canvas->unref(); // SkRefPtr and new both took a reference. | 204 canvas->unref(); // SkRefPtr and new both took a reference. |
248 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); | 205 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); |
249 frame->printPage(params.page_number, canvas.get()); | 206 frame->printPage(params.page_number, canvas.get()); |
250 | 207 |
251 // TODO(myhuang): We should render the header and the footer. | 208 // TODO(myhuang): We should render the header and the footer. |
252 | 209 |
253 // Done printing. Close the device context to retrieve the compiled metafile. | 210 // Done printing. Close the device context to retrieve the compiled metafile. |
254 if (!metafile->FinishPage()) | 211 if (!metafile->FinishPage()) |
255 NOTREACHED() << "metafile failed"; | 212 NOTREACHED() << "metafile failed"; |
256 } | 213 } |
OLD | NEW |