AssociatedURLLoader.cpp [plain text]
#include "config.h"
#include "AssociatedURLLoader.h"
#include "DocumentThreadableLoader.h"
#include "DocumentThreadableLoaderClient.h"
#include "SubresourceLoader.h"
#include "WebApplicationCacheHost.h"
#include "WebDataSource.h"
#include "WebFrameImpl.h"
#include "WebKit.h"
#include "WebKitClient.h"
#include "WebURLError.h"
#include "WebURLLoaderClient.h"
#include "WebURLRequest.h"
#include "WrappedResourceRequest.h"
#include "WrappedResourceResponse.h"
using namespace WebCore;
using namespace WebKit;
using namespace WTF;
namespace WebKit {
class AssociatedURLLoader::ClientAdapter : public DocumentThreadableLoaderClient {
public:
static PassOwnPtr<ClientAdapter> create(AssociatedURLLoader*, WebURLLoaderClient*, bool );
virtual void didSendData(unsigned long long , unsigned long long );
virtual void willSendRequest(ResourceRequest& , const ResourceResponse& );
virtual void didReceiveResponse(const ResourceResponse&);
virtual void didReceiveData(const char*, int );
virtual void didReceiveCachedMetadata(const char*, int );
virtual void didFinishLoading(unsigned long , double );
virtual void didFail(const ResourceError&);
virtual bool isDocumentThreadableLoaderClient() { return true; }
void clearClient() { m_client = 0; }
private:
ClientAdapter(AssociatedURLLoader*, WebURLLoaderClient*, bool );
AssociatedURLLoader* m_loader;
WebURLLoaderClient* m_client;
unsigned long m_downloadLength;
bool m_downloadToFile;
};
PassOwnPtr<AssociatedURLLoader::ClientAdapter> AssociatedURLLoader::ClientAdapter::create(AssociatedURLLoader* loader, WebURLLoaderClient* client, bool downloadToFile)
{
return adoptPtr(new ClientAdapter(loader, client, downloadToFile));
}
AssociatedURLLoader::ClientAdapter::ClientAdapter(AssociatedURLLoader* loader, WebURLLoaderClient* client, bool downloadToFile)
: m_loader(loader)
, m_client(client)
, m_downloadLength(0)
, m_downloadToFile(downloadToFile)
{
ASSERT(m_loader);
ASSERT(m_client);
}
void AssociatedURLLoader::ClientAdapter::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
{
if (!m_client)
return;
WrappedResourceRequest wrappedNewRequest(newRequest);
WrappedResourceResponse wrappedRedirectResponse(redirectResponse);
m_client->willSendRequest(m_loader, wrappedNewRequest, wrappedRedirectResponse);
}
void AssociatedURLLoader::ClientAdapter::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
{
if (!m_client)
return;
m_client->didSendData(m_loader, bytesSent, totalBytesToBeSent);
}
void AssociatedURLLoader::ClientAdapter::didReceiveResponse(const ResourceResponse& response)
{
WrappedResourceResponse wrappedResponse(response);
m_client->didReceiveResponse(m_loader, wrappedResponse);
}
void AssociatedURLLoader::ClientAdapter::didReceiveData(const char* data, int dataLength)
{
if (!m_client)
return;
m_client->didReceiveData(m_loader, data, dataLength, -1);
m_downloadLength += dataLength;
}
void AssociatedURLLoader::ClientAdapter::didReceiveCachedMetadata(const char* data, int dataLength)
{
if (!m_client)
return;
m_client->didReceiveCachedMetadata(m_loader, data, dataLength);
}
void AssociatedURLLoader::ClientAdapter::didFinishLoading(unsigned long identifier, double finishTime)
{
if (!m_client)
return;
if (m_downloadToFile) {
int downloadLength = m_downloadLength <= INT_MAX ? m_downloadLength : INT_MAX;
m_client->didDownloadData(m_loader, downloadLength);
}
m_client->didFinishLoading(m_loader, finishTime);
}
void AssociatedURLLoader::ClientAdapter::didFail(const ResourceError& error)
{
if (!m_client)
return;
WebURLError webError(error);
m_client->didFail(m_loader, webError);
}
AssociatedURLLoader::AssociatedURLLoader(PassRefPtr<WebFrameImpl> frameImpl)
: m_frameImpl(frameImpl)
, m_client(0)
{
ASSERT(m_frameImpl);
m_options.sniffContent = false;
m_options.allowCredentials = true;
m_options.forcePreflight = false;
m_options.crossOriginRequestPolicy = WebURLLoaderOptions::CrossOriginRequestPolicyAllow; }
AssociatedURLLoader::AssociatedURLLoader(PassRefPtr<WebFrameImpl> frameImpl, const WebURLLoaderOptions& options)
: m_frameImpl(frameImpl)
, m_options(options)
, m_client(0)
{
ASSERT(m_frameImpl);
}
AssociatedURLLoader::~AssociatedURLLoader()
{
if (m_clientAdapter)
m_clientAdapter->clearClient();
}
#define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, webcore_name) \
COMPILE_ASSERT(static_cast<int>(WebKit::webkit_name) == static_cast<int>(WebCore::webcore_name), mismatching_enums)
COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyDeny, DenyCrossOriginRequests);
COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl, UseAccessControl);
COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyAllow, AllowCrossOriginRequests);
void AssociatedURLLoader::loadSynchronously(const WebURLRequest& request, WebURLResponse& response, WebURLError& error, WebData& data)
{
ASSERT(0); }
void AssociatedURLLoader::loadAsynchronously(const WebURLRequest& request, WebURLLoaderClient* client)
{
ASSERT(!m_client);
m_client = client;
ASSERT(m_client);
ThreadableLoaderOptions options;
options.sendLoadCallbacks = true; options.sniffContent = m_options.sniffContent;
options.allowCredentials = m_options.allowCredentials;
options.forcePreflight = m_options.forcePreflight;
options.crossOriginRequestPolicy = static_cast<WebCore::CrossOriginRequestPolicy>(m_options.crossOriginRequestPolicy);
options.shouldBufferData = false;
const ResourceRequest& webcoreRequest = request.toResourceRequest();
Document* webcoreDocument = m_frameImpl->frame()->document();
m_clientAdapter = ClientAdapter::create(this, m_client, request.downloadToFile());
m_loader = DocumentThreadableLoader::create(webcoreDocument, m_clientAdapter.get(), webcoreRequest, options);
}
void AssociatedURLLoader::cancel()
{
if (m_loader) {
m_clientAdapter->clearClient();
m_loader->cancel();
}
}
void AssociatedURLLoader::setDefersLoading(bool defersLoading)
{
if (m_loader)
m_loader->setDefersLoading(defersLoading);
}
}