| 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 "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/common/render_messages_params.h" | 10 #include "chrome/common/render_messages_params.h" |
| 11 #include "printing/native_metafile.h" | 11 #include "printing/native_metafile.h" |
| 12 #include "skia/ext/vector_canvas.h" | 12 #include "skia/ext/vector_canvas.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 15 | 15 |
| 16 #if !defined(OS_CHROMEOS) |
| 17 #include "base/process_util.h" |
| 18 #endif // !defined(OS_CHROMEOS) |
| 19 |
| 16 using printing::NativeMetafile; | 20 using printing::NativeMetafile; |
| 17 using WebKit::WebFrame; | 21 using WebKit::WebFrame; |
| 18 using WebKit::WebNode; | 22 using WebKit::WebNode; |
| 19 using WebKit::WebSize; | 23 using WebKit::WebSize; |
| 20 | 24 |
| 21 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, | 25 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, |
| 22 WebFrame* frame, | 26 WebFrame* frame, |
| 23 WebNode* node) { | 27 WebNode* node) { |
| 24 // We only can use PDF in the renderer because Cairo needs to create a | 28 // We only can use PDF in the renderer because Cairo needs to create a |
| 25 // temporary file for a PostScript surface. | 29 // temporary file for a PostScript surface. |
| 26 printing::NativeMetafile metafile(printing::NativeMetafile::PDF); | 30 printing::NativeMetafile metafile(printing::NativeMetafile::PDF); |
| 27 int page_count; | 31 int page_count; |
| 28 skia::VectorCanvas* canvas = NULL; | 32 skia::VectorCanvas* canvas = NULL; |
| 29 | 33 |
| 30 { | 34 { |
| 31 // Hack - when |prep_frame_view| goes out of scope, PrintEnd() gets called. | 35 // Hack - when |prep_frame_view| goes out of scope, PrintEnd() gets called. |
| 32 // Doing this before closing |metafile| below ensures | 36 // Doing this before closing |metafile| below ensures |
| 33 // webkit::ppapi::PluginInstance::PrintEnd() has a valid canvas/metafile to | 37 // webkit::ppapi::PluginInstance::PrintEnd() has a valid canvas/metafile to |
| 34 // save the final output to. See pepper_plugin_instance.cc for the whole | 38 // save the final output to. See pepper_plugin_instance.cc for the whole |
| 35 // story. | 39 // story. |
| 36 ViewMsg_Print_Params printParams = params.params; | 40 ViewMsg_Print_Params printParams = params.params; |
| 37 PrepareFrameAndViewForPrint prep_frame_view(printParams, | 41 PrepareFrameAndViewForPrint prep_frame_view(printParams, |
| 38 frame, | 42 frame, |
| 39 node, | 43 node, |
| 40 frame->view()); | 44 frame->view()); |
| 41 page_count = prep_frame_view.GetExpectedPageCount(); | 45 page_count = prep_frame_view.GetExpectedPageCount(); |
| 46 #if !defined(OS_CHROMEOS) |
| 47 Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), |
| 48 printParams.document_cookie, |
| 49 page_count)); |
| 50 #endif // !defined(OS_CHROMEOS) |
| 42 | 51 |
| 43 // TODO(myhuang): Send ViewHostMsg_DidGetPrintedPagesCount. | 52 if (!page_count) |
| 44 | |
| 45 if (page_count == 0) | |
| 46 return; | 53 return; |
| 47 | 54 |
| 48 metafile.Init(); | 55 metafile.Init(); |
| 49 | 56 |
| 50 ViewMsg_PrintPage_Params page_params; | 57 ViewMsg_PrintPage_Params page_params; |
| 51 page_params.params = printParams; | 58 page_params.params = printParams; |
| 52 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 59 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
| 53 if (params.pages.empty()) { | 60 if (params.pages.empty()) { |
| 54 for (int i = 0; i < page_count; ++i) { | 61 for (int i = 0; i < page_count; ++i) { |
| 55 page_params.page_number = i; | 62 page_params.page_number = i; |
| 56 delete canvas; | 63 delete canvas; |
| 57 PrintPage(page_params, canvas_size, frame, &metafile, &canvas); | 64 PrintPage(page_params, canvas_size, frame, &metafile, &canvas); |
| 58 } | 65 } |
| 59 } else { | 66 } else { |
| 60 for (size_t i = 0; i < params.pages.size(); ++i) { | 67 for (size_t i = 0; i < params.pages.size(); ++i) { |
| 61 page_params.page_number = params.pages[i]; | 68 page_params.page_number = params.pages[i]; |
| 62 delete canvas; | 69 delete canvas; |
| 63 PrintPage(page_params, canvas_size, frame, &metafile, &canvas); | 70 PrintPage(page_params, canvas_size, frame, &metafile, &canvas); |
| 64 } | 71 } |
| 65 } | 72 } |
| 66 } | 73 } |
| 67 | 74 |
| 68 delete canvas; | 75 delete canvas; |
| 69 metafile.Close(); | 76 metafile.Close(); |
| 70 | 77 |
| 71 int sequence_number = -1; | |
| 72 // Get the size of the resulting metafile. | 78 // Get the size of the resulting metafile. |
| 73 uint32 buf_size = metafile.GetDataSize(); | 79 uint32 buf_size = metafile.GetDataSize(); |
| 74 DCHECK_GT(buf_size, 0u); | 80 DCHECK_GT(buf_size, 0u); |
| 75 | 81 |
| 82 #if defined(OS_CHROMEOS) |
| 83 int sequence_number = -1; |
| 76 base::FileDescriptor fd; | 84 base::FileDescriptor fd; |
| 77 | 85 |
| 78 // Ask the browser to open a file for us. | 86 // Ask the browser to open a file for us. |
| 79 if (!Send(new ViewHostMsg_AllocateTempFileForPrinting(&fd, | 87 if (!Send(new ViewHostMsg_AllocateTempFileForPrinting(&fd, |
| 80 &sequence_number))) { | 88 &sequence_number))) { |
| 81 return; | 89 return; |
| 82 } | 90 } |
| 83 | |
| 84 if (!metafile.SaveTo(fd)) | 91 if (!metafile.SaveTo(fd)) |
| 85 return; | 92 return; |
| 86 | 93 |
| 87 // Tell the browser we've finished writing the file. | 94 // Tell the browser we've finished writing the file. |
| 88 Send(new ViewHostMsg_TempFileForPrintingWritten(sequence_number)); | 95 Send(new ViewHostMsg_TempFileForPrintingWritten(sequence_number)); |
| 96 #else |
| 97 ViewHostMsg_DidPrintPage_Params printed_page_params; |
| 98 printed_page_params.data_size = 0; |
| 99 printed_page_params.document_cookie = params.params.document_cookie; |
| 100 |
| 101 base::SharedMemoryHandle shared_mem_handle; |
| 102 if (!Send(new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size, |
| 103 &shared_mem_handle))) { |
| 104 NOTREACHED() << "AllocateSharedMemoryBuffer failed"; |
| 105 return; |
| 106 } |
| 107 if (!base::SharedMemory::IsHandleValid(shared_mem_handle)) { |
| 108 NOTREACHED() << "AllocateSharedMemoryBuffer returned bad handle"; |
| 109 return; |
| 110 } |
| 111 |
| 112 { |
| 113 base::SharedMemory shared_buf(shared_mem_handle, false); |
| 114 if (!shared_buf.Map(buf_size)) { |
| 115 NOTREACHED() << "Map failed"; |
| 116 return; |
| 117 } |
| 118 metafile.GetData(shared_buf.memory(), buf_size); |
| 119 printed_page_params.data_size = buf_size; |
| 120 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), |
| 121 &(printed_page_params.metafile_data_handle)); |
| 122 } |
| 123 |
| 124 // Send the first page with a valid handle. |
| 125 printed_page_params.page_number = 0; |
| 126 Send(new ViewHostMsg_DidPrintPage(routing_id(), printed_page_params)); |
| 127 |
| 128 // Send the rest of the pages with an invalid metafile handle. |
| 129 printed_page_params.metafile_data_handle.fd = -1; |
| 130 if (params.pages.empty()) { |
| 131 for (int i = 1; i < page_count; ++i) { |
| 132 printed_page_params.page_number = i; |
| 133 Send(new ViewHostMsg_DidPrintPage(routing_id(), printed_page_params)); |
| 134 } |
| 135 } else { |
| 136 for (size_t i = 1; i < params.pages.size(); ++i) { |
| 137 printed_page_params.page_number = params.pages[i]; |
| 138 Send(new ViewHostMsg_DidPrintPage(routing_id(), printed_page_params)); |
| 139 } |
| 140 } |
| 141 #endif // defined(OS_CHROMEOS) |
| 89 } | 142 } |
| 90 | 143 |
| 91 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 144 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
| 92 const gfx::Size& canvas_size, | 145 const gfx::Size& canvas_size, |
| 93 WebFrame* frame, | 146 WebFrame* frame, |
| 94 printing::NativeMetafile* metafile, | 147 printing::NativeMetafile* metafile, |
| 95 skia::VectorCanvas** canvas) { | 148 skia::VectorCanvas** canvas) { |
| 96 double content_width_in_points; | 149 double content_width_in_points; |
| 97 double content_height_in_points; | 150 double content_height_in_points; |
| 98 double margin_top_in_points; | 151 double margin_top_in_points; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 124 canvas_size.height()); | 177 canvas_size.height()); |
| 125 frame->printPage(params.page_number, *canvas); | 178 frame->printPage(params.page_number, *canvas); |
| 126 | 179 |
| 127 // TODO(myhuang): We should handle transformation for paper margins. | 180 // TODO(myhuang): We should handle transformation for paper margins. |
| 128 // TODO(myhuang): We should render the header and the footer. | 181 // TODO(myhuang): We should render the header and the footer. |
| 129 | 182 |
| 130 // Done printing. Close the device context to retrieve the compiled metafile. | 183 // Done printing. Close the device context to retrieve the compiled metafile. |
| 131 if (!metafile->FinishPage()) | 184 if (!metafile->FinishPage()) |
| 132 NOTREACHED() << "metafile failed"; | 185 NOTREACHED() << "metafile failed"; |
| 133 } | 186 } |
| OLD | NEW |