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/
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.
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.
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.
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.
26 #ifndef ResourceFetcher_h
27 #define ResourceFetcher_h
29 #include "core/fetch/CachePolicy.h"
30 #include "core/fetch/FetchInitiatorInfo.h"
31 #include "core/fetch/FetchRequest.h"
32 #include "core/fetch/Resource.h"
33 #include "core/fetch/ResourceLoaderHost.h"
34 #include "core/fetch/ResourceLoaderOptions.h"
35 #include "core/fetch/ResourcePtr.h"
36 #include "platform/Timer.h"
37 #include "wtf/Deque.h"
38 #include "wtf/HashMap.h"
39 #include "wtf/HashSet.h"
40 #include "wtf/ListHashSet.h"
41 #include "wtf/text/StringHash.h"
45 class CSSStyleSheetResource;
46 class DocumentResource;
54 class XSLStyleSheetResource;
61 class ResourceTimingInfo;
62 class ResourceLoaderSet;
64 // The ResourceFetcher provides a per-context interface to the MemoryCache
65 // and enforces a bunch of security checks and rules for resource revalidation.
66 // Its lifetime is roughly per-DocumentLoader, in that it is generally created
67 // in the DocumentLoader constructor and loses its ability to generate network
68 // requests when the DocumentLoader is destroyed. Documents also hold a
69 // RefPtr<ResourceFetcher> for their lifetime (and will create one if they
70 // are initialized without a LocalFrame), so a Document can keep a ResourceFetcher
71 // alive past detach if scripts still reference the Document.
72 class ResourceFetcher FINAL : public RefCountedWillBeGarbageCollectedFinalized<ResourceFetcher>, public ResourceLoaderHost {
73 WTF_MAKE_NONCOPYABLE(ResourceFetcher); WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
74 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ResourceFetcher);
75 friend class ImageLoader;
76 friend class ResourceCacheValidationSuppressor;
79 static PassRefPtrWillBeRawPtr<ResourceFetcher> create(DocumentLoader* documentLoader) { return adoptRefWillBeNoop(new ResourceFetcher(documentLoader)); }
80 virtual ~ResourceFetcher();
81 virtual void trace(Visitor*);
84 using RefCounted<ResourceFetcher>::ref;
85 using RefCounted<ResourceFetcher>::deref;
88 ResourcePtr<Resource> fetchSynchronously(FetchRequest&);
89 ResourcePtr<ImageResource> fetchImage(FetchRequest&);
90 ResourcePtr<CSSStyleSheetResource> fetchCSSStyleSheet(FetchRequest&);
91 ResourcePtr<CSSStyleSheetResource> fetchUserCSSStyleSheet(FetchRequest&);
92 ResourcePtr<ScriptResource> fetchScript(FetchRequest&);
93 ResourcePtr<FontResource> fetchFont(FetchRequest&);
94 ResourcePtr<RawResource> fetchRawResource(FetchRequest&);
95 ResourcePtr<RawResource> fetchMainResource(FetchRequest&, const SubstituteData&);
96 ResourcePtr<DocumentResource> fetchSVGDocument(FetchRequest&);
97 ResourcePtr<XSLStyleSheetResource> fetchXSLStyleSheet(FetchRequest&);
98 ResourcePtr<Resource> fetchLinkResource(Resource::Type, FetchRequest&);
99 ResourcePtr<ShaderResource> fetchShader(FetchRequest&);
100 ResourcePtr<RawResource> fetchImport(FetchRequest&);
101 ResourcePtr<RawResource> fetchMedia(FetchRequest&);
102 ResourcePtr<RawResource> fetchTextTrack(FetchRequest&);
104 // Logs an access denied message to the console for the specified URL.
105 void printAccessDeniedMessage(const KURL&) const;
107 Resource* cachedResource(const KURL&) const;
109 typedef HashMap<String, ResourcePtr<Resource> > DocumentResourceMap;
110 const DocumentResourceMap& allResources() const { return m_documentResources; }
112 bool autoLoadImages() const { return m_autoLoadImages; }
113 void setAutoLoadImages(bool);
115 void setImagesEnabled(bool);
117 bool shouldDeferImageLoad(const KURL&) const;
119 LocalFrame* frame() const; // Can be null
120 FetchContext& context() const;
121 Document* document() const { return m_document; } // Can be null
122 void setDocument(RawPtr<Document> document) { m_document = document; }
124 DocumentLoader* documentLoader() const { return m_documentLoader; }
125 void clearDocumentLoader() { m_documentLoader = 0; }
127 void garbageCollectDocumentResources();
129 int requestCount() const { return m_requestCount; }
131 bool isPreloaded(const String& urlString) const;
132 void clearPreloads();
133 void preload(Resource::Type, FetchRequest&, const String& charset);
134 void printPreloadStats();
136 void setDefersLoading(bool);
138 bool isFetching() const;
140 // ResourceLoaderHost
141 virtual void incrementRequestCount(const Resource*) OVERRIDE;
142 virtual void decrementRequestCount(const Resource*) OVERRIDE;
143 virtual void didLoadResource(Resource*) OVERRIDE;
144 virtual void redirectReceived(Resource*, const ResourceResponse&) OVERRIDE;
145 virtual void didFinishLoading(const Resource*, double finishTime, int64_t encodedDataLength) OVERRIDE;
146 virtual void didChangeLoadingPriority(const Resource*, ResourceLoadPriority, int intraPriorityValue) OVERRIDE;
147 virtual void didFailLoading(const Resource*, const ResourceError&) OVERRIDE;
148 virtual void willSendRequest(unsigned long identifier, ResourceRequest&, const ResourceResponse& redirectResponse, const FetchInitiatorInfo&) OVERRIDE;
149 virtual void didReceiveResponse(const Resource*, const ResourceResponse&) OVERRIDE;
150 virtual void didReceiveData(const Resource*, const char* data, int dataLength, int encodedDataLength) OVERRIDE;
151 virtual void didDownloadData(const Resource*, int dataLength, int encodedDataLength) OVERRIDE;
152 virtual void subresourceLoaderFinishedLoadingOnePart(ResourceLoader*) OVERRIDE;
153 virtual void didInitializeResourceLoader(ResourceLoader*) OVERRIDE;
154 virtual void willTerminateResourceLoader(ResourceLoader*) OVERRIDE;
155 virtual void willStartLoadingResource(Resource*, ResourceRequest&) OVERRIDE;
156 virtual bool defersLoading() const OVERRIDE;
157 virtual bool isLoadedBy(ResourceLoaderHost*) const OVERRIDE;
158 virtual bool canAccessRedirect(Resource*, ResourceRequest&, const ResourceResponse&, ResourceLoaderOptions&) OVERRIDE;
159 virtual bool canAccessResource(Resource*, SecurityOrigin*, const KURL&) const OVERRIDE;
162 virtual void refResourceLoaderHost() OVERRIDE;
163 virtual void derefResourceLoaderHost() OVERRIDE;
166 enum ResourceLoadStartType {
167 ResourceLoadingFromNetwork,
168 ResourceLoadingFromCache
170 void requestLoadStarted(Resource*, const FetchRequest&, ResourceLoadStartType);
171 static const ResourceLoaderOptions& defaultResourceOptions();
174 explicit ResourceFetcher(DocumentLoader*);
176 bool shouldLoadNewResource(Resource::Type) const;
178 ResourcePtr<Resource> requestResource(Resource::Type, FetchRequest&);
179 ResourcePtr<Resource> revalidateResource(const FetchRequest&, Resource*);
180 ResourcePtr<Resource> loadResource(Resource::Type, FetchRequest&, const String& charset);
181 void preCacheDataURIImage(const FetchRequest&);
182 void preCacheSubstituteDataForMainResource(const FetchRequest&, const SubstituteData&);
183 void storeResourceTimingInitiatorInformation(Resource*);
184 void requestPreload(Resource::Type, FetchRequest&, const String& charset);
186 enum RevalidationPolicy { Use, Revalidate, Reload, Load };
187 RevalidationPolicy determineRevalidationPolicy(Resource::Type, ResourceRequest&, bool forPreload, Resource* existingResource, FetchRequest::DeferOption, const ResourceLoaderOptions&) const;
189 void determineTargetType(ResourceRequest&, Resource::Type);
190 ResourceRequestCachePolicy resourceRequestCachePolicy(const ResourceRequest&, Resource::Type);
191 void addAdditionalRequestHeaders(ResourceRequest&, Resource::Type);
193 bool canRequest(Resource::Type, const KURL&, const ResourceLoaderOptions&, bool forPreload, FetchRequest::OriginRestriction) const;
194 bool checkInsecureContent(Resource::Type, const KURL&, MixedContentBlockingTreatment) const;
196 static bool resourceNeedsLoad(Resource*, const FetchRequest&, RevalidationPolicy);
198 void notifyLoadedFromMemoryCache(Resource*);
200 void garbageCollectDocumentResourcesTimerFired(Timer<ResourceFetcher>*);
201 void scheduleDocumentResourcesGC();
203 void resourceTimingReportTimerFired(Timer<ResourceFetcher>*);
205 bool clientDefersImage(const KURL&) const;
206 void reloadImagesIfNotDeferred();
208 HashSet<String> m_validatedURLs;
209 mutable DocumentResourceMap m_documentResources;
210 RawPtrWillBeMember<Document> m_document;
211 DocumentLoader* m_documentLoader;
215 OwnPtr<ListHashSet<Resource*> > m_preloads;
217 Timer<ResourceFetcher> m_garbageCollectDocumentResourcesTimer;
218 Timer<ResourceFetcher> m_resourceTimingReportTimer;
220 typedef HashMap<Resource*, RefPtr<ResourceTimingInfo> > ResourceTimingInfoMap;
221 ResourceTimingInfoMap m_resourceTimingInfoMap;
223 HashMap<RefPtr<ResourceTimingInfo>, bool> m_scheduledResourceTimingReports;
225 OwnPtr<ResourceLoaderSet> m_loaders;
226 OwnPtr<ResourceLoaderSet> m_multipartLoaders;
228 // Used in hit rate histograms.
229 class DeadResourceStatsRecorder {
231 DeadResourceStatsRecorder();
232 ~DeadResourceStatsRecorder();
234 void update(RevalidationPolicy);
238 int m_revalidateCount;
241 DeadResourceStatsRecorder m_deadStatsRecorder;
244 bool m_autoLoadImages : 1;
245 bool m_imagesEnabled : 1;
246 bool m_allowStaleResources : 1;
249 class ResourceCacheValidationSuppressor {
250 WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor);
251 WTF_MAKE_FAST_ALLOCATED;
253 ResourceCacheValidationSuppressor(ResourceFetcher* loader)
255 , m_previousState(false)
258 m_previousState = m_loader->m_allowStaleResources;
259 m_loader->m_allowStaleResources = true;
262 ~ResourceCacheValidationSuppressor()
265 m_loader->m_allowStaleResources = m_previousState;
268 ResourceFetcher* m_loader;
269 bool m_previousState;
272 } // namespace WebCore