0f6d60012fda18e61914de744e8a866ec8d23026
[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/Expected.h>
37 #include <wtf/HashMap.h>
38 #include <wtf/HashSet.h>
39 #include <wtf/ListHashSet.h>
40 #include <wtf/text/StringHash.h>
41
42 namespace WebCore {
43
44 #if ENABLE(APPLICATION_MANIFEST)
45 class CachedApplicationManifest;
46 #endif
47 class CachedCSSStyleSheet;
48 class CachedSVGDocument;
49 class CachedFont;
50 class CachedImage;
51 class CachedRawResource;
52 class CachedScript;
53 class CachedTextTrack;
54 class CachedXSLStyleSheet;
55 class Document;
56 class DocumentLoader;
57 class Frame;
58 class ImageLoader;
59 class Page;
60 class Settings;
61
62 template <typename T>
63 using ResourceErrorOr = Expected<T, ResourceError>;
64
65 // The CachedResourceLoader provides a per-context interface to the MemoryCache
66 // and enforces a bunch of security checks and rules for resource revalidation.
67 // Its lifetime is roughly per-DocumentLoader, in that it is generally created
68 // in the DocumentLoader constructor and loses its ability to generate network
69 // requests when the DocumentLoader is destroyed. Documents also hold a 
70 // RefPtr<CachedResourceLoader> for their lifetime (and will create one if they
71 // are initialized without a Frame), so a Document can keep a CachedResourceLoader
72 // alive past detach if scripts still reference the Document.
73 class CachedResourceLoader : public RefCounted<CachedResourceLoader> {
74     WTF_MAKE_NONCOPYABLE(CachedResourceLoader); WTF_MAKE_FAST_ALLOCATED;
75 friend class ImageLoader;
76 friend class ResourceCacheValidationSuppressor;
77
78 public:
79     static Ref<CachedResourceLoader> create(DocumentLoader* documentLoader) { return adoptRef(*new CachedResourceLoader(documentLoader)); }
80     ~CachedResourceLoader();
81
82     ResourceErrorOr<CachedResourceHandle<CachedImage>> requestImage(CachedResourceRequest&&);
83     ResourceErrorOr<CachedResourceHandle<CachedCSSStyleSheet>> requestCSSStyleSheet(CachedResourceRequest&&);
84     CachedResourceHandle<CachedCSSStyleSheet> requestUserCSSStyleSheet(Page&, CachedResourceRequest&&);
85     ResourceErrorOr<CachedResourceHandle<CachedScript>> requestScript(CachedResourceRequest&&);
86     ResourceErrorOr<CachedResourceHandle<CachedFont>> requestFont(CachedResourceRequest&&, bool isSVG);
87     ResourceErrorOr<CachedResourceHandle<CachedRawResource>> requestMedia(CachedResourceRequest&&);
88     ResourceErrorOr<CachedResourceHandle<CachedRawResource>> requestIcon(CachedResourceRequest&&);
89     ResourceErrorOr<CachedResourceHandle<CachedRawResource>> requestBeaconResource(CachedResourceRequest&&);
90     ResourceErrorOr<CachedResourceHandle<CachedRawResource>> requestMainResource(CachedResourceRequest&&);
91     ResourceErrorOr<CachedResourceHandle<CachedSVGDocument>> requestSVGDocument(CachedResourceRequest&&);
92 #if ENABLE(XSLT)
93     ResourceErrorOr<CachedResourceHandle<CachedXSLStyleSheet>> requestXSLStyleSheet(CachedResourceRequest&&);
94 #endif
95     ResourceErrorOr<CachedResourceHandle<CachedResource>> requestLinkResource(CachedResource::Type, CachedResourceRequest&&);
96 #if ENABLE(VIDEO_TRACK)
97     ResourceErrorOr<CachedResourceHandle<CachedTextTrack>> requestTextTrack(CachedResourceRequest&&);
98 #endif
99 #if ENABLE(APPLICATION_MANIFEST)
100     ResourceErrorOr<CachedResourceHandle<CachedApplicationManifest>> requestApplicationManifest(CachedResourceRequest&&);
101 #endif
102
103     // Called to load Web Worker main script, Service Worker main script, importScripts(), XHR,
104     // EventSource, Fetch, and App Cache.
105     ResourceErrorOr<CachedResourceHandle<CachedRawResource>> requestRawResource(CachedResourceRequest&&);
106
107     // Logs an access denied message to the console for the specified URL.
108     void printAccessDeniedMessage(const URL& url) const;
109
110     CachedResource* cachedResource(const String& url) const;
111     CachedResource* cachedResource(const URL& url) const;
112
113     typedef HashMap<String, CachedResourceHandle<CachedResource>> DocumentResourceMap;
114     const DocumentResourceMap& allCachedResources() const { return m_documentResources; }
115
116     bool autoLoadImages() const { return m_autoLoadImages; }
117     void setAutoLoadImages(bool);
118
119     bool imagesEnabled() const { return m_imagesEnabled; }
120     void setImagesEnabled(bool);
121
122     bool shouldDeferImageLoad(const URL&) const;
123     bool shouldPerformImageLoad(const URL&) const;
124     
125     CachePolicy cachePolicy(CachedResource::Type, const URL&) const;
126     
127     Frame* frame() const; // Can be null
128     Document* document() const { return m_document.get(); } // Can be null
129     void setDocument(Document* document) { m_document = makeWeakPtr(document); }
130     void clearDocumentLoader() { m_documentLoader = nullptr; }
131     PAL::SessionID sessionID() const;
132
133     void loadDone(LoadCompletionType, bool shouldPerformPostLoadActions = true);
134
135     WEBCORE_EXPORT void garbageCollectDocumentResources();
136
137     void incrementRequestCount(const CachedResource&);
138     void decrementRequestCount(const CachedResource&);
139     int requestCount() const { return m_requestCount; }
140
141     WEBCORE_EXPORT bool isPreloaded(const String& urlString) const;
142     enum class ClearPreloadsMode { ClearSpeculativePreloads, ClearAllPreloads };
143     void clearPreloads(ClearPreloadsMode);
144     ResourceErrorOr<CachedResourceHandle<CachedResource>> preload(CachedResource::Type, CachedResourceRequest&&);
145     void printPreloadStats();
146     void warnUnusedPreloads();
147     void stopUnusedPreloadsTimer();
148
149     bool updateRequestAfterRedirection(CachedResource::Type, ResourceRequest&, const ResourceLoaderOptions&);
150     bool allowedByContentSecurityPolicy(CachedResource::Type, const URL&, const ResourceLoaderOptions&, ContentSecurityPolicy::RedirectResponseReceived) const;
151
152     static const ResourceLoaderOptions& defaultCachedResourceOptions();
153
154     void documentDidFinishLoadEvent();
155
156     ResourceTimingInformation& resourceTimingInformation() { return m_resourceTimingInfo; }
157
158     bool isAlwaysOnLoggingAllowed() const;
159
160     KeepaliveRequestTracker& keepaliveRequestTracker() { return m_keepaliveRequestTracker; }
161
162 private:
163     explicit CachedResourceLoader(DocumentLoader*);
164
165     enum class ForPreload { Yes, No };
166     enum class DeferOption { NoDefer, DeferredByClient };
167
168     ResourceErrorOr<CachedResourceHandle<CachedResource>> requestResource(CachedResource::Type, CachedResourceRequest&&, ForPreload = ForPreload::No, DeferOption = DeferOption::NoDefer);
169     CachedResourceHandle<CachedResource> revalidateResource(CachedResourceRequest&&, CachedResource&);
170     CachedResourceHandle<CachedResource> loadResource(CachedResource::Type, CachedResourceRequest&&, const CookieJar*);
171
172     void prepareFetch(CachedResource::Type, CachedResourceRequest&);
173     void updateHTTPRequestHeaders(CachedResource::Type, CachedResourceRequest&);
174
175     bool canRequest(CachedResource::Type, const URL&, const CachedResourceRequest&, ForPreload);
176
177     enum RevalidationPolicy { Use, Revalidate, Reload, Load };
178     RevalidationPolicy determineRevalidationPolicy(CachedResource::Type, CachedResourceRequest&, CachedResource* existingResource, ForPreload, DeferOption) const;
179
180     bool shouldUpdateCachedResourceWithCurrentRequest(const CachedResource&, const CachedResourceRequest&);
181     CachedResourceHandle<CachedResource> updateCachedResourceWithCurrentRequest(const CachedResource&, CachedResourceRequest&&, const PAL::SessionID&, const CookieJar*);
182
183     bool shouldContinueAfterNotifyingLoadedFromMemoryCache(const CachedResourceRequest&, CachedResource&, ResourceError&);
184     bool checkInsecureContent(CachedResource::Type, const URL&) const;
185
186     void performPostLoadActions();
187
188     bool clientDefersImage(const URL&) const;
189     void reloadImagesIfNotDeferred();
190
191     bool canRequestAfterRedirection(CachedResource::Type, const URL&, const ResourceLoaderOptions&) const;
192     bool canRequestInContentDispositionAttachmentSandbox(CachedResource::Type, const URL&) const;
193
194     HashSet<String> m_validatedURLs;
195     mutable DocumentResourceMap m_documentResources;
196     WeakPtr<Document> m_document;
197     DocumentLoader* m_documentLoader;
198
199     int m_requestCount;
200
201     std::unique_ptr<ListHashSet<CachedResource*>> m_preloads;
202     Timer m_unusedPreloadsTimer;
203
204     Timer m_garbageCollectDocumentResourcesTimer;
205
206     ResourceTimingInformation m_resourceTimingInfo;
207     KeepaliveRequestTracker m_keepaliveRequestTracker;
208
209     // 29 bits left
210     bool m_autoLoadImages : 1;
211     bool m_imagesEnabled : 1;
212     bool m_allowStaleResources : 1;
213 };
214
215 class ResourceCacheValidationSuppressor {
216     WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor);
217     WTF_MAKE_FAST_ALLOCATED;
218 public:
219     ResourceCacheValidationSuppressor(CachedResourceLoader& loader)
220         : m_loader(loader)
221         , m_previousState(m_loader.m_allowStaleResources)
222     {
223         m_loader.m_allowStaleResources = true;
224     }
225     ~ResourceCacheValidationSuppressor()
226     {
227         m_loader.m_allowStaleResources = m_previousState;
228     }
229 private:
230     CachedResourceLoader& m_loader;
231     bool m_previousState;
232 };
233
234 } // namespace WebCore