Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / printing / renderer / print_web_view_helper_mac.mm
blobf2b94e754a841a93013b2cd22e98bbbcc8ae8538
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"
19 namespace printing {
21 using blink::WebFrame;
23 void PrintWebViewHelper::PrintPageInternal(
24     const PrintMsg_PrintPage_Params& params,
25     WebFrame* frame) {
26   PdfMetafileSkia metafile;
27   if (!metafile.Init())
28     return;
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     // TODO(thestig): Fail and return false instead.
48     page_params.data_size = 0;
49   }
51   Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
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();
61   bool render_to_draft = print_preview_context_.IsModifiable() &&
62                          is_print_ready_metafile_sent_;
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   }
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);
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());
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;
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;
115   scale_factor *= webkit_shrink_factor;
117   gfx::Rect canvas_area =
118       params.display_header_footer ? gfx::Rect(*page_size) : content_area;
120   {
121     skia::PlatformCanvas* canvas = metafile->GetVectorCanvasForNewPage(
122         *page_size, canvas_area, scale_factor);
123     if (!canvas)
124       return;
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   }
141   // Done printing. Close the device context to retrieve the compiled metafile.
142   metafile->FinishPage();
145 }  // namespace printing