1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "RenderFrameBase.h"
28
29#include "Frame.h"
30#include "FrameView.h"
31#include "HTMLFrameElementBase.h"
32#include "RenderView.h"
33
34namespace WebCore {
35
36RenderFrameBase::RenderFrameBase(HTMLFrameElementBase& element, PassRef<RenderStyle> style)
37    : RenderWidget(element, WTF::move(style))
38{
39}
40
41inline bool shouldExpandFrame(LayoutUnit width, LayoutUnit height, bool hasFixedWidth, bool hasFixedHeight)
42{
43    // If the size computed to zero never expand.
44    if (!width || !height)
45        return false;
46    // Really small fixed size frames can't be meant to be scrolled and are there probably by mistake. Avoid expanding.
47    static unsigned smallestUsefullyScrollableDimension = 8;
48    if (hasFixedWidth && width < LayoutUnit(smallestUsefullyScrollableDimension))
49        return false;
50    if (hasFixedHeight && height < LayoutUnit(smallestUsefullyScrollableDimension))
51        return false;
52    return true;
53}
54
55void RenderFrameBase::layoutWithFlattening(bool hasFixedWidth, bool hasFixedHeight)
56{
57    FrameView* childFrameView = childView();
58    RenderView* childRoot = childFrameView ? childFrameView->frame().contentRenderer() : 0;
59
60    if (!childRoot || !shouldExpandFrame(width(), height(), hasFixedWidth, hasFixedHeight)) {
61        updateWidgetPosition();
62        if (childFrameView)
63            childFrameView->layout();
64        clearNeedsLayout();
65        return;
66    }
67
68    // need to update to calculate min/max correctly
69    updateWidgetPosition();
70
71    // if scrollbars are off, and the width or height are fixed
72    // we obey them and do not expand. With frame flattening
73    // no subframe much ever become scrollable.
74
75    bool isScrollable = frameOwnerElement().scrollingMode() != ScrollbarAlwaysOff;
76
77    // consider iframe inset border
78    int hBorder = borderLeft() + borderRight();
79    int vBorder = borderTop() + borderBottom();
80
81    // make sure minimum preferred width is enforced
82    if (isScrollable || !hasFixedWidth) {
83        setWidth(std::max(width(), childRoot->minPreferredLogicalWidth() + hBorder));
84        // update again to pass the new width to the child frame
85        updateWidgetPosition();
86        childFrameView->layout();
87    }
88
89    // expand the frame by setting frame height = content height
90    if (isScrollable || !hasFixedHeight || childRoot->isFrameSet())
91        setHeight(std::max<LayoutUnit>(height(), childFrameView->contentsHeight() + vBorder));
92    if (isScrollable || !hasFixedWidth || childRoot->isFrameSet())
93        setWidth(std::max<LayoutUnit>(width(), childFrameView->contentsWidth() + hBorder));
94
95    updateWidgetPosition();
96
97    ASSERT(!childFrameView->layoutPending());
98    ASSERT(!childRoot->needsLayout());
99    ASSERT(!childRoot->firstChild() || !childRoot->firstChild()->firstChildSlow() || !childRoot->firstChild()->firstChildSlow()->needsLayout());
100
101    clearNeedsLayout();
102}
103
104}
105