| 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 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/scoped_ptr.h" |
| 10 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
| 11 #include "chrome/common/render_messages_params.h" | 12 #include "chrome/common/render_messages_params.h" |
| 13 #include "printing/native_metafile.h" |
| 14 #include "printing/native_metafile_factory.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 13 | 16 |
| 14 using WebKit::WebFrame; | 17 using WebKit::WebFrame; |
| 15 | 18 |
| 16 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 19 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
| 17 const gfx::Size& canvas_size, | 20 const gfx::Size& canvas_size, |
| 18 WebFrame* frame) { | 21 WebFrame* frame) { |
| 19 printing::NativeMetafile metafile; | 22 scoped_ptr<printing::NativeMetafile> metafile( |
| 20 if (!metafile.Init()) | 23 printing::MetafileFactory::GetMetafile()); |
| 24 if (!metafile->Init()) |
| 21 return; | 25 return; |
| 22 | 26 |
| 23 float scale_factor = frame->getPrintPageShrink(params.page_number); | 27 float scale_factor = frame->getPrintPageShrink(params.page_number); |
| 24 int page_number = params.page_number; | 28 int page_number = params.page_number; |
| 25 | 29 |
| 26 // Render page for printing. | 30 // Render page for printing. |
| 27 gfx::Point origin(0.0f, 0.0f); | 31 gfx::Point origin(0.0f, 0.0f); |
| 28 RenderPage(params.params.printable_size, origin, scale_factor, page_number, | 32 RenderPage(params.params.printable_size, origin, scale_factor, page_number, |
| 29 frame, &metafile); | 33 frame, metafile.get()); |
| 30 metafile.Close(); | 34 metafile.Close(); |
| 31 | 35 |
| 32 ViewHostMsg_DidPrintPage_Params page_params; | 36 ViewHostMsg_DidPrintPage_Params page_params; |
| 33 page_params.data_size = metafile.GetDataSize(); | 37 page_params.data_size = metafile->GetDataSize(); |
| 34 page_params.page_number = page_number; | 38 page_params.page_number = page_number; |
| 35 page_params.document_cookie = params.params.document_cookie; | 39 page_params.document_cookie = params.params.document_cookie; |
| 36 page_params.actual_shrink = scale_factor; | 40 page_params.actual_shrink = scale_factor; |
| 37 page_params.page_size = params.params.page_size; | 41 page_params.page_size = params.params.page_size; |
| 38 page_params.content_area = gfx::Rect(params.params.margin_left, | 42 page_params.content_area = gfx::Rect(params.params.margin_left, |
| 39 params.params.margin_top, | 43 params.params.margin_top, |
| 40 params.params.printable_size.width(), | 44 params.params.printable_size.width(), |
| 41 params.params.printable_size.height()); | 45 params.params.printable_size.height()); |
| 42 | 46 |
| 43 // Ask the browser to create the shared memory for us. | 47 // Ask the browser to create the shared memory for us. |
| 44 if (!CopyMetafileDataToSharedMem(&metafile, | 48 if (!CopyMetafileDataToSharedMem(metafile, |
| 45 &(page_params.metafile_data_handle))) { | 49 &(page_params.metafile_data_handle))) { |
| 46 page_params.data_size = 0; | 50 page_params.data_size = 0; |
| 47 } | 51 } |
| 48 | 52 |
| 49 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); | 53 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); |
| 50 } | 54 } |
| 51 | 55 |
| 52 void PrintWebViewHelper::CreatePreviewDocument( | 56 void PrintWebViewHelper::CreatePreviewDocument( |
| 53 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { | 57 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { |
| 54 ViewMsg_Print_Params printParams = params.params; | 58 ViewMsg_Print_Params printParams = params.params; |
| 55 UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams); | 59 UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams); |
| 56 | 60 |
| 57 PrepareFrameAndViewForPrint prep_frame_view(printParams, | 61 PrepareFrameAndViewForPrint prep_frame_view(printParams, |
| 58 frame, NULL, frame->view()); | 62 frame, NULL, frame->view()); |
| 59 int page_count = prep_frame_view.GetExpectedPageCount(); | 63 int page_count = prep_frame_view.GetExpectedPageCount(); |
| 60 | 64 |
| 61 if (!page_count) | 65 if (!page_count) |
| 62 return; | 66 return; |
| 63 | 67 |
| 64 printing::NativeMetafile metafile; | 68 scoped_ptr<printing::NativeMetafile> metafile( |
| 65 if (!metafile.Init()) | 69 printing::MetafileFactory::GetMetafile()); |
| 70 if (!metafile->Init()) |
| 66 return; | 71 return; |
| 67 | 72 |
| 68 float scale_factor = frame->getPrintPageShrink(0); | 73 float scale_factor = frame->getPrintPageShrink(0); |
| 69 gfx::Point origin(printParams.margin_left, printParams.margin_top); | 74 gfx::Point origin(printParams.margin_left, printParams.margin_top); |
| 70 if (params.pages.empty()) { | 75 if (params.pages.empty()) { |
| 71 for (int i = 0; i < page_count; ++i) { | 76 for (int i = 0; i < page_count; ++i) { |
| 72 RenderPage(printParams.page_size, origin, scale_factor, i, frame, | 77 RenderPage(printParams.page_size, origin, scale_factor, i, frame, |
| 73 &metafile); | 78 metafile.get()); |
| 74 } | 79 } |
| 75 } else { | 80 } else { |
| 76 for (size_t i = 0; i < params.pages.size(); ++i) { | 81 for (size_t i = 0; i < params.pages.size(); ++i) { |
| 77 if (params.pages[i] >= page_count) | 82 if (params.pages[i] >= page_count) |
| 78 break; | 83 break; |
| 79 RenderPage(printParams.page_size, origin, scale_factor, | 84 RenderPage(printParams.page_size, origin, scale_factor, |
| 80 static_cast<int>(params.pages[i]), frame, &metafile); | 85 static_cast<int>(params.pages[i]), frame, metafile.get()); |
| 81 } | 86 } |
| 82 } | 87 } |
| 83 metafile.Close(); | 88 metafile->Close(); |
| 84 | 89 |
| 85 ViewHostMsg_DidPreviewDocument_Params preview_params; | 90 ViewHostMsg_DidPreviewDocument_Params preview_params; |
| 86 preview_params.data_size = metafile.GetDataSize(); | 91 preview_params.data_size = metafile->GetDataSize(); |
| 87 preview_params.document_cookie = params.params.document_cookie; | 92 preview_params.document_cookie = params.params.document_cookie; |
| 88 preview_params.expected_pages_count = page_count; | 93 preview_params.expected_pages_count = page_count; |
| 89 | 94 |
| 90 // Ask the browser to create the shared memory for us. | 95 // Ask the browser to create the shared memory for us. |
| 91 if (!CopyMetafileDataToSharedMem(&metafile, | 96 if (!CopyMetafileDataToSharedMem(metafile, |
| 92 &(preview_params.metafile_data_handle))) { | 97 &(preview_params.metafile_data_handle))) { |
| 93 preview_params.data_size = 0; | 98 preview_params.data_size = 0; |
| 94 preview_params.expected_pages_count = 0; | 99 preview_params.expected_pages_count = 0; |
| 95 } | 100 } |
| 96 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 101 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
| 97 } | 102 } |
| 98 | 103 |
| 99 void PrintWebViewHelper::RenderPage( | 104 void PrintWebViewHelper::RenderPage( |
| 100 const gfx::Size& page_size, const gfx::Point& content_origin, | 105 const gfx::Size& page_size, const gfx::Point& content_origin, |
| 101 const float& scale_factor, int page_number, WebFrame* frame, | 106 const float& scale_factor, int page_number, WebFrame* frame, |
| 102 printing::NativeMetafile* metafile) { | 107 printing::NativeMetafile* metafile) { |
| 103 CGContextRef context = metafile->StartPage(page_size, content_origin, | 108 CGContextRef context = metafile->StartPage(page_size, content_origin, |
| 104 scale_factor); | 109 scale_factor); |
| 105 DCHECK(context); | 110 DCHECK(context); |
| 106 | 111 |
| 107 // printPage can create autoreleased references to |context|. PDF contexts | 112 // printPage can create autoreleased references to |context|. PDF contexts |
| 108 // don't write all their data until they are destroyed, so we need to make | 113 // don't write all their data until they are destroyed, so we need to make |
| 109 // certain that there are no lingering references. | 114 // certain that there are no lingering references. |
| 110 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | 115 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
| 111 frame->printPage(page_number, context); | 116 frame->printPage(page_number, context); |
| 112 [pool release]; | 117 [pool release]; |
| 113 | 118 |
| 114 // Done printing. Close the device context to retrieve the compiled metafile. | 119 // Done printing. Close the device context to retrieve the compiled metafile. |
| 115 metafile->FinishPage(); | 120 metafile->FinishPage(); |
| 116 } | 121 } |
| OLD | NEW |