summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/print_web_view_helper_win.cc
blob: 5b502c4688144c547e6757a8593ba2e2143cf10d (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// 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"

#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/process_util.h"
#include "base/scoped_ptr.h"
#include "chrome/common/print_messages.h"
#include "printing/metafile.h"
#include "printing/metafile_impl.h"
#include "printing/metafile_skia_wrapper.h"
#include "printing/units.h"
#include "skia/ext/vector_canvas.h"
#include "skia/ext/vector_platform_device_emf_win.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "ui/gfx/gdi_util.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"

using printing::ConvertUnit;
using printing::ConvertUnitDouble;
using printing::kPointsPerInch;
using printing::Metafile;
using WebKit::WebFrame;

namespace {

int CALLBACK EnhMetaFileProc(HDC dc,
                             HANDLETABLE* handle_table,
                             const ENHMETARECORD *record,
                             int num_objects,
                             LPARAM data) {
  HDC* bitmap_dc = reinterpret_cast<HDC*>(data);
  // Play this command to the bitmap DC.
  PlayEnhMetaFileRecord(*bitmap_dc, handle_table, record, num_objects);
  if (record->iType == EMR_ALPHABLEND) {
    const EMRALPHABLEND* emr_alpha_blend =
        reinterpret_cast<const EMRALPHABLEND*>(record);
    XFORM bitmap_dc_transform, metafile_dc_transform;
    XFORM identity = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f };
    // Temporarily set the world transforms of both DC's to identity.
    GetWorldTransform(dc, &metafile_dc_transform);
    SetWorldTransform(dc, &identity);
    GetWorldTransform(*bitmap_dc, &bitmap_dc_transform);
    SetWorldTransform(*bitmap_dc, &identity);
    const RECTL& rect = emr_alpha_blend->rclBounds;
    // Since the printer does not support alpha blend, copy the alpha blended
    // region from our (software-rendered) bitmap DC to the metafile DC.
    BitBlt(dc,
           rect.left,
           rect.top,
           rect.right - rect.left + 1,
           rect.bottom - rect.top + 1,
           *bitmap_dc,
           rect.left,
           rect.top,
           SRCCOPY);
    // Restore the world transforms of both DC's.
    SetWorldTransform(dc, &metafile_dc_transform);
    SetWorldTransform(*bitmap_dc, &bitmap_dc_transform);
  } else {
    // Play this command to the metafile DC.
    PlayEnhMetaFileRecord(dc, handle_table, record, num_objects);
  }
  return 1;  // Continue enumeration
}

}  // namespace

void PrintWebViewHelper::PrintPageInternal(
    const PrintMsg_PrintPage_Params& params,
    const gfx::Size& canvas_size,
    WebFrame* frame) {
  // Generate a memory-based metafile. It will use the current screen's DPI.
  // Each metafile contains a single page.
  scoped_ptr<Metafile> metafile(new printing::NativeMetafile);
  metafile->Init();
  DCHECK(metafile->context());
  skia::InitializeDC(metafile->context());

  int page_number = params.page_number;

  // Calculate the dpi adjustment.
  float scale_factor = static_cast<float>(params.params.desired_dpi /
                                          params.params.dpi);

  // Render page for printing.
  RenderPage(params.params, &scale_factor, page_number, false, frame,
             &metafile);

  // Close the device context to retrieve the compiled metafile.
  if (!metafile->FinishDocument())
    NOTREACHED();

  // Get the size of the compiled metafile.
  uint32 buf_size = metafile->GetDataSize();
  DCHECK_GT(buf_size, 128u);

  PrintHostMsg_DidPrintPage_Params page_params;
  page_params.data_size = buf_size;
  page_params.metafile_data_handle = NULL;
  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());
  page_params.has_visible_overlays = frame->isPageBoxVisible(page_number);

  if (!CopyMetafileDataToSharedMem(metafile.get(),
                                   &(page_params.metafile_data_handle))) {
    page_params.data_size = 0;
  }
  Send(new PrintHostMsg_DuplicateSection(routing_id(),
                                         page_params.metafile_data_handle,
                                         &page_params.metafile_data_handle));
  Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
}

