ApplicationCache.cpp [plain text]
#include "config.h"
#include "ApplicationCache.h"
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
#include "ApplicationCacheGroup.h"
#include "ApplicationCacheResource.h"
#include "ApplicationCacheStorage.h"
#include "ResourceRequest.h"
#include <stdio.h>
namespace WebCore {
ApplicationCache::ApplicationCache()
: m_group(0)
, m_manifest(0)
, m_storageID(0)
{
}
ApplicationCache::~ApplicationCache()
{
if (m_group)
m_group->cacheDestroyed(this);
}
void ApplicationCache::setGroup(ApplicationCacheGroup* group)
{
ASSERT(!m_group);
m_group = group;
}
void ApplicationCache::setManifestResource(PassRefPtr<ApplicationCacheResource> manifest)
{
ASSERT(manifest);
ASSERT(!m_manifest);
ASSERT(manifest->type() & ApplicationCacheResource::Manifest);
m_manifest = manifest.get();
addResource(manifest);
}
void ApplicationCache::addResource(PassRefPtr<ApplicationCacheResource> resource)
{
ASSERT(resource);
const String& url = resource->url().string();
ASSERT(!m_resources.contains(url));
if (m_storageID) {
ASSERT(!resource->storageID());
cacheStorage().store(resource.get(), this);
}
m_resources.set(url, resource);
}
unsigned ApplicationCache::removeResource(const String& url)
{
HashMap<String, RefPtr<ApplicationCacheResource> >::iterator it = m_resources.find(url);
if (it == m_resources.end())
return 0;
unsigned type = it->second->type();
m_resources.remove(it);
return type;
}
ApplicationCacheResource* ApplicationCache::resourceForURL(const String& url)
{
return m_resources.get(url).get();
}
bool ApplicationCache::requestIsHTTPOrHTTPSGet(const ResourceRequest& request)
{
if (!request.url().protocolIs("http") && !request.url().protocolIs("https"))
return false;
if (!equalIgnoringCase(request.httpMethod(), "GET"))
return false;
return true;
}
ApplicationCacheResource* ApplicationCache::resourceForRequest(const ResourceRequest& request)
{
if (!requestIsHTTPOrHTTPSGet(request))
return false;
return resourceForURL(request.url().string());
}
unsigned ApplicationCache::numDynamicEntries() const
{
return 0;
}
String ApplicationCache::dynamicEntry(unsigned index) const
{
return String();
}
bool ApplicationCache::addDynamicEntry(const String& url)
{
if (!equalIgnoringCase(m_group->manifestURL().protocol(),
KURL(url.deprecatedString()).protocol()))
return false;
return true;
}
void ApplicationCache::removeDynamicEntry(const String& url)
{
}
void ApplicationCache::setOnlineWhitelist(const HashSet<String>& onlineWhitelist)
{
ASSERT(m_onlineWhitelist.isEmpty());
m_onlineWhitelist = onlineWhitelist;
}
bool ApplicationCache::isURLInOnlineWhitelist(const KURL& url)
{
if (!url.hasRef())
return m_onlineWhitelist.contains(url.string());
KURL copy(url);
copy.setRef(DeprecatedString());
return m_onlineWhitelist.contains(copy.string());
}
void ApplicationCache::clearStorageID()
{
m_storageID = 0;
ResourceMap::const_iterator end = m_resources.end();
for (ResourceMap::const_iterator it = m_resources.begin(); it != end; ++it)
it->second->clearStorageID();
}
#ifndef NDEBUG
void ApplicationCache::dump()
{
HashMap<String, RefPtr<ApplicationCacheResource> >::const_iterator end = m_resources.end();
for (HashMap<String, RefPtr<ApplicationCacheResource> >::const_iterator it = m_resources.begin(); it != end; ++it) {
printf("%s ", it->first.ascii().data());
ApplicationCacheResource::dumpType(it->second->type());
}
}
#endif
}
#endif // ENABLE(OFFLINE_WEB_APPLICATIONS)