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>
37 FrameTree::~FrameTree()
39 for (Frame* child = firstChild(); child; child = child->tree()->nextSibling())
43 void FrameTree::setName(const AtomicString& name)
50 m_uniqueName = AtomicString(); // Remove our old frame name so it's not considered in uniqueChildName.
51 m_uniqueName = parent()->tree()->uniqueChildName(name);
54 void FrameTree::clearName()
56 m_name = AtomicString();
57 m_uniqueName = AtomicString();
60 Frame* FrameTree::parent(bool checkForDisconnectedFrame) const
62 if (checkForDisconnectedFrame && m_thisFrame->isDisconnected())
67 bool FrameTree::transferChild(PassRefPtr<Frame> child)
69 Frame* oldParent = child->tree()->parent();
70 if (oldParent == m_thisFrame)
71 return false; // |child| is already a child of m_thisFrame.
74 oldParent->tree()->removeChild(child.get());
76 ASSERT(child->page() == m_thisFrame->page());
77 child->tree()->m_parent = m_thisFrame;
79 // We need to ensure that the child still has a unique frame name with respect to its new parent.
80 child->tree()->setName(child->tree()->m_name);
82 actuallyAppendChild(child); // Note, on return |child| is null.
86 void FrameTree::appendChild(PassRefPtr<Frame> child)
88 ASSERT(child->page() == m_thisFrame->page());
89 child->tree()->m_parent = m_thisFrame;
90 actuallyAppendChild(child); // Note, on return |child| is null.
93 void FrameTree::actuallyAppendChild(PassRefPtr<Frame> child)
95 ASSERT(child->tree()->m_parent == m_thisFrame);
96 Frame* oldLast = m_lastChild;
97 m_lastChild = child.get();
100 child->tree()->m_previousSibling = oldLast;
101 oldLast->tree()->m_nextSibling = child;
103 m_firstChild = child;
107 ASSERT(!m_lastChild->tree()->m_nextSibling);
110 void FrameTree::removeChild(Frame* child)
112 child->tree()->m_parent = 0;
114 // Slightly tricky way to prevent deleting the child until we are done with it, w/o
115 // extra refs. These swaps leave the child in a circular list by itself. Clearing its
116 // previous and next will then finally deref it.
118 RefPtr<Frame>& newLocationForNext = m_firstChild == child ? m_firstChild : child->tree()->m_previousSibling->tree()->m_nextSibling;
119 Frame*& newLocationForPrevious = m_lastChild == child ? m_lastChild : child->tree()->m_nextSibling->tree()->m_previousSibling;
120 swap(newLocationForNext, child->tree()->m_nextSibling);
121 // For some inexplicable reason, the following line does not compile without the explicit std:: namespace
122 std::swap(newLocationForPrevious, child->tree()->m_previousSibling);
124 child->tree()->m_previousSibling = 0;
125 child->tree()->m_nextSibling = 0;
130 AtomicString FrameTree::uniqueChildName(const AtomicString& requestedName) const
132 if (!requestedName.isEmpty() && !child(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 += framePathPrefix;
157 name += frame->tree()->uniqueName().string().substring(framePathPrefixLength,
158 frame->tree()->uniqueName().length() - framePathPrefixLength - framePathSuffixLength);
159 for (int i = chain.size() - 1; i >= 0; --i) {
162 name += frame->tree()->uniqueName();
165 // Suffix buffer has more than enough space for:
166 // 10 characters before the number
167 // a number (20 digits for the largest 64-bit integer)
168 // 6 characters after the number
169 // trailing null byte
170 // But we still use snprintf just to be extra-safe.
172 snprintf(suffix, sizeof(suffix), "/<!--frame%u-->-->", childCount());
176 return AtomicString(name);
179 Frame* FrameTree::child(unsigned index) const
181 Frame* result = firstChild();
182 for (unsigned i = 0; result && i != index; ++i)
183 result = result->tree()->nextSibling();
187 Frame* FrameTree::child(const AtomicString& name) const
189 for (Frame* child = firstChild(); child; child = child->tree()->nextSibling())
190 if (child->tree()->uniqueName() == name)
195 Frame* FrameTree::find(const AtomicString& name) const
197 if (name == "_self" || name == "_current" || name.isEmpty())
203 if (name == "_parent")
204 return parent() ? parent() : m_thisFrame;
206 // Since "_blank" should never be any frame's name, the following just amounts to an optimization.
207 if (name == "_blank")
210 // Search subtree starting with this frame first.
211 for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->traverseNext(m_thisFrame))
212 if (frame->tree()->uniqueName() == name)
215 // Search the entire tree for this page next.
216 Page* page = m_thisFrame->page();
218 // The frame could have been detached from the page, so check it.
222 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext())
223 if (frame->tree()->uniqueName() == name)
226 // Search the entire tree of each of the other pages in this namespace.
227 // FIXME: Is random order OK?
228 const HashSet<Page*>& pages = page->group().pages();
229 HashSet<Page*>::const_iterator end = pages.end();
230 for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) {
231 Page* otherPage = *it;
232 if (otherPage != page) {
233 for (Frame* frame = otherPage->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
234 if (frame->tree()->uniqueName() == name)
243 bool FrameTree::isDescendantOf(const Frame* ancestor) const
248 if (m_thisFrame->page() != ancestor->page())
251 for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->parent())
252 if (frame == ancestor)
257 Frame* FrameTree::traverseNext(const Frame* stayWithin) const
259 Frame* child = firstChild();
261 ASSERT(!stayWithin || child->tree()->isDescendantOf(stayWithin));
265 if (m_thisFrame == stayWithin)
268 Frame* sibling = nextSibling();
270 ASSERT(!stayWithin || sibling->tree()->isDescendantOf(stayWithin));
274 Frame* frame = m_thisFrame;
275 while (!sibling && (!stayWithin || frame->tree()->parent() != stayWithin)) {
276 frame = frame->tree()->parent();
279 sibling = frame->tree()->nextSibling();
283 ASSERT(!stayWithin || !sibling || sibling->tree()->isDescendantOf(stayWithin));
290 Frame* FrameTree::traverseNextWithWrap(bool wrap) const
292 if (Frame* result = traverseNext())
296 return m_thisFrame->page()->mainFrame();
301 Frame* FrameTree::traversePreviousWithWrap(bool wrap) const
303 // FIXME: besides the wrap feature, this is just the traversePreviousNode algorithm
305 if (Frame* prevSibling = previousSibling())
306 return prevSibling->tree()->deepLastChild();
307 if (Frame* parentFrame = parent())
310 // no siblings, no parent, self==top
312 return deepLastChild();
314 // top view is always the last one in this ordering, so prev is nil without wrap
318 Frame* FrameTree::deepLastChild() const
320 Frame* result = m_thisFrame;
321 for (Frame* last = lastChild(); last; last = last->tree()->lastChild())
327 Frame* FrameTree::top(bool checkForDisconnectedFrame) const
329 Frame* frame = m_thisFrame;
330 for (Frame* parent = m_thisFrame; parent; parent = parent->tree()->parent()) {
332 if (checkForDisconnectedFrame && frame->isDisconnected())
338 } // namespace WebCore
342 static void printIndent(int indent)
344 for (int i = 0; i < indent; ++i)
348 static void printFrames(const WebCore::Frame* frame, const WebCore::Frame* targetFrame, int indent)
350 if (frame == targetFrame) {
352 printIndent(indent - 1);
356 WebCore::FrameView* view = frame->view();
357 printf("Frame %p %dx%d\n", frame, view ? view->width() : 0, view ? view->height() : 0);
359 printf(" ownerElement=%p\n", frame->ownerElement());
361 printf(" frameView=%p\n", view);
363 printf(" document=%p\n", frame->document());
365 printf(" uri=%s\n\n", frame->document()->documentURI().utf8().data());
367 for (WebCore::Frame* child = frame->tree()->firstChild(); child; child = child->tree()->nextSibling())
368 printFrames(child, targetFrame, indent + 1);
371 void showFrameTree(const WebCore::Frame* frame)
374 printf("Null input frame\n");
378 printFrames(frame->tree()->top(), frame, 0);