Commit e342abf1 authored by Wei Li's avatar Wei Li Committed by Commit Bot

Use printing layout correctly for subframes

When an oopif is printed along with its parent, this frame should not
use printing layout. Instead, its size is confined by its parent. Before
this change, ShouldUsePrintingLayout() works correctly in
LocalFrame::SetPrinting() since it also checks |use_printing_layout|
variable. But ShouldUsePrintingLayout() doesn't work correctly during
layout code where it is also called to decide whether printing layout
should be applied for style etc.

This CL fixes ShouldUsePrintingLayout() to return the correct value for
all cases.

BUG=853942

Change-Id: I6cad75fd6db59d00d4c07ea8b23dbaf6a4a47afc
Reviewed-on: https://chromium-review.googlesource.com/1175404
Commit-Queue: Wei Li <weili@chromium.org>
Reviewed-by: default avatarKen Buchanan <kenrb@chromium.org>
Reviewed-by: default avatarXianzhu Wang <wangxianzhu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584319}
parent d4f1fbf3
...@@ -1138,6 +1138,10 @@ void LocalFrameClientImpl::SetMouseCapture(bool capture) { ...@@ -1138,6 +1138,10 @@ void LocalFrameClientImpl::SetMouseCapture(bool capture) {
web_frame_->Client()->SetMouseCapture(capture); web_frame_->Client()->SetMouseCapture(capture);
} }
bool LocalFrameClientImpl::UsePrintingLayout() const {
return web_frame_->UsePrintingLayout();
}
STATIC_ASSERT_ENUM(DownloadCrossOriginRedirects::kFollow, STATIC_ASSERT_ENUM(DownloadCrossOriginRedirects::kFollow,
WebLocalFrameClient::CrossOriginRedirects::kFollow); WebLocalFrameClient::CrossOriginRedirects::kFollow);
STATIC_ASSERT_ENUM(DownloadCrossOriginRedirects::kNavigate, STATIC_ASSERT_ENUM(DownloadCrossOriginRedirects::kNavigate,
......
...@@ -287,6 +287,8 @@ class LocalFrameClientImpl final : public LocalFrameClient { ...@@ -287,6 +287,8 @@ class LocalFrameClientImpl final : public LocalFrameClient {
void SetMouseCapture(bool capture) override; void SetMouseCapture(bool capture) override;
bool UsePrintingLayout() const override;
private: private:
explicit LocalFrameClientImpl(WebLocalFrameImpl*); explicit LocalFrameClientImpl(WebLocalFrameImpl*);
......
...@@ -603,22 +603,14 @@ scoped_refptr<InspectorTaskRunner> LocalFrame::GetInspectorTaskRunner() { ...@@ -603,22 +603,14 @@ scoped_refptr<InspectorTaskRunner> LocalFrame::GetInspectorTaskRunner() {
void LocalFrame::StartPrinting(const FloatSize& page_size, void LocalFrame::StartPrinting(const FloatSize& page_size,
const FloatSize& original_page_size, const FloatSize& original_page_size,
float maximum_shrink_ratio) { float maximum_shrink_ratio) {
SetPrinting(/*printing=*/true, /*use_printing_layout=*/true, page_size, SetPrinting(true, page_size, original_page_size, maximum_shrink_ratio);
original_page_size, maximum_shrink_ratio);
}
void LocalFrame::StartPrintingWithoutPrintingLayout() {
SetPrinting(/*printing=*/true, /*use_printing_layout=*/false, FloatSize(),
FloatSize(), 0);
} }
void LocalFrame::EndPrinting() { void LocalFrame::EndPrinting() {
SetPrinting(/*printing=*/false, /*use_printing_layout=*/false, FloatSize(), SetPrinting(false, FloatSize(), FloatSize(), 0);
FloatSize(), 0);
} }
void LocalFrame::SetPrinting(bool printing, void LocalFrame::SetPrinting(bool printing,
bool use_printing_layout,
const FloatSize& page_size, const FloatSize& page_size,
const FloatSize& original_page_size, const FloatSize& original_page_size,
float maximum_shrink_ratio) { float maximum_shrink_ratio) {
...@@ -634,7 +626,7 @@ void LocalFrame::SetPrinting(bool printing, ...@@ -634,7 +626,7 @@ void LocalFrame::SetPrinting(bool printing,
if (TextAutosizer* text_autosizer = GetDocument()->GetTextAutosizer()) if (TextAutosizer* text_autosizer = GetDocument()->GetTextAutosizer())
text_autosizer->UpdatePageInfo(); text_autosizer->UpdatePageInfo();
if (use_printing_layout && ShouldUsePrintingLayout()) { if (ShouldUsePrintingLayout()) {
View()->ForceLayoutForPagination(page_size, original_page_size, View()->ForceLayoutForPagination(page_size, original_page_size,
maximum_shrink_ratio); maximum_shrink_ratio);
} else { } else {
...@@ -652,7 +644,7 @@ void LocalFrame::SetPrinting(bool printing, ...@@ -652,7 +644,7 @@ void LocalFrame::SetPrinting(bool printing,
child = child->Tree().NextSibling()) { child = child->Tree().NextSibling()) {
if (child->IsLocalFrame()) { if (child->IsLocalFrame()) {
if (printing) if (printing)
ToLocalFrame(child)->StartPrintingWithoutPrintingLayout(); ToLocalFrame(child)->StartPrinting();
else else
ToLocalFrame(child)->EndPrinting(); ToLocalFrame(child)->EndPrinting();
} }
...@@ -665,19 +657,22 @@ void LocalFrame::SetPrinting(bool printing, ...@@ -665,19 +657,22 @@ void LocalFrame::SetPrinting(bool printing,
} }
bool LocalFrame::ShouldUsePrintingLayout() const { bool LocalFrame::ShouldUsePrintingLayout() const {
if (!GetDocument()->Printing())
return false;
// Only the top frame being printed should be fitted to page size. // Only the top frame being printed should be fitted to page size.
// Subframes should be constrained by parents only. // Subframes should be constrained by parents only.
// This function considers the following three kinds of frames as top frames: // This function considers the following two kinds of frames as top frames:
// -- frame with no parent; // -- frame with no parent;
// -- frame's parent is remote frame;
// -- frame's parent is not in printing mode. // -- frame's parent is not in printing mode.
// Among them, if a frame's parent is a remote frame, but in printing mode, // For the second type, it is a bit complicated when its parent is a remote
// this frame should not use printing layout either. But in that case, this // frame. In such case, we can not check its document or other internal
// frame is a local top frame, the printing must start from // status. However, if the parent is in printing mode, this frame's printing
// StartPrintingWithoutPrintingLayout() so this function won't been called. // must have started with |use_printing_layout| as false in print context.
return GetDocument()->Printing() && return !Tree().Parent() ||
(!Tree().Parent() || !Tree().Parent()->IsLocalFrame() || (Tree().Parent()->IsLocalFrame() &&
!ToLocalFrame(Tree().Parent())->GetDocument()->Printing()); !ToLocalFrame(Tree().Parent())->GetDocument()->Printing()) ||
(!Tree().Parent()->IsLocalFrame() && Client()->UsePrintingLayout());
} }
FloatSize LocalFrame::ResizePageRectsKeepingRatio( FloatSize LocalFrame::ResizePageRectsKeepingRatio(
......
...@@ -207,13 +207,11 @@ class CORE_EXPORT LocalFrame final : public Frame, ...@@ -207,13 +207,11 @@ class CORE_EXPORT LocalFrame final : public Frame,
// Begin printing with the given page size information. // Begin printing with the given page size information.
// The frame content will fit to the page size with specified shrink ratio. // The frame content will fit to the page size with specified shrink ratio.
void StartPrinting(const FloatSize& page_size, // If this frame doesn't need to fit into a page size, default values are
const FloatSize& original_page_size, // used.
float maximum_shrink_ratio); void StartPrinting(const FloatSize& page_size = FloatSize(),
const FloatSize& original_page_size = FloatSize(),
// Begin printing without changing the the frame's layout. This is used for float maximum_shrink_ratio = 0);
// child frames because they don't need to fit to a page size.
void StartPrintingWithoutPrintingLayout();
void EndPrinting(); void EndPrinting();
bool ShouldUsePrintingLayout() const; bool ShouldUsePrintingLayout() const;
...@@ -402,10 +400,8 @@ class CORE_EXPORT LocalFrame final : public Frame, ...@@ -402,10 +400,8 @@ class CORE_EXPORT LocalFrame final : public Frame,
// Internal implementation for starting or ending printing. // Internal implementation for starting or ending printing.
// |printing| is true when printing starts, false when printing ends. // |printing| is true when printing starts, false when printing ends.
// |page_size|, |original_page_size|, and |maximum_shrink_ratio| are only // |page_size|, |original_page_size|, and |maximum_shrink_ratio| are only
// meaningful when starting to print with printing layout -- both |printing| // meaningful when we should use printing layout for this frame.
// and |use_printing_layout| are true.
void SetPrinting(bool printing, void SetPrinting(bool printing,
bool use_printing_layout,
const FloatSize& page_size, const FloatSize& page_size,
const FloatSize& original_page_size, const FloatSize& original_page_size,
float maximum_shrink_ratio); float maximum_shrink_ratio);
......
...@@ -438,6 +438,10 @@ class CORE_EXPORT LocalFrameClient : public FrameClient { ...@@ -438,6 +438,10 @@ class CORE_EXPORT LocalFrameClient : public FrameClient {
} }
virtual void SetMouseCapture(bool) {} virtual void SetMouseCapture(bool) {}
// Returns whether we are associated with a print context who suggests to use
// printing layout.
virtual bool UsePrintingLayout() const { return false; }
}; };
} // namespace blink } // namespace blink
......
...@@ -594,6 +594,10 @@ bool WebLocalFrameImpl::IsFocused() const { ...@@ -594,6 +594,10 @@ bool WebLocalFrameImpl::IsFocused() const {
ViewImpl()->GetPage()->GetFocusController().FocusedFrame()); ViewImpl()->GetPage()->GetFocusController().FocusedFrame());
} }
bool WebLocalFrameImpl::UsePrintingLayout() const {
return print_context_ ? print_context_->use_printing_layout() : false;
}
WebSize WebLocalFrameImpl::GetScrollOffset() const { WebSize WebLocalFrameImpl::GetScrollOffset() const {
if (ScrollableArea* scrollable_area = LayoutViewport()) if (ScrollableArea* scrollable_area = LayoutViewport())
return scrollable_area->ScrollOffsetInt(); return scrollable_area->ScrollOffsetInt();
......
...@@ -434,6 +434,9 @@ class CORE_EXPORT WebLocalFrameImpl final ...@@ -434,6 +434,9 @@ class CORE_EXPORT WebLocalFrameImpl final
// Returns true if the frame is focused. // Returns true if the frame is focused.
bool IsFocused() const; bool IsFocused() const;
// Returns true if our print context suggests using printing layout.
bool UsePrintingLayout() const;
virtual void Trace(blink::Visitor*); virtual void Trace(blink::Visitor*);
private: private:
......
...@@ -148,11 +148,6 @@ void PrintContext::BeginPrintMode(float width, float height) { ...@@ -148,11 +148,6 @@ void PrintContext::BeginPrintMode(float width, float height) {
// without going back to screen mode. // without going back to screen mode.
is_printing_ = true; is_printing_ = true;
if (!use_printing_layout_) {
frame_->StartPrintingWithoutPrintingLayout();
return;
}
FloatSize original_page_size = FloatSize(width, height); FloatSize original_page_size = FloatSize(width, height);
FloatSize min_layout_size = frame_->ResizePageRectsKeepingRatio( FloatSize min_layout_size = frame_->ResizePageRectsKeepingRatio(
original_page_size, FloatSize(width * kPrintingMinimumShrinkFactor, original_page_size, FloatSize(width * kPrintingMinimumShrinkFactor,
...@@ -326,6 +321,10 @@ void PrintContext::Trace(blink::Visitor* visitor) { ...@@ -326,6 +321,10 @@ void PrintContext::Trace(blink::Visitor* visitor) {
visitor->Trace(linked_destinations_); visitor->Trace(linked_destinations_);
} }
bool PrintContext::use_printing_layout() const {
return use_printing_layout_;
}
ScopedPrintContext::ScopedPrintContext(LocalFrame* frame) ScopedPrintContext::ScopedPrintContext(LocalFrame* frame)
: context_(new PrintContext(frame, /*use_printing_layout=*/true)) {} : context_(new PrintContext(frame, /*use_printing_layout=*/true)) {}
......
...@@ -106,6 +106,8 @@ class CORE_EXPORT PrintContext ...@@ -106,6 +106,8 @@ class CORE_EXPORT PrintContext
virtual void Trace(blink::Visitor*); virtual void Trace(blink::Visitor*);
bool use_printing_layout() const;
protected: protected:
friend class PrintContextTest; friend class PrintContextTest;
......
...@@ -424,7 +424,7 @@ TEST_P(PrintContextFrameTest, DISABLED_SubframePrintPageLayout) { ...@@ -424,7 +424,7 @@ TEST_P(PrintContextFrameTest, DISABLED_SubframePrintPageLayout) {
EXPECT_EQ(child->OffsetWidth(), 800); EXPECT_EQ(child->OffsetWidth(), 800);
EXPECT_EQ(target->OffsetWidth(), 800); EXPECT_EQ(target->OffsetWidth(), 800);
GetDocument().GetFrame()->StartPrintingWithoutPrintingLayout(); GetDocument().GetFrame()->StartPrinting();
EXPECT_EQ(parent->OffsetWidth(), 800); EXPECT_EQ(parent->OffsetWidth(), 800);
EXPECT_EQ(child->OffsetWidth(), 800); EXPECT_EQ(child->OffsetWidth(), 800);
EXPECT_EQ(target->OffsetWidth(), 800); EXPECT_EQ(target->OffsetWidth(), 800);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment