OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/renderer/printing/print_web_view_helper.h" | |
6 | |
7 #import <AppKit/AppKit.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/mac/scoped_nsautorelease_pool.h" | |
11 #include "base/metrics/histogram.h" | |
12 #include "chrome/common/print_messages.h" | |
13 #include "printing/metafile_skia_wrapper.h" | |
14 #include "printing/page_size_margins.h" | |
15 #include "skia/ext/platform_device.h" | |
16 #include "skia/ext/vector_canvas.h" | |
17 #include "third_party/WebKit/public/platform/WebCanvas.h" | |
18 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
19 | |
20 namespace printing { | |
21 | |
22 using blink::WebFrame; | |
23 | |
24 void PrintWebViewHelper::PrintPageInternal( | |
25 const PrintMsg_PrintPage_Params& params, | |
26 WebFrame* frame) { | |
27 PdfMetafileSkia metafile; | |
28 if (!metafile.Init()) | |
29 return; | |
30 | |
31 int page_number = params.page_number; | |
32 gfx::Size page_size_in_dpi; | |
33 gfx::Rect content_area_in_dpi; | |
34 RenderPage(print_pages_params_->params, page_number, frame, false, &metafile, | |
35 &page_size_in_dpi, &content_area_in_dpi); | |
36 metafile.FinishDocument(); | |
37 | |
38 PrintHostMsg_DidPrintPage_Params page_params; | |
39 page_params.data_size = metafile.GetDataSize(); | |
40 page_params.page_number = page_number; | |
41 page_params.document_cookie = params.params.document_cookie; | |
42 page_params.page_size = page_size_in_dpi; | |
43 page_params.content_area = content_area_in_dpi; | |
44 | |
45 // Ask the browser to create the shared memory for us. | |
46 if (!CopyMetafileDataToSharedMem(&metafile, | |
47 &(page_params.metafile_data_handle))) { | |
48 page_params.data_size = 0; | |
49 } | |
50 | |
51 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params)); | |
52 } | |
53 | |
54 bool PrintWebViewHelper::RenderPreviewPage( | |
55 int page_number, | |
56 const PrintMsg_Print_Params& print_params) { | |
57 PrintMsg_Print_Params printParams = print_params; | |
58 scoped_ptr<PdfMetafileSkia> draft_metafile; | |
59 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); | |
60 | |
61 bool render_to_draft = print_preview_context_.IsModifiable() && | |
62 is_print_ready_metafile_sent_; | |
63 | |
64 if (render_to_draft) { | |
65 draft_metafile.reset(new PdfMetafileSkia()); | |
66 if (!draft_metafile->Init()) { | |
67 print_preview_context_.set_error( | |
68 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED); | |
69 LOG(ERROR) << "Draft PdfMetafileSkia Init failed"; | |
70 return false; | |
71 } | |
72 initial_render_metafile = draft_metafile.get(); | |
73 } | |
74 | |
75 base::TimeTicks begin_time = base::TimeTicks::Now(); | |
76 gfx::Size page_size; | |
77 RenderPage(printParams, page_number, print_preview_context_.prepared_frame(), | |
78 true, initial_render_metafile, &page_size, NULL); | |
79 print_preview_context_.RenderedPreviewPage( | |
80 base::TimeTicks::Now() - begin_time); | |
81 | |
82 if (draft_metafile.get()) { | |
83 draft_metafile->FinishDocument(); | |
84 } else { | |
85 if (print_preview_context_.IsModifiable() && | |
86 print_preview_context_.generate_draft_pages()) { | |
87 DCHECK(!draft_metafile.get()); | |
88 draft_metafile = | |
89 print_preview_context_.metafile()->GetMetafileForCurrentPage(); | |
90 } | |
91 } | |
92 return PreviewPageRendered(page_number, draft_metafile.get()); | |
93 } | |
94 | |
95 void PrintWebViewHelper::RenderPage(const PrintMsg_Print_Params& params, | |
96 int page_number, | |
97 WebFrame* frame, | |
98 bool is_preview, | |
99 PdfMetafileSkia* metafile, | |
100 gfx::Size* page_size, | |
101 gfx::Rect* content_rect) { | |
102 double scale_factor = 1.0f; | |
103 double webkit_shrink_factor = frame->getPrintPageShrink(page_number); | |
104 PageSizeMargins page_layout_in_points; | |
105 gfx::Rect content_area; | |
106 | |
107 ComputePageLayoutInPointsForCss(frame, page_number, params, | |
108 ignore_css_margins_, &scale_factor, | |
109 &page_layout_in_points); | |
110 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, page_size, | |
111 &content_area); | |
112 if (content_rect) | |
113 *content_rect = content_area; | |
114 | |
115 scale_factor *= webkit_shrink_factor; | |
116 | |
117 gfx::Rect canvas_area = | |
118 params.display_header_footer ? gfx::Rect(*page_size) : content_area; | |
119 | |
120 { | |
121 skia::VectorCanvas* canvas = metafile->GetVectorCanvasForNewPage( | |
122 *page_size, canvas_area, scale_factor); | |
123 if (!canvas) | |
124 return; | |
125 | |
126 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); | |
127 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_); | |
128 skia::SetIsPreviewMetafile(*canvas, is_preview); | |
129 #if defined(ENABLE_PRINT_PREVIEW) | |
130 if (params.display_header_footer) { | |
131 PrintHeaderAndFooter(static_cast<blink::WebCanvas*>(canvas), | |
132 page_number + 1, | |
133 print_preview_context_.total_page_count(), *frame, | |
134 scale_factor, page_layout_in_points, params); | |
135 } | |
136 #endif // defined(ENABLE_PRINT_PREVIEW) | |
137 RenderPageContent(frame, page_number, canvas_area, content_area, | |
138 scale_factor, static_cast<blink::WebCanvas*>(canvas)); | |
139 } | |
140 | |
141 // Done printing. Close the device context to retrieve the compiled metafile. | |
142 metafile->FinishPage(); | |
143 } | |
144 | |
145 } // namespace printing | |
OLD | NEW |