f0b5f2fec8ff14a6250a7159789f43fa65827c71
[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     void updateHTTPRequestHeaders(CachedResourceRequest&);
158     CachedResourceHandle<CachedResource> requestResource(CachedResource::Type, CachedResourceRequest&&, ForPreload = ForPreload::No, DeferOption = DeferOption::NoDefer);
159     void prepareFetch(CachedResource::Type, CachedResourceRequest&);
160     CachedResourceHandle<CachedResource> revalidateResource(CachedResourceRequest&&, CachedResource&);
161     CachedResourceHandle<CachedResource> loadResource(CachedResource::Type, CachedResourceRequest&&);
162     CachedResourceHandle<CachedResource> requestPreload(CachedResource::Type, CachedResourceRequest&&);
163
164     bool canRequest(CachedResource::Type, const URL&, const CachedResourceRequest&, ForPreload);
165
166     enum RevalidationPolicy { Use, Revalidate, Reload, Load };
167     RevalidationPolicy determineRevalidationPolicy(CachedResource::Type, CachedResourceRequest&, CachedResource* existingResource, ForPreload, DeferOption) const;
168
169     bool shouldUpdateCachedResourceWithCurrentRequest(const CachedResource&, const CachedResourceRequest&);
170     CachedResourceHandle<CachedResource> updateCachedResourceWithCurrentRequest(const CachedResource&, CachedResourceRequest&&);
171
172     bool shouldContinueAfterNotifyingLoadedFromMemoryCache(const CachedResourceRequest&, CachedResource*);
173     bool checkInsecureContent(CachedResource::Type, const URL&) const;
174     bool allowedByContentSecurityPolicy(CachedResource::Type, const URL&, const ResourceLoaderOptions&, ContentSecurityPolicy::RedirectResponseReceived) const;
175
176     void performPostLoadActions();
177
178     bool clientDefersImage(const URL&) const;
179     void reloadImagesIfNotDeferred();
180
181     bool canRequestAfterRedirection(CachedResource::Type, const URL&, const ResourceLoaderOptions&) const;
182     bool canRequestInContentDispositionAttachmentSandbox(CachedResource::Type, const URL&) const;
183
184     HashSet<String> m_validatedURLs;
185     mutable DocumentResourceMap m_documentResources;
186     Document* m_document;
187     DocumentLoader* m_documentLoader;
188     
189     int m_requestCount;
190     
191     std::unique_ptr<ListHashSet<CachedResource*>> m_preloads;
192     struct PendingPreload {
193         CachedResource::Type m_type;
194         CachedResourceRequest m_request;
195     };
196     Deque<PendingPreload> m_pendingPreloads;
197
198     Timer m_garbageCollectDocumentResourcesTimer;
199
200 #if ENABLE(WEB_TIMING)
201     ResourceTimingInformation m_resourceTimingInfo;
202 #endif
203
204     // 29 bits left
205     bool m_autoLoadImages : 1;
206     bool m_imagesEnabled : 1;
207     bool m_allowStaleResources : 1;
208 };
209
210 class ResourceCacheValidationSuppressor {
211     WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor);
212     WTF_MAKE_FAST_ALLOCATED;
213 public:
214     ResourceCacheValidationSuppressor(CachedResourceLoader& loader)
215         : m_loader(loader)
216         , m_previousState(m_loader.m_allowStaleResources)
217     {
218         m_loader.m_allowStaleResources = true;
219     }
220     ~ResourceCacheValidationSuppressor()
221     {
222         m_loader.m_allowStaleResources = m_previousState;
223     }
224 private:
225     CachedResourceLoader& m_loader;
226     bool m_previousState;
227 };
228
229 } // namespace WebCore
230
231 #endif