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()
53 return m_resourceData;
55 if (ResourceHandle::supportsBufferedData() && m_handle)
56 return m_handle->bufferedData();
61 ResourceLoader::ResourceLoader(Frame* frame, bool sendResourceLoadCallbacks, bool shouldContentSniff)
63 , m_documentLoader(frame->loader()->activeDocumentLoader())
65 , m_reachedTerminalState(false)
66 , m_calledWillCancel(false)
68 , m_calledDidFinishLoad(false)
69 , m_sendResourceLoadCallbacks(sendResourceLoadCallbacks)
70 , m_shouldContentSniff(shouldContentSniff)
71 , m_shouldBufferData(true)
72 , m_defersLoading(frame->page()->defersLoading())
76 ResourceLoader::~ResourceLoader()
78 ASSERT(m_reachedTerminalState);
81 void ResourceLoader::releaseResources()
83 ASSERT(!m_reachedTerminalState);
85 // It's possible that when we release the handle, it will be
86 // deallocated and release the last reference to this object.
87 // We need to retain to avoid accessing the object after it
88 // has been deallocated and also to avoid reentering this method.
89 RefPtr<ResourceLoader> protector(this);
94 // We need to set reachedTerminalState to true before we release
95 // the resources to prevent a double dealloc of WebView <rdar://problem/4372628>
96 m_reachedTerminalState = true;
100 resourceLoadScheduler()->remove(this);
103 // Clear out the ResourceHandle's client so that it doesn't try to call
104 // us back after we release it, unless it has been replaced by someone else.
105 if (m_handle->client() == this)
106 m_handle->setClient(0);
111 m_deferredRequest = ResourceRequest();
114 bool ResourceLoader::init(const ResourceRequest& r)
117 ASSERT(m_request.isNull());
118 ASSERT(m_deferredRequest.isNull());
119 ASSERT(!m_documentLoader->isSubstituteLoadPending(this));
121 ResourceRequest clientRequest(r);
123 // https://bugs.webkit.org/show_bug.cgi?id=26391
124 // The various plug-in implementations call directly to ResourceLoader::load() instead of piping requests
125 // through FrameLoader. As a result, they miss the FrameLoader::addExtraFieldsToRequest() step which sets
126 // up the 1st party for cookies URL. Until plug-in implementations can be reigned in to pipe through that
127 // method, we need to make sure there is always a 1st party for cookies set.
128 if (clientRequest.firstPartyForCookies().isNull()) {
129 if (Document* document = m_frame->document())
130 clientRequest.setFirstPartyForCookies(document->firstPartyForCookies());
133 willSendRequest(clientRequest, ResourceResponse());
134 if (clientRequest.isNull()) {
135 didFail(frameLoader()->cancelledError(m_request));
139 m_request = clientRequest;
143 void ResourceLoader::start()
146 ASSERT(!m_request.isNull());
147 ASSERT(m_deferredRequest.isNull());
149 #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
150 if (m_documentLoader->scheduleArchiveLoad(this, m_request, m_request.url()))
154 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
155 if (m_documentLoader->applicationCacheHost()->maybeLoadResource(this, m_request, m_request.url()))
159 if (m_defersLoading) {
160 m_deferredRequest = m_request;
164 if (!m_reachedTerminalState)
165 m_handle = ResourceHandle::create(m_frame->loader()->networkingContext(), m_request, this, m_defersLoading, m_shouldContentSniff);
168 void ResourceLoader::setDefersLoading(bool defers)
170 m_defersLoading = defers;
172 m_handle->setDefersLoading(defers);
173 if (!defers && !m_deferredRequest.isNull()) {
174 m_request = m_deferredRequest;
175 m_deferredRequest = ResourceRequest();
180 FrameLoader* ResourceLoader::frameLoader() const
184 return m_frame->loader();
187 void ResourceLoader::setShouldBufferData(bool shouldBufferData)
189 m_shouldBufferData = shouldBufferData;
191 // Reset any already buffered data
192 if (!m_shouldBufferData)
197 void ResourceLoader::addData(const char* data, int length, bool allAtOnce)
199 if (!m_shouldBufferData)
203 m_resourceData = SharedBuffer::create(data, length);
207 if (ResourceHandle::supportsBufferedData()) {
208 // Buffer data only if the connection has handed us the data because is has stopped buffering it.
210 m_resourceData->append(data, length);
213 m_resourceData = SharedBuffer::create(data, length);
215 m_resourceData->append(data, length);
219 void ResourceLoader::clearResourceData()
222 m_resourceData->clear();
225 void ResourceLoader::willSendRequest(ResourceRequest& request, const ResourceResponse& redirectResponse)
227 // Protect this in this delegate method since the additional processing can do
228 // anything including possibly derefing this; one example of this is Radar 3266216.
229 RefPtr<ResourceLoader> protector(this);
231 ASSERT(!m_reachedTerminalState);
233 if (m_sendResourceLoadCallbacks) {
235 m_identifier = m_frame->page()->progress()->createUniqueIdentifier();
236 frameLoader()->notifier()->assignIdentifierToInitialRequest(m_identifier, documentLoader(), request);
239 frameLoader()->notifier()->willSendRequest(this, request, redirectResponse);
242 if (!redirectResponse.isNull())
243 resourceLoadScheduler()->crossOriginRedirectReceived(this, request.url());
247 void ResourceLoader::didSendData(unsigned long long, unsigned long long)
251 void ResourceLoader::didReceiveResponse(const ResourceResponse& r)
253 ASSERT(!m_reachedTerminalState);
255 // Protect this in this delegate method since the additional processing can do
256 // anything including possibly derefing this; one example of this is Radar 3266216.
257 RefPtr<ResourceLoader> protector(this);
261 if (FormData* data = m_request.httpBody())
262 data->removeGeneratedFilesIfNeeded();
264 if (m_sendResourceLoadCallbacks)
265 frameLoader()->notifier()->didReceiveResponse(this, m_response);
268 void ResourceLoader::didReceiveData(const char* data, int length, long long encodedDataLength, bool allAtOnce)
270 // The following assertions are not quite valid here, since a subclass
271 // might override didReceiveData in a way that invalidates them. This
272 // happens with the steps listed in 3266216
273 // ASSERT(con == connection);
274 // ASSERT(!m_reachedTerminalState);
276 // Protect this in this delegate method since the additional processing can do
277 // anything including possibly derefing this; one example of this is Radar 3266216.
278 RefPtr<ResourceLoader> protector(this);
280 addData(data, length, allAtOnce);
281 // FIXME: If we get a resource with more than 2B bytes, this code won't do the right thing.
282 // However, with today's computers and networking speeds, this won't happen in practice.
283 // Could be an issue with a giant local file.
284 if (m_sendResourceLoadCallbacks && m_frame)
285 frameLoader()->notifier()->didReceiveData(this, data, length, static_cast<int>(encodedDataLength));
288 void ResourceLoader::willStopBufferingData(const char* data, int length)
290 if (!m_shouldBufferData)
293 ASSERT(!m_resourceData);
294 m_resourceData = SharedBuffer::create(data, length);
297 void ResourceLoader::didFinishLoading(double finishTime)
299 // If load has been cancelled after finishing (which could happen with a
300 // JavaScript that changes the window location), do nothing.
303 ASSERT(!m_reachedTerminalState);
305 didFinishLoadingOnePart(finishTime);
309 void ResourceLoader::didFinishLoadingOnePart(double finishTime)
313 ASSERT(!m_reachedTerminalState);
315 if (m_calledDidFinishLoad)
317 m_calledDidFinishLoad = true;
318 if (m_sendResourceLoadCallbacks)
319 frameLoader()->notifier()->didFinishLoad(this, finishTime);
322 void ResourceLoader::didFail(const ResourceError& error)
326 ASSERT(!m_reachedTerminalState);
328 // Protect this in this delegate method since the additional processing can do
329 // anything including possibly derefing this; one example of this is Radar 3266216.
330 RefPtr<ResourceLoader> protector(this);
332 if (FormData* data = m_request.httpBody())
333 data->removeGeneratedFilesIfNeeded();
335 if (m_sendResourceLoadCallbacks && !m_calledDidFinishLoad)
336 frameLoader()->notifier()->didFailToLoad(this, error);
341 void ResourceLoader::cancel()
343 cancel(ResourceError());
346 void ResourceLoader::cancel(const ResourceError& error)
348 // If the load has already completed - succeeded, failed, or previously cancelled - do nothing.
349 if (m_reachedTerminalState)
352 ResourceError nonNullError = error.isNull() ? cancelledError() : error;
354 // willCancel() and didFailToLoad() both call out to clients that might do
355 // something causing the last reference to this object to go away.
356 RefPtr<ResourceLoader> protector(this);
358 // If we re-enter cancel() from inside willCancel(), we want to pick up from where we left
359 // off without re-running willCancel()
360 if (!m_calledWillCancel) {
361 m_calledWillCancel = true;
363 willCancel(nonNullError);
366 // If we re-enter cancel() from inside didFailToLoad(), we want to pick up from where we
367 // left off without redoing any of this work.
371 if (FormData* data = m_request.httpBody())
372 data->removeGeneratedFilesIfNeeded();
375 m_handle->clearAuthentication();
377 m_documentLoader->cancelPendingSubstituteLoad(this);
383 if (m_sendResourceLoadCallbacks && m_identifier && !m_calledDidFinishLoad)
384 frameLoader()->notifier()->didFailToLoad(this, nonNullError);
387 // If cancel() completed from within the call to willCancel() or didFailToLoad(),
388 // we don't want to redo didCancel() or releasesResources().
389 if (m_reachedTerminalState)
392 didCancel(nonNullError);
397 const ResourceResponse& ResourceLoader::response() const
402 ResourceError ResourceLoader::cancelledError()
404 return frameLoader()->cancelledError(m_request);
407 ResourceError ResourceLoader::blockedError()
409 return frameLoader()->blockedError(m_request);
412 ResourceError ResourceLoader::cannotShowURLError()
414 return frameLoader()->cannotShowURLError(m_request);
417 void ResourceLoader::willSendRequest(ResourceHandle*, ResourceRequest& request, const ResourceResponse& redirectResponse)
419 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
420 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForRedirect(this, request, redirectResponse))
423 willSendRequest(request, redirectResponse);
426 void ResourceLoader::didSendData(ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
428 didSendData(bytesSent, totalBytesToBeSent);
431 void ResourceLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
433 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
434 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForResponse(this, response))
437 didReceiveResponse(response);
440 void ResourceLoader::didReceiveData(ResourceHandle*, const char* data, int length, int encodedDataLength)
442 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willReceiveResourceData(m_frame.get(), identifier());
443 didReceiveData(data, length, encodedDataLength, false);
444 InspectorInstrumentation::didReceiveResourceData(cookie);
447 void ResourceLoader::didFinishLoading(ResourceHandle*, double finishTime)
449 didFinishLoading(finishTime);
452 void ResourceLoader::didFail(ResourceHandle*, const ResourceError& error)
454 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
455 if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForError(this, error))
461 void ResourceLoader::wasBlocked(ResourceHandle*)
463 didFail(blockedError());
466 void ResourceLoader::cannotShowURL(ResourceHandle*)
468 didFail(cannotShowURLError());
471 bool ResourceLoader::shouldUseCredentialStorage()
473 RefPtr<ResourceLoader> protector(this);
474 return frameLoader()->shouldUseCredentialStorage(this);
477 void ResourceLoader::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
479 // Protect this in this delegate method since the additional processing can do
480 // anything including possibly derefing this; one example of this is Radar 3266216.
481 RefPtr<ResourceLoader> protector(this);
482 frameLoader()->notifier()->didReceiveAuthenticationChallenge(this, challenge);
485 void ResourceLoader::didCancelAuthenticationChallenge(const AuthenticationChallenge& challenge)
487 // Protect this in this delegate method since the additional processing can do
488 // anything including possibly derefing this; one example of this is Radar 3266216.
489 RefPtr<ResourceLoader> protector(this);
490 frameLoader()->notifier()->didCancelAuthenticationChallenge(this, challenge);
493 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
494 bool ResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
496 RefPtr<ResourceLoader> protector(this);
497 return frameLoader()->canAuthenticateAgainstProtectionSpace(this, protectionSpace);
501 void ResourceLoader::receivedCancellation(const AuthenticationChallenge&)
506 void ResourceLoader::willCacheResponse(ResourceHandle*, CacheStoragePolicy& policy)
508 // <rdar://problem/7249553> - There are reports of crashes with this method being called
509 // with a null m_frame->settings(), which can only happen if the frame doesn't have a page.
510 // Sadly we have no reproducible cases of this.
511 // We think that any frame without a page shouldn't have any loads happening in it, yet
512 // there is at least one code path where that is not true.
513 ASSERT(m_frame->settings());
515 // When in private browsing mode, prevent caching to disk
516 if (policy == StorageAllowed && m_frame->settings() && m_frame->settings()->privateBrowsingEnabled())
517 policy = StorageAllowedInMemoryOnly;
521 AsyncFileStream* ResourceLoader::createAsyncFileStream(FileStreamClient* client)
523 // It is OK to simply return a pointer since FileStreamProxy::create adds an extra ref.
524 return FileStreamProxy::create(m_frame->document()->scriptExecutionContext(), client).get();