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