2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
3 * Copyright (C) 2006 Apple Computer, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #include "core/page/FrameTree.h"
24 #include "core/dom/Document.h"
25 #include "core/frame/FrameClient.h"
26 #include "core/frame/FrameView.h"
27 #include "core/frame/LocalFrame.h"
28 #include "core/frame/RemoteFrame.h"
29 #include "core/frame/RemoteFrameView.h"
30 #include "core/page/Page.h"
31 #include "wtf/Vector.h"
32 #include "wtf/text/CString.h"
33 #include "wtf/text/StringBuilder.h"
41 const unsigned invalidChildCount = ~0U;
45 FrameTree::FrameTree(Frame* thisFrame)
46 : m_thisFrame(thisFrame)
47 , m_scopedChildCount(invalidChildCount)
51 FrameTree::~FrameTree()
54 // FIXME: Why is this here? Doesn't this parallel what we already do in ~LocalFrame?
55 for (Frame* child = firstChild(); child; child = child->tree().nextSibling()) {
56 if (child->isLocalFrame())
57 toLocalFrame(child)->setView(nullptr);
58 else if (child->isRemoteFrame())
59 toRemoteFrame(child)->setView(nullptr);
64 void FrameTree::setName(const AtomicString& name, const AtomicString& fallbackName)
71 m_uniqueName = AtomicString(); // Remove our old frame name so it's not considered in uniqueChildName.
72 m_uniqueName = parent()->tree().uniqueChildName(name.isEmpty() ? fallbackName : name);
75 Frame* FrameTree::parent() const
77 if (!m_thisFrame->client())
79 return m_thisFrame->client()->parent();
82 Frame* FrameTree::top() const
84 // FIXME: top() should never return null, so here are some hacks to deal
85 // with EmptyFrameLoaderClient and cases where the frame is detached
87 if (!m_thisFrame->client())
89 Frame* candidate = m_thisFrame->client()->top();
90 return candidate ? candidate : m_thisFrame.get();
93 Frame* FrameTree::previousSibling() const
95 if (!m_thisFrame->client())
97 return m_thisFrame->client()->previousSibling();
100 Frame* FrameTree::nextSibling() const
102 if (!m_thisFrame->client())
104 return m_thisFrame->client()->nextSibling();
107 Frame* FrameTree::firstChild() const
109 if (!m_thisFrame->client())
111 return m_thisFrame->client()->firstChild();
114 Frame* FrameTree::lastChild() const
116 if (!m_thisFrame->client())
118 return m_thisFrame->client()->lastChild();
121 bool FrameTree::uniqueNameExists(const AtomicString& name) const
123 for (Frame* frame = top(); frame; frame = frame->tree().traverseNext()) {
124 if (frame->tree().uniqueName() == name)
130 AtomicString FrameTree::uniqueChildName(const AtomicString& requestedName) const
132 if (!requestedName.isEmpty() && !uniqueNameExists(requestedName) && requestedName != "_blank")
133 return requestedName;
135 // Create a repeatable name for a child about to be added to us. The name must be
136 // unique within the frame tree. The string we generate includes a "path" of names
137 // from the root frame down to us. For this path to be unique, each set of siblings must
138 // contribute a unique name to the path, which can't collide with any HTML-assigned names.
139 // We generate this path component by index in the child list along with an unlikely
140 // frame name that can't be set in HTML because it collides with comment syntax.
142 const char framePathPrefix[] = "<!--framePath ";
143 const int framePathPrefixLength = 14;
144 const int framePathSuffixLength = 3;
146 // Find the nearest parent that has a frame with a path in it.
147 Vector<Frame*, 16> chain;
149 for (frame = m_thisFrame; frame; frame = frame->tree().parent()) {
150 if (frame->tree().uniqueName().startsWith(framePathPrefix))
155 name.append(framePathPrefix);
157 name.append(frame->tree().uniqueName().string().substring(framePathPrefixLength,
158 frame->tree().uniqueName().length() - framePathPrefixLength - framePathSuffixLength));
160 for (int i = chain.size() - 1; i >= 0; --i) {
163 name.append(frame->tree().uniqueName());
166 name.appendLiteral("/<!--frame");
167 name.appendNumber(childCount() - 1);
168 name.appendLiteral("-->-->");
170 return name.toAtomicString();
173 Frame* FrameTree::scopedChild(unsigned index) const
175 if (!m_thisFrame->isLocalFrame())
177 TreeScope* scope = toLocalFrame(m_thisFrame)->document();
181 unsigned scopedIndex = 0;
182 for (Frame* result = firstChild(); result; result = result->tree().nextSibling()) {
183 if (result->isLocalFrame() && toLocalFrame(result)->inScope(scope)) {
184 if (scopedIndex == index)
193 Frame* FrameTree::scopedChild(const AtomicString& name) const
195 if (!m_thisFrame->isLocalFrame())
198 TreeScope* scope = toLocalFrame(m_thisFrame)->document();
202 for (Frame* child = firstChild(); child; child = child->tree().nextSibling())
203 if (child->tree().name() == name && child->isLocalFrame() && toLocalFrame(child)->inScope(scope))
208 inline unsigned FrameTree::scopedChildCount(TreeScope* scope) const
213 unsigned scopedCount = 0;
214 for (Frame* result = firstChild(); result; result = result->tree().nextSibling()) {
215 if (result->isLocalFrame() && toLocalFrame(result)->inScope(scope))
222 unsigned FrameTree::scopedChildCount() const
224 if (m_scopedChildCount == invalidChildCount)
225 m_scopedChildCount = scopedChildCount(toLocalFrame(m_thisFrame)->document());
226 return m_scopedChildCount;
229 void FrameTree::invalidateScopedChildCount()
231 m_scopedChildCount = invalidChildCount;
234 unsigned FrameTree::childCount() const
237 for (Frame* result = firstChild(); result; result = result->tree().nextSibling())
242 Frame* FrameTree::child(const AtomicString& name) const
244 for (Frame* child = firstChild(); child; child = child->tree().nextSibling())
245 if (child->tree().name() == name)
250 Frame* FrameTree::find(const AtomicString& name) const
252 if (name == "_self" || name == "_current" || name.isEmpty())
258 if (name == "_parent")
259 return parent() ? parent() : m_thisFrame.get();
261 // Since "_blank" should never be any frame's name, the following just amounts to an optimization.
262 if (name == "_blank")
265 // Search subtree starting with this frame first.
266 for (Frame* frame = m_thisFrame; frame; frame = frame->tree().traverseNext(m_thisFrame))
267 if (frame->tree().name() == name)
270 // Search the entire tree for this page next.
271 Page* page = m_thisFrame->page();
273 // The frame could have been detached from the page, so check it.
277 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverseNext())
278 if (frame->tree().name() == name)
281 // Search the entire tree of each of the other pages in this namespace.
282 // FIXME: Is random order OK?
283 const HashSet<Page*>& pages = Page::ordinaryPages();
284 HashSet<Page*>::const_iterator end = pages.end();
285 for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) {
286 Page* otherPage = *it;
287 if (otherPage != page) {
288 for (Frame* frame = otherPage->mainFrame(); frame; frame = frame->tree().traverseNext()) {
289 if (frame->tree().name() == name)
298 bool FrameTree::isDescendantOf(const Frame* ancestor) const
303 if (m_thisFrame->page() != ancestor->page())
306 for (Frame* frame = m_thisFrame; frame; frame = frame->tree().parent())
307 if (frame == ancestor)
312 Frame* FrameTree::traverseNext(const Frame* stayWithin) const
314 Frame* child = firstChild();
316 ASSERT(!stayWithin || child->tree().isDescendantOf(stayWithin));
320 if (m_thisFrame == stayWithin)
323 Frame* sibling = nextSibling();
325 ASSERT(!stayWithin || sibling->tree().isDescendantOf(stayWithin));
329 Frame* frame = m_thisFrame;
330 while (!sibling && (!stayWithin || frame->tree().parent() != stayWithin)) {
331 frame = frame->tree().parent();
334 sibling = frame->tree().nextSibling();
338 ASSERT(!stayWithin || !sibling || sibling->tree().isDescendantOf(stayWithin));
345 Frame* FrameTree::traverseNextWithWrap(bool wrap) const
347 if (Frame* result = traverseNext())
351 return m_thisFrame->page()->mainFrame();
356 Frame* FrameTree::traversePreviousWithWrap(bool wrap) const
358 // FIXME: besides the wrap feature, this is just the traversePreviousNode algorithm
360 if (Frame* prevSibling = previousSibling())
361 return prevSibling->tree().deepLastChild();
362 if (Frame* parentFrame = parent())
365 // no siblings, no parent, self==top
367 return deepLastChild();
369 // top view is always the last one in this ordering, so prev is nil without wrap
373 Frame* FrameTree::deepLastChild() const
375 Frame* result = m_thisFrame;
376 for (Frame* last = lastChild(); last; last = last->tree().lastChild())
382 void FrameTree::trace(Visitor* visitor)
384 visitor->trace(m_thisFrame);
391 static void printIndent(int indent)
393 for (int i = 0; i < indent; ++i)
397 static void printFrames(const blink::Frame* frame, const blink::Frame* targetFrame, int indent)
399 if (frame == targetFrame) {
401 printIndent(indent - 1);
405 blink::FrameView* view = frame->isLocalFrame() ? toLocalFrame(frame)->view() : 0;
406 printf("Frame %p %dx%d\n", frame, view ? view->width() : 0, view ? view->height() : 0);
408 printf(" owner=%p\n", frame->owner());
410 printf(" frameView=%p\n", view);
412 printf(" document=%p\n", frame->isLocalFrame() ? toLocalFrame(frame)->document() : 0);
414 printf(" uri=%s\n\n", frame->isLocalFrame() ? toLocalFrame(frame)->document()->url().string().utf8().data() : 0);
416 for (blink::Frame* child = frame->tree().firstChild(); child; child = child->tree().nextSibling())
417 printFrames(child, targetFrame, indent + 1);
420 void showFrameTree(const blink::Frame* frame)
423 printf("Null input frame\n");
427 printFrames(frame->tree().top(), frame, 0);