mjs | 9561158 | 2006-10-09 09:53:40 +0000 | [diff] [blame] | 1 | /* |
darin@apple.com | 54d0e29 | 2009-03-29 23:29:24 +0000 | [diff] [blame] | 2 | * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
zimmermann@webkit.org | e612847 | 2008-12-22 02:22:10 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) |
mjs | 9561158 | 2006-10-09 09:53:40 +0000 | [diff] [blame] | 4 | * |
| 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 | |
darin | 27bd662 | 2006-10-30 01:16:42 +0000 | [diff] [blame] | 30 | #include "config.h" |
| 31 | #include "MainResourceLoader.h" |
mjs | 9561158 | 2006-10-09 09:53:40 +0000 | [diff] [blame] | 32 | |
andersca | 2dbcee8 | 2007-03-07 19:31:55 +0000 | [diff] [blame] | 33 | #include "DocumentLoader.h" |
darin@apple.com | 54d0e29 | 2009-03-29 23:29:24 +0000 | [diff] [blame] | 34 | #include "FormState.h" |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 35 | #include "Frame.h" |
| 36 | #include "FrameLoader.h" |
| 37 | #include "FrameLoaderClient.h" |
| 38 | #include "HTMLFormElement.h" |
andersca@apple.com | 41e5f29 | 2008-05-06 22:15:52 +0000 | [diff] [blame] | 39 | #include "Page.h" |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 40 | #include "ResourceError.h" |
| 41 | #include "ResourceHandle.h" |
andersca@apple.com | c3a561b | 2008-04-25 23:36:19 +0000 | [diff] [blame] | 42 | #include "Settings.h" |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 43 | |
darin@apple.com | 54d0e29 | 2009-03-29 23:29:24 +0000 | [diff] [blame] | 44 | #if ENABLE(OFFLINE_WEB_APPLICATIONS) |
darin@chromium.org | 6042aea | 2009-07-30 23:08:33 +0000 | [diff] [blame^] | 45 | #include "ApplicationCacheHost.h" |
darin@apple.com | 54d0e29 | 2009-03-29 23:29:24 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 48 | // FIXME: More that is in common with SubresourceLoader should move up into ResourceLoader. |
| 49 | |
darin | 9ab4ef2 | 2006-10-13 17:07:20 +0000 | [diff] [blame] | 50 | namespace WebCore { |
mjs | 9561158 | 2006-10-09 09:53:40 +0000 | [diff] [blame] | 51 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 52 | MainResourceLoader::MainResourceLoader(Frame* frame) |
andersca | 2d985c7 | 2007-08-28 19:05:56 +0000 | [diff] [blame] | 53 | : ResourceLoader(frame, true, true) |
mjs | 074dfb3 | 2007-01-31 02:40:54 +0000 | [diff] [blame] | 54 | , m_dataLoadTimer(this, &MainResourceLoader::handleDataLoadNow) |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 55 | , m_loadingMultipartContent(false) |
| 56 | , m_waitingForContentPolicy(false) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | MainResourceLoader::~MainResourceLoader() |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | PassRefPtr<MainResourceLoader> MainResourceLoader::create(Frame* frame) |
| 65 | { |
andersca@apple.com | 27b4630 | 2008-02-19 18:25:56 +0000 | [diff] [blame] | 66 | return adoptRef(new MainResourceLoader(frame)); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void 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); |
mjs | 8d620d5 | 2007-05-10 12:31:42 +0000 | [diff] [blame] | 73 | RefPtr<Frame> protectFrame(m_frame); |
andersca | 5d5f802 | 2007-02-28 00:38:00 +0000 | [diff] [blame] | 74 | |
andersca@apple.com | 3b145bf | 2008-01-10 21:51:40 +0000 | [diff] [blame] | 75 | // 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 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 81 | if (!cancelled()) { |
| 82 | ASSERT(!reachedTerminalState()); |
| 83 | frameLoader()->didFailToLoad(this, error); |
andersca@apple.com | 3b145bf | 2008-01-10 21:51:40 +0000 | [diff] [blame] | 84 | |
mjs | 8d620d5 | 2007-05-10 12:31:42 +0000 | [diff] [blame] | 85 | releaseResources(); |
| 86 | } |
| 87 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 88 | ASSERT(reachedTerminalState()); |
| 89 | } |
| 90 | |
| 91 | void MainResourceLoader::didCancel(const ResourceError& error) |
| 92 | { |
mjs | 074dfb3 | 2007-01-31 02:40:54 +0000 | [diff] [blame] | 93 | m_dataLoadTimer.stop(); |
| 94 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 95 | // 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 | |
| 108 | ResourceError MainResourceLoader::interruptionForPolicyChangeError() const |
| 109 | { |
| 110 | return frameLoader()->interruptionForPolicyChangeError(request()); |
| 111 | } |
| 112 | |
| 113 | void MainResourceLoader::stopLoadingForPolicyChange() |
| 114 | { |
| 115 | cancel(interruptionForPolicyChangeError()); |
| 116 | } |
| 117 | |
darin@apple.com | 76421a5 | 2008-01-16 08:02:07 +0000 | [diff] [blame] | 118 | void MainResourceLoader::callContinueAfterNavigationPolicy(void* argument, const ResourceRequest& request, PassRefPtr<FormState>, bool shouldContinue) |
| 119 | { |
| 120 | static_cast<MainResourceLoader*>(argument)->continueAfterNavigationPolicy(request, shouldContinue); |
| 121 | } |
| 122 | |
darin@apple.com | 98a7ac6 | 2009-01-05 17:26:53 +0000 | [diff] [blame] | 123 | void MainResourceLoader::continueAfterNavigationPolicy(const ResourceRequest&, bool shouldContinue) |
darin@apple.com | 76421a5 | 2008-01-16 08:02:07 +0000 | [diff] [blame] | 124 | { |
| 125 | if (!shouldContinue) |
| 126 | stopLoadingForPolicyChange(); |
| 127 | deref(); // balances ref in willSendRequest |
| 128 | } |
| 129 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 130 | bool 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 | |
| 143 | void MainResourceLoader::addData(const char* data, int length, bool allAtOnce) |
| 144 | { |
| 145 | ResourceLoader::addData(data, length, allAtOnce); |
| 146 | frameLoader()->receivedData(data, length); |
| 147 | } |
| 148 | |
| 149 | void 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.org | 622d9df | 2009-05-22 16:44:47 +0000 | [diff] [blame] | 164 | newRequest.setFirstPartyForCookies(newRequest.url()); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 165 | |
| 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.com | 7df6f45 | 2008-03-21 01:07:22 +0000 | [diff] [blame] | 173 | ResourceLoader::willSendRequest(newRequest, redirectResponse); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 174 | |
| 175 | // Don't set this on the first request. It is set when the main load was started. |
andersca | 2dbcee8 | 2007-03-07 19:31:55 +0000 | [diff] [blame] | 176 | m_documentLoader->setRequest(newRequest); |
darin@apple.com | 76421a5 | 2008-01-16 08:02:07 +0000 | [diff] [blame] | 177 | |
| 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.com | 79b68bae | 2009-04-23 06:42:52 +0000 | [diff] [blame] | 182 | if (!redirectResponse.isNull()) { |
| 183 | ref(); // balanced by deref in continueAfterNavigationPolicy |
| 184 | frameLoader()->checkNavigationPolicy(newRequest, callContinueAfterNavigationPolicy, this); |
| 185 | } |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 186 | } |
| 187 | |
weinig@apple.com | c500266 | 2007-12-12 07:26:19 +0000 | [diff] [blame] | 188 | static bool shouldLoadAsEmptyDocument(const KURL& url) |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 189 | { |
staikos@webkit.org | 89e13457 | 2009-02-02 21:48:30 +0000 | [diff] [blame] | 190 | #if PLATFORM(TORCHMOBILE) |
| 191 | return url.isEmpty() || (url.protocolIs("about") && equalIgnoringRef(url, blankURL())); |
| 192 | #else |
| 193 | return url.isEmpty() || url.protocolIs("about"); |
| 194 | #endif |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void 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). |
kmccullo | adfd67d | 2007-03-03 02:18:43 +0000 | [diff] [blame] | 205 | bool isRemoteWebArchive = equalIgnoringCase("application/x-webarchive", mimeType) && !m_substituteData.isValid() && !url.isLocalFile(); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 206 | 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.org | 7ec5280 | 2009-01-08 17:05:45 +0000 | [diff] [blame] | 217 | // 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 | } |
tristan | ee758b1 | 2007-07-17 18:27:04 +0000 | [diff] [blame] | 222 | frameLoader()->client()->download(m_handle.get(), request(), m_handle.get()->request(), r); |
hausmann@webkit.org | 903314a | 2008-04-29 14:41:55 +0000 | [diff] [blame] | 223 | // It might have gone missing |
| 224 | if (frameLoader()) |
| 225 | receivedError(interruptionForPolicyChangeError()); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 226 | 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 |
mjs | 2d326f5 | 2007-01-29 12:50:49 +0000 | [diff] [blame] | 253 | if (!reachedTerminalState()) |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 254 | ResourceLoader::didReceiveResponse(r); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 255 | |
staikos@webkit.org | 478595b | 2009-03-25 13:01:45 +0000 | [diff] [blame] | 256 | if (frameLoader() && !frameLoader()->isStopping()) { |
mjs | 2d326f5 | 2007-01-29 12:50:49 +0000 | [diff] [blame] | 257 | if (m_substituteData.isValid()) { |
mjs | 71fc41d | 2007-02-09 10:44:41 +0000 | [diff] [blame] | 258 | if (m_substituteData.content()->size()) |
| 259 | didReceiveData(m_substituteData.content()->data(), m_substituteData.content()->size(), m_substituteData.content()->size(), true); |
mjs | 2d326f5 | 2007-01-29 12:50:49 +0000 | [diff] [blame] | 260 | if (frameLoader() && !frameLoader()->isStopping()) |
| 261 | didFinishLoading(); |
| 262 | } else if (shouldLoadAsEmptyDocument(url) || frameLoader()->representationExistsForURLScheme(url.protocol())) |
| 263 | didFinishLoading(); |
staikos@webkit.org | 478595b | 2009-03-25 13:01:45 +0000 | [diff] [blame] | 264 | } |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void MainResourceLoader::callContinueAfterContentPolicy(void* argument, PolicyAction policy) |
| 268 | { |
| 269 | static_cast<MainResourceLoader*>(argument)->continueAfterContentPolicy(policy); |
| 270 | } |
| 271 | |
| 272 | void MainResourceLoader::continueAfterContentPolicy(PolicyAction policy) |
| 273 | { |
| 274 | ASSERT(m_waitingForContentPolicy); |
| 275 | m_waitingForContentPolicy = false; |
adele | 31ccc45 | 2007-07-30 23:56:19 +0000 | [diff] [blame] | 276 | if (frameLoader() && !frameLoader()->isStopping()) |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 277 | continueAfterContentPolicy(policy, m_response); |
| 278 | deref(); // balances ref in didReceiveResponse |
| 279 | } |
| 280 | |
| 281 | void MainResourceLoader::didReceiveResponse(const ResourceResponse& r) |
| 282 | { |
ap@webkit.org | fb96f8a | 2009-01-02 09:25:02 +0000 | [diff] [blame] | 283 | #if ENABLE(OFFLINE_WEB_APPLICATIONS) |
darin@chromium.org | 6042aea | 2009-07-30 23:08:33 +0000 | [diff] [blame^] | 284 | if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForMainResponse(request(), r)) |
| 285 | return; |
ap@webkit.org | fb96f8a | 2009-01-02 09:25:02 +0000 | [diff] [blame] | 286 | #endif |
| 287 | |
weinig@apple.com | 5af461c | 2009-04-08 22:16:23 +0000 | [diff] [blame] | 288 | 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.com | 7402157 | 2008-10-21 01:05:19 +0000 | [diff] [blame] | 297 | // 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) |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 300 | ASSERT(shouldLoadAsEmptyDocument(r.url()) || !defersLoading()); |
andersca@apple.com | 7402157 | 2008-10-21 01:05:19 +0000 | [diff] [blame] | 301 | #endif |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 302 | |
| 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 | |
andersca | 2dbcee8 | 2007-03-07 19:31:55 +0000 | [diff] [blame] | 315 | m_documentLoader->setResponse(r); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 316 | |
| 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 | |
| 325 | void MainResourceLoader::didReceiveData(const char* data, int length, long long lengthReceived, bool allAtOnce) |
| 326 | { |
| 327 | ASSERT(data); |
| 328 | ASSERT(length != 0); |
andersca@apple.com | 7402157 | 2008-10-21 01:05:19 +0000 | [diff] [blame] | 329 | |
beidson@apple.com | 1fbfab9 | 2009-02-07 01:26:54 +0000 | [diff] [blame] | 330 | 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.com | 7402157 | 2008-10-21 01:05:19 +0000 | [diff] [blame] | 341 | // 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) |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 344 | ASSERT(!defersLoading()); |
andersca@apple.com | 7402157 | 2008-10-21 01:05:19 +0000 | [diff] [blame] | 345 | #endif |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 346 | |
darin@chromium.org | 6042aea | 2009-07-30 23:08:33 +0000 | [diff] [blame^] | 347 | #if ENABLE(OFFLINE_WEB_APPLICATIONS) |
| 348 | documentLoader()->applicationCacheHost()->mainResourceDataReceived(data, length, lengthReceived, allAtOnce); |
| 349 | #endif |
| 350 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 351 | // 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 | |
| 358 | void MainResourceLoader::didFinishLoading() |
| 359 | { |
andersca@apple.com | 7402157 | 2008-10-21 01:05:19 +0000 | [diff] [blame] | 360 | // 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.com | c500266 | 2007-12-12 07:26:19 +0000 | [diff] [blame] | 363 | ASSERT(shouldLoadAsEmptyDocument(frameLoader()->activeDocumentLoader()->url()) || !defersLoading()); |
andersca@apple.com | 7402157 | 2008-10-21 01:05:19 +0000 | [diff] [blame] | 364 | #endif |
| 365 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 366 | // The additional processing can do anything including possibly removing the last |
| 367 | // reference to this object. |
| 368 | RefPtr<MainResourceLoader> protect(this); |
| 369 | |
andersca@apple.com | 875b865 | 2008-04-21 18:21:28 +0000 | [diff] [blame] | 370 | #if ENABLE(OFFLINE_WEB_APPLICATIONS) |
| 371 | RefPtr<DocumentLoader> dl = documentLoader(); |
| 372 | #endif |
| 373 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 374 | frameLoader()->finishedLoading(); |
| 375 | ResourceLoader::didFinishLoading(); |
andersca@apple.com | 875b865 | 2008-04-21 18:21:28 +0000 | [diff] [blame] | 376 | |
| 377 | #if ENABLE(OFFLINE_WEB_APPLICATIONS) |
darin@chromium.org | 6042aea | 2009-07-30 23:08:33 +0000 | [diff] [blame^] | 378 | dl->applicationCacheHost()->finishedLoadingMainResource(); |
andersca@apple.com | 875b865 | 2008-04-21 18:21:28 +0000 | [diff] [blame] | 379 | #endif |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void MainResourceLoader::didFail(const ResourceError& error) |
| 383 | { |
ap@webkit.org | fb96f8a | 2009-01-02 09:25:02 +0000 | [diff] [blame] | 384 | #if ENABLE(OFFLINE_WEB_APPLICATIONS) |
darin@chromium.org | 6042aea | 2009-07-30 23:08:33 +0000 | [diff] [blame^] | 385 | if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForMainError(request(), error)) |
| 386 | return; |
ap@webkit.org | fb96f8a | 2009-01-02 09:25:02 +0000 | [diff] [blame] | 387 | #endif |
| 388 | |
andersca@apple.com | 7402157 | 2008-10-21 01:05:19 +0000 | [diff] [blame] | 389 | // 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) |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 392 | ASSERT(!defersLoading()); |
andersca@apple.com | 7402157 | 2008-10-21 01:05:19 +0000 | [diff] [blame] | 393 | #endif |
| 394 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 395 | receivedError(error); |
| 396 | } |
| 397 | |
mjs | 2d326f5 | 2007-01-29 12:50:49 +0000 | [diff] [blame] | 398 | void 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.com | 67a028b | 2009-03-19 23:45:57 +0000 | [diff] [blame] | 410 | void MainResourceLoader::handleDataLoadNow(MainResourceLoaderTimer*) |
mjs | 2d326f5 | 2007-01-29 12:50:49 +0000 | [diff] [blame] | 411 | { |
| 412 | RefPtr<MainResourceLoader> protect(this); |
| 413 | |
bdash | ec5a79e | 2007-05-04 16:42:11 +0000 | [diff] [blame] | 414 | 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(), ""); |
mjs | 2d326f5 | 2007-01-29 12:50:49 +0000 | [diff] [blame] | 419 | didReceiveResponse(response); |
| 420 | } |
| 421 | |
timothy@apple.com | 67a028b | 2009-03-19 23:45:57 +0000 | [diff] [blame] | 422 | void 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 | |
mjs | 074dfb3 | 2007-01-31 02:40:54 +0000 | [diff] [blame] | 432 | void MainResourceLoader::handleDataLoadSoon(ResourceRequest& r) |
| 433 | { |
| 434 | m_initialRequest = r; |
andersca | e894632 | 2007-06-20 00:23:48 +0000 | [diff] [blame] | 435 | |
| 436 | if (m_documentLoader->deferMainResourceDataLoad()) |
timothy@apple.com | 67a028b | 2009-03-19 23:45:57 +0000 | [diff] [blame] | 437 | startDataLoadTimer(); |
andersca | e894632 | 2007-06-20 00:23:48 +0000 | [diff] [blame] | 438 | else |
| 439 | handleDataLoadNow(0); |
mjs | 074dfb3 | 2007-01-31 02:40:54 +0000 | [diff] [blame] | 440 | } |
| 441 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 442 | bool 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(); |
bdash | ec5a79e | 2007-05-04 16:42:11 +0000 | [diff] [blame] | 460 | bool shouldLoadEmpty = shouldLoadAsEmptyDocument(url) && !m_substituteData.isValid(); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 461 | |
| 462 | if (shouldLoadEmptyBeforeRedirect && !shouldLoadEmpty && defersLoading()) |
| 463 | return true; |
| 464 | |
andersca | e894632 | 2007-06-20 00:23:48 +0000 | [diff] [blame] | 465 | if (m_substituteData.isValid()) |
mjs | 074dfb3 | 2007-01-31 02:40:54 +0000 | [diff] [blame] | 466 | handleDataLoadSoon(r); |
mjs | 2d326f5 | 2007-01-29 12:50:49 +0000 | [diff] [blame] | 467 | else if (shouldLoadEmpty || frameLoader()->representationExistsForURLScheme(url.protocol())) |
| 468 | handleEmptyLoad(url, !shouldLoadEmpty); |
| 469 | else |
andersca | 2d985c7 | 2007-08-28 19:05:56 +0000 | [diff] [blame] | 470 | m_handle = ResourceHandle::create(r, this, m_frame.get(), false, true, true); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 471 | |
| 472 | return false; |
| 473 | } |
| 474 | |
andersca@apple.com | 875b865 | 2008-04-21 18:21:28 +0000 | [diff] [blame] | 475 | bool MainResourceLoader::load(const ResourceRequest& r, const SubstituteData& substituteData) |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 476 | { |
| 477 | ASSERT(!m_handle); |
| 478 | |
mjs | 2d326f5 | 2007-01-29 12:50:49 +0000 | [diff] [blame] | 479 | m_substituteData = substituteData; |
| 480 | |
darin@chromium.org | 6042aea | 2009-07-30 23:08:33 +0000 | [diff] [blame^] | 481 | ResourceRequest request(r); |
| 482 | |
andersca@apple.com | 875b865 | 2008-04-21 18:21:28 +0000 | [diff] [blame] | 483 | #if ENABLE(OFFLINE_WEB_APPLICATIONS) |
darin@chromium.org | 6042aea | 2009-07-30 23:08:33 +0000 | [diff] [blame^] | 484 | documentLoader()->applicationCacheHost()->maybeLoadMainResource(request, m_substituteData); |
andersca@apple.com | 875b865 | 2008-04-21 18:21:28 +0000 | [diff] [blame] | 485 | #endif |
| 486 | |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 487 | bool defer = defersLoading(); |
| 488 | if (defer) { |
darin@chromium.org | 6042aea | 2009-07-30 23:08:33 +0000 | [diff] [blame^] | 489 | bool shouldLoadEmpty = shouldLoadAsEmptyDocument(request.url()); |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 490 | 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 | } |
mjs | 2d326f5 | 2007-01-29 12:50:49 +0000 | [diff] [blame] | 500 | if (defer) |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 501 | m_initialRequest = request; |
| 502 | |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | void MainResourceLoader::setDefersLoading(bool defers) |
| 507 | { |
| 508 | ResourceLoader::setDefersLoading(defers); |
timothy@apple.com | 67a028b | 2009-03-19 23:45:57 +0000 | [diff] [blame] | 509 | |
andersca | 14cd912 | 2007-03-08 07:15:04 +0000 | [diff] [blame] | 510 | if (defers) { |
| 511 | if (m_dataLoadTimer.isActive()) |
| 512 | m_dataLoadTimer.stop(); |
| 513 | } else { |
| 514 | if (m_initialRequest.isNull()) |
| 515 | return; |
timothy@apple.com | 67a028b | 2009-03-19 23:45:57 +0000 | [diff] [blame] | 516 | |
| 517 | if (m_substituteData.isValid() && m_documentLoader->deferMainResourceDataLoad()) |
| 518 | startDataLoadTimer(); |
andersca | 14cd912 | 2007-03-08 07:15:04 +0000 | [diff] [blame] | 519 | else { |
andersca | 8f7934f | 2007-01-10 19:39:07 +0000 | [diff] [blame] | 520 | ResourceRequest r(m_initialRequest); |
| 521 | m_initialRequest = ResourceRequest(); |
| 522 | loadNow(r); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
darin | 9ab4ef2 | 2006-10-13 17:07:20 +0000 | [diff] [blame] | 527 | } |