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