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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 if (!CopyMetafileDataToSharedMem(metafile.get(), | 116 if (!CopyMetafileDataToSharedMem(metafile.get(), |
117 &(page_params.metafile_data_handle))) { | 117 &(page_params.metafile_data_handle))) { |
118 page_params.data_size = 0; | 118 page_params.data_size = 0; |
119 } | 119 } |
120 Send(new PrintHostMsg_DuplicateSection(routing_id(), | 120 Send(new PrintHostMsg_DuplicateSection(routing_id(), |
121 page_params.metafile_data_handle, | 121 page_params.metafile_data_handle, |
122 &page_params.metafile_data_handle)); | 122 &page_params.metafile_data_handle)); |
123 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 123 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
124 } | 124 } |
125 | 125 |
126 bool PrintWebViewHelper::CreatePreviewDocument( | 126 void PrintWebViewHelper::RenderPreviewPage(int page_number) { |
127 const PrintMsg_PrintPages_Params& params, WebKit::WebFrame* frame, | 127 PrintMsg_Print_Params print_params = print_preview_context_.print_params(); |
128 WebKit::WebNode* node) { | |
129 if (!PreviewPageRendered(-1)) | |
130 return false; | |
131 | |
132 PrintMsg_Print_Params print_params = params.params; | |
133 UpdatePrintableSizeInPrintParameters(frame, node, &print_params); | |
134 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node, | |
135 frame->view()); | |
136 if (!PreviewPageRendered(-1)) | |
137 return false; | |
138 | |
139 preview_page_count_ = prep_frame_view.GetExpectedPageCount(); | |
140 if (!preview_page_count_) | |
141 return false; | |
142 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), | |
143 preview_page_count_)); | |
144 | |
145 scoped_ptr<Metafile> metafile(new printing::PreviewMetafile); | |
146 metafile->Init(); | |
147 | |
148 // Calculate the dpi adjustment. | 128 // Calculate the dpi adjustment. |
149 float shrink = static_cast<float>(print_params.desired_dpi / | 129 float scale_factor = static_cast<float>(print_params.desired_dpi / |
150 print_params.dpi); | 130 print_params.dpi); |
| 131 // Needed for RenderPage() below. |
| 132 // Not taking ownership with intent to reset(). |
| 133 scoped_ptr<Metafile> metafile(print_preview_context_.metafile()); |
151 | 134 |
152 base::TimeTicks begin_time = base::TimeTicks::Now(); | 135 base::TimeTicks begin_time = base::TimeTicks::Now(); |
153 base::TimeTicks page_begin_time = begin_time; | 136 RenderPage(print_params, &scale_factor, page_number, true, |
154 | 137 print_preview_context_.frame(), &metafile); |
155 if (params.pages.empty()) { | 138 // Release since |print_preview_context_| is the real owner. |
156 for (int i = 0; i < preview_page_count_; ++i) { | 139 metafile.release(); |
157 float scale_factor = shrink; | 140 print_preview_context_.RenderedPreviewPage( |
158 RenderPage(print_params, &scale_factor, i, true, frame, &metafile); | 141 base::TimeTicks::Now() - begin_time); |
159 page_begin_time = ReportPreviewPageRenderTime(page_begin_time); | 142 PreviewPageRendered(page_number); |
160 if (!PreviewPageRendered(i)) | |
161 return false; | |
162 } | |
163 } else { | |
164 for (size_t i = 0; i < params.pages.size(); ++i) { | |
165 if (params.pages[i] >= preview_page_count_) | |
166 break; | |
167 float scale_factor = shrink; | |
168 RenderPage(print_params, &scale_factor, params.pages[i], true, frame, | |
169 &metafile); | |
170 page_begin_time = ReportPreviewPageRenderTime(page_begin_time); | |
171 if (!PreviewPageRendered(params.pages[i])) | |
172 return false; | |
173 } | |
174 } | |
175 | |
176 base::TimeDelta render_time = base::TimeTicks::Now() - begin_time; | |
177 | |
178 // Ensure that printing has finished before we start cleaning up and | |
179 // allocating buffers; this causes prep_frame_view to flush anything pending | |
180 // into the metafile. Then we can get the final size and copy it into a | |
181 // shared segment. | |
182 prep_frame_view.FinishPrinting(); | |
183 | |
184 if (!metafile->FinishDocument()) | |
185 NOTREACHED(); | |
186 | |
187 ReportTotalPreviewGenerationTime(params.pages.size(), | |
188 preview_page_count_, | |
189 render_time, | |
190 base::TimeTicks::Now() - begin_time); | |
191 | |
192 // Get the size of the compiled metafile. | |
193 uint32 buf_size = metafile->GetDataSize(); | |
194 DCHECK_GT(buf_size, 128u); | |
195 | |
196 PrintHostMsg_DidPreviewDocument_Params preview_params; | |
197 preview_params.reuse_existing_data = false; | |
198 preview_params.data_size = buf_size; | |
199 preview_params.document_cookie = params.params.document_cookie; | |
200 preview_params.expected_pages_count = preview_page_count_; | |
201 preview_params.modifiable = IsModifiable(frame, node); | |
202 preview_params.preview_request_id = params.params.preview_request_id; | |
203 | |
204 if (!CopyMetafileDataToSharedMem(metafile.get(), | |
205 &(preview_params.metafile_data_handle))) { | |
206 return false; | |
207 } | |
208 Send(new PrintHostMsg_DuplicateSection(routing_id(), | |
209 preview_params.metafile_data_handle, | |
210 &preview_params.metafile_data_handle)); | |
211 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | |
212 return true; | |
213 } | 143 } |
214 | 144 |
215 void PrintWebViewHelper::RenderPage( | 145 void PrintWebViewHelper::RenderPage( |
216 const PrintMsg_Print_Params& params, float* scale_factor, int page_number, | 146 const PrintMsg_Print_Params& params, float* scale_factor, int page_number, |
217 bool is_preview, WebFrame* frame, scoped_ptr<Metafile>* metafile) { | 147 bool is_preview, WebFrame* frame, scoped_ptr<Metafile>* metafile) { |
218 double content_width_in_points; | 148 double content_width_in_points; |
219 double content_height_in_points; | 149 double content_height_in_points; |
220 double margin_top_in_points; | 150 double margin_top_in_points; |
221 double margin_left_in_points; | 151 double margin_left_in_points; |
222 GetPageSizeAndMarginsInPoints(frame, page_number, params, | 152 GetPageSizeAndMarginsInPoints(frame, page_number, params, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 // Copy the bits into shared memory. | 277 // Copy the bits into shared memory. |
348 if (!metafile->GetData(shared_buf.memory(), buf_size)) { | 278 if (!metafile->GetData(shared_buf.memory(), buf_size)) { |
349 NOTREACHED() << "GetData() failed"; | 279 NOTREACHED() << "GetData() failed"; |
350 shared_buf.Unmap(); | 280 shared_buf.Unmap(); |
351 return false; | 281 return false; |
352 } | 282 } |
353 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | 283 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); |
354 shared_buf.Unmap(); | 284 shared_buf.Unmap(); |
355 return true; | 285 return true; |
356 } | 286 } |
OLD | NEW |