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/fetch/ResourceFetcher.h"
36 #include "core/frame/ContentSecurityPolicyResponseHeaders.h"
37 #include "core/html/HTMLDocument.h"
38 #include "core/html/HTMLImport.h"
39 #include "core/html/HTMLImportLoaderClient.h"
40 #include "core/loader/DocumentWriter.h"
45 HTMLImportLoader::HTMLImportLoader(HTMLImport* import)
47 , m_state(StateLoading)
51 HTMLImportLoader::~HTMLImportLoader()
53 if (m_importedDocument)
54 m_importedDocument->setImport(0);
57 void HTMLImportLoader::startLoading(const ResourcePtr<RawResource>& resource)
59 setResource(resource);
62 void HTMLImportLoader::responseReceived(Resource* resource, const ResourceResponse& response)
64 // Resource may already have been loaded with the import loader
65 // being added as a client later & now being notified. Fail early.
66 if (resource->loadFailedOrCanceled() || response.httpStatusCode() >= 400) {
70 setState(startWritingAndParsing(response));
73 void HTMLImportLoader::dataReceived(Resource*, const char* data, int length)
75 RefPtr<DocumentWriter> protectingWriter(m_writer);
76 m_writer->addData(data, length);
79 void HTMLImportLoader::notifyFinished(Resource* resource)
81 // The writer instance indicates that a part of the document can be already loaded.
82 // We don't take such a case as an error because the partially-loaded document has been visible from script at this point.
83 if (resource->loadFailedOrCanceled() && !m_writer) {
88 setState(finishWriting());
91 HTMLImportLoader::State HTMLImportLoader::startWritingAndParsing(const ResourceResponse& response)
93 DocumentInit init = DocumentInit(response.url(), 0, m_import->master()->contextDocument(), m_import)
94 .withRegistrationContext(m_import->master()->registrationContext());
95 m_importedDocument = HTMLDocument::create(init);
96 m_importedDocument->initContentSecurityPolicy(ContentSecurityPolicyResponseHeaders(response));
97 m_writer = DocumentWriter::create(m_importedDocument.get(), response.mimeType(), response.textEncodingName());
102 HTMLImportLoader::State HTMLImportLoader::finishWriting()
107 HTMLImportLoader::State HTMLImportLoader::finishParsing()
112 void HTMLImportLoader::setState(State state)
114 if (m_state == state)
119 if (m_state == StateReady || m_state == StateError || m_state == StateWritten) {
120 if (RefPtr<DocumentWriter> writer = m_writer.release())
124 // Since DocumentWriter::end() can let setState() reenter, we shouldn't refer to m_state here.
125 if (state == StateReady || state == StateError)
129 void HTMLImportLoader::didFinishParsing()
131 setState(finishParsing());
134 Document* HTMLImportLoader::importedDocument() const
136 if (m_state == StateError)
138 return m_importedDocument.get();
141 void HTMLImportLoader::didFinish()
143 for (size_t i = 0; i < m_clients.size(); ++i)
144 m_clients[i]->didFinishLoading();
148 ASSERT(!m_importedDocument || !m_importedDocument->parsing());
151 void HTMLImportLoader::addClient(HTMLImportLoaderClient* client)
153 ASSERT(kNotFound == m_clients.find(client));
154 m_clients.append(client);
156 client->didFinishLoading();
159 void HTMLImportLoader::removeClient(HTMLImportLoaderClient* client)
161 ASSERT(kNotFound != m_clients.find(client));
162 m_clients.remove(m_clients.find(client));
165 } // namespace WebCore