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" |
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_ptr = printing::MetafileFactory::GetMetafil
e(); |
30 if (!metafile.Init()) | 30 if (!metafile_ptr->Init()) |
31 return; | 31 return; |
32 | 32 |
33 float scale_factor = frame->getPrintPageShrink(params.page_number); | 33 float scale_factor = frame->getPrintPageShrink(params.page_number); |
34 double width = params.params.printable_size.width(); | 34 double width = params.params.printable_size.width(); |
35 double height = params.params.printable_size.height(); | 35 double height = params.params.printable_size.height(); |
36 int page_number = params.page_number; | 36 int page_number = params.page_number; |
37 | 37 |
38 // Render page for printing. | 38 // Render page for printing. |
39 gfx::Point origin(0.0f, 0.0f); | 39 gfx::Point origin(0.0f, 0.0f); |
40 RenderPage(params.params.printable_size, origin, scale_factor, page_number, | 40 RenderPage(params.params.printable_size, origin, scale_factor, page_number, |
41 frame, &metafile); | 41 frame, metafile_ptr); |
42 | 42 |
43 metafile.Close(); | 43 metafile_ptr->Close(); |
44 | 44 |
45 double margin_left = params.params.margin_left; | 45 double margin_left = params.params.margin_left; |
46 double margin_top = params.params.margin_top; | 46 double margin_top = params.params.margin_top; |
47 | 47 |
48 // Get the size of the compiled metafile. | 48 // Get the size of the compiled metafile. |
49 ViewHostMsg_DidPrintPage_Params page_params; | 49 ViewHostMsg_DidPrintPage_Params page_params; |
50 page_params.data_size = metafile.GetDataSize(); | 50 page_params.data_size = metafile_ptr->GetDataSize(); |
51 page_params.page_number = page_number; | 51 page_params.page_number = page_number; |
52 page_params.document_cookie = params.params.document_cookie; | 52 page_params.document_cookie = params.params.document_cookie; |
53 page_params.actual_shrink = scale_factor; | 53 page_params.actual_shrink = scale_factor; |
54 | 54 |
55 page_params.page_size = params.params.page_size; | 55 page_params.page_size = params.params.page_size; |
56 page_params.content_area = gfx::Rect(margin_left, margin_top, width, height); | 56 page_params.content_area = gfx::Rect(margin_left, margin_top, width, height); |
57 | 57 |
58 // Ask the browser to create the shared memory for us. | 58 // Ask the browser to create the shared memory for us. |
59 if (!CopyMetafileDataToSharedMem(&metafile, | 59 if (!CopyMetafileDataToSharedMem(metafile_ptr, |
60 &(page_params.metafile_data_handle))) { | 60 &(page_params.metafile_data_handle))) { |
61 page_params.data_size = 0; | 61 page_params.data_size = 0; |
62 } | 62 } |
63 | 63 |
64 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); | 64 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); |
65 } | 65 } |
66 | 66 |
67 void PrintWebViewHelper::CreatePreviewDocument( | 67 void PrintWebViewHelper::CreatePreviewDocument( |
68 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { | 68 const ViewMsg_PrintPages_Params& params, WebFrame* frame) { |
69 ViewMsg_Print_Params printParams = params.params; | 69 ViewMsg_Print_Params printParams = params.params; |
70 UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams); | 70 UpdatePrintableSizeInPrintParameters(frame, NULL, &printParams); |
71 | 71 |
72 PrepareFrameAndViewForPrint prep_frame_view(printParams, | 72 PrepareFrameAndViewForPrint prep_frame_view(printParams, |
73 frame, NULL, frame->view()); | 73 frame, NULL, frame->view()); |
74 int page_count = prep_frame_view.GetExpectedPageCount(); | 74 int page_count = prep_frame_view.GetExpectedPageCount(); |
75 | 75 |
76 if (!page_count) | 76 if (!page_count) |
77 return; | 77 return; |
78 | 78 |
79 float scale_factor = frame->getPrintPageShrink(0); | 79 float scale_factor = frame->getPrintPageShrink(0); |
80 double originX = printParams.margin_left; | 80 double originX = printParams.margin_left; |
81 double originY = printParams.margin_top; | 81 double originY = printParams.margin_top; |
82 | 82 |
83 printing::NativeMetafile metafile; | 83 printing::NativeMetafile* metafile_ptr = printing::MetafileFactory::GetMetafil
e(); |
84 if (!metafile.Init()) | 84 if (!metafile_ptr->Init()) |
85 return; | 85 return; |
86 | 86 |
87 gfx::Point origin(originX, originY); | 87 gfx::Point origin(originX, originY); |
88 | 88 |
89 if (params.pages.empty()) { | 89 if (params.pages.empty()) { |
90 for (int i = 0; i < page_count; ++i) { | 90 for (int i = 0; i < page_count; ++i) { |
91 RenderPage(printParams.page_size, origin, scale_factor, i, frame, | 91 RenderPage(printParams.page_size, origin, scale_factor, i, frame, |
92 &metafile); | 92 metafile_ptr); |
93 } | 93 } |
94 } else { | 94 } else { |
95 for (size_t i = 0; i < params.pages.size(); ++i) { | 95 for (size_t i = 0; i < params.pages.size(); ++i) { |
96 if (params.pages[i] >= page_count) | 96 if (params.pages[i] >= page_count) |
97 break; | 97 break; |
98 RenderPage(printParams.page_size, origin, scale_factor, | 98 RenderPage(printParams.page_size, origin, scale_factor, |
99 static_cast<int>(params.pages[i]), frame, &metafile); | 99 static_cast<int>(params.pages[i]), frame, metafile_ptr); |
100 } | 100 } |
101 } | 101 } |
102 metafile.Close(); | 102 metafile_ptr->Close(); |
103 | 103 |
104 ViewHostMsg_DidPreviewDocument_Params preview_params; | 104 ViewHostMsg_DidPreviewDocument_Params preview_params; |
105 preview_params.data_size = metafile.GetDataSize(); | 105 preview_params.data_size = metafile_ptr->GetDataSize(); |
106 preview_params.document_cookie = params.params.document_cookie; | 106 preview_params.document_cookie = params.params.document_cookie; |
107 | 107 |
108 // Ask the browser to create the shared memory for us. | 108 // Ask the browser to create the shared memory for us. |
109 if (!CopyMetafileDataToSharedMem(&metafile, | 109 if (!CopyMetafileDataToSharedMem(metafile_ptr, |
110 &(preview_params.metafile_data_handle))) { | 110 &(preview_params.metafile_data_handle))) { |
111 preview_params.data_size = 0; | 111 preview_params.data_size = 0; |
112 } | 112 } |
113 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 113 Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
114 } | 114 } |
115 | 115 |
116 void PrintWebViewHelper::RenderPage( | 116 void PrintWebViewHelper::RenderPage( |
117 const gfx::Size& page_size, const gfx::Point& content_origin, | 117 const gfx::Size& page_size, const gfx::Point& content_origin, |
118 const float& scale_factor, int page_number, WebFrame* frame, | 118 const float& scale_factor, int page_number, WebFrame* frame, |
119 printing::NativeMetafile* metafile) { | 119 printing::NativeMetafile* metafile) { |
120 CGContextRef context = metafile->StartPage(page_size, content_origin, | 120 CGContextRef context = metafile->StartPage(page_size, content_origin, |
121 scale_factor); | 121 scale_factor); |
122 DCHECK(context); | 122 DCHECK(context); |
123 | 123 |
124 // printPage can create autoreleased references to |context|. PDF contexts | 124 // 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 | 125 // don't write all their data until they are destroyed, so we need to make |
126 // certain that there are no lingering references. | 126 // certain that there are no lingering references. |
127 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | 127 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
128 frame->printPage(page_number, context); | 128 frame->printPage(page_number, context); |
129 [pool release]; | 129 [pool release]; |
130 | 130 |
131 // Done printing. Close the device context to retrieve the compiled metafile. | 131 // Done printing. Close the device context to retrieve the compiled metafile. |
132 metafile->FinishPage(); | 132 metafile->FinishPage(); |
133 } | 133 } |
OLD | NEW |