| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "components/printing/renderer/print_web_view_helper.h" | 5 #include "components/printing/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 "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "components/printing/common/print_messages.h" | 12 #include "components/printing/common/print_messages.h" |
| 13 #include "printing/metafile_skia_wrapper.h" | 13 #include "printing/metafile_skia_wrapper.h" |
| 14 #include "printing/page_size_margins.h" | 14 #include "printing/page_size_margins.h" |
| 15 #include "skia/ext/platform_canvas.h" | 15 #include "skia/ext/platform_canvas.h" |
| 16 #include "third_party/WebKit/public/platform/WebCanvas.h" | 16 #include "third_party/WebKit/public/platform/WebCanvas.h" |
| 17 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 17 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 18 | 18 |
| 19 namespace printing { | 19 namespace printing { |
| 20 | 20 |
| 21 using blink::WebFrame; | 21 #if defined(ENABLE_BASIC_PRINTING) |
| 22 | |
| 23 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame, | 22 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame, |
| 24 int page_count) { | 23 int page_count) { |
| 25 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 24 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
| 26 const PrintMsg_Print_Params& print_params = params.params; | 25 const PrintMsg_Print_Params& print_params = params.params; |
| 27 | 26 |
| 28 std::vector<int> printed_pages = GetPrintedPages(params, page_count); | 27 std::vector<int> printed_pages = GetPrintedPages(params, page_count); |
| 29 if (printed_pages.empty()) | 28 if (printed_pages.empty()) |
| 30 return false; | 29 return false; |
| 31 | 30 |
| 32 PrintMsg_PrintPage_Params page_params; | 31 PrintMsg_PrintPage_Params page_params; |
| 33 page_params.params = print_params; | 32 page_params.params = print_params; |
| 34 for (int page_number : printed_pages) { | 33 for (int page_number : printed_pages) { |
| 35 page_params.page_number = page_number; | 34 page_params.page_number = page_number; |
| 36 PrintPageInternal(page_params, frame); | 35 PrintPageInternal(page_params, frame); |
| 37 } | 36 } |
| 38 return true; | 37 return true; |
| 39 } | 38 } |
| 39 #endif // defined(ENABLE_BASIC_PRINTING) |
| 40 | 40 |
| 41 void PrintWebViewHelper::PrintPageInternal( | 41 void PrintWebViewHelper::PrintPageInternal( |
| 42 const PrintMsg_PrintPage_Params& params, | 42 const PrintMsg_PrintPage_Params& params, |
| 43 WebFrame* frame) { | 43 blink::WebFrame* frame) { |
| 44 PdfMetafileSkia metafile; | 44 PdfMetafileSkia metafile; |
| 45 if (!metafile.Init()) | 45 if (!metafile.Init()) |
| 46 return; | 46 return; |
| 47 | 47 |
| 48 int page_number = params.page_number; | 48 int page_number = params.page_number; |
| 49 gfx::Size page_size_in_dpi; | 49 gfx::Size page_size_in_dpi; |
| 50 gfx::Rect content_area_in_dpi; | 50 gfx::Rect content_area_in_dpi; |
| 51 RenderPage(print_pages_params_->params, page_number, frame, false, &metafile, | 51 RenderPage(print_pages_params_->params, page_number, frame, false, &metafile, |
| 52 &page_size_in_dpi, &content_area_in_dpi); | 52 &page_size_in_dpi, &content_area_in_dpi); |
| 53 metafile.FinishDocument(); | 53 metafile.FinishDocument(); |
| 54 | 54 |
| 55 PrintHostMsg_DidPrintPage_Params page_params; | 55 PrintHostMsg_DidPrintPage_Params page_params; |
| 56 page_params.data_size = metafile.GetDataSize(); | 56 page_params.data_size = metafile.GetDataSize(); |
| 57 page_params.page_number = page_number; | 57 page_params.page_number = page_number; |
| 58 page_params.document_cookie = params.params.document_cookie; | 58 page_params.document_cookie = params.params.document_cookie; |
| 59 page_params.page_size = page_size_in_dpi; | 59 page_params.page_size = page_size_in_dpi; |
| 60 page_params.content_area = content_area_in_dpi; | 60 page_params.content_area = content_area_in_dpi; |
| 61 | 61 |
| 62 // Ask the browser to create the shared memory for us. | 62 // Ask the browser to create the shared memory for us. |
| 63 if (!CopyMetafileDataToSharedMem(metafile, | 63 if (!CopyMetafileDataToSharedMem(metafile, |
| 64 &(page_params.metafile_data_handle))) { | 64 &(page_params.metafile_data_handle))) { |
| 65 // TODO(thestig): Fail and return false instead. | 65 // TODO(thestig): Fail and return false instead. |
| 66 page_params.data_size = 0; | 66 page_params.data_size = 0; |
| 67 } | 67 } |
| 68 | 68 |
| 69 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | 69 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); |
| 70 } | 70 } |
| 71 | 71 |
| 72 #if defined(ENABLE_PRINT_PREVIEW) |
| 72 bool PrintWebViewHelper::RenderPreviewPage( | 73 bool PrintWebViewHelper::RenderPreviewPage( |
| 73 int page_number, | 74 int page_number, |
| 74 const PrintMsg_Print_Params& print_params) { | 75 const PrintMsg_Print_Params& print_params) { |
| 75 PrintMsg_Print_Params printParams = print_params; | 76 PrintMsg_Print_Params printParams = print_params; |
| 76 scoped_ptr<PdfMetafileSkia> draft_metafile; | 77 scoped_ptr<PdfMetafileSkia> draft_metafile; |
| 77 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); | 78 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); |
| 78 | 79 |
| 79 bool render_to_draft = print_preview_context_.IsModifiable() && | 80 bool render_to_draft = print_preview_context_.IsModifiable() && |
| 80 is_print_ready_metafile_sent_; | 81 is_print_ready_metafile_sent_; |
| 81 | 82 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 102 } else { | 103 } else { |
| 103 if (print_preview_context_.IsModifiable() && | 104 if (print_preview_context_.IsModifiable() && |
| 104 print_preview_context_.generate_draft_pages()) { | 105 print_preview_context_.generate_draft_pages()) { |
| 105 DCHECK(!draft_metafile.get()); | 106 DCHECK(!draft_metafile.get()); |
| 106 draft_metafile = | 107 draft_metafile = |
| 107 print_preview_context_.metafile()->GetMetafileForCurrentPage(); | 108 print_preview_context_.metafile()->GetMetafileForCurrentPage(); |
| 108 } | 109 } |
| 109 } | 110 } |
| 110 return PreviewPageRendered(page_number, draft_metafile.get()); | 111 return PreviewPageRendered(page_number, draft_metafile.get()); |
| 111 } | 112 } |
| 113 #endif // defined(ENABLE_PRINT_PREVIEW) |
| 112 | 114 |
| 113 void PrintWebViewHelper::RenderPage(const PrintMsg_Print_Params& params, | 115 void PrintWebViewHelper::RenderPage(const PrintMsg_Print_Params& params, |
| 114 int page_number, | 116 int page_number, |
| 115 WebFrame* frame, | 117 blink::WebFrame* frame, |
| 116 bool is_preview, | 118 bool is_preview, |
| 117 PdfMetafileSkia* metafile, | 119 PdfMetafileSkia* metafile, |
| 118 gfx::Size* page_size, | 120 gfx::Size* page_size, |
| 119 gfx::Rect* content_rect) { | 121 gfx::Rect* content_rect) { |
| 120 double scale_factor = 1.0f; | 122 double scale_factor = 1.0f; |
| 121 double webkit_shrink_factor = frame->getPrintPageShrink(page_number); | 123 double webkit_shrink_factor = frame->getPrintPageShrink(page_number); |
| 122 PageSizeMargins page_layout_in_points; | 124 PageSizeMargins page_layout_in_points; |
| 123 gfx::Rect content_area; | 125 gfx::Rect content_area; |
| 124 | 126 |
| 125 ComputePageLayoutInPointsForCss(frame, page_number, params, | 127 ComputePageLayoutInPointsForCss(frame, page_number, params, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 153 #endif // defined(ENABLE_PRINT_PREVIEW) | 155 #endif // defined(ENABLE_PRINT_PREVIEW) |
| 154 RenderPageContent(frame, page_number, canvas_area, content_area, | 156 RenderPageContent(frame, page_number, canvas_area, content_area, |
| 155 scale_factor, static_cast<blink::WebCanvas*>(canvas)); | 157 scale_factor, static_cast<blink::WebCanvas*>(canvas)); |
| 156 } | 158 } |
| 157 | 159 |
| 158 // Done printing. Close the device context to retrieve the compiled metafile. | 160 // Done printing. Close the device context to retrieve the compiled metafile. |
| 159 metafile->FinishPage(); | 161 metafile->FinishPage(); |
| 160 } | 162 } |
| 161 | 163 |
| 162 } // namespace printing | 164 } // namespace printing |
| OLD | NEW |