1/*
2 * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#pragma once
21
22#include <wtf/text/AtomString.h>
23
24namespace WebCore {
25
26enum class CanWrap : bool { No, Yes };
27enum class DidWrap : bool { No, Yes };
28
29class Frame;
30class TreeScope;
31
32class FrameTree {
33 WTF_MAKE_NONCOPYABLE(FrameTree);
34public:
35 const static unsigned invalidCount = static_cast<unsigned>(-1);
36
37 FrameTree(Frame& thisFrame, Frame* parentFrame)
38 : m_thisFrame(thisFrame)
39 , m_parent(parentFrame)
40 , m_previousSibling(nullptr)
41 , m_lastChild(nullptr)
42 , m_scopedChildCount(invalidCount)
43 {
44 }
45
46 ~FrameTree();
47
48 const AtomString& name() const { return m_name; }
49 const AtomString& uniqueName() const { return m_uniqueName; }
50 WEBCORE_EXPORT void setName(const AtomString&);
51 WEBCORE_EXPORT void clearName();
52 WEBCORE_EXPORT Frame* parent() const;
53
54 Frame* nextSibling() const { return m_nextSibling.get(); }
55 Frame* previousSibling() const { return m_previousSibling; }
56 Frame* firstChild() const { return m_firstChild.get(); }
57 Frame* lastChild() const { return m_lastChild; }
58
59 Frame* firstRenderedChild() const;
60 Frame* nextRenderedSibling() const;
61
62 WEBCORE_EXPORT bool isDescendantOf(const Frame* ancestor) const;
63
64 WEBCORE_EXPORT Frame* traverseNext(const Frame* stayWithin = nullptr) const;
65 // Rendered means being the main frame or having an ownerRenderer. It may not have been parented in the Widget tree yet (see WidgetHierarchyUpdatesSuspensionScope).
66 WEBCORE_EXPORT Frame* traverseNextRendered(const Frame* stayWithin = nullptr) const;
67 WEBCORE_EXPORT Frame* traverseNext(CanWrap, DidWrap* = nullptr) const;
68 WEBCORE_EXPORT Frame* traversePrevious(CanWrap, DidWrap* = nullptr) const;
69
70 Frame* traverseNextInPostOrder(CanWrap) const;
71
72 WEBCORE_EXPORT void appendChild(Frame&);
73 void detachFromParent() { m_parent = nullptr; }
74 void removeChild(Frame&);
75
76 Frame* child(unsigned index) const;
77 Frame* child(const AtomString& name) const;
78 WEBCORE_EXPORT Frame* find(const AtomString& name, Frame& activeFrame) const;
79 WEBCORE_EXPORT unsigned childCount() const;
80 WEBCORE_EXPORT Frame& top() const;
81
82 WEBCORE_EXPORT Frame* scopedChild(unsigned index) const;
83 WEBCORE_EXPORT Frame* scopedChild(const AtomString& name) const;
84 unsigned scopedChildCount() const;
85
86 void resetFrameIdentifiers() { m_frameIDGenerator = 0; }
87
88private:
89 Frame* deepFirstChild() const;
90 Frame* deepLastChild() const;
91
92 bool scopedBy(TreeScope*) const;
93 Frame* scopedChild(unsigned index, TreeScope*) const;
94 Frame* scopedChild(const AtomString& name, TreeScope*) const;
95 unsigned scopedChildCount(TreeScope*) const;
96
97 AtomString uniqueChildName(const AtomString& requestedName) const;
98 AtomString generateUniqueName() const;
99
100 Frame& m_thisFrame;
101
102 Frame* m_parent;
103 AtomString m_name; // The actual frame name (may be empty).
104 AtomString m_uniqueName;
105
106 RefPtr<Frame> m_nextSibling;
107 Frame* m_previousSibling;
108 RefPtr<Frame> m_firstChild;
109 Frame* m_lastChild;
110 mutable unsigned m_scopedChildCount;
111 mutable uint64_t m_frameIDGenerator { 0 };
112};
113
114} // namespace WebCore
115
116#if ENABLE(TREE_DEBUGGING)
117// Outside the WebCore namespace for ease of invocation from the debugger.
118WEBCORE_EXPORT void showFrameTree(const WebCore::Frame*);
119#endif
120