ApplicationCacheHost.cpp [plain text]
#include "config.h"
#include "ApplicationCacheHost.h"
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
#include "ApplicationCacheHostInternal.h"
#include "DocumentLoader.h"
#include "DOMApplicationCache.h"
#include "Frame.h"
#include "Settings.h"
#include "WebURL.h"
#include "WebURLError.h"
#include "WebURLResponse.h"
#include "WrappedResourceRequest.h"
#include "WrappedResourceResponse.h"
using namespace WebKit;
namespace WebCore {
ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
: m_domApplicationCache(0)
, m_documentLoader(documentLoader)
, m_defersEvents(true)
{
ASSERT(m_documentLoader);
}
ApplicationCacheHost::~ApplicationCacheHost()
{
}
void ApplicationCacheHost::maybeLoadMainResource(ResourceRequest& request, SubstituteData&)
{
if (!isApplicationCacheEnabled())
return;
m_internal.set(new ApplicationCacheHostInternal(this));
if (m_internal->m_outerHost) {
WrappedResourceRequest wrapped(request);
m_internal->m_outerHost->willStartMainResourceRequest(wrapped);
} else
m_internal.clear();
}
void ApplicationCacheHost::selectCacheWithoutManifest()
{
if (m_internal)
m_internal->m_outerHost->selectCacheWithoutManifest();
}
void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
{
if (m_internal) {
if (!m_internal->m_outerHost->selectCacheWithManifest(manifestURL)) {
const KURL& docURL = m_documentLoader->frame()->document()->url();
String referrer = m_documentLoader->frameLoader()->referrer();
m_documentLoader->frame()->redirectScheduler()->scheduleLocationChange(docURL, referrer);
}
}
}
bool ApplicationCacheHost::maybeLoadFallbackForMainResponse(const ResourceRequest&, const ResourceResponse& response)
{
if (m_internal) {
WrappedResourceResponse wrapped(response);
m_internal->m_outerHost->didReceiveResponseForMainResource(wrapped);
}
return false;
}
bool ApplicationCacheHost::maybeLoadFallbackForMainError(const ResourceRequest&, const ResourceError& error)
{
return false;
}
void ApplicationCacheHost::mainResourceDataReceived(const char* data, int length, long long, bool)
{
if (m_internal)
m_internal->m_outerHost->didReceiveDataForMainResource(data, length);
}
void ApplicationCacheHost::failedLoadingMainResource()
{
if (m_internal)
m_internal->m_outerHost->didFinishLoadingMainResource(false);
}
void ApplicationCacheHost::finishedLoadingMainResource()
{
if (m_internal)
m_internal->m_outerHost->didFinishLoadingMainResource(true);
}
bool ApplicationCacheHost::maybeLoadResource(ResourceLoader*, ResourceRequest& request, const KURL&)
{
if (m_internal) {
WrappedResourceRequest wrapped(request);
m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
}
return false;
}
bool ApplicationCacheHost::maybeLoadFallbackForRedirect(ResourceLoader*, ResourceRequest&, const ResourceResponse&)
{
return false;
}
bool ApplicationCacheHost::maybeLoadFallbackForResponse(ResourceLoader*, const ResourceResponse&)
{
return false;
}
bool ApplicationCacheHost::maybeLoadFallbackForError(ResourceLoader*, const ResourceError&)
{
return false;
}
bool ApplicationCacheHost::maybeLoadSynchronously(ResourceRequest& request, ResourceError&, ResourceResponse&, Vector<char>&)
{
if (m_internal) {
WrappedResourceRequest wrapped(request);
m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
}
return false;
}
void ApplicationCacheHost::maybeLoadFallbackSynchronously(const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>&)
{
}
bool ApplicationCacheHost::canCacheInPageCache() const
{
return false;
}
void ApplicationCacheHost::setDOMApplicationCache(DOMApplicationCache* domApplicationCache)
{
ASSERT(!m_domApplicationCache || !domApplicationCache);
m_domApplicationCache = domApplicationCache;
}
void ApplicationCacheHost::notifyDOMApplicationCache(EventID id)
{
if (m_defersEvents) {
m_deferredEvents.append(id);
return;
}
if (m_domApplicationCache) {
ExceptionCode ec = 0;
m_domApplicationCache->dispatchEvent(Event::create(DOMApplicationCache::toEventType(id), false, false), ec);
ASSERT(!ec);
}
}
void ApplicationCacheHost::stopDeferringEvents()
{
RefPtr<DocumentLoader> protect(documentLoader());
for (unsigned i = 0; i < m_deferredEvents.size(); ++i) {
EventID id = m_deferredEvents[i];
if (m_domApplicationCache) {
ExceptionCode ec = 0;
m_domApplicationCache->dispatchEvent(Event::create(DOMApplicationCache::toEventType(id), false, false), ec);
ASSERT(!ec);
}
}
m_deferredEvents.clear();
m_defersEvents = false;
}
ApplicationCacheHost::Status ApplicationCacheHost::status() const
{
return m_internal ? static_cast<Status>(m_internal->m_outerHost->status()) : UNCACHED;
}
bool ApplicationCacheHost::update()
{
return m_internal ? m_internal->m_outerHost->startUpdate() : false;
}
bool ApplicationCacheHost::swapCache()
{
return m_internal ? m_internal->m_outerHost->swapCache() : false;
}
bool ApplicationCacheHost::isApplicationCacheEnabled()
{
ASSERT(m_documentLoader->frame());
return m_documentLoader->frame()->settings()
&& m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
}
}
#endif // ENABLE(OFFLINE_WEB_APPLICATIONS)