1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/renderer/printing/print_web_view_helper.h"
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/logging.h"
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/process/process_handle.h"
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/common/print_messages.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/renderer/render_thread.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "printing/metafile_skia_wrapper.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "printing/page_size_margins.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "printing/pdf_metafile_skia.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "printing/units.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "skia/ext/platform_device.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "skia/ext/vector_canvas.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "third_party/WebKit/public/web/WebLocalFrame.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace printing {
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using blink::WebFrame;
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool PrintWebViewHelper::RenderPreviewPage(
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    int page_number,
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const PrintMsg_Print_Params& print_params) {
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PrintMsg_PrintPage_Params page_params;
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  page_params.params = print_params;
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  page_params.page_number = page_number;
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_ptr<PdfMetafileSkia> draft_metafile;
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) {
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    draft_metafile.reset(new PdfMetafileSkia);
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    initial_render_metafile = draft_metafile.get();
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::TimeTicks begin_time = base::TimeTicks::Now();
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PrintPageInternal(page_params,
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    print_preview_context_.prepared_frame(),
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    initial_render_metafile,
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    NULL,
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    NULL);
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  print_preview_context_.RenderedPreviewPage(
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::TimeTicks::Now() - begin_time);
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (draft_metafile.get()) {
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    draft_metafile->FinishDocument();
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  } else if (print_preview_context_.IsModifiable() &&
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)             print_preview_context_.generate_draft_pages()) {
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    DCHECK(!draft_metafile.get());
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    draft_metafile =
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        print_preview_context_.metafile()->GetMetafileForCurrentPage();
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return PreviewPageRendered(page_number, draft_metafile.get());
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                          int page_count) {
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  PdfMetafileSkia metafile;
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!metafile.Init())
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return false;
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const PrintMsg_PrintPages_Params& params = *print_pages_params_;
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::vector<int> printed_pages;
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (params.pages.empty()) {
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    for (int i = 0; i < page_count; ++i) {
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      printed_pages.push_back(i);
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  } else {
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // TODO(vitalybuka): redesign to make more code cross platform.
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    for (size_t i = 0; i < params.pages.size(); ++i) {
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      if (params.pages[i] >= 0 && params.pages[i] < page_count) {
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        printed_pages.push_back(params.pages[i]);
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      }
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (printed_pages.empty())
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return false;
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::vector<gfx::Size> page_size_in_dpi(printed_pages.size());
81f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::vector<gfx::Rect> content_area_in_dpi(printed_pages.size());
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PrintMsg_PrintPage_Params page_params;
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  page_params.params = params.params;
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (size_t i = 0; i < printed_pages.size(); ++i) {
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    page_params.page_number = printed_pages[i];
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PrintPageInternal(page_params,
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      frame,
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      &metafile,
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      &page_size_in_dpi[i],
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      &content_area_in_dpi[i]);
92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // blink::printEnd() for PDF should be called before metafile is closed.
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  FinishFramePrinting();
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  metafile.FinishDocument();
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Get the size of the resulting metafile.
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  uint32 buf_size = metafile.GetDataSize();
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK_GT(buf_size, 0u);
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PrintHostMsg_DidPrintPage_Params printed_page_params;
104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  printed_page_params.data_size = 0;
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  printed_page_params.document_cookie = params.params.document_cookie;
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  printed_page_params.page_size = params.params.page_size;
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  printed_page_params.content_area = params.params.printable_area;
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  {
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::SharedMemory shared_buf;
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Allocate a shared memory buffer to hold the generated metafile data.
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (!shared_buf.CreateAndMapAnonymous(buf_size)) {
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NOTREACHED() << "Buffer allocation failed";
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return false;
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Copy the bits into shared memory.
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (!metafile.GetData(shared_buf.memory(), buf_size)) {
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NOTREACHED() << "GetData() failed";
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      shared_buf.Unmap();
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return false;
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    shared_buf.GiveToProcess(base::GetCurrentProcessHandle(),
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                             &printed_page_params.metafile_data_handle);
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    shared_buf.Unmap();
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    printed_page_params.data_size = buf_size;
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    Send(new PrintHostMsg_DuplicateSection(
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        routing_id(),
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        printed_page_params.metafile_data_handle,
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        &printed_page_params.metafile_data_handle));
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (size_t i = 0; i < printed_pages.size(); ++i) {
135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    printed_page_params.page_number = printed_pages[i];
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    printed_page_params.page_size = page_size_in_dpi[i];
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    printed_page_params.content_area = content_area_in_dpi[i];
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params));
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    printed_page_params.metafile_data_handle = INVALID_HANDLE_VALUE;
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return true;
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void PrintWebViewHelper::PrintPageInternal(
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const PrintMsg_PrintPage_Params& params,
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    WebFrame* frame,
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PdfMetafileSkia* metafile,
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    gfx::Size* page_size_in_dpi,
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    gfx::Rect* content_area_in_dpi) {
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PageSizeMargins page_layout_in_points;
151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  double css_scale_factor = 1.0f;
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                  ignore_css_margins_, &css_scale_factor,
154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                  &page_layout_in_points);
155cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  gfx::Size page_size;
156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  gfx::Rect content_area;
157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                          &content_area);
159cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int dpi = static_cast<int>(params.params.dpi);
160cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Calculate the actual page size and content area in dpi.
161cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (page_size_in_dpi) {
162cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    *page_size_in_dpi =
163cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        gfx::Size(static_cast<int>(ConvertUnitDouble(
164cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      page_size.width(), kPointsPerInch, dpi)),
165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                  static_cast<int>(ConvertUnitDouble(
166cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      page_size.height(), kPointsPerInch, dpi)));
167cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
168cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (content_area_in_dpi) {
170f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // Output PDF matches paper size and should be printer edge to edge.
171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    *content_area_in_dpi =
172f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        gfx::Rect(0, 0, page_size_in_dpi->width(), page_size_in_dpi->height());
173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
175cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  gfx::Rect canvas_area =
176cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      params.params.display_header_footer ? gfx::Rect(page_size) : content_area;
177cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
178cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  float webkit_page_shrink_factor =
179cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      frame->getPrintPageShrink(params.page_number);
180cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  float scale_factor = css_scale_factor * webkit_page_shrink_factor;
181cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
182cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SkBaseDevice* device = metafile->StartPageForVectorCanvas(page_size,
183cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                            canvas_area,
184cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                            scale_factor);
185cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!device)
186cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
187cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
188cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The printPage method take a reference to the canvas we pass down, so it
189cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // can't be a stack object.
190cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  skia::RefPtr<skia::VectorCanvas> canvas =
191cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      skia::AdoptRef(new skia::VectorCanvas(device));
192cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile);
193cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  skia::SetIsDraftMode(*canvas, is_print_ready_metafile_sent_);
194cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
195cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (params.params.display_header_footer) {
196cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // |page_number| is 0-based, so 1 is added.
197f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    PrintHeaderAndFooter(canvas.get(),
198f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                         params.page_number + 1,
199f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                         print_preview_context_.total_page_count(),
2001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                         *frame,
201f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                         scale_factor,
202f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                         page_layout_in_points,
203f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                         params.params);
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  float webkit_scale_factor = RenderPageContent(frame,
207cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                params.page_number,
208cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                canvas_area,
209cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                content_area,
210cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                scale_factor,
211cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                canvas.get());
212f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DCHECK_GT(webkit_scale_factor, 0.0f);
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Done printing. Close the device context to retrieve the compiled metafile.
214cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!metafile->FinishPage())
215cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    NOTREACHED() << "metafile failed";
216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
217cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
218cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
2191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PdfMetafileSkia* metafile,
2201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::SharedMemoryHandle* shared_mem_handle) {
221cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  uint32 buf_size = metafile->GetDataSize();
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::SharedMemory shared_buf;
223cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Allocate a shared memory buffer to hold the generated metafile data.
224cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!shared_buf.CreateAndMapAnonymous(buf_size)) {
225cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    NOTREACHED() << "Buffer allocation failed";
226cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return false;
227cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
228cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
229cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Copy the bits into shared memory.
230cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!metafile->GetData(shared_buf.memory(), buf_size)) {
231cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    NOTREACHED() << "GetData() failed";
232cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    shared_buf.Unmap();
233cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return false;
234cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
235cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle);
236cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  shared_buf.Unmap();
237cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
238cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  Send(new PrintHostMsg_DuplicateSection(routing_id(), *shared_mem_handle,
239cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                         shared_mem_handle));
240cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return true;
241cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
242cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
243cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace printing
244