1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "core/html/imports/HTMLImport.h"
33
34 #include "core/dom/Document.h"
35 #include "core/html/imports/HTMLImportStateResolver.h"
36 #include "wtf/Vector.h"
37
38 namespace blink {
39
root()40 HTMLImport* HTMLImport::root()
41 {
42 HTMLImport* i = this;
43 while (i->parent())
44 i = i->parent();
45 return i;
46 }
47
precedes(HTMLImport * import)48 bool HTMLImport::precedes(HTMLImport* import)
49 {
50 for (HTMLImport* i = this; i; i = traverseNext(i)) {
51 if (i == import)
52 return true;
53 }
54
55 return false;
56 }
57
formsCycle() const58 bool HTMLImport::formsCycle() const
59 {
60 for (const HTMLImport* i = this->parent(); i; i = i->parent()) {
61 if (i->document() == this->document())
62 return true;
63 }
64
65 return false;
66
67 }
68
appendImport(HTMLImport * child)69 void HTMLImport::appendImport(HTMLImport* child)
70 {
71 appendChild(child);
72
73 // This prevents HTML parser from going beyond the
74 // blockage line before the precise state is computed by recalcState().
75 if (child->isSync())
76 m_state = HTMLImportState::blockedState();
77
78 stateWillChange();
79 }
80
stateDidChange()81 void HTMLImport::stateDidChange()
82 {
83 if (!state().shouldBlockScriptExecution()) {
84 if (Document* document = this->document())
85 document->didLoadAllImports();
86 }
87 }
88
recalcTreeState(HTMLImport * root)89 void HTMLImport::recalcTreeState(HTMLImport* root)
90 {
91 WillBeHeapHashMap<RawPtrWillBeMember<HTMLImport>, HTMLImportState> snapshot;
92 WillBeHeapVector<RawPtrWillBeMember<HTMLImport> > updated;
93
94 for (HTMLImport* i = root; i; i = traverseNext(i)) {
95 snapshot.add(i, i->state());
96 i->m_state = HTMLImportState::invalidState();
97 }
98
99 // The post-visit DFS order matters here because
100 // HTMLImportStateResolver in recalcState() Depends on
101 // |m_state| of its children and precedents of ancestors.
102 // Accidental cycle dependency of state computation is prevented
103 // by invalidateCachedState() and isStateCacheValid() check.
104 for (HTMLImport* i = traverseFirstPostOrder(root); i; i = traverseNextPostOrder(i)) {
105 ASSERT(!i->m_state.isValid());
106 i->m_state = HTMLImportStateResolver(i).resolve();
107
108 HTMLImportState newState = i->state();
109 HTMLImportState oldState = snapshot.get(i);
110 // Once the state reaches Ready, it shouldn't go back.
111 ASSERT(!oldState.isReady() || oldState <= newState);
112 if (newState != oldState)
113 updated.append(i);
114 }
115
116 for (size_t i = 0; i < updated.size(); ++i)
117 updated[i]->stateDidChange();
118 }
119
120 #if !defined(NDEBUG)
show()121 void HTMLImport::show()
122 {
123 root()->showTree(this, 0);
124 }
125
showTree(HTMLImport * highlight,unsigned depth)126 void HTMLImport::showTree(HTMLImport* highlight, unsigned depth)
127 {
128 for (unsigned i = 0; i < depth*4; ++i)
129 fprintf(stderr, " ");
130
131 fprintf(stderr, "%s", this == highlight ? "*" : " ");
132 showThis();
133 fprintf(stderr, "\n");
134 for (HTMLImport* child = firstChild(); child; child = child->next())
135 child->showTree(highlight, depth + 1);
136 }
137
showThis()138 void HTMLImport::showThis()
139 {
140 fprintf(stderr, "%p state=%d", this, m_state.peekValueForDebug());
141 }
142 #endif
143
144 } // namespace blink
145