Adding CreateFromData to NativeMetafileFactory
[chromium-blink-merge.git] / chrome / renderer / print_web_view_helper_mac.mm
blob83e1f1f5ac7bca6fa1d1e1e725fe077d48de64d5
1 // Copyright (c) 2011 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 "chrome/renderer/print_web_view_helper.h"
7 #import <AppKit/AppKit.h>
9 #include "base/logging.h"
10 #include "base/scoped_ptr.h"
11 #include "chrome/common/print_messages.h"
12 #include "printing/native_metafile_factory.h"
13 #include "printing/native_metafile.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
16 using WebKit::WebFrame;
18 void PrintWebViewHelper::PrintPage(const PrintMsg_PrintPage_Params& params,
19                                    const gfx::Size& canvas_size,
20                                    WebFrame* frame) {
21   scoped_ptr<printing::NativeMetafile> metafile(
22       printing::NativeMetafileFactory::Create());
23   if(!metafile.get())
24     return;
26   float scale_factor = frame->getPrintPageShrink(params.page_number);
27   int page_number = params.page_number;
29   // Render page for printing.
30   gfx::Point origin(0.0f, 0.0f);
31   RenderPage(params.params.printable_size, origin, scale_factor, page_number,
32       frame, metafile.get());
33   metafile->FinishDocument();
35   PrintHostMsg_DidPrintPage_Params page_params;
36   page_params.data_size = metafile->GetDataSize();
37   page_params.page_number = page_number;
38   page_params.document_cookie = params.params.document_cookie;
39   page_params.actual_shrink = scale_factor;
40   page_params.page_size = params.params.page_size;
41   page_params.content_area = gfx::Rect(params.params.margin_left,
42                                        params.params.margin_top,
43                                        params.params.printable_size.width(),
44                                        params.params.printable_size.height());
46   // Ask the browser to create the shared memory for us.
47   if (!CopyMetafileDataToSharedMem(metafile.get(),
48                                    &(page_params.metafile_data_handle))) {
49     page_params.data_size = 0;
50   }
52   Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
55 void PrintWebViewHelper::CreatePreviewDocument(
56     const PrintMsg_PrintPages_Params& params, WebKit::WebFrame* frame,
57     WebKit::WebNode* node) {
58   PrintMsg_Print_Params printParams = params.params;
59   UpdatePrintableSizeInPrintParameters(frame, node, &printParams);
61   PrepareFrameAndViewForPrint prep_frame_view(printParams,
62                                               frame, node, frame->view());
63   int page_count = prep_frame_view.GetExpectedPageCount();
65   if (!page_count)
66     return;
68   scoped_ptr<printing::NativeMetafile> metafile(
69       printing::NativeMetafileFactory::Create());
70   if(!metafile.get())
71     return;
73   float scale_factor = frame->getPrintPageShrink(0);
74   gfx::Point origin(printParams.margin_left, printParams.margin_top);
75   if (params.pages.empty()) {
76     for (int i = 0; i < page_count; ++i) {
77       RenderPage(printParams.page_size, origin, scale_factor, i, frame,
78           metafile.get());
79     }
80   } else {
81     for (size_t i = 0; i < params.pages.size(); ++i) {
82       if (params.pages[i] >= page_count)
83         break;
84       RenderPage(printParams.page_size, origin, scale_factor,
85           static_cast<int>(params.pages[i]), frame, metafile.get());
86     }
87   }
88   metafile->FinishDocument();
90   PrintHostMsg_DidPreviewDocument_Params preview_params;
91   preview_params.data_size = metafile->GetDataSize();
92   preview_params.document_cookie = params.params.document_cookie;
93   preview_params.expected_pages_count = page_count;
95   // Ask the browser to create the shared memory for us.
96   if (!CopyMetafileDataToSharedMem(metafile.get(),
97           &(preview_params.metafile_data_handle))) {
98     preview_params.data_size = 0;
99     preview_params.expected_pages_count = 0;
100   }
101   Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params));
104 void PrintWebViewHelper::RenderPage(
105     const gfx::Size& page_size, const gfx::Point& content_origin,
106     const float& scale_factor, int page_number, WebFrame* frame,
107     printing::NativeMetafile* metafile) {
108   bool success = metafile->StartPage(page_size, content_origin, scale_factor);
109   DCHECK(success);
111   // printPage can create autoreleased references to |context|. PDF contexts
112   // don't write all their data until they are destroyed, so we need to make
113   // certain that there are no lingering references.
114   NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
115   frame->printPage(page_number, metafile->context());
116   [pool release];
118   // Done printing. Close the device context to retrieve the compiled metafile.
119   metafile->FinishPage();