OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/print_web_view_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "chrome/common/render_messages.h" | 21 #include "chrome/common/render_messages.h" |
22 #include "chrome/grit/browser_resources.h" | 22 #include "chrome/grit/browser_resources.h" |
23 #include "chrome/renderer/prerender/prerender_helper.h" | 23 #include "chrome/renderer/prerender/prerender_helper.h" |
24 #include "content/public/common/web_preferences.h" | 24 #include "content/public/common/web_preferences.h" |
25 #include "content/public/renderer/render_frame.h" | 25 #include "content/public/renderer/render_frame.h" |
26 #include "content/public/renderer/render_thread.h" | 26 #include "content/public/renderer/render_thread.h" |
27 #include "content/public/renderer/render_view.h" | 27 #include "content/public/renderer/render_view.h" |
28 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
29 #include "printing/pdf_metafile_skia.h" | 29 #include "printing/pdf_metafile_skia.h" |
30 #include "printing/units.h" | 30 #include "printing/units.h" |
31 #include "skia/ext/vector_platform_device_skia.h" | |
32 #include "third_party/WebKit/public/platform/WebSize.h" | 31 #include "third_party/WebKit/public/platform/WebSize.h" |
33 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 32 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
34 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 33 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
35 #include "third_party/WebKit/public/web/WebDocument.h" | 34 #include "third_party/WebKit/public/web/WebDocument.h" |
36 #include "third_party/WebKit/public/web/WebElement.h" | 35 #include "third_party/WebKit/public/web/WebElement.h" |
37 #include "third_party/WebKit/public/web/WebFrameClient.h" | 36 #include "third_party/WebKit/public/web/WebFrameClient.h" |
38 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 37 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
39 #include "third_party/WebKit/public/web/WebPlugin.h" | 38 #include "third_party/WebKit/public/web/WebPlugin.h" |
40 #include "third_party/WebKit/public/web/WebPluginDocument.h" | 39 #include "third_party/WebKit/public/web/WebPluginDocument.h" |
41 #include "third_party/WebKit/public/web/WebPrintParams.h" | 40 #include "third_party/WebKit/public/web/WebPrintParams.h" |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 | 444 |
446 // static - Not anonymous so that platform implementations can use it. | 445 // static - Not anonymous so that platform implementations can use it. |
447 void PrintWebViewHelper::PrintHeaderAndFooter( | 446 void PrintWebViewHelper::PrintHeaderAndFooter( |
448 blink::WebCanvas* canvas, | 447 blink::WebCanvas* canvas, |
449 int page_number, | 448 int page_number, |
450 int total_pages, | 449 int total_pages, |
451 const blink::WebFrame& source_frame, | 450 const blink::WebFrame& source_frame, |
452 float webkit_scale_factor, | 451 float webkit_scale_factor, |
453 const PageSizeMargins& page_layout, | 452 const PageSizeMargins& page_layout, |
454 const PrintMsg_Print_Params& params) { | 453 const PrintMsg_Print_Params& params) { |
455 skia::VectorPlatformDeviceSkia* device = | |
456 static_cast<skia::VectorPlatformDeviceSkia*>(canvas->getTopDevice()); | |
457 device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea); | |
458 | |
459 SkAutoCanvasRestore auto_restore(canvas, true); | 454 SkAutoCanvasRestore auto_restore(canvas, true); |
460 canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor); | 455 canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor); |
461 | 456 |
462 blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right + | 457 blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right + |
463 page_layout.content_width, | 458 page_layout.content_width, |
464 page_layout.margin_top + page_layout.margin_bottom + | 459 page_layout.margin_top + page_layout.margin_bottom + |
465 page_layout.content_height); | 460 page_layout.content_height); |
466 | 461 |
467 blink::WebView* web_view = blink::WebView::create(NULL); | 462 blink::WebView* web_view = blink::WebView::create(NULL); |
468 web_view->settings()->setJavaScriptEnabled(true); | 463 web_view->settings()->setJavaScriptEnabled(true); |
469 | 464 |
470 blink::WebLocalFrame* frame = blink::WebLocalFrame::create(NULL); | 465 blink::WebLocalFrame* frame = blink::WebLocalFrame::create(NULL); |
471 web_view->setMainFrame(frame); | 466 web_view->setMainFrame(frame); |
472 | 467 |
473 base::StringValue html(ResourceBundle::GetSharedInstance().GetLocalizedString( | 468 base::StringValue html(ResourceBundle::GetSharedInstance().GetLocalizedString( |
474 IDR_PRINT_PREVIEW_PAGE)); | 469 IDR_PRINT_PREVIEW_PAGE)); |
475 // Load page with script to avoid async operations. | 470 // Load page with script to avoid async operations. |
476 ExecuteScript(frame, kPageLoadScriptFormat, html); | 471 ExecuteScript(frame, kPageLoadScriptFormat, html); |
477 | 472 |
478 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); | 473 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); |
479 options.reset(new base::DictionaryValue()); | 474 options.reset(new base::DictionaryValue()); |
480 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); | 475 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); |
481 options->SetDouble("width", page_size.width); | 476 options->SetDouble("width", page_size.width); |
482 options->SetDouble("height", page_size.height); | 477 options->SetDouble("height", page_size.height); |
483 options->SetDouble("topMargin", page_layout.margin_top); | 478 options->SetDouble("topMargin", page_layout.margin_top); |
484 options->SetDouble("bottomMargin", page_layout.margin_bottom); | 479 options->SetDouble("bottomMargin", page_layout.margin_bottom); |
485 options->SetString("pageNumber", | 480 options->SetString("pageNumber", |
486 base::StringPrintf("%d/%d", page_number, total_pages)); | 481 base::StringPrintf("%d/%d", page_number, total_pages)); |
487 | |
488 // Fallback to initiator URL and title if it's empty for printed frame. | 482 // Fallback to initiator URL and title if it's empty for printed frame. |
489 base::string16 url = source_frame.document().url().string(); | 483 base::string16 url = source_frame.document().url().string(); |
490 options->SetString("url", url.empty() ? params.url : url); | 484 options->SetString("url", url.empty() ? params.url : url); |
491 base::string16 title = source_frame.document().title(); | 485 base::string16 title = source_frame.document().title(); |
492 options->SetString("title", title.empty() ? params.title : title); | 486 options->SetString("title", title.empty() ? params.title : title); |
493 | 487 |
494 ExecuteScript(frame, kPageSetupScriptFormat, *options); | 488 ExecuteScript(frame, kPageSetupScriptFormat, *options); |
495 | 489 |
496 blink::WebPrintParams webkit_params(page_size); | 490 blink::WebPrintParams webkit_params(page_size); |
497 webkit_params.printerDPI = GetDPI(¶ms); | 491 webkit_params.printerDPI = GetDPI(¶ms); |
498 | 492 |
499 frame->printBegin(webkit_params); | 493 frame->printBegin(webkit_params); |
500 frame->printPage(0, canvas); | 494 frame->printPage(0, canvas); |
501 frame->printEnd(); | 495 frame->printEnd(); |
502 | 496 |
503 web_view->close(); | 497 web_view->close(); |
504 frame->close(); | 498 frame->close(); |
505 | |
506 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); | |
507 } | 499 } |
508 | 500 |
509 // static - Not anonymous so that platform implementations can use it. | 501 // static - Not anonymous so that platform implementations can use it. |
510 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame, | 502 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame, |
511 int page_number, | 503 int page_number, |
512 const gfx::Rect& canvas_area, | 504 const gfx::Rect& canvas_area, |
513 const gfx::Rect& content_area, | 505 const gfx::Rect& content_area, |
514 double scale_factor, | 506 double scale_factor, |
515 blink::WebCanvas* canvas) { | 507 blink::WebCanvas* canvas) { |
516 SkAutoCanvasRestore auto_restore(canvas, true); | 508 SkAutoCanvasRestore auto_restore(canvas, true); |
(...skipping 1504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2021 blink::WebConsoleMessage::LevelWarning, message)); | 2013 blink::WebConsoleMessage::LevelWarning, message)); |
2022 return false; | 2014 return false; |
2023 } | 2015 } |
2024 | 2016 |
2025 void PrintWebViewHelper::ScriptingThrottler::Reset() { | 2017 void PrintWebViewHelper::ScriptingThrottler::Reset() { |
2026 // Reset counter on successful print. | 2018 // Reset counter on successful print. |
2027 count_ = 0; | 2019 count_ = 0; |
2028 } | 2020 } |
2029 | 2021 |
2030 } // namespace printing | 2022 } // namespace printing |
OLD | NEW |