Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: Source/core/html/HTMLImports.cpp

Issue 15856002: First step of HTMLImports (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Got rid of CachedDocument usage Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/HTMLImports.h"
33
34 #include "core/dom/DocumentFragment.h"
35 #include "core/dom/DocumentType.h"
36 #include "core/dom/Range.h"
37 #include "core/html/HTMLDocument.h"
38 #include "core/html/HTMLLinkElement.h"
39 #include "core/loader/cache/CachedResourceLoader.h"
40 #include "core/loader/cache/CachedScript.h"
41 #include "weborigin/SecurityOrigin.h"
42
43 namespace WebCore {
44
45 PassRefPtr<LinkImport> LinkImport::create(HTMLLinkElement* owner)
46 {
47 return adoptRef(new LinkImport(owner));
48 }
49
50 LinkImport::LinkImport(HTMLLinkElement* owner)
51 : LinkResource(owner)
52 , m_imports(0)
53 , m_ofSameLocation(0)
54 , m_state(StatePreparing)
55 {
56 }
57
58 LinkImport::~LinkImport()
59 {
60 if (m_resource)
61 m_resource->removeClient(this);
62 }
63
64 LinkImport::State LinkImport::finish()
65 {
66 if (!m_imports)
67 return StateError;
68
69 if (m_resource->loadFailedOrCanceled())
70 return StateError;
71
72 String error;
73 if (!m_imports->securityOrigin()->canRequest(m_resource->response().url())
74 && !m_resource->passesAccessControlCheck(m_imports->securityOrigin(), er ror)) {
75 m_imports->showSecurityErrorMessage("Import from origin '" + SecurityOri gin::create(m_resource->response().url())->toString() + "' has been blocked from loading by Cross-Origin Resource Sharing policy: " + error);
76 return StateError;
77 }
78
79 // FIXME(morrita): This should be done in incremental way.
80 RefPtr<Document> parsingPlaceholder = HTMLDocument::create(0, KURL());
81 parsingPlaceholder->setContent(m_resource->script());
82
83 // Doctypes cannot be moved between documents. So we remove it before the mi gration.
84 if (RefPtr<Node> doctype = parsingPlaceholder->doctype())
85 parsingPlaceholder->removeChild(doctype.get());
86 m_importedFragment->takeAllChildrenFrom(parsingPlaceholder.get());
87
88 return StateReady;
89 }
90
91 void LinkImport::notifyFinished(CachedResource*)
92 {
93 setState(finish());
94 }
95
96 void LinkImport::setState(State state)
97 {
98 if (m_state == state)
99 return;
100 m_state = state;
101
102 if ((m_state == StateReady || m_state == StateError)
103 && m_imports)
104 m_imports->didLoad();
105 }
106
107 LinkImport::State LinkImport::startRequest()
108 {
109 ASSERT(m_owner);
110 ASSERT(m_state == StatePreparing);
111
112 // FIXME(morrita): Should take care of sub-imports whose document doesn't ha ve frame.
113 if (!m_owner->document()->frame())
114 return StateError;
115
116 LinkRequestBuilder builder(m_owner);
117 if (!builder.isValid())
118 return StateError;
119
120 m_imports = m_owner->document()->imports();
121 if (RefPtr<LinkImport> found = m_imports->findLinkFor(builder.url())) {
122 m_ofSameLocation = found.get();
123 return StateReady;
124 }
125
126 CachedResourceRequest request = builder.build(true);
127 m_resource = m_owner->document()->cachedResourceLoader()->requestScript(requ est);
128 if (!m_resource)
129 return StateError;
130
131 m_resource->addClient(this);
132 m_url = builder.url();
133 m_imports->addImport(this);
134 m_importedFragment = m_imports->createDocumentFragment();
135
136 return StateStarted;
137 }
138
139 DocumentFragment* LinkImport::importedFragment() const
140 {
141 if (!m_owner)
142 return 0;
143 if (m_state != StateReady)
144 return 0;
145
146 if (m_ofSameLocation) {
147 ASSERT(!m_importedFragment);
148 return m_ofSameLocation->importedFragment();
149 }
150
151 return m_importedFragment.get();
152 }
153
154 void LinkImport::process()
155 {
156 if (StatePreparing != m_state)
157 return;
158 setState(startRequest());
159 }
160
161 void LinkImport::ownerRemoved()
162 {
163 m_owner = 0;
164 }
165
166 void LinkImport::importDestroyed()
167 {
168 m_imports = 0;
169 m_importedFragment.clear();
170 }
171
172 PassOwnPtr<HTMLImports> HTMLImports::create(HTMLImportsMaster* master)
173 {
174 return adoptPtr(new HTMLImports(master));
175 }
176
177 HTMLImports::HTMLImports(HTMLImportsMaster* master)
178 : m_master(master)
179 , m_owner(HTMLDocument::create(0, KURL()))
dglazkov 2013/05/28 17:12:47 THIS is where things will get super-crazy. Because
Hajime Morrita 2013/05/29 01:36:00 Probably we should have multiple master documents
180 {
181 }
182
183 HTMLImports::~HTMLImports()
184 {
185 for (size_t i = 0; i < m_imports.size(); ++i)
186 m_imports[i]->importDestroyed();
187 }
188
189 void HTMLImports::addImport(PassRefPtr<LinkImport> link)
190 {
191 ASSERT(!link->url().isEmpty() && link->url().isValid());
192 m_imports.append(link);
193 }
194
195 void HTMLImports::showSecurityErrorMessage(const String& message)
196 {
197 m_master->showSecurityErrorMessage(message);
198 }
199
200 void HTMLImports::didLoad()
201 {
202 if (haveLoaded())
203 m_master->didLoadAllImports();
204 }
205
206 PassRefPtr<DocumentFragment> HTMLImports::createDocumentFragment() const
207 {
208 return m_owner->createDocumentFragment();
209 }
210
211 PassRefPtr<LinkImport> HTMLImports::findLinkFor(const KURL& url) const
212 {
213 for (size_t i = 0; i < m_imports.size(); ++i) {
214 if (m_imports[i]->url() == url)
215 return m_imports[i];
216 }
217
218 return 0;
219 }
220
221 SecurityOrigin* HTMLImports::securityOrigin() const
222 {
223 return m_master->importsContext()->securityOrigin();
224 }
225
226 bool HTMLImports::haveLoaded() const
227 {
228 for (size_t i = 0; i < m_imports.size(); ++i) {
229 if (!m_imports[i]->isDone())
230 return false;
231 }
232
233 return true;
234 }
235
236 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698