summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/print_web_view_helper_mac.mm
blob: 4abae9545dd3cccdde902ae21f0de9a60ec5e73e (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
108
109
110
111
112
113
114
115
116
// Copyright (c) 2011 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 "base/logging.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/render_messages_params.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"

using WebKit::WebFrame;

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

  float scale_factor = frame->getPrintPageShrink(params.page_number);
  int page_number = params.page_number;

  // Render page for printing.
  gfx::Point origin(0.0f, 0.0f);
  RenderPage(params.params.printable_size, origin, scale_factor, page_number,
      frame, &metafile);
  metafile.Close();

  ViewHostMsg_DidPrintPage_Params page_params;
  page_params.data_size = metafile.GetDataSize();
  page_params.page_number = page_number;
  page_params.document_cookie = params.params.document_cookie;
  page_params.actual_shrink = scale_factor;
  page_params.page_size = params.params.page_size;
  page_params.content_area = gfx::Rect(params.params.margin_left,
                                       params.params.margin_top,
                                       params.params.printable_size.width(),
                                       params.params.printable_size.height());

  // Ask the browser to create the shared memory for us.
  if (!CopyMetafileDataToSharedMem(&metafile,
                                   &(page_params.metafile_data_handle))) {
    page_params.data_size = 0;
  }

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

void PrintWebViewHelper::CreatePreviewDocument(
    const ViewMsg_PrintPages_Params& params, WebFrame* frame) {
  ViewMsg_Print_Params printParams = params.params;
  UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams);

  PrepareFrameAndViewForPrint prep_frame_view(printParams,
                                              frame, NULL, frame->view());
  int page_count = prep_frame_view.GetExpectedPageCount();

  if (!page_count)
    return;

  printing::NativeMetafile metafile;
  if (!metafile.Init())
    return;

  float scale_factor = frame->getPrintPageShrink(0);
  gfx::Point origin(printParams.margin_left, printParams.margin_top);
  if (params.pages.empty()) {
    for (int i = 0; i < page_count; ++i) {
      RenderPage(printParams.page_size, origin, scale_factor, i, frame,
          &metafile);
    }
  } else {
    for (size_t i = 0; i < params.pages.size(); ++i) {
      if (params.pages[i] >= page_count)
        break;
      RenderPage(printParams.page_size, origin, scale_factor,
          static_cast<int>(params.pages[i]), frame, &metafile);
    }
  }
  metafile.Close();

  ViewHostMsg_DidPreviewDocument_Params preview_params;
  preview_params.data_size = metafile.GetDataSize();
  preview_params.document_cookie = params.params.document_cookie;
  preview_params.expected_pages_count = page_count;

  // Ask the browser to create the shared memory for us.
  if (!CopyMetafileDataToSharedMem(&metafile,
          &(preview_params.metafile_data_handle))) {
    preview_params.data_size = 0;
    preview_params.expected_pages_count = 0;
  }
  Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params));
}

void PrintWebViewHelper::RenderPage(
    const gfx::Size& page_size, const gfx::Point& content_origin,
    const float& scale_factor, int page_number, WebFrame* frame,
    printing::NativeMetafile* metafile) {
  CGContextRef context = metafile->StartPage(page_size, content_origin,
                                             scale_factor);
  DCHECK(context);

  // printPage can create autoreleased references to |context|. 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(page_number, context);
  [pool release];

  // Done printing. Close the device context to retrieve the compiled metafile.
  metafile->FinishPage();
}