1/* 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) 3 Copyright (C) 2001 Dirk Mueller <mueller@kde.org> 4 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. 5 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/ 6 7 This library is free software; you can redistribute it and/or 8 modify it under the terms of the GNU Library General Public 9 License as published by the Free Software Foundation; either 10 version 2 of the License, or (at your option) any later version. 11 12 This library is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Library General Public License for more details. 16 17 You should have received a copy of the GNU Library General Public License 18 along with this library; see the file COPYING.LIB. If not, write to 19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 Boston, MA 02110-1301, USA. 21 22 This class provides all functionality needed for loading images, style sheets and html 23 pages from the web. It has a memory cache for these objects. 24*/ 25 26#ifndef CachedResourceLoader_h 27#define CachedResourceLoader_h 28 29#include "CachePolicy.h" 30#include "CachedResource.h" 31#include "CachedResourceHandle.h" 32#include "CachedResourceRequest.h" 33#include "ResourceLoadPriority.h" 34#include "Timer.h" 35#include <wtf/Deque.h> 36#include <wtf/HashMap.h> 37#include <wtf/HashSet.h> 38#include <wtf/ListHashSet.h> 39#include <wtf/text/StringHash.h> 40 41namespace WebCore { 42 43class CachedCSSStyleSheet; 44class CachedSVGDocument; 45class CachedFont; 46class CachedImage; 47class CachedRawResource; 48class CachedScript; 49class CachedShader; 50class CachedTextTrack; 51class CachedXSLStyleSheet; 52class Document; 53class DocumentLoader; 54class Frame; 55class ImageLoader; 56class KURL; 57 58// The CachedResourceLoader provides a per-context interface to the MemoryCache 59// and enforces a bunch of security checks and rules for resource revalidation. 60// Its lifetime is roughly per-DocumentLoader, in that it is generally created 61// in the DocumentLoader constructor and loses its ability to generate network 62// requests when the DocumentLoader is destroyed. Documents also hold a 63// RefPtr<CachedResourceLoader> for their lifetime (and will create one if they 64// are initialized without a Frame), so a Document can keep a CachedResourceLoader 65// alive past detach if scripts still reference the Document. 66class CachedResourceLoader : public RefCounted<CachedResourceLoader> { 67 WTF_MAKE_NONCOPYABLE(CachedResourceLoader); WTF_MAKE_FAST_ALLOCATED; 68friend class ImageLoader; 69friend class ResourceCacheValidationSuppressor; 70 71public: 72 static PassRefPtr<CachedResourceLoader> create(DocumentLoader* documentLoader) { return adoptRef(new CachedResourceLoader(documentLoader)); } 73 ~CachedResourceLoader(); 74 75 CachedResourceHandle<CachedImage> requestImage(CachedResourceRequest&); 76 CachedResourceHandle<CachedCSSStyleSheet> requestCSSStyleSheet(CachedResourceRequest&); 77 CachedResourceHandle<CachedCSSStyleSheet> requestUserCSSStyleSheet(CachedResourceRequest&); 78 CachedResourceHandle<CachedScript> requestScript(CachedResourceRequest&); 79 CachedResourceHandle<CachedFont> requestFont(CachedResourceRequest&); 80 CachedResourceHandle<CachedRawResource> requestRawResource(CachedResourceRequest&); 81 CachedResourceHandle<CachedRawResource> requestMainResource(CachedResourceRequest&); 82 83#if ENABLE(SVG) 84 CachedResourceHandle<CachedSVGDocument> requestSVGDocument(CachedResourceRequest&); 85#endif 86#if ENABLE(XSLT) 87 CachedResourceHandle<CachedXSLStyleSheet> requestXSLStyleSheet(CachedResourceRequest&); 88#endif 89#if ENABLE(LINK_PREFETCH) 90 CachedResourceHandle<CachedResource> requestLinkResource(CachedResource::Type, CachedResourceRequest&); 91#endif 92#if ENABLE(VIDEO_TRACK) 93 CachedResourceHandle<CachedTextTrack> requestTextTrack(CachedResourceRequest&); 94#endif 95#if ENABLE(CSS_SHADERS) 96 CachedResourceHandle<CachedShader> requestShader(CachedResourceRequest&); 97#endif 98 99 // Logs an access denied message to the console for the specified URL. 100 void printAccessDeniedMessage(const KURL& url) const; 101 102 CachedResource* cachedResource(const String& url) const; 103 CachedResource* cachedResource(const KURL& url) const; 104 105 typedef HashMap<String, CachedResourceHandle<CachedResource> > DocumentResourceMap; 106 const DocumentResourceMap& allCachedResources() const { return m_documentResources; } 107 108 bool autoLoadImages() const { return m_autoLoadImages; } 109 void setAutoLoadImages(bool); 110 111 void setImagesEnabled(bool); 112 113 bool shouldDeferImageLoad(const KURL&) const; 114 115 CachePolicy cachePolicy(CachedResource::Type) const; 116 117 Frame* frame() const; // Can be null 118 Document* document() const { return m_document; } // Can be null 119 void setDocument(Document* document) { m_document = document; } 120 void clearDocumentLoader() { m_documentLoader = 0; } 121 122 void removeCachedResource(CachedResource*) const; 123 void loadDone(CachedResource*); 124 void garbageCollectDocumentResources(); 125 126 void incrementRequestCount(const CachedResource*); 127 void decrementRequestCount(const CachedResource*); 128 int requestCount() const { return m_requestCount; } 129 130 bool isPreloaded(const String& urlString) const; 131 void clearPreloads(); 132 void clearPendingPreloads(); 133 void preload(CachedResource::Type, CachedResourceRequest&, const String& charset); 134 void checkForPendingPreloads(); 135 void printPreloadStats(); 136 bool canRequest(CachedResource::Type, const KURL&, bool forPreload = false); 137 138 static const ResourceLoaderOptions& defaultCachedResourceOptions(); 139 140private: 141 explicit CachedResourceLoader(DocumentLoader*); 142 143 CachedResourceHandle<CachedResource> requestResource(CachedResource::Type, CachedResourceRequest&); 144 CachedResourceHandle<CachedResource> revalidateResource(const CachedResourceRequest&, CachedResource*); 145 CachedResourceHandle<CachedResource> loadResource(CachedResource::Type, CachedResourceRequest&, const String& charset); 146#if ENABLE(RESOURCE_TIMING) 147 void storeResourceTimingInitiatorInformation(const CachedResourceHandle<CachedResource>&, const CachedResourceRequest&); 148#endif 149 void requestPreload(CachedResource::Type, CachedResourceRequest&, const String& charset); 150 151 enum RevalidationPolicy { Use, Revalidate, Reload, Load }; 152 RevalidationPolicy determineRevalidationPolicy(CachedResource::Type, ResourceRequest&, bool forPreload, CachedResource* existingResource, CachedResourceRequest::DeferOption) const; 153 154 bool shouldContinueAfterNotifyingLoadedFromMemoryCache(const CachedResourceRequest&, CachedResource*); 155 bool checkInsecureContent(CachedResource::Type, const KURL&) const; 156 157 void garbageCollectDocumentResourcesTimerFired(Timer<CachedResourceLoader>*); 158 void performPostLoadActions(); 159 160 bool clientDefersImage(const KURL&) const; 161 void reloadImagesIfNotDeferred(); 162 163 HashSet<String> m_validatedURLs; 164 mutable DocumentResourceMap m_documentResources; 165 Document* m_document; 166 DocumentLoader* m_documentLoader; 167 168 int m_requestCount; 169 170 OwnPtr<ListHashSet<CachedResource*> > m_preloads; 171 struct PendingPreload { 172 CachedResource::Type m_type; 173 CachedResourceRequest m_request; 174 String m_charset; 175 }; 176 Deque<PendingPreload> m_pendingPreloads; 177 178 Timer<CachedResourceLoader> m_garbageCollectDocumentResourcesTimer; 179 180#if ENABLE(RESOURCE_TIMING) 181 struct InitiatorInfo { 182 AtomicString name; 183 double startTime; 184 }; 185 HashMap<CachedResource*, InitiatorInfo> m_initiatorMap; 186#endif 187 188 // 29 bits left 189 bool m_autoLoadImages : 1; 190 bool m_imagesEnabled : 1; 191 bool m_allowStaleResources : 1; 192}; 193 194class ResourceCacheValidationSuppressor { 195 WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor); 196 WTF_MAKE_FAST_ALLOCATED; 197public: 198 ResourceCacheValidationSuppressor(CachedResourceLoader* loader) 199 : m_loader(loader) 200 , m_previousState(false) 201 { 202 if (m_loader) { 203 m_previousState = m_loader->m_allowStaleResources; 204 m_loader->m_allowStaleResources = true; 205 } 206 } 207 ~ResourceCacheValidationSuppressor() 208 { 209 if (m_loader) 210 m_loader->m_allowStaleResources = m_previousState; 211 } 212private: 213 CachedResourceLoader* m_loader; 214 bool m_previousState; 215}; 216 217} // namespace WebCore 218 219#endif 220