#include "config.h"
#include "IconLoader.h"
#include "Document.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "IconDatabase.h"
#include "Logging.h"
#include "ResourceHandle.h"
#include "ResourceLoadScheduler.h"
#include "ResourceResponse.h"
#include "ResourceRequest.h"
#include "SharedBuffer.h"
#include "SubresourceLoader.h"
#include <wtf/UnusedParam.h>
#include <wtf/text/CString.h>
using namespace std;
namespace WebCore {
IconLoader::IconLoader(Frame* frame)
: m_frame(frame)
, m_loadIsInProgress(false)
{
}
PassOwnPtr<IconLoader> IconLoader::create(Frame* frame)
{
return adoptPtr(new IconLoader(frame));
}
IconLoader::~IconLoader()
{
}
void IconLoader::startLoading()
{
if (m_resourceLoader)
return;
m_loadIsInProgress = true;
ResourceRequest resourceRequest(m_frame->loader()->iconURL());
resourceRequest.setPriority(ResourceLoadPriorityLow);
RefPtr<SubresourceLoader> loader = resourceLoadScheduler()->scheduleSubresourceLoad(m_frame, this, resourceRequest);
if (!loader)
LOG_ERROR("Failed to start load for icon at url %s", m_frame->loader()->iconURL().string().ascii().data());
if (m_loadIsInProgress)
m_resourceLoader = loader.release();
}
void IconLoader::stopLoading()
{
clearLoadingState();
}
void IconLoader::didReceiveResponse(SubresourceLoader* resourceLoader, const ResourceResponse& response)
{
int status = response.httpStatusCode();
LOG(IconDatabase, "IconLoader::didReceiveResponse() - Loader %p, response %i", resourceLoader, status);
if (status && (status < 200 || status > 299)) {
ResourceHandle* handle = resourceLoader->handle();
finishLoading(handle ? handle->firstRequest().url() : KURL(), 0);
}
}
void IconLoader::didReceiveData(SubresourceLoader* unusedLoader, const char*, int unusedSize)
{
#if LOG_DISABLED
UNUSED_PARAM(unusedLoader);
UNUSED_PARAM(unusedSize);
#endif
LOG(IconDatabase, "IconLoader::didReceiveData() - Loader %p, number of bytes %i", unusedLoader, unusedSize);
}
void IconLoader::didFail(SubresourceLoader* resourceLoader, const ResourceError&)
{
LOG(IconDatabase, "IconLoader::didFail() - Loader %p", resourceLoader);
if (m_loadIsInProgress) {
ASSERT(resourceLoader == m_resourceLoader);
ResourceHandle* handle = resourceLoader->handle();
finishLoading(handle ? handle->firstRequest().url() : KURL(), 0);
}
}
void IconLoader::didReceiveAuthenticationChallenge(SubresourceLoader*, const AuthenticationChallenge&)
{
m_resourceLoader->cancel();
}
void IconLoader::didFinishLoading(SubresourceLoader* resourceLoader, double)
{
LOG(IconDatabase, "IconLoader::didFinishLoading() - Loader %p", resourceLoader);
if (m_loadIsInProgress) {
ASSERT(resourceLoader == m_resourceLoader);
ResourceHandle* handle = resourceLoader->handle();
finishLoading(handle ? handle->firstRequest().url() : KURL(), m_resourceLoader->resourceData());
}
}
void IconLoader::finishLoading(const KURL& iconURL, PassRefPtr<SharedBuffer> data)
{
if (!iconURL.isEmpty() && m_loadIsInProgress) {
LOG(IconDatabase, "IconLoader::finishLoading() - Committing iconURL %s to database", iconURL.string().ascii().data());
m_frame->loader()->commitIconURLToIconDatabase(iconURL);
iconDatabase().setIconDataForIconURL(data, iconURL.string());
m_frame->loader()->client()->dispatchDidReceiveIcon();
}
clearLoadingState();
}
void IconLoader::clearLoadingState()
{
m_resourceLoader = 0;
m_loadIsInProgress = false;
}
}