| 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" |
| 11 #include "printing/native_metafile.h" | 12 #include "printing/metafile_factory.h" |
| 12 #include "skia/ext/vector_canvas.h" | 13 #include "skia/ext/vector_canvas.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 15 | 16 |
| 16 using printing::NativeMetafile; | |
| 17 using WebKit::WebFrame; | 17 using WebKit::WebFrame; |
| 18 using WebKit::WebNode; | 18 using WebKit::WebNode; |
| 19 using WebKit::WebSize; | 19 using WebKit::WebSize; |
| 20 | 20 |
| 21 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, | 21 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, |
| 22 WebFrame* frame, | 22 WebFrame* frame, |
| 23 WebNode* node) { | 23 WebNode* node) { |
| 24 // We only can use PDF in the renderer because Cairo needs to create a | 24 // We only can use PDF in the renderer because Cairo needs to create a |
| 25 // temporary file for a PostScript surface. | 25 // temporary file for a PostScript surface. |
| 26 printing::NativeMetafile metafile(printing::NativeMetafile::PDF); | 26 scoped_ptr<printing::NativeMetafile> metafile_ptr( |
| 27 printing::MetafileFactory::GetMetafile()); |
| 27 int page_count; | 28 int page_count; |
| 28 skia::VectorCanvas* canvas = NULL; | 29 skia::VectorCanvas* canvas = NULL; |
| 29 | 30 |
| 30 { | 31 { |
| 31 // Hack - when |prep_frame_view| goes out of scope, PrintEnd() gets called. | 32 // Hack - when |prep_frame_view| goes out of scope, PrintEnd() gets called. |
| 32 // Doing this before closing |metafile| below ensures | 33 // Doing this before closing |metafile| below ensures |
| 33 // webkit::ppapi::PluginInstance::PrintEnd() has a valid canvas/metafile to | 34 // webkit::ppapi::PluginInstance::PrintEnd() has a valid canvas/metafile to |
| 34 // save the final output to. See pepper_plugin_instance.cc for the whole | 35 // save the final output to. See pepper_plugin_instance.cc for the whole |
| 35 // story. | 36 // story. |
| 36 ViewMsg_Print_Params printParams = params.params; | 37 ViewMsg_Print_Params printParams = params.params; |
| 37 PrepareFrameAndViewForPrint prep_frame_view(printParams, | 38 PrepareFrameAndViewForPrint prep_frame_view(printParams, |
| 38 frame, | 39 frame, |
| 39 node, | 40 node, |
| 40 frame->view()); | 41 frame->view()); |
| 41 page_count = prep_frame_view.GetExpectedPageCount(); | 42 page_count = prep_frame_view.GetExpectedPageCount(); |
| 42 | 43 |
| 43 // TODO(myhuang): Send ViewHostMsg_DidGetPrintedPagesCount. | 44 // TODO(myhuang): Send ViewHostMsg_DidGetPrintedPagesCount. |
| 44 | 45 |
| 45 if (page_count == 0) | 46 if (page_count == 0) |
| 46 return; | 47 return; |
| 47 | 48 |
| 48 metafile.Init(); | 49 metafile_ptr->Init(); |
| 49 | 50 |
| 50 ViewMsg_PrintPage_Params page_params; | 51 ViewMsg_PrintPage_Params page_params; |
| 51 page_params.params = printParams; | 52 page_params.params = printParams; |
| 52 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 53 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
| 53 if (params.pages.empty()) { | 54 if (params.pages.empty()) { |
| 54 for (int i = 0; i < page_count; ++i) { | 55 for (int i = 0; i < page_count; ++i) { |
| 55 page_params.page_number = i; | 56 page_params.page_number = i; |
| 56 delete canvas; | 57 delete canvas; |
| 57 PrintPage(page_params, canvas_size, frame, &metafile, &canvas); | 58 PrintPage(page_params, canvas_size, frame, metafile_ptr.get(), &canvas); |
| 58 } | 59 } |
| 59 } else { | 60 } else { |
| 60 for (size_t i = 0; i < params.pages.size(); ++i) { | 61 for (size_t i = 0; i < params.pages.size(); ++i) { |
| 61 page_params.page_number = params.pages[i]; | 62 page_params.page_number = params.pages[i]; |
| 62 delete canvas; | 63 delete canvas; |
| 63 PrintPage(page_params, canvas_size, frame, &metafile, &canvas); | 64 PrintPage(page_params, canvas_size, frame, metafile_ptr.get(), &canvas); |
| 64 } | 65 } |
| 65 } | 66 } |
| 66 } | 67 } |
| 67 | 68 |
| 68 delete canvas; | 69 delete canvas; |
| 69 metafile.Close(); | 70 metafile_ptr->Close(); |
| 70 | 71 |
| 71 int sequence_number = -1; | 72 int sequence_number = -1; |
| 72 // Get the size of the resulting metafile. | 73 // Get the size of the resulting metafile. |
| 73 uint32 buf_size = metafile.GetDataSize(); | 74 uint32 buf_size = metafile_ptr->GetDataSize(); |
| 74 DCHECK_GT(buf_size, 0u); | 75 DCHECK_GT(buf_size, 0u); |
| 75 | 76 |
| 76 base::FileDescriptor fd; | 77 base::FileDescriptor fd; |
| 77 | 78 |
| 78 // Ask the browser to open a file for us. | 79 // Ask the browser to open a file for us. |
| 79 if (!Send(new ViewHostMsg_AllocateTempFileForPrinting(&fd, | 80 if (!Send(new ViewHostMsg_AllocateTempFileForPrinting(&fd, |
| 80 &sequence_number))) { | 81 &sequence_number))) { |
| 81 return; | 82 return; |
| 82 } | 83 } |
| 83 | 84 |
| 84 if (!metafile.SaveTo(fd)) | 85 if (!metafile_ptr->SaveTo(fd)) |
| 85 return; | 86 return; |
| 86 | 87 |
| 87 // Tell the browser we've finished writing the file. | 88 // Tell the browser we've finished writing the file. |
| 88 Send(new ViewHostMsg_TempFileForPrintingWritten(sequence_number)); | 89 Send(new ViewHostMsg_TempFileForPrintingWritten(sequence_number)); |
| 89 } | 90 } |
| 90 | 91 |
| 91 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 92 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
| 92 const gfx::Size& canvas_size, | 93 const gfx::Size& canvas_size, |
| 93 WebFrame* frame, | 94 WebFrame* frame, |
| 94 printing::NativeMetafile* metafile, | 95 printing::NativeMetafile* metafile, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 124 canvas_size.height()); | 125 canvas_size.height()); |
| 125 frame->printPage(params.page_number, *canvas); | 126 frame->printPage(params.page_number, *canvas); |
| 126 | 127 |
| 127 // TODO(myhuang): We should handle transformation for paper margins. | 128 // TODO(myhuang): We should handle transformation for paper margins. |
| 128 // TODO(myhuang): We should render the header and the footer. | 129 // TODO(myhuang): We should render the header and the footer. |
| 129 | 130 |
| 130 // Done printing. Close the device context to retrieve the compiled metafile. | 131 // Done printing. Close the device context to retrieve the compiled metafile. |
| 131 if (!metafile->FinishPage()) | 132 if (!metafile->FinishPage()) |
| 132 NOTREACHED() << "metafile failed"; | 133 NOTREACHED() << "metafile failed"; |
| 133 } | 134 } |
| OLD | NEW |