#include "config.h"
#include "DocLoader.h"
#include "Cache.h"
#include "CachedCSSStyleSheet.h"
#include "CachedFont.h"
#include "CachedImage.h"
#include "CachedScript.h"
#include "CachedXSLStyleSheet.h"
#include "CString.h"
#include "Document.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "loader.h"
#define PRELOAD_DEBUG 0
namespace WebCore {
DocLoader::DocLoader(Document* doc)
: m_cache(cache())
, m_cachePolicy(CachePolicyVerify)
, m_doc(doc)
, m_requestCount(0)
, m_autoLoadImages(true)
, m_loadInProgress(false)
, m_allowStaleResources(false)
{
m_cache->addDocLoader(this);
}
DocLoader::~DocLoader()
{
clearPreloads();
HashMap<String, CachedResource*>::iterator end = m_docResources.end();
for (HashMap<String, CachedResource*>::iterator it = m_docResources.begin(); it != end; ++it)
it->second->setDocLoader(0);
m_cache->removeDocLoader(this);
}
Frame* DocLoader::frame() const
{
return m_doc->frame();
}
void DocLoader::checkForReload(const KURL& fullURL)
{
if (m_allowStaleResources)
return; if (m_cachePolicy == CachePolicyVerify) {
if (!m_reloadedURLs.contains(fullURL.string())) {
CachedResource* existing = cache()->resourceForURL(fullURL.string());
if (existing && existing->isExpired() && !existing->isPreloaded()) {
cache()->remove(existing);
m_reloadedURLs.add(fullURL.string());
}
}
} else if ((m_cachePolicy == CachePolicyReload) || (m_cachePolicy == CachePolicyRefresh)) {
if (!m_reloadedURLs.contains(fullURL.string())) {
CachedResource* existing = cache()->resourceForURL(fullURL.string());
if (existing && !existing->isPreloaded()) {
cache()->remove(existing);
m_reloadedURLs.add(fullURL.string());
}
}
}
}
CachedImage* DocLoader::requestImage(const String& url)
{
CachedImage* resource = static_cast<CachedImage*>(requestResource(CachedResource::ImageResource, url));
if (autoLoadImages() && resource && resource->stillNeedsLoad()) {
resource->setLoading(true);
cache()->loader()->load(this, resource, true);
}
return resource;
}
CachedFont* DocLoader::requestFont(const String& url)
{
return static_cast<CachedFont*>(requestResource(CachedResource::FontResource, url));
}
CachedCSSStyleSheet* DocLoader::requestCSSStyleSheet(const String& url, const String& charset, bool isUserStyleSheet)
{
CachedCSSStyleSheet *sheet = static_cast<CachedCSSStyleSheet*>(requestResource(CachedResource::CSSStyleSheet, url, &charset, isUserStyleSheet, !isUserStyleSheet));
if (sheet && isUserStyleSheet) {
sheet->setDocLoader(0);
m_docResources.remove(sheet->url());
}
return sheet;
}
CachedCSSStyleSheet* DocLoader::requestUserCSSStyleSheet(const String& url, const String& charset)
{
return requestCSSStyleSheet(url, charset, true);
}
CachedScript* DocLoader::requestScript(const String& url, const String& charset)
{
return static_cast<CachedScript*>(requestResource(CachedResource::Script, url, &charset));
}
#if ENABLE(XSLT)
CachedXSLStyleSheet* DocLoader::requestXSLStyleSheet(const String& url)
{
return static_cast<CachedXSLStyleSheet*>(requestResource(CachedResource::XSLStyleSheet, url));
}
#endif
#if ENABLE(XBL)
CachedXBLDocument* DocLoader::requestXBLDocument(const String& url)
{
return static_cast<CachedXSLStyleSheet*>(requestResource(CachedResource::XBL, url));
}
#endif
CachedResource* DocLoader::requestResource(CachedResource::Type type, const String& url, const String* charset, bool skipCanLoadCheck, bool sendResourceLoadCallbacks, bool isPreload)
{
KURL fullURL = m_doc->completeURL(url.deprecatedString());
if (cache()->disabled()) {
HashMap<String, CachedResource*>::iterator it = m_docResources.find(fullURL.string());
if (it != m_docResources.end()) {
it->second->setDocLoader(0);
m_docResources.remove(it);
}
}
if (frame() && frame()->loader()->isReloading())
setCachePolicy(CachePolicyReload);
checkForReload(fullURL);
CachedResource* resource = cache()->requestResource(this, type, fullURL, charset, skipCanLoadCheck, sendResourceLoadCallbacks, isPreload);
if (resource) {
m_docResources.set(resource->url(), resource);
checkCacheObjectStatus(resource);
}
return resource;
}
void DocLoader::setAutoLoadImages(bool enable)
{
if (enable == m_autoLoadImages)
return;
m_autoLoadImages = enable;
if (!m_autoLoadImages)
return;
HashMap<String, CachedResource*>::iterator end = m_docResources.end();
for (HashMap<String, CachedResource*>::iterator it = m_docResources.begin(); it != end; ++it) {
CachedResource* resource = it->second;
if (resource->type() == CachedResource::ImageResource) {
CachedImage* image = const_cast<CachedImage*>(static_cast<const CachedImage*>(resource));
if (image->stillNeedsLoad())
cache()->loader()->load(this, image, true);
}
}
}
void DocLoader::setCachePolicy(CachePolicy cachePolicy)
{
m_cachePolicy = cachePolicy;
}
void DocLoader::removeCachedResource(CachedResource* resource) const
{
m_docResources.remove(resource->url());
}
void DocLoader::setLoadInProgress(bool load)
{
m_loadInProgress = load;
if (!load && frame())
frame()->loader()->loadDone();
}
void DocLoader::checkCacheObjectStatus(CachedResource* resource)
{
if (!resource || !frame())
return;
switch (resource->status()) {
case CachedResource::Cached:
break;
case CachedResource::NotCached:
case CachedResource::Unknown:
case CachedResource::New:
case CachedResource::Pending:
return;
}
frame()->loader()->loadedResourceFromMemoryCache(resource);
}
void DocLoader::incrementRequestCount()
{
++m_requestCount;
}
void DocLoader::decrementRequestCount()
{
--m_requestCount;
ASSERT(m_requestCount > -1);
}
int DocLoader::requestCount()
{
if (loadInProgress())
return m_requestCount + 1;
return m_requestCount;
}
void DocLoader::preload(CachedResource::Type type, const String& url, const String& charset, bool inBody)
{
if ((inBody || type == CachedResource::ImageResource) && (!m_doc->body() || !m_doc->body()->renderer())) {
PendingPreload pendingPreload = { type, url, charset };
m_pendingPreloads.append(pendingPreload);
return;
}
requestPreload(type, url, charset);
}
void DocLoader::checkForPendingPreloads()
{
unsigned count = m_pendingPreloads.size();
if (!count || !m_doc->body() || !m_doc->body()->renderer())
return;
for (unsigned n = 0; n < count; ++n) {
PendingPreload& preload = m_pendingPreloads[n];
requestPreload(preload.m_type, preload.m_url, preload.m_charset);
}
m_pendingPreloads.clear();
}
void DocLoader::requestPreload(CachedResource::Type type, const String& url, const String& charset)
{
String encoding;
if (type == CachedResource::Script || type == CachedResource::CSSStyleSheet)
encoding = charset.isEmpty() ? m_doc->frame()->loader()->encoding() : charset;
CachedResource* resource = requestResource(type, url, &encoding, false, true, true);
if (!resource || m_preloads.contains(resource))
return;
resource->increasePreloadCount();
m_preloads.add(resource);
#if PRELOAD_DEBUG
printf("PRELOADING %s\n", resource->url().latin1().data());
#endif
}
void DocLoader::clearPreloads()
{
#if PRELOAD_DEBUG
printPreloadStats();
#endif
ListHashSet<CachedResource*>::iterator end = m_preloads.end();
for (ListHashSet<CachedResource*>::iterator it = m_preloads.begin(); it != end; ++it) {
CachedResource* res = *it;
res->decreasePreloadCount();
if (res->canDelete() && !res->inCache())
delete res;
else if (res->preloadResult() == CachedResource::PreloadNotReferenced)
cache()->remove(res);
}
m_preloads.clear();
}
#if PRELOAD_DEBUG
void DocLoader::printPreloadStats()
{
unsigned scripts = 0;
unsigned scriptMisses = 0;
unsigned stylesheets = 0;
unsigned stylesheetMisses = 0;
unsigned images = 0;
unsigned imageMisses = 0;
ListHashSet<CachedResource*>::iterator end = m_preloads.end();
for (ListHashSet<CachedResource*>::iterator it = m_preloads.begin(); it != end; ++it) {
CachedResource* res = *it;
if (res->preloadResult() == CachedResource::PreloadNotReferenced)
printf("!! UNREFERENCED PRELOAD %s\n", res->url().latin1().data());
else if (res->preloadResult() == CachedResource::PreloadReferencedWhileComplete)
printf("HIT COMPLETE PRELOAD %s\n", res->url().latin1().data());
else if (res->preloadResult() == CachedResource::PreloadReferencedWhileLoading)
printf("HIT LOADING PRELOAD %s\n", res->url().latin1().data());
if (res->type() == CachedResource::Script) {
scripts++;
if (res->preloadResult() < CachedResource::PreloadReferencedWhileLoading)
scriptMisses++;
} else if (res->type() == CachedResource::CSSStyleSheet) {
stylesheets++;
if (res->preloadResult() < CachedResource::PreloadReferencedWhileLoading)
stylesheetMisses++;
} else {
images++;
if (res->preloadResult() < CachedResource::PreloadReferencedWhileLoading)
imageMisses++;
}
if (res->errorOccurred())
cache()->remove(res);
res->decreasePreloadCount();
}
m_preloads.clear();
if (scripts)
printf("SCRIPTS: %d (%d hits, hit rate %d%%)\n", scripts, scripts - scriptMisses, (scripts - scriptMisses) * 100 / scripts);
if (stylesheets)
printf("STYLESHEETS: %d (%d hits, hit rate %d%%)\n", stylesheets, stylesheets - stylesheetMisses, (stylesheets - stylesheetMisses) * 100 / stylesheets);
if (images)
printf("IMAGES: %d (%d hits, hit rate %d%%)\n", images, images - imageMisses, (images - imageMisses) * 100 / images);
}
#endif
}