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