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 "FrameTree.h"
25 #include "FrameView.h"
27 #include "PageGroup.h"
30 #include <wtf/StringExtras.h>
31 #include <wtf/Vector.h>
32 #include <wtf/text/CString.h>
38 FrameTree::~FrameTree()
40 for (Frame* child = firstChild(); child; child = child->tree()->nextSibling())
44 void FrameTree::setName(const AtomicString& name)
51 m_uniqueName = AtomicString(); // Remove our old frame name so it's not considered in uniqueChildName.
52 m_uniqueName = parent()->tree()->uniqueChildName(name);
55 void FrameTree::clearName()
57 m_name = AtomicString();
58 m_uniqueName = AtomicString();
61 Frame* FrameTree::parent() const
66 bool FrameTree::transferChild(PassRefPtr<Frame> child)
68 Frame* oldParent = child->tree()->parent();
69 if (oldParent == m_thisFrame)
70 return false; // |child| is already a child of m_thisFrame.
73 oldParent->tree()->removeChild(child.get());
75 ASSERT(child->page() == m_thisFrame->page());
76 child->tree()->m_parent = m_thisFrame;
78 // We need to ensure that the child still has a unique frame name with respect to its new parent.
79 child->tree()->setName(child->tree()->m_name);
81 actuallyAppendChild(child); // Note, on return |child| is null.
85 void FrameTree::appendChild(PassRefPtr<Frame> child)
87 ASSERT(child->page() == m_thisFrame->page());
88 child->tree()->m_parent = m_thisFrame;
89 actuallyAppendChild(child); // Note, on return |child| is null.
92 void FrameTree::actuallyAppendChild(PassRefPtr<Frame> child)
94 ASSERT(child->tree()->m_parent == m_thisFrame);
95 Frame* oldLast = m_lastChild;
96 m_lastChild = child.get();
99 child->tree()->m_previousSibling = oldLast;
100 oldLast->tree()->m_nextSibling = child;
102 m_firstChild = child;
104 m_scopedChildCount = invalidCount;
106 ASSERT(!m_lastChild->tree()->m_nextSibling);
109 void FrameTree::removeChild(Frame* child)
111 child->tree()->m_parent = 0;
113 // Slightly tricky way to prevent deleting the child until we are done with it, w/o
114 // extra refs. These swaps leave the child in a circular list by itself. Clearing its
115 // previous and next will then finally deref it.
117 RefPtr<Frame>& newLocationForNext = m_firstChild == child ? m_firstChild : child->tree()->m_previousSibling->tree()->m_nextSibling;
118 Frame*& newLocationForPrevious = m_lastChild == child ? m_lastChild : child->tree()->m_nextSibling->tree()->m_previousSibling;
119 swap(newLocationForNext, child->tree()->m_nextSibling);
120 // For some inexplicable reason, the following line does not compile without the explicit std:: namespace
121 std::swap(newLocationForPrevious, child->tree()->m_previousSibling);
123 child->tree()->m_previousSibling = 0;
124 child->tree()->m_nextSibling = 0;
126 m_scopedChildCount = invalidCount;
129 AtomicString FrameTree::uniqueChildName(const AtomicString& requestedName) const
131 if (!requestedName.isEmpty() && !child(requestedName) && requestedName != "_blank")
132 return requestedName;
134 // Create a repeatable name for a child about to be added to us. The name must be
135 // unique within the frame tree. The string we generate includes a "path" of names
136 // from the root frame down to us. For this path to be unique, each set of siblings must
137 // contribute a unique name to the path, which can't collide with any HTML-assigned names.
138 // We generate this path component by index in the child list along with an unlikely
139 // frame name that can't be set in HTML because it collides with comment syntax.
141 const char framePathPrefix[] = "<!--framePath ";
142 const int framePathPrefixLength = 14;
143 const int framePathSuffixLength = 3;
145 // Find the nearest parent that has a frame with a path in it.
146 Vector<Frame*, 16> chain;
148 for (frame = m_thisFrame; frame; frame = frame->tree()->parent()) {
149 if (frame->tree()->uniqueName().startsWith(framePathPrefix))
154 name += framePathPrefix;
156 name += frame->tree()->uniqueName().string().substring(framePathPrefixLength,
157 frame->tree()->uniqueName().length() - framePathPrefixLength - framePathSuffixLength);
158 for (int i = chain.size() - 1; i >= 0; --i) {
161 name += frame->tree()->uniqueName();
164 // Suffix buffer has more than enough space for:
165 // 10 characters before the number
166 // a number (20 digits for the largest 64-bit integer)
167 // 6 characters after the number
168 // trailing null byte
169 // But we still use snprintf just to be extra-safe.
171 snprintf(suffix, sizeof(suffix), "/<!--frame%u-->-->", childCount());
175 return AtomicString(name);
178 inline Frame* FrameTree::scopedChild(unsigned index, TreeScope* scope) const
180 unsigned scopedIndex = 0;
181 for (Frame* result = firstChild(); result; result = result->tree()->nextSibling()) {
182 if (result->inScope(scope)) {
183 if (scopedIndex == index)
192 inline Frame* FrameTree::scopedChild(const AtomicString& name, TreeScope* scope) const
194 for (Frame* child = firstChild(); child; child = child->tree()->nextSibling())
195 if (child->tree()->uniqueName() == name && child->inScope(scope))
200 inline unsigned FrameTree::scopedChildCount(TreeScope* scope) const
202 unsigned scopedCount = 0;
203 for (Frame* result = firstChild(); result; result = result->tree()->nextSibling()) {
204 if (result->inScope(scope))
211 Frame* FrameTree::scopedChild(unsigned index) const
213 return scopedChild(index, m_thisFrame->document());
216 Frame* FrameTree::scopedChild(const AtomicString& name) const
218 return scopedChild(name, m_thisFrame->document());
221 unsigned FrameTree::scopedChildCount() const
223 if (m_scopedChildCount == invalidCount)
224 m_scopedChildCount = scopedChildCount(m_thisFrame->document());
225 return m_scopedChildCount;
228 unsigned FrameTree::childCount() const
231 for (Frame* result = firstChild(); result; result = result->tree()->nextSibling())
236 Frame* FrameTree::child(unsigned index) const
238 Frame* result = firstChild();
239 for (unsigned i = 0; result && i != index; ++i)
240 result = result->tree()->nextSibling();
244 Frame* FrameTree::child(const AtomicString& name) const
246 for (Frame* child = firstChild(); child; child = child->tree()->nextSibling())
247 if (child->tree()->uniqueName() == name)
252 Frame* FrameTree::find(const AtomicString& name) const
254 if (name == "_self" || name == "_current" || name.isEmpty())
260 if (name == "_parent")
261 return parent() ? parent() : m_thisFrame;
263 // Since "_blank" should never be any frame's name, the following just amounts to an optimization.
264 if (name == "_blank")
267 // Search subtree starting with this frame first.
268 for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->traverseNext(m_thisFrame))
269 if (frame->tree()->uniqueName() == name)
272 // Search the entire tree for this page next.
273 Page* page = m_thisFrame->page();
275 // The frame could have been detached from the page, so check it.
279 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext())
280 if (frame->tree()->uniqueName() == name)
283 // Search the entire tree of each of the other pages in this namespace.
284 // FIXME: Is random order OK?
285 const HashSet<Page*>& pages = page->group().pages();
286 HashSet<Page*>::const_iterator end = pages.end();
287 for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) {
288 Page* otherPage = *it;
289 if (otherPage != page) {
290 for (Frame* frame = otherPage->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
291 if (frame->tree()->uniqueName() == name)
300 bool FrameTree::isDescendantOf(const Frame* ancestor) const
305 if (m_thisFrame->page() != ancestor->page())
308 for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->parent())
309 if (frame == ancestor)
314 Frame* FrameTree::traverseNext(const Frame* stayWithin) const
316 Frame* child = firstChild();
318 ASSERT(!stayWithin || child->tree()->isDescendantOf(stayWithin));
322 if (m_thisFrame == stayWithin)
325 Frame* sibling = nextSibling();
327 ASSERT(!stayWithin || sibling->tree()->isDescendantOf(stayWithin));
331 Frame* frame = m_thisFrame;
332 while (!sibling && (!stayWithin || frame->tree()->parent() != stayWithin)) {
333 frame = frame->tree()->parent();
336 sibling = frame->tree()->nextSibling();
340 ASSERT(!stayWithin || !sibling || sibling->tree()->isDescendantOf(stayWithin));
347 Frame* FrameTree::traverseNextWithWrap(bool wrap) const
349 if (Frame* result = traverseNext())
353 return m_thisFrame->page()->mainFrame();
358 Frame* FrameTree::traversePreviousWithWrap(bool wrap) const
360 // FIXME: besides the wrap feature, this is just the traversePreviousNode algorithm
362 if (Frame* prevSibling = previousSibling())
363 return prevSibling->tree()->deepLastChild();
364 if (Frame* parentFrame = parent())
367 // no siblings, no parent, self==top
369 return deepLastChild();
371 // top view is always the last one in this ordering, so prev is nil without wrap
375 Frame* FrameTree::deepLastChild() const
377 Frame* result = m_thisFrame;
378 for (Frame* last = lastChild(); last; last = last->tree()->lastChild())
384 Frame* FrameTree::top() const
386 Frame* frame = m_thisFrame;
387 for (Frame* parent = m_thisFrame; parent; parent = parent->tree()->parent())
392 } // namespace WebCore
396 static void printIndent(int indent)
398 for (int i = 0; i < indent; ++i)
402 static void printFrames(const WebCore::Frame* frame, const WebCore::Frame* targetFrame, int indent)
404 if (frame == targetFrame) {
406 printIndent(indent - 1);
410 WebCore::FrameView* view = frame->view();
411 printf("Frame %p %dx%d\n", frame, view ? view->width() : 0, view ? view->height() : 0);
413 printf(" ownerElement=%p\n", frame->ownerElement());
415 printf(" frameView=%p\n", view);
417 printf(" document=%p\n", frame->document());
419 printf(" uri=%s\n\n", frame->document()->documentURI().utf8().data());
421 for (WebCore::Frame* child = frame->tree()->firstChild(); child; child = child->tree()->nextSibling())
422 printFrames(child, targetFrame, indent + 1);
425 void showFrameTree(const WebCore::Frame* frame)
428 printf("Null input frame\n");
432 printFrames(frame->tree()->top(), frame, 0);