Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLImportChild.cpp
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/HTMLImportChild.h"
33
34 #include "core/dom/Document.h"
35 #include "core/dom/custom/CustomElement.h"
36 #include "core/dom/custom/CustomElementMicrotaskImportStep.h"
37 #include "core/html/HTMLImportChildClient.h"
38 #include "core/html/HTMLImportLoader.h"
39
40 namespace WebCore {
41
42 HTMLImportChild::HTMLImportChild(const KURL& url, bool createdByParser)
43     : HTMLImport(createdByParser)
44     , m_url(url)
45     , m_customElementMicrotaskStep(0)
46     , m_client(0)
47 {
48 }
49
50 HTMLImportChild::~HTMLImportChild()
51 {
52     // importDestroyed() should be called before the destruction.
53     ASSERT(!m_loader);
54
55     if (m_customElementMicrotaskStep) {
56         // if Custom Elements were blocked, must unblock them before death
57         m_customElementMicrotaskStep->importDidFinish();
58         m_customElementMicrotaskStep = 0;
59     }
60
61     if (m_client)
62         m_client->importChildWasDestroyed(this);
63 }
64
65 void HTMLImportChild::wasAlreadyLoaded()
66 {
67     ASSERT(!m_loader);
68     ASSERT(m_client);
69     stateWillChange();
70 }
71
72 void HTMLImportChild::startLoading(const ResourcePtr<RawResource>& resource)
73 {
74     ASSERT(!this->resource());
75     ASSERT(!m_loader);
76
77     if (isCreatedByParser()) {
78         ASSERT(!m_customElementMicrotaskStep);
79         m_customElementMicrotaskStep = CustomElement::didCreateImport(this);
80     }
81
82     setResource(resource);
83
84     // If the node is "document blocked", it cannot create HTMLImportLoader
85     // even if there is no sharable one found, as there is possibility that
86     // preceding imports load the sharable imports.
87     // In that case preceding one should win because it comes first in the tree order.
88     // See also didUnblockFromCreatingDocument().
89     if (state().shouldBlockDocumentCreation())
90         return;
91
92     ensureLoader();
93 }
94
95 void HTMLImportChild::didFinish()
96 {
97     if (m_client)
98         m_client->didFinish();
99
100     if (m_customElementMicrotaskStep) {
101         m_customElementMicrotaskStep->importDidFinish();
102         m_customElementMicrotaskStep = 0;
103     }
104 }
105
106 void HTMLImportChild::didFinishLoading()
107 {
108     clearResource();
109     stateWillChange();
110 }
111
112 Document* HTMLImportChild::importedDocument() const
113 {
114     if (!m_loader)
115         return 0;
116     return m_loader->importedDocument();
117 }
118
119 void HTMLImportChild::importDestroyed()
120 {
121     if (parent())
122         parent()->removeChild(this);
123     if (m_loader) {
124         m_loader->removeClient(this);
125         m_loader.clear();
126     }
127 }
128
129 HTMLImportRoot* HTMLImportChild::root()
130 {
131     return parent() ? parent()->root() : 0;
132 }
133
134 Document* HTMLImportChild::document() const
135 {
136     return (m_loader && m_loader->isOwnedBy(this)) ? m_loader->document() : 0;
137 }
138
139 void HTMLImportChild::wasDetachedFromDocument()
140 {
141     // For imported documens this shouldn't be called because Document::m_import is
142     // cleared before Document is destroyed by HTMLImportChild::importDestroyed().
143     ASSERT_NOT_REACHED();
144 }
145
146 void HTMLImportChild::didFinishParsing()
147 {
148     ASSERT(m_loader->isOwnedBy(this));
149     m_loader->didFinishParsing();
150 }
151
152 void HTMLImportChild::stateDidChange()
153 {
154     HTMLImport::stateDidChange();
155
156     // Once all preceding imports are loaded,
157     // HTMLImportChild can decide whether it should load the import by itself
158     // or it can share existing one.
159     if (!state().shouldBlockDocumentCreation())
160         ensureLoader();
161     if (state().isReady())
162         didFinish();
163 }
164
165 void HTMLImportChild::ensureLoader()
166 {
167     if (m_loader)
168         return;
169
170     if (HTMLImportChild* found = root()->findLinkFor(m_url, this))
171         shareLoader(found);
172     else
173         createLoader();
174 }
175
176 void HTMLImportChild::createLoader()
177 {
178     ASSERT(!state().shouldBlockDocumentCreation());
179     ASSERT(!m_loader);
180     m_loader = HTMLImportLoader::create(this);
181     m_loader->addClient(this);
182     m_loader->startLoading(resource());
183 }
184
185 void HTMLImportChild::shareLoader(HTMLImportChild* loader)
186 {
187     ASSERT(!m_loader);
188     m_loader = loader->m_loader;
189     m_loader->addClient(this);
190     stateWillChange();
191 }
192
193 bool HTMLImportChild::isDone() const
194 {
195     return m_loader && m_loader->isDone();
196 }
197
198 bool HTMLImportChild::hasLoader() const
199 {
200     return m_loader;
201 }
202
203 bool HTMLImportChild::ownsLoader() const
204 {
205     return m_loader && m_loader->isOwnedBy(this);
206 }
207
208 bool HTMLImportChild::loaderHasError() const
209 {
210     return m_loader && m_loader->hasError();
211 }
212
213
214 void HTMLImportChild::setClient(HTMLImportChildClient* client)
215 {
216     ASSERT(client);
217     ASSERT(!m_client);
218     m_client = client;
219 }
220
221 void HTMLImportChild::clearClient()
222 {
223     // Doesn't check m_client nullity because we allow
224     // clearClient() to reenter.
225     m_client = 0;
226 }
227
228 HTMLLinkElement* HTMLImportChild::link() const
229 {
230     if (!m_client)
231         return 0;
232     return m_client->link();
233 }
234
235 #if !defined(NDEBUG)
236 void HTMLImportChild::showThis()
237 {
238     HTMLImport::showThis();
239     fprintf(stderr, " loader=%p own=%s async=%s url=%s",
240         m_loader.get(),
241         hasLoader() && ownsLoader() ? "Y" : "N",
242         isCreatedByParser() ? "Y" : "N",
243         url().string().utf8().data());
244 }
245 #endif
246
247 } // namespace WebCore