blob: 159aaa1f58b03151c4e12290eb9dca4b62ba6543 [file] [log] [blame]
mjs95611582006-10-09 09:53:40 +00001/*
darin@apple.com54d0e292009-03-29 23:29:24 +00002 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
zimmermann@webkit.orge6128472008-12-22 02:22:10 +00003 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
mjs95611582006-10-09 09:53:40 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
darin27bd6622006-10-30 01:16:42 +000030#include "config.h"
31#include "MainResourceLoader.h"
mjs95611582006-10-09 09:53:40 +000032
andersca2dbcee82007-03-07 19:31:55 +000033#include "DocumentLoader.h"
darin@apple.com54d0e292009-03-29 23:29:24 +000034#include "FormState.h"
andersca8f7934f2007-01-10 19:39:07 +000035#include "Frame.h"
36#include "FrameLoader.h"
37#include "FrameLoaderClient.h"
38#include "HTMLFormElement.h"
andersca@apple.com41e5f292008-05-06 22:15:52 +000039#include "Page.h"
andersca8f7934f2007-01-10 19:39:07 +000040#include "ResourceError.h"
41#include "ResourceHandle.h"
andersca@apple.comc3a561b2008-04-25 23:36:19 +000042#include "Settings.h"
andersca8f7934f2007-01-10 19:39:07 +000043
darin@apple.com54d0e292009-03-29 23:29:24 +000044#if ENABLE(OFFLINE_WEB_APPLICATIONS)
darin@chromium.org6042aea2009-07-30 23:08:33 +000045#include "ApplicationCacheHost.h"
darin@apple.com54d0e292009-03-29 23:29:24 +000046#endif
47
andersca8f7934f2007-01-10 19:39:07 +000048// FIXME: More that is in common with SubresourceLoader should move up into ResourceLoader.
49
darin9ab4ef22006-10-13 17:07:20 +000050namespace WebCore {
mjs95611582006-10-09 09:53:40 +000051
andersca8f7934f2007-01-10 19:39:07 +000052MainResourceLoader::MainResourceLoader(Frame* frame)
andersca2d985c72007-08-28 19:05:56 +000053 : ResourceLoader(frame, true, true)
mjs074dfb32007-01-31 02:40:54 +000054 , m_dataLoadTimer(this, &MainResourceLoader::handleDataLoadNow)
andersca8f7934f2007-01-10 19:39:07 +000055 , m_loadingMultipartContent(false)
56 , m_waitingForContentPolicy(false)
57{
58}
59
60MainResourceLoader::~MainResourceLoader()
61{
62}
63
64PassRefPtr<MainResourceLoader> MainResourceLoader::create(Frame* frame)
65{
andersca@apple.com27b46302008-02-19 18:25:56 +000066 return adoptRef(new MainResourceLoader(frame));
andersca8f7934f2007-01-10 19:39:07 +000067}
68
69void MainResourceLoader::receivedError(const ResourceError& error)
70{
71 // Calling receivedMainResourceError will likely result in the last reference to this object to go away.
72 RefPtr<MainResourceLoader> protect(this);
mjs8d620d52007-05-10 12:31:42 +000073 RefPtr<Frame> protectFrame(m_frame);
andersca5d5f8022007-02-28 00:38:00 +000074
andersca@apple.com3b145bf2008-01-10 21:51:40 +000075 // It is important that we call FrameLoader::receivedMainResourceError before calling
76 // FrameLoader::didFailToLoad because receivedMainResourceError clears out the relevant
77 // document loaders. Also, receivedMainResourceError ends up calling a FrameLoadDelegate method
78 // and didFailToLoad calls a ResourceLoadDelegate method and they need to be in the correct order.
79 frameLoader()->receivedMainResourceError(error, true);
80
andersca8f7934f2007-01-10 19:39:07 +000081 if (!cancelled()) {
82 ASSERT(!reachedTerminalState());
83 frameLoader()->didFailToLoad(this, error);
andersca@apple.com3b145bf2008-01-10 21:51:40 +000084
mjs8d620d52007-05-10 12:31:42 +000085 releaseResources();
86 }
87
andersca8f7934f2007-01-10 19:39:07 +000088 ASSERT(reachedTerminalState());
89}
90
91void MainResourceLoader::didCancel(const ResourceError& error)
92{
mjs074dfb32007-01-31 02:40:54 +000093 m_dataLoadTimer.stop();
94
andersca8f7934f2007-01-10 19:39:07 +000095 // Calling receivedMainResourceError will likely result in the last reference to this object to go away.
96 RefPtr<MainResourceLoader> protect(this);
97
98 if (m_waitingForContentPolicy) {
99 frameLoader()->cancelContentPolicyCheck();
100 ASSERT(m_waitingForContentPolicy);
101 m_waitingForContentPolicy = false;
102 deref(); // balances ref in didReceiveResponse
103 }
104 frameLoader()->receivedMainResourceError(error, true);
105 ResourceLoader::didCancel(error);
106}
107
108ResourceError MainResourceLoader::interruptionForPolicyChangeError() const
109{
110 return frameLoader()->interruptionForPolicyChangeError(request());
111}
112
113void MainResourceLoader::stopLoadingForPolicyChange()
114{
115 cancel(interruptionForPolicyChangeError());
116}
117
darin@apple.com76421a52008-01-16 08:02:07 +0000118void MainResourceLoader::callContinueAfterNavigationPolicy(void* argument, const ResourceRequest& request, PassRefPtr<FormState>, bool shouldContinue)
119{
120 static_cast<MainResourceLoader*>(argument)->continueAfterNavigationPolicy(request, shouldContinue);
121}
122
darin@apple.com98a7ac62009-01-05 17:26:53 +0000123void MainResourceLoader::continueAfterNavigationPolicy(const ResourceRequest&, bool shouldContinue)
darin@apple.com76421a52008-01-16 08:02:07 +0000124{
125 if (!shouldContinue)
126 stopLoadingForPolicyChange();
127 deref(); // balances ref in willSendRequest
128}
129
andersca8f7934f2007-01-10 19:39:07 +0000130bool MainResourceLoader::isPostOrRedirectAfterPost(const ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
131{
132 if (newRequest.httpMethod() == "POST")
133 return true;
134
135 int status = redirectResponse.httpStatusCode();
136 if (((status >= 301 && status <= 303) || status == 307)
137 && frameLoader()->initialRequest().httpMethod() == "POST")
138 return true;
139
140 return false;
141}
142
143void MainResourceLoader::addData(const char* data, int length, bool allAtOnce)
144{
145 ResourceLoader::addData(data, length, allAtOnce);
146 frameLoader()->receivedData(data, length);
147}
148
149void MainResourceLoader::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
150{
151 // Note that there are no asserts here as there are for the other callbacks. This is due to the
152 // fact that this "callback" is sent when starting every load, and the state of callback
153 // deferrals plays less of a part in this function in preventing the bad behavior deferring
154 // callbacks is meant to prevent.
155 ASSERT(!newRequest.isNull());
156
157 // The additional processing can do anything including possibly removing the last
158 // reference to this object; one example of this is 3266216.
159 RefPtr<MainResourceLoader> protect(this);
160
161 // Update cookie policy base URL as URL changes, except for subframes, which use the
162 // URL of the main frame which doesn't change when we redirect.
163 if (frameLoader()->isLoadingMainFrame())
abarth@webkit.org622d9df2009-05-22 16:44:47 +0000164 newRequest.setFirstPartyForCookies(newRequest.url());
andersca8f7934f2007-01-10 19:39:07 +0000165
166 // If we're fielding a redirect in response to a POST, force a load from origin, since
167 // this is a common site technique to return to a page viewing some data that the POST
168 // just modified.
169 // Also, POST requests always load from origin, but this does not affect subresources.
170 if (newRequest.cachePolicy() == UseProtocolCachePolicy && isPostOrRedirectAfterPost(newRequest, redirectResponse))
171 newRequest.setCachePolicy(ReloadIgnoringCacheData);
172
andersca@apple.com7df6f452008-03-21 01:07:22 +0000173 ResourceLoader::willSendRequest(newRequest, redirectResponse);
andersca8f7934f2007-01-10 19:39:07 +0000174
175 // Don't set this on the first request. It is set when the main load was started.
andersca2dbcee82007-03-07 19:31:55 +0000176 m_documentLoader->setRequest(newRequest);
darin@apple.com76421a52008-01-16 08:02:07 +0000177
178 // FIXME: Ideally we'd stop the I/O until we hear back from the navigation policy delegate
179 // listener. But there's no way to do that in practice. So instead we cancel later if the
180 // listener tells us to. In practice that means the navigation policy needs to be decided
181 // synchronously for these redirect cases.
beidson@apple.com79b68bae2009-04-23 06:42:52 +0000182 if (!redirectResponse.isNull()) {
183 ref(); // balanced by deref in continueAfterNavigationPolicy
184 frameLoader()->checkNavigationPolicy(newRequest, callContinueAfterNavigationPolicy, this);
185 }
andersca8f7934f2007-01-10 19:39:07 +0000186}
187
weinig@apple.comc5002662007-12-12 07:26:19 +0000188static bool shouldLoadAsEmptyDocument(const KURL& url)
andersca8f7934f2007-01-10 19:39:07 +0000189{
staikos@webkit.org89e134572009-02-02 21:48:30 +0000190#if PLATFORM(TORCHMOBILE)
191 return url.isEmpty() || (url.protocolIs("about") && equalIgnoringRef(url, blankURL()));
192#else
193 return url.isEmpty() || url.protocolIs("about");
194#endif
andersca8f7934f2007-01-10 19:39:07 +0000195}
196
197void MainResourceLoader::continueAfterContentPolicy(PolicyAction contentPolicy, const ResourceResponse& r)
198{
199 KURL url = request().url();
200 const String& mimeType = r.mimeType();
201
202 switch (contentPolicy) {
203 case PolicyUse: {
204 // 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 +0000205 bool isRemoteWebArchive = equalIgnoringCase("application/x-webarchive", mimeType) && !m_substituteData.isValid() && !url.isLocalFile();
andersca8f7934f2007-01-10 19:39:07 +0000206 if (!frameLoader()->canShowMIMEType(mimeType) || isRemoteWebArchive) {
207 frameLoader()->cannotShowMIMEType(r);
208 // Check reachedTerminalState since the load may have already been cancelled inside of _handleUnimplementablePolicyWithErrorCode::.
209 if (!reachedTerminalState())
210 stopLoadingForPolicyChange();
211 return;
212 }
213 break;
214 }
215
216 case PolicyDownload:
ap@webkit.org7ec52802009-01-08 17:05:45 +0000217 // m_handle can be null, e.g. when loading a substitute resource from application cache.
218 if (!m_handle) {
219 receivedError(cannotShowURLError());
220 return;
221 }
tristanee758b12007-07-17 18:27:04 +0000222 frameLoader()->client()->download(m_handle.get(), request(), m_handle.get()->request(), r);
hausmann@webkit.org903314a2008-04-29 14:41:55 +0000223 // It might have gone missing
224 if (frameLoader())
225 receivedError(interruptionForPolicyChangeError());
andersca8f7934f2007-01-10 19:39:07 +0000226 return;
227
228 case PolicyIgnore:
229 stopLoadingForPolicyChange();
230 return;
231
232 default:
233 ASSERT_NOT_REACHED();
234 }
235
236 RefPtr<MainResourceLoader> protect(this);
237
238 if (r.isHTTP()) {
239 int status = r.httpStatusCode();
240 if (status < 200 || status >= 300) {
241 bool hostedByObject = frameLoader()->isHostedByObjectElement();
242
243 frameLoader()->handleFallbackContent();
244 // object elements are no longer rendered after we fallback, so don't
245 // keep trying to process data from their load
246
247 if (hostedByObject)
248 cancel();
249 }
250 }
251
252 // we may have cancelled this load as part of switching to fallback content
mjs2d326f52007-01-29 12:50:49 +0000253 if (!reachedTerminalState())
andersca8f7934f2007-01-10 19:39:07 +0000254 ResourceLoader::didReceiveResponse(r);
andersca8f7934f2007-01-10 19:39:07 +0000255
staikos@webkit.org478595b2009-03-25 13:01:45 +0000256 if (frameLoader() && !frameLoader()->isStopping()) {
mjs2d326f52007-01-29 12:50:49 +0000257 if (m_substituteData.isValid()) {
mjs71fc41d2007-02-09 10:44:41 +0000258 if (m_substituteData.content()->size())
259 didReceiveData(m_substituteData.content()->data(), m_substituteData.content()->size(), m_substituteData.content()->size(), true);
mjs2d326f52007-01-29 12:50:49 +0000260 if (frameLoader() && !frameLoader()->isStopping())
261 didFinishLoading();
262 } else if (shouldLoadAsEmptyDocument(url) || frameLoader()->representationExistsForURLScheme(url.protocol()))
263 didFinishLoading();
staikos@webkit.org478595b2009-03-25 13:01:45 +0000264 }
andersca8f7934f2007-01-10 19:39:07 +0000265}
266
267void MainResourceLoader::callContinueAfterContentPolicy(void* argument, PolicyAction policy)
268{
269 static_cast<MainResourceLoader*>(argument)->continueAfterContentPolicy(policy);
270}
271
272void MainResourceLoader::continueAfterContentPolicy(PolicyAction policy)
273{
274 ASSERT(m_waitingForContentPolicy);
275 m_waitingForContentPolicy = false;
adele31ccc452007-07-30 23:56:19 +0000276 if (frameLoader() && !frameLoader()->isStopping())
andersca8f7934f2007-01-10 19:39:07 +0000277 continueAfterContentPolicy(policy, m_response);
278 deref(); // balances ref in didReceiveResponse
279}
280
281void MainResourceLoader::didReceiveResponse(const ResourceResponse& r)
282{
ap@webkit.orgfb96f8a2009-01-02 09:25:02 +0000283#if ENABLE(OFFLINE_WEB_APPLICATIONS)
darin@chromium.org6042aea2009-07-30 23:08:33 +0000284 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForMainResponse(request(), r))
285 return;
ap@webkit.orgfb96f8a2009-01-02 09:25:02 +0000286#endif
287
weinig@apple.com5af461c2009-04-08 22:16:23 +0000288 HTTPHeaderMap::const_iterator it = r.httpHeaderFields().find(AtomicString("x-frame-options"));
289 if (it != r.httpHeaderFields().end()) {
290 String content = it->second;
291 if (m_frame->loader()->shouldInterruptLoadForXFrameOptions(content, r.url())) {
292 cancel();
293 return;
294 }
295 }
296
andersca@apple.com74021572008-10-21 01:05:19 +0000297 // There is a bug in CFNetwork where callbacks can be dispatched even when loads are deferred.
298 // See <rdar://problem/6304600> for more details.
299#if !PLATFORM(CF)
andersca8f7934f2007-01-10 19:39:07 +0000300 ASSERT(shouldLoadAsEmptyDocument(r.url()) || !defersLoading());
andersca@apple.com74021572008-10-21 01:05:19 +0000301#endif
andersca8f7934f2007-01-10 19:39:07 +0000302
303 if (m_loadingMultipartContent) {
304 frameLoader()->setupForReplaceByMIMEType(r.mimeType());
305 clearResourceData();
306 }
307
308 if (r.isMultipart())
309 m_loadingMultipartContent = true;
310
311 // The additional processing can do anything including possibly removing the last
312 // reference to this object; one example of this is 3266216.
313 RefPtr<MainResourceLoader> protect(this);
314
andersca2dbcee82007-03-07 19:31:55 +0000315 m_documentLoader->setResponse(r);
andersca8f7934f2007-01-10 19:39:07 +0000316
317 m_response = r;
318
319 ASSERT(!m_waitingForContentPolicy);
320 m_waitingForContentPolicy = true;
321 ref(); // balanced by deref in continueAfterContentPolicy and didCancel
322 frameLoader()->checkContentPolicy(m_response.mimeType(), callContinueAfterContentPolicy, this);
323}
324
325void MainResourceLoader::didReceiveData(const char* data, int length, long long lengthReceived, bool allAtOnce)
326{
327 ASSERT(data);
328 ASSERT(length != 0);
andersca@apple.com74021572008-10-21 01:05:19 +0000329
beidson@apple.com1fbfab92009-02-07 01:26:54 +0000330 ASSERT(!m_response.isNull());
331
332#if USE(CFNETWORK) || (PLATFORM(MAC) && !defined(BUILDING_ON_TIGER))
333 // Workaround for <rdar://problem/6060782>
334 if (m_response.isNull()) {
335 m_response = ResourceResponse(KURL(), "text/html", 0, String(), String());
336 if (DocumentLoader* documentLoader = frameLoader()->activeDocumentLoader())
337 documentLoader->setResponse(m_response);
338 }
339#endif
340
andersca@apple.com74021572008-10-21 01:05:19 +0000341 // There is a bug in CFNetwork where callbacks can be dispatched even when loads are deferred.
342 // See <rdar://problem/6304600> for more details.
343#if !PLATFORM(CF)
andersca8f7934f2007-01-10 19:39:07 +0000344 ASSERT(!defersLoading());
andersca@apple.com74021572008-10-21 01:05:19 +0000345#endif
andersca8f7934f2007-01-10 19:39:07 +0000346
darin@chromium.org6042aea2009-07-30 23:08:33 +0000347 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
348 documentLoader()->applicationCacheHost()->mainResourceDataReceived(data, length, lengthReceived, allAtOnce);
349#endif
350
andersca8f7934f2007-01-10 19:39:07 +0000351 // The additional processing can do anything including possibly removing the last
352 // reference to this object; one example of this is 3266216.
353 RefPtr<MainResourceLoader> protect(this);
354
355 ResourceLoader::didReceiveData(data, length, lengthReceived, allAtOnce);
356}
357
358void MainResourceLoader::didFinishLoading()
359{
andersca@apple.com74021572008-10-21 01:05:19 +0000360 // There is a bug in CFNetwork where callbacks can be dispatched even when loads are deferred.
361 // See <rdar://problem/6304600> for more details.
362#if !PLATFORM(CF)
weinig@apple.comc5002662007-12-12 07:26:19 +0000363 ASSERT(shouldLoadAsEmptyDocument(frameLoader()->activeDocumentLoader()->url()) || !defersLoading());
andersca@apple.com74021572008-10-21 01:05:19 +0000364#endif
365
andersca8f7934f2007-01-10 19:39:07 +0000366 // The additional processing can do anything including possibly removing the last
367 // reference to this object.
368 RefPtr<MainResourceLoader> protect(this);
369
andersca@apple.com875b8652008-04-21 18:21:28 +0000370#if ENABLE(OFFLINE_WEB_APPLICATIONS)
371 RefPtr<DocumentLoader> dl = documentLoader();
372#endif
373
andersca8f7934f2007-01-10 19:39:07 +0000374 frameLoader()->finishedLoading();
375 ResourceLoader::didFinishLoading();
andersca@apple.com875b8652008-04-21 18:21:28 +0000376
377#if ENABLE(OFFLINE_WEB_APPLICATIONS)
darin@chromium.org6042aea2009-07-30 23:08:33 +0000378 dl->applicationCacheHost()->finishedLoadingMainResource();
andersca@apple.com875b8652008-04-21 18:21:28 +0000379#endif
andersca8f7934f2007-01-10 19:39:07 +0000380}
381
382void MainResourceLoader::didFail(const ResourceError& error)
383{
ap@webkit.orgfb96f8a2009-01-02 09:25:02 +0000384#if ENABLE(OFFLINE_WEB_APPLICATIONS)
darin@chromium.org6042aea2009-07-30 23:08:33 +0000385 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForMainError(request(), error))
386 return;
ap@webkit.orgfb96f8a2009-01-02 09:25:02 +0000387#endif
388
andersca@apple.com74021572008-10-21 01:05:19 +0000389 // There is a bug in CFNetwork where callbacks can be dispatched even when loads are deferred.
390 // See <rdar://problem/6304600> for more details.
391#if !PLATFORM(CF)
andersca8f7934f2007-01-10 19:39:07 +0000392 ASSERT(!defersLoading());
andersca@apple.com74021572008-10-21 01:05:19 +0000393#endif
394
andersca8f7934f2007-01-10 19:39:07 +0000395 receivedError(error);
396}
397
mjs2d326f52007-01-29 12:50:49 +0000398void MainResourceLoader::handleEmptyLoad(const KURL& url, bool forURLScheme)
399{
400 String mimeType;
401 if (forURLScheme)
402 mimeType = frameLoader()->generatedMIMETypeForURLScheme(url.protocol());
403 else
404 mimeType = "text/html";
405
406 ResourceResponse response(url, mimeType, 0, String(), String());
407 didReceiveResponse(response);
408}
409
timothy@apple.com67a028b2009-03-19 23:45:57 +0000410void MainResourceLoader::handleDataLoadNow(MainResourceLoaderTimer*)
mjs2d326f52007-01-29 12:50:49 +0000411{
412 RefPtr<MainResourceLoader> protect(this);
413
bdashec5a79e2007-05-04 16:42:11 +0000414 KURL url = m_substituteData.responseURL();
415 if (url.isEmpty())
416 url = m_initialRequest.url();
417
418 ResourceResponse response(url, m_substituteData.mimeType(), m_substituteData.content()->size(), m_substituteData.textEncoding(), "");
mjs2d326f52007-01-29 12:50:49 +0000419 didReceiveResponse(response);
420}
421
timothy@apple.com67a028b2009-03-19 23:45:57 +0000422void MainResourceLoader::startDataLoadTimer()
423{
424 m_dataLoadTimer.startOneShot(0);
425
426#if HAVE(RUNLOOP_TIMER)
427 if (SchedulePairHashSet* scheduledPairs = m_frame->page()->scheduledRunLoopPairs())
428 m_dataLoadTimer.schedule(*scheduledPairs);
429#endif
430}
431
mjs074dfb32007-01-31 02:40:54 +0000432void MainResourceLoader::handleDataLoadSoon(ResourceRequest& r)
433{
434 m_initialRequest = r;
anderscae8946322007-06-20 00:23:48 +0000435
436 if (m_documentLoader->deferMainResourceDataLoad())
timothy@apple.com67a028b2009-03-19 23:45:57 +0000437 startDataLoadTimer();
anderscae8946322007-06-20 00:23:48 +0000438 else
439 handleDataLoadNow(0);
mjs074dfb32007-01-31 02:40:54 +0000440}
441
andersca8f7934f2007-01-10 19:39:07 +0000442bool MainResourceLoader::loadNow(ResourceRequest& r)
443{
444 bool shouldLoadEmptyBeforeRedirect = shouldLoadAsEmptyDocument(r.url());
445
446 ASSERT(!m_handle);
447 ASSERT(shouldLoadEmptyBeforeRedirect || !defersLoading());
448
449 // Send this synthetic delegate callback since clients expect it, and
450 // we no longer send the callback from within NSURLConnection for
451 // initial requests.
452 willSendRequest(r, ResourceResponse());
453
454 // <rdar://problem/4801066>
455 // willSendRequest() is liable to make the call to frameLoader() return NULL, so we need to check that here
456 if (!frameLoader())
457 return false;
458
459 const KURL& url = r.url();
bdashec5a79e2007-05-04 16:42:11 +0000460 bool shouldLoadEmpty = shouldLoadAsEmptyDocument(url) && !m_substituteData.isValid();
andersca8f7934f2007-01-10 19:39:07 +0000461
462 if (shouldLoadEmptyBeforeRedirect && !shouldLoadEmpty && defersLoading())
463 return true;
464
anderscae8946322007-06-20 00:23:48 +0000465 if (m_substituteData.isValid())
mjs074dfb32007-01-31 02:40:54 +0000466 handleDataLoadSoon(r);
mjs2d326f52007-01-29 12:50:49 +0000467 else if (shouldLoadEmpty || frameLoader()->representationExistsForURLScheme(url.protocol()))
468 handleEmptyLoad(url, !shouldLoadEmpty);
469 else
andersca2d985c72007-08-28 19:05:56 +0000470 m_handle = ResourceHandle::create(r, this, m_frame.get(), false, true, true);
andersca8f7934f2007-01-10 19:39:07 +0000471
472 return false;
473}
474
andersca@apple.com875b8652008-04-21 18:21:28 +0000475bool MainResourceLoader::load(const ResourceRequest& r, const SubstituteData& substituteData)
andersca8f7934f2007-01-10 19:39:07 +0000476{
477 ASSERT(!m_handle);
478
mjs2d326f52007-01-29 12:50:49 +0000479 m_substituteData = substituteData;
480
darin@chromium.org6042aea2009-07-30 23:08:33 +0000481 ResourceRequest request(r);
482
andersca@apple.com875b8652008-04-21 18:21:28 +0000483#if ENABLE(OFFLINE_WEB_APPLICATIONS)
darin@chromium.org6042aea2009-07-30 23:08:33 +0000484 documentLoader()->applicationCacheHost()->maybeLoadMainResource(request, m_substituteData);
andersca@apple.com875b8652008-04-21 18:21:28 +0000485#endif
486
andersca8f7934f2007-01-10 19:39:07 +0000487 bool defer = defersLoading();
488 if (defer) {
darin@chromium.org6042aea2009-07-30 23:08:33 +0000489 bool shouldLoadEmpty = shouldLoadAsEmptyDocument(request.url());
andersca8f7934f2007-01-10 19:39:07 +0000490 if (shouldLoadEmpty)
491 defer = false;
492 }
493 if (!defer) {
494 if (loadNow(request)) {
495 // Started as an empty document, but was redirected to something non-empty.
496 ASSERT(defersLoading());
497 defer = true;
498 }
499 }
mjs2d326f52007-01-29 12:50:49 +0000500 if (defer)
andersca8f7934f2007-01-10 19:39:07 +0000501 m_initialRequest = request;
502
503 return true;
504}
505
506void MainResourceLoader::setDefersLoading(bool defers)
507{
508 ResourceLoader::setDefersLoading(defers);
timothy@apple.com67a028b2009-03-19 23:45:57 +0000509
andersca14cd9122007-03-08 07:15:04 +0000510 if (defers) {
511 if (m_dataLoadTimer.isActive())
512 m_dataLoadTimer.stop();
513 } else {
514 if (m_initialRequest.isNull())
515 return;
timothy@apple.com67a028b2009-03-19 23:45:57 +0000516
517 if (m_substituteData.isValid() && m_documentLoader->deferMainResourceDataLoad())
518 startDataLoadTimer();
andersca14cd9122007-03-08 07:15:04 +0000519 else {
andersca8f7934f2007-01-10 19:39:07 +0000520 ResourceRequest r(m_initialRequest);
521 m_initialRequest = ResourceRequest();
522 loadNow(r);
523 }
524 }
525}
526
darin9ab4ef22006-10-13 17:07:20 +0000527}