38a9c0981b1a42edaed17bb899ff934a4ed52758
[WebKit-https.git] / Source / WebCore / loader / cache / CachedResourceLoader.h
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 #pragma once
27
28 #include "CachePolicy.h"
29 #include "CachedResource.h"
30 #include "CachedResourceHandle.h"
31 #include "CachedResourceRequest.h"
32 #include "ContentSecurityPolicy.h"
33 #include "KeepaliveRequestTracker.h"
34 #include "ResourceTimingInformation.h"
35 #include "Timer.h"
36 #include <wtf/HashMap.h>
37 #include <wtf/HashSet.h>
38 #include <wtf/ListHashSet.h>
39 #include <wtf/text/StringHash.h>
40
41 namespace WebCore {
42
43 class CachedCSSStyleSheet;
44 class CachedSVGDocument;
45 class CachedFont;
46 class CachedImage;
47 class CachedRawResource;
48 class CachedScript;
49 class CachedTextTrack;
50 class CachedXSLStyleSheet;
51 class Document;
52 class DocumentLoader;
53 class Frame;
54 class ImageLoader;
55 class Settings;
56 class URL;
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.
66 class CachedResourceLoader : public RefCounted<CachedResourceLoader> {
67     WTF_MAKE_NONCOPYABLE(CachedResourceLoader); WTF_MAKE_FAST_ALLOCATED;
68 friend class ImageLoader;
69 friend class ResourceCacheValidationSuppressor;
70
71 public:
72     static Ref<CachedResourceLoader> create(DocumentLoader* documentLoader) { return adoptRef(*new CachedResourceLoader(documentLoader)); }
73     ~CachedResourceLoader();
74
75     CachedResourceHandle<CachedImage> requestImage(CachedResourceRequest&&, ResourceError* = nullptr);
76     CachedResourceHandle<CachedCSSStyleSheet> requestCSSStyleSheet(CachedResourceRequest&&, ResourceError* = nullptr);
77     CachedResourceHandle<CachedCSSStyleSheet> requestUserCSSStyleSheet(CachedResourceRequest&&, ResourceError* = nullptr);
78     CachedResourceHandle<CachedScript> requestScript(CachedResourceRequest&&, ResourceError* = nullptr);
79     CachedResourceHandle<CachedFont> requestFont(CachedResourceRequest&&, bool isSVG, ResourceError* = nullptr);
80     CachedResourceHandle<CachedRawResource> requestMedia(CachedResourceRequest&&, ResourceError* = nullptr);
81     CachedResourceHandle<CachedRawResource> requestIcon(CachedResourceRequest&&, ResourceError* = nullptr);
82     CachedResourceHandle<CachedResource> requestBeaconResource(CachedResourceRequest&&, ResourceError* = nullptr);
83     CachedResourceHandle<CachedRawResource> requestRawResource(CachedResourceRequest&&, ResourceError* = nullptr);
84     CachedResourceHandle<CachedRawResource> requestMainResource(CachedResourceRequest&&, ResourceError* = nullptr);
85     CachedResourceHandle<CachedSVGDocument> requestSVGDocument(CachedResourceRequest&&, ResourceError* = nullptr);
86 #if ENABLE(XSLT)
87     CachedResourceHandle<CachedXSLStyleSheet> requestXSLStyleSheet(CachedResourceRequest&&, ResourceError* = nullptr);
88 #endif
89 #if ENABLE(LINK_PREFETCH)
90     CachedResourceHandle<CachedResource> requestLinkResource(CachedResource::Type, CachedResourceRequest&&, ResourceError* = nullptr);
91 #endif
92 #if ENABLE(VIDEO_TRACK)
93     CachedResourceHandle<CachedTextTrack> requestTextTrack(CachedResourceRequest&&, ResourceError* = nullptr);
94 #endif
95
96     // Logs an access denied message to the console for the specified URL.
97     void printAccessDeniedMessage(const URL& url) const;
98
99     CachedResource* cachedResource(const String& url) const;
100     CachedResource* cachedResource(const URL& url) const;
101
102     typedef HashMap<String, CachedResourceHandle<CachedResource>> DocumentResourceMap;
103     const DocumentResourceMap& allCachedResources() const { return m_documentResources; }
104
105     bool autoLoadImages() const { return m_autoLoadImages; }
106     void setAutoLoadImages(bool);
107
108     bool imagesEnabled() const { return m_imagesEnabled; }
109     void setImagesEnabled(bool);
110
111     bool shouldDeferImageLoad(const URL&) const;
112     bool shouldPerformImageLoad(const URL&) const;
113     
114     CachePolicy cachePolicy(CachedResource::Type, const URL&) const;
115     
116     Frame* frame() const; // Can be null
117     Document* document() const { return m_document; } // Can be null
118     void setDocument(Document* document) { m_document = document; }
119     void clearDocumentLoader() { m_documentLoader = nullptr; }
120     SessionID sessionID() const;
121
122     void removeCachedResource(CachedResource&);
123
124     void loadDone(bool shouldPerformPostLoadActions = true);
125
126     WEBCORE_EXPORT void garbageCollectDocumentResources();
127
128     void incrementRequestCount(const CachedResource&);
129     void decrementRequestCount(const CachedResource&);
130     int requestCount() const { return m_requestCount; }
131
132     WEBCORE_EXPORT bool isPreloaded(const String& urlString) const;
133     enum class ClearPreloadsMode { ClearSpeculativePreloads, ClearAllPreloads };
134     void clearPreloads(ClearPreloadsMode);
135     CachedResourceHandle<CachedResource> preload(CachedResource::Type, CachedResourceRequest&&);
136     void printPreloadStats();
137     void warnUnusedPreloads();
138     void stopUnusedPreloadsTimer();
139
140     bool updateRequestAfterRedirection(CachedResource::Type, ResourceRequest&, const ResourceLoaderOptions&);
141
142     static const ResourceLoaderOptions& defaultCachedResourceOptions();
143
144     void documentDidFinishLoadEvent();
145
146     ResourceTimingInformation& resourceTimingInformation() { return m_resourceTimingInfo; }
147
148     bool isAlwaysOnLoggingAllowed() const;
149
150     KeepaliveRequestTracker& keepaliveRequestTracker() { return m_keepaliveRequestTracker; }
151
152 private:
153     explicit CachedResourceLoader(DocumentLoader*);
154
155     enum class ForPreload { Yes, No };
156     enum class DeferOption { NoDefer, DeferredByClient };
157
158     CachedResourceHandle<CachedResource> requestResource(CachedResource::Type, CachedResourceRequest&&, ResourceError* = nullptr, ForPreload = ForPreload::No, DeferOption = DeferOption::NoDefer);
159     CachedResourceHandle<CachedResource> revalidateResource(CachedResourceRequest&&, CachedResource&);
160     CachedResourceHandle<CachedResource> loadResource(CachedResource::Type, CachedResourceRequest&&);
161
162     void prepareFetch(CachedResource::Type, CachedResourceRequest&);
163     void updateHTTPRequestHeaders(CachedResource::Type, CachedResourceRequest&);
164     void updateReferrerOriginAndUserAgentHeaders(CachedResourceRequest&);
165
166     bool canRequest(CachedResource::Type, const URL&, const CachedResourceRequest&, ForPreload);
167
168     enum RevalidationPolicy { Use, Revalidate, Reload, Load };
169     RevalidationPolicy determineRevalidationPolicy(CachedResource::Type, CachedResourceRequest&, CachedResource* existingResource, ForPreload, DeferOption) const;
170
171     bool shouldUpdateCachedResourceWithCurrentRequest(const CachedResource&, const CachedResourceRequest&);
172     CachedResourceHandle<CachedResource> updateCachedResourceWithCurrentRequest(const CachedResource&, CachedResourceRequest&&);
173
174     bool shouldContinueAfterNotifyingLoadedFromMemoryCache(const CachedResourceRequest&, CachedResource*);
175     bool checkInsecureContent(CachedResource::Type, const URL&) const;
176     bool allowedByContentSecurityPolicy(CachedResource::Type, const URL&, const ResourceLoaderOptions&, ContentSecurityPolicy::RedirectResponseReceived) const;
177
178     void performPostLoadActions();
179
180     bool clientDefersImage(const URL&) const;
181     void reloadImagesIfNotDeferred();
182
183     bool canRequestAfterRedirection(CachedResource::Type, const URL&, const ResourceLoaderOptions&) const;
184     bool canRequestInContentDispositionAttachmentSandbox(CachedResource::Type, const URL&) const;
185
186     HashSet<String> m_validatedURLs;
187     mutable DocumentResourceMap m_documentResources;
188     Document* m_document;
189     DocumentLoader* m_documentLoader;
190
191     int m_requestCount;
192
193     std::unique_ptr<ListHashSet<CachedResource*>> m_preloads;
194     Timer m_unusedPreloadsTimer;
195
196     Timer m_garbageCollectDocumentResourcesTimer;
197
198     ResourceTimingInformation m_resourceTimingInfo;
199     KeepaliveRequestTracker m_keepaliveRequestTracker;
200
201     // 29 bits left
202     bool m_autoLoadImages : 1;
203     bool m_imagesEnabled : 1;
204     bool m_allowStaleResources : 1;
205 };
206
207 class ResourceCacheValidationSuppressor {
208     WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor);
209     WTF_MAKE_FAST_ALLOCATED;
210 public:
211     ResourceCacheValidationSuppressor(CachedResourceLoader& loader)
212         : m_loader(loader)
213         , m_previousState(m_loader.m_allowStaleResources)
214     {
215         m_loader.m_allowStaleResources = true;
216     }
217     ~ResourceCacheValidationSuppressor()
218     {
219         m_loader.m_allowStaleResources = m_previousState;
220     }
221 private:
222     CachedResourceLoader& m_loader;
223     bool m_previousState;
224 };
225
226 } // namespace WebCore