1 // Copyright 2013 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 // TODO(sgurun) copied from chrome/renderer. Remove after crbug.com/322276
7 #include "android_webview/renderer/print_web_view_helper.h"
9 #include "android_webview/common/print_messages.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "content/public/renderer/render_thread.h"
13 #include "printing/metafile.h"
14 #include "printing/metafile_impl.h"
15 #include "printing/metafile_skia_wrapper.h"
16 #include "printing/page_size_margins.h"
17 #include "skia/ext/platform_device.h"
18 #include "skia/ext/vector_canvas.h"
19 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
22 #include "base/process/process_handle.h"
24 #include "base/file_descriptor_posix.h"
25 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
29 using blink::WebFrame
;
31 bool PrintWebViewHelper::RenderPreviewPage(
33 const PrintMsg_Print_Params
& print_params
) {
34 PrintMsg_PrintPage_Params page_params
;
35 page_params
.params
= print_params
;
36 page_params
.page_number
= page_number
;
37 scoped_ptr
<Metafile
> draft_metafile
;
38 Metafile
* initial_render_metafile
= print_preview_context_
.metafile();
39 if (print_preview_context_
.IsModifiable() && is_print_ready_metafile_sent_
) {
40 draft_metafile
.reset(new PreviewMetafile
);
41 initial_render_metafile
= draft_metafile
.get();
44 base::TimeTicks begin_time
= base::TimeTicks::Now();
45 PrintPageInternal(page_params
,
46 print_preview_context_
.GetPrintCanvasSize(),
47 print_preview_context_
.prepared_frame(),
48 initial_render_metafile
);
49 print_preview_context_
.RenderedPreviewPage(
50 base::TimeTicks::Now() - begin_time
);
51 if (draft_metafile
.get()) {
52 draft_metafile
->FinishDocument();
53 } else if (print_preview_context_
.IsModifiable() &&
54 print_preview_context_
.generate_draft_pages()) {
55 DCHECK(!draft_metafile
.get());
57 print_preview_context_
.metafile()->GetMetafileForCurrentPage());
59 return PreviewPageRendered(page_number
, draft_metafile
.get());
62 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame
* frame
,
64 const gfx::Size
& canvas_size
) {
65 NativeMetafile metafile
;
69 const PrintMsg_PrintPages_Params
& params
= *print_pages_params_
;
70 std::vector
<int> printed_pages
;
72 if (params
.pages
.empty()) {
73 for (int i
= 0; i
< page_count
; ++i
) {
74 printed_pages
.push_back(i
);
77 // TODO(vitalybuka): redesign to make more code cross platform.
78 for (size_t i
= 0; i
< params
.pages
.size(); ++i
) {
79 if (params
.pages
[i
] >= 0 && params
.pages
[i
] < page_count
) {
80 printed_pages
.push_back(params
.pages
[i
]);
85 if (printed_pages
.empty())
88 PrintMsg_PrintPage_Params page_params
;
89 page_params
.params
= params
.params
;
90 for (size_t i
= 0; i
< printed_pages
.size(); ++i
) {
91 page_params
.page_number
= printed_pages
[i
];
92 PrintPageInternal(page_params
, canvas_size
, frame
, &metafile
);
95 // blink::printEnd() for PDF should be called before metafile is closed.
96 FinishFramePrinting();
98 metafile
.FinishDocument();
100 // Get the size of the resulting metafile.
101 uint32 buf_size
= metafile
.GetDataSize();
102 DCHECK_GT(buf_size
, 0u);
104 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
105 int sequence_number
= -1;
106 base::FileDescriptor fd
;
108 // Ask the browser to open a file for us.
109 Send(new PrintHostMsg_AllocateTempFileForPrinting(routing_id(),
112 if (!metafile
.SaveToFD(fd
))
115 // Tell the browser we've finished writing the file.
116 Send(new PrintHostMsg_TempFileForPrintingWritten(routing_id(),
120 PrintHostMsg_DidPrintPage_Params printed_page_params
;
121 printed_page_params
.data_size
= 0;
122 printed_page_params
.document_cookie
= params
.params
.document_cookie
;
125 scoped_ptr
<base::SharedMemory
> shared_mem(
126 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(
127 buf_size
).release());
128 if (!shared_mem
.get()) {
129 NOTREACHED() << "AllocateSharedMemoryBuffer failed";
133 if (!shared_mem
->Map(buf_size
)) {
134 NOTREACHED() << "Map failed";
137 metafile
.GetData(shared_mem
->memory(), buf_size
);
138 printed_page_params
.data_size
= buf_size
;
139 shared_mem
->GiveToProcess(base::GetCurrentProcessHandle(),
140 &(printed_page_params
.metafile_data_handle
));
143 for (size_t i
= 0; i
< printed_pages
.size(); ++i
) {
144 printed_page_params
.page_number
= printed_pages
[i
];
145 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params
));
146 // Send the rest of the pages with an invalid metafile handle.
147 printed_page_params
.metafile_data_handle
.fd
= -1;
150 #endif // defined(OS_CHROMEOS) || defined(OS_ANDROID)
153 void PrintWebViewHelper::PrintPageInternal(
154 const PrintMsg_PrintPage_Params
& params
,
155 const gfx::Size
& canvas_size
,
157 Metafile
* metafile
) {
158 PageSizeMargins page_layout_in_points
;
159 double scale_factor
= 1.0f
;
160 ComputePageLayoutInPointsForCss(frame
, params
.page_number
, params
.params
,
161 ignore_css_margins_
, &scale_factor
,
162 &page_layout_in_points
);
164 gfx::Rect content_area
;
165 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points
, &page_size
,
167 gfx::Rect canvas_area
=
168 params
.params
.display_header_footer
? gfx::Rect(page_size
) : content_area
;
170 SkBaseDevice
* device
= metafile
->StartPageForVectorCanvas(page_size
,
176 // The printPage method take a reference to the canvas we pass down, so it
177 // can't be a stack object.
178 skia::RefPtr
<skia::VectorCanvas
> canvas
=
179 skia::AdoptRef(new skia::VectorCanvas(device
));
180 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas
, metafile
);
181 skia::SetIsDraftMode(*canvas
, is_print_ready_metafile_sent_
);
183 if (params
.params
.display_header_footer
) {
184 // |page_number| is 0-based, so 1 is added.
185 // TODO(vitalybuka) : why does it work only with 1.25?
186 PrintHeaderAndFooter(canvas
.get(), params
.page_number
+ 1,
187 print_preview_context_
.total_page_count(),
189 page_layout_in_points
, *header_footer_info_
,
192 RenderPageContent(frame
, params
.page_number
, canvas_area
, content_area
,
193 scale_factor
, canvas
.get());
195 // Done printing. Close the device context to retrieve the compiled metafile.
196 if (!metafile
->FinishPage())
197 NOTREACHED() << "metafile failed";
200 } // namespace printing