CachedResourceLoader.cpp [plain text]
#include "config.h"
#include "CachedResourceLoader.h"
#include "CachedCSSStyleSheet.h"
#include "CachedFont.h"
#include "CachedImage.h"
#include "CachedResourceRequest.h"
#include "CachedScript.h"
#include "CachedXSLStyleSheet.h"
#include "Console.h"
#include "ContentSecurityPolicy.h"
#include "DOMWindow.h"
#include "Document.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "HTMLElement.h"
#include "Logging.h"
#include "MemoryCache.h"
#include "PingLoader.h"
#include "ResourceLoadScheduler.h"
#include "SecurityOrigin.h"
#include "Settings.h"
#include <wtf/UnusedParam.h>
#include <wtf/text/CString.h>
#include <wtf/text/WTFString.h>
#define PRELOAD_DEBUG 0
namespace WebCore {
static CachedResource* createResource(CachedResource::Type type, const KURL& url, const String& charset)
{
switch (type) {
case CachedResource::ImageResource:
return new CachedImage(url.string());
case CachedResource::CSSStyleSheet:
return new CachedCSSStyleSheet(url.string(), charset);
case CachedResource::Script:
return new CachedScript(url.string(), charset);
case CachedResource::FontResource:
return new CachedFont(url.string());
#if ENABLE(XSLT)
case CachedResource::XSLStyleSheet:
return new CachedXSLStyleSheet(url.string());
#endif
#if ENABLE(LINK_PREFETCH)
case CachedResource::LinkResource:
return new CachedResource(url.string(), CachedResource::LinkResource);
#endif
}
ASSERT_NOT_REACHED();
return 0;
}
CachedResourceLoader::CachedResourceLoader(Document* document)
: m_document(document)
, m_requestCount(0)
, m_loadDoneActionTimer(this, &CachedResourceLoader::loadDoneActionTimerFired)
, m_autoLoadImages(true)
, m_loadFinishing(false)
, m_allowStaleResources(false)
{
}
CachedResourceLoader::~CachedResourceLoader()
{
m_document = 0;
cancelRequests();
clearPreloads();
DocumentResourceMap::iterator end = m_documentResources.end();
for (DocumentResourceMap::iterator it = m_documentResources.begin(); it != end; ++it)
it->second->setOwningCachedResourceLoader(0);
ASSERT(m_requestCount == 0);
}
CachedResource* CachedResourceLoader::cachedResource(const String& resourceURL) const
{
KURL url = m_document->completeURL(resourceURL);
return cachedResource(url);
}
CachedResource* CachedResourceLoader::cachedResource(const KURL& resourceURL) const
{
KURL url = MemoryCache::removeFragmentIdentifierIfNeeded(resourceURL);
return m_documentResources.get(url).get();
}
Frame* CachedResourceLoader::frame() const
{
return m_document ? m_document->frame() : 0;
}
CachedImage* CachedResourceLoader::requestImage(const String& url)
{
if (Frame* f = frame()) {
Settings* settings = f->settings();
if (!f->loader()->client()->allowImages(!settings || settings->areImagesEnabled()))
return 0;
if (f->loader()->pageDismissalEventBeingDispatched()) {
KURL completeURL = m_document->completeURL(url);
if (completeURL.isValid() && canRequest(CachedResource::ImageResource, completeURL))
PingLoader::loadImage(f, completeURL);
return 0;
}
}
CachedImage* resource = static_cast<CachedImage*>(requestResource(CachedResource::ImageResource, url, String()));
if (autoLoadImages() && resource && resource->stillNeedsLoad()) {
resource->setLoading(true);
load(resource, true);
}
return resource;
}
CachedFont* CachedResourceLoader::requestFont(const String& url)
{
return static_cast<CachedFont*>(requestResource(CachedResource::FontResource, url, String()));
}
CachedCSSStyleSheet* CachedResourceLoader::requestCSSStyleSheet(const String& url, const String& charset, ResourceLoadPriority priority)
{
return static_cast<CachedCSSStyleSheet*>(requestResource(CachedResource::CSSStyleSheet, url, charset, priority));
}
CachedCSSStyleSheet* CachedResourceLoader::requestUserCSSStyleSheet(const String& requestURL, const String& charset)
{
KURL url = MemoryCache::removeFragmentIdentifierIfNeeded(KURL(KURL(), requestURL));
if (CachedResource* existing = memoryCache()->resourceForURL(url)) {
if (existing->type() == CachedResource::CSSStyleSheet)
return static_cast<CachedCSSStyleSheet*>(existing);
memoryCache()->remove(existing);
}
CachedCSSStyleSheet* userSheet = new CachedCSSStyleSheet(url, charset);
bool inCache = memoryCache()->add(userSheet);
if (!inCache)
userSheet->setInCache(true);
userSheet->load(this, false, SkipSecurityCheck, false);
if (!inCache)
userSheet->setInCache(false);
return userSheet;
}
CachedScript* CachedResourceLoader::requestScript(const String& url, const String& charset)
{
return static_cast<CachedScript*>(requestResource(CachedResource::Script, url, charset));
}
#if ENABLE(XSLT)
CachedXSLStyleSheet* CachedResourceLoader::requestXSLStyleSheet(const String& url)
{
return static_cast<CachedXSLStyleSheet*>(requestResource(CachedResource::XSLStyleSheet, url, String()));
}
#endif
#if ENABLE(LINK_PREFETCH)
CachedResource* CachedResourceLoader::requestLinkResource(const String& url, ResourceLoadPriority priority)
{
ASSERT(frame());
return requestResource(CachedResource::LinkResource, url, String(), priority);
}
#endif
bool CachedResourceLoader::canRequest(CachedResource::Type type, const KURL& url, bool forPreload)
{
if (!document()->securityOrigin()->canDisplay(url)) {
if (!forPreload)
FrameLoader::reportLocalLoadFailed(document()->frame(), url.string());
LOG(ResourceLoading, "CachedResourceLoader::requestResource URL was not allowed by SecurityOrigin::canDisplay");
return 0;
}
switch (type) {
case CachedResource::ImageResource:
case CachedResource::CSSStyleSheet:
case CachedResource::Script:
case CachedResource::FontResource:
#if ENABLE(LINK_PREFETCH)
case CachedResource::LinkResource:
#endif
break;
#if ENABLE(XSLT)
case CachedResource::XSLStyleSheet:
if (!m_document->securityOrigin()->canRequest(url)) {
printAccessDeniedMessage(url);
return false;
}
break;
#endif
}
switch (type) {
case CachedResource::Script:
#if ENABLE(XSLT)
case CachedResource::XSLStyleSheet:
#endif
case CachedResource::CSSStyleSheet:
if (Frame* f = frame())
if (!f->loader()->checkIfRunInsecureContent(m_document->securityOrigin(), url))
return false;
break;
case CachedResource::ImageResource:
case CachedResource::FontResource: {
if (Frame* f = frame()) {
Frame* top = f->tree()->top();
if (!top->loader()->checkIfDisplayInsecureContent(top->document()->securityOrigin(), url))
return false;
}
break;
}
#if ENABLE(LINK_PREFETCH)
case CachedResource::LinkResource:
break;
#endif
}
switch (type) {
case CachedResource::Script:
if (!m_document->contentSecurityPolicy()->allowScriptFromSource(url))
return false;
break;
#if ENABLE(XSLT)
case CachedResource::XSLStyleSheet:
#endif
case CachedResource::CSSStyleSheet:
if (!m_document->contentSecurityPolicy()->allowStyleFromSource(url))
return false;
break;
case CachedResource::ImageResource:
if (!m_document->contentSecurityPolicy()->allowImageFromSource(url))
return false;
break;
case CachedResource::FontResource: {
if (!m_document->contentSecurityPolicy()->allowFontFromSource(url))
return false;
break;
}
#if ENABLE(LINK_PREFETCH)
case CachedResource::LinkResource:
break;
#endif
}
return true;
}
CachedResource* CachedResourceLoader::requestResource(CachedResource::Type type, const String& resourceURL, const String& charset, ResourceLoadPriority priority, bool forPreload)
{
KURL url = m_document->completeURL(resourceURL);
LOG(ResourceLoading, "CachedResourceLoader::requestResource '%s', charset '%s', priority=%d, forPreload=%u", url.string().latin1().data(), charset.latin1().data(), priority, forPreload);
url = MemoryCache::removeFragmentIdentifierIfNeeded(url);
if (!url.isValid())
return 0;
if (!canRequest(type, url, forPreload))
return 0;
if (memoryCache()->disabled()) {
DocumentResourceMap::iterator it = m_documentResources.find(url.string());
if (it != m_documentResources.end()) {
it->second->setOwningCachedResourceLoader(0);
m_documentResources.remove(it);
}
}
CachedResource* resource = memoryCache()->resourceForURL(url);
switch (determineRevalidationPolicy(type, forPreload, resource)) {
case Load:
resource = loadResource(type, url, charset, priority);
break;
case Reload:
memoryCache()->remove(resource);
resource = loadResource(type, url, charset, priority);
break;
case Revalidate:
resource = revalidateResource(resource, priority);
break;
case Use:
memoryCache()->resourceAccessed(resource);
notifyLoadedFromMemoryCache(resource);
break;
}
if (!resource)
return 0;
ASSERT(resource->url() == url.string());
m_documentResources.set(resource->url(), resource);
return resource;
}
CachedResource* CachedResourceLoader::revalidateResource(CachedResource* resource, ResourceLoadPriority priority)
{
ASSERT(resource);
ASSERT(resource->inCache());
ASSERT(!memoryCache()->disabled());
ASSERT(resource->canUseCacheValidator());
ASSERT(!resource->resourceToRevalidate());
String url = resource->url();
CachedResource* newResource = createResource(resource->type(), KURL(ParsedURLString, url), resource->encoding());
LOG(ResourceLoading, "Resource %p created to revalidate %p", newResource, resource);
newResource->setResourceToRevalidate(resource);
memoryCache()->remove(resource);
memoryCache()->add(newResource);
newResource->setLoadPriority(priority);
newResource->load(this);
m_validatedURLs.add(url);
return newResource;
}
CachedResource* CachedResourceLoader::loadResource(CachedResource::Type type, const KURL& url, const String& charset, ResourceLoadPriority priority)
{
ASSERT(!memoryCache()->resourceForURL(url));
LOG(ResourceLoading, "Loading CachedResource for '%s'.", url.string().latin1().data());
CachedResource* resource = createResource(type, url, charset);
bool inCache = memoryCache()->add(resource);
if (!inCache)
resource->setInCache(true);
resource->setLoadPriority(priority);
resource->load(this);
if (!inCache) {
resource->setOwningCachedResourceLoader(this);
resource->setInCache(false);
}
if (resource->errorOccurred()) {
if (inCache)
memoryCache()->remove(resource);
else
delete resource;
return 0;
}
m_validatedURLs.add(url.string());
return resource;
}
CachedResourceLoader::RevalidationPolicy CachedResourceLoader::determineRevalidationPolicy(CachedResource::Type type, bool forPreload, CachedResource* existingResource) const
{
if (!existingResource)
return Load;
if (forPreload && existingResource->isPreloaded())
return Use;
if (existingResource->type() != type) {
LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to type mismatch.");
return Reload;
}
if (m_allowStaleResources)
return Use;
if (existingResource->isPreloaded())
return Use;
if (cachePolicy() == CachePolicyHistoryBuffer)
return Use;
if (existingResource->response().cacheControlContainsNoStore()) {
LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to Cache-control: no-store.");
return Reload;
}
if (m_validatedURLs.contains(existingResource->url()))
return Use;
if (cachePolicy() == CachePolicyReload) {
LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to CachePolicyReload.");
return Reload;
}
if (existingResource->errorOccurred()) {
LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicye reloading due to resource being in the error state");
return Reload;
}
if (existingResource->isLoading())
return Use;
if (existingResource->mustRevalidateDueToCacheHeaders(cachePolicy())) {
if (existingResource->canUseCacheValidator())
return Revalidate;
LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to missing cache validators.");
return Reload;
}
return Use;
}
void CachedResourceLoader::printAccessDeniedMessage(const KURL& url) const
{
if (url.isNull())
return;
if (!frame())
return;
Settings* settings = frame()->settings();
if (!settings || settings->privateBrowsingEnabled())
return;
String message;
if (m_document->url().isNull())
message = "Unsafe attempt to load URL " + url.string() + '.';
else
message = "Unsafe attempt to load URL " + url.string() + " from frame with URL " + m_document->url().string() + ". Domains, protocols and ports must match.\n";
frame()->domWindow()->console()->addMessage(OtherMessageSource, LogMessageType, ErrorMessageLevel, message, 1, String());
}
void CachedResourceLoader::setAutoLoadImages(bool enable)
{
if (enable == m_autoLoadImages)
return;
m_autoLoadImages = enable;
if (!m_autoLoadImages)
return;
DocumentResourceMap::iterator end = m_documentResources.end();
for (DocumentResourceMap::iterator it = m_documentResources.begin(); it != end; ++it) {
CachedResource* resource = it->second.get();
if (resource->type() == CachedResource::ImageResource) {
CachedImage* image = const_cast<CachedImage*>(static_cast<const CachedImage*>(resource));
if (image->stillNeedsLoad())
load(image, true);
}
}
}
CachePolicy CachedResourceLoader::cachePolicy() const
{
return frame() ? frame()->loader()->subresourceCachePolicy() : CachePolicyVerify;
}
void CachedResourceLoader::removeCachedResource(CachedResource* resource) const
{
#ifndef NDEBUG
DocumentResourceMap::iterator it = m_documentResources.find(resource->url());
if (it != m_documentResources.end())
ASSERT(it->second.get() == resource);
#endif
m_documentResources.remove(resource->url());
}
void CachedResourceLoader::load(CachedResource* resource, bool incremental, SecurityCheckPolicy securityCheck, bool sendResourceLoadCallbacks)
{
incrementRequestCount(resource);
RefPtr<CachedResourceRequest> request = CachedResourceRequest::load(this, resource, incremental, securityCheck, sendResourceLoadCallbacks);
if (request)
m_requests.add(request);
}
void CachedResourceLoader::loadDone(CachedResourceRequest* request)
{
m_loadFinishing = false;
RefPtr<CachedResourceRequest> protect(request);
if (request)
m_requests.remove(request);
if (frame())
frame()->loader()->loadDone();
if (!request) {
m_loadDoneActionTimer.startOneShot(0);
return;
}
performPostLoadActions();
}
void CachedResourceLoader::loadDoneActionTimerFired(Timer<CachedResourceLoader>*)
{
performPostLoadActions();
}
void CachedResourceLoader::performPostLoadActions()
{
checkForPendingPreloads();
resourceLoadScheduler()->servePendingRequests();
}
void CachedResourceLoader::cancelRequests()
{
clearPendingPreloads();
Vector<CachedResourceRequest*, 256> requestsToCancel;
RequestSet::iterator end = m_requests.end();
for (RequestSet::iterator i = m_requests.begin(); i != end; ++i)
requestsToCancel.append((*i).get());
for (unsigned i = 0; i < requestsToCancel.size(); ++i)
requestsToCancel[i]->didFail(true);
}
void CachedResourceLoader::notifyLoadedFromMemoryCache(CachedResource* resource)
{
if (!resource || !frame() || resource->status() != CachedResource::Cached)
return;
frame()->loader()->loadedResourceFromMemoryCache(resource);
}
void CachedResourceLoader::incrementRequestCount(const CachedResource* res)
{
if (res->isLinkResource())
return;
++m_requestCount;
}
void CachedResourceLoader::decrementRequestCount(const CachedResource* res)
{
if (res->isLinkResource())
return;
--m_requestCount;
ASSERT(m_requestCount > -1);
}
int CachedResourceLoader::requestCount()
{
if (m_loadFinishing)
return m_requestCount + 1;
return m_requestCount;
}
void CachedResourceLoader::preload(CachedResource::Type type, const String& url, const String& charset, bool referencedFromBody)
{
UNUSED_PARAM(referencedFromBody);
bool hasRendering = m_document->body() && m_document->body()->renderer();
bool canBlockParser = type == CachedResource::Script || type == CachedResource::CSSStyleSheet;
if (!hasRendering && !canBlockParser) {
PendingPreload pendingPreload = { type, url, charset };
m_pendingPreloads.append(pendingPreload);
return;
}
requestPreload(type, url, charset);
}
void CachedResourceLoader::checkForPendingPreloads()
{
if (m_pendingPreloads.isEmpty() || !m_document->body() || !m_document->body()->renderer())
return;
while (!m_pendingPreloads.isEmpty()) {
PendingPreload preload = m_pendingPreloads.takeFirst();
if (!cachedResource(m_document->completeURL(preload.m_url)))
requestPreload(preload.m_type, preload.m_url, preload.m_charset);
}
m_pendingPreloads.clear();
}
void CachedResourceLoader::requestPreload(CachedResource::Type type, const String& url, const String& charset)
{
String encoding;
if (type == CachedResource::Script || type == CachedResource::CSSStyleSheet)
encoding = charset.isEmpty() ? m_document->charset() : charset;
CachedResource* resource = requestResource(type, url, encoding, ResourceLoadPriorityUnresolved, true);
if (!resource || (m_preloads && m_preloads->contains(resource)))
return;
resource->increasePreloadCount();
if (!m_preloads)
m_preloads = adoptPtr(new ListHashSet<CachedResource*>);
m_preloads->add(resource);
#if PRELOAD_DEBUG
printf("PRELOADING %s\n", resource->url().latin1().data());
#endif
}
void CachedResourceLoader::clearPreloads()
{
#if PRELOAD_DEBUG
printPreloadStats();
#endif
if (!m_preloads)
return;
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)
memoryCache()->remove(res);
}
m_preloads.clear();
}
void CachedResourceLoader::clearPendingPreloads()
{
m_pendingPreloads.clear();
}
#if PRELOAD_DEBUG
void CachedResourceLoader::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())
memoryCache()->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
}