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/HTMLImportLoader.h"
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"
44 HTMLImportLoader::HTMLImportLoader(HTMLImport* parent, const KURL& url)
46 , m_state(StateLoading)
51 HTMLImportLoader::~HTMLImportLoader()
53 // importDestroyed() should be called before the destruction.
55 ASSERT(!m_importedDocument);
57 m_resource->removeClient(this);
60 void HTMLImportLoader::setResource(const ResourcePtr<RawResource>& resource)
62 m_resource = resource;
63 m_resource->addClient(this);
66 void HTMLImportLoader::responseReceived(Resource*, const ResourceResponse& response)
68 setState(startWritingAndParsing(response));
71 void HTMLImportLoader::dataReceived(Resource*, const char* data, int length)
73 RefPtr<DocumentWriter> protectingWriter(m_writer);
74 m_writer->addData(data, length);
77 void HTMLImportLoader::notifyFinished(Resource*)
79 setState(finishWriting());
82 void HTMLImportLoader::setState(State state)
89 if (m_state == StateReady || m_state == StateError || m_state == StateWritten) {
90 if (RefPtr<DocumentWriter> writer = m_writer.release())
94 // Since DocumentWriter::end() let setState() reenter, we shouldn't refer to m_state here.
95 if (state == StateReady || state == StateError)
99 void HTMLImportLoader::didFinish()
101 for (size_t i = 0; i < m_clients.size(); ++i)
102 m_clients[i]->didFinish();
105 m_resource->removeClient(this);
109 ASSERT(!document() || !document()->parsing());
110 root()->importWasDisposed();
113 HTMLImportLoader::State HTMLImportLoader::startWritingAndParsing(const ResourceResponse& response)
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()))
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());
128 HTMLImportLoader::State HTMLImportLoader::finishWriting()
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)
140 HTMLImportLoader::State HTMLImportLoader::finishParsing()
147 Document* HTMLImportLoader::importedDocument() const
149 if (m_state == StateError)
151 return m_importedDocument.get();
154 void HTMLImportLoader::addClient(HTMLImportLoaderClient* client)
156 ASSERT(kNotFound == m_clients.find(client));
157 m_clients.append(client);
162 void HTMLImportLoader::removeClient(HTMLImportLoaderClient* client)
164 ASSERT(kNotFound != m_clients.find(client));
165 m_clients.remove(m_clients.find(client));
168 void HTMLImportLoader::importDestroyed()
171 if (RefPtr<Document> document = m_importedDocument.release())
172 document->setImport(0);
175 HTMLImportRoot* HTMLImportLoader::root()
177 return m_parent ? m_parent->root() : 0;
180 HTMLImport* HTMLImportLoader::parent() const
185 Document* HTMLImportLoader::document() const
187 return m_importedDocument.get();
190 void HTMLImportLoader::wasDetachedFromDocument()
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();
197 void HTMLImportLoader::didFinishParsing()
199 setState(finishParsing());
202 bool HTMLImportLoader::isProcessing() const
204 if (!m_importedDocument)
206 return m_importedDocument->parsing();
209 } // namespace WebCore