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/scoped_ptr.h" |
9 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
10 #include "chrome/common/render_messages_params.h" | 11 #include "chrome/common/render_messages_params.h" |
| 12 #include "printing/native_metafile_factory.h" |
11 #include "printing/native_metafile.h" | 13 #include "printing/native_metafile.h" |
12 #include "skia/ext/vector_canvas.h" | 14 #include "skia/ext/vector_canvas.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
14 | 16 |
15 #if !defined(OS_CHROMEOS) | 17 #if !defined(OS_CHROMEOS) |
16 #include "base/process_util.h" | 18 #include "base/process_util.h" |
17 #endif // !defined(OS_CHROMEOS) | 19 #endif // !defined(OS_CHROMEOS) |
18 | 20 |
19 using WebKit::WebFrame; | 21 using WebKit::WebFrame; |
20 using WebKit::WebNode; | 22 using WebKit::WebNode; |
21 | 23 |
22 void PrintWebViewHelper::CreatePreviewDocument( | 24 void PrintWebViewHelper::CreatePreviewDocument( |
23 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { | 25 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { |
24 // We only can use PDF in the renderer because Cairo needs to create a | 26 // We only can use PDF in the renderer because Cairo needs to create a |
25 // temporary file for a PostScript surface. | 27 // temporary file for a PostScript surface. |
26 printing::NativeMetafile metafile(printing::NativeMetafile::PDF); | 28 scoped_ptr<printing::NativeMetafile> metafile( |
| 29 printing::NativeMetafileFactory::CreateMetafile()); |
27 int page_count = 0; | 30 int page_count = 0; |
28 | 31 |
29 if (!RenderPages(params, frame, false, &page_count, &metafile)) | 32 if (!RenderPages(params, frame, false, &page_count, metafile.get())) |
30 return; | 33 return; |
31 | 34 |
32 // Get the size of the resulting metafile. | 35 // Get the size of the resulting metafile. |
33 uint32 buf_size = metafile.GetDataSize(); | 36 uint32 buf_size = metafile->GetDataSize(); |
34 DCHECK_GT(buf_size, 0u); | 37 DCHECK_GT(buf_size, 0u); |
35 | 38 |
36 ViewHostMsg_DidPreviewDocument_Params preview_params; | 39 ViewHostMsg_DidPreviewDocument_Params preview_params; |
37 preview_params.data_size = buf_size; | 40 preview_params.data_size = buf_size; |
38 preview_params.document_cookie = params.params.document_cookie; | 41 preview_params.document_cookie = params.params.document_cookie; |
39 | 42 |
40 if (!CopyMetafileDataToSharedMem(&metafile, | 43 if (!CopyMetafileDataToSharedMem(metafile.get(), |
41 &(preview_params.metafile_data_handle))) { | 44 &(preview_params.metafile_data_handle))) { |
42 preview_params.data_size = 0; | 45 preview_params.data_size = 0; |
43 } | 46 } |
44 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 47 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
45 } | 48 } |
46 | 49 |
47 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, | 50 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, |
48 WebFrame* frame, | 51 WebFrame* frame, |
49 WebNode* node) { | 52 WebNode* node) { |
50 // We only can use PDF in the renderer because Cairo needs to create a | 53 // We only can use PDF in the renderer because Cairo needs to create a |
51 // temporary file for a PostScript surface. | 54 // temporary file for a PostScript surface. |
52 printing::NativeMetafile metafile(printing::NativeMetafile::PDF); | 55 scoped_ptr<printing::NativeMetafile> metafile( |
| 56 printing::NativeMetafileFactory::CreateMetafile()); |
53 int page_count = 0; | 57 int page_count = 0; |
54 bool send_expected_page_count = | 58 bool send_expected_page_count = |
55 #if defined(OS_CHROMEOS) | 59 #if defined(OS_CHROMEOS) |
56 false; | 60 false; |
57 #else | 61 #else |
58 true; | 62 true; |
59 #endif // defined(OS_CHROMEOS) | 63 #endif // defined(OS_CHROMEOS) |
60 | 64 |
61 if (!RenderPages(params, frame, send_expected_page_count, &page_count, | 65 if (!RenderPages(params, frame, send_expected_page_count, &page_count, |
62 &metafile)) { | 66 metafile.get())) { |
63 return; | 67 return; |
64 } | 68 } |
65 | 69 |
66 // Get the size of the resulting metafile. | 70 // Get the size of the resulting metafile. |
67 uint32 buf_size = metafile.GetDataSize(); | 71 uint32 buf_size = metafile->GetDataSize(); |
68 DCHECK_GT(buf_size, 0u); | 72 DCHECK_GT(buf_size, 0u); |
69 | 73 |
70 #if defined(OS_CHROMEOS) | 74 #if defined(OS_CHROMEOS) |
71 int sequence_number = -1; | 75 int sequence_number = -1; |
72 base::FileDescriptor fd; | 76 base::FileDescriptor fd; |
73 | 77 |
74 // Ask the browser to open a file for us. | 78 // Ask the browser to open a file for us. |
75 if (!Send(new ViewHostMsg_AllocateTempFileForPrinting(&fd, | 79 if (!Send(new ViewHostMsg_AllocateTempFileForPrinting(&fd, |
76 &sequence_number))) { | 80 &sequence_number))) { |
77 return; | 81 return; |
(...skipping 18 matching lines...) Expand all Loading... |
96 NOTREACHED() << "AllocateSharedMemoryBuffer returned bad handle"; | 100 NOTREACHED() << "AllocateSharedMemoryBuffer returned bad handle"; |
97 return; | 101 return; |
98 } | 102 } |
99 | 103 |
100 { | 104 { |
101 base::SharedMemory shared_buf(shared_mem_handle, false); | 105 base::SharedMemory shared_buf(shared_mem_handle, false); |
102 if (!shared_buf.Map(buf_size)) { | 106 if (!shared_buf.Map(buf_size)) { |
103 NOTREACHED() << "Map failed"; | 107 NOTREACHED() << "Map failed"; |
104 return; | 108 return; |
105 } | 109 } |
106 metafile.GetData(shared_buf.memory(), buf_size); | 110 metafile->GetData(shared_buf.memory(), buf_size); |
107 printed_page_params.data_size = buf_size; | 111 printed_page_params.data_size = buf_size; |
108 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), | 112 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), |
109 &(printed_page_params.metafile_data_handle)); | 113 &(printed_page_params.metafile_data_handle)); |
110 } | 114 } |
111 | 115 |
112 // Send the first page with a valid handle. | 116 // Send the first page with a valid handle. |
113 printed_page_params.page_number = 0; | 117 printed_page_params.page_number = 0; |
114 Send(new ViewHostMsg_DidPrintPage(routing_id(), printed_page_params)); | 118 Send(new ViewHostMsg_DidPrintPage(routing_id(), printed_page_params)); |
115 | 119 |
116 // Send the rest of the pages with an invalid metafile handle. | 120 // Send the rest of the pages with an invalid metafile handle. |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 canvas_size.height()); | 222 canvas_size.height()); |
219 frame->printPage(params.page_number, *canvas); | 223 frame->printPage(params.page_number, *canvas); |
220 | 224 |
221 // TODO(myhuang): We should handle transformation for paper margins. | 225 // TODO(myhuang): We should handle transformation for paper margins. |
222 // TODO(myhuang): We should render the header and the footer. | 226 // TODO(myhuang): We should render the header and the footer. |
223 | 227 |
224 // Done printing. Close the device context to retrieve the compiled metafile. | 228 // Done printing. Close the device context to retrieve the compiled metafile. |
225 if (!metafile->FinishPage()) | 229 if (!metafile->FinishPage()) |
226 NOTREACHED() << "metafile failed"; | 230 NOTREACHED() << "metafile failed"; |
227 } | 231 } |
OLD | NEW |