1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
|
/*
* Copyright (C) 2006-2017 Apple Inc. All rights reserved.
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "FrameTree.h"
#include "Document.h"
#include "FrameLoader.h"
#include "HTMLFrameOwnerElement.h"
#include "LocalFrame.h"
#include "LocalFrameView.h"
#include "Page.h"
#include "PageGroup.h"
#include <stdarg.h>
#include <wtf/Vector.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
FrameTree::FrameTree(Frame& thisFrame, Frame* parentFrame)
: m_thisFrame(thisFrame)
, m_parent(parentFrame)
{
}
FrameTree::~FrameTree()
{
for (auto* child = firstChild(); child; child = child->tree().nextSibling())
child->disconnectView();
}
void FrameTree::setSpecifiedName(const AtomString& specifiedName)
{
m_specifiedName = specifiedName;
}
void FrameTree::clearName()
{
m_specifiedName = nullAtom();
}
Frame* FrameTree::parent() const
{
return m_parent.get();
}
void FrameTree::appendChild(Frame& child)
{
ASSERT(child.page() == m_thisFrame->page());
child.tree().m_parent = m_thisFrame.ptr();
WeakPtr<Frame> oldLast = m_lastChild;
m_lastChild = child;
if (oldLast) {
child.tree().m_previousSibling = oldLast;
oldLast->tree().m_nextSibling = &child;
} else
m_firstChild = &child;
m_scopedChildCount = invalidCount;
ASSERT(!m_lastChild->tree().m_nextSibling);
}
void FrameTree::removeChild(Frame& child)
{
WeakPtr<Frame>& newLocationForPrevious = m_lastChild == &child ? m_lastChild : child.tree().m_nextSibling->tree().m_previousSibling;
RefPtr<Frame>& newLocationForNext = m_firstChild == &child ? m_firstChild : child.tree().m_previousSibling->tree().m_nextSibling;
child.tree().m_parent = nullptr;
newLocationForPrevious = std::exchange(child.tree().m_previousSibling, nullptr);
newLocationForNext = WTFMove(child.tree().m_nextSibling);
m_scopedChildCount = invalidCount;
}
Ref<Frame> FrameTree::protectedThisFrame() const
{
return m_thisFrame.get();
}
static bool inScope(Frame& frame, TreeScope& scope)
{
RefPtr localFrame = dynamicDowncast<LocalFrame>(frame);
if (!localFrame)
return true;
RefPtr document = localFrame->document();
if (!document)
return false;
RefPtr owner = document->ownerElement();
return owner && &owner->treeScope() == &scope;
}
Frame* FrameTree::scopedChild(unsigned index, TreeScope* scope) const
{
if (!scope)
return nullptr;
unsigned scopedIndex = 0;
for (auto* result = firstChild(); result; result = result->tree().nextSibling()) {
if (inScope(*result, *scope)) {
if (scopedIndex == index)
return result;
scopedIndex++;
}
}
return nullptr;
}
inline Frame* FrameTree::scopedChild(NOESCAPE const Function<bool(const FrameTree&)>& isMatch, TreeScope* scope) const
{
if (!scope)
return nullptr;
for (auto* child = firstChild(); child; child = child->tree().nextSibling()) {
if (isMatch(child->tree()) && inScope(*child, *scope))
return child;
}
return nullptr;
}
inline unsigned FrameTree::scopedChildCount(TreeScope* scope) const
{
if (!scope)
return 0;
unsigned scopedCount = 0;
for (auto* result = firstChild(); result; result = result->tree().nextSibling()) {
if (inScope(*result, *scope))
scopedCount++;
}
return scopedCount;
}
Frame* FrameTree::scopedChild(unsigned index) const
{
auto* localFrame = dynamicDowncast<LocalFrame>(m_thisFrame.get());
if (!localFrame)
return nullptr;
return scopedChild(index, localFrame->protectedDocument().get());
}
Frame* FrameTree::scopedChildByUniqueName(const AtomString& uniqueName) const
{
auto* localFrame = dynamicDowncast<LocalFrame>(m_thisFrame.get());
if (!localFrame)
return nullptr;
return scopedChild([&](auto& frameTree) {
return frameTree.uniqueName() == uniqueName;
}, localFrame->protectedDocument().get());
}
Frame* FrameTree::scopedChildBySpecifiedName(const AtomString& specifiedName) const
{
auto* localFrame = dynamicDowncast<LocalFrame>(m_thisFrame.get());
if (!localFrame)
return nullptr;
return scopedChild([&](auto& frameTree) {
return frameTree.specifiedName() == specifiedName;
}, localFrame->protectedDocument().get());
}
unsigned FrameTree::scopedChildCount() const
{
if (m_scopedChildCount == invalidCount) {
if (auto* localFrame = dynamicDowncast<LocalFrame>(m_thisFrame.get()))
m_scopedChildCount = scopedChildCount(localFrame->document());
}
return m_scopedChildCount;
}
unsigned FrameTree::childCount() const
{
unsigned count = 0;
for (auto* child = firstChild(); child; child = child->tree().nextSibling())
++count;
return count;
}
unsigned FrameTree::descendantCount() const
{
unsigned count = 0;
for (auto* child = firstChild(); child; child = child->tree().nextSibling())
count += 1 + child->tree().descendantCount();
return count;
}
Frame* FrameTree::child(unsigned index) const
{
auto* result = firstChild();
for (unsigned i = 0; result && i != index; ++i)
result = result->tree().nextSibling();
return result;
}
Frame* FrameTree::descendantByFrameID(FrameIdentifier frameID) const
{
for (auto* child = firstChild(); child; child = child->tree().traverseNext()) {
if (child->frameID() == frameID)
return child;
}
return nullptr;
}
Frame* FrameTree::childBySpecifiedName(const AtomString& name) const
{
for (auto* child = firstChild(); child; child = child->tree().nextSibling()) {
if (child->tree().specifiedName() == name)
return child;
}
return nullptr;
}
// FrameTree::find() only returns frames in pages that are related to the active
// page by an opener <-> openee relationship.
static bool isFrameFamiliarWith(Frame& frameA, Frame& frameB)
{
if (frameA.page() == frameB.page())
return true;
auto* frameAOpener = frameA.mainFrame().opener();
auto* frameBOpener = frameB.mainFrame().opener();
return (frameAOpener && frameAOpener->page() == frameB.page())
|| (frameBOpener && frameBOpener->page() == frameA.page())
|| (frameAOpener && frameBOpener && frameAOpener->page() == frameBOpener->page());
}
template<typename F>
inline Frame* FrameTree::find(const AtomString& name, F&& nameGetter, Frame& activeFrame) const
{
if (isSelfTargetFrameName(name))
return m_thisFrame.ptr();
if (isTopTargetFrameName(name))
return &top();
if (isParentTargetFrameName(name))
return parent() ? parent() : m_thisFrame.ptr();
// Since "_blank" cannot be a frame's name, this check is an optimization, not for correctness.
if (isBlankTargetFrameName(name))
return nullptr;
// Search subtree starting with this frame first.
Ref thisFrame = m_thisFrame.get();
for (auto* frame = thisFrame.ptr(); frame; frame = frame->tree().traverseNext(thisFrame.ptr())) {
if (nameGetter(frame->tree()) == name)
return frame;
}
// Then the rest of the tree.
for (Frame* frame = &thisFrame->mainFrame(); frame; frame = frame->tree().traverseNext()) {
if (nameGetter(frame->tree()) == name)
return frame;
}
// Search the entire tree of each of the other pages in this namespace.
RefPtr page = thisFrame->page();
if (!page)
return nullptr;
// FIXME: These pages are searched in random order; that doesn't seem good. Maybe use order of creation?
for (auto& otherPage : page->group().pages()) {
if (&otherPage == page || otherPage.isClosing())
continue;
for (Frame* frame = &otherPage.mainFrame(); frame; frame = frame->tree().traverseNext()) {
if (nameGetter(frame->tree()) == name && isFrameFamiliarWith(activeFrame, *frame))
return frame;
}
}
return nullptr;
}
Frame* FrameTree::findByUniqueName(const AtomString& uniqueName, Frame& activeFrame) const
{
return find(uniqueName, [&](auto& frameTree) -> AtomString {
return frameTree.uniqueName();
}, activeFrame);
}
Frame* FrameTree::findBySpecifiedName(const AtomString& specifiedName, Frame& activeFrame) const
{
return find(specifiedName, [&](auto& frameTree) -> const AtomString& {
return frameTree.specifiedName();
}, activeFrame);
}
bool FrameTree::isDescendantOf(const Frame* ancestor) const
{
if (!ancestor)
return false;
if (m_thisFrame->page() != ancestor->page())
return false;
for (Frame* frame = m_thisFrame.ptr(); frame; frame = frame->tree().parent()) {
if (frame == ancestor)
return true;
}
return false;
}
Frame* FrameTree::traverseNext(const Frame* stayWithin) const
{
auto* child = firstChild();
if (child) {
ASSERT(!stayWithin || child->tree().isDescendantOf(stayWithin));
return child;
}
if (m_thisFrame.ptr() == stayWithin)
return nullptr;
auto* sibling = nextSibling();
if (sibling) {
ASSERT(!stayWithin || sibling->tree().isDescendantOf(stayWithin));
return sibling;
}
auto* frame = m_thisFrame.ptr();
while (!sibling && (!stayWithin || frame->tree().parent() != stayWithin)) {
frame = frame->tree().parent();
if (!frame)
return nullptr;
sibling = frame->tree().nextSibling();
}
if (frame) {
ASSERT(!stayWithin || !sibling || sibling->tree().isDescendantOf(stayWithin));
return sibling;
}
return nullptr;
}
Frame* FrameTree::traverseNextSkippingChildren(const Frame* stayWithin) const
{
if (m_thisFrame.ptr() == stayWithin)
return nullptr;
if (auto* sibling = nextSibling())
return sibling;
return nextAncestorSibling(stayWithin);
}
Frame* FrameTree::nextAncestorSibling(const Frame* stayWithin) const
{
ASSERT(!nextSibling());
ASSERT(m_thisFrame.ptr() != stayWithin);
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->tree().parent()) {
if (ancestor == stayWithin)
return nullptr;
if (auto ancestorSibling = ancestor->tree().nextSibling())
return ancestorSibling;
}
return nullptr;
}
Frame* FrameTree::firstRenderedChild() const
{
auto* child = firstChild();
if (!child)
return nullptr;
if (auto* localChild = dynamicDowncast<LocalFrame>(child); localChild && localChild->ownerRenderer())
return child;
while ((child = child->tree().nextSibling())) {
if (auto* localChild = dynamicDowncast<LocalFrame>(child); localChild && localChild->ownerRenderer())
return child;
}
return nullptr;
}
Frame* FrameTree::nextRenderedSibling() const
{
auto* sibling = m_thisFrame.ptr();
while ((sibling = sibling->tree().nextSibling())) {
if (auto* localSibling = dynamicDowncast<LocalFrame>(sibling); localSibling && localSibling->ownerRenderer())
return sibling;
}
return nullptr;
}
LocalFrame* FrameTree::firstLocalDescendant() const
{
for (auto* child = firstChild(); child; child = child->tree().traverseNext()) {
if (auto* localChild = dynamicDowncast<LocalFrame>(child))
return localChild;
}
return nullptr;
}
LocalFrame* FrameTree::nextLocalSibling() const
{
for (auto* sibling = nextSibling(); sibling; sibling = sibling->tree().nextSibling()) {
if (auto* localSibling = dynamicDowncast<LocalFrame>(sibling))
return localSibling;
}
return nullptr;
}
Frame* FrameTree::traverseNextRendered(const Frame* stayWithin) const
{
auto* child = firstRenderedChild();
if (child) {
ASSERT(!stayWithin || child->tree().isDescendantOf(stayWithin));
return child;
}
if (m_thisFrame.ptr() == stayWithin)
return nullptr;
auto* sibling = nextRenderedSibling();
if (sibling) {
ASSERT(!stayWithin || sibling->tree().isDescendantOf(stayWithin));
return sibling;
}
auto* frame = m_thisFrame.ptr();
while (!sibling && (!stayWithin || frame->tree().parent() != stayWithin)) {
frame = frame->tree().parent();
if (!frame)
return nullptr;
sibling = frame->tree().nextRenderedSibling();
}
if (frame) {
ASSERT(!stayWithin || !sibling || sibling->tree().isDescendantOf(stayWithin));
return sibling;
}
return nullptr;
}
Frame* FrameTree::traverseNext(CanWrap canWrap, DidWrap* didWrap) const
{
if (auto* result = traverseNext())
return result;
if (canWrap == CanWrap::Yes) {
if (didWrap)
*didWrap = DidWrap::Yes;
return &m_thisFrame->mainFrame();
}
return nullptr;
}
Frame* FrameTree::traversePrevious(CanWrap canWrap, DidWrap* didWrap) const
{
// FIXME: besides the wrap feature, this is just the traversePreviousNode algorithm
if (auto* prevSibling = previousSibling())
return prevSibling->tree().deepLastChild();
if (auto* parentFrame = parent())
return parentFrame;
// no siblings, no parent, self==top
if (canWrap == CanWrap::Yes) {
if (didWrap)
*didWrap = DidWrap::Yes;
return deepLastChild();
}
// top view is always the last one in this ordering, so prev is nil without wrap
return nullptr;
}
Frame* FrameTree::traverseNextInPostOrder(CanWrap canWrap) const
{
if (m_nextSibling)
return m_nextSibling->tree().deepFirstChild();
if (m_parent)
return m_parent.get();
if (canWrap == CanWrap::Yes)
return deepFirstChild();
return nullptr;
}
Frame* FrameTree::deepFirstChild() const
{
auto* result = m_thisFrame.ptr();
while (auto* next = result->tree().firstChild())
result = next;
return result;
}
Frame* FrameTree::deepLastChild() const
{
auto* result = m_thisFrame.ptr();
for (auto* last = lastChild(); last; last = last->tree().lastChild())
result = last;
return result;
}
Frame& FrameTree::top() const
{
ASSERT(m_thisFrame->isMainFrame() || m_thisFrame->tree().parent());
return m_thisFrame->mainFrame();
}
Ref<Frame> FrameTree::protectedTop() const
{
return top();
}
unsigned FrameTree::depth() const
{
unsigned depth = 0;
for (auto* parent = m_thisFrame.ptr(); parent; parent = parent->tree().parent())
depth++;
return depth;
}
AtomString FrameTree::uniqueName() const
{
if (!parent())
return m_specifiedName;
auto frameIndex { 0u };
for (RefPtr frame = top().tree().firstChild(); frame; frame = frame->tree().traverseNext()) {
bool frameMatch = frame->frameID() == m_thisFrame->frameID();
auto frameName = frame->tree().specifiedName();
if (frameName.isEmpty() || isBlankTargetFrameName(frameName) || frame->tree().childBySpecifiedName(frameName)) {
frameIndex++;
if (frameMatch)
return makeAtomString("<!--frame"_s, frameIndex, "-->"_s);
}
if (frameMatch)
return frameName;
}
return nullAtom();
}
ASCIILiteral blankTargetFrameName()
{
return "_blank"_s;
}
// FIXME: Is it important to have this? Can't we just use the empty string everywhere this is used, instead?
ASCIILiteral selfTargetFrameName()
{
return "_self"_s;
}
bool isBlankTargetFrameName(StringView name)
{
return equalIgnoringASCIICase(name, "_blank"_s);
}
bool isParentTargetFrameName(StringView name)
{
return equalIgnoringASCIICase(name, "_parent"_s);
}
bool isSelfTargetFrameName(StringView name)
{
// FIXME: Some day we should remove _current, which is not part of the HTML specification.
return name.isEmpty() || equalIgnoringASCIICase(name, "_self"_s) || name == "_current"_s;
}
bool isTopTargetFrameName(StringView name)
{
return equalIgnoringASCIICase(name, "_top"_s);
}
} // namespace WebCore
#ifndef NDEBUG
static void printIndent(int indent)
{
for (int i = 0; i < indent; ++i)
printf(" ");
}
static void printFrames(const WebCore::Frame& frame, const WebCore::Frame* targetFrame, int indent)
{
if (&frame == targetFrame) {
printf("--> ");
printIndent(indent - 1);
} else
printIndent(indent);
auto* localFrame = dynamicDowncast<WebCore::LocalFrame>(frame);
if (!localFrame)
printf("RemoteFrame %p\n", &frame);
else {
auto* view = localFrame->view();
printf("Frame %p %dx%d\n", &frame, view ? view->width() : 0, view ? view->height() : 0);
printIndent(indent);
printf(" ownerElement=%p\n", localFrame->ownerElement());
printIndent(indent);
printf(" frameView=%p (needs layout %d)\n", view, view ? view->needsLayout() : false);
printIndent(indent);
printf(" renderView=%p\n", view ? view->renderView() : nullptr);
printIndent(indent);
printf(" ownerRenderer=%p\n", localFrame->ownerRenderer());
printIndent(indent);
printf(" document=%p (needs style recalc %d)\n", localFrame->document(), localFrame->document() ? localFrame->document()->childNeedsStyleRecalc() : false);
printIndent(indent);
SAFE_PRINTF(" uri=%s\n", localFrame->document()->documentURI().utf8());
}
for (auto* child = frame.tree().firstChild(); child; child = child->tree().nextSibling())
printFrames(*child, targetFrame, indent + 1);
}
void showFrameTree(const WebCore::Frame* frame)
{
if (!frame) {
printf("Null input frame\n");
return;
}
printFrames(frame->tree().protectedTop(), frame, 0);
}
#endif
|