2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/html/HTMLImportChild.h"
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"
42 HTMLImportChild::HTMLImportChild(const KURL& url, bool createdByParser)
43 : HTMLImport(createdByParser)
45 , m_customElementMicrotaskStep(0)
50 HTMLImportChild::~HTMLImportChild()
52 // importDestroyed() should be called before the destruction.
55 if (m_customElementMicrotaskStep) {
56 // if Custom Elements were blocked, must unblock them before death
57 m_customElementMicrotaskStep->importDidFinish();
58 m_customElementMicrotaskStep = 0;
62 m_client->importChildWasDestroyed(this);
65 void HTMLImportChild::wasAlreadyLoaded()
72 void HTMLImportChild::startLoading(const ResourcePtr<RawResource>& resource)
74 ASSERT(!this->resource());
77 if (isCreatedByParser()) {
78 ASSERT(!m_customElementMicrotaskStep);
79 m_customElementMicrotaskStep = CustomElement::didCreateImport(this);
82 setResource(resource);
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())
95 void HTMLImportChild::didFinish()
98 m_client->didFinish();
100 if (m_customElementMicrotaskStep) {
101 m_customElementMicrotaskStep->importDidFinish();
102 m_customElementMicrotaskStep = 0;
106 void HTMLImportChild::didFinishLoading()
112 Document* HTMLImportChild::importedDocument() const
116 return m_loader->importedDocument();
119 void HTMLImportChild::importDestroyed()
122 parent()->removeChild(this);
124 m_loader->removeClient(this);
129 HTMLImportRoot* HTMLImportChild::root()
131 return parent() ? parent()->root() : 0;
134 Document* HTMLImportChild::document() const
136 return (m_loader && m_loader->isOwnedBy(this)) ? m_loader->document() : 0;
139 void HTMLImportChild::wasDetachedFromDocument()
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();
146 void HTMLImportChild::didFinishParsing()
148 ASSERT(m_loader->isOwnedBy(this));
149 m_loader->didFinishParsing();
152 void HTMLImportChild::stateDidChange()
154 HTMLImport::stateDidChange();
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())
161 if (state().isReady())
165 void HTMLImportChild::ensureLoader()
170 if (HTMLImportChild* found = root()->findLinkFor(m_url, this))
176 void HTMLImportChild::createLoader()
178 ASSERT(!state().shouldBlockDocumentCreation());
180 m_loader = HTMLImportLoader::create(this);
181 m_loader->addClient(this);
182 m_loader->startLoading(resource());
185 void HTMLImportChild::shareLoader(HTMLImportChild* loader)
188 m_loader = loader->m_loader;
189 m_loader->addClient(this);
193 bool HTMLImportChild::isDone() const
195 return m_loader && m_loader->isDone();
198 bool HTMLImportChild::hasLoader() const
203 bool HTMLImportChild::ownsLoader() const
205 return m_loader && m_loader->isOwnedBy(this);
208 bool HTMLImportChild::loaderHasError() const
210 return m_loader && m_loader->hasError();
214 void HTMLImportChild::setClient(HTMLImportChildClient* client)
221 void HTMLImportChild::clearClient()
223 // Doesn't check m_client nullity because we allow
224 // clearClient() to reenter.
228 HTMLLinkElement* HTMLImportChild::link() const
232 return m_client->link();
236 void HTMLImportChild::showThis()
238 HTMLImport::showThis();
239 fprintf(stderr, " loader=%p own=%s async=%s url=%s",
241 hasLoader() && ownsLoader() ? "Y" : "N",
242 isCreatedByParser() ? "Y" : "N",
243 url().string().utf8().data());
247 } // namespace WebCore