Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / chrome / renderer / printing / print_web_view_helper_linux.cc
blobae862d9528a9feccef66056fc936f9dd3b2745eb
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"
20 #else
21 #include "base/file_descriptor_posix.h"
22 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
24 namespace printing {
26 using blink::WebFrame;
28 bool PrintWebViewHelper::RenderPreviewPage(
29 int page_number,
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());
52 draft_metafile =
53 print_preview_context_.metafile()->GetMetafileForCurrentPage();
55 return PreviewPageRendered(page_number, draft_metafile.get());
58 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
59 int page_count) {
60 PdfMetafileSkia metafile;
61 if (!metafile.Init())
62 return false;
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);
71 } else {
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())
81 return false;
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(),
105 &fd,
106 &sequence_number));
107 if (!metafile.SaveToFD(fd))
108 return false;
110 // Tell the browser we've finished writing the file.
111 Send(new PrintHostMsg_TempFileForPrintingWritten(routing_id(),
112 sequence_number));
113 return true;
114 #else
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";
125 return false;
128 if (!shared_mem->Map(buf_size)) {
129 NOTREACHED() << "Map failed";
130 return false;
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;
144 return true;
145 #endif // defined(OS_CHROMEOS)
148 void PrintWebViewHelper::PrintPageInternal(
149 const PrintMsg_PrintPage_Params& params,
150 WebFrame* frame,
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);
157 gfx::Size page_size;
158 gfx::Rect content_area;
159 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
160 &content_area);
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);
166 if (!canvas)
167 return;
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,
179 params.params);
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