OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/renderer/printing/print_web_view_helper.h" | |
6 | |
7 #if defined(PRINTING_WIN_USES_PDF_AS_METAFILE) | |
Vitaly Buka (NO REVIEWS)
2014/05/13 23:54:33
maybe in GYP file?
scottmg
2014/05/14 00:34:48
Done.
| |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/process/process_handle.h" | |
12 #include "chrome/common/print_messages.h" | |
13 #include "content/public/renderer/render_thread.h" | |
14 #include "printing/metafile.h" | |
15 #include "printing/metafile_impl.h" | |
16 #include "printing/metafile_skia_wrapper.h" | |
17 #include "printing/page_size_margins.h" | |
18 #include "printing/units.h" | |
19 #include "skia/ext/platform_device.h" | |
20 #include "skia/ext/vector_canvas.h" | |
21 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
22 | |
23 | |
24 namespace printing { | |
25 | |
26 using blink::WebFrame; | |
27 | |
28 bool PrintWebViewHelper::RenderPreviewPage( | |
29 int page_number, | |
30 const PrintMsg_Print_Params& print_params) { | |
31 PrintMsg_PrintPage_Params page_params; | |
32 page_params.params = print_params; | |
33 page_params.page_number = page_number; | |
34 scoped_ptr<Metafile> draft_metafile; | |
35 Metafile* initial_render_metafile = print_preview_context_.metafile(); | |
36 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { | |
37 draft_metafile.reset(new PreviewMetafile); | |
38 initial_render_metafile = draft_metafile.get(); | |
39 } | |
40 | |
41 base::TimeTicks begin_time = base::TimeTicks::Now(); | |
42 double actual_shrink = | |
43 static_cast<float>(print_params.desired_dpi / print_params.dpi); | |
44 PrintPageInternal(page_params, | |
45 print_preview_context_.GetPrintCanvasSize(), | |
46 print_preview_context_.prepared_frame(), | |
47 initial_render_metafile, | |
48 true, | |
49 &actual_shrink, | |
50 NULL, | |
51 NULL); | |
52 print_preview_context_.RenderedPreviewPage( | |
53 base::TimeTicks::Now() - begin_time); | |
54 if (draft_metafile.get()) { | |
55 draft_metafile->FinishDocument(); | |
56 } else if (print_preview_context_.IsModifiable() && | |
57 print_preview_context_.generate_draft_pages()) { | |
58 DCHECK(!draft_metafile.get()); | |
59 draft_metafile.reset( | |
60 print_preview_context_.metafile()->GetMetafileForCurrentPage()); | |
61 } | |
62 return PreviewPageRendered(page_number, draft_metafile.get()); | |
63 } | |
64 | |
65 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame, | |
66 int page_count, | |
67 const gfx::Size& canvas_size) { | |
68 NativeMetafile metafile; | |
69 if (!metafile.Init()) | |
70 return false; | |
71 | |
72 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | |
73 std::vector<int> printed_pages; | |
74 std::vector<double> shrink; | |
75 std::vector<gfx::Size> page_size_in_dpi; | |
76 std::vector<gfx::Rect> content_area_in_dpi; | |
77 double dpi_shrink = | |
78 static_cast<float>(params.params.desired_dpi / params.params.dpi); | |
79 | |
80 if (params.pages.empty()) { | |
81 for (int i = 0; i < page_count; ++i) { | |
82 printed_pages.push_back(i); | |
83 } | |
84 } else { | |
85 // TODO(vitalybuka): redesign to make more code cross platform. | |
86 for (size_t i = 0; i < params.pages.size(); ++i) { | |
87 if (params.pages[i] >= 0 && params.pages[i] < page_count) { | |
88 printed_pages.push_back(params.pages[i]); | |
89 } | |
90 } | |
91 } | |
92 if (printed_pages.empty()) | |
93 return false; | |
94 | |
95 for (size_t i = 0; i < printed_pages.size(); ++i) { | |
96 shrink.push_back(dpi_shrink); | |
97 page_size_in_dpi.push_back(gfx::Size()); | |
98 content_area_in_dpi.push_back(gfx::Rect()); | |
99 } | |
100 | |
101 PrintMsg_PrintPage_Params page_params; | |
102 page_params.params = params.params; | |
103 for (size_t i = 0; i < printed_pages.size(); ++i) { | |
104 page_params.page_number = printed_pages[i]; | |
105 PrintPageInternal(page_params, | |
106 canvas_size, | |
107 frame, | |
108 &metafile, | |
109 false, | |
110 &shrink[i], | |
111 &page_size_in_dpi[i], | |
112 &content_area_in_dpi[i]); | |
113 } | |
114 | |
115 // blink::printEnd() for PDF should be called before metafile is closed. | |
116 FinishFramePrinting(); | |
117 | |
118 metafile.FinishDocument(); | |
119 | |
120 // Get the size of the resulting metafile. | |
121 uint32 buf_size = metafile.GetDataSize(); | |
122 DCHECK_GT(buf_size, 0u); | |
123 | |
124 PrintHostMsg_DidPrintPage_Params printed_page_params; | |
125 printed_page_params.data_size = 0; | |
126 printed_page_params.document_cookie = params.params.document_cookie; | |
127 printed_page_params.page_size = params.params.page_size; | |
128 printed_page_params.content_area = params.params.printable_area; | |
129 | |
130 { | |
131 base::SharedMemory shared_buf; | |
132 // Allocate a shared memory buffer to hold the generated metafile data. | |
133 if (!shared_buf.CreateAndMapAnonymous(buf_size)) { | |
134 NOTREACHED() << "Buffer allocation failed"; | |
135 return false; | |
136 } | |
137 | |
138 // Copy the bits into shared memory. | |
139 if (!metafile.GetData(shared_buf.memory(), buf_size)) { | |
140 NOTREACHED() << "GetData() failed"; | |
141 shared_buf.Unmap(); | |
142 return false; | |
143 } | |
144 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), | |
145 &printed_page_params.metafile_data_handle); | |
146 shared_buf.Unmap(); | |
147 | |
148 printed_page_params.data_size = buf_size; | |
149 Send(new PrintHostMsg_DuplicateSection( | |
150 routing_id(), | |
151 printed_page_params.metafile_data_handle, | |
152 &printed_page_params.metafile_data_handle)); | |
153 } | |
154 | |
155 for (size_t i = 0; i < printed_pages.size(); ++i) { | |
156 printed_page_params.page_number = printed_pages[i]; | |
157 printed_page_params.actual_shrink = shrink[i]; | |
158 printed_page_params.page_size = page_size_in_dpi[i]; | |
159 printed_page_params.content_area = content_area_in_dpi[i]; | |
160 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); | |
161 printed_page_params.metafile_data_handle = INVALID_HANDLE_VALUE; | |
162 } | |
163 return true; | |
164 } | |
165 | |
166 void PrintWebViewHelper::PrintPageInternal( | |
167 const PrintMsg_PrintPage_Params& params, | |
168 const gfx::Size& canvas_size, | |
169 WebFrame* frame, | |
170 Metafile* metafile, | |
171 bool is_preview, | |
172 double* actual_shrink, | |
173 gfx::Size* page_size_in_dpi, | |
174 gfx::Rect* content_area_in_dpi) { | |
175 PageSizeMargins page_layout_in_points; | |
176 double css_scale_factor = 1.0f; | |
177 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, | |
178 ignore_css_margins_, &css_scale_factor, | |
179 &page_layout_in_points); | |
180 gfx::Size page_size; | |
181 gfx::Rect content_area; | |
182 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, | |
183 &content_area); | |
184 int dpi = static_cast<int>(params.params.dpi); | |
185 // Calculate the actual page size and content area in dpi. | |
186 if (page_size_in_dpi) { | |
187 *page_size_in_dpi = | |
188 gfx::Size(static_cast<int>(ConvertUnitDouble( | |
189 page_size.width(), kPointsPerInch, dpi)), | |
190 static_cast<int>(ConvertUnitDouble( | |
191 page_size.height(), kPointsPerInch, dpi))); | |
192 } | |
193 | |
194 if (content_area_in_dpi) { | |
195 *content_area_in_dpi = | |
196 gfx::Rect(static_cast<int>( | |
197 ConvertUnitDouble(content_area.x(), kPointsPerInch, dpi)), | |
198 static_cast<int>( | |
199 ConvertUnitDouble(content_area.y(), kPointsPerInch, dpi)), | |
200 static_cast<int>(ConvertUnitDouble( | |
201 content_area.width(), kPointsPerInch, dpi)), | |
202 static_cast<int>(ConvertUnitDouble( | |
203 content_area.height(), kPointsPerInch, dpi))); | |
204 } | |
205 | |
206 if (!is_preview) { | |
207 page_size = | |
208 gfx::Size(static_cast<int>(page_layout_in_points.content_width * | |
209 params.params.max_shrink), | |
210 static_cast<int>(page_layout_in_points.content_height * | |
211 params.params.max_shrink)); | |
212 } | |
213 | |
214 gfx::Rect canvas_area = | |
215 params.params.display_header_footer ? gfx::Rect(page_size) : content_area; | |
216 | |
217 float webkit_page_shrink_factor = | |
218 frame->getPrintPageShrink(params.page_number); | |
219 float scale_factor = css_scale_factor * webkit_page_shrink_factor; | |
220 | |
221 SkBaseDevice* device = metafile->StartPageForVectorCanvas(page_size, | |
222 canvas_area, | |
223 scale_factor); | |
224 if (!device) | |
225 return; | |
226 | |
227 // The printPage method take a reference to the canvas we pass down, so it | |
228 // can't be a stack object. | |
229 skia::RefPtr<skia::VectorCanvas> canvas = | |
230 skia::AdoptRef(new skia::VectorCanvas(device)); | |
231 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | |
232 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | |
233 | |
234 if (params.params.display_header_footer) { | |
235 // |page_number| is 0-based, so 1 is added. | |
236 PrintHeaderAndFooter(canvas.get(), params.page_number + 1, | |
237 print_preview_context_.total_page_count(), | |
238 scale_factor, | |
239 page_layout_in_points, *header_footer_info_, | |
240 params.params); | |
241 } | |
242 | |
243 float webkit_scale_factor = RenderPageContent(frame, | |
244 params.page_number, | |
245 canvas_area, | |
246 content_area, | |
247 scale_factor, | |
248 canvas.get()); | |
249 | |
250 if (*actual_shrink <= 0 || webkit_scale_factor <= 0) { | |
251 NOTREACHED() << "Printing page " << params.page_number << " failed."; | |
252 } else { | |
253 // While rendering certain plugins (PDF) to metafile, we might need to | |
254 // set custom scale factor. Update |actual_shrink| with custom scale | |
255 // if it is set on canvas. | |
256 // TODO(gene): We should revisit this solution for the next versions. | |
257 // Consider creating metafile of the right size (or resizable) | |
258 // https://code.google.com/p/chromium/issues/detail?id=126037 | |
259 if (!MetafileSkiaWrapper::GetCustomScaleOnCanvas( | |
260 *canvas, actual_shrink)) { | |
261 // Update the dpi adjustment with the "page |actual_shrink|" calculated in | |
262 // webkit. | |
263 *actual_shrink /= (webkit_scale_factor * css_scale_factor); | |
264 } | |
265 } | |
266 | |
267 // Done printing. Close the device context to retrieve the compiled metafile. | |
268 if (!metafile->FinishPage()) | |
269 NOTREACHED() << "metafile failed"; | |
270 } | |
271 | |
272 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | |
273 Metafile* metafile, base::SharedMemoryHandle* shared_mem_handle) { | |
274 uint32 buf_size = metafile->GetDataSize(); | |
275 base::SharedMemory shared_buf; | |
276 // Allocate a shared memory buffer to hold the generated metafile data. | |
277 if (!shared_buf.CreateAndMapAnonymous(buf_size)) { | |
278 NOTREACHED() << "Buffer allocation failed"; | |
279 return false; | |
280 } | |
281 | |
282 // Copy the bits into shared memory. | |
283 if (!metafile->GetData(shared_buf.memory(), buf_size)) { | |
284 NOTREACHED() << "GetData() failed"; | |
285 shared_buf.Unmap(); | |
286 return false; | |
287 } | |
288 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle); | |
289 shared_buf.Unmap(); | |
290 | |
291 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle, | |
292 shared_mem_handle)); | |
293 return true; | |
294 } | |
295 | |
296 } // namespace printing | |
297 | |
298 #endif // PRINTING_WIN_USES_PDF_AS_METAFILE | |
OLD | NEW |