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