ApplicationCacheHost.cpp [plain text]
#include "config.h"
#include "ApplicationCacheHost.h"
#include "ApplicationCacheHostInternal.h"
#include "DOMApplicationCache.h"
#include "DocumentLoader.h"
#include "Frame.h"
#include "InspectorApplicationCacheAgent.h"
#include "InspectorInstrumentation.h"
#include "Page.h"
#include "ProgressEvent.h"
#include "SecurityOrigin.h"
#include "Settings.h"
#include "WebFrameImpl.h"
#include "WrappedResourceRequest.h"
#include "WrappedResourceResponse.h"
#include "platform/WebURL.h"
#include "platform/WebURLError.h"
#include "platform/WebURLResponse.h"
#include "platform/WebVector.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 = adoptPtr(new ApplicationCacheHostInternal(this));
if (m_internal->m_outerHost) {
WrappedResourceRequest wrapped(request);
m_internal->m_outerHost->willStartMainResourceRequest(wrapped, WebFrameImpl::fromFrame(m_documentLoader->frame()));
} 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)) {
Frame* frame = m_documentLoader->frame();
frame->navigationScheduler()->scheduleLocationChange(frame->document()->securityOrigin(),
frame->document()->url(), frame->loader()->referrer());
}
}
}
void ApplicationCacheHost::maybeLoadMainResourceForRedirect(ResourceRequest&, SubstituteData&)
{
}
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()
{
return !isApplicationCacheEnabled() || status() == UNCACHED;
}
void ApplicationCacheHost::setDOMApplicationCache(DOMApplicationCache* domApplicationCache)
{
ASSERT(!m_domApplicationCache || !domApplicationCache);
m_domApplicationCache = domApplicationCache;
}
void ApplicationCacheHost::notifyDOMApplicationCache(EventID id, int total, int done)
{
#if ENABLE(INSPECTOR)
if (id != PROGRESS_EVENT)
InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
#endif
if (m_defersEvents) {
m_deferredEvents.append(DeferredEvent(id, total, done));
return;
}
dispatchDOMEvent(id, total, done);
}
#if ENABLE(INSPECTOR)
ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
{
if (!m_internal)
return CacheInfo(KURL(), 0, 0, 0);
WebKit::WebApplicationCacheHost::CacheInfo webInfo;
m_internal->m_outerHost->getAssociatedCacheInfo(&webInfo);
return CacheInfo(webInfo.manifestURL, webInfo.creationTime, webInfo.updateTime, webInfo.totalSize);
}
void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
{
if (!m_internal)
return;
WebKit::WebVector<WebKit::WebApplicationCacheHost::ResourceInfo> webResources;
m_internal->m_outerHost->getResourceList(&webResources);
for (size_t i = 0; i < webResources.size(); ++i) {
resources->append(ResourceInfo(
webResources[i].url, webResources[i].isMaster, webResources[i].isManifest, webResources[i].isFallback,
webResources[i].isForeign, webResources[i].isExplicit, webResources[i].size));
}
}
#endif
void ApplicationCacheHost::stopDeferringEvents()
{
RefPtr<DocumentLoader> protect(documentLoader());
for (unsigned i = 0; i < m_deferredEvents.size(); ++i) {
const DeferredEvent& deferred = m_deferredEvents[i];
dispatchDOMEvent(deferred.eventID, deferred.progressTotal, deferred.progressDone);
}
m_deferredEvents.clear();
m_defersEvents = false;
}
void ApplicationCacheHost::stopLoadingInFrame(Frame* frame)
{
}
void ApplicationCacheHost::dispatchDOMEvent(EventID id, int total, int done)
{
if (m_domApplicationCache) {
const AtomicString& eventType = DOMApplicationCache::toEventType(id);
ExceptionCode ec = 0;
RefPtr<Event> event;
if (id == PROGRESS_EVENT)
event = ProgressEvent::create(eventType, true, done, total);
else
event = Event::create(eventType, false, false);
m_domApplicationCache->dispatchEvent(event, ec);
ASSERT(!ec);
}
}
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()
{
bool success = m_internal ? m_internal->m_outerHost->swapCache() : false;
if (success)
InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
return success;
}
void ApplicationCacheHost::abort()
{
}
bool ApplicationCacheHost::isApplicationCacheEnabled()
{
ASSERT(m_documentLoader->frame());
return m_documentLoader->frame()->settings()
&& m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
}
}