Chromium Code Reviews| 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 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 520 | 520 |
| 521 blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right + | 521 blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right + |
| 522 page_layout.content_width, | 522 page_layout.content_width, |
| 523 page_layout.margin_top + page_layout.margin_bottom + | 523 page_layout.margin_top + page_layout.margin_bottom + |
| 524 page_layout.content_height); | 524 page_layout.content_height); |
| 525 | 525 |
| 526 blink::WebView* web_view = | 526 blink::WebView* web_view = |
| 527 blink::WebView::create(nullptr, blink::WebPageVisibilityStateVisible); | 527 blink::WebView::create(nullptr, blink::WebPageVisibilityStateVisible); |
| 528 web_view->settings()->setJavaScriptEnabled(true); | 528 web_view->settings()->setJavaScriptEnabled(true); |
| 529 | 529 |
| 530 blink::WebLocalFrame* frame = | 530 blink::WebFrameClient frame_client; |
|
esprehn
2016/09/29 04:28:42
This scares me, the WebLocalFrame(Impl) below is g
dcheng
2016/09/29 04:42:38
When a frame is detached, it nulls out the client
| |
| 531 blink::WebLocalFrame::create(blink::WebTreeScopeType::Document, NULL); | 531 blink::WebLocalFrame* frame = blink::WebLocalFrame::create( |
| 532 blink::WebTreeScopeType::Document, &frame_client); | |
| 532 web_view->setMainFrame(frame); | 533 web_view->setMainFrame(frame); |
| 533 blink::WebFrameWidget* widget = | 534 blink::WebFrameWidget::create(nullptr, web_view, frame); |
| 534 blink::WebFrameWidget::create(nullptr, web_view, frame); | |
| 535 | 535 |
| 536 base::StringValue html(ResourceBundle::GetSharedInstance().GetLocalizedString( | 536 base::StringValue html(ResourceBundle::GetSharedInstance().GetLocalizedString( |
| 537 IDR_PRINT_PREVIEW_PAGE)); | 537 IDR_PRINT_PREVIEW_PAGE)); |
| 538 // Load page with script to avoid async operations. | 538 // Load page with script to avoid async operations. |
| 539 ExecuteScript(frame, kPageLoadScriptFormat, html); | 539 ExecuteScript(frame, kPageLoadScriptFormat, html); |
| 540 | 540 |
| 541 std::unique_ptr<base::DictionaryValue> options(new base::DictionaryValue()); | 541 std::unique_ptr<base::DictionaryValue> options(new base::DictionaryValue()); |
| 542 options.reset(new base::DictionaryValue()); | 542 options.reset(new base::DictionaryValue()); |
| 543 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); | 543 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); |
| 544 options->SetDouble("width", page_size.width); | 544 options->SetDouble("width", page_size.width); |
| 545 options->SetDouble("height", page_size.height); | 545 options->SetDouble("height", page_size.height); |
| 546 options->SetDouble("topMargin", page_layout.margin_top); | 546 options->SetDouble("topMargin", page_layout.margin_top); |
| 547 options->SetDouble("bottomMargin", page_layout.margin_bottom); | 547 options->SetDouble("bottomMargin", page_layout.margin_bottom); |
| 548 options->SetString("pageNumber", | 548 options->SetString("pageNumber", |
| 549 base::StringPrintf("%d/%d", page_number, total_pages)); | 549 base::StringPrintf("%d/%d", page_number, total_pages)); |
| 550 | 550 |
| 551 options->SetString("url", params.url); | 551 options->SetString("url", params.url); |
| 552 base::string16 title = source_frame.document().title(); | 552 base::string16 title = source_frame.document().title(); |
| 553 options->SetString("title", title.empty() ? params.title : title); | 553 options->SetString("title", title.empty() ? params.title : title); |
| 554 | 554 |
| 555 ExecuteScript(frame, kPageSetupScriptFormat, *options); | 555 ExecuteScript(frame, kPageSetupScriptFormat, *options); |
| 556 | 556 |
| 557 blink::WebPrintParams webkit_params(page_size); | 557 blink::WebPrintParams webkit_params(page_size); |
| 558 webkit_params.printerDPI = GetDPI(¶ms); | 558 webkit_params.printerDPI = GetDPI(¶ms); |
| 559 | 559 |
| 560 frame->printBegin(webkit_params); | 560 frame->printBegin(webkit_params); |
| 561 frame->printPage(0, canvas); | 561 frame->printPage(0, canvas); |
| 562 frame->printEnd(); | 562 frame->printEnd(); |
| 563 | 563 |
| 564 widget->close(); | |
| 565 web_view->close(); | 564 web_view->close(); |
| 566 frame->close(); | |
| 567 } | 565 } |
| 568 #endif // defined(ENABLE_PRINT_PREVIEW) | 566 #endif // defined(ENABLE_PRINT_PREVIEW) |
| 569 | 567 |
| 570 // static - Not anonymous so that platform implementations can use it. | 568 // static - Not anonymous so that platform implementations can use it. |
| 571 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame, | 569 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame, |
| 572 int page_number, | 570 int page_number, |
| 573 const gfx::Rect& canvas_area, | 571 const gfx::Rect& canvas_area, |
| 574 const gfx::Rect& content_area, | 572 const gfx::Rect& content_area, |
| 575 double scale_factor, | 573 double scale_factor, |
| 576 blink::WebCanvas* canvas) { | 574 blink::WebCanvas* canvas) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 613 } | 611 } |
| 614 | 612 |
| 615 private: | 613 private: |
| 616 // blink::WebViewClient: | 614 // blink::WebViewClient: |
| 617 void didStopLoading() override; | 615 void didStopLoading() override; |
| 618 // TODO(ojan): Remove this override and have this class use a non-null | 616 // TODO(ojan): Remove this override and have this class use a non-null |
| 619 // layerTreeView. | 617 // layerTreeView. |
| 620 bool allowsBrokenNullLayerTreeView() const override; | 618 bool allowsBrokenNullLayerTreeView() const override; |
| 621 | 619 |
| 622 // blink::WebFrameClient: | 620 // blink::WebFrameClient: |
| 623 blink::WebFrame* createChildFrame( | 621 blink::WebLocalFrame* createChildFrame( |
| 624 blink::WebLocalFrame* parent, | 622 blink::WebLocalFrame* parent, |
| 625 blink::WebTreeScopeType scope, | 623 blink::WebTreeScopeType scope, |
| 626 const blink::WebString& name, | 624 const blink::WebString& name, |
| 627 const blink::WebString& unique_name, | 625 const blink::WebString& unique_name, |
| 628 blink::WebSandboxFlags sandbox_flags, | 626 blink::WebSandboxFlags sandbox_flags, |
| 629 const blink::WebFrameOwnerProperties& frame_owner_properties) override; | 627 const blink::WebFrameOwnerProperties& frame_owner_properties) override; |
| 630 void frameDetached(blink::WebLocalFrame* frame, DetachType type) override; | |
| 631 | 628 |
| 632 void CallOnReady(); | 629 void CallOnReady(); |
| 633 void ResizeForPrinting(); | 630 void ResizeForPrinting(); |
| 634 void RestoreSize(); | 631 void RestoreSize(); |
| 635 void CopySelection(const WebPreferences& preferences); | 632 void CopySelection(const WebPreferences& preferences); |
| 636 | 633 |
| 637 FrameReference frame_; | 634 FrameReference frame_; |
| 638 blink::WebNode node_to_print_; | 635 blink::WebNode node_to_print_; |
| 639 bool owns_web_view_; | 636 bool owns_web_view_; |
| 640 blink::WebPrintParams web_print_params_; | 637 blink::WebPrintParams web_print_params_; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 768 | 765 |
| 769 void PrepareFrameAndViewForPrint::didStopLoading() { | 766 void PrepareFrameAndViewForPrint::didStopLoading() { |
| 770 DCHECK(!on_ready_.is_null()); | 767 DCHECK(!on_ready_.is_null()); |
| 771 // Don't call callback here, because it can delete |this| and WebView that is | 768 // Don't call callback here, because it can delete |this| and WebView that is |
| 772 // called didStopLoading. | 769 // called didStopLoading. |
| 773 base::ThreadTaskRunnerHandle::Get()->PostTask( | 770 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 774 FROM_HERE, base::Bind(&PrepareFrameAndViewForPrint::CallOnReady, | 771 FROM_HERE, base::Bind(&PrepareFrameAndViewForPrint::CallOnReady, |
| 775 weak_ptr_factory_.GetWeakPtr())); | 772 weak_ptr_factory_.GetWeakPtr())); |
| 776 } | 773 } |
| 777 | 774 |
| 778 blink::WebFrame* PrepareFrameAndViewForPrint::createChildFrame( | 775 blink::WebLocalFrame* PrepareFrameAndViewForPrint::createChildFrame( |
| 779 blink::WebLocalFrame* parent, | 776 blink::WebLocalFrame* parent, |
| 780 blink::WebTreeScopeType scope, | 777 blink::WebTreeScopeType scope, |
| 781 const blink::WebString& name, | 778 const blink::WebString& name, |
| 782 const blink::WebString& unique_name, | 779 const blink::WebString& unique_name, |
| 783 blink::WebSandboxFlags sandbox_flags, | 780 blink::WebSandboxFlags sandbox_flags, |
| 784 const blink::WebFrameOwnerProperties& frame_owner_properties) { | 781 const blink::WebFrameOwnerProperties& frame_owner_properties) { |
| 785 blink::WebFrame* frame = blink::WebLocalFrame::create(scope, this); | 782 blink::WebLocalFrame* frame = blink::WebLocalFrame::create(scope, this); |
| 786 parent->appendChild(frame); | 783 parent->appendChild(frame); |
| 787 return frame; | 784 return frame; |
| 788 } | 785 } |
| 789 | 786 |
| 790 void PrepareFrameAndViewForPrint::frameDetached(blink::WebLocalFrame* frame, | |
| 791 DetachType type) { | |
| 792 DCHECK(type == DetachType::Remove); | |
| 793 if (frame->parent()) | |
| 794 frame->parent()->removeChild(frame); | |
| 795 frame->close(); | |
| 796 } | |
| 797 | |
| 798 void PrepareFrameAndViewForPrint::CallOnReady() { | 787 void PrepareFrameAndViewForPrint::CallOnReady() { |
| 799 return on_ready_.Run(); // Can delete |this|. | 788 return on_ready_.Run(); // Can delete |this|. |
| 800 } | 789 } |
| 801 | 790 |
| 802 void PrepareFrameAndViewForPrint::RestoreSize() { | 791 void PrepareFrameAndViewForPrint::RestoreSize() { |
| 803 if (frame()) { | 792 if (frame()) { |
| 804 blink::WebView* web_view = frame_.GetFrame()->view(); | 793 blink::WebView* web_view = frame_.GetFrame()->view(); |
| 805 web_view->resize(prev_view_size_); | 794 web_view->resize(prev_view_size_); |
| 806 if (blink::WebFrame* web_frame = web_view->mainFrame()) | 795 if (blink::WebFrame* web_frame = web_view->mainFrame()) |
| 807 web_frame->setScrollOffset(prev_scroll_offset_); | 796 web_frame->setScrollOffset(prev_scroll_offset_); |
| (...skipping 1453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2261 blink::WebConsoleMessage::LevelWarning, message)); | 2250 blink::WebConsoleMessage::LevelWarning, message)); |
| 2262 return false; | 2251 return false; |
| 2263 } | 2252 } |
| 2264 | 2253 |
| 2265 void PrintWebViewHelper::ScriptingThrottler::Reset() { | 2254 void PrintWebViewHelper::ScriptingThrottler::Reset() { |
| 2266 // Reset counter on successful print. | 2255 // Reset counter on successful print. |
| 2267 count_ = 0; | 2256 count_ = 0; |
| 2268 } | 2257 } |
| 2269 | 2258 |
| 2270 } // namespace printing | 2259 } // namespace printing |
| OLD | NEW |