Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / printing / renderer / print_web_view_helper_linux.cc
blob52f92ec78d106c46578aa9b04a976cb0696b3855
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"
19 #else
20 #include "base/file_descriptor_posix.h"
21 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
23 namespace printing {
25 using blink::WebFrame;
27 bool PrintWebViewHelper::RenderPreviewPage(
28 int page_number,
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());
51 draft_metafile =
52 print_preview_context_.metafile()->GetMetafileForCurrentPage();
54 return PreviewPageRendered(page_number, draft_metafile.get());
57 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
58 int page_count) {
59 PdfMetafileSkia metafile;
60 if (!metafile.Init())
61 return false;
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);
70 } else {
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())
80 return false;
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 #if defined(OS_CHROMEOS)
95 NOTREACHED();
96 return false;
97 #elif defined(OS_ANDROID)
98 int sequence_number = -1;
99 base::FileDescriptor fd;
101 // Ask the browser to open a file for us.
102 Send(new PrintHostMsg_AllocateTempFileForPrinting(routing_id(),
103 &fd,
104 &sequence_number));
105 if (!metafile.SaveToFD(fd))
106 return false;
108 // Tell the browser we've finished writing the file.
109 Send(new PrintHostMsg_TempFileForPrintingWritten(routing_id(),
110 sequence_number));
111 return true;
112 #else
113 PrintHostMsg_DidPrintPage_Params printed_page_params;
115 if (!CopyMetafileDataToSharedMem(
116 metafile, &printed_page_params.metafile_data_handle)) {
117 return false;
120 printed_page_params.data_size = metafile.GetDataSize();
121 printed_page_params.document_cookie = params.params.document_cookie;
123 for (size_t i = 0; i < printed_pages.size(); ++i) {
124 printed_page_params.page_number = printed_pages[i];
125 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params));
126 // Send the rest of the pages with an invalid metafile handle.
127 printed_page_params.metafile_data_handle.fd = -1;
129 return true;
130 #endif // defined(OS_CHROMEOS)
133 void PrintWebViewHelper::PrintPageInternal(
134 const PrintMsg_PrintPage_Params& params,
135 WebFrame* frame,
136 PdfMetafileSkia* metafile) {
137 PageSizeMargins page_layout_in_points;
138 double scale_factor = 1.0f;
139 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
140 ignore_css_margins_, &scale_factor,
141 &page_layout_in_points);
142 gfx::Size page_size;
143 gfx::Rect content_area;
144 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
145 &content_area);
146 gfx::Rect canvas_area =
147 params.params.display_header_footer ? gfx::Rect(page_size) : content_area;
149 skia::PlatformCanvas* canvas =
150 metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor);
151 if (!canvas)
152 return;
154 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile);
155 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_);
157 #if defined(ENABLE_PRINT_PREVIEW)
158 if (params.params.display_header_footer) {
159 // |page_number| is 0-based, so 1 is added.
160 // TODO(vitalybuka) : why does it work only with 1.25?
161 PrintHeaderAndFooter(canvas, params.page_number + 1,
162 print_preview_context_.total_page_count(), *frame,
163 scale_factor / 1.25, page_layout_in_points,
164 params.params);
166 #endif // defined(ENABLE_PRINT_PREVIEW)
168 RenderPageContent(frame, params.page_number, canvas_area, content_area,
169 scale_factor, canvas);
171 // Done printing. Close the canvas to retrieve the compiled metafile.
172 if (!metafile->FinishPage())
173 NOTREACHED() << "metafile failed";
176 } // namespace printing