bool PrintWebViewHelper::CreatePreviewDocument(
    const PrintMsg_PrintPages_Params& params, WebKit::WebFrame* frame,
    WebKit::WebNode* node) {
  int page_count = 0;
  PrintMsg_Print_Params print_params = params.params;
  UpdatePrintableSizeInPrintParameters(frame, node, &print_params);
  PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node,
                                              frame->view());
  page_count = prep_frame_view.GetExpectedPageCount();
  if (!page_count)
    return false;

  scoped_ptr<Metafile> metafile(new printing::PreviewMetafile);
  metafile->Init();

  // Calculate the dpi adjustment.
  float shrink = static_cast<float>(print_params.desired_dpi /
                                    print_params.dpi);

  // Record the begin time.
  base::TimeTicks begin_time = base::TimeTicks::Now();

  if (params.pages.empty()) {
    for (int i = 0; i < page_count; ++i) {
      float scale_factor = shrink;
      RenderPage(print_params, &scale_factor, i, true, frame, &metafile);
    }
  } else {
    for (size_t i = 0; i < params.pages.size(); ++i) {
      if (params.pages[i] >= page_count)
        break;
      float scale_factor = shrink;
      RenderPage(print_params, &scale_factor,
                 static_cast<int>(params.pages[i]), true, frame, &metafile);
    }
  }

  // Ensure that printing has finished before we start cleaning up and
  // allocating buffers; this causes prep_frame_view to flush anything pending
  // into the metafile. Then we can get the final size and copy it into a
  // shared segment.
  prep_frame_view.FinishPrinting();

  if (!metafile->FinishDocument())
    NOTREACHED();

  // Calculate the time taken to render the requested page for preview and add
  // the net time in the histogram.
  UMA_HISTOGRAM_TIMES("PrintPreview.RenderTime",
                      base::TimeTicks::Now() - begin_time);

  // Get the size of the compiled metafile.
  uint32 buf_size = metafile->GetDataSize();
  DCHECK_GT(buf_size, 128u);

  PrintHostMsg_DidPreviewDocument_Params preview_params;
  preview_params.data_size = buf_size;
  preview_params.document_cookie = params.params.document_cookie;
  preview_params.expected_pages_count = page_count;
  preview_params.modifiable = IsModifiable(frame, node);

  if (!CopyMetafileDataToSharedMem(metafile.get(),
                                   &(preview_params.metafile_data_handle))) {
    return false;
  }
  Send(new PrintHostMsg_DuplicateSection(routing_id(),
                                         preview_params.metafile_data_handle,
                                         &preview_params.metafile_data_handle));
  Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params));
  return true;
}

