SubresourceLoader.cpp [plain text]
#include "config.h"
#include "SubresourceLoader.h"
#include "DocumentLoader.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "ResourceHandle.h"
#include "SecurityOrigin.h"
#include "SubresourceLoaderClient.h"
#include <wtf/RefCountedLeakCounter.h>
namespace WebCore {
#ifndef NDEBUG
static WTF::RefCountedLeakCounter subresourceLoaderCounter("SubresourceLoader");
#endif
SubresourceLoader::SubresourceLoader(Frame* frame, SubresourceLoaderClient* client, bool sendResourceLoadCallbacks, bool shouldContentSniff)
: ResourceLoader(frame, sendResourceLoadCallbacks, shouldContentSniff)
, m_client(client)
, m_loadingMultipartContent(false)
{
#ifndef NDEBUG
subresourceLoaderCounter.increment();
#endif
m_documentLoader->addSubresourceLoader(this);
}
SubresourceLoader::~SubresourceLoader()
{
#ifndef NDEBUG
subresourceLoaderCounter.decrement();
#endif
}
PassRefPtr<SubresourceLoader> SubresourceLoader::create(Frame* frame, SubresourceLoaderClient* client, const ResourceRequest& request, SecurityCheckPolicy securityCheck, bool sendResourceLoadCallbacks, bool shouldContentSniff)
{
if (!frame)
return 0;
FrameLoader* fl = frame->loader();
if (securityCheck == DoSecurityCheck && (fl->state() == FrameStateProvisional || !fl->activeDocumentLoader() || fl->activeDocumentLoader()->isStopping()))
return 0;
ResourceRequest newRequest = request;
if (securityCheck == DoSecurityCheck
&& SecurityOrigin::restrictAccessToLocal()
&& !SecurityOrigin::canLoad(request.url(), String(), frame->document())) {
FrameLoader::reportLocalLoadFailed(frame, request.url().string());
return 0;
}
if (SecurityOrigin::shouldHideReferrer(request.url(), fl->outgoingReferrer()))
newRequest.clearHTTPReferrer();
else if (!request.httpReferrer())
newRequest.setHTTPReferrer(fl->outgoingReferrer());
FrameLoader::addHTTPOriginIfNeeded(newRequest, fl->outgoingOrigin());
fl->addExtraFieldsToSubresourceRequest(newRequest);
RefPtr<SubresourceLoader> subloader(adoptRef(new SubresourceLoader(frame, client, sendResourceLoadCallbacks, shouldContentSniff)));
if (!subloader->load(newRequest))
return 0;
return subloader.release();
}
void SubresourceLoader::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
{
KURL previousURL = request().url();
ResourceLoader::willSendRequest(newRequest, redirectResponse);
if (!previousURL.isNull() && !newRequest.isNull() && previousURL != newRequest.url() && m_client)
m_client->willSendRequest(this, newRequest, redirectResponse);
}
void SubresourceLoader::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
{
RefPtr<SubresourceLoader> protect(this);
if (m_client)
m_client->didSendData(this, bytesSent, totalBytesToBeSent);
}
void SubresourceLoader::didReceiveResponse(const ResourceResponse& r)
{
ASSERT(!r.isNull());
if (r.isMultipart())
m_loadingMultipartContent = true;
RefPtr<SubresourceLoader> protect(this);
if (m_client)
m_client->didReceiveResponse(this, r);
if (reachedTerminalState())
return;
ResourceLoader::didReceiveResponse(r);
RefPtr<SharedBuffer> buffer = resourceData();
if (m_loadingMultipartContent && buffer && buffer->size()) {
if (m_client)
m_client->didReceiveData(this, buffer->data(), buffer->size());
clearResourceData();
m_documentLoader->subresourceLoaderFinishedLoadingOnePart(this);
didFinishLoadingOnePart();
}
}
void SubresourceLoader::didReceiveData(const char* data, int length, long long lengthReceived, bool allAtOnce)
{
RefPtr<SubresourceLoader> protect(this);
ResourceLoader::didReceiveData(data, length, lengthReceived, allAtOnce);
if (!m_loadingMultipartContent && m_client)
m_client->didReceiveData(this, data, length);
}
void SubresourceLoader::didFinishLoading()
{
if (cancelled())
return;
ASSERT(!reachedTerminalState());
RefPtr<SubresourceLoader> protect(this);
if (resourceData())
resourceData()->shouldUsePurgeableMemory(true);
if (m_client)
m_client->didFinishLoading(this);
m_handle = 0;
if (cancelled())
return;
m_documentLoader->removeSubresourceLoader(this);
ResourceLoader::didFinishLoading();
}
void SubresourceLoader::didFail(const ResourceError& error)
{
if (cancelled())
return;
ASSERT(!reachedTerminalState());
RefPtr<SubresourceLoader> protect(this);
if (m_client)
m_client->didFail(this, error);
m_handle = 0;
if (cancelled())
return;
m_documentLoader->removeSubresourceLoader(this);
ResourceLoader::didFail(error);
}
void SubresourceLoader::didCancel(const ResourceError& error)
{
ASSERT(!reachedTerminalState());
RefPtr<SubresourceLoader> protect(this);
if (m_client)
m_client->didFail(this, error);
if (cancelled())
return;
ASSERT(!reachedTerminalState());
if (reachedTerminalState())
return;
m_documentLoader->removeSubresourceLoader(this);
ResourceLoader::didCancel(error);
}
bool SubresourceLoader::shouldUseCredentialStorage()
{
RefPtr<SubresourceLoader> protect(this);
bool shouldUse;
if (m_client && m_client->getShouldUseCredentialStorage(this, shouldUse))
return shouldUse;
return ResourceLoader::shouldUseCredentialStorage();
}
void SubresourceLoader::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
{
RefPtr<SubresourceLoader> protect(this);
if (m_client)
m_client->didReceiveAuthenticationChallenge(this, challenge);
if (reachedTerminalState())
return;
ResourceLoader::didReceiveAuthenticationChallenge(challenge);
}
void SubresourceLoader::receivedCancellation(const AuthenticationChallenge& challenge)
{
ASSERT(!reachedTerminalState());
RefPtr<SubresourceLoader> protect(this);
if (m_client)
m_client->receivedCancellation(this, challenge);
ResourceLoader::receivedCancellation(challenge);
}
}