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 "components/printing/renderer/print_web_view_helper.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "components/printing/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 "third_party/WebKit/public/web/WebLocalFrame.h"
17 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
18 #include "base/process/process_handle.h"
20 #include "base/file_descriptor_posix.h"
21 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
25 using blink::WebFrame
;
27 bool PrintWebViewHelper::RenderPreviewPage(
29 const PrintMsg_Print_Params
& print_params
) {
30 PrintMsg_PrintPage_Params page_params
;
31 page_params
.params
= print_params
;
32 page_params
.page_number
= page_number
;
33 scoped_ptr
<PdfMetafileSkia
> draft_metafile
;
34 PdfMetafileSkia
* initial_render_metafile
= print_preview_context_
.metafile();
35 if (print_preview_context_
.IsModifiable() && is_print_ready_metafile_sent_
) {
36 draft_metafile
.reset(new PdfMetafileSkia
);
37 initial_render_metafile
= draft_metafile
.get();
40 base::TimeTicks begin_time
= base::TimeTicks::Now();
41 PrintPageInternal(page_params
,
42 print_preview_context_
.prepared_frame(),
43 initial_render_metafile
);
44 print_preview_context_
.RenderedPreviewPage(
45 base::TimeTicks::Now() - begin_time
);
46 if (draft_metafile
.get()) {
47 draft_metafile
->FinishDocument();
48 } else if (print_preview_context_
.IsModifiable() &&
49 print_preview_context_
.generate_draft_pages()) {
50 DCHECK(!draft_metafile
.get());
52 print_preview_context_
.metafile()->GetMetafileForCurrentPage();
54 return PreviewPageRendered(page_number
, draft_metafile
.get());
57 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame
* frame
,
59 PdfMetafileSkia metafile
;
63 const PrintMsg_PrintPages_Params
& params
= *print_pages_params_
;
64 std::vector
<int> printed_pages
;
66 if (params
.pages
.empty()) {
67 for (int i
= 0; i
< page_count
; ++i
) {
68 printed_pages
.push_back(i
);
71 // TODO(vitalybuka): redesign to make more code cross platform.
72 for (size_t i
= 0; i
< params
.pages
.size(); ++i
) {
73 if (params
.pages
[i
] >= 0 && params
.pages
[i
] < page_count
) {
74 printed_pages
.push_back(params
.pages
[i
]);
79 if (printed_pages
.empty())
82 PrintMsg_PrintPage_Params page_params
;
83 page_params
.params
= params
.params
;
84 for (size_t i
= 0; i
< printed_pages
.size(); ++i
) {
85 page_params
.page_number
= printed_pages
[i
];
86 PrintPageInternal(page_params
, frame
, &metafile
);
89 // blink::printEnd() for PDF should be called before metafile is closed.
90 FinishFramePrinting();
92 metafile
.FinishDocument();
94 // Get the size of the resulting metafile.
95 uint32 buf_size
= metafile
.GetDataSize();
96 DCHECK_GT(buf_size
, 0u);
98 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
99 int sequence_number
= -1;
100 base::FileDescriptor fd
;
102 // Ask the browser to open a file for us.
103 Send(new PrintHostMsg_AllocateTempFileForPrinting(routing_id(),
106 if (!metafile
.SaveToFD(fd
))
109 // Tell the browser we've finished writing the file.
110 Send(new PrintHostMsg_TempFileForPrintingWritten(routing_id(),
114 PrintHostMsg_DidPrintPage_Params printed_page_params
;
115 printed_page_params
.data_size
= 0;
116 printed_page_params
.document_cookie
= params
.params
.document_cookie
;
119 scoped_ptr
<base::SharedMemory
> shared_mem(
120 content::RenderThread::Get()
121 ->HostAllocateSharedMemoryBuffer(buf_size
)
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::PlatformCanvas
* 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