SubresourceLoader.cpp [plain text]
#include "config.h"
#include "SubresourceLoader.h"
#include "Document.h"
#include "DocumentLoader.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "Logging.h"
#include "ResourceHandle.h"
#include "ResourceRequest.h"
#include "SubresourceLoaderClient.h"
#include "SharedBuffer.h"
namespace WebCore {
#ifndef NDEBUG
WTFLogChannel LogWebCoreSubresourceLoaderLeaks = { 0x00000000, "", WTFLogChannelOn };
struct SubresourceLoaderCounter {
static unsigned count;
~SubresourceLoaderCounter()
{
if (count)
LOG(WebCoreSubresourceLoaderLeaks, "LEAK: %u SubresourceLoader\n", count);
}
};
unsigned SubresourceLoaderCounter::count = 0;
static SubresourceLoaderCounter subresourceLoaderCounter;
#endif
SubresourceLoader::SubresourceLoader(Frame* frame, SubresourceLoaderClient* client, bool sendResourceLoadCallbacks, bool shouldContentSniff)
: ResourceLoader(frame, sendResourceLoadCallbacks, shouldContentSniff)
, m_client(client)
, m_loadingMultipartContent(false)
{
#ifndef NDEBUG
++SubresourceLoaderCounter::count;
#endif
m_documentLoader->addSubresourceLoader(this);
}
SubresourceLoader::~SubresourceLoader()
{
#ifndef NDEBUG
--SubresourceLoaderCounter::count;
#endif
}
bool SubresourceLoader::load(const ResourceRequest& r)
{
m_frame->loader()->didTellClientAboutLoad(r.url().string());
return ResourceLoader::load(r);
}
PassRefPtr<SubresourceLoader> SubresourceLoader::create(Frame* frame, SubresourceLoaderClient* client, const ResourceRequest& request, bool skipCanLoadCheck, bool sendResourceLoadCallbacks, bool shouldContentSniff)
{
if (!frame)
return 0;
FrameLoader* fl = frame->loader();
if (!skipCanLoadCheck && fl->state() == FrameStateProvisional)
return 0;
ResourceRequest newRequest = request;
if (!skipCanLoadCheck
&& FrameLoader::restrictAccessToLocal()
&& !FrameLoader::canLoad(request.url(), frame->document())) {
FrameLoader::reportLocalLoadFailed(frame->page(), request.url().string());
return 0;
}
if (FrameLoader::shouldHideReferrer(request.url(), fl->outgoingReferrer()))
newRequest.clearHTTPReferrer();
else if (!request.httpReferrer())
newRequest.setHTTPReferrer(fl->outgoingReferrer());
if (newRequest.isConditional())
newRequest.setCachePolicy(ReloadIgnoringCacheData);
else
newRequest.setCachePolicy(fl->originalRequest().cachePolicy());
fl->addExtraFieldsToRequest(newRequest, false, false);
RefPtr<SubresourceLoader> subloader(new SubresourceLoader(frame, client, sendResourceLoadCallbacks, shouldContentSniff));
if (!subloader->load(newRequest))
return 0;
return subloader.release();
}
void SubresourceLoader::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
{
ResourceLoader::willSendRequest(newRequest, redirectResponse);
if (!newRequest.isNull() && m_originalURL != newRequest.url() && m_client)
m_client->willSendRequest(this, newRequest, redirectResponse);
}
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 (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;
m_documentLoader->removeSubresourceLoader(this);
ResourceLoader::didCancel(error);
}
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);
}
}