OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/gfx/size.h" | 8 #include "base/gfx/size.h" |
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/renderer/render_view.h" | 11 #include "chrome/renderer/render_view.h" |
12 #include "grit/generated_resources.h" | 12 #include "grit/generated_resources.h" |
13 #include "printing/native_metafile.h" | 13 #include "printing/native_metafile.h" |
14 #include "skia/ext/vector_canvas.h" | |
14 #include "webkit/api/public/WebConsoleMessage.h" | 15 #include "webkit/api/public/WebConsoleMessage.h" |
15 #include "webkit/api/public/WebFrame.h" | 16 #include "webkit/api/public/WebFrame.h" |
16 | 17 |
17 using WebKit::WebConsoleMessage; | 18 using WebKit::WebConsoleMessage; |
18 using WebKit::WebFrame; | 19 using WebKit::WebFrame; |
19 using WebKit::WebString; | 20 using WebKit::WebString; |
20 | 21 |
21 #include "skia/ext/vector_canvas.h" | |
22 | |
23 void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { | 22 void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { |
24 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; | 23 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; |
25 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. | 24 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. |
26 | 25 |
27 // If still not finished with earlier print request simply ignore. | 26 // If still not finished with earlier print request simply ignore. |
28 if (IsPrinting()) | 27 if (IsPrinting()) |
29 return; | 28 return; |
30 | 29 |
31 // TODO(maruel): Move this out of platform specific code. | 30 // TODO(maruel): Move this out of platform specific code. |
32 // Check if there is script repeatedly trying to print and ignore it if too | 31 // Check if there is script repeatedly trying to print and ignore it if too |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 // When |user_cancelled_print| is true, we treat it as success so that | 141 // When |user_cancelled_print| is true, we treat it as success so that |
143 // DidFinishPrinting() won't show any error alert. | 142 // DidFinishPrinting() won't show any error alert. |
144 // If |user_cancelled_print| is false and we reach here, there must be | 143 // If |user_cancelled_print| is false and we reach here, there must be |
145 // something wrong and hence is not success, DidFinishPrinting() should show | 144 // something wrong and hence is not success, DidFinishPrinting() should show |
146 // an error alert. | 145 // an error alert. |
147 // In both cases, we have to call DidFinishPrinting() here to release | 146 // In both cases, we have to call DidFinishPrinting() here to release |
148 // printing resources, since we do need them anymore. | 147 // printing resources, since we do need them anymore. |
149 DidFinishPrinting(user_cancelled_print); | 148 DidFinishPrinting(user_cancelled_print); |
150 } | 149 } |
151 | 150 |
151 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, | |
152 WebFrame* frame) { | |
153 PrepareFrameAndViewForPrint prep_frame_view(params.params, | |
154 frame, | |
155 frame->view()); | |
156 int page_count = prep_frame_view.GetExpectedPageCount(); | |
157 | |
158 Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), | |
159 params.params.document_cookie, | |
160 page_count)); | |
161 if (page_count) { | |
162 ViewMsg_PrintPage_Params page_params; | |
163 page_params.params = params.params; | |
164 if (params.pages.empty()) { | |
165 for (int i = 0; i < page_count; ++i) { | |
166 page_params.page_number = i; | |
167 PrintPage(page_params, prep_frame_view.GetPrintCanvasSize(), frame); | |
168 } | |
169 } else { | |
170 for (size_t i = 0; i < params.pages.size(); ++i) { | |
171 page_params.page_number = params.pages[i]; | |
172 PrintPage(page_params, prep_frame_view.GetPrintCanvasSize(), frame); | |
173 } | |
174 } | |
175 } | |
176 } | |
177 | |
152 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 178 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
153 const gfx::Size& canvas_size, | 179 const gfx::Size& canvas_size, |
154 WebFrame* frame) { | 180 WebFrame* frame) { |
155 // Generate a memory-based metafile. It will use the current screen's DPI. | 181 // Generate a memory-based metafile. It will use the current screen's DPI. |
156 printing::NativeMetafile metafile; | 182 printing::NativeMetafile metafile; |
157 | 183 |
158 metafile.CreateDc(NULL, NULL); | 184 metafile.CreateDc(NULL, NULL); |
159 HDC hdc = metafile.hdc(); | 185 HDC hdc = metafile.hdc(); |
160 DCHECK(hdc); | 186 DCHECK(hdc); |
161 skia::PlatformDevice::InitializeDC(hdc); | 187 skia::PlatformDevice::InitializeDC(hdc); |
162 // Since WebKit extends the page width depending on the magical shrink | 188 // Since WebKit extends the page width depending on the magical shrink |
163 // factor we make sure the canvas covers the worst case scenario | 189 // factor we make sure the canvas covers the worst case scenario |
164 // (x2.0 currently). PrintContext will then set the correct clipping region. | 190 // (x2.0 currently). PrintContext will then set the correct clipping region. |
165 int size_x = static_cast<int>(canvas_size.width() * params.params.max_shrink); | 191 int size_x = static_cast<int>(canvas_size.width() * params.params.max_shrink); |
166 int size_y = static_cast<int>(canvas_size.height() * | 192 int size_y = static_cast<int>(canvas_size.height() * |
167 params.params.max_shrink); | 193 params.params.max_shrink); |
168 // Calculate the dpi adjustment. | 194 // Calculate the dpi adjustment. |
169 float shrink = static_cast<float>(canvas_size.width()) / | 195 float shrink = static_cast<float>(canvas_size.width()) / |
170 params.params.printable_size.width(); | 196 params.params.printable_size.width(); |
171 #if 0 | 197 #if 0 |
172 // TODO(maruel): This code is kept for testing until the 100% GDI drawing | 198 // TODO(maruel): This code is kept for testing until the 100% GDI drawing |
173 // code is stable. maruels use this code's output as a reference when the | 199 // code is stable. maruels use this code's output as a reference when the |
174 // GDI drawing code fails. | 200 // GDI drawing code fails. |
175 | 201 |
176 // Mix of Skia and GDI based. | 202 // Mix of Skia and GDI based. |
177 skia::PlatformCanvas canvas(size_x, size_y, true); | 203 skia::PlatformCanvas canvas(size_x, size_y, true); |
178 canvas.drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode); | 204 canvas.drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode); |
179 float webkit_shrink = frame->PrintPage(params.page_number, &canvas); | 205 float webkit_shrink = frame->PrintPage(params.page_number, &canvas); |
180 if (shrink <= 0) { | 206 if (shrink <= 0) { |
M-A Ruel
2009/09/08 14:38:51
Fix this one too for consistency.
| |
181 NOTREACHED() << "Printing page " << params.page_number << " failed."; | 207 NOTREACHED() << "Printing page " << params.page_number << " failed."; |
182 } else { | 208 } else { |
183 // Update the dpi adjustment with the "page shrink" calculated in webkit. | 209 // Update the dpi adjustment with the "page shrink" calculated in webkit. |
184 shrink /= webkit_shrink; | 210 shrink /= webkit_shrink; |
185 } | 211 } |
186 | 212 |
187 // Create a BMP v4 header that we can serialize. | 213 // Create a BMP v4 header that we can serialize. |
188 BITMAPV4HEADER bitmap_header; | 214 BITMAPV4HEADER bitmap_header; |
189 gfx::CreateBitmapV4Header(size_x, size_y, &bitmap_header); | 215 gfx::CreateBitmapV4Header(size_x, size_y, &bitmap_header); |
190 const SkBitmap& src_bmp = canvas.getDevice()->accessBitmap(true); | 216 const SkBitmap& src_bmp = canvas.getDevice()->accessBitmap(true); |
191 SkAutoLockPixels src_lock(src_bmp); | 217 SkAutoLockPixels src_lock(src_bmp); |
192 int retval = StretchDIBits(hdc, | 218 int retval = StretchDIBits(hdc, |
193 0, | 219 0, |
194 0, | 220 0, |
195 size_x, size_y, | 221 size_x, size_y, |
196 0, 0, | 222 0, 0, |
197 size_x, size_y, | 223 size_x, size_y, |
198 src_bmp.getPixels(), | 224 src_bmp.getPixels(), |
199 reinterpret_cast<BITMAPINFO*>(&bitmap_header), | 225 reinterpret_cast<BITMAPINFO*>(&bitmap_header), |
200 DIB_RGB_COLORS, | 226 DIB_RGB_COLORS, |
201 SRCCOPY); | 227 SRCCOPY); |
202 DCHECK(retval != GDI_ERROR); | 228 DCHECK(retval != GDI_ERROR); |
203 #else | 229 #else |
204 // 100% GDI based. | 230 // 100% GDI based. |
205 skia::VectorCanvas canvas(hdc, size_x, size_y); | 231 skia::VectorCanvas canvas(hdc, size_x, size_y); |
206 float webkit_shrink = frame->printPage(params.page_number, &canvas); | 232 float webkit_shrink = frame->printPage(params.page_number, &canvas); |
207 if (shrink <= 0) { | 233 if (webkit_shrink <= 0) { |
M-A Ruel
2009/09/08 14:38:51
Please test both.
| |
208 NOTREACHED() << "Printing page " << params.page_number << " failed."; | 234 NOTREACHED() << "Printing page " << params.page_number << " failed."; |
209 } else { | 235 } else { |
210 // Update the dpi adjustment with the "page shrink" calculated in webkit. | 236 // Update the dpi adjustment with the "page shrink" calculated in webkit. |
211 shrink /= webkit_shrink; | 237 shrink /= webkit_shrink; |
212 } | 238 } |
213 #endif | 239 #endif |
214 | 240 |
215 // Done printing. Close the device context to retrieve the compiled metafile. | 241 // Done printing. Close the device context to retrieve the compiled metafile. |
216 if (!metafile.CloseDc()) { | 242 if (!metafile.CloseDc()) { |
217 NOTREACHED() << "metafile failed"; | 243 NOTREACHED() << "metafile failed"; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 } | 277 } |
252 metafile.CloseEmf(); | 278 metafile.CloseEmf(); |
253 if (Send(new ViewHostMsg_DuplicateSection( | 279 if (Send(new ViewHostMsg_DuplicateSection( |
254 routing_id(), | 280 routing_id(), |
255 page_params.metafile_data_handle, | 281 page_params.metafile_data_handle, |
256 &page_params.metafile_data_handle))) { | 282 &page_params.metafile_data_handle))) { |
257 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); | 283 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); |
258 } | 284 } |
259 } | 285 } |
260 | 286 |
OLD | NEW |