#include "config.h"
#include "FrameTree.h"
#include "Frame.h"
#include "Page.h"
#include <stdarg.h>
#include <wtf/Platform.h>
#include <wtf/StringExtras.h>
#include <wtf/Vector.h>
using std::swap;
namespace WebCore {
FrameTree::~FrameTree()
{
for (Frame* child = firstChild(); child; child = child->tree()->nextSibling())
child->setView(0);
}
void FrameTree::setName(const AtomicString& name)
{
if (!parent()) {
m_name = name;
return;
}
m_name = AtomicString(); m_name = parent()->tree()->uniqueChildName(name);
}
void FrameTree::appendChild(PassRefPtr<Frame> child)
{
ASSERT(child->page() == m_thisFrame->page());
child->tree()->m_parent = m_thisFrame;
Frame* oldLast = m_lastChild;
m_lastChild = child.get();
if (oldLast) {
child->tree()->m_previousSibling = oldLast;
oldLast->tree()->m_nextSibling = child;
} else
m_firstChild = child;
m_childCount++;
ASSERT(!m_lastChild->tree()->m_nextSibling);
}
void FrameTree::removeChild(Frame* child)
{
child->tree()->m_parent = 0;
child->setView(0);
if (child->ownerElement())
child->page()->decrementFrameCount();
child->pageDestroyed();
RefPtr<Frame>& newLocationForNext = m_firstChild == child ? m_firstChild : child->tree()->m_previousSibling->tree()->m_nextSibling;
Frame*& newLocationForPrevious = m_lastChild == child ? m_lastChild : child->tree()->m_nextSibling->tree()->m_previousSibling;
swap(newLocationForNext, child->tree()->m_nextSibling);
std::swap(newLocationForPrevious, child->tree()->m_previousSibling);
child->tree()->m_previousSibling = 0;
child->tree()->m_nextSibling = 0;
m_childCount--;
}
AtomicString FrameTree::uniqueChildName(const AtomicString& requestedName) const
{
if (!requestedName.isEmpty() && !child(requestedName) && requestedName != "_blank")
return requestedName;
const char framePathPrefix[] = "<!--framePath ";
const int framePathPrefixLength = 14;
const int framePathSuffixLength = 3;
Vector<Frame*, 16> chain;
Frame* frame;
for (frame = m_thisFrame; frame; frame = frame->tree()->parent()) {
if (frame->tree()->name().startsWith(framePathPrefix))
break;
chain.append(frame);
}
String name;
name += framePathPrefix;
if (frame)
name += frame->tree()->name().domString().substring(framePathPrefixLength,
frame->tree()->name().length() - framePathPrefixLength - framePathSuffixLength);
for (int i = chain.size() - 1; i >= 0; --i) {
frame = chain[i];
name += "/";
name += frame->tree()->name();
}
char suffix[40];
snprintf(suffix, sizeof(suffix), "/<!--frame%u-->-->", childCount());
name += suffix;
return AtomicString(name);
}
Frame* FrameTree::child(unsigned index) const
{
Frame* result = firstChild();
for (unsigned i = 0; result && i != index; ++i)
result = result->tree()->nextSibling();
return result;
}
Frame* FrameTree::child(const AtomicString& name) const
{
for (Frame* child = firstChild(); child; child = child->tree()->nextSibling())
if (child->tree()->name() == name)
return child;
return 0;
}
Frame* FrameTree::find(const AtomicString& name) const
{
if (name == "_self" || name == "_current" || name.isEmpty())
return m_thisFrame;
if (name == "_top")
return m_thisFrame->page()->mainFrame();
if (name == "_parent")
return parent() ? parent() : m_thisFrame;
if (name == "_blank")
return 0;
for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->traverseNext(m_thisFrame))
if (frame->tree()->name() == name)
return frame;
Page* page = m_thisFrame->page();
for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext())
if (frame->tree()->name() == name)
return frame;
const HashSet<Page*>* pages = page->frameNamespace();
if (pages) {
HashSet<Page*>::const_iterator end = pages->end();
for (HashSet<Page*>::const_iterator it = pages->begin(); it != end; ++it) {
Page* otherPage = *it;
if (otherPage != page)
for (Frame* frame = otherPage->mainFrame(); frame; frame = frame->tree()->traverseNext())
if (frame->tree()->name() == name)
return frame;
}
}
return 0;
}
bool FrameTree::isDescendantOf(const Frame* ancestor) const
{
if (!ancestor)
return false;
if (m_thisFrame->page() != ancestor->page())
return false;
for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->parent())
if (frame == ancestor)
return true;
return false;
}
Frame* FrameTree::traverseNext(const Frame* stayWithin) const
{
Frame* child = firstChild();
if (child) {
ASSERT(!stayWithin || child->tree()->isDescendantOf(stayWithin));
return child;
}
if (m_thisFrame == stayWithin)
return 0;
Frame* sibling = nextSibling();
if (sibling) {
ASSERT(!stayWithin || sibling->tree()->isDescendantOf(stayWithin));
return sibling;
}
Frame* frame = m_thisFrame;
while (!sibling && (!stayWithin || frame->tree()->parent() != stayWithin)) {
frame = frame->tree()->parent();
if (!frame)
return 0;
sibling = frame->tree()->nextSibling();
}
if (frame) {
ASSERT(!stayWithin || !sibling || sibling->tree()->isDescendantOf(stayWithin));
return sibling;
}
return 0;
}
Frame* FrameTree::traverseNextWithWrap(bool wrap) const
{
if (Frame* result = traverseNext())
return result;
if (wrap)
return m_thisFrame->page()->mainFrame();
return 0;
}
Frame* FrameTree::traversePreviousWithWrap(bool wrap) const
{
if (Frame* prevSibling = previousSibling())
return prevSibling->tree()->deepLastChild();
if (Frame* parentFrame = parent())
return parentFrame;
if (wrap)
return deepLastChild();
return 0;
}
Frame* FrameTree::deepLastChild() const
{
Frame* result = m_thisFrame;
for (Frame* last = lastChild(); last; last = last->tree()->lastChild())
result = last;
return result;
}
Frame* FrameTree::top() const
{
if (Page* page = m_thisFrame->page())
return page->mainFrame();
Frame* frame = m_thisFrame;
while (Frame* parent = frame->tree()->parent())
frame = parent;
return frame;
}
}