- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLImportLoader.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/HTMLImportLoader.h"
33
34 #include "core/dom/Document.h"
35 #include "core/dom/custom/CustomElementRegistrationContext.h"
36 #include "core/fetch/ResourceFetcher.h"
37 #include "core/html/HTMLDocument.h"
38 #include "core/html/HTMLImportLoaderClient.h"
39 #include "core/loader/DocumentWriter.h"
40 #include "core/frame/ContentSecurityPolicyResponseHeaders.h"
41
42 namespace WebCore {
43
44 HTMLImportLoader::HTMLImportLoader(HTMLImport* parent, const KURL& url)
45     : m_parent(parent)
46     , m_state(StateLoading)
47     , m_url(url)
48 {
49 }
50
51 HTMLImportLoader::~HTMLImportLoader()
52 {
53     // importDestroyed() should be called before the destruction.
54     ASSERT(!m_parent);
55     ASSERT(!m_importedDocument);
56     if (m_resource)
57         m_resource->removeClient(this);
58 }
59
60 void HTMLImportLoader::setResource(const ResourcePtr<RawResource>& resource)
61 {
62     m_resource = resource;
63     m_resource->addClient(this);
64 }
65
66 void HTMLImportLoader::responseReceived(Resource*, const ResourceResponse& response)
67 {
68     setState(startWritingAndParsing(response));
69 }
70
71 void HTMLImportLoader::dataReceived(Resource*, const char* data, int length)
72 {
73     RefPtr<DocumentWriter> protectingWriter(m_writer);
74     m_writer->addData(data, length);
75 }
76
77 void HTMLImportLoader::notifyFinished(Resource*)
78 {
79     setState(finishWriting());
80 }
81
82 void HTMLImportLoader::setState(State state)
83 {
84     if (m_state == state)
85         return;
86
87     m_state = state;
88
89     if (m_state == StateReady || m_state == StateError || m_state == StateWritten) {
90         if (RefPtr<DocumentWriter> writer = m_writer.release())
91             writer->end();
92     }
93
94     // Since DocumentWriter::end() let setState() reenter, we shouldn't refer to m_state here.
95     if (state == StateReady || state == StateError)
96         didFinish();
97 }
98
99 void HTMLImportLoader::didFinish()
100 {
101     for (size_t i = 0; i < m_clients.size(); ++i)
102         m_clients[i]->didFinish();
103
104     if (m_resource) {
105         m_resource->removeClient(this);
106         m_resource = 0;
107     }
108
109     ASSERT(!document() || !document()->parsing());
110     root()->importWasDisposed();
111 }
112
113 HTMLImportLoader::State HTMLImportLoader::startWritingAndParsing(const ResourceResponse& response)
114 {
115     // Current canAccess() implementation isn't sufficient for catching cross-domain redirects: http://crbug.com/256976
116     if (!m_parent->document()->fetcher()->canAccess(m_resource.get()))
117         return StateError;
118
119     DocumentInit init = DocumentInit(response.url(), 0, root()->document()->contextDocument(), this)
120         .withRegistrationContext(root()->document()->registrationContext());
121     m_importedDocument = HTMLDocument::create(init);
122     m_importedDocument->initContentSecurityPolicy(ContentSecurityPolicyResponseHeaders(response));
123     m_writer = DocumentWriter::create(m_importedDocument.get(), response.mimeType(), response.textEncodingName());
124
125     return StateLoading;
126 }
127
128 HTMLImportLoader::State HTMLImportLoader::finishWriting()
129 {
130     if (!m_parent)
131         return StateError;
132     // The writer instance indicates that a part of the document can be already loaded.
133     // We don't take such a case as an error because the partially-loaded document has been visible from script at this point.
134     if (m_resource->loadFailedOrCanceled() && !m_writer)
135         return StateError;
136
137     return StateWritten;
138 }
139
140 HTMLImportLoader::State HTMLImportLoader::finishParsing()
141 {
142     if (!m_parent)
143         return StateError;
144     return StateReady;
145 }
146
147 Document* HTMLImportLoader::importedDocument() const
148 {
149     if (m_state == StateError)
150         return 0;
151     return m_importedDocument.get();
152 }
153
154 void HTMLImportLoader::addClient(HTMLImportLoaderClient* client)
155 {
156     ASSERT(kNotFound == m_clients.find(client));
157     m_clients.append(client);
158     if (isDone())
159         client->didFinish();
160 }
161
162 void HTMLImportLoader::removeClient(HTMLImportLoaderClient* client)
163 {
164     ASSERT(kNotFound != m_clients.find(client));
165     m_clients.remove(m_clients.find(client));
166 }
167
168 void HTMLImportLoader::importDestroyed()
169 {
170     m_parent = 0;
171     if (RefPtr<Document> document = m_importedDocument.release())
172         document->setImport(0);
173 }
174
175 HTMLImportRoot* HTMLImportLoader::root()
176 {
177     return m_parent ? m_parent->root() : 0;
178 }
179
180 HTMLImport* HTMLImportLoader::parent() const
181 {
182     return m_parent;
183 }
184
185 Document* HTMLImportLoader::document() const
186 {
187     return m_importedDocument.get();
188 }
189
190 void HTMLImportLoader::wasDetachedFromDocument()
191 {
192     // For imported documens this shouldn't be called because Document::m_import is
193     // cleared before Document is destroyed by HTMLImportLoader::importDestroyed().
194     ASSERT_NOT_REACHED();
195 }
196
197 void HTMLImportLoader::didFinishParsing()
198 {
199     setState(finishParsing());
200 }
201
202 bool HTMLImportLoader::isProcessing() const
203 {
204     if (!m_importedDocument)
205         return !isDone();
206     return m_importedDocument->parsing();
207 }
208
209 } // namespace WebCore