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/html/HTMLImportChildClient.h"
36#include "core/html/HTMLImportLoader.h"
37
38namespace WebCore {
39
40HTMLImportChild::HTMLImportChild(const KURL& url, HTMLImportChildClient* client)
41    : m_url(url)
42    , m_client(client)
43{
44}
45
46HTMLImportChild::~HTMLImportChild()
47{
48    // importDestroyed() should be called before the destruction.
49    ASSERT(!m_loader);
50}
51
52void HTMLImportChild::wasAlreadyLoadedAs(HTMLImportChild* found)
53{
54    ASSERT(!m_loader);
55    ASSERT(m_client);
56    shareLoader(found);
57}
58
59void HTMLImportChild::startLoading(const ResourcePtr<RawResource>& resource)
60{
61    ASSERT(!hasResource());
62    ASSERT(!m_loader);
63
64    HTMLImportResourceOwner::setResource(resource);
65
66    // If the node is "document blocked", it cannot create HTMLImportLoader
67    // even if there is no sharable one found, as there is possibility that
68    // preceding imports load the sharable imports.
69    // In that case preceding one should win because it comes first in the tree order.
70    // See also didUnblockDocument().
71    if (isDocumentBlocked())
72        return;
73
74    createLoader();
75}
76
77void HTMLImportChild::didFinish()
78{
79    if (m_client)
80        m_client->didFinish();
81    clearResource();
82    root()->blockerGone();
83}
84
85Document* HTMLImportChild::importedDocument() const
86{
87    if (!m_loader)
88        return 0;
89    return m_loader->importedDocument();
90}
91
92void HTMLImportChild::importDestroyed()
93{
94    if (parent())
95        parent()->removeChild(this);
96    if (m_loader) {
97        m_loader->removeClient(this);
98        m_loader.clear();
99    }
100}
101
102HTMLImportRoot* HTMLImportChild::root()
103{
104    return parent() ? parent()->root() : 0;
105}
106
107Document* HTMLImportChild::document() const
108{
109    return (m_loader && m_loader->isOwnedBy(this)) ? m_loader->document() : 0;
110}
111
112void HTMLImportChild::wasDetachedFromDocument()
113{
114    // For imported documens this shouldn't be called because Document::m_import is
115    // cleared before Document is destroyed by HTMLImportChild::importDestroyed().
116    ASSERT_NOT_REACHED();
117}
118
119void HTMLImportChild::didFinishParsing()
120{
121    ASSERT(m_loader->isOwnedBy(this));
122    m_loader->didFinishParsing();
123}
124
125// Once all preceding imports are loaded and "document blocking" ends,
126// HTMLImportChild can decide whether it should load the import by itself
127// or it can share existing one.
128void HTMLImportChild::didUnblockDocument()
129{
130    HTMLImport::didUnblockDocument();
131    ASSERT(!isDocumentBlocked());
132    ASSERT(!m_loader || !m_loader->isOwnedBy(this));
133
134    if (m_loader)
135        return;
136    if (HTMLImportChild* found = root()->findLinkFor(m_url, this))
137        shareLoader(found);
138    else
139        createLoader();
140}
141
142void HTMLImportChild::createLoader()
143{
144    ASSERT(!isDocumentBlocked());
145    ASSERT(!m_loader);
146    m_loader = HTMLImportLoader::create(this, parent()->document()->fetcher());
147    m_loader->addClient(this);
148    m_loader->startLoading(resource());
149}
150
151void HTMLImportChild::shareLoader(HTMLImportChild* loader)
152{
153    ASSERT(!m_loader);
154    m_loader = loader->m_loader;
155    m_loader->addClient(this);
156    root()->blockerGone();
157}
158
159bool HTMLImportChild::isProcessing() const
160{
161    return m_loader && m_loader->isOwnedBy(this) && m_loader->isProcessing();
162}
163
164bool HTMLImportChild::isDone() const
165{
166    return m_loader && m_loader->isDone();
167}
168
169bool HTMLImportChild::isLoaded() const
170{
171    return m_loader->isLoaded();
172}
173
174} // namespace WebCore
175