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.
5 #include "chrome/renderer/printing/print_web_view_helper.h"
7 #import <AppKit/AppKit.h>
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.h"
14 #include "printing/metafile_impl.h"
15 #include "printing/metafile_skia_wrapper.h"
16 #include "printing/page_size_margins.h"
17 #include "skia/ext/platform_device.h"
18 #include "skia/ext/vector_canvas.h"
19 #include "third_party/WebKit/public/platform/WebCanvas.h"
20 #include "third_party/WebKit/public/web/WebLocalFrame.h"
24 using blink::WebFrame;
26 void PrintWebViewHelper::PrintPageInternal(
27 const PrintMsg_PrintPage_Params& params,
28 const gfx::Size& canvas_size,
30 NativeMetafile metafile;
34 int page_number = params.page_number;
35 gfx::Size page_size_in_dpi;
36 gfx::Rect content_area_in_dpi;
37 RenderPage(print_pages_params_->params, page_number, frame, false, &metafile,
38 &page_size_in_dpi, &content_area_in_dpi);
39 metafile.FinishDocument();
41 PrintHostMsg_DidPrintPage_Params page_params;
42 page_params.data_size = metafile.GetDataSize();
43 page_params.page_number = page_number;
44 page_params.document_cookie = params.params.document_cookie;
45 page_params.page_size = page_size_in_dpi;
46 page_params.content_area = content_area_in_dpi;
48 // Ask the browser to create the shared memory for us.
49 if (!CopyMetafileDataToSharedMem(&metafile,
50 &(page_params.metafile_data_handle))) {
51 page_params.data_size = 0;
54 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
57 bool PrintWebViewHelper::RenderPreviewPage(
59 const PrintMsg_Print_Params& print_params) {
60 PrintMsg_Print_Params printParams = print_params;
61 scoped_ptr<Metafile> draft_metafile;
62 Metafile* initial_render_metafile = print_preview_context_.metafile();
64 bool render_to_draft = print_preview_context_.IsModifiable() &&
65 is_print_ready_metafile_sent_;
67 if (render_to_draft) {
68 draft_metafile.reset(new PreviewMetafile());
69 if (!draft_metafile->Init()) {
70 print_preview_context_.set_error(
71 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED);
72 LOG(ERROR) << "Draft PreviewMetafile Init failed";
75 initial_render_metafile = draft_metafile.get();
78 base::TimeTicks begin_time = base::TimeTicks::Now();
80 RenderPage(printParams, page_number, print_preview_context_.prepared_frame(),
81 true, initial_render_metafile, &page_size, NULL);
82 print_preview_context_.RenderedPreviewPage(
83 base::TimeTicks::Now() - begin_time);
85 if (draft_metafile.get()) {
86 draft_metafile->FinishDocument();
88 if (print_preview_context_.IsModifiable() &&
89 print_preview_context_.generate_draft_pages()) {
90 DCHECK(!draft_metafile.get());
92 print_preview_context_.metafile()->GetMetafileForCurrentPage());
95 return PreviewPageRendered(page_number, draft_metafile.get());
98 void PrintWebViewHelper::RenderPage(
99 const PrintMsg_Print_Params& params, int page_number, WebFrame* frame,
100 bool is_preview, Metafile* metafile, 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;
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,
113 *content_rect = content_area;
115 scale_factor *= webkit_shrink_factor;
117 gfx::Rect canvas_area =
118 params.display_header_footer ? gfx::Rect(*page_size) : content_area;
121 SkBaseDevice* device = metafile->StartPageForVectorCanvas(
122 *page_size, canvas_area, scale_factor);
126 skia::RefPtr<skia::VectorCanvas> canvas =
127 skia::AdoptRef(new skia::VectorCanvas(device));
128 blink::WebCanvas* canvas_ptr = canvas.get();
129 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile);
130 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_);
131 skia::SetIsPreviewMetafile(*canvas, is_preview);
133 if (print_pages_params_->params.display_header_footer) {
134 PrintHeaderAndFooter(canvas_ptr, page_number + 1,
135 print_preview_context_.total_page_count(),
136 scale_factor, page_layout_in_points,
137 *header_footer_info_, params);
139 RenderPageContent(frame, page_number, canvas_area, content_area,
140 scale_factor, canvas_ptr);
143 // Done printing. Close the device context to retrieve the compiled metafile.
144 metafile->FinishPage();
147 } // namespace printing