OLD | NEW |
---|---|
(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/Range.h" | |
36 #include "core/html/HTMLDocument.h" | |
37 #include "core/html/HTMLLinkElement.h" | |
38 #include "core/loader/cache/CachedResourceLoader.h" | |
39 #include "weborigin/SecurityOrigin.h" | |
40 | |
41 namespace WebCore { | |
42 | |
43 PassRefPtr<LinkImport> LinkImport::create(HTMLLinkElement* owner) | |
44 { | |
45 return adoptRef(new LinkImport(owner)); | |
46 } | |
47 | |
48 LinkImport::LinkImport(HTMLLinkElement* owner) | |
49 : LinkResource(owner) | |
50 , m_imports(0) | |
51 , m_ofSameLocation(0) | |
52 , m_state(StatePreparing) | |
53 { | |
54 } | |
55 | |
56 LinkImport::~LinkImport() | |
57 { | |
58 if (m_cachedDocument) | |
59 m_cachedDocument->removeClient(this); | |
60 } | |
61 | |
62 LinkImport::State LinkImport::finish() | |
63 { | |
64 if (!m_imports || !m_cachedDocument->document()) | |
65 return StateDone; | |
66 | |
67 if (!m_imports->securityOrigin()->canRequest(m_cachedDocument->response().ur l()) | |
68 && !m_cachedDocument->passesAccessControlCheck(m_imports->securityOrigin ())) | |
69 return StateDone; | |
70 | |
71 m_importedFragment = m_imports->createFragmentFrom(m_cachedDocument->documen t()); | |
72 return StateDone; | |
73 } | |
74 | |
75 void LinkImport::notifyFinished(CachedResource*) | |
76 { | |
77 setState(finish()); | |
78 } | |
79 | |
80 void LinkImport::setState(State state) | |
81 { | |
82 if (m_state == state) | |
83 return; | |
84 m_state = state; | |
85 if (m_state == StateDone && m_imports) | |
86 m_imports->didLoad(); | |
87 } | |
88 | |
89 LinkImport::State LinkImport::startRequest() | |
90 { | |
91 ASSERT(m_owner); | |
92 ASSERT(m_state == StatePreparing); | |
93 | |
94 // FIXME(morrita): Should take care of sub-imports whose document doesn't ha ve frame. | |
95 if (!m_owner->document()->frame()) | |
96 return StateDone; | |
97 | |
98 LinkRequestBuilder builder(m_owner); | |
99 if (!builder.isValid()) | |
100 return StateDone; | |
101 | |
102 m_imports = m_owner->document()->imports(); | |
103 if (RefPtr<LinkImport> found = m_imports->findLinkFor(builder.url())) { | |
104 m_ofSameLocation = found.get(); | |
105 return StateDone; | |
106 } | |
107 | |
108 // XXX: take care of CORS and cookies | |
109 CachedResourceRequest request = builder.build(true); | |
110 m_cachedDocument = m_owner->document()->cachedResourceLoader()->requestHTMLD ocument(request); | |
dglazkov
2013/05/23 18:32:00
I don't know if we need to be creating a new docum
Hajime Morrita
2013/05/24 02:28:27
That's what I've been wondering, too. There are so
dglazkov
2013/05/24 03:15:16
Can you explain why?
| |
111 if (!m_cachedDocument) | |
112 return StateDone; | |
113 | |
114 m_cachedDocument->addClient(this); | |
115 m_url = builder.url(); | |
116 m_imports->addLink(this); | |
117 | |
118 return StateStarted; | |
119 } | |
120 | |
121 DocumentFragment* LinkImport::importedFragment() const | |
122 { | |
123 if (!m_owner) | |
124 return 0; | |
125 | |
126 if (m_ofSameLocation) { | |
127 ASSERT(!m_importedFragment); | |
128 return m_ofSameLocation->importedFragment(); | |
129 } | |
130 | |
131 return m_importedFragment.get(); | |
132 } | |
133 | |
134 void LinkImport::process() | |
135 { | |
136 if (StatePreparing != m_state) | |
137 return; | |
138 setState(startRequest()); | |
139 } | |
140 | |
141 void LinkImport::ownerRemoved() | |
142 { | |
143 m_owner = 0; | |
144 } | |
145 | |
146 void LinkImport::importDestroyed() | |
147 { | |
148 m_imports = 0; | |
149 m_importedFragment.clear(); | |
150 } | |
151 | |
152 PassOwnPtr<HTMLImports> HTMLImports::create(HTMLImportsHost* host) | |
153 { | |
154 return adoptPtr(new HTMLImports(host)); | |
155 } | |
156 | |
157 HTMLImports::HTMLImports(HTMLImportsHost* host) | |
158 : m_host(host) | |
159 , m_owner(HTMLDocument::create(0, KURL())) | |
160 { | |
161 } | |
162 | |
163 HTMLImports::~HTMLImports() | |
164 { | |
165 for (size_t i = 0; i < m_links.size(); ++i) | |
166 m_links[i]->importDestroyed(); | |
167 } | |
168 | |
169 void HTMLImports::addLink(PassRefPtr<LinkImport> link) | |
170 { | |
171 ASSERT(!link->url().isEmpty() && link->url().isValid()); | |
172 m_links.append(link); | |
173 } | |
174 | |
175 void HTMLImports::didLoad() | |
176 { | |
177 if (haveLoaded()) | |
178 m_host->didLoadAllImports(); | |
179 } | |
180 | |
181 PassRefPtr<DocumentFragment> HTMLImports::createFragmentFrom(Document* source) c onst | |
182 { | |
183 RefPtr<Range> toClone = Range::create(source); | |
184 toClone->setStartBefore(source->documentElement(), ASSERT_NO_EXCEPTION); | |
185 toClone->setEndAfter(source->documentElement(), ASSERT_NO_EXCEPTION); | |
186 RefPtr<DocumentFragment> created = toClone->cloneContents(ASSERT_NO_EXCEPTIO N); | |
187 m_owner->adoptNode(created, ASSERT_NO_EXCEPTION); | |
188 return created.release(); | |
189 } | |
190 | |
191 PassRefPtr<LinkImport> HTMLImports::findLinkFor(const KURL& url) const | |
192 { | |
193 for (size_t i = 0; i < m_links.size(); ++i) { | |
194 if (m_links[i]->url() == url) | |
195 return m_links[i]; | |
196 } | |
197 | |
198 return 0; | |
199 } | |
200 | |
201 SecurityOrigin* HTMLImports::securityOrigin() const | |
202 { | |
203 return m_host->importsContext()->securityOrigin(); | |
204 } | |
205 | |
206 bool HTMLImports::haveLoaded() const | |
207 { | |
208 for (size_t i = 0; i < m_links.size(); ++i) { | |
209 if (!m_links[i]->isDone()) | |
210 return false; | |
211 } | |
212 | |
213 return true; | |
214 } | |
215 | |
216 } // namespace WebCore | |
OLD | NEW |