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/imports/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/imports/HTMLImportChildClient.h"
38 #include "core/html/imports/HTMLImportLoader.h"
42 HTMLImportChild::HTMLImportChild(Document& master, const KURL& url, SyncMode sync)
46 , m_customElementMicrotaskStep(0)
52 HTMLImportChild::~HTMLImportChild()
54 // importDestroyed() should be called before the destruction.
57 if (m_customElementMicrotaskStep) {
58 // if Custom Elements were blocked, must unblock them before death
59 m_customElementMicrotaskStep->importDidFinish();
60 m_customElementMicrotaskStep = 0;
64 m_client->importChildWasDestroyed(this);
66 m_master.guardDeref();
69 void HTMLImportChild::wasAlreadyLoaded()
76 void HTMLImportChild::startLoading(const ResourcePtr<RawResource>& resource)
78 ASSERT(!this->resource());
82 ASSERT(!m_customElementMicrotaskStep);
83 m_customElementMicrotaskStep = CustomElement::didCreateImport(this);
86 setResource(resource);
88 // If the node is "document blocked", it cannot create HTMLImportLoader
89 // even if there is no sharable one found, as there is possibility that
90 // preceding imports load the sharable imports.
91 // In that case preceding one should win because it comes first in the tree order.
92 // See also didUnblockFromCreatingDocument().
93 if (state().shouldBlockDocumentCreation())
99 void HTMLImportChild::didFinish()
102 m_client->didFinish();
104 if (m_customElementMicrotaskStep) {
105 m_customElementMicrotaskStep->importDidFinish();
106 m_customElementMicrotaskStep = 0;
110 void HTMLImportChild::didFinishLoading()
116 Document* HTMLImportChild::importedDocument() const
120 return m_loader->importedDocument();
123 void HTMLImportChild::importDestroyed()
126 parent()->removeChild(this);
128 m_loader->removeImport(this);
133 HTMLImportRoot* HTMLImportChild::root()
135 return parent() ? parent()->root() : 0;
138 Document* HTMLImportChild::document() const
140 return (m_loader && m_loader->isOwnedBy(this)) ? m_loader->document() : 0;
143 void HTMLImportChild::wasDetachedFromDocument()
145 // For imported documens this shouldn't be called because Document::m_import is
146 // cleared before Document is destroyed by HTMLImportChild::importDestroyed().
147 ASSERT_NOT_REACHED();
150 void HTMLImportChild::didFinishParsing()
152 ASSERT(m_loader->isOwnedBy(this));
153 m_loader->didFinishParsing();
156 void HTMLImportChild::didRemoveAllPendingStylesheet()
158 ASSERT(m_loader->isOwnedBy(this));
159 m_loader->didRemoveAllPendingStylesheet();
162 void HTMLImportChild::stateDidChange()
164 HTMLImport::stateDidChange();
166 // Once all preceding imports are loaded,
167 // HTMLImportChild can decide whether it should load the import by itself
168 // or it can share existing one.
169 if (!state().shouldBlockDocumentCreation())
171 if (state().isReady())
175 void HTMLImportChild::ensureLoader()
180 if (HTMLImportChild* found = root()->findLinkFor(m_url, this))
186 void HTMLImportChild::createLoader()
188 ASSERT(!state().shouldBlockDocumentCreation());
190 m_loader = HTMLImportLoader::create();
191 m_loader->addImport(this);
192 m_loader->startLoading(resource());
195 void HTMLImportChild::shareLoader(HTMLImportChild* loader)
198 m_loader = loader->m_loader;
199 m_loader->addImport(this);
203 bool HTMLImportChild::isDone() const
205 return m_loader && m_loader->isDone();
208 bool HTMLImportChild::hasLoader() const
213 bool HTMLImportChild::ownsLoader() const
215 return m_loader && m_loader->isOwnedBy(this);
218 bool HTMLImportChild::loaderHasError() const
220 return m_loader && m_loader->hasError();
224 void HTMLImportChild::setClient(HTMLImportChildClient* client)
231 void HTMLImportChild::clearClient()
233 // Doesn't check m_client nullity because we allow
234 // clearClient() to reenter.
238 HTMLLinkElement* HTMLImportChild::link() const
242 return m_client->link();
246 void HTMLImportChild::showThis()
248 HTMLImport::showThis();
249 fprintf(stderr, " loader=%p own=%s async=%s url=%s",
251 hasLoader() && ownsLoader() ? "Y" : "N",
252 isSync() ? "Y" : "N",
253 url().string().utf8().data());
257 } // namespace WebCore