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 "AsyncFileStream.h"
35
#include "DocumentLoader.h"
37
#include "FrameLoader.h"
38
#include "FrameLoaderClient.h"
39
#include "InspectorInstrumentation.h"
40
#include "LoaderStrategy.h"
42
#include "PlatformStrategies.h"
43
#include "ProgressTracker.h"
44
#include "ResourceBuffer.h"
45
#include "ResourceError.h"
46
#include "ResourceHandle.h"
47
#include "ResourceLoadScheduler.h"
48
#include "SecurityOrigin.h"
50
#include "SharedBuffer.h"
54
PassRefPtr<ResourceBuffer> ResourceLoader::resourceData()
56
return m_resourceData;
59
ResourceLoader::ResourceLoader(Frame* frame, ResourceLoaderOptions options)
61
, m_documentLoader(frame->loader()->activeDocumentLoader())
63
, m_reachedTerminalState(false)
64
, m_calledWillCancel(false)
66
, m_notifiedLoadComplete(false)
67
, m_defersLoading(frame->page()->defersLoading())
72
ResourceLoader::~ResourceLoader()
74
ASSERT(m_reachedTerminalState);
77
void ResourceLoader::releaseResources()
79
ASSERT(!m_reachedTerminalState);
81
// It's possible that when we release the handle, it will be
82
// deallocated and release the last reference to this object.
83
// We need to retain to avoid accessing the object after it
84
// has been deallocated and also to avoid reentering this method.
85
RefPtr<ResourceLoader> protector(this);
90
// We need to set reachedTerminalState to true before we release
91
// the resources to prevent a double dealloc of WebView <rdar://problem/4372628>
92
m_reachedTerminalState = true;
94
#if USE(PLATFORM_STRATEGIES)
95
platformStrategies()->loaderStrategy()->resourceLoadScheduler()->remove(this);
98
#if !USE(PLATFORM_STRATEGIES)
99
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
m_defersLoading = m_frame->page()->defersLoading();
124
if (m_options.securityCheck == DoSecurityCheck && !m_frame->document()->securityOrigin()->canDisplay(clientRequest.url())) {
125
FrameLoader::reportLocalLoadFailed(m_frame.get(), clientRequest.url().string());
130
// https://bugs.webkit.org/show_bug.cgi?id=26391
131
// The various plug-in implementations call directly to ResourceLoader::load() instead of piping requests
132
// through FrameLoader. As a result, they miss the FrameLoader::addExtraFieldsToRequest() step which sets
133
// up the 1st party for cookies URL. Until plug-in implementations can be reigned in to pipe through that
134
// method, we need to make sure there is always a 1st party for cookies set.
135
if (clientRequest.firstPartyForCookies().isNull()) {
136
if (Document* document = m_frame->document())
137
clientRequest.setFirstPartyForCookies(document->firstPartyForCookies());
140
willSendRequest(clientRequest, ResourceResponse());
141
if (clientRequest.isNull()) {
146
m_originalRequest = m_request = clientRequest;
150
void ResourceLoader::start()
153
ASSERT(!m_request.isNull());
154
ASSERT(m_deferredRequest.isNull());
156
#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
157
if (m_documentLoader->scheduleArchiveLoad(this, m_request, m_request.url()))
161
if (m_documentLoader->applicationCacheHost()->maybeLoadResource(this, m_request, m_request.url()))
164
if (m_defersLoading) {
165
m_deferredRequest = m_request;
169
if (!m_reachedTerminalState)
170
m_handle = ResourceHandle::create(m_frame->loader()->networkingContext(), m_request, this, m_defersLoading, m_options.sniffContent == SniffContent);
173
void ResourceLoader::setDefersLoading(bool defers)
175
m_defersLoading = defers;
177
m_handle->setDefersLoading(defers);
178
if (!defers && !m_deferredRequest.isNull()) {
179
m_request = m_deferredRequest;
180
m_deferredRequest = ResourceRequest();
185
FrameLoader* ResourceLoader::frameLoader() const
189
return m_frame->loader();
192
void ResourceLoader::setShouldBufferData(DataBufferingPolicy shouldBufferData)
194
m_options.shouldBufferData = shouldBufferData;
196
// Reset any already buffered data
197
if (!shouldBufferData)
202
void ResourceLoader::addData(const char* data, int length, bool allAtOnce)
204
if (m_options.shouldBufferData == DoNotBufferData)
208
m_resourceData = ResourceBuffer::create(data, length);
213
m_resourceData = ResourceBuffer::create(data, length);
215
m_resourceData->append(data, length);
218
void ResourceLoader::clearResourceData()
221
m_resourceData->clear();
224
bool ResourceLoader::isSubresourceLoader()
229
void ResourceLoader::willSendRequest(ResourceRequest& request, const ResourceResponse& redirectResponse)
231
// Protect this in this delegate method since the additional processing can do
232
// anything including possibly derefing this; one example of this is Radar 3266216.
233
RefPtr<ResourceLoader> protector(this);
235
ASSERT(!m_reachedTerminalState);
237
if (m_options.sendLoadCallbacks == SendCallbacks) {
239
m_identifier = m_frame->page()->progress()->createUniqueIdentifier();
240
frameLoader()->notifier()->assignIdentifierToInitialRequest(m_identifier, documentLoader(), request);
243
frameLoader()->notifier()->willSendRequest(this, request, redirectResponse);
246
if (!redirectResponse.isNull()) {
247
#if USE(PLATFORM_STRATEGIES)
248
platformStrategies()->loaderStrategy()->resourceLoadScheduler()->crossOriginRedirectReceived(this, request.url());
250
resourceLoadScheduler()->crossOriginRedirectReceived(this, request.url());
256
void ResourceLoader::didSendData(unsigned long long, unsigned long long)
260
void ResourceLoader::didReceiveResponse(const ResourceResponse& r)
262
ASSERT(!m_reachedTerminalState);
264
// Protect this in this delegate method since the additional processing can do
265
// anything including possibly derefing this; one example of this is Radar 3266216.
266
RefPtr<ResourceLoader> protector(this);
270
if (FormData* data = m_request.httpBody())
271
data->removeGeneratedFilesIfNeeded();
273
if (m_options.sendLoadCallbacks == SendCallbacks)
274
frameLoader()->notifier()->didReceiveResponse(this, m_response);
277
void ResourceLoader::didReceiveData(const char* data, int length, long long encodedDataLength, bool allAtOnce)
279
// The following assertions are not quite valid here, since a subclass
280
// might override didReceiveData in a way that invalidates them. This
281
// happens with the steps listed in 3266216
282
// ASSERT(con == connection);
283
// ASSERT(!m_reachedTerminalState);
285
// Protect this in this delegate method since the additional processing can do
286
// anything including possibly derefing this; one example of this is Radar 3266216.
287
RefPtr<ResourceLoader> protector(this);
289
addData(data, length, allAtOnce);
290
// FIXME: If we get a resource with more than 2B bytes, this code won't do the right thing.
291
// However, with today's computers and networking speeds, this won't happen in practice.
292
// Could be an issue with a giant local file.
293
if (m_options.sendLoadCallbacks == SendCallbacks && m_frame)
294
frameLoader()->notifier()->didReceiveData(this, data, length, static_cast<int>(encodedDataLength));
297
void ResourceLoader::willStopBufferingData(const char* data, int length)
299
if (m_options.shouldBufferData == DoNotBufferData)
302
ASSERT(!m_resourceData);
303
m_resourceData = ResourceBuffer::create(data, length);
306
void ResourceLoader::didFinishLoading(double finishTime)
308
didFinishLoadingOnePart(finishTime);
310
// If the load has been cancelled by a delegate in response to didFinishLoad(), do not release
311
// the resources a second time, they have been released by cancel.
317
void ResourceLoader::didFinishLoadingOnePart(double finishTime)
319
// If load has been cancelled after finishing (which could happen with a
320
// JavaScript that changes the window location), do nothing.
323
ASSERT(!m_reachedTerminalState);
325
if (m_notifiedLoadComplete)
327
m_notifiedLoadComplete = 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_notifiedLoadComplete) {
346
m_notifiedLoadComplete = true;
347
if (m_options.sendLoadCallbacks == SendCallbacks)
348
frameLoader()->notifier()->didFailToLoad(this, error);
354
void ResourceLoader::cancel()
356
cancel(ResourceError());
359
void ResourceLoader::cancel(const ResourceError& error)
361
// If the load has already completed - succeeded, failed, or previously cancelled - do nothing.
362
if (m_reachedTerminalState)
365
ResourceError nonNullError = error.isNull() ? cancelledError() : error;
367
// willCancel() and didFailToLoad() both call out to clients that might do
368
// something causing the last reference to this object to go away.
369
RefPtr<ResourceLoader> protector(this);
371
// If we re-enter cancel() from inside willCancel(), we want to pick up from where we left
372
// off without re-running willCancel()
373
if (!m_calledWillCancel) {
374
m_calledWillCancel = true;
376
willCancel(nonNullError);
379
// If we re-enter cancel() from inside didFailToLoad(), we want to pick up from where we
380
// left off without redoing any of this work.
384
if (FormData* data = m_request.httpBody())
385
data->removeGeneratedFilesIfNeeded();
388
m_handle->clearAuthentication();
390
m_documentLoader->cancelPendingSubstituteLoad(this);
396
if (m_options.sendLoadCallbacks == SendCallbacks && m_identifier && !m_notifiedLoadComplete)
397
frameLoader()->notifier()->didFailToLoad(this, nonNullError);
400
// If cancel() completed from within the call to willCancel() or didFailToLoad(),
401
// we don't want to redo didCancel() or releasesResources().
402
if (m_reachedTerminalState)
405
didCancel(nonNullError);
410
ResourceError ResourceLoader::cancelledError()
412
return frameLoader()->cancelledError(m_request);
415
ResourceError ResourceLoader::blockedError()
417
return frameLoader()->client()->blockedError(m_request);
420
ResourceError ResourceLoader::cannotShowURLError()
422
return frameLoader()->client()->cannotShowURLError(m_request);
425
void ResourceLoader::willSendRequest(ResourceHandle*, ResourceRequest& request, const ResourceResponse& redirectResponse)
427
if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForRedirect(this, request, redirectResponse))
429
willSendRequest(request, redirectResponse);
432
void ResourceLoader::didSendData(ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
434
didSendData(bytesSent, totalBytesToBeSent);
437
void ResourceLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
439
if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForResponse(this, response))
441
didReceiveResponse(response);
444
void ResourceLoader::didReceiveData(ResourceHandle*, const char* data, int length, int encodedDataLength)
446
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willReceiveResourceData(m_frame.get(), identifier(), encodedDataLength);
447
didReceiveData(data, length, encodedDataLength, false);
448
InspectorInstrumentation::didReceiveResourceData(cookie);
451
void ResourceLoader::didFinishLoading(ResourceHandle*, double finishTime)
453
didFinishLoading(finishTime);
456
void ResourceLoader::didFail(ResourceHandle*, const ResourceError& error)
458
if (documentLoader()->applicationCacheHost()->maybeLoadFallbackForError(this, error))
463
void ResourceLoader::wasBlocked(ResourceHandle*)
465
didFail(blockedError());
468
void ResourceLoader::cannotShowURL(ResourceHandle*)
470
didFail(cannotShowURLError());
473
bool ResourceLoader::shouldUseCredentialStorage()
475
if (m_options.allowCredentials == DoNotAllowStoredCredentials)
478
RefPtr<ResourceLoader> protector(this);
479
return frameLoader()->client()->shouldUseCredentialStorage(documentLoader(), identifier());
482
void ResourceLoader::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
484
ASSERT(!handle() || handle()->hasAuthenticationChallenge());
486
// Protect this in this delegate method since the additional processing can do
487
// anything including possibly derefing this; one example of this is Radar 3266216.
488
RefPtr<ResourceLoader> protector(this);
490
if (m_options.allowCredentials == AllowStoredCredentials) {
491
if (m_options.crossOriginCredentialPolicy == AskClientForCrossOriginCredentials || m_frame->document()->securityOrigin()->canRequest(originalRequest().url())) {
492
frameLoader()->notifier()->didReceiveAuthenticationChallenge(this, challenge);
496
// Only these platforms provide a way to continue without credentials.
497
// If we can't continue with credentials, we need to cancel the load altogether.
498
#if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL) || PLATFORM(GTK) || PLATFORM(EFL)
499
handle()->receivedRequestToContinueWithoutCredential(challenge);
500
ASSERT(!handle()->hasAuthenticationChallenge());
502
didFail(blockedError());
506
void ResourceLoader::didCancelAuthenticationChallenge(const AuthenticationChallenge& challenge)
508
// Protect this in this delegate method since the additional processing can do
509
// anything including possibly derefing this; one example of this is Radar 3266216.
510
RefPtr<ResourceLoader> protector(this);
511
frameLoader()->notifier()->didCancelAuthenticationChallenge(this, challenge);
514
#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
515
bool ResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
517
RefPtr<ResourceLoader> protector(this);
518
return frameLoader()->client()->canAuthenticateAgainstProtectionSpace(documentLoader(), identifier(), protectionSpace);
522
void ResourceLoader::receivedCancellation(const AuthenticationChallenge&)
527
void ResourceLoader::willCacheResponse(ResourceHandle*, CacheStoragePolicy& policy)
529
// <rdar://problem/7249553> - There are reports of crashes with this method being called
530
// with a null m_frame->settings(), which can only happen if the frame doesn't have a page.
531
// Sadly we have no reproducible cases of this.
532
// We think that any frame without a page shouldn't have any loads happening in it, yet
533
// there is at least one code path where that is not true.
534
ASSERT(m_frame->settings());
536
// When in private browsing mode, prevent caching to disk
537
if (policy == StorageAllowed && m_frame->settings() && m_frame->settings()->privateBrowsingEnabled())
538
policy = StorageAllowedInMemoryOnly;
542
AsyncFileStream* ResourceLoader::createAsyncFileStream(FileStreamClient* client)
544
// It is OK to simply return a pointer since AsyncFileStream::create adds an extra ref.
545
return AsyncFileStream::create(m_frame->document()->scriptExecutionContext(), client).get();
549
void ResourceLoader::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
551
MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::Loader);
552
info.addMember(m_handle.get());
553
info.addMember(m_frame);
554
info.addMember(m_documentLoader);
555
info.addMember(m_request);
556
info.addMember(m_originalRequest);
557
info.addMember(m_resourceData);
558
info.addMember(m_deferredRequest);
559
info.addMember(m_options);
563
void ResourceLoader::setIdentifier(unsigned long identifier)
565
// FIXME (NetworkProcess): This is temporary to allow WebKit to directly set the identifier on a ResourceLoader.
566
// More permanently we'll want the identifier to be piped through ResourceLoader::init/start so
567
// it always has it, especially in willSendRequest.
568
m_identifier = identifier;