This source file includes following definitions.
- m_resource
- create
- m_requestCountTracker
- releaseResources
- init
- start
- changeToSynchronous
- setDefersLoading
- attachThreadedDataReceiver
- didDownloadData
- didFinishLoadingOnePart
- didChangePriority
- cancelIfNotFinishing
- cancel
- cancel
- willSendRequest
- didReceiveCachedMetadata
- didSendData
- responseNeedsAccessControlCheck
- didReceiveResponse
- didReceiveData
- didFinishLoading
- didFail
- isLoadedBy
- requestSynchronously
- applyOptions
#include "config.h"
#include "core/fetch/ResourceLoader.h"
#include "core/fetch/Resource.h"
#include "core/fetch/ResourceLoaderHost.h"
#include "core/fetch/ResourcePtr.h"
#include "platform/Logging.h"
#include "platform/SharedBuffer.h"
#include "platform/exported/WrappedResourceRequest.h"
#include "platform/exported/WrappedResourceResponse.h"
#include "platform/network/ResourceError.h"
#include "public/platform/Platform.h"
#include "public/platform/WebData.h"
#include "public/platform/WebThreadedDataReceiver.h"
#include "public/platform/WebURLError.h"
#include "public/platform/WebURLRequest.h"
#include "public/platform/WebURLResponse.h"
#include "wtf/Assertions.h"
#include "wtf/CurrentTime.h"
namespace WebCore {
ResourceLoader::RequestCountTracker::RequestCountTracker(ResourceLoaderHost* host, Resource* resource)
: m_host(host)
, m_resource(resource)
{
m_host->incrementRequestCount(m_resource);
}
ResourceLoader::RequestCountTracker::~RequestCountTracker()
{
m_host->decrementRequestCount(m_resource);
}
PassRefPtr<ResourceLoader> ResourceLoader::create(ResourceLoaderHost* host, Resource* resource, const ResourceRequest& request, const ResourceLoaderOptions& options)
{
RefPtr<ResourceLoader> loader(adoptRef(new ResourceLoader(host, resource, options)));
loader->init(request);
return loader.release();
}
ResourceLoader::ResourceLoader(ResourceLoaderHost* host, Resource* resource, const ResourceLoaderOptions& options)
: m_host(host)
, m_notifiedLoadComplete(false)
, m_defersLoading(host->defersLoading())
, m_options(options)
, m_resource(resource)
, m_state(Initialized)
, m_connectionState(ConnectionStateNew)
, m_requestCountTracker(adoptPtr(new RequestCountTracker(host, resource)))
{
}
ResourceLoader::~ResourceLoader()
{
ASSERT(m_state == Terminated);
}
void ResourceLoader::releaseResources()
{
ASSERT(m_state != Terminated);
ASSERT(m_notifiedLoadComplete);
m_requestCountTracker.clear();
m_host->didLoadResource(m_resource);
if (m_state == Terminated)
return;
m_resource->clearLoader();
m_host->willTerminateResourceLoader(this);
ASSERT(m_state != Terminated);
RefPtr<ResourceLoader> protector(this);
m_host.clear();
m_state = Terminated;
if (m_loader) {
m_loader->cancel();
m_loader.clear();
}
m_deferredRequest = ResourceRequest();
}
void ResourceLoader::init(const ResourceRequest& passedRequest)
{
ResourceRequest request(passedRequest);
m_host->willSendRequest(m_resource->identifier(), request, ResourceResponse(), m_options.initiatorInfo);
request.setReportLoadTiming(true);
ASSERT(m_state != Terminated);
ASSERT(!request.isNull());
m_originalRequest = m_request = applyOptions(request);
m_resource->updateRequest(request);
m_host->didInitializeResourceLoader(this);
}
void ResourceLoader::start()
{
ASSERT(!m_loader);
ASSERT(!m_request.isNull());
ASSERT(m_deferredRequest.isNull());
m_host->willStartLoadingResource(m_request);
if (m_options.synchronousPolicy == RequestSynchronously) {
requestSynchronously();
return;
}
if (m_defersLoading) {
m_deferredRequest = m_request;
return;
}
if (m_state == Terminated)
return;
RELEASE_ASSERT(m_connectionState == ConnectionStateNew);
m_connectionState = ConnectionStateStarted;
m_loader = adoptPtr(blink::Platform::current()->createURLLoader());
ASSERT(m_loader);
blink::WrappedResourceRequest wrappedRequest(m_request);
m_loader->loadAsynchronously(wrappedRequest, this);
}
void ResourceLoader::changeToSynchronous()
{
ASSERT(m_options.synchronousPolicy == RequestAsynchronously);
ASSERT(m_loader);
m_loader->cancel();
m_loader.clear();
m_request.setPriority(ResourceLoadPriorityHighest);
m_connectionState = ConnectionStateNew;
requestSynchronously();
}
void ResourceLoader::setDefersLoading(bool defers)
{
m_defersLoading = defers;
if (m_loader)
m_loader->setDefersLoading(defers);
if (!defers && !m_deferredRequest.isNull()) {
m_request = applyOptions(m_deferredRequest);
m_deferredRequest = ResourceRequest();
start();
}
}
void ResourceLoader::attachThreadedDataReceiver(PassOwnPtr<blink::WebThreadedDataReceiver> threadedDataReceiver)
{
if (m_loader) {
blink::WebThreadedDataReceiver* rawThreadedDataReceiver = threadedDataReceiver.leakPtr();
if (!m_loader->attachThreadedDataReceiver(rawThreadedDataReceiver))
delete rawThreadedDataReceiver;
}
}
void ResourceLoader::didDownloadData(blink::WebURLLoader*, int length, int encodedDataLength)
{
RefPtr<ResourceLoader> protect(this);
RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse);
m_host->didDownloadData(m_resource, length, encodedDataLength);
m_resource->didDownloadData(length);
}
void ResourceLoader::didFinishLoadingOnePart(double finishTime, int64 encodedDataLength)
{
if (m_state == Terminated)
return;
if (m_notifiedLoadComplete)
return;
m_notifiedLoadComplete = true;
m_host->didFinishLoading(m_resource, finishTime, encodedDataLength);
}
void ResourceLoader::didChangePriority(ResourceLoadPriority loadPriority, int intraPriorityValue)
{
if (m_loader) {
m_host->didChangeLoadingPriority(m_resource, loadPriority, intraPriorityValue);
m_loader->didChangePriority(static_cast<blink::WebURLRequest::Priority>(loadPriority), intraPriorityValue);
}
}
void ResourceLoader::cancelIfNotFinishing()
{
if (m_state != Initialized)
return;
cancel();
}
void ResourceLoader::cancel()
{
cancel(ResourceError());
}
void ResourceLoader::cancel(const ResourceError& error)
{
if (m_state == Terminated)
return;
if (m_state == Finishing) {
releaseResources();
return;
}
ResourceError nonNullError = error.isNull() ? ResourceError::cancelledError(m_request.url()) : error;
RefPtr<ResourceLoader> protector(this);
WTF_LOG(ResourceLoading, "Cancelled load of '%s'.\n", m_resource->url().string().latin1().data());
if (m_state == Initialized)
m_state = Finishing;
m_resource->setResourceError(nonNullError);
if (m_loader) {
m_connectionState = ConnectionStateCanceled;
m_loader->cancel();
m_loader.clear();
}
if (!m_notifiedLoadComplete) {
m_notifiedLoadComplete = true;
m_host->didFailLoading(m_resource, nonNullError);
}
if (m_state == Finishing)
m_resource->error(Resource::LoadError);
if (m_state != Terminated)
releaseResources();
}
void ResourceLoader::willSendRequest(blink::WebURLLoader*, blink::WebURLRequest& passedRequest, const blink::WebURLResponse& passedRedirectResponse)
{
RefPtr<ResourceLoader> protect(this);
ResourceRequest& request(applyOptions(passedRequest.toMutableResourceRequest()));
ASSERT(!request.isNull());
const ResourceResponse& redirectResponse(passedRedirectResponse.toResourceResponse());
ASSERT(!redirectResponse.isNull());
if (!m_host->canAccessRedirect(m_resource, request, redirectResponse, m_options)) {
cancel();
return;
}
applyOptions(request);
m_host->redirectReceived(m_resource, redirectResponse);
m_resource->willSendRequest(request, redirectResponse);
if (request.isNull() || m_state == Terminated)
return;
m_host->willSendRequest(m_resource->identifier(), request, redirectResponse, m_options.initiatorInfo);
request.setReportLoadTiming(true);
ASSERT(!request.isNull());
m_resource->updateRequest(request);
m_request = request;
}
void ResourceLoader::didReceiveCachedMetadata(blink::WebURLLoader*, const char* data, int length)
{
RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse || m_connectionState == ConnectionStateReceivingData);
ASSERT(m_state == Initialized);
m_resource->setSerializedCachedMetadata(data, length);
}
void ResourceLoader::didSendData(blink::WebURLLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
{
ASSERT(m_state == Initialized);
RefPtr<ResourceLoader> protect(this);
m_resource->didSendData(bytesSent, totalBytesToBeSent);
}
bool ResourceLoader::responseNeedsAccessControlCheck() const
{
return m_options.corsEnabled == IsCORSEnabled;
}
void ResourceLoader::didReceiveResponse(blink::WebURLLoader*, const blink::WebURLResponse& response)
{
ASSERT(!response.isNull());
ASSERT(m_state == Initialized);
bool isMultipartPayload = response.isMultipartPayload();
bool isValidStateTransition = (m_connectionState == ConnectionStateStarted || m_connectionState == ConnectionStateReceivedResponse);
RELEASE_ASSERT(isMultipartPayload || isValidStateTransition);
m_connectionState = ConnectionStateReceivedResponse;
const ResourceResponse& resourceResponse = response.toResourceResponse();
if (responseNeedsAccessControlCheck()) {
Resource* resource = m_resource;
if (resource->isCacheValidator() && resourceResponse.httpStatusCode() == 304)
resource = m_resource->resourceToRevalidate();
else
m_resource->setResponse(resourceResponse);
if (!m_host->canAccessResource(resource, m_options.securityOrigin.get(), response.url())) {
m_host->didReceiveResponse(m_resource, resourceResponse);
cancel();
return;
}
}
RefPtr<ResourceLoader> protect(this);
m_resource->responseReceived(resourceResponse);
if (m_state == Terminated)
return;
m_host->didReceiveResponse(m_resource, resourceResponse);
if (response.toResourceResponse().isMultipart()) {
m_requestCountTracker.clear();
if (!m_resource->isImage()) {
cancel();
return;
}
} else if (isMultipartPayload) {
m_host->subresourceLoaderFinishedLoadingOnePart(this);
didFinishLoadingOnePart(0, blink::WebURLLoaderClient::kUnknownEncodedDataLength);
}
if (m_resource->response().httpStatusCode() < 400 || m_resource->shouldIgnoreHTTPStatusCodeErrors())
return;
m_state = Finishing;
if (!m_notifiedLoadComplete) {
m_notifiedLoadComplete = true;
m_host->didFailLoading(m_resource, ResourceError::cancelledError(m_request.url()));
}
m_resource->error(Resource::LoadError);
cancel();
}
void ResourceLoader::didReceiveData(blink::WebURLLoader*, const char* data, int length, int encodedDataLength)
{
RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse || m_connectionState == ConnectionStateReceivingData);
m_connectionState = ConnectionStateReceivingData;
if (m_resource->response().httpStatusCode() >= 400 && !m_resource->shouldIgnoreHTTPStatusCodeErrors())
return;
ASSERT(m_state == Initialized);
RefPtr<ResourceLoader> protect(this);
m_host->didReceiveData(m_resource, data, length, encodedDataLength);
m_resource->appendData(data, length);
}
void ResourceLoader::didFinishLoading(blink::WebURLLoader*, double finishTime, int64 encodedDataLength)
{
RELEASE_ASSERT(m_connectionState == ConnectionStateReceivedResponse || m_connectionState == ConnectionStateReceivingData);
m_connectionState = ConnectionStateFinishedLoading;
if (m_state != Initialized)
return;
ASSERT(m_state != Terminated);
WTF_LOG(ResourceLoading, "Received '%s'.", m_resource->url().string().latin1().data());
RefPtr<ResourceLoader> protect(this);
ResourcePtr<Resource> protectResource(m_resource);
m_state = Finishing;
didFinishLoadingOnePart(finishTime, encodedDataLength);
m_resource->finish(finishTime);
if (m_state == Terminated)
return;
releaseResources();
}
void ResourceLoader::didFail(blink::WebURLLoader*, const blink::WebURLError& error)
{
m_connectionState = ConnectionStateFailed;
ASSERT(m_state != Terminated);
WTF_LOG(ResourceLoading, "Failed to load '%s'.\n", m_resource->url().string().latin1().data());
RefPtr<ResourceLoader> protect(this);
RefPtr<ResourceLoaderHost> protectHost(m_host);
ResourcePtr<Resource> protectResource(m_resource);
m_state = Finishing;
m_resource->setResourceError(error);
if (!m_notifiedLoadComplete) {
m_notifiedLoadComplete = true;
m_host->didFailLoading(m_resource, error);
}
m_resource->error(Resource::LoadError);
if (m_state == Terminated)
return;
releaseResources();
}
bool ResourceLoader::isLoadedBy(ResourceLoaderHost* loader) const
{
return m_host->isLoadedBy(loader);
}
void ResourceLoader::requestSynchronously()
{
OwnPtr<blink::WebURLLoader> loader = adoptPtr(blink::Platform::current()->createURLLoader());
ASSERT(loader);
ASSERT(!m_request.downloadToFile());
RefPtr<ResourceLoader> protect(this);
RefPtr<ResourceLoaderHost> protectHost(m_host);
ResourcePtr<Resource> protectResource(m_resource);
RELEASE_ASSERT(m_connectionState == ConnectionStateNew);
m_connectionState = ConnectionStateStarted;
blink::WrappedResourceRequest requestIn(m_request);
blink::WebURLResponse responseOut;
responseOut.initialize();
blink::WebURLError errorOut;
blink::WebData dataOut;
loader->loadSynchronously(requestIn, responseOut, errorOut, dataOut);
if (errorOut.reason) {
didFail(0, errorOut);
return;
}
didReceiveResponse(0, responseOut);
if (m_state == Terminated)
return;
RefPtr<ResourceLoadInfo> resourceLoadInfo = responseOut.toResourceResponse().resourceLoadInfo();
int64 encodedDataLength = resourceLoadInfo ? resourceLoadInfo->encodedDataLength : blink::WebURLLoaderClient::kUnknownEncodedDataLength;
m_host->didReceiveData(m_resource, dataOut.data(), dataOut.size(), encodedDataLength);
m_resource->setResourceBuffer(dataOut);
didFinishLoading(0, monotonicallyIncreasingTime(), encodedDataLength);
}
ResourceRequest& ResourceLoader::applyOptions(ResourceRequest& request) const
{
request.setAllowStoredCredentials(m_options.allowCredentials == AllowStoredCredentials);
return request;
}
}