void PrintWebViewHelper::RenderPage(
    const PrintMsg_Print_Params& params, float* scale_factor, int page_number,
    bool is_preview, WebFrame* frame, scoped_ptr<Metafile>* metafile) {
  double content_width_in_points;
  double content_height_in_points;
  double margin_top_in_points;
  double margin_left_in_points;
  GetPageSizeAndMarginsInPoints(frame, page_number, params,
                                &content_width_in_points,
                                &content_height_in_points,
                                &margin_top_in_points, NULL, NULL,
                                &margin_left_in_points);

  int width;
  int height;
  if (is_preview) {
    int dpi = static_cast<int>(params.dpi);
    int desired_dpi = printing::kPointsPerInch;
    width = ConvertUnit(params.page_size.width(), dpi, desired_dpi);
    height = ConvertUnit(params.page_size.height(), dpi, desired_dpi);
  } else {
    // Since WebKit extends the page width depending on the magical scale factor
    // we make sure the canvas covers the worst case scenario (x2.0 currently).
    // PrintContext will then set the correct clipping region.
    width = static_cast<int>(content_width_in_points * params.max_shrink);
    height = static_cast<int>(content_height_in_points * params.max_shrink);
  }

  gfx::Size page_size(width, height);
  gfx::Rect content_area(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));
  SkDevice* device = (*metafile)->StartPageForVectorCanvas(
      page_size, content_area, frame->getPrintPageShrink(page_number));
  DCHECK(device);
  // The printPage method may take a reference to the canvas we pass down, so it
  // can't be a stack object.
  SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device);
  canvas->unref();  // SkRefPtr and new both took a reference.
  if (is_preview) {
    printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(),
                                                       metafile->get());
  }

  float webkit_scale_factor = frame->printPage(page_number, canvas.get());
  if (*scale_factor <= 0 || webkit_scale_factor <= 0) {
    NOTREACHED() << "Printing page " << page_number << " failed.";
  } else {
    // Update the dpi adjustment with the "page |scale_factor|" calculated in
    // webkit.
    *scale_factor /= webkit_scale_factor;
  }

  bool result = (*metafile)->FinishPage();
  DCHECK(result);

  if (!params.supports_alpha_blend) {
    // PreviewMetafile (PDF) supports alpha blend, so we only hit this case
    // for NativeMetafile.
    DCHECK(!is_preview);
    skia::VectorPlatformDeviceEmf* platform_device =
        static_cast<skia::VectorPlatformDeviceEmf*>(device);
    if (platform_device->alpha_blend_used()) {
      // Currently, we handle alpha blend transparency for a single page.
      // Therefore, expecting a metafile with page count 1.
      DCHECK_EQ(1U, (*metafile)->GetPageCount());

      // Close the device context to retrieve the compiled metafile.
      if (!(*metafile)->FinishDocument())
        NOTREACHED();

      // Page used alpha blend, but printer doesn't support it.  Rewrite the
      // metafile and flatten out the transparency.
      HDC bitmap_dc = CreateCompatibleDC(GetDC(NULL));
      if (!bitmap_dc)
        NOTREACHED() << "Bitmap DC creation failed";
      SetGraphicsMode(bitmap_dc, GM_ADVANCED);
      void* bits = NULL;
      BITMAPINFO hdr;
      gfx::CreateBitmapHeader(width, height, &hdr.bmiHeader);
      HBITMAP hbitmap = CreateDIBSection(
          bitmap_dc, &hdr, DIB_RGB_COLORS, &bits, NULL, 0);
      if (!hbitmap)
        NOTREACHED() << "Raster bitmap creation for printing failed";

      HGDIOBJ old_bitmap = SelectObject(bitmap_dc, hbitmap);
      RECT rect = {0, 0, width, height };
      HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
      FillRect(bitmap_dc, &rect, whiteBrush);

      scoped_ptr<Metafile> metafile2(new printing::NativeMetafile);
      metafile2->Init();
      HDC hdc = metafile2->context();
      DCHECK(hdc);
      skia::InitializeDC(hdc);

      RECT metafile_bounds = (*metafile)->GetPageBounds(1).ToRECT();
      // Process the old metafile, placing all non-AlphaBlend calls into the
      // new metafile, and copying the results of all the AlphaBlend calls
      // from the bitmap DC.
      EnumEnhMetaFile(hdc,
                      (*metafile)->emf(),
                      EnhMetaFileProc,
                      &bitmap_dc,
                      &metafile_bounds);

      SelectObject(bitmap_dc, old_bitmap);
      metafile->reset(metafile2.release());
    }
  }
}

bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
    Metafile* metafile, base::SharedMemoryHandle* shared_mem_handle) {
  uint32 buf_size = metafile->GetDataSize();
  base::SharedMemory shared_buf;

  // http://msdn2.microsoft.com/en-us/library/ms535522.aspx
  // Windows 2000/XP: When a page in a spooled file exceeds approximately 350
  // MB, it can fail to print and not send an error message.
  if (buf_size >= 350*1024*1024) {
    NOTREACHED() << "Buffer too large: " << buf_size;
    return false;
  }

  // Allocate a shared memory buffer to hold the generated metafile data.
  if (!shared_buf.CreateAndMapAnonymous(buf_size)) {
    NOTREACHED() << "Buffer allocation failed";
    return false;
  }

  // Copy the bits into shared memory.
  if (!metafile->GetData(shared_buf.memory(), buf_size)) {
    NOTREACHED() << "GetData() failed";
    shared_buf.Unmap();
    return false;
  }
  shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), shared_mem_handle);
  shared_buf.Unmap();
  return true;
}