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/printing/print_web_view_helper.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/common/print_messages.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "printing/metafile_skia_wrapper.h"
12 #include "printing/page_size_margins.h"
13 #include "printing/pdf_metafile_skia.h"
14 #include "skia/ext/platform_device.h"
15 #include "skia/ext/vector_canvas.h"
16 #include "third_party/WebKit/public/web/WebLocalFrame.h"
18 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
19 #include "base/process/process_handle.h"
21 #include "base/file_descriptor_posix.h"
22 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
26 using blink::WebFrame
;
28 bool PrintWebViewHelper::RenderPreviewPage(
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
<PdfMetafileSkia
> draft_metafile
;
35 PdfMetafileSkia
* initial_render_metafile
= print_preview_context_
.metafile();
36 if (print_preview_context_
.IsModifiable() && is_print_ready_metafile_sent_
) {
37 draft_metafile
.reset(new PdfMetafileSkia
);
38 initial_render_metafile
= draft_metafile
.get();
41 base::TimeTicks begin_time
= base::TimeTicks::Now();
42 PrintPageInternal(page_params
,
43 print_preview_context_
.prepared_frame(),
44 initial_render_metafile
);
45 print_preview_context_
.RenderedPreviewPage(
46 base::TimeTicks::Now() - begin_time
);
47 if (draft_metafile
.get()) {
48 draft_metafile
->FinishDocument();
49 } else if (print_preview_context_
.IsModifiable() &&
50 print_preview_context_
.generate_draft_pages()) {
51 DCHECK(!draft_metafile
.get());
53 print_preview_context_
.metafile()->GetMetafileForCurrentPage();
55 return PreviewPageRendered(page_number
, draft_metafile
.get());
58 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame
* frame
,
60 PdfMetafileSkia metafile
;
64 const PrintMsg_PrintPages_Params
& params
= *print_pages_params_
;
65 std::vector
<int> printed_pages
;
67 if (params
.pages
.empty()) {
68 for (int i
= 0; i
< page_count
; ++i
) {
69 printed_pages
.push_back(i
);
72 // TODO(vitalybuka): redesign to make more code cross platform.
73 for (size_t i
= 0; i
< params
.pages
.size(); ++i
) {
74 if (params
.pages
[i
] >= 0 && params
.pages
[i
] < page_count
) {
75 printed_pages
.push_back(params
.pages
[i
]);
80 if (printed_pages
.empty())
83 PrintMsg_PrintPage_Params page_params
;
84 page_params
.params
= params
.params
;
85 for (size_t i
= 0; i
< printed_pages
.size(); ++i
) {
86 page_params
.page_number
= printed_pages
[i
];
87 PrintPageInternal(page_params
, frame
, &metafile
);
90 // blink::printEnd() for PDF should be called before metafile is closed.
91 FinishFramePrinting();
93 metafile
.FinishDocument();
95 // Get the size of the resulting metafile.
96 uint32 buf_size
= metafile
.GetDataSize();
97 DCHECK_GT(buf_size
, 0u);
99 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
100 int sequence_number
= -1;
101 base::FileDescriptor fd
;
103 // Ask the browser to open a file for us.
104 Send(new PrintHostMsg_AllocateTempFileForPrinting(routing_id(),
107 if (!metafile
.SaveToFD(fd
))
110 // Tell the browser we've finished writing the file.
111 Send(new PrintHostMsg_TempFileForPrintingWritten(routing_id(),
115 PrintHostMsg_DidPrintPage_Params printed_page_params
;
116 printed_page_params
.data_size
= 0;
117 printed_page_params
.document_cookie
= params
.params
.document_cookie
;
120 scoped_ptr
<base::SharedMemory
> shared_mem(
121 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(
122 buf_size
).release());
123 if (!shared_mem
.get()) {
124 NOTREACHED() << "AllocateSharedMemoryBuffer failed";
128 if (!shared_mem
->Map(buf_size
)) {
129 NOTREACHED() << "Map failed";
132 metafile
.GetData(shared_mem
->memory(), buf_size
);
133 printed_page_params
.data_size
= buf_size
;
134 shared_mem
->GiveToProcess(base::GetCurrentProcessHandle(),
135 &(printed_page_params
.metafile_data_handle
));
138 for (size_t i
= 0; i
< printed_pages
.size(); ++i
) {
139 printed_page_params
.page_number
= printed_pages
[i
];
140 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params
));
141 // Send the rest of the pages with an invalid metafile handle.
142 printed_page_params
.metafile_data_handle
.fd
= -1;
145 #endif // defined(OS_CHROMEOS)
148 void PrintWebViewHelper::PrintPageInternal(
149 const PrintMsg_PrintPage_Params
& params
,
151 PdfMetafileSkia
* metafile
) {
152 PageSizeMargins page_layout_in_points
;
153 double scale_factor
= 1.0f
;
154 ComputePageLayoutInPointsForCss(frame
, params
.page_number
, params
.params
,
155 ignore_css_margins_
, &scale_factor
,
156 &page_layout_in_points
);
158 gfx::Rect content_area
;
159 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points
, &page_size
,
161 gfx::Rect canvas_area
=
162 params
.params
.display_header_footer
? gfx::Rect(page_size
) : content_area
;
164 skia::VectorCanvas
* canvas
=
165 metafile
->GetVectorCanvasForNewPage(page_size
, canvas_area
, scale_factor
);
169 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas
, metafile
);
170 skia::SetIsDraftMode(*canvas
, is_print_ready_metafile_sent_
);
172 #if defined(ENABLE_PRINT_PREVIEW)
173 if (params
.params
.display_header_footer
) {
174 // |page_number| is 0-based, so 1 is added.
175 // TODO(vitalybuka) : why does it work only with 1.25?
176 PrintHeaderAndFooter(canvas
, params
.page_number
+ 1,
177 print_preview_context_
.total_page_count(), *frame
,
178 scale_factor
/ 1.25, page_layout_in_points
,
181 #endif // defined(ENABLE_PRINT_PREVIEW)
183 RenderPageContent(frame
, params
.page_number
, canvas_area
, content_area
,
184 scale_factor
, canvas
);
186 // Done printing. Close the canvas to retrieve the compiled metafile.
187 if (!metafile
->FinishPage())
188 NOTREACHED() << "metafile failed";
191 } // namespace printing