0
|
1 |
/*
|
|
2 |
* Copyright (C) 2006, 2007, 2009 Apple 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
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* 1. Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
11 |
* notice, this list of conditions and the following disclaimer in the
|
|
12 |
* documentation and/or other materials provided with the distribution.
|
|
13 |
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
|
|
14 |
* its contributors may be used to endorse or promote products derived
|
|
15 |
* from this software without specific prior written permission.
|
|
16 |
*
|
|
17 |
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
|
|
18 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
19 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
20 |
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
|
|
21 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
22 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
23 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
24 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
25 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
26 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27 |
*/
|
|
28 |
|
|
29 |
#include "config.h"
|
|
30 |
#include "SubresourceLoader.h"
|
|
31 |
|
|
32 |
#include "DocumentLoader.h"
|
|
33 |
#include "Frame.h"
|
|
34 |
#include "FrameLoader.h"
|
|
35 |
#include "ResourceHandle.h"
|
|
36 |
#include "SecurityOrigin.h"
|
|
37 |
#include "SubresourceLoaderClient.h"
|
|
38 |
#include <wtf/RefCountedLeakCounter.h>
|
|
39 |
|
|
40 |
namespace WebCore {
|
|
41 |
|
|
42 |
#ifndef NDEBUG
|
|
43 |
static WTF::RefCountedLeakCounter subresourceLoaderCounter("SubresourceLoader");
|
|
44 |
#endif
|
|
45 |
|
|
46 |
SubresourceLoader::SubresourceLoader(Frame* frame, SubresourceLoaderClient* client, bool sendResourceLoadCallbacks, bool shouldContentSniff)
|
|
47 |
: ResourceLoader(frame, sendResourceLoadCallbacks, shouldContentSniff)
|
|
48 |
, m_client(client)
|
|
49 |
, m_loadingMultipartContent(false)
|
|
50 |
{
|
|
51 |
#ifndef NDEBUG
|
|
52 |
subresourceLoaderCounter.increment();
|
|
53 |
#endif
|
|
54 |
m_documentLoader->addSubresourceLoader(this);
|
|
55 |
}
|
|
56 |
|
|
57 |
SubresourceLoader::~SubresourceLoader()
|
|
58 |
{
|
|
59 |
#ifndef NDEBUG
|
|
60 |
subresourceLoaderCounter.decrement();
|
|
61 |
#endif
|
|
62 |
}
|
|
63 |
|
|
64 |
PassRefPtr<SubresourceLoader> SubresourceLoader::create(Frame* frame, SubresourceLoaderClient* client, const ResourceRequest& request, bool skipCanLoadCheck, bool sendResourceLoadCallbacks, bool shouldContentSniff)
|
|
65 |
{
|
|
66 |
if (!frame)
|
|
67 |
return 0;
|
|
68 |
|
|
69 |
FrameLoader* fl = frame->loader();
|
|
70 |
if (!skipCanLoadCheck && (fl->state() == FrameStateProvisional || fl->activeDocumentLoader()->isStopping()))
|
|
71 |
return 0;
|
|
72 |
|
|
73 |
ResourceRequest newRequest = request;
|
|
74 |
|
|
75 |
if (!skipCanLoadCheck
|
|
76 |
&& SecurityOrigin::restrictAccessToLocal()
|
|
77 |
&& !SecurityOrigin::canLoad(request.url(), String(), frame->document())) {
|
|
78 |
FrameLoader::reportLocalLoadFailed(frame, request.url().string());
|
|
79 |
return 0;
|
|
80 |
}
|
|
81 |
|
|
82 |
if (SecurityOrigin::shouldHideReferrer(request.url(), fl->outgoingReferrer()))
|
|
83 |
newRequest.clearHTTPReferrer();
|
|
84 |
else if (!request.httpReferrer())
|
|
85 |
newRequest.setHTTPReferrer(fl->outgoingReferrer());
|
|
86 |
FrameLoader::addHTTPOriginIfNeeded(newRequest, fl->outgoingOrigin());
|
|
87 |
|
|
88 |
// Use the original request's cache policy for two reasons:
|
|
89 |
// 1. For POST requests, we mutate the cache policy for the main resource,
|
|
90 |
// but we do not want this to apply to subresources
|
|
91 |
// 2. Delegates that modify the cache policy using willSendRequest: should
|
|
92 |
// not affect any other resources. Such changes need to be done
|
|
93 |
// per request.
|
|
94 |
if (newRequest.isConditional())
|
|
95 |
newRequest.setCachePolicy(ReloadIgnoringCacheData);
|
|
96 |
else
|
|
97 |
newRequest.setCachePolicy(fl->originalRequest().cachePolicy());
|
|
98 |
|
|
99 |
fl->addExtraFieldsToSubresourceRequest(newRequest);
|
|
100 |
|
|
101 |
RefPtr<SubresourceLoader> subloader(adoptRef(new SubresourceLoader(frame, client, sendResourceLoadCallbacks, shouldContentSniff)));
|
|
102 |
if (!subloader->load(newRequest))
|
|
103 |
return 0;
|
|
104 |
|
|
105 |
return subloader.release();
|
|
106 |
}
|
|
107 |
|
|
108 |
void SubresourceLoader::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
|
|
109 |
{
|
|
110 |
// Store the previous URL because the call to ResourceLoader::willSendRequest will modify it.
|
|
111 |
KURL previousURL = request().url();
|
|
112 |
|
|
113 |
ResourceLoader::willSendRequest(newRequest, redirectResponse);
|
|
114 |
if (!previousURL.isNull() && !newRequest.isNull() && previousURL != newRequest.url() && m_client)
|
|
115 |
m_client->willSendRequest(this, newRequest, redirectResponse);
|
|
116 |
}
|
|
117 |
|
|
118 |
void SubresourceLoader::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
|
|
119 |
{
|
|
120 |
RefPtr<SubresourceLoader> protect(this);
|
|
121 |
|
|
122 |
if (m_client)
|
|
123 |
m_client->didSendData(this, bytesSent, totalBytesToBeSent);
|
|
124 |
}
|
|
125 |
|
|
126 |
void SubresourceLoader::didReceiveResponse(const ResourceResponse& r)
|
|
127 |
{
|
|
128 |
ASSERT(!r.isNull());
|
|
129 |
|
|
130 |
if (r.isMultipart())
|
|
131 |
m_loadingMultipartContent = true;
|
|
132 |
|
|
133 |
// Reference the object in this method since the additional processing can do
|
|
134 |
// anything including removing the last reference to this object; one example of this is 3266216.
|
|
135 |
RefPtr<SubresourceLoader> protect(this);
|
|
136 |
|
|
137 |
if (m_client)
|
|
138 |
m_client->didReceiveResponse(this, r);
|
|
139 |
|
|
140 |
// The loader can cancel a load if it receives a multipart response for a non-image
|
|
141 |
if (reachedTerminalState())
|
|
142 |
return;
|
|
143 |
ResourceLoader::didReceiveResponse(r);
|
|
144 |
|
|
145 |
RefPtr<SharedBuffer> buffer = resourceData();
|
|
146 |
if (m_loadingMultipartContent && buffer && buffer->size()) {
|
|
147 |
// Since a subresource loader does not load multipart sections progressively,
|
|
148 |
// deliver the previously received data to the loader all at once now.
|
|
149 |
// Then clear the data to make way for the next multipart section.
|
|
150 |
if (m_client)
|
|
151 |
m_client->didReceiveData(this, buffer->data(), buffer->size());
|
|
152 |
clearResourceData();
|
|
153 |
|
|
154 |
// After the first multipart section is complete, signal to delegates that this load is "finished"
|
|
155 |
m_documentLoader->subresourceLoaderFinishedLoadingOnePart(this);
|
|
156 |
didFinishLoadingOnePart();
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
void SubresourceLoader::didReceiveData(const char* data, int length, long long lengthReceived, bool allAtOnce)
|
|
161 |
{
|
|
162 |
// Reference the object in this method since the additional processing can do
|
|
163 |
// anything including removing the last reference to this object; one example of this is 3266216.
|
|
164 |
RefPtr<SubresourceLoader> protect(this);
|
|
165 |
|
|
166 |
ResourceLoader::didReceiveData(data, length, lengthReceived, allAtOnce);
|
|
167 |
|
|
168 |
// A subresource loader does not load multipart sections progressively.
|
|
169 |
// So don't deliver any data to the loader yet.
|
|
170 |
if (!m_loadingMultipartContent && m_client)
|
|
171 |
m_client->didReceiveData(this, data, length);
|
|
172 |
}
|
|
173 |
|
|
174 |
void SubresourceLoader::didFinishLoading()
|
|
175 |
{
|
|
176 |
if (cancelled())
|
|
177 |
return;
|
|
178 |
ASSERT(!reachedTerminalState());
|
|
179 |
|
|
180 |
// Calling removeSubresourceLoader will likely result in a call to deref, so we must protect ourselves.
|
|
181 |
RefPtr<SubresourceLoader> protect(this);
|
|
182 |
|
|
183 |
if (m_client)
|
|
184 |
m_client->didFinishLoading(this);
|
|
185 |
|
|
186 |
m_handle = 0;
|
|
187 |
|
|
188 |
if (cancelled())
|
|
189 |
return;
|
|
190 |
m_documentLoader->removeSubresourceLoader(this);
|
|
191 |
ResourceLoader::didFinishLoading();
|
|
192 |
}
|
|
193 |
|
|
194 |
void SubresourceLoader::didFail(const ResourceError& error)
|
|
195 |
{
|
|
196 |
if (cancelled())
|
|
197 |
return;
|
|
198 |
ASSERT(!reachedTerminalState());
|
|
199 |
|
|
200 |
// Calling removeSubresourceLoader will likely result in a call to deref, so we must protect ourselves.
|
|
201 |
RefPtr<SubresourceLoader> protect(this);
|
|
202 |
|
|
203 |
if (m_client)
|
|
204 |
m_client->didFail(this, error);
|
|
205 |
|
|
206 |
m_handle = 0;
|
|
207 |
|
|
208 |
if (cancelled())
|
|
209 |
return;
|
|
210 |
m_documentLoader->removeSubresourceLoader(this);
|
|
211 |
ResourceLoader::didFail(error);
|
|
212 |
}
|
|
213 |
|
|
214 |
void SubresourceLoader::didCancel(const ResourceError& error)
|
|
215 |
{
|
|
216 |
ASSERT(!reachedTerminalState());
|
|
217 |
|
|
218 |
// Calling removeSubresourceLoader will likely result in a call to deref, so we must protect ourselves.
|
|
219 |
RefPtr<SubresourceLoader> protect(this);
|
|
220 |
|
|
221 |
if (m_client)
|
|
222 |
m_client->didFail(this, error);
|
|
223 |
|
|
224 |
if (cancelled())
|
|
225 |
return;
|
|
226 |
|
|
227 |
// The only way the subresource loader can reach the terminal state here is if the run loop spins when calling
|
|
228 |
// m_client->didFail. This should in theory not happen which is why the assert is here.
|
|
229 |
ASSERT(!reachedTerminalState());
|
|
230 |
if (reachedTerminalState())
|
|
231 |
return;
|
|
232 |
|
|
233 |
m_documentLoader->removeSubresourceLoader(this);
|
|
234 |
ResourceLoader::didCancel(error);
|
|
235 |
}
|
|
236 |
|
|
237 |
bool SubresourceLoader::shouldUseCredentialStorage()
|
|
238 |
{
|
|
239 |
RefPtr<SubresourceLoader> protect(this);
|
|
240 |
|
|
241 |
bool shouldUse;
|
|
242 |
if (m_client && m_client->getShouldUseCredentialStorage(this, shouldUse))
|
|
243 |
return shouldUse;
|
|
244 |
|
|
245 |
return ResourceLoader::shouldUseCredentialStorage();
|
|
246 |
}
|
|
247 |
|
|
248 |
void SubresourceLoader::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
|
|
249 |
{
|
|
250 |
RefPtr<SubresourceLoader> protect(this);
|
|
251 |
|
|
252 |
if (m_client)
|
|
253 |
m_client->didReceiveAuthenticationChallenge(this, challenge);
|
|
254 |
|
|
255 |
// The SubResourceLoaderClient may have cancelled this ResourceLoader in response to the challenge.
|
|
256 |
// If that's the case, don't call didReceiveAuthenticationChallenge
|
|
257 |
if (reachedTerminalState())
|
|
258 |
return;
|
|
259 |
|
|
260 |
ResourceLoader::didReceiveAuthenticationChallenge(challenge);
|
|
261 |
}
|
|
262 |
|
|
263 |
void SubresourceLoader::receivedCancellation(const AuthenticationChallenge& challenge)
|
|
264 |
{
|
|
265 |
ASSERT(!reachedTerminalState());
|
|
266 |
|
|
267 |
RefPtr<SubresourceLoader> protect(this);
|
|
268 |
|
|
269 |
if (m_client)
|
|
270 |
m_client->receivedCancellation(this, challenge);
|
|
271 |
|
|
272 |
ResourceLoader::receivedCancellation(challenge);
|
|
273 |
}
|
|
274 |
|
|
275 |
|
|
276 |
}
|