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/logging.h" | 8 #include "base/logging.h" |
9 #include "base/gfx/size.h" | 9 #include "base/gfx/size.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/units.h" | 14 #include "printing/units.h" |
14 #include "webkit/api/public/WebConsoleMessage.h" | 15 #include "webkit/api/public/WebConsoleMessage.h" |
15 #include "webkit/api/public/WebScreenInfo.h" | 16 #include "webkit/api/public/WebScreenInfo.h" |
16 #include "webkit/api/public/WebSize.h" | 17 #include "webkit/api/public/WebSize.h" |
17 #include "webkit/api/public/WebURL.h" | 18 #include "webkit/api/public/WebURL.h" |
18 #include "webkit/api/public/WebURLRequest.h" | 19 #include "webkit/api/public/WebURLRequest.h" |
19 #include "webkit/glue/webframe.h" | 20 #include "webkit/glue/webframe.h" |
20 | 21 |
21 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
22 #include "chrome/common/gfx/emf.h" | |
23 #include "skia/ext/vector_canvas.h" | 23 #include "skia/ext/vector_canvas.h" |
24 #endif | 24 #endif |
25 | 25 |
26 using WebKit::WebConsoleMessage; | 26 using WebKit::WebConsoleMessage; |
27 using WebKit::WebString; | 27 using WebKit::WebString; |
28 using WebKit::WebURLRequest; | 28 using WebKit::WebURLRequest; |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; | 32 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 PrintPage(page_params, prep_frame_view.GetPrintCanvasSize(), frame); | 276 PrintPage(page_params, prep_frame_view.GetPrintCanvasSize(), frame); |
277 } | 277 } |
278 } | 278 } |
279 } | 279 } |
280 } | 280 } |
281 | 281 |
282 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, | 282 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, |
283 const gfx::Size& canvas_size, | 283 const gfx::Size& canvas_size, |
284 WebFrame* frame) { | 284 WebFrame* frame) { |
285 #if defined(OS_WIN) | 285 #if defined(OS_WIN) |
286 // Generate a memory-based EMF file. The EMF will use the current screen's | 286 // Generate a memory-based metafile. It will use the current screen's DPI. |
287 // DPI. | 287 printing::NativeMetafile metafile; |
288 gfx::Emf emf; | |
289 | 288 |
290 emf.CreateDc(NULL, NULL); | 289 metafile.CreateDc(NULL, NULL); |
291 HDC hdc = emf.hdc(); | 290 HDC hdc = metafile.hdc(); |
292 DCHECK(hdc); | 291 DCHECK(hdc); |
293 skia::PlatformDevice::InitializeDC(hdc); | 292 skia::PlatformDevice::InitializeDC(hdc); |
294 // Since WebKit extends the page width depending on the magical shrink | 293 // Since WebKit extends the page width depending on the magical shrink |
295 // factor we make sure the canvas covers the worst case scenario | 294 // factor we make sure the canvas covers the worst case scenario |
296 // (x2.0 currently). PrintContext will then set the correct clipping region. | 295 // (x2.0 currently). PrintContext will then set the correct clipping region. |
297 int size_x = static_cast<int>(canvas_size.width() * params.params.max_shrink); | 296 int size_x = static_cast<int>(canvas_size.width() * params.params.max_shrink); |
298 int size_y = static_cast<int>(canvas_size.height() * | 297 int size_y = static_cast<int>(canvas_size.height() * |
299 params.params.max_shrink); | 298 params.params.max_shrink); |
300 // Calculate the dpi adjustment. | 299 // Calculate the dpi adjustment. |
301 float shrink = static_cast<float>(canvas_size.width()) / | 300 float shrink = static_cast<float>(canvas_size.width()) / |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 skia::VectorCanvas canvas(hdc, size_x, size_y); | 336 skia::VectorCanvas canvas(hdc, size_x, size_y); |
338 float webkit_shrink = frame->PrintPage(params.page_number, &canvas); | 337 float webkit_shrink = frame->PrintPage(params.page_number, &canvas); |
339 if (shrink <= 0) { | 338 if (shrink <= 0) { |
340 NOTREACHED() << "Printing page " << params.page_number << " failed."; | 339 NOTREACHED() << "Printing page " << params.page_number << " failed."; |
341 } else { | 340 } else { |
342 // Update the dpi adjustment with the "page shrink" calculated in webkit. | 341 // Update the dpi adjustment with the "page shrink" calculated in webkit. |
343 shrink /= webkit_shrink; | 342 shrink /= webkit_shrink; |
344 } | 343 } |
345 #endif | 344 #endif |
346 | 345 |
347 // Done printing. Close the device context to retrieve the compiled EMF. | 346 // Done printing. Close the device context to retrieve the compiled metafile. |
348 if (!emf.CloseDc()) { | 347 if (!metafile.CloseDc()) { |
349 NOTREACHED() << "EMF failed"; | 348 NOTREACHED() << "metafile failed"; |
350 } | 349 } |
351 | 350 |
352 // Get the size of the compiled EMF. | 351 // Get the size of the compiled metafile. |
353 unsigned buf_size = emf.GetDataSize(); | 352 unsigned buf_size = metafile.GetDataSize(); |
354 DCHECK_GT(buf_size, 128u); | 353 DCHECK_GT(buf_size, 128u); |
355 ViewHostMsg_DidPrintPage_Params page_params; | 354 ViewHostMsg_DidPrintPage_Params page_params; |
356 page_params.data_size = 0; | 355 page_params.data_size = 0; |
357 page_params.emf_data_handle = NULL; | 356 page_params.metafile_data_handle = NULL; |
358 page_params.page_number = params.page_number; | 357 page_params.page_number = params.page_number; |
359 page_params.document_cookie = params.params.document_cookie; | 358 page_params.document_cookie = params.params.document_cookie; |
360 page_params.actual_shrink = shrink; | 359 page_params.actual_shrink = shrink; |
361 base::SharedMemory shared_buf; | 360 base::SharedMemory shared_buf; |
362 | 361 |
363 // http://msdn2.microsoft.com/en-us/library/ms535522.aspx | 362 // http://msdn2.microsoft.com/en-us/library/ms535522.aspx |
364 // Windows 2000/XP: When a page in a spooled file exceeds approximately 350 | 363 // Windows 2000/XP: When a page in a spooled file exceeds approximately 350 |
365 // MB, it can fail to print and not send an error message. | 364 // MB, it can fail to print and not send an error message. |
366 if (buf_size < 350*1024*1024) { | 365 if (buf_size < 350*1024*1024) { |
367 // Allocate a shared memory buffer to hold the generated EMF data. | 366 // Allocate a shared memory buffer to hold the generated metafile data. |
368 if (shared_buf.Create(L"", false, false, buf_size) && | 367 if (shared_buf.Create(L"", false, false, buf_size) && |
369 shared_buf.Map(buf_size)) { | 368 shared_buf.Map(buf_size)) { |
370 // Copy the bits into shared memory. | 369 // Copy the bits into shared memory. |
371 if (emf.GetData(shared_buf.memory(), buf_size)) { | 370 if (metafile.GetData(shared_buf.memory(), buf_size)) { |
372 page_params.emf_data_handle = shared_buf.handle(); | 371 page_params.metafile_data_handle = shared_buf.handle(); |
373 page_params.data_size = buf_size; | 372 page_params.data_size = buf_size; |
374 } else { | 373 } else { |
375 NOTREACHED() << "GetData() failed"; | 374 NOTREACHED() << "GetData() failed"; |
376 } | 375 } |
377 shared_buf.Unmap(); | 376 shared_buf.Unmap(); |
378 } else { | 377 } else { |
379 NOTREACHED() << "Buffer allocation failed"; | 378 NOTREACHED() << "Buffer allocation failed"; |
380 } | 379 } |
381 } else { | 380 } else { |
382 NOTREACHED() << "Buffer too large: " << buf_size; | 381 NOTREACHED() << "Buffer too large: " << buf_size; |
383 } | 382 } |
384 emf.CloseEmf(); | 383 metafile.CloseEmf(); |
385 if (Send(new ViewHostMsg_DuplicateSection(routing_id(), | 384 if (Send(new ViewHostMsg_DuplicateSection( |
386 page_params.emf_data_handle, | 385 routing_id(), |
387 &page_params.emf_data_handle))) { | 386 page_params.metafile_data_handle, |
| 387 &page_params.metafile_data_handle))) { |
388 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); | 388 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); |
389 } | 389 } |
390 #else // defined(OS_WIN) | 390 #else // defined(OS_WIN) |
391 // TODO(port) implement printing | 391 // TODO(port) implement printing |
392 NOTIMPLEMENTED(); | 392 NOTIMPLEMENTED(); |
393 #endif | 393 #endif |
394 } | 394 } |
395 | 395 |
396 bool PrintWebViewHelper::Send(IPC::Message* msg) { | 396 bool PrintWebViewHelper::Send(IPC::Message* msg) { |
397 return render_view_->Send(msg); | 397 return render_view_->Send(msg); |
(...skipping 22 matching lines...) Expand all Loading... |
420 | 420 |
421 gfx::NativeViewId PrintWebViewHelper::GetContainingView(WebWidget* webwidget) { | 421 gfx::NativeViewId PrintWebViewHelper::GetContainingView(WebWidget* webwidget) { |
422 NOTREACHED(); | 422 NOTREACHED(); |
423 return gfx::NativeViewId(); | 423 return gfx::NativeViewId(); |
424 } | 424 } |
425 | 425 |
426 bool PrintWebViewHelper::IsHidden(WebWidget* webwidget) { | 426 bool PrintWebViewHelper::IsHidden(WebWidget* webwidget) { |
427 NOTREACHED(); | 427 NOTREACHED(); |
428 return true; | 428 return true; |
429 } | 429 } |
OLD | NEW |