summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/print_web_view_helper_mac.mm
blob: 97410dc4f4428461fe97d4038779f3ac97429e10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/print_web_view_helper.h"

#import <AppKit/AppKit.h>

#include "app/l10n_util.h"
#include "base/logging.h"
#include "base/process_util.h"
#include "base/scoped_cftyperef.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/render_messages_params.h"
#include "chrome/renderer/render_view.h"
#include "grit/generated_resources.h"
#include "printing/native_metafile.h"
#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/WebKit/chromium/public/WebCanvas.h"
#include "third_party/WebKit/WebKit/chromium/public/WebRect.h"
#include "third_party/WebKit/WebKit/chromium/public/WebSize.h"

using WebKit::WebFrame;
using WebKit::WebCanvas;
using WebKit::WebRect;
using WebKit::WebSize;

void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
                                   const gfx::Size& canvas_size,
                                   WebFrame* frame) {
  printing::NativeMetafile metafile;
  CGContextRef context = metafile.Init();

  float scale_factor = frame->getPrintPageShrink(params.page_number);
  double content_width_in_points;
  double content_height_in_points;
  double margin_top_in_points;
  double margin_right_in_points;
  double margin_bottom_in_points;
  double margin_left_in_points;
  GetPageSizeAndMarginsInPoints(frame,
                                params.page_number,
                                params.params,
                                &content_width_in_points,
                                &content_height_in_points,
                                &margin_top_in_points,
                                &margin_right_in_points,
                                &margin_bottom_in_points,
                                &margin_left_in_points);
  metafile.StartPage(content_width_in_points,
                     content_height_in_points,
                     scale_factor);

  // printPage can create autoreleased references to |canvas|. PDF contexts
  // don't write all their data until they are destroyed, so we need to make
  // certain that there are no lingering references.
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  frame->printPage(params.page_number, context);
  [pool release];

  metafile.FinishPage();
  metafile.Close();

  // Get the size of the compiled metafile.
  ViewHostMsg_DidPrintPage_Params page_params;
  page_params.data_size = 0;
  page_params.page_number = params.page_number;
  page_params.document_cookie = params.params.document_cookie;
  page_params.actual_shrink = scale_factor;
  base::SharedMemory shared_buf;

  page_params.page_size = gfx::Size(
      static_cast<int>(content_width_in_points +
                       margin_left_in_points + margin_right_in_points),
      static_cast<int>(content_height_in_points +
                       margin_top_in_points + margin_bottom_in_points));
  page_params.content_area = gfx::Rect(
      static_cast<int>(margin_left_in_points),
      static_cast<int>(margin_top_in_points),
      static_cast<int>(content_width_in_points),
      static_cast<int>(content_height_in_points));

  // Ask the browser to create the shared memory for us.
  uint32 buf_size = metafile.GetDataSize();
  base::SharedMemoryHandle shared_mem_handle;
  if (Send(new ViewHostMsg_AllocatePDFTransport(routing_id(), buf_size,
                                                &shared_mem_handle))) {
    if (base::SharedMemory::IsHandleValid(shared_mem_handle)) {
      base::SharedMemory shared_buf(shared_mem_handle, false);
      if (shared_buf.Map(buf_size)) {
        metafile.GetData(shared_buf.memory(), buf_size);
        page_params.data_size = buf_size;
        shared_buf.GiveToProcess(base::GetCurrentProcessHandle(),
                                 &(page_params.metafile_data_handle));
      } else {
        NOTREACHED() << "Map failed";
      }
    } else {
      NOTREACHED() << "Browser failed to allocate shared memory";
    }
  } else {
    NOTREACHED() << "Browser allocation request message failed";
  }

  Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params));
}