OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
6 | 6 |
7 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/scoped_ptr.h" |
10 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
11 #include "chrome/common/render_messages_params.h" | 12 #include "chrome/common/render_messages_params.h" |
12 #include "chrome/renderer/render_view.h" | 13 #include "chrome/renderer/render_view.h" |
13 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
14 #include "printing/native_metafile.h" | 15 #include "printing/native_metafile.h" |
| 16 #include "printing/native_metafile_factory.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
19 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
20 | 22 |
21 using WebKit::WebFrame; | 23 using WebKit::WebFrame; |
22 using WebKit::WebCanvas; | 24 using WebKit::WebCanvas; |
23 using WebKit::WebRect; | 25 using WebKit::WebRect; |
24 using WebKit::WebSize; | 26 using WebKit::WebSize; |
25 | 27 |
26 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 28 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
27 const gfx::Size& canvas_size, | 29 const gfx::Size& canvas_size, |
28 WebFrame* frame) { | 30 WebFrame* frame) { |
29 printing::NativeMetafile metafile; | 31 scoped_ptr<printing::NativeMetafile> metafile( |
30 if (!metafile.Init()) | 32 printing::MetafileFactory::GetMetafile()); |
| 33 if (!metafile->Init()) |
31 return; | 34 return; |
32 | 35 |
33 float scale_factor = frame->getPrintPageShrink(params.page_number); | 36 float scale_factor = frame->getPrintPageShrink(params.page_number); |
34 double width = params.params.printable_size.width(); | 37 double width = params.params.printable_size.width(); |
35 double height = params.params.printable_size.height(); | 38 double height = params.params.printable_size.height(); |
36 int page_number = params.page_number; | 39 int page_number = params.page_number; |
37 | 40 |
38 // Render page for printing. | 41 // Render page for printing. |
39 gfx::Point origin(0.0f, 0.0f); | 42 gfx::Point origin(0.0f, 0.0f); |
40 RenderPage(params.params.printable_size, origin, scale_factor, page_number, | 43 RenderPage(params.params.printable_size, origin, scale_factor, page_number, |
41 frame, &metafile); | 44 frame, metafile); |
42 | 45 |
43 metafile.Close(); | 46 metafile->Close(); |
44 | 47 |
45 double margin_left = params.params.margin_left; | 48 double margin_left = params.params.margin_left; |
46 double margin_top = params.params.margin_top; | 49 double margin_top = params.params.margin_top; |
47 | 50 |
48 // Get the size of the compiled metafile. | 51 // Get the size of the compiled metafile. |
49 ViewHostMsg_DidPrintPage_Params page_params; | 52 ViewHostMsg_DidPrintPage_Params page_params; |
50 page_params.data_size = metafile.GetDataSize(); | 53 page_params.data_size = metafile->GetDataSize(); |
51 page_params.page_number = page_number; | 54 page_params.page_number = page_number; |
52 page_params.document_cookie = params.params.document_cookie; | 55 page_params.document_cookie = params.params.document_cookie; |
53 page_params.actual_shrink = scale_factor; | 56 page_params.actual_shrink = scale_factor; |
54 | 57 |
55 page_params.page_size = params.params.page_size; | 58 page_params.page_size = params.params.page_size; |
56 page_params.content_area = gfx::Rect(margin_left, margin_top, width, height); | 59 page_params.content_area = gfx::Rect(margin_left, margin_top, width, height); |
57 | 60 |
58 // Ask the browser to create the shared memory for us. | 61 // Ask the browser to create the shared memory for us. |
59 if (!CopyMetafileDataToSharedMem(&metafile, | 62 if (!CopyMetafileDataToSharedMem(metafile, |
60 &(page_params.metafile_data_handle))) { | 63 &(page_params.metafile_data_handle))) { |
61 page_params.data_size = 0; | 64 page_params.data_size = 0; |
62 } | 65 } |
63 | 66 |
64 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); | 67 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); |
65 } | 68 } |
66 | 69 |
67 void PrintWebViewHelper::CreatePreviewDocument( | 70 void PrintWebViewHelper::CreatePreviewDocument( |
68 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { | 71 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { |
69 ViewMsg_Print_Params printParams = params.params; | 72 ViewMsg_Print_Params printParams = params.params; |
70 UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams); | 73 UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams); |
71 | 74 |
72 PrepareFrameAndViewForPrint prep_frame_view(printParams, | 75 PrepareFrameAndViewForPrint prep_frame_view(printParams, |
73 frame, NULL, frame->view()); | 76 frame, NULL, frame->view()); |
74 int page_count = prep_frame_view.GetExpectedPageCount(); | 77 int page_count = prep_frame_view.GetExpectedPageCount(); |
75 | 78 |
76 if (!page_count) | 79 if (!page_count) |
77 return; | 80 return; |
78 | 81 |
79 float scale_factor = frame->getPrintPageShrink(0); | 82 float scale_factor = frame->getPrintPageShrink(0); |
80 double originX = printParams.margin_left; | 83 double originX = printParams.margin_left; |
81 double originY = printParams.margin_top; | 84 double originY = printParams.margin_top; |
82 | 85 |
83 printing::NativeMetafile metafile; | 86 scoped_ptr<printing::NativeMetafile> metafile( |
84 if (!metafile.Init()) | 87 printing::MetafileFactory::GetMetafile()); |
| 88 if (!metafile->Init()) |
85 return; | 89 return; |
86 | 90 |
87 gfx::Point origin(originX, originY); | 91 gfx::Point origin(originX, originY); |
88 | 92 |
89 if (params.pages.empty()) { | 93 if (params.pages.empty()) { |
90 for (int i = 0; i < page_count; ++i) { | 94 for (int i = 0; i < page_count; ++i) { |
91 RenderPage(printParams.page_size, origin, scale_factor, i, frame, | 95 RenderPage(printParams.page_size, origin, scale_factor, i, frame, |
92 &metafile); | 96 metafile); |
93 } | 97 } |
94 } else { | 98 } else { |
95 for (size_t i = 0; i < params.pages.size(); ++i) { | 99 for (size_t i = 0; i < params.pages.size(); ++i) { |
96 if (params.pages[i] >= page_count) | 100 if (params.pages[i] >= page_count) |
97 break; | 101 break; |
98 RenderPage(printParams.page_size, origin, scale_factor, | 102 RenderPage(printParams.page_size, origin, scale_factor, |
99 static_cast<int>(params.pages[i]), frame, &metafile); | 103 static_cast<int>(params.pages[i]), frame, metafile); |
100 } | 104 } |
101 } | 105 } |
102 metafile.Close(); | 106 metafile->Close(); |
103 | 107 |
104 ViewHostMsg_DidPreviewDocument_Params preview_params; | 108 ViewHostMsg_DidPreviewDocument_Params preview_params; |
105 preview_params.data_size = metafile.GetDataSize(); | 109 preview_params.data_size = metafile->GetDataSize(); |
106 preview_params.document_cookie = params.params.document_cookie; | 110 preview_params.document_cookie = params.params.document_cookie; |
107 | 111 |
108 // Ask the browser to create the shared memory for us. | 112 // Ask the browser to create the shared memory for us. |
109 if (!CopyMetafileDataToSharedMem(&metafile, | 113 if (!CopyMetafileDataToSharedMem(metafile, |
110 &(preview_params.metafile_data_handle))) { | 114 &(preview_params.metafile_data_handle))) { |
111 preview_params.data_size = 0; | 115 preview_params.data_size = 0; |
112 } | 116 } |
113 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 117 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
114 } | 118 } |
115 | 119 |
116 void PrintWebViewHelper::RenderPage( | 120 void PrintWebViewHelper::RenderPage( |
117 const gfx::Size& page_size, const gfx::Point& content_origin, | 121 const gfx::Size& page_size, const gfx::Point& content_origin, |
118 const float& scale_factor, int page_number, WebFrame* frame, | 122 const float& scale_factor, int page_number, WebFrame* frame, |
119 printing::NativeMetafile* metafile) { | 123 printing::NativeMetafile* metafile) { |
120 CGContextRef context = metafile->StartPage(page_size, content_origin, | 124 CGContextRef context = metafile->StartPage(page_size, content_origin, |
121 scale_factor); | 125 scale_factor); |
122 DCHECK(context); | 126 DCHECK(context); |
123 | 127 |
124 // printPage can create autoreleased references to |context|. PDF contexts | 128 // printPage can create autoreleased references to |context|. PDF contexts |
125 // don't write all their data until they are destroyed, so we need to make | 129 // don't write all their data until they are destroyed, so we need to make |
126 // certain that there are no lingering references. | 130 // certain that there are no lingering references. |
127 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | 131 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
128 frame->printPage(page_number, context); | 132 frame->printPage(page_number, context); |
129 [pool release]; | 133 [pool release]; |
130 | 134 |
131 // Done printing. Close the device context to retrieve the compiled metafile. | 135 // Done printing. Close the device context to retrieve the compiled metafile. |
132 metafile->FinishPage(); | 136 metafile->FinishPage(); |
133 } | 137 } |
OLD | NEW |