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
|
/*
* Copyright (C) 2006, 2013 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 "FrameView.h"
#include "HTMLFrameOwnerElement.h"
#include "MainFrame.h"
#include "Page.h"
#include "PageGroup.h"
#include <stdarg.h>
#include <wtf/StringExtras.h>
#include <wtf/Vector.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
FrameTree::~FrameTree()
{
for (Frame* child = firstChild(); child; child = child->tree().nextSibling())
child->setView(nullptr);
}
void FrameTree::setName(const AtomicString& name)
{
m_name = name;
if (!parent()) {
m_uniqueName = name;
return;
}
m_uniqueName = AtomicString(); // Remove our old frame name so it's not considered in uniqueChildName.
m_uniqueName = parent()->tree().uniqueChildName(name);
}
void FrameTree::clearName()
{
m_name = AtomicString();
m_uniqueName = AtomicString();
}
Frame* FrameTree::parent() const
{
return m_parent;
}
bool FrameTree::transferChild(PassRefPtr<Frame> child)
{
Frame* oldParent = child->tree().parent();
if (oldParent == &m_thisFrame)
return false; // |child| is already a child of m_thisFrame.
if (oldParent)
oldParent->tree().removeChild(child.get());
ASSERT(child->page() == m_thisFrame.page());
child->tree().m_parent = &m_thisFrame;
// We need to ensure that the child still has a unique frame name with respect to its new parent.
child->tree().setName(child->tree().m_name);
actuallyAppendChild(child); // Note, on return |child| is null.
return true;
}
unsigned FrameTree::indexInParent() const
{
if (!m_parent)
return 0;
unsigned index = 0;
for (Frame* frame = m_parent->tree().firstChild(); frame; frame = frame->tree().nextSibling()) {
if (&frame->tree() == this)
return index;
++index;
}
RELEASE_ASSERT_NOT_REACHED();
}
void FrameTree::appendChild(PassRefPtr<Frame> child)
{
ASSERT(child->page() == m_thisFrame.page());
child->tree().m_parent = &m_thisFrame;
actuallyAppendChild(child); // Note, on return |child| is null.
}
void FrameTree::actuallyAppendChild(PassRefPtr<Frame> child)
{
ASSERT(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_scopedChildCount = invalidCount;
ASSERT(!m_lastChild->tree().m_nextSibling);
}
void FrameTree::removeChild(Frame* child)
{
child->tree().m_parent = nullptr;
// Slightly tricky way to prevent deleting the child until we are done with it, w/o
// extra refs. These swaps leave the child in a circular list by itself. Clearing its
// previous and next will then finally deref it.
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);
// For some inexplicable reason, the following line does not compile without the explicit std:: namespace
std::swap(newLocationForPrevious, child->tree().m_previousSibling);
child->tree().m_previousSibling = nullptr;
child->tree().m_nextSibling = nullptr;
m_scopedChildCount = invalidCount;
}
AtomicString FrameTree::uniqueChildName(const AtomicString& requestedName) const
{
// If the requested name (the frame's "name" attribute) is unique, just use that.
if (!requestedName.isEmpty() && !child(requestedName) && requestedName != "_blank")
return requestedName;
// The "name" attribute was not unique or absent. Generate a name based on the
// new frame's location in the frame tree. The name uses HTML comment syntax to
// avoid collisions with author names.
// An example path for the third child of the second child of the root frame:
// <!--framePath //<!--frame1-->/<!--frame2-->-->
const char framePathPrefix[] = "<!--framePath ";
const int framePathPrefixLength = 14;
const int framePathSuffixLength = 3;
// Find the nearest parent that has a frame with a path in it.
Vector<Frame*, 16> chain;
Frame* frame;
for (frame = &m_thisFrame; frame; frame = frame->tree().parent()) {
if (frame->tree().uniqueName().startsWith(framePathPrefix))
break;
chain.append(frame);
}
StringBuilder name;
name.append(framePathPrefix);
if (frame) {
name.append(frame->tree().uniqueName().string().substring(framePathPrefixLength,
frame->tree().uniqueName().length() - framePathPrefixLength - framePathSuffixLength));
}
for (int i = chain.size() - 1; i >= 0; --i) {
frame = chain[i];
name.append('/');
if (frame->tree().parent()) {
name.appendLiteral("<!--frame");
name.appendNumber(frame->tree().indexInParent());
name.appendLiteral("-->");
}
}
name.appendLiteral("/<!--frame");
name.appendNumber(childCount());
name.appendLiteral("-->-->");
return name.toAtomicString();
}
static bool inScope(Frame& frame, TreeScope& scope)
{
Document* document = frame.document();
if (!document)
return false;
HTMLFrameOwnerElement* owner = document->ownerElement();
if (!owner)
return false;
return &owner->treeScope() == &scope;
}
inline Frame* FrameTree::scopedChild(unsigned index, TreeScope* scope) const
{
if (!scope)
return nullptr;
unsigned scopedIndex = 0;
for (Frame* result = firstChild(); result; result = result->tree().nextSibling()) {
if (inScope(*result, *scope)) {
if (scopedIndex == index)
return result;
scopedIndex++;
}
}
return nullptr;
}
inline Frame* FrameTree::scopedChild(const AtomicString& name, TreeScope* scope) const
{
if (!scope)
return nullptr;
for (Frame* child = firstChild(); child; child = child->tree().nextSibling()) {
if (child->tree().uniqueName() == name && inScope(*child, *scope))
return child;
}
return nullptr;
}
inline unsigned FrameTree::scopedChildCount(TreeScope* scope) const
{
if (!scope)
return 0;
unsigned scopedCount = 0;
for (Frame* result = firstChild(); result; result = result->tree().nextSibling()) {
if (inScope(*result, *scope))
scopedCount++;
}
return scopedCount;
}
Frame* FrameTree::scopedChild(unsigned index) const
{
return scopedChild(index, m_thisFrame.document());
}
Frame* FrameTree::scopedChild(const AtomicString& name) const
{
return scopedChild(name, m_thisFrame.document());
}
unsigned FrameTree::scopedChildCount() const
{
if (m_scopedChildCount == invalidCount)
m_scopedChildCount = scopedChildCount(m_thisFrame.document());
return m_scopedChildCount;
}
unsigned FrameTree::childCount() const
{
unsigned count = 0;
for (Frame* result = firstChild(); result; result = result->tree().nextSibling())
++count;
return count;
}
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().uniqueName() == name)
return child;
return nullptr;
}
Frame* FrameTree::find(const AtomicString& name) const
{
if (name == "_self" || name == "_current" || name.isEmpty())
return &m_thisFrame;
if (name == "_top")
return &top();
if (name == "_parent")
return parent() ? parent() : &m_thisFrame;
// Since "_blank" should never be any frame's name, the following is only an optimization.
if (name == "_blank")
return nullptr;
// Search subtree starting with this frame first.
for (Frame* frame = &m_thisFrame; frame; frame = frame->tree().traverseNext(&m_thisFrame)) {
if (frame->tree().uniqueName() == name)
return frame;
}
// Then the rest of the tree.
for (Frame* frame = &m_thisFrame.mainFrame(); frame; frame = frame->tree().traverseNext()) {
if (frame->tree().uniqueName() == name)
return frame;
}
// Search the entire tree of each of the other pages in this namespace.
// FIXME: Is random order OK?
Page* page = m_thisFrame.page();
if (!page)
return nullptr;
for (auto* otherPage : page->group().pages()) {
if (otherPage == page)
continue;
for (Frame* frame = &otherPage->mainFrame(); frame; frame = frame->tree().traverseNext()) {
if (frame->tree().uniqueName() == name)
return frame;
}
}
return nullptr;
}
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 nullptr;
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 nullptr;
sibling = frame->tree().nextSibling();
}
if (frame) {
ASSERT(!stayWithin || !sibling || sibling->tree().isDescendantOf(stayWithin));
return sibling;
}
return nullptr;
}
Frame* FrameTree::firstRenderedChild() const
{
Frame* child = firstChild();
if (!child)
return nullptr;
if (child->ownerRenderer())
return child;
while ((child = child->tree().nextSibling())) {
if (child->ownerRenderer())
return child;
}
return nullptr;
}
Frame* FrameTree::nextRenderedSibling() const
{
Frame* sibling = &m_thisFrame;
while ((sibling = sibling->tree().nextSibling())) {
if (sibling->ownerRenderer())
return sibling;
}
return nullptr;
}
Frame* FrameTree::traverseNextRendered(const Frame* stayWithin) const
{
Frame* child = firstRenderedChild();
if (child) {
ASSERT(!stayWithin || child->tree().isDescendantOf(stayWithin));
return child;
}
if (&m_thisFrame == stayWithin)
return nullptr;
Frame* sibling = nextRenderedSibling();
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 nullptr;
sibling = frame->tree().nextRenderedSibling();
}
if (frame) {
ASSERT(!stayWithin || !sibling || sibling->tree().isDescendantOf(stayWithin));
return sibling;
}
return nullptr;
}
Frame* FrameTree::traverseNextWithWrap(bool wrap) const
{
if (Frame* result = traverseNext())
return result;
if (wrap)
return &m_thisFrame.mainFrame();
return nullptr;
}
Frame* FrameTree::traversePreviousWithWrap(bool wrap) const
{
// FIXME: besides the wrap feature, this is just the traversePreviousNode algorithm
if (Frame* prevSibling = previousSibling())
return prevSibling->tree().deepLastChild();
if (Frame* parentFrame = parent())
return parentFrame;
// no siblings, no parent, self==top
if (wrap)
return deepLastChild();
// top view is always the last one in this ordering, so prev is nil without wrap
return nullptr;
}
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
{
Frame* frame = &m_thisFrame;
for (Frame* parent = &m_thisFrame; parent; parent = parent->tree().parent())
frame = parent;
return *frame;
}
} // 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);
WebCore::FrameView* view = frame.view();
printf("Frame %p %dx%d\n", &frame, view ? view->width() : 0, view ? view->height() : 0);
printIndent(indent);
printf(" ownerElement=%p\n", frame.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", frame.ownerRenderer());
printIndent(indent);
printf(" document=%p (needs style recalc %d)\n", frame.document(), frame.document() ? frame.document()->childNeedsStyleRecalc() : false);
printIndent(indent);
printf(" uri=%s\n", frame.document()->documentURI().utf8().data());
for (WebCore::Frame* 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().top(), frame, 0);
}
#endif
|