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"
29 #include <wtf/StringExtras.h>
30 #include <wtf/Vector.h>
36 FrameTree::~FrameTree()
38 for (Frame* child = firstChild(); child; child = child->tree()->nextSibling())
42 void FrameTree::setName(const AtomicString& name)
49 m_uniqueName = AtomicString(); // Remove our old frame name so it's not considered in uniqueChildName.
50 m_uniqueName = parent()->tree()->uniqueChildName(name);
53 void FrameTree::clearName()
55 m_name = AtomicString();
56 m_uniqueName = AtomicString();
59 Frame* FrameTree::parent(bool checkForDisconnectedFrame) const
61 if (checkForDisconnectedFrame && m_thisFrame->isDisconnected())
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;
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;
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 Frame* FrameTree::child(unsigned index) const
180 Frame* result = firstChild();
181 for (unsigned i = 0; result && i != index; ++i)
182 result = result->tree()->nextSibling();
186 Frame* FrameTree::child(const AtomicString& name) const
188 for (Frame* child = firstChild(); child; child = child->tree()->nextSibling())
189 if (child->tree()->uniqueName() == name)
194 Frame* FrameTree::find(const AtomicString& name) const
196 if (name == "_self" || name == "_current" || name.isEmpty())
202 if (name == "_parent")
203 return parent() ? parent() : m_thisFrame;
205 // Since "_blank" should never be any frame's name, the following just amounts to an optimization.
206 if (name == "_blank")
209 // Search subtree starting with this frame first.
210 for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->traverseNext(m_thisFrame))
211 if (frame->tree()->uniqueName() == name)
214 // Search the entire tree for this page next.
215 Page* page = m_thisFrame->page();
217 // The frame could have been detached from the page, so check it.
221 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext())
222 if (frame->tree()->uniqueName() == name)
225 // Search the entire tree of each of the other pages in this namespace.
226 // FIXME: Is random order OK?
227 const HashSet<Page*>& pages = page->group().pages();
228 HashSet<Page*>::const_iterator end = pages.end();
229 for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) {
230 Page* otherPage = *it;
231 if (otherPage != page) {
232 for (Frame* frame = otherPage->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
233 if (frame->tree()->uniqueName() == name)
242 bool FrameTree::isDescendantOf(const Frame* ancestor) const
247 if (m_thisFrame->page() != ancestor->page())
250 for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->parent())
251 if (frame == ancestor)
256 Frame* FrameTree::traverseNext(const Frame* stayWithin) const
258 Frame* child = firstChild();
260 ASSERT(!stayWithin || child->tree()->isDescendantOf(stayWithin));
264 if (m_thisFrame == stayWithin)
267 Frame* sibling = nextSibling();
269 ASSERT(!stayWithin || sibling->tree()->isDescendantOf(stayWithin));
273 Frame* frame = m_thisFrame;
274 while (!sibling && (!stayWithin || frame->tree()->parent() != stayWithin)) {
275 frame = frame->tree()->parent();
278 sibling = frame->tree()->nextSibling();
282 ASSERT(!stayWithin || !sibling || sibling->tree()->isDescendantOf(stayWithin));
289 Frame* FrameTree::traverseNextWithWrap(bool wrap) const
291 if (Frame* result = traverseNext())
295 return m_thisFrame->page()->mainFrame();
300 Frame* FrameTree::traversePreviousWithWrap(bool wrap) const
302 // FIXME: besides the wrap feature, this is just the traversePreviousNode algorithm
304 if (Frame* prevSibling = previousSibling())
305 return prevSibling->tree()->deepLastChild();
306 if (Frame* parentFrame = parent())
309 // no siblings, no parent, self==top
311 return deepLastChild();
313 // top view is always the last one in this ordering, so prev is nil without wrap
317 Frame* FrameTree::deepLastChild() const
319 Frame* result = m_thisFrame;
320 for (Frame* last = lastChild(); last; last = last->tree()->lastChild())
326 Frame* FrameTree::top(bool checkForDisconnectedFrame) const
328 Frame* frame = m_thisFrame;
329 for (Frame* parent = m_thisFrame; parent; parent = parent->tree()->parent()) {
331 if (checkForDisconnectedFrame && frame->isDisconnected())
337 } // namespace WebCore