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)
101 #elif defined(OS_ANDROID)
102 int sequence_number
= -1;
103 base::FileDescriptor fd
;
105 // Ask the browser to open a file for us.
106 Send(new PrintHostMsg_AllocateTempFileForPrinting(routing_id(),
109 if (!metafile
.SaveToFD(fd
))
112 // Tell the browser we've finished writing the file.
113 Send(new PrintHostMsg_TempFileForPrintingWritten(routing_id(),
117 PrintHostMsg_DidPrintPage_Params printed_page_params
;
118 printed_page_params
.data_size
= 0;
119 printed_page_params
.document_cookie
= params
.params
.document_cookie
;
122 scoped_ptr
<base::SharedMemory
> shared_mem(
123 content::RenderThread::Get()
124 ->HostAllocateSharedMemoryBuffer(buf_size
)
126 if (!shared_mem
.get()) {
127 NOTREACHED() << "AllocateSharedMemoryBuffer failed";
131 if (!shared_mem
->Map(buf_size
)) {
132 NOTREACHED() << "Map failed";
135 metafile
.GetData(shared_mem
->memory(), buf_size
);
136 printed_page_params
.data_size
= buf_size
;
137 shared_mem
->GiveToProcess(base::GetCurrentProcessHandle(),
138 &(printed_page_params
.metafile_data_handle
));
141 for (size_t i
= 0; i
< printed_pages
.size(); ++i
) {
142 printed_page_params
.page_number
= printed_pages
[i
];
143 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params
));
144 // Send the rest of the pages with an invalid metafile handle.
145 printed_page_params
.metafile_data_handle
.fd
= -1;
148 #endif // defined(OS_CHROMEOS)
151 void PrintWebViewHelper::PrintPageInternal(
152 const PrintMsg_PrintPage_Params
& params
,
154 PdfMetafileSkia
* metafile
) {
155 PageSizeMargins page_layout_in_points
;
156 double scale_factor
= 1.0f
;
157 ComputePageLayoutInPointsForCss(frame
, params
.page_number
, params
.params
,
158 ignore_css_margins_
, &scale_factor
,
159 &page_layout_in_points
);
161 gfx::Rect content_area
;
162 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points
, &page_size
,
164 gfx::Rect canvas_area
=
165 params
.params
.display_header_footer
? gfx::Rect(page_size
) : content_area
;
167 skia::PlatformCanvas
* canvas
=
168 metafile
->GetVectorCanvasForNewPage(page_size
, canvas_area
, scale_factor
);
172 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas
, metafile
);
173 skia::SetIsDraftMode(*canvas
, is_print_ready_metafile_sent_
);
175 #if defined(ENABLE_PRINT_PREVIEW)
176 if (params
.params
.display_header_footer
) {
177 // |page_number| is 0-based, so 1 is added.
178 // TODO(vitalybuka) : why does it work only with 1.25?
179 PrintHeaderAndFooter(canvas
, params
.page_number
+ 1,
180 print_preview_context_
.total_page_count(), *frame
,
181 scale_factor
/ 1.25, page_layout_in_points
,
184 #endif // defined(ENABLE_PRINT_PREVIEW)
186 RenderPageContent(frame
, params
.page_number
, canvas_area
, content_area
,
187 scale_factor
, canvas
);
189 // Done printing. Close the canvas to retrieve the compiled metafile.
190 if (!metafile
->FinishPage())
191 NOTREACHED() << "metafile failed";
194 } // namespace printing