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/dom/custom/CustomElementSyncMicrotaskQueue.h"
38 #include "core/html/imports/HTMLImportChildClient.h"
39 #include "core/html/imports/HTMLImportLoader.h"
40 #include "core/html/imports/HTMLImportTreeRoot.h"
41 #include "core/html/imports/HTMLImportsController.h"
45 HTMLImportChild::HTMLImportChild(const KURL& url, HTMLImportLoader* loader, SyncMode sync)
56 HTMLImportChild::~HTMLImportChild()
59 // importDestroyed() should be called before the destruction.
63 m_client->importChildWasDestroyed(this);
67 void HTMLImportChild::ownerInserted()
69 if (!m_loader->isDone())
71 root()->document()->styleResolverChanged();
74 void HTMLImportChild::didShareLoader()
76 createCustomElementMicrotaskStepIfNeeded();
80 void HTMLImportChild::didStartLoading()
82 createCustomElementMicrotaskStepIfNeeded();
85 void HTMLImportChild::didFinish()
88 m_client->didFinish();
91 void HTMLImportChild::didFinishLoading()
94 CustomElement::didFinishLoadingImport(*(root()->document()));
97 void HTMLImportChild::didFinishUpgradingCustomElements()
100 m_customElementMicrotaskStep.clear();
104 void HTMLImportChild::importDestroyed()
107 parent()->removeChild(this);
110 m_loader->removeImport(this);
115 Document* HTMLImportChild::document() const
118 return m_loader->document();
121 void HTMLImportChild::stateWillChange()
123 toHTMLImportTreeRoot(root())->scheduleRecalcState();
126 void HTMLImportChild::stateDidChange()
128 HTMLImport::stateDidChange();
130 if (state().isReady())
134 void HTMLImportChild::invalidateCustomElementMicrotaskStep()
136 if (!m_customElementMicrotaskStep)
138 m_customElementMicrotaskStep->invalidate();
139 m_customElementMicrotaskStep.clear();
142 void HTMLImportChild::createCustomElementMicrotaskStepIfNeeded()
144 ASSERT(!m_customElementMicrotaskStep);
146 if (!isDone() && !formsCycle()) {
148 m_customElementMicrotaskStep = CustomElement::didCreateImport(this);
150 m_customElementMicrotaskStep = CustomElement::didCreateImport(this)->weakPtr();
155 bool HTMLImportChild::isDone() const
159 return m_loader->isDone() && m_loader->microtaskQueue()->isEmpty() && !m_customElementMicrotaskStep;
162 HTMLImportLoader* HTMLImportChild::loader() const
164 // This should never be called after importDestroyed.
169 void HTMLImportChild::setClient(HTMLImportChildClient* client)
177 void HTMLImportChild::clearClient()
179 // Doesn't check m_client nullity because we allow
180 // clearClient() to reenter.
185 HTMLLinkElement* HTMLImportChild::link() const
189 return m_client->link();
192 // Ensuring following invariants against the import tree:
193 // - HTMLImportChild::firstImport() is the "first import" of the DFS order of the import tree.
194 // - The "first import" manages all the children that is loaded by the document.
195 void HTMLImportChild::normalize()
197 if (!loader()->isFirstImport(this) && this->precedes(loader()->firstImport())) {
198 HTMLImportChild* oldFirst = loader()->firstImport();
199 loader()->moveToFirst(this);
200 takeChildrenFrom(oldFirst);
203 for (HTMLImportChild* child = toHTMLImportChild(firstChild()); child; child = toHTMLImportChild(child->next())) {
204 if (child->formsCycle())
205 child->invalidateCustomElementMicrotaskStep();
211 void HTMLImportChild::showThis()
213 bool isFirst = loader() ? loader()->isFirstImport(this) : false;
214 HTMLImport::showThis();
215 fprintf(stderr, " loader=%p first=%d, step=%p sync=%s url=%s",
218 m_customElementMicrotaskStep.get(),
219 isSync() ? "Y" : "N",
220 url().string().utf8().data());
224 void HTMLImportChild::trace(Visitor* visitor)
226 visitor->trace(m_customElementMicrotaskStep);
227 visitor->trace(m_loader);
228 visitor->trace(m_client);
229 HTMLImport::trace(visitor);