2 * Copyright (C) 2006, 2007, 2010, 2011 Apple Inc. All rights reserved.
3 * (C) 2007 Graham Dennis (graham.dennis@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include "ResourceLoader.h"
33 #include "ApplicationCacheHost.h"
34 #include "DocumentLoader.h"
35 #include "FileStreamProxy.h"
37 #include "FrameLoader.h"
38 #include "FrameLoaderClient.h"
39 #include "InspectorInstrumentation.h"
41 #include "ProgressTracker.h"
42 #include "ResourceError.h"
43 #include "ResourceHandle.h"
44 #include "ResourceLoadScheduler.h"
46 #include "SharedBuffer.h"
50 PassRefPtr<SharedBuffer> ResourceLoader::resourceData()
52 return m_resourceData;
55 ResourceLoader::ResourceLoader(Frame* frame, ResourceLoaderOptions options)
57 , m_documentLoader(frame->loader()->activeDocumentLoader())
59 , m_reachedTerminalState(false)
60 , m_calledWillCancel(false)
62 , m_calledDidFinishLoad(false)
63 , m_defersLoading(frame->page()->defersLoading())
68 ResourceLoader::~ResourceLoader()
70 ASSERT(m_reachedTerminalState);
73 void ResourceLoader::releaseResources()
75 ASSERT(!m_reachedTerminalState);
77 // It's possible that when we release the handle, it will be
78 // deallocated and release the last reference to this object.
79 // We need to retain to avoid accessing the object after it
80 // has been deallocated and also to avoid reentering this method.
81 RefPtr<ResourceLoader> protector(this);
86 // We need to set reachedTerminalState to true before we release
87 // the resources to prevent a double dealloc of WebView <rdar://problem/4372628>
88 m_reachedTerminalState = true;
92 resourceLoadScheduler()->remove(this);
95 // Clear out the ResourceHandle's client so that it doesn't try to call
96 // us back after we release it, unless it has been replaced by someone else.
97 if (m_handle->client() == this)
98 m_handle->setClient(0);
103 m_deferredRequest = ResourceRequest();
106 bool ResourceLoader::init(const ResourceRequest& r)
109 ASSERT(m_request.isNull());
110 ASSERT(m_deferredRequest.isNull());
111 ASSERT(!m_documentLoader->isSubstituteLoadPending(this));
113 ResourceRequest clientRequest(r);
115 // https://bugs.webkit.org/show_bug.cgi?id=26391
116 // The various plug-in implementations call directly to ResourceLoader::load() instead of piping requests
117 // through FrameLoader. As a result, they miss the FrameLoader::addExtraFieldsToRequest() step which sets
118 // up the 1st party for cookies URL. Until plug-in implementations can be reigned in to pipe through that
119 // method, we need to make sure there is always a 1st party for cookies set.
120 if (clientRequest.firstPartyForCookies().isNull()) {
121 if (Document* document = m_frame->document())
122 clientRequest.setFirstPartyForCookies(document->firstPartyForCookies());
125 willSendRequest(clientRequest, ResourceResponse());
126 if (clientRequest.isNull()) {
127 didFail(cancelledError());
131 m_originalRequest = m_request = clientRequest;
135 void ResourceLoader::start()
138 ASSERT(!m_request.isNull());
139 ASSERT(m_deferredRequest.isNull());
141 #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
142 if (m_documentLoader->scheduleArchiveLoad(this, m_request, m_request.url()))
146 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
147 if (m_documentLoader->applicationCacheHost()->maybeLoadResource(this, m_request, m_request.url()))
151 if (m_defersLoading) {
152 m_deferredRequest = m_request;
156 if (!m_reachedTerminalState)
157 m_handle = ResourceHandle::create(m_frame->loader()->networkingContext(), m_request, this, m_defersLoading, m_options.sniffContent == SniffContent);
160 void ResourceLoader::setDefersLoading(bool defers)
162 m_defersLoading = defers;
164 m_handle->setDefersLoading(defers);
165 if (!defers && !m_deferredRequest.isNull()) {
166 m_request = m_deferredRequest;
167 m_deferredRequest = ResourceRequest();
172 FrameLoader* ResourceLoader::frameLoader() const
176 return m_frame->loader();
179 void ResourceLoader::setShouldBufferData(DataBufferingPolicy shouldBufferData)
181 m_options.shouldBufferData = shouldBufferData;
183 // Reset any already buffered data
184 if (!shouldBufferData)
189 void ResourceLoader::addData(const char* data, int length, bool allAtOnce)
191 if (m_options.shouldBufferData == DoNotBufferData)
195 m_resourceData = SharedBuffer::create(data, length);
200 m_resourceData = SharedBuffer::create(data, length);
202 m_resourceData->append(data, length);
205 void ResourceLoader::clearResourceData()
208 m_resourceData->clear();
211 void ResourceLoader::willSendRequest(ResourceRequest& request, const ResourceResponse& redirectResponse)
213 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
214 if (!fastMallocSize(documentLoader()->applicationCacheHost()))
217 if (!fastMallocSize(documentLoader()->frame()))
219 // Protect this in this delegate method since the additional processing can do
220 // anything including possibly derefing this; one example of this is Radar 3266216.
221 RefPtr<ResourceLoader> protector(this);
223 ASSERT(!m_reachedTerminalState);
225 if (m_options.sendLoadCallbacks == SendCallbacks) {
227 m_identifier = m_frame->page()->progress()->createUniqueIdentifier();
228 frameLoader()->notifier()->assignIdentifierToInitialRequest(m_identifier, documentLoader(), request);
231 frameLoader()->notifier()->willSendRequest(this, request, redirectResponse);
234 if (!redirectResponse.isNull())
235 resourceLoadScheduler()->crossOriginRedirectReceived(this, request.url());
239 void ResourceLoader::didSendData(unsigned long long, unsigned long long)
241 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
242 if (!fastMallocSize(documentLoader()->applicationCacheHost()))
245 if (!fastMallocSize(documentLoader()->frame()))
249 void ResourceLoader::didReceiveResponse(const ResourceResponse& r)
251 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
252 if (!fastMallocSize(documentLoader()->applicationCacheHost()))
255 if (!fastMallocSize(documentLoader()->frame()))
257 ASSERT(!m_reachedTerminalState);
259 // Protect this in this delegate method since the additional processing can do
260 // anything including possibly derefing this; one example of this is Radar 3266216.
261 RefPtr<ResourceLoader> protector(this);
265 if (FormData* data = m_request.httpBody())
266 data->removeGeneratedFilesIfNeeded();
268 if (m_options.sendLoadCallbacks == SendCallbacks)
269 frameLoader()->notifier()->didReceiveResponse(this, m_response);
272 void ResourceLoader::didReceiveData(const char* data, int length, long long encodedDataLength, bool allAtOnce)
274 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
275 if (!m_cancelled && !fastMallocSize(documentLoader()->applicationCacheHost()))
278 if (!m_cancelled && !fastMallocSize(documentLoader()->frame()))
280 // The following assertions are not quite valid here, since a subclass
281 // might override didReceiveData in a way that invalidates them. This
282 // happens with the steps listed in 3266216
283 // ASSERT(con == connection);
284 // ASSERT(!m_reachedTerminalState);
286 // Protect this in this delegate method since the additional processing can do
287 // anything including possibly derefing this; one example of this is Radar 3266216.
288 RefPtr<ResourceLoader> protector(this);
290 addData(data, length, allAtOnce);
291 // FIXME: If we get a resource with more than 2B bytes, this code won't do the right thing.
292 // However, with today's computers and networking speeds, this won't happen in practice.
293 // Could be an issue with a giant local file.
294 if (m_options.sendLoadCallbacks == SendCallbacks && m_frame)
295 frameLoader()->notifier()->didReceiveData(this, data, length, static_cast<int>(encodedDataLength));
298 void ResourceLoader::willStopBufferingData(const char* data, int length)
300 if (m_options.shouldBufferData == DoNotBufferData)
303 ASSERT(!m_resourceData);
304 m_resourceData = SharedBuffer::create(data, length);
307 void ResourceLoader::didFinishLoading(double finishTime)
309 // If load has been cancelled after finishing (which could happen with a
310 // JavaScript that changes the window location), do nothing.
313 ASSERT(!m_reachedTerminalState);
315 didFinishLoadingOnePart(finishTime);
319 void ResourceLoader::didFinishLoadingOnePart(double finishTime)
323 ASSERT(!m_reachedTerminalState);
325 if (m_calledDidFinishLoad)
327 m_calledDidFinishLoad = true;
328 if (m_options.sendLoadCallbacks == SendCallbacks)
329 frameLoader()->notifier()->didFinishLoad(this, finishTime);
332 void ResourceLoader::didFail(const ResourceError& error)
336 ASSERT(!m_reachedTerminalState);
338 // Protect this in this delegate method since the additional processing can do
339 // anything including possibly derefing this; one example of this is Radar 3266216.
340 RefPtr<ResourceLoader> protector(this);
342 if (FormData* data = m_request.httpBody())
343 data->removeGeneratedFilesIfNeeded();
345 if (m_options.sendLoadCallbacks == SendCallbacks && !m_calledDidFinishLoad)
346 frameLoader()->notifier()->didFailToLoad(this, error);
351 void ResourceLoader::cancel()
353 cancel(ResourceError());
356 void ResourceLoader::cancel(const ResourceError& error)
358 // If the load has already completed - succeeded, failed, or previously cancelled - do nothing.
359 if (m_reachedTerminalState)
362 ResourceError nonNullError = error.isNull() ? cancelledError() : error;
364 // willCancel() and didFailToLoad() both call out to clients that might do
365 // something causing the last reference to this object to go away.
366 RefPtr<ResourceLoader> protector(this);
368 // If we re-enter cancel() from inside willCancel(), we want to pick up from where we left
369 // off without re-running willCancel()
370 if (!m_calledWillCancel) {
371 m_calledWillCancel = true;
373 willCancel(nonNullError);
376 // If we re-enter cancel() from inside didFailToLoad(), we want to pick up from where we
377 // left off without redoing any of this work.
381 if (FormData* data = m_request.httpBody())
382 data->removeGeneratedFilesIfNeeded();
385 m_handle->clearAuthentication();
387 m_documentLoader->cancelPendingSubstituteLoad(this);
393 if (m_options.sendLoadCallbacks == SendCallbacks && m_identifier && !m_calledDidFinishLoad)
394 frameLoader()->notifier()->didFailToLoad(this, nonNullError);
397 // If cancel() completed from within the call to willCancel() or didFailToLoad(),
398 // we don't want to redo didCancel() or releasesResources().
399 if (m_reachedTerminalState)
402 didCancel(nonNullError);
407 ResourceError ResourceLoader::cancelledError()
409 return frameLoader()->cancelledError(m_request);
412 ResourceError ResourceLoader::blockedError()
414 return frameLoader()->client()->blockedError(m_request);
417 ResourceError ResourceLoader::cannotShowURLError()
419 return frameLoader()->client()->cannotShowURLError(m_request);
422 void ResourceLoader::willSendRequest(ResourceHandle*, ResourceRequest& request, const ResourceResponse& redirectResponse)
424 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
425 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForRedirect(this, request, redirectResponse))
428 willSendRequest(request, redirectResponse);
431 void ResourceLoader::didSendData(ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
433 didSendData(bytesSent, totalBytesToBeSent);
436 void ResourceLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
438 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
439 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForResponse(this, response))
442 didReceiveResponse(response);
445 void ResourceLoader::didReceiveData(ResourceHandle*, const char* data, int length, int encodedDataLength)
447 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willReceiveResourceData(m_frame.get(), identifier());
448 didReceiveData(data, length, encodedDataLength, false);
449 InspectorInstrumentation::didReceiveResourceData(cookie);
452 void ResourceLoader::didFinishLoading(ResourceHandle*, double finishTime)
454 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
455 if (!fastMallocSize(documentLoader()->applicationCacheHost()))
458 if (!fastMallocSize(documentLoader()->frame()))
460 didFinishLoading(finishTime);
463 void ResourceLoader::didFail(ResourceHandle*, const ResourceError& error)
465 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
466 if (!fastMallocSize(documentLoader()->applicationCacheHost()))
469 if (!fastMallocSize(documentLoader()->frame()))
471 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
472 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForError(this, error))
478 void ResourceLoader::wasBlocked(ResourceHandle*)
480 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
481 if (!fastMallocSize(documentLoader()->applicationCacheHost()))
484 if (!fastMallocSize(documentLoader()->frame()))
486 didFail(blockedError());
489 void ResourceLoader::cannotShowURL(ResourceHandle*)
491 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
492 if (!fastMallocSize(documentLoader()->applicationCacheHost()))
495 if (!fastMallocSize(documentLoader()->frame()))
497 didFail(cannotShowURLError());
500 bool ResourceLoader::shouldUseCredentialStorage()
502 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
503 if (!fastMallocSize(documentLoader()->applicationCacheHost()))
506 if (!fastMallocSize(documentLoader()->frame()))
509 if (m_options.allowCredentials == DoNotAllowStoredCredentials)
512 RefPtr<ResourceLoader> protector(this);
513 return frameLoader()->client()->shouldUseCredentialStorage(documentLoader(), identifier());
516 void ResourceLoader::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
518 // Protect this in this delegate method since the additional processing can do
519 // anything including possibly derefing this; one example of this is Radar 3266216.
520 RefPtr<ResourceLoader> protector(this);
521 frameLoader()->notifier()->didReceiveAuthenticationChallenge(this, challenge);
524 void ResourceLoader::didCancelAuthenticationChallenge(const AuthenticationChallenge& challenge)
526 // Protect this in this delegate method since the additional processing can do
527 // anything including possibly derefing this; one example of this is Radar 3266216.
528 RefPtr<ResourceLoader> protector(this);
529 frameLoader()->notifier()->didCancelAuthenticationChallenge(this, challenge);
532 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
533 bool ResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
535 RefPtr<ResourceLoader> protector(this);
536 return frameLoader()->client()->canAuthenticateAgainstProtectionSpace(documentLoader(), identifier(), protectionSpace);
540 void ResourceLoader::receivedCancellation(const AuthenticationChallenge&)
545 void ResourceLoader::willCacheResponse(ResourceHandle*, CacheStoragePolicy& policy)
547 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
548 if (!fastMallocSize(documentLoader()->applicationCacheHost()))
551 if (!fastMallocSize(documentLoader()->frame()))
553 // <rdar://problem/7249553> - There are reports of crashes with this method being called
554 // with a null m_frame->settings(), which can only happen if the frame doesn't have a page.
555 // Sadly we have no reproducible cases of this.
556 // We think that any frame without a page shouldn't have any loads happening in it, yet
557 // there is at least one code path where that is not true.
558 ASSERT(m_frame->settings());
560 // When in private browsing mode, prevent caching to disk
561 if (policy == StorageAllowed && m_frame->settings() && m_frame->settings()->privateBrowsingEnabled())
562 policy = StorageAllowedInMemoryOnly;
566 AsyncFileStream* ResourceLoader::createAsyncFileStream(FileStreamClient* client)
568 // It is OK to simply return a pointer since FileStreamProxy::create adds an extra ref.
569 return FileStreamProxy::create(m_frame->document()->scriptExecutionContext(), client).get();