CachedResourceLoader should set headers of the HTTP request prior checking for the...
[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 #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 "ContentSecurityPolicy.h"
34 #include "ResourceLoadPriority.h"
35 #include "ResourceTimingInformation.h"
36 #include "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>
42
43 namespace WebCore {
44
45 class CachedCSSStyleSheet;
46 class CachedSVGDocument;
47 class CachedFont;
48 class CachedImage;
49 class CachedRawResource;
50 class CachedScript;
51 class CachedTextTrack;
52 class CachedXSLStyleSheet;
53 class Document;
54 class DocumentLoader;
55 class Frame;
56 class ImageLoader;
57 class URL;
58
59 // The CachedResourceLoader provides a per-context interface to the MemoryCache
60 // and enforces a bunch of security checks and rules for resource revalidation.
61 // Its lifetime is roughly per-DocumentLoader, in that it is generally created
62 // in the DocumentLoader constructor and loses its ability to generate network
63 // requests when the DocumentLoader is destroyed. Documents also hold a 
64 // RefPtr<CachedResourceLoader> for their lifetime (and will create one if they
65 // are initialized without a Frame), so a Document can keep a CachedResourceLoader
66 // alive past detach if scripts still reference the Document.
67 class CachedResourceLoader : public RefCounted<CachedResourceLoader> {
68     WTF_MAKE_NONCOPYABLE(CachedResourceLoader); WTF_MAKE_FAST_ALLOCATED;
69 friend class ImageLoader;
70 friend class ResourceCacheValidationSuppressor;
71
72 public:
73     static Ref<CachedResourceLoader> create(DocumentLoader* documentLoader) { return adoptRef(*new CachedResourceLoader(documentLoader)); }
74     ~CachedResourceLoader();
75
76     CachedResourceHandle<CachedImage> requestImage(CachedResourceRequest&&);
77     CachedResourceHandle<CachedCSSStyleSheet> requestCSSStyleSheet(CachedResourceRequest&&);
78     CachedResourceHandle<CachedCSSStyleSheet> requestUserCSSStyleSheet(CachedResourceRequest&&);
79     CachedResourceHandle<CachedScript> requestScript(CachedResourceRequest&&);
80     CachedResourceHandle<CachedFont> requestFont(CachedResourceRequest&&, bool isSVG);
81     CachedResourceHandle<CachedRawResource> requestMedia(CachedResourceRequest&&);
82     CachedResourceHandle<CachedRawResource> requestRawResource(CachedResourceRequest&&);
83     CachedResourceHandle<CachedRawResource> requestMainResource(CachedResourceRequest&&);
84     CachedResourceHandle<CachedSVGDocument> requestSVGDocument(CachedResourceRequest&&);
85 #if ENABLE(XSLT)
86     CachedResourceHandle<CachedXSLStyleSheet> requestXSLStyleSheet(CachedResourceRequest&&);
87 #endif
88 #if ENABLE(LINK_PREFETCH)
89     CachedResourceHandle<CachedResource> requestLinkResource(CachedResource::Type, CachedResourceRequest&&);
90 #endif
91 #if ENABLE(VIDEO_TRACK)
92     CachedResourceHandle<CachedTextTrack> requestTextTrack(CachedResourceRequest&&);
93 #endif
94
95     // Logs an access denied message to the console for the specified URL.
96     void printAccessDeniedMessage(const URL& url) const;
97
98     CachedResource* cachedResource(const String& url) const;
99     CachedResource* cachedResource(const URL& url) const;
100
101     typedef HashMap<String, CachedResourceHandle<CachedResource>> DocumentResourceMap;
102     const DocumentResourceMap& allCachedResources() const { return m_documentResources; }
103
104     bool autoLoadImages() const { return m_autoLoadImages; }
105     void setAutoLoadImages(bool);
106
107     bool imagesEnabled() const { return m_imagesEnabled; }
108     void setImagesEnabled(bool);
109
110     bool shouldDeferImageLoad(const URL&) const;
111     bool shouldPerformImageLoad(const URL&) const;
112     
113     CachePolicy cachePolicy(CachedResource::Type) const;
114     
115     Frame* frame() const; // Can be null
116     Document* document() const { return m_document; } // Can be null
117     void setDocument(Document* document) { m_document = document; }
118     void clearDocumentLoader() { m_documentLoader = nullptr; }
119     SessionID sessionID() const;
120
121     void removeCachedResource(CachedResource&);
122
123     void loadDone(CachedResource*, bool shouldPerformPostLoadActions = true);
124
125     WEBCORE_EXPORT void garbageCollectDocumentResources();
126
127     void incrementRequestCount(const CachedResource&);
128     void decrementRequestCount(const CachedResource&);
129     int requestCount() const { return m_requestCount; }
130
131     WEBCORE_EXPORT bool isPreloaded(const String& urlString) const;
132     void clearPreloads();
133     void clearPendingPreloads();
134     enum PreloadType { ImplicitPreload, ExplicitPreload };
135     CachedResourceHandle<CachedResource> preload(CachedResource::Type, CachedResourceRequest&&, PreloadType);
136     void checkForPendingPreloads();
137     void printPreloadStats();
138
139     bool updateRequestAfterRedirection(CachedResource::Type, ResourceRequest&, const ResourceLoaderOptions&);
140
141     static const ResourceLoaderOptions& defaultCachedResourceOptions();
142
143     void documentDidFinishLoadEvent();
144
145 #if ENABLE(WEB_TIMING)
146     ResourceTimingInformation& resourceTimingInformation() { return m_resourceTimingInfo; }
147 #endif
148
149     bool isAlwaysOnLoggingAllowed() const;
150
151 private:
152     explicit CachedResourceLoader(DocumentLoader*);
153
154     enum class ForPreload { Yes, No };
155     enum class DeferOption { NoDefer, DeferredByClient };
156
157     CachedResourceHandle<CachedResource> requestResource(CachedResource::Type, CachedResourceRequest&&, ForPreload = ForPreload::No, DeferOption = DeferOption::NoDefer);
158     CachedResourceHandle<CachedResource> revalidateResource(CachedResourceRequest&&, CachedResource&);
159     CachedResourceHandle<CachedResource> loadResource(CachedResource::Type, CachedResourceRequest&&);
160     CachedResourceHandle<CachedResource> requestPreload(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     struct PendingPreload {
195         CachedResource::Type m_type;
196         CachedResourceRequest m_request;
197     };
198     Deque<PendingPreload> m_pendingPreloads;
199
200     Timer m_garbageCollectDocumentResourcesTimer;
201
202 #if ENABLE(WEB_TIMING)
203     ResourceTimingInformation m_resourceTimingInfo;
204 #endif
205
206     // 29 bits left
207     bool m_autoLoadImages : 1;
208     bool m_imagesEnabled : 1;
209     bool m_allowStaleResources : 1;
210 };
211
212 class ResourceCacheValidationSuppressor {
213     WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor);
214     WTF_MAKE_FAST_ALLOCATED;
215 public:
216     ResourceCacheValidationSuppressor(CachedResourceLoader& loader)
217         : m_loader(loader)
218         , m_previousState(m_loader.m_allowStaleResources)
219     {
220         m_loader.m_allowStaleResources = true;
221     }
222     ~ResourceCacheValidationSuppressor()
223     {
224         m_loader.m_allowStaleResources = m_previousState;
225     }
226 private:
227     CachedResourceLoader& m_loader;
228     bool m_previousState;
229 };
230
231 } // namespace WebCore
232
233 #endif