| 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/mac/scoped_nsautorelease_pool.h" | 10 #include "base/mac/scoped_nsautorelease_pool.h" |
| 11 #include "chrome/common/print_messages.h" | 11 #include "chrome/common/print_messages.h" |
| 12 #include "printing/metafile.h" | 12 #include "printing/metafile.h" |
| 13 #include "printing/metafile_impl.h" | 13 #include "printing/metafile_impl.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 15 | 15 |
| 16 using WebKit::WebFrame; | 16 using WebKit::WebFrame; |
| 17 | 17 |
| 18 void PrintWebViewHelper::PrintPageInternal( | 18 void PrintWebViewHelper::PrintPageInternal( |
| 19 const PrintMsg_PrintPage_Params& params, | 19 const PrintMsg_PrintPage_Params& params, |
| 20 const gfx::Size& canvas_size, | 20 const gfx::Size& canvas_size, |
| 21 WebFrame* frame) { | 21 WebFrame* frame) { |
| 22 printing::NativeMetafile metafile; | 22 printing::NativeMetafile metafile; |
| 23 if (!metafile.Init()) | 23 if (!metafile.Init()) |
| 24 return; | 24 return; |
| 25 | 25 |
| 26 float scale_factor = frame->getPrintPageShrink(params.page_number); | 26 float scale_factor = frame->getPrintPageShrink(params.page_number); |
| 27 int page_number = params.page_number; | 27 int page_number = params.page_number; |
| 28 | 28 |
| 29 // Render page for printing. | 29 // Render page for printing. |
| 30 gfx::Point origin(0.0f, 0.0f); | 30 gfx::Rect content_size(params.params.printable_size); |
| 31 RenderPage(params.params.printable_size, origin, scale_factor, page_number, | 31 RenderPage(params.params.printable_size, content_size, scale_factor, |
| 32 frame, &metafile); | 32 page_number, frame, &metafile); |
| 33 metafile.FinishDocument(); | 33 metafile.FinishDocument(); |
| 34 | 34 |
| 35 PrintHostMsg_DidPrintPage_Params page_params; | 35 PrintHostMsg_DidPrintPage_Params page_params; |
| 36 page_params.data_size = metafile.GetDataSize(); | 36 page_params.data_size = metafile.GetDataSize(); |
| 37 page_params.page_number = page_number; | 37 page_params.page_number = page_number; |
| 38 page_params.document_cookie = params.params.document_cookie; | 38 page_params.document_cookie = params.params.document_cookie; |
| 39 page_params.actual_shrink = scale_factor; | 39 page_params.actual_shrink = scale_factor; |
| 40 page_params.page_size = params.params.page_size; | 40 page_params.page_size = params.params.page_size; |
| 41 page_params.content_area = gfx::Rect(params.params.margin_left, | 41 page_params.content_area = gfx::Rect(params.params.margin_left, |
| 42 params.params.margin_top, | 42 params.params.margin_top, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 63 int page_count = prep_frame_view.GetExpectedPageCount(); | 63 int page_count = prep_frame_view.GetExpectedPageCount(); |
| 64 | 64 |
| 65 if (!page_count) | 65 if (!page_count) |
| 66 return false; | 66 return false; |
| 67 | 67 |
| 68 printing::PreviewMetafile metafile; | 68 printing::PreviewMetafile metafile; |
| 69 if (!metafile.Init()) | 69 if (!metafile.Init()) |
| 70 return false; | 70 return false; |
| 71 | 71 |
| 72 float scale_factor = frame->getPrintPageShrink(0); | 72 float scale_factor = frame->getPrintPageShrink(0); |
| 73 gfx::Point origin(printParams.margin_left, printParams.margin_top); | 73 gfx::Rect content_size(printParams.margin_left, printParams.margin_top, |
| 74 printParams.printable_size.width(), |
| 75 printParams.printable_size.height()); |
| 74 if (params.pages.empty()) { | 76 if (params.pages.empty()) { |
| 75 for (int i = 0; i < page_count; ++i) { | 77 for (int i = 0; i < page_count; ++i) { |
| 76 RenderPage(printParams.page_size, origin, scale_factor, i, frame, | 78 RenderPage(printParams.page_size, content_size, scale_factor, i, frame, |
| 77 &metafile); | 79 &metafile); |
| 78 } | 80 } |
| 79 } else { | 81 } else { |
| 80 for (size_t i = 0; i < params.pages.size(); ++i) { | 82 for (size_t i = 0; i < params.pages.size(); ++i) { |
| 81 if (params.pages[i] >= page_count) | 83 if (params.pages[i] >= page_count) |
| 82 break; | 84 break; |
| 83 RenderPage(printParams.page_size, origin, scale_factor, | 85 RenderPage(printParams.page_size, content_size, scale_factor, |
| 84 static_cast<int>(params.pages[i]), frame, &metafile); | 86 static_cast<int>(params.pages[i]), frame, &metafile); |
| 85 } | 87 } |
| 86 } | 88 } |
| 87 metafile.FinishDocument(); | 89 metafile.FinishDocument(); |
| 88 | 90 |
| 89 PrintHostMsg_DidPreviewDocument_Params preview_params; | 91 PrintHostMsg_DidPreviewDocument_Params preview_params; |
| 90 preview_params.data_size = metafile.GetDataSize(); | 92 preview_params.data_size = metafile.GetDataSize(); |
| 91 preview_params.document_cookie = params.params.document_cookie; | 93 preview_params.document_cookie = params.params.document_cookie; |
| 92 preview_params.expected_pages_count = page_count; | 94 preview_params.expected_pages_count = page_count; |
| 93 | 95 |
| 94 // Ask the browser to create the shared memory for us. | 96 // Ask the browser to create the shared memory for us. |
| 95 if (!CopyMetafileDataToSharedMem(&metafile, | 97 if (!CopyMetafileDataToSharedMem(&metafile, |
| 96 &(preview_params.metafile_data_handle))) { | 98 &(preview_params.metafile_data_handle))) { |
| 97 return false; | 99 return false; |
| 98 } | 100 } |
| 99 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 101 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
| 100 return true; | 102 return true; |
| 101 } | 103 } |
| 102 | 104 |
| 103 void PrintWebViewHelper::RenderPage( | 105 void PrintWebViewHelper::RenderPage( |
| 104 const gfx::Size& page_size, const gfx::Point& content_origin, | 106 const gfx::Size& page_size, const gfx::Rect& content_size, |
| 105 const float& scale_factor, int page_number, WebFrame* frame, | 107 const float& scale_factor, int page_number, WebFrame* frame, |
| 106 printing::Metafile* metafile) { | 108 printing::Metafile* metafile) { |
| 107 bool success = metafile->StartPage(page_size, content_origin, scale_factor); | 109 bool success = metafile->StartPage(page_size, content_size, scale_factor); |
| 108 DCHECK(success); | 110 DCHECK(success); |
| 109 | 111 |
| 110 // printPage can create autoreleased references to |context|. PDF contexts | 112 // printPage can create autoreleased references to |context|. PDF contexts |
| 111 // 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 |
| 112 // certain that there are no lingering references. | 114 // certain that there are no lingering references. |
| 113 { | 115 { |
| 114 base::mac::ScopedNSAutoreleasePool pool; | 116 base::mac::ScopedNSAutoreleasePool pool; |
| 115 frame->printPage(page_number, metafile->context()); | 117 frame->printPage(page_number, metafile->context()); |
| 116 } | 118 } |
| 117 | 119 |
| 118 // Done printing. Close the device context to retrieve the compiled metafile. | 120 // Done printing. Close the device context to retrieve the compiled metafile. |
| 119 metafile->FinishPage(); | 121 metafile->FinishPage(); |
| 120 } | 122 } |
| OLD | NEW |