| 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>> requestPingResource(CachedResourceRequest&&); |
| 91 | ResourceErrorOr<CachedResourceHandle<CachedRawResource>> requestMainResource(CachedResourceRequest&&); |
| 92 | ResourceErrorOr<CachedResourceHandle<CachedSVGDocument>> requestSVGDocument(CachedResourceRequest&&); |
| 93 | #if ENABLE(XSLT) |
| 94 | ResourceErrorOr<CachedResourceHandle<CachedXSLStyleSheet>> requestXSLStyleSheet(CachedResourceRequest&&); |
| 95 | #endif |
| 96 | ResourceErrorOr<CachedResourceHandle<CachedResource>> requestLinkResource(CachedResource::Type, CachedResourceRequest&&); |
| 97 | #if ENABLE(VIDEO_TRACK) |
| 98 | ResourceErrorOr<CachedResourceHandle<CachedTextTrack>> requestTextTrack(CachedResourceRequest&&); |
| 99 | #endif |
| 100 | #if ENABLE(APPLICATION_MANIFEST) |
| 101 | ResourceErrorOr<CachedResourceHandle<CachedApplicationManifest>> requestApplicationManifest(CachedResourceRequest&&); |
| 102 | #endif |
| 103 | |
| 104 | // Called to load Web Worker main script, Service Worker main script, importScripts(), XHR, |
| 105 | // EventSource, Fetch, and App Cache. |
| 106 | ResourceErrorOr<CachedResourceHandle<CachedRawResource>> requestRawResource(CachedResourceRequest&&); |
| 107 | |
| 108 | // Logs an access denied message to the console for the specified URL. |
| 109 | void printAccessDeniedMessage(const URL& url) const; |
| 110 | |
| 111 | CachedResource* cachedResource(const String& url) const; |
| 112 | CachedResource* cachedResource(const URL& url) const; |
| 113 | |
| 114 | typedef HashMap<String, CachedResourceHandle<CachedResource>> DocumentResourceMap; |
| 115 | const DocumentResourceMap& allCachedResources() const { return m_documentResources; } |
| 116 | |
| 117 | bool autoLoadImages() const { return m_autoLoadImages; } |
| 118 | void setAutoLoadImages(bool); |
| 119 | |
| 120 | bool imagesEnabled() const { return m_imagesEnabled; } |
| 121 | void setImagesEnabled(bool); |
| 122 | |
| 123 | bool shouldDeferImageLoad(const URL&) const; |
| 124 | bool shouldPerformImageLoad(const URL&) const; |
| 125 | |
| 126 | CachePolicy cachePolicy(CachedResource::Type, const URL&) const; |
| 127 | |
| 128 | Frame* frame() const; // Can be null |
| 129 | Document* document() const { return m_document.get(); } // Can be null |
| 130 | void setDocument(Document* document) { m_document = makeWeakPtr(document); } |
| 131 | void clearDocumentLoader() { m_documentLoader = nullptr; } |
| 132 | PAL::SessionID sessionID() const; |
| 133 | |
| 134 | void loadDone(LoadCompletionType, bool shouldPerformPostLoadActions = true); |
| 135 | |
| 136 | WEBCORE_EXPORT void garbageCollectDocumentResources(); |
| 137 | |
| 138 | void incrementRequestCount(const CachedResource&); |
| 139 | void decrementRequestCount(const CachedResource&); |
| 140 | int requestCount() const { return m_requestCount; } |
| 141 | |
| 142 | WEBCORE_EXPORT bool isPreloaded(const String& urlString) const; |
| 143 | enum class ClearPreloadsMode { ClearSpeculativePreloads, ClearAllPreloads }; |
| 144 | void clearPreloads(ClearPreloadsMode); |
| 145 | ResourceErrorOr<CachedResourceHandle<CachedResource>> preload(CachedResource::Type, CachedResourceRequest&&); |
| 146 | void printPreloadStats(); |
| 147 | void warnUnusedPreloads(); |
| 148 | void stopUnusedPreloadsTimer(); |
| 149 | |
| 150 | bool updateRequestAfterRedirection(CachedResource::Type, ResourceRequest&, const ResourceLoaderOptions&); |
| 151 | bool allowedByContentSecurityPolicy(CachedResource::Type, const URL&, const ResourceLoaderOptions&, ContentSecurityPolicy::RedirectResponseReceived) const; |
| 152 | |
| 153 | static const ResourceLoaderOptions& defaultCachedResourceOptions(); |
| 154 | |
| 155 | void documentDidFinishLoadEvent(); |
| 156 | |
| 157 | ResourceTimingInformation& resourceTimingInformation() { return m_resourceTimingInfo; } |
| 158 | |
| 159 | bool isAlwaysOnLoggingAllowed() const; |
| 160 | |
| 161 | KeepaliveRequestTracker& keepaliveRequestTracker() { return m_keepaliveRequestTracker; } |
| 162 | |
| 163 | private: |
| 164 | explicit CachedResourceLoader(DocumentLoader*); |
| 165 | |
| 166 | enum class ForPreload { Yes, No }; |
| 167 | enum class DeferOption { NoDefer, DeferredByClient }; |
| 168 | |
| 169 | ResourceErrorOr<CachedResourceHandle<CachedResource>> requestResource(CachedResource::Type, CachedResourceRequest&&, ForPreload = ForPreload::No, DeferOption = DeferOption::NoDefer); |
| 170 | CachedResourceHandle<CachedResource> revalidateResource(CachedResourceRequest&&, CachedResource&); |
| 171 | CachedResourceHandle<CachedResource> loadResource(CachedResource::Type, CachedResourceRequest&&, const CookieJar*); |
| 172 | |
| 173 | void prepareFetch(CachedResource::Type, CachedResourceRequest&); |
| 174 | void (CachedResource::Type, CachedResourceRequest&); |
| 175 | |
| 176 | bool canRequest(CachedResource::Type, const URL&, const CachedResourceRequest&, ForPreload); |
| 177 | |
| 178 | enum RevalidationPolicy { Use, Revalidate, Reload, Load }; |
| 179 | RevalidationPolicy determineRevalidationPolicy(CachedResource::Type, CachedResourceRequest&, CachedResource* existingResource, ForPreload, DeferOption) const; |
| 180 | |
| 181 | bool shouldUpdateCachedResourceWithCurrentRequest(const CachedResource&, const CachedResourceRequest&); |
| 182 | CachedResourceHandle<CachedResource> updateCachedResourceWithCurrentRequest(const CachedResource&, CachedResourceRequest&&, const PAL::SessionID&, const CookieJar*); |
| 183 | |
| 184 | bool shouldContinueAfterNotifyingLoadedFromMemoryCache(const CachedResourceRequest&, CachedResource&, ResourceError&); |
| 185 | bool checkInsecureContent(CachedResource::Type, const URL&) const; |
| 186 | |
| 187 | void performPostLoadActions(); |
| 188 | |
| 189 | bool clientDefersImage(const URL&) const; |
| 190 | void reloadImagesIfNotDeferred(); |
| 191 | |
| 192 | bool canRequestAfterRedirection(CachedResource::Type, const URL&, const ResourceLoaderOptions&) const; |
| 193 | bool canRequestInContentDispositionAttachmentSandbox(CachedResource::Type, const URL&) const; |
| 194 | |
| 195 | HashSet<String> m_validatedURLs; |
| 196 | mutable DocumentResourceMap m_documentResources; |
| 197 | WeakPtr<Document> m_document; |
| 198 | DocumentLoader* m_documentLoader; |
| 199 | |
| 200 | int m_requestCount; |
| 201 | |
| 202 | std::unique_ptr<ListHashSet<CachedResource*>> m_preloads; |
| 203 | Timer m_unusedPreloadsTimer; |
| 204 | |
| 205 | Timer m_garbageCollectDocumentResourcesTimer; |
| 206 | |
| 207 | ResourceTimingInformation m_resourceTimingInfo; |
| 208 | KeepaliveRequestTracker m_keepaliveRequestTracker; |
| 209 | |
| 210 | // 29 bits left |
| 211 | bool m_autoLoadImages : 1; |
| 212 | bool m_imagesEnabled : 1; |
| 213 | bool m_allowStaleResources : 1; |
| 214 | }; |
| 215 | |
| 216 | class ResourceCacheValidationSuppressor { |
| 217 | WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor); |
| 218 | WTF_MAKE_FAST_ALLOCATED; |
| 219 | public: |
| 220 | ResourceCacheValidationSuppressor(CachedResourceLoader& loader) |
| 221 | : m_loader(loader) |
| 222 | , m_previousState(m_loader.m_allowStaleResources) |
| 223 | { |
| 224 | m_loader.m_allowStaleResources = true; |
| 225 | } |
| 226 | ~ResourceCacheValidationSuppressor() |
| 227 | { |
| 228 | m_loader.m_allowStaleResources = m_previousState; |
| 229 | } |
| 230 | private: |
| 231 | CachedResourceLoader& m_loader; |
| 232 | bool m_previousState; |
| 233 | }; |
| 234 | |
| 235 | } // namespace WebCore |
| 236 | |