blob: 5b95419d08740f880de5d53bfb259c304d5fb105 [file] [log] [blame]
mjs95611582006-10-09 09:53:40 +00001/*
2 * Copyright (C) 2006 Apple Computer, 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
darin27bd6622006-10-30 01:16:42 +000029#include "config.h"
30#include "MainResourceLoader.h"
mjs95611582006-10-09 09:53:40 +000031
andersca2dbcee82007-03-07 19:31:55 +000032#include "DocumentLoader.h"
andersca8f7934f2007-01-10 19:39:07 +000033#include "Frame.h"
34#include "FrameLoader.h"
35#include "FrameLoaderClient.h"
36#include "HTMLFormElement.h"
37#include "ResourceError.h"
38#include "ResourceHandle.h"
39
andersca8f7934f2007-01-10 19:39:07 +000040// FIXME: More that is in common with SubresourceLoader should move up into ResourceLoader.
41
darin9ab4ef22006-10-13 17:07:20 +000042namespace WebCore {
mjs95611582006-10-09 09:53:40 +000043
andersca8f7934f2007-01-10 19:39:07 +000044MainResourceLoader::MainResourceLoader(Frame* frame)
anderscad92451d2007-06-22 19:04:58 +000045 : ResourceLoader(frame, true)
mjs074dfb32007-01-31 02:40:54 +000046 , m_dataLoadTimer(this, &MainResourceLoader::handleDataLoadNow)
andersca8f7934f2007-01-10 19:39:07 +000047 , m_loadingMultipartContent(false)
48 , m_waitingForContentPolicy(false)
49{
50}
51
52MainResourceLoader::~MainResourceLoader()
53{
54}
55
56PassRefPtr<MainResourceLoader> MainResourceLoader::create(Frame* frame)
57{
58 return new MainResourceLoader(frame);
59}
60
61void MainResourceLoader::receivedError(const ResourceError& error)
62{
63 // Calling receivedMainResourceError will likely result in the last reference to this object to go away.
64 RefPtr<MainResourceLoader> protect(this);
mjs8d620d52007-05-10 12:31:42 +000065 RefPtr<Frame> protectFrame(m_frame);
andersca5d5f8022007-02-28 00:38:00 +000066
andersca8f7934f2007-01-10 19:39:07 +000067 if (!cancelled()) {
68 ASSERT(!reachedTerminalState());
69 frameLoader()->didFailToLoad(this, error);
andersca5d5f8022007-02-28 00:38:00 +000070 }
71
mjs8d620d52007-05-10 12:31:42 +000072 frameLoader()->receivedMainResourceError(error, true);
73
74 if (!cancelled()) {
75 releaseResources();
76 }
77
andersca8f7934f2007-01-10 19:39:07 +000078 ASSERT(reachedTerminalState());
79}
80
81void MainResourceLoader::didCancel(const ResourceError& error)
82{
mjs074dfb32007-01-31 02:40:54 +000083 m_dataLoadTimer.stop();
84
andersca8f7934f2007-01-10 19:39:07 +000085 // Calling receivedMainResourceError will likely result in the last reference to this object to go away.
86 RefPtr<MainResourceLoader> protect(this);
87
88 if (m_waitingForContentPolicy) {
89 frameLoader()->cancelContentPolicyCheck();
90 ASSERT(m_waitingForContentPolicy);
91 m_waitingForContentPolicy = false;
92 deref(); // balances ref in didReceiveResponse
93 }
94 frameLoader()->receivedMainResourceError(error, true);
95 ResourceLoader::didCancel(error);
96}
97
98ResourceError MainResourceLoader::interruptionForPolicyChangeError() const
99{
100 return frameLoader()->interruptionForPolicyChangeError(request());
101}
102
103void MainResourceLoader::stopLoadingForPolicyChange()
104{
105 cancel(interruptionForPolicyChangeError());
106}
107
108void MainResourceLoader::callContinueAfterNavigationPolicy(void* argument, const ResourceRequest& request, PassRefPtr<FormState>, bool shouldContinue)
109{
110 static_cast<MainResourceLoader*>(argument)->continueAfterNavigationPolicy(request, shouldContinue);
111}
112
113void MainResourceLoader::continueAfterNavigationPolicy(const ResourceRequest& request, bool shouldContinue)
114{
115 if (!shouldContinue)
116 stopLoadingForPolicyChange();
117 deref(); // balances ref in willSendRequest
118}
119
120bool MainResourceLoader::isPostOrRedirectAfterPost(const ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
121{
122 if (newRequest.httpMethod() == "POST")
123 return true;
124
125 int status = redirectResponse.httpStatusCode();
126 if (((status >= 301 && status <= 303) || status == 307)
127 && frameLoader()->initialRequest().httpMethod() == "POST")
128 return true;
129
130 return false;
131}
132
133void MainResourceLoader::addData(const char* data, int length, bool allAtOnce)
134{
135 ResourceLoader::addData(data, length, allAtOnce);
136 frameLoader()->receivedData(data, length);
137}
138
139void MainResourceLoader::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
140{
141 // Note that there are no asserts here as there are for the other callbacks. This is due to the
142 // fact that this "callback" is sent when starting every load, and the state of callback
143 // deferrals plays less of a part in this function in preventing the bad behavior deferring
144 // callbacks is meant to prevent.
145 ASSERT(!newRequest.isNull());
146
147 // The additional processing can do anything including possibly removing the last
148 // reference to this object; one example of this is 3266216.
149 RefPtr<MainResourceLoader> protect(this);
150
151 // Update cookie policy base URL as URL changes, except for subframes, which use the
152 // URL of the main frame which doesn't change when we redirect.
153 if (frameLoader()->isLoadingMainFrame())
154 newRequest.setMainDocumentURL(newRequest.url());
155
156 // If we're fielding a redirect in response to a POST, force a load from origin, since
157 // this is a common site technique to return to a page viewing some data that the POST
158 // just modified.
159 // Also, POST requests always load from origin, but this does not affect subresources.
160 if (newRequest.cachePolicy() == UseProtocolCachePolicy && isPostOrRedirectAfterPost(newRequest, redirectResponse))
161 newRequest.setCachePolicy(ReloadIgnoringCacheData);
162
mjs2d326f52007-01-29 12:50:49 +0000163 if (!newRequest.isNull()) {
164 ResourceLoader::willSendRequest(newRequest, redirectResponse);
165 setRequest(newRequest);
166 }
andersca8f7934f2007-01-10 19:39:07 +0000167
168 // Don't set this on the first request. It is set when the main load was started.
andersca2dbcee82007-03-07 19:31:55 +0000169 m_documentLoader->setRequest(newRequest);
andersca8f7934f2007-01-10 19:39:07 +0000170
171 ref(); // balanced by deref in continueAfterNavigationPolicy
172 frameLoader()->checkNavigationPolicy(newRequest, callContinueAfterNavigationPolicy, this);
173}
174
175static bool shouldLoadAsEmptyDocument(const KURL& URL)
176{
177 return URL.isEmpty() || equalIgnoringCase(String(URL.protocol()), "about");
178}
179
180void MainResourceLoader::continueAfterContentPolicy(PolicyAction contentPolicy, const ResourceResponse& r)
181{
182 KURL url = request().url();
183 const String& mimeType = r.mimeType();
184
185 switch (contentPolicy) {
186 case PolicyUse: {
187 // Prevent remote web archives from loading because they can claim to be from any domain and thus avoid cross-domain security checks (4120255).
kmcculloadfd67d2007-03-03 02:18:43 +0000188 bool isRemoteWebArchive = equalIgnoringCase("application/x-webarchive", mimeType) && !m_substituteData.isValid() && !url.isLocalFile();
andersca8f7934f2007-01-10 19:39:07 +0000189 if (!frameLoader()->canShowMIMEType(mimeType) || isRemoteWebArchive) {
190 frameLoader()->cannotShowMIMEType(r);
191 // Check reachedTerminalState since the load may have already been cancelled inside of _handleUnimplementablePolicyWithErrorCode::.
192 if (!reachedTerminalState())
193 stopLoadingForPolicyChange();
194 return;
195 }
196 break;
197 }
198
199 case PolicyDownload:
tristanee758b12007-07-17 18:27:04 +0000200 frameLoader()->client()->download(m_handle.get(), request(), m_handle.get()->request(), r);
andersca8f7934f2007-01-10 19:39:07 +0000201 receivedError(interruptionForPolicyChangeError());
202 return;
203
204 case PolicyIgnore:
205 stopLoadingForPolicyChange();
206 return;
207
208 default:
209 ASSERT_NOT_REACHED();
210 }
211
212 RefPtr<MainResourceLoader> protect(this);
213
214 if (r.isHTTP()) {
215 int status = r.httpStatusCode();
216 if (status < 200 || status >= 300) {
217 bool hostedByObject = frameLoader()->isHostedByObjectElement();
218
219 frameLoader()->handleFallbackContent();
220 // object elements are no longer rendered after we fallback, so don't
221 // keep trying to process data from their load
222
223 if (hostedByObject)
224 cancel();
225 }
226 }
227
228 // we may have cancelled this load as part of switching to fallback content
mjs2d326f52007-01-29 12:50:49 +0000229 if (!reachedTerminalState())
andersca8f7934f2007-01-10 19:39:07 +0000230 ResourceLoader::didReceiveResponse(r);
andersca8f7934f2007-01-10 19:39:07 +0000231
mjs2d326f52007-01-29 12:50:49 +0000232 if (frameLoader() && !frameLoader()->isStopping())
233 if (m_substituteData.isValid()) {
mjs71fc41d2007-02-09 10:44:41 +0000234 if (m_substituteData.content()->size())
235 didReceiveData(m_substituteData.content()->data(), m_substituteData.content()->size(), m_substituteData.content()->size(), true);
mjs2d326f52007-01-29 12:50:49 +0000236 if (frameLoader() && !frameLoader()->isStopping())
237 didFinishLoading();
238 } else if (shouldLoadAsEmptyDocument(url) || frameLoader()->representationExistsForURLScheme(url.protocol()))
239 didFinishLoading();
andersca8f7934f2007-01-10 19:39:07 +0000240}
241
242void MainResourceLoader::callContinueAfterContentPolicy(void* argument, PolicyAction policy)
243{
244 static_cast<MainResourceLoader*>(argument)->continueAfterContentPolicy(policy);
245}
246
247void MainResourceLoader::continueAfterContentPolicy(PolicyAction policy)
248{
249 ASSERT(m_waitingForContentPolicy);
250 m_waitingForContentPolicy = false;
adele31ccc452007-07-30 23:56:19 +0000251 if (frameLoader() && !frameLoader()->isStopping())
andersca8f7934f2007-01-10 19:39:07 +0000252 continueAfterContentPolicy(policy, m_response);
253 deref(); // balances ref in didReceiveResponse
254}
255
256void MainResourceLoader::didReceiveResponse(const ResourceResponse& r)
257{
258 ASSERT(shouldLoadAsEmptyDocument(r.url()) || !defersLoading());
259
260 if (m_loadingMultipartContent) {
261 frameLoader()->setupForReplaceByMIMEType(r.mimeType());
262 clearResourceData();
263 }
264
265 if (r.isMultipart())
266 m_loadingMultipartContent = true;
267
268 // The additional processing can do anything including possibly removing the last
269 // reference to this object; one example of this is 3266216.
270 RefPtr<MainResourceLoader> protect(this);
271
andersca2dbcee82007-03-07 19:31:55 +0000272 m_documentLoader->setResponse(r);
andersca8f7934f2007-01-10 19:39:07 +0000273
274 m_response = r;
275
276 ASSERT(!m_waitingForContentPolicy);
277 m_waitingForContentPolicy = true;
278 ref(); // balanced by deref in continueAfterContentPolicy and didCancel
279 frameLoader()->checkContentPolicy(m_response.mimeType(), callContinueAfterContentPolicy, this);
280}
281
282void MainResourceLoader::didReceiveData(const char* data, int length, long long lengthReceived, bool allAtOnce)
283{
284 ASSERT(data);
285 ASSERT(length != 0);
286 ASSERT(!defersLoading());
287
288 // The additional processing can do anything including possibly removing the last
289 // reference to this object; one example of this is 3266216.
290 RefPtr<MainResourceLoader> protect(this);
291
292 ResourceLoader::didReceiveData(data, length, lengthReceived, allAtOnce);
293}
294
295void MainResourceLoader::didFinishLoading()
296{
297 ASSERT(shouldLoadAsEmptyDocument(frameLoader()->URL()) || !defersLoading());
298
299 // The additional processing can do anything including possibly removing the last
300 // reference to this object.
301 RefPtr<MainResourceLoader> protect(this);
302
303 frameLoader()->finishedLoading();
304 ResourceLoader::didFinishLoading();
305}
306
307void MainResourceLoader::didFail(const ResourceError& error)
308{
309 ASSERT(!defersLoading());
310
311 receivedError(error);
312}
313
mjs2d326f52007-01-29 12:50:49 +0000314void MainResourceLoader::handleEmptyLoad(const KURL& url, bool forURLScheme)
315{
316 String mimeType;
317 if (forURLScheme)
318 mimeType = frameLoader()->generatedMIMETypeForURLScheme(url.protocol());
319 else
320 mimeType = "text/html";
321
322 ResourceResponse response(url, mimeType, 0, String(), String());
323 didReceiveResponse(response);
324}
325
mjs074dfb32007-01-31 02:40:54 +0000326void MainResourceLoader::handleDataLoadNow(Timer<MainResourceLoader>*)
mjs2d326f52007-01-29 12:50:49 +0000327{
328 RefPtr<MainResourceLoader> protect(this);
329
bdashec5a79e2007-05-04 16:42:11 +0000330 KURL url = m_substituteData.responseURL();
331 if (url.isEmpty())
332 url = m_initialRequest.url();
333
334 ResourceResponse response(url, m_substituteData.mimeType(), m_substituteData.content()->size(), m_substituteData.textEncoding(), "");
mjs2d326f52007-01-29 12:50:49 +0000335 didReceiveResponse(response);
336}
337
mjs074dfb32007-01-31 02:40:54 +0000338void MainResourceLoader::handleDataLoadSoon(ResourceRequest& r)
339{
340 m_initialRequest = r;
anderscae8946322007-06-20 00:23:48 +0000341
342 if (m_documentLoader->deferMainResourceDataLoad())
343 m_dataLoadTimer.startOneShot(0);
344 else
345 handleDataLoadNow(0);
mjs074dfb32007-01-31 02:40:54 +0000346}
347
andersca8f7934f2007-01-10 19:39:07 +0000348bool MainResourceLoader::loadNow(ResourceRequest& r)
349{
350 bool shouldLoadEmptyBeforeRedirect = shouldLoadAsEmptyDocument(r.url());
351
352 ASSERT(!m_handle);
353 ASSERT(shouldLoadEmptyBeforeRedirect || !defersLoading());
354
355 // Send this synthetic delegate callback since clients expect it, and
356 // we no longer send the callback from within NSURLConnection for
357 // initial requests.
358 willSendRequest(r, ResourceResponse());
359
360 // <rdar://problem/4801066>
361 // willSendRequest() is liable to make the call to frameLoader() return NULL, so we need to check that here
362 if (!frameLoader())
363 return false;
364
365 const KURL& url = r.url();
bdashec5a79e2007-05-04 16:42:11 +0000366 bool shouldLoadEmpty = shouldLoadAsEmptyDocument(url) && !m_substituteData.isValid();
andersca8f7934f2007-01-10 19:39:07 +0000367
368 if (shouldLoadEmptyBeforeRedirect && !shouldLoadEmpty && defersLoading())
369 return true;
370
anderscae8946322007-06-20 00:23:48 +0000371 if (m_substituteData.isValid())
mjs074dfb32007-01-31 02:40:54 +0000372 handleDataLoadSoon(r);
mjs2d326f52007-01-29 12:50:49 +0000373 else if (shouldLoadEmpty || frameLoader()->representationExistsForURLScheme(url.protocol()))
374 handleEmptyLoad(url, !shouldLoadEmpty);
375 else
andersca14a02542007-01-30 22:04:28 +0000376 m_handle = ResourceHandle::create(r, this, m_frame.get(), false, true);
andersca8f7934f2007-01-10 19:39:07 +0000377
378 return false;
379}
380
mjs2d326f52007-01-29 12:50:49 +0000381bool MainResourceLoader::load(const ResourceRequest& r, const SubstituteData& substituteData)
andersca8f7934f2007-01-10 19:39:07 +0000382{
383 ASSERT(!m_handle);
384
mjs2d326f52007-01-29 12:50:49 +0000385 m_substituteData = substituteData;
386
andersca8f7934f2007-01-10 19:39:07 +0000387 ResourceRequest request(r);
388 bool defer = defersLoading();
389 if (defer) {
390 bool shouldLoadEmpty = shouldLoadAsEmptyDocument(r.url());
391 if (shouldLoadEmpty)
392 defer = false;
393 }
394 if (!defer) {
395 if (loadNow(request)) {
396 // Started as an empty document, but was redirected to something non-empty.
397 ASSERT(defersLoading());
398 defer = true;
399 }
400 }
mjs2d326f52007-01-29 12:50:49 +0000401 if (defer)
andersca8f7934f2007-01-10 19:39:07 +0000402 m_initialRequest = request;
403
404 return true;
405}
406
407void MainResourceLoader::setDefersLoading(bool defers)
408{
409 ResourceLoader::setDefersLoading(defers);
andersca14cd9122007-03-08 07:15:04 +0000410
411 if (defers) {
412 if (m_dataLoadTimer.isActive())
413 m_dataLoadTimer.stop();
414 } else {
415 if (m_initialRequest.isNull())
416 return;
417
anderscae8946322007-06-20 00:23:48 +0000418 if (m_substituteData.isValid() &&
419 m_documentLoader->deferMainResourceDataLoad())
420 m_dataLoadTimer.startOneShot(0);
andersca14cd9122007-03-08 07:15:04 +0000421 else {
andersca8f7934f2007-01-10 19:39:07 +0000422 ResourceRequest r(m_initialRequest);
423 m_initialRequest = ResourceRequest();
424 loadNow(r);
425 }
426 }
427}
428
darin9ab4ef22006-10-13 17:07:20 +0000429}