1 // Copyright (c) 2012 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.
5 #include "chrome/renderer/print_web_view_helper.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/histogram.h"
10 #include "base/process_util.h"
11 #include "base/win/scoped_gdi_object.h"
12 #include "base/win/scoped_hdc.h"
13 #include "base/win/scoped_select_object.h"
14 #include "chrome/common/print_messages.h"
15 #include "printing/metafile.h"
16 #include "printing/metafile_impl.h"
17 #include "printing/metafile_skia_wrapper.h"
18 #include "printing/page_size_margins.h"
19 #include "printing/units.h"
20 #include "skia/ext/platform_device.h"
21 #include "skia/ext/refptr.h"
22 #include "skia/ext/vector_canvas.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
24 #include "ui/gfx/gdi_util.h"
25 #include "ui/gfx/point.h"
26 #include "ui/gfx/rect.h"
27 #include "ui/gfx/size.h"
29 using printing::ConvertUnit
;
30 using printing::ConvertUnitDouble
;
31 using printing::kPointsPerInch
;
32 using printing::Metafile
;
33 using WebKit::WebFrame
;
35 void PrintWebViewHelper::PrintPageInternal(
36 const PrintMsg_PrintPage_Params
& params
,
37 const gfx::Size
& canvas_size
,
39 // Generate a memory-based metafile. It will use the current screen's DPI.
40 // Each metafile contains a single page.
41 scoped_ptr
<printing::NativeMetafile
> metafile(new printing::NativeMetafile
);
43 DCHECK(metafile
->context());
44 skia::InitializeDC(metafile
->context());
46 int page_number
= params
.page_number
;
48 // Calculate the dpi adjustment.
49 // Browser will render context using desired_dpi, so we need to calculate
50 // adjustment factor to play content on the printer DC later during the
52 double actual_shrink
= static_cast<float>(params
.params
.desired_dpi
/
54 gfx::Size page_size_in_dpi
;
55 gfx::Rect content_area_in_dpi
;
57 // Render page for printing.
58 RenderPage(params
.params
, page_number
, frame
, false, metafile
.get(),
59 &actual_shrink
, &page_size_in_dpi
, &content_area_in_dpi
);
61 // Close the device context to retrieve the compiled metafile.
62 if (!metafile
->FinishDocument())
65 if (!params
.params
.supports_alpha_blend
&& metafile
->IsAlphaBlendUsed()) {
66 scoped_ptr
<printing::NativeMetafile
> raster_metafile(
67 metafile
->RasterizeAlphaBlend());
68 if (raster_metafile
.get())
69 metafile
.swap(raster_metafile
);
72 // Get the size of the compiled metafile.
73 uint32 buf_size
= metafile
->GetDataSize();
74 DCHECK_GT(buf_size
, 128u);
76 PrintHostMsg_DidPrintPage_Params page_params
;
77 page_params
.data_size
= buf_size
;
78 page_params
.metafile_data_handle
= NULL
;
79 page_params
.page_number
= page_number
;
80 page_params
.document_cookie
= params
.params
.document_cookie
;
81 page_params
.actual_shrink
= actual_shrink
;
82 page_params
.page_size
= page_size_in_dpi
;
83 page_params
.content_area
= content_area_in_dpi
;
85 if (!CopyMetafileDataToSharedMem(metafile
.get(),
86 &(page_params
.metafile_data_handle
))) {
87 page_params
.data_size
= 0;
90 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params
));
93 bool PrintWebViewHelper::RenderPreviewPage(
95 const PrintMsg_Print_Params
& print_params
) {
96 // Calculate the dpi adjustment.
97 double actual_shrink
= static_cast<float>(print_params
.desired_dpi
/
99 scoped_ptr
<Metafile
> draft_metafile
;
100 printing::Metafile
* initial_render_metafile
=
101 print_preview_context_
.metafile();
103 if (print_preview_context_
.IsModifiable() && is_print_ready_metafile_sent_
) {
104 draft_metafile
.reset(new printing::PreviewMetafile
);
105 initial_render_metafile
= draft_metafile
.get();
108 base::TimeTicks begin_time
= base::TimeTicks::Now();
109 RenderPage(print_params
, page_number
, print_preview_context_
.frame(), true,
110 initial_render_metafile
, &actual_shrink
, NULL
, NULL
);
111 print_preview_context_
.RenderedPreviewPage(
112 base::TimeTicks::Now() - begin_time
);
114 if (draft_metafile
.get()) {
115 draft_metafile
->FinishDocument();
116 } else if (print_preview_context_
.IsModifiable() &&
117 print_preview_context_
.generate_draft_pages()) {
118 DCHECK(!draft_metafile
.get());
119 draft_metafile
.reset(
120 print_preview_context_
.metafile()->GetMetafileForCurrentPage());
122 return PreviewPageRendered(page_number
, draft_metafile
.get());
125 void PrintWebViewHelper::RenderPage(
126 const PrintMsg_Print_Params
& params
, int page_number
, WebFrame
* frame
,
127 bool is_preview
, Metafile
* metafile
, double* actual_shrink
,
128 gfx::Size
* page_size_in_dpi
, gfx::Rect
* content_area_in_dpi
) {
129 printing::PageSizeMargins page_layout_in_points
;
130 double css_scale_factor
= 1.0f
;
131 ComputePageLayoutInPointsForCss(frame
, page_number
, params
,
132 ignore_css_margins_
, &css_scale_factor
,
133 &page_layout_in_points
);
135 gfx::Rect content_area
;
136 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points
, &page_size
,
138 int dpi
= static_cast<int>(params
.dpi
);
139 // Calculate the actual page size and content area in dpi.
140 if (page_size_in_dpi
) {
141 *page_size_in_dpi
= gfx::Size(
142 static_cast<int>(ConvertUnitDouble(
143 page_size
.width(), printing::kPointsPerInch
, dpi
)),
144 static_cast<int>(ConvertUnitDouble(
145 page_size
.height(), printing::kPointsPerInch
, dpi
)));
148 if (content_area_in_dpi
) {
149 *content_area_in_dpi
= gfx::Rect(
150 static_cast<int>(ConvertUnitDouble(content_area
.x(),
151 printing::kPointsPerInch
, dpi
)),
152 static_cast<int>(ConvertUnitDouble(content_area
.y(),
153 printing::kPointsPerInch
, dpi
)),
154 static_cast<int>(ConvertUnitDouble(content_area
.width(),
155 printing::kPointsPerInch
, dpi
)),
156 static_cast<int>(ConvertUnitDouble(content_area
.height(),
157 printing::kPointsPerInch
, dpi
)));
161 // Since WebKit extends the page width depending on the magical scale factor
162 // we make sure the canvas covers the worst case scenario (x2.0 currently).
163 // PrintContext will then set the correct clipping region.
164 page_size
= gfx::Size(
165 static_cast<int>(page_layout_in_points
.content_width
*
167 static_cast<int>(page_layout_in_points
.content_height
*
171 float webkit_page_shrink_factor
= frame
->getPrintPageShrink(page_number
);
172 float scale_factor
= css_scale_factor
* webkit_page_shrink_factor
;
174 gfx::Rect canvas_area
=
175 params
.display_header_footer
? gfx::Rect(page_size
) : content_area
;
177 SkDevice
* device
= metafile
->StartPageForVectorCanvas(
178 page_size
, canvas_area
, scale_factor
);
180 // The printPage method may take a reference to the canvas we pass down, so it
181 // can't be a stack object.
182 skia::RefPtr
<skia::VectorCanvas
> canvas
=
183 skia::AdoptRef(new skia::VectorCanvas(device
));
186 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas
, metafile
);
187 skia::SetIsDraftMode(*canvas
, is_print_ready_metafile_sent_
);
188 skia::SetIsPreviewMetafile(*canvas
, is_preview
);
191 if (params
.display_header_footer
) {
192 // |page_number| is 0-based, so 1 is added.
193 PrintHeaderAndFooter(canvas
.get(), page_number
+ 1,
194 print_preview_context_
.total_page_count(), scale_factor
,
195 page_layout_in_points
, *header_footer_info_
, params
);
198 float webkit_scale_factor
= RenderPageContent(frame
, page_number
, canvas_area
,
199 content_area
, scale_factor
,
202 if (*actual_shrink
<= 0 || webkit_scale_factor
<= 0) {
203 NOTREACHED() << "Printing page " << page_number
<< " failed.";
205 // While rendering certain plugins (PDF) to metafile, we might need to
206 // set custom scale factor. Update |actual_shrink| with custom scale
207 // if it is set on canvas.
208 // TODO(gene): We should revisit this solution for the next versions.
209 // Consider creating metafile of the right size (or resizable)
210 // https://code.google.com/p/chromium/issues/detail?id=126037
211 if (!printing::MetafileSkiaWrapper::GetCustomScaleOnCanvas(
212 *canvas
, actual_shrink
)) {
213 // Update the dpi adjustment with the "page |actual_shrink|" calculated in
215 *actual_shrink
/= (webkit_scale_factor
* css_scale_factor
);
219 bool result
= metafile
->FinishPage();
223 bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
224 Metafile
* metafile
, base::SharedMemoryHandle
* shared_mem_handle
) {
225 uint32 buf_size
= metafile
->GetDataSize();
226 base::SharedMemory shared_buf
;
227 if (buf_size
>= printing::kMetafileMaxSize
) {
228 NOTREACHED() << "Buffer too large: " << buf_size
;
232 // Allocate a shared memory buffer to hold the generated metafile data.
233 if (!shared_buf
.CreateAndMapAnonymous(buf_size
)) {
234 NOTREACHED() << "Buffer allocation failed";
238 // Copy the bits into shared memory.
239 if (!metafile
->GetData(shared_buf
.memory(), buf_size
)) {
240 NOTREACHED() << "GetData() failed";
244 shared_buf
.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle
);
247 Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle
,