OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
6 | 6 |
7 #include "base/file_descriptor_posix.h" | 7 #include "base/file_descriptor_posix.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "chrome/common/print_messages.h" | 10 #include "chrome/common/print_messages.h" |
11 #include "content/common/view_messages.h" | 11 #include "content/common/view_messages.h" |
12 #include "printing/native_metafile_factory.h" | 12 #include "printing/metafile.h" |
13 #include "printing/native_metafile_skia_wrapper.h" | 13 #include "printing/metafile_impl.h" |
14 #include "printing/native_metafile.h" | 14 #include "printing/metafile_skia_wrapper.h" |
15 #include "skia/ext/vector_canvas.h" | 15 #include "skia/ext/vector_canvas.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
17 #include "ui/gfx/point.h" | 17 #include "ui/gfx/point.h" |
18 | 18 |
19 #if !defined(OS_CHROMEOS) | 19 #if !defined(OS_CHROMEOS) |
20 #include "base/process_util.h" | 20 #include "base/process_util.h" |
21 #endif // !defined(OS_CHROMEOS) | 21 #endif // !defined(OS_CHROMEOS) |
22 | 22 |
23 using WebKit::WebFrame; | 23 using WebKit::WebFrame; |
24 using WebKit::WebNode; | 24 using WebKit::WebNode; |
25 | 25 |
26 void PrintWebViewHelper::CreatePreviewDocument( | 26 void PrintWebViewHelper::CreatePreviewDocument( |
27 const PrintMsg_PrintPages_Params& params, WebKit::WebFrame* frame, | 27 const PrintMsg_PrintPages_Params& params, WebKit::WebFrame* frame, |
28 WebKit::WebNode* node) { | 28 WebKit::WebNode* node) { |
29 // We only can use PDF in the renderer because Cairo needs to create a | |
30 // temporary file for a PostScript surface. | |
31 scoped_ptr<printing::NativeMetafile> metafile( | |
32 printing::NativeMetafileFactory::Create()); | |
33 int page_count = 0; | 29 int page_count = 0; |
| 30 printing::PreviewMetafile metafile; |
| 31 if (!metafile.Init()) |
| 32 return; |
34 | 33 |
35 if (!RenderPages(params, frame, node, false, &page_count, metafile.get())) | 34 if (!RenderPages(params, frame, node, false, &page_count, &metafile)) |
36 return; | 35 return; |
37 | 36 |
38 // Get the size of the resulting metafile. | 37 // Get the size of the resulting metafile. |
39 uint32 buf_size = metafile->GetDataSize(); | 38 uint32 buf_size = metafile.GetDataSize(); |
40 DCHECK_GT(buf_size, 0u); | 39 DCHECK_GT(buf_size, 0u); |
41 | 40 |
42 PrintHostMsg_DidPreviewDocument_Params preview_params; | 41 PrintHostMsg_DidPreviewDocument_Params preview_params; |
43 preview_params.document_cookie = params.params.document_cookie; | 42 preview_params.document_cookie = params.params.document_cookie; |
44 preview_params.expected_pages_count = page_count; | 43 preview_params.expected_pages_count = page_count; |
45 preview_params.data_size = buf_size; | 44 preview_params.data_size = buf_size; |
46 | 45 |
47 if (!CopyMetafileDataToSharedMem(metafile.get(), | 46 if (!CopyMetafileDataToSharedMem(&metafile, |
48 &(preview_params.metafile_data_handle))) { | 47 &(preview_params.metafile_data_handle))) { |
49 preview_params.expected_pages_count = 0; | 48 preview_params.expected_pages_count = 0; |
50 preview_params.data_size = 0; | 49 preview_params.data_size = 0; |
51 } | 50 } |
52 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 51 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
53 } | 52 } |
54 | 53 |
55 void PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, | 54 void PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, |
56 WebFrame* frame, | 55 WebFrame* frame, |
57 WebNode* node) { | 56 WebNode* node) { |
58 // We only can use PDF in the renderer because Cairo needs to create a | |
59 // temporary file for a PostScript surface. | |
60 scoped_ptr<printing::NativeMetafile> metafile( | |
61 printing::NativeMetafileFactory::Create()); | |
62 int page_count = 0; | 57 int page_count = 0; |
63 bool send_expected_page_count = | 58 bool send_expected_page_count = |
64 #if defined(OS_CHROMEOS) | 59 #if defined(OS_CHROMEOS) |
65 false; | 60 false; |
66 #else | 61 #else |
67 true; | 62 true; |
68 #endif // defined(OS_CHROMEOS) | 63 #endif // defined(OS_CHROMEOS) |
69 | 64 |
| 65 printing::NativeMetafile metafile; |
| 66 if (!metafile.Init()) |
| 67 return; |
| 68 |
70 if (!RenderPages(params, frame, node, send_expected_page_count, &page_count, | 69 if (!RenderPages(params, frame, node, send_expected_page_count, &page_count, |
71 metafile.get())) { | 70 &metafile)) { |
72 return; | 71 return; |
73 } | 72 } |
74 | 73 |
75 // Get the size of the resulting metafile. | 74 // Get the size of the resulting metafile. |
76 uint32 buf_size = metafile->GetDataSize(); | 75 uint32 buf_size = metafile.GetDataSize(); |
77 DCHECK_GT(buf_size, 0u); | 76 DCHECK_GT(buf_size, 0u); |
78 | 77 |
79 #if defined(OS_CHROMEOS) | 78 #if defined(OS_CHROMEOS) |
80 int sequence_number = -1; | 79 int sequence_number = -1; |
81 base::FileDescriptor fd; | 80 base::FileDescriptor fd; |
82 | 81 |
83 // Ask the browser to open a file for us. | 82 // Ask the browser to open a file for us. |
84 if (!Send(new PrintHostMsg_AllocateTempFileForPrinting(&fd, | 83 if (!Send(new PrintHostMsg_AllocateTempFileForPrinting(&fd, |
85 &sequence_number))) { | 84 &sequence_number))) { |
86 return; | 85 return; |
87 } | 86 } |
88 if (!metafile->SaveToFD(fd)) | 87 if (!metafile.SaveToFD(fd)) |
89 return; | 88 return; |
90 | 89 |
91 // Tell the browser we've finished writing the file. | 90 // Tell the browser we've finished writing the file. |
92 Send(new PrintHostMsg_TempFileForPrintingWritten(sequence_number)); | 91 Send(new PrintHostMsg_TempFileForPrintingWritten(sequence_number)); |
93 #else | 92 #else |
94 PrintHostMsg_DidPrintPage_Params printed_page_params; | 93 PrintHostMsg_DidPrintPage_Params printed_page_params; |
95 printed_page_params.data_size = 0; | 94 printed_page_params.data_size = 0; |
96 printed_page_params.document_cookie = params.params.document_cookie; | 95 printed_page_params.document_cookie = params.params.document_cookie; |
97 | 96 |
98 base::SharedMemoryHandle shared_mem_handle; | 97 base::SharedMemoryHandle shared_mem_handle; |
99 if (!Send(new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size, | 98 if (!Send(new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size, |
100 &shared_mem_handle))) { | 99 &shared_mem_handle))) { |
101 NOTREACHED() << "AllocateSharedMemoryBuffer failed"; | 100 NOTREACHED() << "AllocateSharedMemoryBuffer failed"; |
102 return; | 101 return; |
103 } | 102 } |
104 if (!base::SharedMemory::IsHandleValid(shared_mem_handle)) { | 103 if (!base::SharedMemory::IsHandleValid(shared_mem_handle)) { |
105 NOTREACHED() << "AllocateSharedMemoryBuffer returned bad handle"; | 104 NOTREACHED() << "AllocateSharedMemoryBuffer returned bad handle"; |
106 return; | 105 return; |
107 } | 106 } |
108 | 107 |
109 { | 108 { |
110 base::SharedMemory shared_buf(shared_mem_handle, false); | 109 base::SharedMemory shared_buf(shared_mem_handle, false); |
111 if (!shared_buf.Map(buf_size)) { | 110 if (!shared_buf.Map(buf_size)) { |
112 NOTREACHED() << "Map failed"; | 111 NOTREACHED() << "Map failed"; |
113 return; | 112 return; |
114 } | 113 } |
115 metafile->GetData(shared_buf.memory(), buf_size); | 114 metafile.GetData(shared_buf.memory(), buf_size); |
116 printed_page_params.data_size = buf_size; | 115 printed_page_params.data_size = buf_size; |
117 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), | 116 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), |
118 &(printed_page_params.metafile_data_handle)); | 117 &(printed_page_params.metafile_data_handle)); |
119 } | 118 } |
120 | 119 |
121 if (params.pages.empty()) { | 120 if (params.pages.empty()) { |
122 // Send the first page with a valid handle. | 121 // Send the first page with a valid handle. |
123 printed_page_params.page_number = 0; | 122 printed_page_params.page_number = 0; |
124 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); | 123 Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params)); |
125 | 124 |
(...skipping 16 matching lines...) Expand all Loading... |
142 } | 141 } |
143 } | 142 } |
144 #endif // defined(OS_CHROMEOS) | 143 #endif // defined(OS_CHROMEOS) |
145 } | 144 } |
146 | 145 |
147 bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, | 146 bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, |
148 WebKit::WebFrame* frame, | 147 WebKit::WebFrame* frame, |
149 WebKit::WebNode* node, | 148 WebKit::WebNode* node, |
150 bool send_expected_page_count, | 149 bool send_expected_page_count, |
151 int* page_count, | 150 int* page_count, |
152 printing::NativeMetafile* metafile) { | 151 printing::Metafile* metafile) { |
153 PrintMsg_Print_Params printParams = params.params; | 152 PrintMsg_Print_Params printParams = params.params; |
154 scoped_ptr<skia::VectorCanvas> canvas; | 153 scoped_ptr<skia::VectorCanvas> canvas; |
155 | 154 |
156 { | 155 { |
157 // Hack - when |prep_frame_view| goes out of scope, PrintEnd() gets called. | 156 // Hack - when |prep_frame_view| goes out of scope, PrintEnd() gets called. |
158 // Doing this before closing |metafile| below ensures | 157 // Doing this before closing |metafile| below ensures |
159 // webkit::ppapi::PluginInstance::PrintEnd() has a valid canvas/metafile to | 158 // webkit::ppapi::PluginInstance::PrintEnd() has a valid canvas/metafile to |
160 // save the final output to. See pepper_plugin_instance.cc for the whole | 159 // save the final output to. See pepper_plugin_instance.cc for the whole |
161 // story. | 160 // story. |
162 PrepareFrameAndViewForPrint prep_frame_view(printParams, | 161 PrepareFrameAndViewForPrint prep_frame_view(printParams, |
(...skipping 27 matching lines...) Expand all Loading... |
190 | 189 |
191 metafile->FinishDocument(); | 190 metafile->FinishDocument(); |
192 | 191 |
193 return true; | 192 return true; |
194 } | 193 } |
195 | 194 |
196 void PrintWebViewHelper::PrintPageInternal( | 195 void PrintWebViewHelper::PrintPageInternal( |
197 const PrintMsg_PrintPage_Params& params, | 196 const PrintMsg_PrintPage_Params& params, |
198 const gfx::Size& canvas_size, | 197 const gfx::Size& canvas_size, |
199 WebFrame* frame, | 198 WebFrame* frame, |
200 printing::NativeMetafile* metafile, | 199 printing::Metafile* metafile, |
201 scoped_ptr<skia::VectorCanvas>* canvas) { | 200 scoped_ptr<skia::VectorCanvas>* canvas) { |
202 double content_width_in_points; | 201 double content_width_in_points; |
203 double content_height_in_points; | 202 double content_height_in_points; |
204 double margin_top_in_points; | 203 double margin_top_in_points; |
205 double margin_right_in_points; | 204 double margin_right_in_points; |
206 double margin_bottom_in_points; | 205 double margin_bottom_in_points; |
207 double margin_left_in_points; | 206 double margin_left_in_points; |
208 GetPageSizeAndMarginsInPoints(frame, | 207 GetPageSizeAndMarginsInPoints(frame, |
209 params.page_number, | 208 params.page_number, |
210 params.params, | 209 params.params, |
(...skipping 10 matching lines...) Expand all Loading... |
221 content_height_in_points + margin_top_in_points + | 220 content_height_in_points + margin_top_in_points + |
222 margin_bottom_in_points); | 221 margin_bottom_in_points); |
223 gfx::Point content_origin(margin_left_in_points, margin_top_in_points); | 222 gfx::Point content_origin(margin_left_in_points, margin_top_in_points); |
224 | 223 |
225 skia::PlatformDevice* device = metafile->StartPageForVectorCanvas( | 224 skia::PlatformDevice* device = metafile->StartPageForVectorCanvas( |
226 page_size, content_origin, 1.0f); | 225 page_size, content_origin, 1.0f); |
227 if (!device) | 226 if (!device) |
228 return; | 227 return; |
229 | 228 |
230 canvas->reset(new skia::VectorCanvas(device)); | 229 canvas->reset(new skia::VectorCanvas(device)); |
231 printing::NativeMetafileSkiaWrapper::SetMetafileOnCanvas(canvas->get(), | 230 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas->get(), metafile); |
232 metafile); | |
233 frame->printPage(params.page_number, canvas->get()); | 231 frame->printPage(params.page_number, canvas->get()); |
234 | 232 |
235 // TODO(myhuang): We should handle transformation for paper margins. | 233 // TODO(myhuang): We should handle transformation for paper margins. |
236 // TODO(myhuang): We should render the header and the footer. | 234 // TODO(myhuang): We should render the header and the footer. |
237 | 235 |
238 // Done printing. Close the device context to retrieve the compiled metafile. | 236 // Done printing. Close the device context to retrieve the compiled metafile. |
239 if (!metafile->FinishPage()) | 237 if (!metafile->FinishPage()) |
240 NOTREACHED() << "metafile failed"; | 238 NOTREACHED() << "metafile failed"; |
241 } | 239 } |
OLD | NEW |