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 "components/printing/renderer/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 "components/printing/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 "third_party/WebKit/public/platform/WebCanvas.h"
17 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 using blink::WebFrame;
23 void PrintWebViewHelper::PrintPageInternal(
24 const PrintMsg_PrintPage_Params& params,
26 PdfMetafileSkia metafile;
30 int page_number = params.page_number;
31 gfx::Size page_size_in_dpi;
32 gfx::Rect content_area_in_dpi;
33 RenderPage(print_pages_params_->params, page_number, frame, false, &metafile,
34 &page_size_in_dpi, &content_area_in_dpi);
35 metafile.FinishDocument();
37 PrintHostMsg_DidPrintPage_Params page_params;
38 page_params.data_size = metafile.GetDataSize();
39 page_params.page_number = page_number;
40 page_params.document_cookie = params.params.document_cookie;
41 page_params.page_size = page_size_in_dpi;
42 page_params.content_area = content_area_in_dpi;
44 // Ask the browser to create the shared memory for us.
45 if (!CopyMetafileDataToSharedMem(&metafile,
46 &(page_params.metafile_data_handle))) {
47 page_params.data_size = 0;
50 Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
53 bool PrintWebViewHelper::RenderPreviewPage(
55 const PrintMsg_Print_Params& print_params) {
56 PrintMsg_Print_Params printParams = print_params;
57 scoped_ptr<PdfMetafileSkia> draft_metafile;
58 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
60 bool render_to_draft = print_preview_context_.IsModifiable() &&
61 is_print_ready_metafile_sent_;
63 if (render_to_draft) {
64 draft_metafile.reset(new PdfMetafileSkia());
65 if (!draft_metafile->Init()) {
66 print_preview_context_.set_error(
67 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED);
68 LOG(ERROR) << "Draft PdfMetafileSkia Init failed";
71 initial_render_metafile = draft_metafile.get();
74 base::TimeTicks begin_time = base::TimeTicks::Now();
76 RenderPage(printParams, page_number, print_preview_context_.prepared_frame(),
77 true, initial_render_metafile, &page_size, NULL);
78 print_preview_context_.RenderedPreviewPage(
79 base::TimeTicks::Now() - begin_time);
81 if (draft_metafile.get()) {
82 draft_metafile->FinishDocument();
84 if (print_preview_context_.IsModifiable() &&
85 print_preview_context_.generate_draft_pages()) {
86 DCHECK(!draft_metafile.get());
88 print_preview_context_.metafile()->GetMetafileForCurrentPage();
91 return PreviewPageRendered(page_number, draft_metafile.get());
94 void PrintWebViewHelper::RenderPage(const PrintMsg_Print_Params& params,
98 PdfMetafileSkia* metafile,
100 gfx::Rect* content_rect) {
101 double scale_factor = 1.0f;
102 double webkit_shrink_factor = frame->getPrintPageShrink(page_number);
103 PageSizeMargins page_layout_in_points;
104 gfx::Rect content_area;
106 ComputePageLayoutInPointsForCss(frame, page_number, params,
107 ignore_css_margins_, &scale_factor,
108 &page_layout_in_points);
109 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, page_size,
112 *content_rect = content_area;
114 scale_factor *= webkit_shrink_factor;
116 gfx::Rect canvas_area =
117 params.display_header_footer ? gfx::Rect(*page_size) : content_area;
120 skia::PlatformCanvas* canvas = metafile->GetVectorCanvasForNewPage(
121 *page_size, canvas_area, scale_factor);
125 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile);
126 skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_);
127 skia::SetIsPreviewMetafile(*canvas, is_preview);
128 #if defined(ENABLE_PRINT_PREVIEW)
129 if (params.display_header_footer) {
130 PrintHeaderAndFooter(static_cast<blink::WebCanvas*>(canvas),
132 print_preview_context_.total_page_count(), *frame,
133 scale_factor, page_layout_in_points, params);
135 #endif // defined(ENABLE_PRINT_PREVIEW)
136 RenderPageContent(frame, page_number, canvas_area, content_area,
137 scale_factor, static_cast<blink::WebCanvas*>(canvas));
140 // Done printing. Close the device context to retrieve the compiled metafile.
141 metafile->FinishPage();
144 } // namespace printing