Upstream version 5.34.104.0
[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/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"
41
42
43 namespace WebCore {
44
45 HTMLImportLoader::HTMLImportLoader(HTMLImport* import)
46     : m_import(import)
47     , m_state(StateLoading)
48 {
49 }
50
51 HTMLImportLoader::~HTMLImportLoader()
52 {
53     if (m_importedDocument)
54         m_importedDocument->setImport(0);
55 }
56
57 void HTMLImportLoader::startLoading(const ResourcePtr<RawResource>& resource)
58 {
59     setResource(resource);
60 }
61
62 void HTMLImportLoader::responseReceived(Resource* resource, const ResourceResponse& response)
63 {
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) {
67         setState(StateError);
68         return;
69     }
70     setState(startWritingAndParsing(response));
71 }
72
73 void HTMLImportLoader::dataReceived(Resource*, const char* data, int length)
74 {
75     RefPtr<DocumentWriter> protectingWriter(m_writer);
76     m_writer->addData(data, length);
77 }
78
79 void HTMLImportLoader::notifyFinished(Resource* resource)
80 {
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) {
84         setState(StateError);
85         return;
86     }
87
88     setState(finishWriting());
89 }
90
91 HTMLImportLoader::State HTMLImportLoader::startWritingAndParsing(const ResourceResponse& response)
92 {
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());
98
99     return StateLoading;
100 }
101
102 HTMLImportLoader::State HTMLImportLoader::finishWriting()
103 {
104     return StateWritten;
105 }
106
107 HTMLImportLoader::State HTMLImportLoader::finishParsing()
108 {
109     return StateReady;
110 }
111
112 void HTMLImportLoader::setState(State state)
113 {
114     if (m_state == state)
115         return;
116
117     m_state = state;
118
119     if (m_state == StateReady || m_state == StateError || m_state == StateWritten) {
120         if (RefPtr<DocumentWriter> writer = m_writer.release())
121             writer->end();
122     }
123
124     // Since DocumentWriter::end() can let setState() reenter, we shouldn't refer to m_state here.
125     if (state == StateReady || state == StateError)
126         didFinish();
127 }
128
129 void HTMLImportLoader::didFinishParsing()
130 {
131     setState(finishParsing());
132 }
133
134 Document* HTMLImportLoader::importedDocument() const
135 {
136     if (m_state == StateError)
137         return 0;
138     return m_importedDocument.get();
139 }
140
141 void HTMLImportLoader::didFinish()
142 {
143     for (size_t i = 0; i < m_clients.size(); ++i)
144         m_clients[i]->didFinishLoading();
145
146     clearResource();
147
148     ASSERT(!m_importedDocument || !m_importedDocument->parsing());
149 }
150
151 void HTMLImportLoader::addClient(HTMLImportLoaderClient* client)
152 {
153     ASSERT(kNotFound == m_clients.find(client));
154     m_clients.append(client);
155     if (isDone())
156         client->didFinishLoading();
157 }
158
159 void HTMLImportLoader::removeClient(HTMLImportLoaderClient* client)
160 {
161     ASSERT(kNotFound != m_clients.find(client));
162     m_clients.remove(m_clients.find(client));
163 }
164
165 } // namespace WebCore