|
1 // -*- c-basic-offset: 4 -*- |
|
2 /* |
|
3 * Copyright (C) 2006 Apple Computer, Inc. |
|
4 * |
|
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. |
|
9 * |
|
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. |
|
14 * |
|
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. |
|
19 */ |
|
20 |
|
21 #include "config.h" |
|
22 #include "Frame.h" |
|
23 #include "FrameTree.h" |
|
24 |
|
25 #include "Frame.h" |
|
26 #include "Page.h" |
|
27 #include <stdarg.h> |
|
28 #include <wtf/Platform.h> |
|
29 #include <wtf/Vector.h> |
|
30 |
|
31 using std::swap; |
|
32 |
|
33 namespace WebCore { |
|
34 |
|
35 // FIXME: This belongs in some header file where multiple clients can share it. |
|
36 #if COMPILER(MSVC) |
|
37 int snprintf(char* str, size_t size, const char* format, ...) |
|
38 { |
|
39 va_list args; |
|
40 va_start(args, format); |
|
41 int result = vsnprintf_s(str, size, _TRUNCATE, format, args); |
|
42 va_end(args); |
|
43 return result; |
|
44 } |
|
45 #endif |
|
46 |
|
47 FrameTree::~FrameTree() |
|
48 { |
|
49 for (Frame* child = firstChild(); child; child = child->tree()->nextSibling()) |
|
50 child->setView(0); |
|
51 } |
|
52 |
|
53 void FrameTree::setName(const AtomicString& name) |
|
54 { |
|
55 if (!parent()) { |
|
56 m_name = name; |
|
57 return; |
|
58 } |
|
59 m_name = AtomicString(); // Remove our old frame name so it's not considered in uniqueChildName. |
|
60 m_name = parent()->tree()->uniqueChildName(name); |
|
61 } |
|
62 |
|
63 void FrameTree::appendChild(PassRefPtr<Frame> child) |
|
64 { |
|
65 ASSERT(child->page() == m_thisFrame->page()); |
|
66 child->tree()->m_parent = m_thisFrame; |
|
67 |
|
68 Frame* oldLast = m_lastChild; |
|
69 m_lastChild = child.get(); |
|
70 |
|
71 if (oldLast) { |
|
72 child->tree()->m_previousSibling = oldLast; |
|
73 oldLast->tree()->m_nextSibling = child; |
|
74 } else |
|
75 m_firstChild = child; |
|
76 |
|
77 m_childCount++; |
|
78 |
|
79 ASSERT(!m_lastChild->tree()->m_nextSibling); |
|
80 } |
|
81 |
|
82 void FrameTree::removeChild(Frame* child) |
|
83 { |
|
84 child->tree()->m_parent = 0; |
|
85 child->setView(0); |
|
86 if (child->ownerElement()) |
|
87 child->page()->decrementFrameCount(); |
|
88 child->pageDestroyed(); |
|
89 |
|
90 // Slightly tricky way to prevent deleting the child until we are done with it, w/o |
|
91 // extra refs. These swaps leave the child in a circular list by itself. Clearing its |
|
92 // previous and next will then finally deref it. |
|
93 |
|
94 RefPtr<Frame>& newLocationForNext = m_firstChild == child ? m_firstChild : child->tree()->m_previousSibling->tree()->m_nextSibling; |
|
95 Frame*& newLocationForPrevious = m_lastChild == child ? m_lastChild : child->tree()->m_nextSibling->tree()->m_previousSibling; |
|
96 swap(newLocationForNext, child->tree()->m_nextSibling); |
|
97 // For some inexplicable reason, the following line does not compile without the explicit std:: namepsace |
|
98 std::swap(newLocationForPrevious, child->tree()->m_previousSibling); |
|
99 |
|
100 child->tree()->m_previousSibling = 0; |
|
101 child->tree()->m_nextSibling = 0; |
|
102 |
|
103 m_childCount--; |
|
104 } |
|
105 |
|
106 AtomicString FrameTree::uniqueChildName(const AtomicString& requestedName) const |
|
107 { |
|
108 if (!requestedName.isEmpty() && !child(requestedName) && requestedName != "_blank") |
|
109 return requestedName; |
|
110 |
|
111 // Create a repeatable name for a child about to be added to us. The name must be |
|
112 // unique within the frame tree. The string we generate includes a "path" of names |
|
113 // from the root frame down to us. For this path to be unique, each set of siblings must |
|
114 // contribute a unique name to the path, which can't collide with any HTML-assigned names. |
|
115 // We generate this path component by index in the child list along with an unlikely |
|
116 // frame name that can't be set in HTML because it collides with comment syntax. |
|
117 |
|
118 const char framePathPrefix[] = "<!--framePath "; |
|
119 const int framePathPrefixLength = 14; |
|
120 const int framePathSuffixLength = 3; |
|
121 |
|
122 // Find the nearest parent that has a frame with a path in it. |
|
123 Vector<Frame*, 16> chain; |
|
124 Frame* frame; |
|
125 for (frame = m_thisFrame; frame; frame = frame->tree()->parent()) { |
|
126 if (frame->tree()->name().startsWith(framePathPrefix)) |
|
127 break; |
|
128 chain.append(frame); |
|
129 } |
|
130 String name; |
|
131 name += framePathPrefix; |
|
132 if (frame) |
|
133 name += frame->tree()->name().domString().substring(framePathPrefixLength, |
|
134 frame->tree()->name().length() - framePathPrefixLength - framePathSuffixLength); |
|
135 for (int i = chain.size() - 1; i >= 0; --i) { |
|
136 frame = chain[i]; |
|
137 name += "/"; |
|
138 name += frame->tree()->name(); |
|
139 } |
|
140 |
|
141 // Suffix buffer has more than enough space for: |
|
142 // 10 characters before the number |
|
143 // a number (3 digits for the highest this gets in practice, 20 digits for the largest 64-bit integer) |
|
144 // 6 characters after the number |
|
145 // trailing null byte |
|
146 // But we still use snprintf just to be extra-safe. |
|
147 char suffix[40]; |
|
148 snprintf(suffix, sizeof(suffix), "/<!--frame%u-->-->", childCount()); |
|
149 |
|
150 name += suffix; |
|
151 |
|
152 return AtomicString(name); |
|
153 } |
|
154 |
|
155 Frame* FrameTree::child(unsigned index) const |
|
156 { |
|
157 Frame* result = firstChild(); |
|
158 for (unsigned i = 0; result && i != index; ++i) |
|
159 result = result->tree()->nextSibling(); |
|
160 return result; |
|
161 } |
|
162 |
|
163 Frame* FrameTree::child(const AtomicString& name) const |
|
164 { |
|
165 for (Frame* child = firstChild(); child; child = child->tree()->nextSibling()) |
|
166 if (child->tree()->name() == name) |
|
167 return child; |
|
168 return 0; |
|
169 } |
|
170 |
|
171 Frame* FrameTree::find(const AtomicString& name) const |
|
172 { |
|
173 if (name == "_self" || name == "_current" || name.isEmpty()) |
|
174 return m_thisFrame; |
|
175 |
|
176 if (name == "_top") |
|
177 return m_thisFrame->page()->mainFrame(); |
|
178 |
|
179 if (name == "_parent") |
|
180 return parent() ? parent() : m_thisFrame; |
|
181 |
|
182 // Since "_blank" should never be any frame's name, the following just amounts to an optimization. |
|
183 if (name == "_blank") |
|
184 return 0; |
|
185 |
|
186 // Search subtree starting with this frame first. |
|
187 for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->traverseNext(m_thisFrame)) |
|
188 if (frame->tree()->name() == name) |
|
189 return frame; |
|
190 |
|
191 // Search the entire tree for this page next. |
|
192 Page* page = m_thisFrame->page(); |
|
193 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) |
|
194 if (frame->tree()->name() == name) |
|
195 return frame; |
|
196 |
|
197 // Search the entire tree for all other pages in this namespace. |
|
198 const HashSet<Page*>* pages = page->frameNamespace(); |
|
199 if (pages) { |
|
200 HashSet<Page*>::const_iterator end = pages->end(); |
|
201 for (HashSet<Page*>::const_iterator it = pages->begin(); it != end; ++it) { |
|
202 Page* otherPage = *it; |
|
203 if (otherPage != page) |
|
204 for (Frame* frame = otherPage->mainFrame(); frame; frame = frame->tree()->traverseNext()) |
|
205 if (frame->tree()->name() == name) |
|
206 return frame; |
|
207 } |
|
208 } |
|
209 |
|
210 return 0; |
|
211 } |
|
212 |
|
213 bool FrameTree::isDescendantOf(const Frame* ancestor) const |
|
214 { |
|
215 |
|
216 #if PLATFORM(SYMBIAN) |
|
217 if ( !ancestor ) |
|
218 return false; |
|
219 #endif |
|
220 |
|
221 if (m_thisFrame->page() != ancestor->page()) |
|
222 return false; |
|
223 |
|
224 for (Frame* frame = m_thisFrame; frame; frame = frame->tree()->parent()) |
|
225 if (frame == ancestor) |
|
226 return true; |
|
227 return false; |
|
228 } |
|
229 |
|
230 Frame* FrameTree::traverseNext(const Frame* stayWithin) const |
|
231 { |
|
232 Frame* child = firstChild(); |
|
233 if (child) { |
|
234 ASSERT(!stayWithin || child->tree()->isDescendantOf(stayWithin)); |
|
235 return child; |
|
236 } |
|
237 |
|
238 if (m_thisFrame == stayWithin) |
|
239 return 0; |
|
240 |
|
241 Frame* sibling = nextSibling(); |
|
242 if (sibling) { |
|
243 ASSERT(!stayWithin || sibling->tree()->isDescendantOf(stayWithin)); |
|
244 return sibling; |
|
245 } |
|
246 |
|
247 Frame* frame = m_thisFrame; |
|
248 while (!sibling && (!stayWithin || frame->tree()->parent() != stayWithin)) { |
|
249 frame = frame->tree()->parent(); |
|
250 if (!frame) |
|
251 return 0; |
|
252 sibling = frame->tree()->nextSibling(); |
|
253 } |
|
254 |
|
255 if (frame) { |
|
256 ASSERT(!stayWithin || !sibling || sibling->tree()->isDescendantOf(stayWithin)); |
|
257 return sibling; |
|
258 } |
|
259 |
|
260 return 0; |
|
261 } |
|
262 |
|
263 Frame* FrameTree::traverseNextWithWrap(bool wrap) const |
|
264 { |
|
265 if (Frame* result = traverseNext()) |
|
266 return result; |
|
267 |
|
268 if (wrap) |
|
269 return m_thisFrame->page()->mainFrame(); |
|
270 |
|
271 return 0; |
|
272 } |
|
273 |
|
274 Frame* FrameTree::traversePreviousWithWrap(bool wrap) const |
|
275 { |
|
276 // FIXME: besides the wrap feature, this is just the traversePreviousNode algorithm |
|
277 |
|
278 if (Frame* prevSibling = previousSibling()) |
|
279 return prevSibling->tree()->deepLastChild(); |
|
280 if (Frame* parentFrame = parent()) |
|
281 return parentFrame; |
|
282 |
|
283 // no siblings, no parent, self==top |
|
284 if (wrap) |
|
285 return deepLastChild(); |
|
286 |
|
287 // top view is always the last one in this ordering, so prev is nil without wrap |
|
288 return 0; |
|
289 } |
|
290 |
|
291 Frame* FrameTree::deepLastChild() const |
|
292 { |
|
293 Frame* result = m_thisFrame; |
|
294 for (Frame* last = lastChild(); last; last = last->tree()->lastChild()) |
|
295 result = last; |
|
296 |
|
297 return result; |
|
298 } |
|
299 |
|
300 Frame* FrameTree::top() const |
|
301 { |
|
302 if (Page* page = m_thisFrame->page()) |
|
303 return page->mainFrame(); |
|
304 |
|
305 Frame* frame = m_thisFrame; |
|
306 while (Frame* parent = frame->tree()->parent()) |
|
307 frame = parent; |
|
308 return frame; |
|
309 } |
|
310 |
|
311 } // namespace WebCore |