| 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 "components/printing/renderer/print_web_view_helper.h" | 5 #include "components/printing/renderer/print_web_view_helper.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #include "printing/metafile_skia_wrapper.h" | 32 #include "printing/metafile_skia_wrapper.h" |
| 33 #include "printing/pdf_metafile_skia.h" | 33 #include "printing/pdf_metafile_skia.h" |
| 34 #include "printing/units.h" | 34 #include "printing/units.h" |
| 35 #include "third_party/WebKit/public/platform/WebSize.h" | 35 #include "third_party/WebKit/public/platform/WebSize.h" |
| 36 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 36 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 37 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 37 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
| 38 #include "third_party/WebKit/public/web/WebDocument.h" | 38 #include "third_party/WebKit/public/web/WebDocument.h" |
| 39 #include "third_party/WebKit/public/web/WebElement.h" | 39 #include "third_party/WebKit/public/web/WebElement.h" |
| 40 #include "third_party/WebKit/public/web/WebFrameClient.h" | 40 #include "third_party/WebKit/public/web/WebFrameClient.h" |
| 41 #include "third_party/WebKit/public/web/WebFrameOwnerProperties.h" | 41 #include "third_party/WebKit/public/web/WebFrameOwnerProperties.h" |
| 42 #include "third_party/WebKit/public/web/WebFrameWidget.h" |
| 42 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 43 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 43 #include "third_party/WebKit/public/web/WebPlugin.h" | 44 #include "third_party/WebKit/public/web/WebPlugin.h" |
| 44 #include "third_party/WebKit/public/web/WebPluginDocument.h" | 45 #include "third_party/WebKit/public/web/WebPluginDocument.h" |
| 45 #include "third_party/WebKit/public/web/WebPrintParams.h" | 46 #include "third_party/WebKit/public/web/WebPrintParams.h" |
| 46 #include "third_party/WebKit/public/web/WebPrintPresetOptions.h" | 47 #include "third_party/WebKit/public/web/WebPrintPresetOptions.h" |
| 47 #include "third_party/WebKit/public/web/WebSandboxFlags.h" | 48 #include "third_party/WebKit/public/web/WebSandboxFlags.h" |
| 48 #include "third_party/WebKit/public/web/WebScriptSource.h" | 49 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 49 #include "third_party/WebKit/public/web/WebSettings.h" | 50 #include "third_party/WebKit/public/web/WebSettings.h" |
| 50 #include "third_party/WebKit/public/web/WebView.h" | 51 #include "third_party/WebKit/public/web/WebView.h" |
| 51 #include "third_party/WebKit/public/web/WebViewClient.h" | 52 #include "third_party/WebKit/public/web/WebViewClient.h" |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 page_layout.margin_top + page_layout.margin_bottom + | 523 page_layout.margin_top + page_layout.margin_bottom + |
| 523 page_layout.content_height); | 524 page_layout.content_height); |
| 524 | 525 |
| 525 blink::WebView* web_view = | 526 blink::WebView* web_view = |
| 526 blink::WebView::create(nullptr, blink::WebPageVisibilityStateVisible); | 527 blink::WebView::create(nullptr, blink::WebPageVisibilityStateVisible); |
| 527 web_view->settings()->setJavaScriptEnabled(true); | 528 web_view->settings()->setJavaScriptEnabled(true); |
| 528 | 529 |
| 529 blink::WebLocalFrame* frame = | 530 blink::WebLocalFrame* frame = |
| 530 blink::WebLocalFrame::create(blink::WebTreeScopeType::Document, NULL); | 531 blink::WebLocalFrame::create(blink::WebTreeScopeType::Document, NULL); |
| 531 web_view->setMainFrame(frame); | 532 web_view->setMainFrame(frame); |
| 533 blink::WebFrameWidget* widget = |
| 534 blink::WebFrameWidget::create(nullptr, web_view, frame); |
| 532 | 535 |
| 533 base::StringValue html(ResourceBundle::GetSharedInstance().GetLocalizedString( | 536 base::StringValue html(ResourceBundle::GetSharedInstance().GetLocalizedString( |
| 534 IDR_PRINT_PREVIEW_PAGE)); | 537 IDR_PRINT_PREVIEW_PAGE)); |
| 535 // Load page with script to avoid async operations. | 538 // Load page with script to avoid async operations. |
| 536 ExecuteScript(frame, kPageLoadScriptFormat, html); | 539 ExecuteScript(frame, kPageLoadScriptFormat, html); |
| 537 | 540 |
| 538 std::unique_ptr<base::DictionaryValue> options(new base::DictionaryValue()); | 541 std::unique_ptr<base::DictionaryValue> options(new base::DictionaryValue()); |
| 539 options.reset(new base::DictionaryValue()); | 542 options.reset(new base::DictionaryValue()); |
| 540 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); | 543 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); |
| 541 options->SetDouble("width", page_size.width); | 544 options->SetDouble("width", page_size.width); |
| 542 options->SetDouble("height", page_size.height); | 545 options->SetDouble("height", page_size.height); |
| 543 options->SetDouble("topMargin", page_layout.margin_top); | 546 options->SetDouble("topMargin", page_layout.margin_top); |
| 544 options->SetDouble("bottomMargin", page_layout.margin_bottom); | 547 options->SetDouble("bottomMargin", page_layout.margin_bottom); |
| 545 options->SetString("pageNumber", | 548 options->SetString("pageNumber", |
| 546 base::StringPrintf("%d/%d", page_number, total_pages)); | 549 base::StringPrintf("%d/%d", page_number, total_pages)); |
| 547 | 550 |
| 548 options->SetString("url", params.url); | 551 options->SetString("url", params.url); |
| 549 base::string16 title = source_frame.document().title(); | 552 base::string16 title = source_frame.document().title(); |
| 550 options->SetString("title", title.empty() ? params.title : title); | 553 options->SetString("title", title.empty() ? params.title : title); |
| 551 | 554 |
| 552 ExecuteScript(frame, kPageSetupScriptFormat, *options); | 555 ExecuteScript(frame, kPageSetupScriptFormat, *options); |
| 553 | 556 |
| 554 blink::WebPrintParams webkit_params(page_size); | 557 blink::WebPrintParams webkit_params(page_size); |
| 555 webkit_params.printerDPI = GetDPI(¶ms); | 558 webkit_params.printerDPI = GetDPI(¶ms); |
| 556 | 559 |
| 557 frame->printBegin(webkit_params); | 560 frame->printBegin(webkit_params); |
| 558 frame->printPage(0, canvas); | 561 frame->printPage(0, canvas); |
| 559 frame->printEnd(); | 562 frame->printEnd(); |
| 560 | 563 |
| 564 widget->close(); |
| 561 web_view->close(); | 565 web_view->close(); |
| 562 frame->close(); | 566 frame->close(); |
| 563 } | 567 } |
| 564 #endif // defined(ENABLE_PRINT_PREVIEW) | 568 #endif // defined(ENABLE_PRINT_PREVIEW) |
| 565 | 569 |
| 566 // static - Not anonymous so that platform implementations can use it. | 570 // static - Not anonymous so that platform implementations can use it. |
| 567 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame, | 571 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame, |
| 568 int page_number, | 572 int page_number, |
| 569 const gfx::Rect& canvas_area, | 573 const gfx::Rect& canvas_area, |
| 570 const gfx::Rect& content_area, | 574 const gfx::Rect& content_area, |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 // Create a new WebView with the same settings as the current display one. | 741 // Create a new WebView with the same settings as the current display one. |
| 738 // Except that we disable javascript (don't want any active content running | 742 // Except that we disable javascript (don't want any active content running |
| 739 // on the page). | 743 // on the page). |
| 740 WebPreferences prefs = preferences; | 744 WebPreferences prefs = preferences; |
| 741 prefs.javascript_enabled = false; | 745 prefs.javascript_enabled = false; |
| 742 | 746 |
| 743 blink::WebView* web_view = | 747 blink::WebView* web_view = |
| 744 blink::WebView::create(this, blink::WebPageVisibilityStateVisible); | 748 blink::WebView::create(this, blink::WebPageVisibilityStateVisible); |
| 745 owns_web_view_ = true; | 749 owns_web_view_ = true; |
| 746 content::RenderView::ApplyWebPreferences(prefs, web_view); | 750 content::RenderView::ApplyWebPreferences(prefs, web_view); |
| 747 web_view->setMainFrame( | 751 blink::WebLocalFrame* main_frame = |
| 748 blink::WebLocalFrame::create(blink::WebTreeScopeType::Document, this)); | 752 blink::WebLocalFrame::create(blink::WebTreeScopeType::Document, this); |
| 753 web_view->setMainFrame(main_frame); |
| 754 blink::WebFrameWidget::create(this, web_view, main_frame); |
| 749 frame_.Reset(web_view->mainFrame()->toWebLocalFrame()); | 755 frame_.Reset(web_view->mainFrame()->toWebLocalFrame()); |
| 750 node_to_print_.reset(); | 756 node_to_print_.reset(); |
| 751 | 757 |
| 752 // When loading is done this will call didStopLoading() and that will do the | 758 // When loading is done this will call didStopLoading() and that will do the |
| 753 // actual printing. | 759 // actual printing. |
| 754 frame()->loadRequest(blink::WebURLRequest(GURL(url_str))); | 760 frame()->loadRequest(blink::WebURLRequest(GURL(url_str))); |
| 755 } | 761 } |
| 756 | 762 |
| 757 bool PrepareFrameAndViewForPrint::allowsBrokenNullLayerTreeView() const { | 763 bool PrepareFrameAndViewForPrint::allowsBrokenNullLayerTreeView() const { |
| 758 return true; | 764 return true; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 808 is_printing_started_ = false; | 814 is_printing_started_ = false; |
| 809 frame->printEnd(); | 815 frame->printEnd(); |
| 810 if (!owns_web_view_) { | 816 if (!owns_web_view_) { |
| 811 web_view->settings()->setShouldPrintBackgrounds(false); | 817 web_view->settings()->setShouldPrintBackgrounds(false); |
| 812 RestoreSize(); | 818 RestoreSize(); |
| 813 } | 819 } |
| 814 } | 820 } |
| 815 if (owns_web_view_) { | 821 if (owns_web_view_) { |
| 816 DCHECK(!frame->isLoading()); | 822 DCHECK(!frame->isLoading()); |
| 817 owns_web_view_ = false; | 823 owns_web_view_ = false; |
| 824 frame->frameWidget()->close(); |
| 818 web_view->close(); | 825 web_view->close(); |
| 819 } | 826 } |
| 820 } | 827 } |
| 821 frame_.Reset(NULL); | 828 frame_.Reset(NULL); |
| 822 on_ready_.Reset(); | 829 on_ready_.Reset(); |
| 823 } | 830 } |
| 824 | 831 |
| 825 bool PrintWebViewHelper::Delegate::IsAskPrintSettingsEnabled() { | 832 bool PrintWebViewHelper::Delegate::IsAskPrintSettingsEnabled() { |
| 826 return true; | 833 return true; |
| 827 } | 834 } |
| (...skipping 1424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2252 blink::WebConsoleMessage::LevelWarning, message)); | 2259 blink::WebConsoleMessage::LevelWarning, message)); |
| 2253 return false; | 2260 return false; |
| 2254 } | 2261 } |
| 2255 | 2262 |
| 2256 void PrintWebViewHelper::ScriptingThrottler::Reset() { | 2263 void PrintWebViewHelper::ScriptingThrottler::Reset() { |
| 2257 // Reset counter on successful print. | 2264 // Reset counter on successful print. |
| 2258 count_ = 0; | 2265 count_ = 0; |
| 2259 } | 2266 } |
| 2260 | 2267 |
| 2261 } // namespace printing | 2268 } // namespace printing |
| OLD | NEW |