eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 1 | /* |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 2 | Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) |
| 3 | Copyright (C) 2001 Dirk Mueller (mueller@kde.org) |
| 4 | Copyright (C) 2002 Waldo Bastian (bastian@kde.org) |
bfulgham@apple.com | b30d7f8 | 2016-06-07 15:47:30 +0000 | [diff] [blame] | 5 | Copyright (C) 2004-2016 Apple Inc. All rights reserved. |
treat@webkit.org | 915ef89 | 2009-02-26 20:09:50 +0000 | [diff] [blame] | 6 | Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/ |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 7 | |
| 8 | This library is free software; you can redistribute it and/or |
| 9 | modify it under the terms of the GNU Library General Public |
| 10 | License as published by the Free Software Foundation; either |
| 11 | version 2 of the License, or (at your option) any later version. |
| 12 | |
| 13 | This library is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | Library General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU Library General Public License |
| 19 | along with this library; see the file COPYING.LIB. If not, write to |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | Boston, MA 02110-1301, USA. |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 22 | |
| 23 | This class provides all functionality needed for loading images, style sheets and html |
| 24 | pages from the web. It has a memory cache for these objects. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 28 | #include "CachedResourceLoader.h" |
darin | 644b75e | 2006-02-21 06:59:15 +0000 | [diff] [blame] | 29 | |
darin | 89ec05a | 2006-03-07 09:03:23 +0000 | [diff] [blame] | 30 | #include "CachedCSSStyleSheet.h" |
reni@webkit.org | f76a86e | 2012-02-24 14:31:59 +0000 | [diff] [blame] | 31 | #include "CachedSVGDocument.h" |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 32 | #include "CachedFont.h" |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 33 | #include "CachedImage.h" |
japhet@chromium.org | b0444b3 | 2011-10-07 18:16:03 +0000 | [diff] [blame] | 34 | #include "CachedRawResource.h" |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 35 | #include "CachedResourceRequest.h" |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 36 | #include "CachedSVGFont.h" |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 37 | #include "CachedScript.h" |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 38 | #include "CachedXSLStyleSheet.h" |
dbates@webkit.org | eda26cf | 2014-10-22 23:43:11 +0000 | [diff] [blame] | 39 | #include "Chrome.h" |
| 40 | #include "ChromeClient.h" |
commit-queue@webkit.org | 8090ccb | 2015-10-06 08:18:08 +0000 | [diff] [blame] | 41 | #include "ContentExtensionError.h" |
achristensen@apple.com | 364f0cf | 2015-03-04 01:57:19 +0000 | [diff] [blame] | 42 | #include "ContentExtensionRule.h" |
abarth@webkit.org | 4c18a30 | 2011-03-28 05:15:38 +0000 | [diff] [blame] | 43 | #include "ContentSecurityPolicy.h" |
abarth@webkit.org | 234c23a | 2008-06-21 20:33:53 +0000 | [diff] [blame] | 44 | #include "DOMWindow.h" |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 45 | #include "DiagnosticLoggingClient.h" |
| 46 | #include "DiagnosticLoggingKeys.h" |
abarth@webkit.org | 94d2d9f | 2010-11-08 06:46:03 +0000 | [diff] [blame] | 47 | #include "Document.h" |
japhet@chromium.org | 97fe905 | 2012-10-09 21:58:25 +0000 | [diff] [blame] | 48 | #include "DocumentLoader.h" |
mjs | 810e976 | 2006-01-09 21:39:14 +0000 | [diff] [blame] | 49 | #include "Frame.h" |
darin | c370e7e | 2006-11-08 05:52:27 +0000 | [diff] [blame] | 50 | #include "FrameLoader.h" |
darin@chromium.org | 6d31800 | 2010-01-29 08:43:28 +0000 | [diff] [blame] | 51 | #include "FrameLoaderClient.h" |
abarth@webkit.org | 94d2d9f | 2010-11-08 06:46:03 +0000 | [diff] [blame] | 52 | #include "HTMLElement.h" |
simonjam@chromium.org | c5ab4b5 | 2013-04-02 05:51:43 +0000 | [diff] [blame] | 53 | #include "HTMLFrameOwnerElement.h" |
commit-queue@webkit.org | 66c7811 | 2012-10-26 23:55:46 +0000 | [diff] [blame] | 54 | #include "LoaderStrategy.h" |
commit-queue@webkit.org | 8090ccb | 2015-10-06 08:18:08 +0000 | [diff] [blame] | 55 | #include "LocalizedStrings.h" |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 56 | #include "Logging.h" |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 57 | #include "MainFrame.h" |
abarth@webkit.org | 94d2d9f | 2010-11-08 06:46:03 +0000 | [diff] [blame] | 58 | #include "MemoryCache.h" |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 59 | #include "Page.h" |
yoav@yoav.ws | 4ea6dc8 | 2016-05-02 08:21:50 +0000 | [diff] [blame] | 60 | #include "Performance.h" |
japhet@chromium.org | bc21302 | 2010-08-24 17:26:13 +0000 | [diff] [blame] | 61 | #include "PingLoader.h" |
commit-queue@webkit.org | 66c7811 | 2012-10-26 23:55:46 +0000 | [diff] [blame] | 62 | #include "PlatformStrategies.h" |
antti@apple.com | c3cbb6b | 2013-09-20 08:59:25 +0000 | [diff] [blame] | 63 | #include "RenderElement.h" |
commit-queue@webkit.org | 5c53393 | 2015-03-12 01:03:43 +0000 | [diff] [blame] | 64 | #include "ResourceLoadInfo.h" |
yoav@yoav.ws | 4ea6dc8 | 2016-05-02 08:21:50 +0000 | [diff] [blame] | 65 | #include "RuntimeEnabledFeatures.h" |
weinig@apple.com | 123e4aa | 2013-04-14 00:54:27 +0000 | [diff] [blame] | 66 | #include "ScriptController.h" |
abarth@webkit.org | c17c959 | 2008-06-21 20:33:42 +0000 | [diff] [blame] | 67 | #include "SecurityOrigin.h" |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 68 | #include "SessionID.h" |
abarth@webkit.org | 234c23a | 2008-06-21 20:33:53 +0000 | [diff] [blame] | 69 | #include "Settings.h" |
beidson@apple.com | b98919a | 2015-03-23 22:34:53 +0000 | [diff] [blame] | 70 | #include "StyleSheetContents.h" |
weinig@apple.com | 91defc8 | 2015-01-18 21:54:12 +0000 | [diff] [blame] | 71 | #include "UserContentController.h" |
beidson@apple.com | b98919a | 2015-03-23 22:34:53 +0000 | [diff] [blame] | 72 | #include "UserStyleSheet.h" |
ossy@webkit.org | b98796d | 2011-01-11 16:49:38 +0000 | [diff] [blame] | 73 | #include <wtf/text/CString.h> |
zimmermann@webkit.org | b5e11e6 | 2011-05-16 07:56:06 +0000 | [diff] [blame] | 74 | #include <wtf/text/WTFString.h> |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 75 | |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 76 | #if ENABLE(VIDEO_TRACK) |
eric.carlson@apple.com | 4dd460d | 2011-10-20 18:05:14 +0000 | [diff] [blame] | 77 | #include "CachedTextTrack.h" |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 80 | #define PRELOAD_DEBUG 0 |
| 81 | |
darin | 644b75e | 2006-02-21 06:59:15 +0000 | [diff] [blame] | 82 | namespace WebCore { |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 83 | |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 84 | static CachedResource* createResource(CachedResource::Type type, ResourceRequest& request, const String& charset, SessionID sessionID) |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 85 | { |
| 86 | switch (type) { |
| 87 | case CachedResource::ImageResource: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 88 | return new CachedImage(request, sessionID); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 89 | case CachedResource::CSSStyleSheet: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 90 | return new CachedCSSStyleSheet(request, charset, sessionID); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 91 | case CachedResource::Script: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 92 | return new CachedScript(request, charset, sessionID); |
reni@webkit.org | f76a86e | 2012-02-24 14:31:59 +0000 | [diff] [blame] | 93 | case CachedResource::SVGDocumentResource: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 94 | return new CachedSVGDocument(request, sessionID); |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 95 | #if ENABLE(SVG_FONTS) |
| 96 | case CachedResource::SVGFontResource: |
| 97 | return new CachedSVGFont(request, sessionID); |
| 98 | #endif |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 99 | case CachedResource::FontResource: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 100 | return new CachedFont(request, sessionID); |
dbates@webkit.org | 55b489b | 2016-03-16 19:46:49 +0000 | [diff] [blame] | 101 | case CachedResource::MediaResource: |
japhet@chromium.org | b0444b3 | 2011-10-07 18:16:03 +0000 | [diff] [blame] | 102 | case CachedResource::RawResource: |
japhet@chromium.org | a269e56 | 2012-10-25 20:05:17 +0000 | [diff] [blame] | 103 | case CachedResource::MainResource: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 104 | return new CachedRawResource(request, type, sessionID); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 105 | #if ENABLE(XSLT) |
| 106 | case CachedResource::XSLStyleSheet: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 107 | return new CachedXSLStyleSheet(request, sessionID); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 108 | #endif |
yoav@yoav.ws | c7bd832 | 2016-04-18 06:06:42 +0000 | [diff] [blame] | 109 | case CachedResource::LinkPreload: |
| 110 | return new CachedResource(request, CachedResource::LinkPreload, sessionID); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 111 | #if ENABLE(LINK_PREFETCH) |
commit-queue@webkit.org | 768c402 | 2011-05-21 19:24:56 +0000 | [diff] [blame] | 112 | case CachedResource::LinkPrefetch: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 113 | return new CachedResource(request, CachedResource::LinkPrefetch, sessionID); |
commit-queue@webkit.org | 768c402 | 2011-05-21 19:24:56 +0000 | [diff] [blame] | 114 | case CachedResource::LinkSubresource: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 115 | return new CachedResource(request, CachedResource::LinkSubresource, sessionID); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 116 | #endif |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 117 | #if ENABLE(VIDEO_TRACK) |
japhet@chromium.org | 5385c71 | 2011-10-31 23:49:47 +0000 | [diff] [blame] | 118 | case CachedResource::TextTrackResource: |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 119 | return new CachedTextTrack(request, sessionID); |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 120 | #endif |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 121 | } |
| 122 | ASSERT_NOT_REACHED(); |
hs85.jeong@samsung.com | 1317914 | 2015-10-21 02:12:32 +0000 | [diff] [blame] | 123 | return nullptr; |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 124 | } |
| 125 | |
japhet@chromium.org | 97fe905 | 2012-10-09 21:58:25 +0000 | [diff] [blame] | 126 | CachedResourceLoader::CachedResourceLoader(DocumentLoader* documentLoader) |
hs85.jeong@samsung.com | 1317914 | 2015-10-21 02:12:32 +0000 | [diff] [blame] | 127 | : m_document(nullptr) |
japhet@chromium.org | 97fe905 | 2012-10-09 21:58:25 +0000 | [diff] [blame] | 128 | , m_documentLoader(documentLoader) |
beidson | 22febd1 | 2007-05-04 04:35:28 +0000 | [diff] [blame] | 129 | , m_requestCount(0) |
cdumez@apple.com | 7f86ada | 2015-08-01 03:41:07 +0000 | [diff] [blame] | 130 | , m_garbageCollectDocumentResourcesTimer(*this, &CachedResourceLoader::garbageCollectDocumentResources) |
oliver | 1c8a487 | 2007-03-29 04:59:45 +0000 | [diff] [blame] | 131 | , m_autoLoadImages(true) |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 132 | , m_imagesEnabled(true) |
oliver | 06d89a0 | 2007-03-29 06:15:27 +0000 | [diff] [blame] | 133 | , m_allowStaleResources(false) |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 134 | { |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 135 | } |
| 136 | |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 137 | CachedResourceLoader::~CachedResourceLoader() |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 138 | { |
hs85.jeong@samsung.com | 1317914 | 2015-10-21 02:12:32 +0000 | [diff] [blame] | 139 | m_documentLoader = nullptr; |
| 140 | m_document = nullptr; |
andreas.kling@nokia.com | c929103 | 2011-02-17 13:38:06 +0000 | [diff] [blame] | 141 | |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 142 | clearPreloads(); |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 143 | for (auto& resource : m_documentResources.values()) |
| 144 | resource->setOwningCachedResourceLoader(nullptr); |
eric@webkit.org | bbb86d3 | 2009-03-04 22:58:23 +0000 | [diff] [blame] | 145 | |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 146 | // Make sure no requests still point to this CachedResourceLoader |
eric@webkit.org | bbb86d3 | 2009-03-04 22:58:23 +0000 | [diff] [blame] | 147 | ASSERT(m_requestCount == 0); |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 148 | } |
| 149 | |
antti@apple.com | 76bf3f2 | 2010-12-15 11:27:52 +0000 | [diff] [blame] | 150 | CachedResource* CachedResourceLoader::cachedResource(const String& resourceURL) const |
| 151 | { |
commit-queue@webkit.org | 8bcf60b | 2015-11-18 23:17:21 +0000 | [diff] [blame] | 152 | ASSERT(!resourceURL.isNull()); |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 153 | URL url = m_document->completeURL(resourceURL); |
antti@apple.com | 76bf3f2 | 2010-12-15 11:27:52 +0000 | [diff] [blame] | 154 | return cachedResource(url); |
| 155 | } |
| 156 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 157 | CachedResource* CachedResourceLoader::cachedResource(const URL& resourceURL) const |
antti@apple.com | 76bf3f2 | 2010-12-15 11:27:52 +0000 | [diff] [blame] | 158 | { |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 159 | URL url = MemoryCache::removeFragmentIdentifierIfNeeded(resourceURL); |
antti@apple.com | 76bf3f2 | 2010-12-15 11:27:52 +0000 | [diff] [blame] | 160 | return m_documentResources.get(url).get(); |
| 161 | } |
| 162 | |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 163 | Frame* CachedResourceLoader::frame() const |
eric@webkit.org | e0a5162 | 2008-06-26 22:45:47 +0000 | [diff] [blame] | 164 | { |
commit-queue@webkit.org | 3dd4580 | 2016-03-22 19:12:32 +0000 | [diff] [blame] | 165 | return m_documentLoader ? m_documentLoader->frame() : nullptr; |
eric@webkit.org | e0a5162 | 2008-06-26 22:45:47 +0000 | [diff] [blame] | 166 | } |
| 167 | |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 168 | SessionID CachedResourceLoader::sessionID() const |
| 169 | { |
| 170 | SessionID sessionID = SessionID::defaultSessionID(); |
| 171 | |
| 172 | if (Frame* f = frame()) |
| 173 | sessionID = f->page()->sessionID(); |
| 174 | |
| 175 | return sessionID; |
| 176 | } |
| 177 | |
commit-queue@webkit.org | 4b342506 | 2015-04-02 00:22:54 +0000 | [diff] [blame] | 178 | CachedResourceHandle<CachedImage> CachedResourceLoader::requestImage(CachedResourceRequest& request) |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 179 | { |
andersca@apple.com | a223587 | 2014-02-08 20:02:40 +0000 | [diff] [blame] | 180 | if (Frame* frame = this->frame()) { |
cdumez@apple.com | 83847a9 | 2015-06-26 18:26:19 +0000 | [diff] [blame] | 181 | if (frame->loader().pageDismissalEventBeingDispatched() != FrameLoader::PageDismissalType::None) { |
bfulgham@apple.com | b30d7f8 | 2016-06-07 15:47:30 +0000 | [diff] [blame] | 182 | if (Document* document = frame->document()) |
| 183 | document->contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request.mutableResourceRequest(), ContentSecurityPolicy::InsecureRequestType::Load); |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 184 | URL requestURL = request.resourceRequest().url(); |
hmuller@adobe.com | 1233f92 | 2013-06-22 19:56:37 +0000 | [diff] [blame] | 185 | if (requestURL.isValid() && canRequest(CachedResource::ImageResource, requestURL, request.options(), request.forPreload())) |
andersca@apple.com | a223587 | 2014-02-08 20:02:40 +0000 | [diff] [blame] | 186 | PingLoader::loadImage(*frame, requestURL); |
| 187 | return nullptr; |
japhet@chromium.org | bc21302 | 2010-08-24 17:26:13 +0000 | [diff] [blame] | 188 | } |
darin@chromium.org | 6d31800 | 2010-01-29 08:43:28 +0000 | [diff] [blame] | 189 | } |
andersca@apple.com | a223587 | 2014-02-08 20:02:40 +0000 | [diff] [blame] | 190 | |
commit-queue@webkit.org | 864dbda | 2012-11-16 12:21:57 +0000 | [diff] [blame] | 191 | request.setDefer(clientDefersImage(request.resourceRequest().url()) ? CachedResourceRequest::DeferredByClient : CachedResourceRequest::NoDefer); |
commit-queue@webkit.org | 4b342506 | 2015-04-02 00:22:54 +0000 | [diff] [blame] | 192 | return downcast<CachedImage>(requestResource(CachedResource::ImageResource, request).get()); |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 193 | } |
| 194 | |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 195 | CachedResourceHandle<CachedFont> CachedResourceLoader::requestFont(CachedResourceRequest& request, bool isSVG) |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 196 | { |
mmaxfield@apple.com | c314aef | 2014-11-20 21:45:54 +0000 | [diff] [blame] | 197 | #if ENABLE(SVG_FONTS) |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 198 | if (isSVG) |
| 199 | return downcast<CachedSVGFont>(requestResource(CachedResource::SVGFontResource, request).get()); |
mmaxfield@apple.com | c314aef | 2014-11-20 21:45:54 +0000 | [diff] [blame] | 200 | #else |
| 201 | UNUSED_PARAM(isSVG); |
| 202 | #endif |
cdumez@apple.com | c4a3af6 | 2014-10-06 00:31:05 +0000 | [diff] [blame] | 203 | return downcast<CachedFont>(requestResource(CachedResource::FontResource, request).get()); |
oliver | d6ae496 | 2007-10-12 14:55:24 +0000 | [diff] [blame] | 204 | } |
| 205 | |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 206 | #if ENABLE(VIDEO_TRACK) |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 207 | CachedResourceHandle<CachedTextTrack> CachedResourceLoader::requestTextTrack(CachedResourceRequest& request) |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 208 | { |
cdumez@apple.com | c4a3af6 | 2014-10-06 00:31:05 +0000 | [diff] [blame] | 209 | return downcast<CachedTextTrack>(requestResource(CachedResource::TextTrackResource, request).get()); |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 210 | } |
| 211 | #endif |
| 212 | |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 213 | CachedResourceHandle<CachedCSSStyleSheet> CachedResourceLoader::requestCSSStyleSheet(CachedResourceRequest& request) |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 214 | { |
cdumez@apple.com | c4a3af6 | 2014-10-06 00:31:05 +0000 | [diff] [blame] | 215 | return downcast<CachedCSSStyleSheet>(requestResource(CachedResource::CSSStyleSheet, request).get()); |
bdash | 281524a | 2007-02-09 08:50:57 +0000 | [diff] [blame] | 216 | } |
| 217 | |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 218 | CachedResourceHandle<CachedCSSStyleSheet> CachedResourceLoader::requestUserCSSStyleSheet(CachedResourceRequest& request) |
bdash | 281524a | 2007-02-09 08:50:57 +0000 | [diff] [blame] | 219 | { |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 220 | URL url = MemoryCache::removeFragmentIdentifierIfNeeded(request.resourceRequest().url()); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 221 | |
jpfau@apple.com | 97c6a7f | 2013-02-26 00:49:17 +0000 | [diff] [blame] | 222 | #if ENABLE(CACHE_PARTITIONING) |
cdumez@apple.com | 8730b6e | 2014-10-21 02:10:47 +0000 | [diff] [blame] | 223 | request.mutableResourceRequest().setDomainForCachePartition(document()->topOrigin()->domainForCachePartition()); |
jpfau@apple.com | 97c6a7f | 2013-02-26 00:49:17 +0000 | [diff] [blame] | 224 | #endif |
| 225 | |
cdumez@apple.com | 41d2d70 | 2015-02-02 20:50:16 +0000 | [diff] [blame] | 226 | auto& memoryCache = MemoryCache::singleton(); |
jer.noble@apple.com | 2ed319d | 2016-01-28 19:52:57 +0000 | [diff] [blame] | 227 | if (request.allowsCaching()) { |
| 228 | if (CachedResource* existing = memoryCache.resourceForRequest(request.resourceRequest(), sessionID())) { |
| 229 | if (is<CachedCSSStyleSheet>(*existing)) |
| 230 | return downcast<CachedCSSStyleSheet>(existing); |
| 231 | memoryCache.remove(*existing); |
| 232 | } |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 233 | } |
jer.noble@apple.com | 2ed319d | 2016-01-28 19:52:57 +0000 | [diff] [blame] | 234 | |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 235 | if (url.string() != request.resourceRequest().url()) |
| 236 | request.mutableResourceRequest().setURL(url); |
japhet@chromium.org | 7e4338d | 2011-05-25 00:24:38 +0000 | [diff] [blame] | 237 | |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 238 | CachedResourceHandle<CachedCSSStyleSheet> userSheet = new CachedCSSStyleSheet(request.resourceRequest(), request.charset(), sessionID()); |
ap@apple.com | 6edc42b | 2012-05-11 02:31:26 +0000 | [diff] [blame] | 239 | |
jer.noble@apple.com | 2ed319d | 2016-01-28 19:52:57 +0000 | [diff] [blame] | 240 | if (request.allowsCaching()) |
| 241 | memoryCache.add(*userSheet); |
ap@apple.com | 6edc42b | 2012-05-11 02:31:26 +0000 | [diff] [blame] | 242 | // FIXME: loadResource calls setOwningCachedResourceLoader() if the resource couldn't be added to cache. Does this function need to call it, too? |
japhet@chromium.org | 4a496a3 | 2011-08-29 19:26:46 +0000 | [diff] [blame] | 243 | |
commit-queue@webkit.org | 16c43b4 | 2016-07-04 18:36:30 +0000 | [diff] [blame^] | 244 | userSheet->load(*this, ResourceLoaderOptions(DoNotSendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, SkipSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::SkipPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching)); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 245 | |
| 246 | return userSheet; |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 247 | } |
| 248 | |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 249 | CachedResourceHandle<CachedScript> CachedResourceLoader::requestScript(CachedResourceRequest& request) |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 250 | { |
cdumez@apple.com | c4a3af6 | 2014-10-06 00:31:05 +0000 | [diff] [blame] | 251 | return downcast<CachedScript>(requestResource(CachedResource::Script, request).get()); |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 252 | } |
| 253 | |
mjs | d2948ef | 2007-02-26 19:29:04 +0000 | [diff] [blame] | 254 | #if ENABLE(XSLT) |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 255 | CachedResourceHandle<CachedXSLStyleSheet> CachedResourceLoader::requestXSLStyleSheet(CachedResourceRequest& request) |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 256 | { |
cdumez@apple.com | c4a3af6 | 2014-10-06 00:31:05 +0000 | [diff] [blame] | 257 | return downcast<CachedXSLStyleSheet>(requestResource(CachedResource::XSLStyleSheet, request).get()); |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 258 | } |
| 259 | #endif |
| 260 | |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 261 | CachedResourceHandle<CachedSVGDocument> CachedResourceLoader::requestSVGDocument(CachedResourceRequest& request) |
reni@webkit.org | ff4b9f2 | 2012-03-14 08:52:35 +0000 | [diff] [blame] | 262 | { |
cdumez@apple.com | c4a3af6 | 2014-10-06 00:31:05 +0000 | [diff] [blame] | 263 | return downcast<CachedSVGDocument>(requestResource(CachedResource::SVGDocumentResource, request).get()); |
reni@webkit.org | ff4b9f2 | 2012-03-14 08:52:35 +0000 | [diff] [blame] | 264 | } |
reni@webkit.org | ff4b9f2 | 2012-03-14 08:52:35 +0000 | [diff] [blame] | 265 | |
commit-queue@webkit.org | a67f42c | 2010-07-10 02:41:10 +0000 | [diff] [blame] | 266 | #if ENABLE(LINK_PREFETCH) |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 267 | CachedResourceHandle<CachedResource> CachedResourceLoader::requestLinkResource(CachedResource::Type type, CachedResourceRequest& request) |
commit-queue@webkit.org | a67f42c | 2010-07-10 02:41:10 +0000 | [diff] [blame] | 268 | { |
abarth@webkit.org | 926400e | 2010-08-31 07:35:01 +0000 | [diff] [blame] | 269 | ASSERT(frame()); |
gavinp@chromium.org | af445b9 | 2012-05-15 04:42:52 +0000 | [diff] [blame] | 270 | ASSERT(type == CachedResource::LinkPrefetch || type == CachedResource::LinkSubresource); |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 271 | return requestResource(type, request); |
commit-queue@webkit.org | a67f42c | 2010-07-10 02:41:10 +0000 | [diff] [blame] | 272 | } |
| 273 | #endif |
| 274 | |
dbates@webkit.org | 55b489b | 2016-03-16 19:46:49 +0000 | [diff] [blame] | 275 | CachedResourceHandle<CachedRawResource> CachedResourceLoader::requestMedia(CachedResourceRequest& request) |
| 276 | { |
| 277 | return downcast<CachedRawResource>(requestResource(CachedResource::MediaResource, request).get()); |
| 278 | } |
| 279 | |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 280 | CachedResourceHandle<CachedRawResource> CachedResourceLoader::requestRawResource(CachedResourceRequest& request) |
japhet@chromium.org | b0444b3 | 2011-10-07 18:16:03 +0000 | [diff] [blame] | 281 | { |
cdumez@apple.com | 3a3edad | 2014-10-06 05:13:09 +0000 | [diff] [blame] | 282 | return downcast<CachedRawResource>(requestResource(CachedResource::RawResource, request).get()); |
japhet@chromium.org | b0444b3 | 2011-10-07 18:16:03 +0000 | [diff] [blame] | 283 | } |
| 284 | |
japhet@chromium.org | a269e56 | 2012-10-25 20:05:17 +0000 | [diff] [blame] | 285 | CachedResourceHandle<CachedRawResource> CachedResourceLoader::requestMainResource(CachedResourceRequest& request) |
| 286 | { |
cdumez@apple.com | 3a3edad | 2014-10-06 05:13:09 +0000 | [diff] [blame] | 287 | return downcast<CachedRawResource>(requestResource(CachedResource::MainResource, request).get()); |
japhet@chromium.org | a269e56 | 2012-10-25 20:05:17 +0000 | [diff] [blame] | 288 | } |
| 289 | |
oliver@apple.com | 74dbf62 | 2015-03-06 04:43:12 +0000 | [diff] [blame] | 290 | static MixedContentChecker::ContentType contentTypeFromResourceType(CachedResource::Type type) |
| 291 | { |
| 292 | switch (type) { |
jer.noble@apple.com | 0ca560f | 2016-03-18 19:04:58 +0000 | [diff] [blame] | 293 | // https://w3c.github.io/webappsec-mixed-content/#category-optionally-blockable |
| 294 | // Editor's Draft, 11 February 2016 |
| 295 | // 3.1. Optionally-blockable Content |
oliver@apple.com | 74dbf62 | 2015-03-06 04:43:12 +0000 | [diff] [blame] | 296 | case CachedResource::ImageResource: |
jer.noble@apple.com | 0ca560f | 2016-03-18 19:04:58 +0000 | [diff] [blame] | 297 | case CachedResource::MediaResource: |
oliver@apple.com | 74dbf62 | 2015-03-06 04:43:12 +0000 | [diff] [blame] | 298 | return MixedContentChecker::ContentType::ActiveCanWarn; |
| 299 | |
| 300 | case CachedResource::CSSStyleSheet: |
| 301 | case CachedResource::Script: |
| 302 | case CachedResource::FontResource: |
| 303 | return MixedContentChecker::ContentType::Active; |
| 304 | |
| 305 | #if ENABLE(SVG_FONTS) |
| 306 | case CachedResource::SVGFontResource: |
| 307 | return MixedContentChecker::ContentType::Active; |
| 308 | #endif |
| 309 | |
| 310 | case CachedResource::RawResource: |
| 311 | case CachedResource::SVGDocumentResource: |
| 312 | return MixedContentChecker::ContentType::Active; |
| 313 | #if ENABLE(XSLT) |
| 314 | case CachedResource::XSLStyleSheet: |
| 315 | return MixedContentChecker::ContentType::Active; |
| 316 | #endif |
| 317 | |
yoav@yoav.ws | c7bd832 | 2016-04-18 06:06:42 +0000 | [diff] [blame] | 318 | case CachedResource::LinkPreload: |
oliver@apple.com | 74dbf62 | 2015-03-06 04:43:12 +0000 | [diff] [blame] | 319 | #if ENABLE(LINK_PREFETCH) |
| 320 | case CachedResource::LinkPrefetch: |
| 321 | case CachedResource::LinkSubresource: |
| 322 | return MixedContentChecker::ContentType::Active; |
| 323 | #endif |
| 324 | |
| 325 | #if ENABLE(VIDEO_TRACK) |
| 326 | case CachedResource::TextTrackResource: |
| 327 | return MixedContentChecker::ContentType::Active; |
| 328 | #endif |
| 329 | default: |
| 330 | ASSERT_NOT_REACHED(); |
| 331 | return MixedContentChecker::ContentType::Active; |
| 332 | } |
| 333 | } |
| 334 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 335 | bool CachedResourceLoader::checkInsecureContent(CachedResource::Type type, const URL& url) const |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 336 | { |
| 337 | switch (type) { |
| 338 | case CachedResource::Script: |
| 339 | #if ENABLE(XSLT) |
| 340 | case CachedResource::XSLStyleSheet: |
| 341 | #endif |
reni@webkit.org | f76a86e | 2012-02-24 14:31:59 +0000 | [diff] [blame] | 342 | case CachedResource::SVGDocumentResource: |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 343 | case CachedResource::CSSStyleSheet: |
| 344 | // These resource can inject script into the current document (Script, |
| 345 | // XSL) or exfiltrate the content of the current document (CSS). |
| 346 | if (Frame* f = frame()) |
akling@apple.com | 10093a9 | 2013-09-08 08:15:27 +0000 | [diff] [blame] | 347 | if (!f->loader().mixedContentChecker().canRunInsecureContent(m_document->securityOrigin(), url)) |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 348 | return false; |
| 349 | break; |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 350 | #if ENABLE(VIDEO_TRACK) |
japhet@chromium.org | 5385c71 | 2011-10-31 23:49:47 +0000 | [diff] [blame] | 351 | case CachedResource::TextTrackResource: |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 352 | #endif |
dbates@webkit.org | 55b489b | 2016-03-16 19:46:49 +0000 | [diff] [blame] | 353 | case CachedResource::MediaResource: |
tsepez@chromium.org | 60c4653 | 2012-06-26 23:18:31 +0000 | [diff] [blame] | 354 | case CachedResource::RawResource: |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 355 | case CachedResource::ImageResource: |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 356 | #if ENABLE(SVG_FONTS) |
| 357 | case CachedResource::SVGFontResource: |
| 358 | #endif |
yoav@yoav.ws | c7bd832 | 2016-04-18 06:06:42 +0000 | [diff] [blame] | 359 | case CachedResource::FontResource: |
| 360 | case CachedResource::LinkPreload: { |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 361 | // These resources can corrupt only the frame's pixels. |
| 362 | if (Frame* f = frame()) { |
akling@apple.com | 5298947 | 2013-09-16 20:09:25 +0000 | [diff] [blame] | 363 | Frame& topFrame = f->tree().top(); |
oliver@apple.com | 74dbf62 | 2015-03-06 04:43:12 +0000 | [diff] [blame] | 364 | if (!topFrame.loader().mixedContentChecker().canDisplayInsecureContent(topFrame.document()->securityOrigin(), contentTypeFromResourceType(type), url)) |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 365 | return false; |
| 366 | } |
| 367 | break; |
| 368 | } |
japhet@chromium.org | a269e56 | 2012-10-25 20:05:17 +0000 | [diff] [blame] | 369 | case CachedResource::MainResource: |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 370 | #if ENABLE(LINK_PREFETCH) |
| 371 | case CachedResource::LinkPrefetch: |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 372 | case CachedResource::LinkSubresource: |
| 373 | // Prefetch cannot affect the current document. |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 374 | #endif |
japhet@chromium.org | b0444b3 | 2011-10-07 18:16:03 +0000 | [diff] [blame] | 375 | break; |
commit-queue@webkit.org | 4650bbcb | 2011-06-17 19:11:14 +0000 | [diff] [blame] | 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
dbates@webkit.org | 28db74a | 2016-04-15 22:23:44 +0000 | [diff] [blame] | 380 | bool CachedResourceLoader::canRequest(CachedResource::Type type, const URL& url, const ResourceLoaderOptions& options, bool forPreload, bool didReceiveRedirectResponse) |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 381 | { |
japhet@chromium.org | a269e56 | 2012-10-25 20:05:17 +0000 | [diff] [blame] | 382 | if (document() && !document()->securityOrigin()->canDisplay(url)) { |
abarth@webkit.org | e60f0a6 | 2011-04-22 21:20:12 +0000 | [diff] [blame] | 383 | if (!forPreload) |
mkwst@chromium.org | 6f6d9bc | 2013-05-30 12:08:58 +0000 | [diff] [blame] | 384 | FrameLoader::reportLocalLoadFailed(frame(), url.stringCenterEllipsizedToLength()); |
abarth@webkit.org | e60f0a6 | 2011-04-22 21:20:12 +0000 | [diff] [blame] | 385 | LOG(ResourceLoading, "CachedResourceLoader::requestResource URL was not allowed by SecurityOrigin::canDisplay"); |
hs85.jeong@samsung.com | 1317914 | 2015-10-21 02:12:32 +0000 | [diff] [blame] | 386 | return false; |
abarth@webkit.org | e60f0a6 | 2011-04-22 21:20:12 +0000 | [diff] [blame] | 387 | } |
| 388 | |
dbates@webkit.org | 264add7 | 2015-07-07 00:42:50 +0000 | [diff] [blame] | 389 | bool skipContentSecurityPolicyCheck = options.contentSecurityPolicyImposition() == ContentSecurityPolicyImposition::SkipPolicyCheck; |
dbates@webkit.org | 28db74a | 2016-04-15 22:23:44 +0000 | [diff] [blame] | 390 | ContentSecurityPolicy::RedirectResponseReceived redirectResponseReceived = didReceiveRedirectResponse ? ContentSecurityPolicy::RedirectResponseReceived::Yes : ContentSecurityPolicy::RedirectResponseReceived::No; |
mkwst@chromium.org | 8cd0cc7 | 2012-10-31 09:59:45 +0000 | [diff] [blame] | 391 | |
abarth@webkit.org | c17c959 | 2008-06-21 20:33:42 +0000 | [diff] [blame] | 392 | // Some types of resources can be loaded only from the same origin. Other |
| 393 | // types of resources, like Images, Scripts, and CSS, can be loaded from |
| 394 | // any URL. |
| 395 | switch (type) { |
japhet@chromium.org | a269e56 | 2012-10-25 20:05:17 +0000 | [diff] [blame] | 396 | case CachedResource::MainResource: |
abarth@webkit.org | c17c959 | 2008-06-21 20:33:42 +0000 | [diff] [blame] | 397 | case CachedResource::ImageResource: |
| 398 | case CachedResource::CSSStyleSheet: |
| 399 | case CachedResource::Script: |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 400 | #if ENABLE(SVG_FONTS) |
| 401 | case CachedResource::SVGFontResource: |
| 402 | #endif |
dbates@webkit.org | 55b489b | 2016-03-16 19:46:49 +0000 | [diff] [blame] | 403 | case CachedResource::MediaResource: |
abarth@webkit.org | c17c959 | 2008-06-21 20:33:42 +0000 | [diff] [blame] | 404 | case CachedResource::FontResource: |
japhet@chromium.org | b0444b3 | 2011-10-07 18:16:03 +0000 | [diff] [blame] | 405 | case CachedResource::RawResource: |
yoav@yoav.ws | c7bd832 | 2016-04-18 06:06:42 +0000 | [diff] [blame] | 406 | case CachedResource::LinkPreload: |
commit-queue@webkit.org | a67f42c | 2010-07-10 02:41:10 +0000 | [diff] [blame] | 407 | #if ENABLE(LINK_PREFETCH) |
commit-queue@webkit.org | 768c402 | 2011-05-21 19:24:56 +0000 | [diff] [blame] | 408 | case CachedResource::LinkPrefetch: |
commit-queue@webkit.org | 768c402 | 2011-05-21 19:24:56 +0000 | [diff] [blame] | 409 | case CachedResource::LinkSubresource: |
commit-queue@webkit.org | a67f42c | 2010-07-10 02:41:10 +0000 | [diff] [blame] | 410 | #endif |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 411 | #if ENABLE(VIDEO_TRACK) |
japhet@chromium.org | 5385c71 | 2011-10-31 23:49:47 +0000 | [diff] [blame] | 412 | case CachedResource::TextTrackResource: |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 413 | #endif |
commit-queue@webkit.org | 16c43b4 | 2016-07-04 18:36:30 +0000 | [diff] [blame^] | 414 | if (options.mode == FetchOptions::Mode::SameOrigin && !m_document->securityOrigin()->canRequest(url)) { |
hmuller@adobe.com | 1233f92 | 2013-06-22 19:56:37 +0000 | [diff] [blame] | 415 | printAccessDeniedMessage(url); |
| 416 | return false; |
| 417 | } |
abarth@webkit.org | c17c959 | 2008-06-21 20:33:42 +0000 | [diff] [blame] | 418 | break; |
reni@webkit.org | f76a86e | 2012-02-24 14:31:59 +0000 | [diff] [blame] | 419 | case CachedResource::SVGDocumentResource: |
abarth@webkit.org | c17c959 | 2008-06-21 20:33:42 +0000 | [diff] [blame] | 420 | #if ENABLE(XSLT) |
| 421 | case CachedResource::XSLStyleSheet: |
abarth@webkit.org | 8b48cdb | 2010-11-08 08:59:24 +0000 | [diff] [blame] | 422 | if (!m_document->securityOrigin()->canRequest(url)) { |
abarth@webkit.org | a796cc0 | 2008-11-01 09:31:42 +0000 | [diff] [blame] | 423 | printAccessDeniedMessage(url); |
| 424 | return false; |
abarth@webkit.org | 234c23a | 2008-06-21 20:33:53 +0000 | [diff] [blame] | 425 | } |
abarth@webkit.org | c17c959 | 2008-06-21 20:33:42 +0000 | [diff] [blame] | 426 | #endif |
ossy@webkit.org | 25970ce | 2012-02-24 16:33:06 +0000 | [diff] [blame] | 427 | break; |
abarth@webkit.org | c17c959 | 2008-06-21 20:33:42 +0000 | [diff] [blame] | 428 | } |
abarth@webkit.org | 0f9b302 | 2009-09-11 05:48:06 +0000 | [diff] [blame] | 429 | |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 430 | switch (type) { |
abarth@webkit.org | 7e4c7fa | 2011-10-13 20:03:36 +0000 | [diff] [blame] | 431 | #if ENABLE(XSLT) |
| 432 | case CachedResource::XSLStyleSheet: |
dbates@webkit.org | 28db74a | 2016-04-15 22:23:44 +0000 | [diff] [blame] | 433 | if (!m_document->contentSecurityPolicy()->allowScriptFromSource(url, skipContentSecurityPolicyCheck, redirectResponseReceived)) |
vivek.vg@samsung.com | 4094db7 | 2013-01-23 09:02:01 +0000 | [diff] [blame] | 434 | return false; |
| 435 | break; |
abarth@webkit.org | 7e4c7fa | 2011-10-13 20:03:36 +0000 | [diff] [blame] | 436 | #endif |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 437 | case CachedResource::Script: |
dbates@webkit.org | 28db74a | 2016-04-15 22:23:44 +0000 | [diff] [blame] | 438 | if (!m_document->contentSecurityPolicy()->allowScriptFromSource(url, skipContentSecurityPolicyCheck, redirectResponseReceived)) |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 439 | return false; |
darin@apple.com | c50e047 | 2014-04-21 18:08:49 +0000 | [diff] [blame] | 440 | if (frame() && !frame()->settings().isScriptEnabled()) |
| 441 | return false; |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 442 | break; |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 443 | case CachedResource::CSSStyleSheet: |
dbates@webkit.org | 28db74a | 2016-04-15 22:23:44 +0000 | [diff] [blame] | 444 | if (!m_document->contentSecurityPolicy()->allowStyleFromSource(url, skipContentSecurityPolicyCheck, redirectResponseReceived)) |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 445 | return false; |
| 446 | break; |
reni@webkit.org | f76a86e | 2012-02-24 14:31:59 +0000 | [diff] [blame] | 447 | case CachedResource::SVGDocumentResource: |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 448 | case CachedResource::ImageResource: |
dbates@webkit.org | 28db74a | 2016-04-15 22:23:44 +0000 | [diff] [blame] | 449 | if (!m_document->contentSecurityPolicy()->allowImageFromSource(url, skipContentSecurityPolicyCheck, redirectResponseReceived)) |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 450 | return false; |
| 451 | break; |
mmaxfield@apple.com | af53563 | 2014-11-18 20:04:38 +0000 | [diff] [blame] | 452 | #if ENABLE(SVG_FONTS) |
| 453 | case CachedResource::SVGFontResource: |
| 454 | #endif |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 455 | case CachedResource::FontResource: { |
dbates@webkit.org | 28db74a | 2016-04-15 22:23:44 +0000 | [diff] [blame] | 456 | if (!m_document->contentSecurityPolicy()->allowFontFromSource(url, skipContentSecurityPolicyCheck, redirectResponseReceived)) |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 457 | return false; |
| 458 | break; |
| 459 | } |
japhet@chromium.org | a269e56 | 2012-10-25 20:05:17 +0000 | [diff] [blame] | 460 | case CachedResource::MainResource: |
japhet@chromium.org | b0444b3 | 2011-10-07 18:16:03 +0000 | [diff] [blame] | 461 | case CachedResource::RawResource: |
yoav@yoav.ws | c7bd832 | 2016-04-18 06:06:42 +0000 | [diff] [blame] | 462 | // FIXME: Preload should be subject to connect-src. |
| 463 | case CachedResource::LinkPreload: |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 464 | #if ENABLE(LINK_PREFETCH) |
commit-queue@webkit.org | 768c402 | 2011-05-21 19:24:56 +0000 | [diff] [blame] | 465 | case CachedResource::LinkPrefetch: |
commit-queue@webkit.org | 768c402 | 2011-05-21 19:24:56 +0000 | [diff] [blame] | 466 | case CachedResource::LinkSubresource: |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 467 | #endif |
japhet@chromium.org | b0444b3 | 2011-10-07 18:16:03 +0000 | [diff] [blame] | 468 | break; |
dbates@webkit.org | 55b489b | 2016-03-16 19:46:49 +0000 | [diff] [blame] | 469 | case CachedResource::MediaResource: |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 470 | #if ENABLE(VIDEO_TRACK) |
japhet@chromium.org | 5385c71 | 2011-10-31 23:49:47 +0000 | [diff] [blame] | 471 | case CachedResource::TextTrackResource: |
dbates@webkit.org | 55b489b | 2016-03-16 19:46:49 +0000 | [diff] [blame] | 472 | #endif |
dbates@webkit.org | 28db74a | 2016-04-15 22:23:44 +0000 | [diff] [blame] | 473 | if (!m_document->contentSecurityPolicy()->allowMediaFromSource(url, skipContentSecurityPolicyCheck, redirectResponseReceived)) |
eric.carlson@apple.com | a24babb | 2011-10-17 19:22:45 +0000 | [diff] [blame] | 474 | return false; |
| 475 | break; |
abarth@webkit.org | 75a72fe | 2011-04-08 01:08:59 +0000 | [diff] [blame] | 476 | } |
abarth@webkit.org | 4c18a30 | 2011-03-28 05:15:38 +0000 | [diff] [blame] | 477 | |
dbates@webkit.org | eda26cf | 2014-10-22 23:43:11 +0000 | [diff] [blame] | 478 | // SVG Images have unique security rules that prevent all subresource requests except for data urls. |
| 479 | if (type != CachedResource::MainResource && frame() && frame()->page()) { |
| 480 | if (frame()->page()->chrome().client().isSVGImageChromeClient() && !url.protocolIsData()) |
| 481 | return false; |
| 482 | } |
| 483 | |
aestes@apple.com | 13f72b7 | 2015-12-17 05:53:40 +0000 | [diff] [blame] | 484 | if (!canRequestInContentDispositionAttachmentSandbox(type, url)) |
| 485 | return false; |
| 486 | |
abarth@webkit.org | 1abaf46 | 2011-11-04 22:32:13 +0000 | [diff] [blame] | 487 | // Last of all, check for insecure content. We do this last so that when |
| 488 | // folks block insecure content with a CSP policy, they don't get a warning. |
| 489 | // They'll still get a warning in the console about CSP blocking the load. |
| 490 | |
| 491 | // FIXME: Should we consider forPreload here? |
| 492 | if (!checkInsecureContent(type, url)) |
| 493 | return false; |
| 494 | |
abarth@webkit.org | a796cc0 | 2008-11-01 09:31:42 +0000 | [diff] [blame] | 495 | return true; |
| 496 | } |
| 497 | |
aestes@apple.com | 13f72b7 | 2015-12-17 05:53:40 +0000 | [diff] [blame] | 498 | bool CachedResourceLoader::canRequestInContentDispositionAttachmentSandbox(CachedResource::Type type, const URL& url) const |
| 499 | { |
| 500 | Document* document; |
| 501 | |
| 502 | // FIXME: Do we want to expand this to all resource types that the mixed content checker would consider active content? |
| 503 | switch (type) { |
| 504 | case CachedResource::MainResource: |
| 505 | if (auto ownerElement = frame() ? frame()->ownerElement() : nullptr) { |
| 506 | document = &ownerElement->document(); |
| 507 | break; |
| 508 | } |
| 509 | return true; |
| 510 | case CachedResource::CSSStyleSheet: |
| 511 | document = m_document; |
| 512 | break; |
| 513 | default: |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | if (!document->shouldEnforceContentDispositionAttachmentSandbox() || document->securityOrigin()->canRequest(url)) |
| 518 | return true; |
| 519 | |
| 520 | String message = "Unsafe attempt to load URL " + url.stringCenterEllipsizedToLength() + " from document with Content-Disposition: attachment at URL " + document->url().stringCenterEllipsizedToLength() + "."; |
| 521 | document->addConsoleMessage(MessageSource::Security, MessageLevel::Error, message); |
| 522 | return false; |
| 523 | } |
| 524 | |
commit-queue@webkit.org | 5eac608 | 2014-03-11 04:22:10 +0000 | [diff] [blame] | 525 | bool CachedResourceLoader::shouldContinueAfterNotifyingLoadedFromMemoryCache(const CachedResourceRequest& request, CachedResource* resource) |
aestes@apple.com | 0c8e191 | 2013-04-06 03:09:25 +0000 | [diff] [blame] | 526 | { |
| 527 | if (!resource || !frame() || resource->status() != CachedResource::Cached) |
| 528 | return true; |
| 529 | |
commit-queue@webkit.org | 5eac608 | 2014-03-11 04:22:10 +0000 | [diff] [blame] | 530 | ResourceRequest newRequest = ResourceRequest(resource->url()); |
commit-queue@webkit.org | 5eac608 | 2014-03-11 04:22:10 +0000 | [diff] [blame] | 531 | if (request.resourceRequest().hiddenFromInspector()) |
| 532 | newRequest.setHiddenFromInspector(true); |
andersca@apple.com | df550b9 | 2013-08-15 22:17:17 +0000 | [diff] [blame] | 533 | frame()->loader().loadedResourceFromMemoryCache(resource, newRequest); |
ossy@webkit.org | d77a314 | 2015-01-21 08:45:11 +0000 | [diff] [blame] | 534 | |
aestes@apple.com | 0c8e191 | 2013-04-06 03:09:25 +0000 | [diff] [blame] | 535 | // FIXME <http://webkit.org/b/113251>: If the delegate modifies the request's |
| 536 | // URL, it is no longer appropriate to use this CachedResource. |
| 537 | return !newRequest.isNull(); |
| 538 | } |
| 539 | |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 540 | static inline void logMemoryCacheResourceRequest(Frame* frame, const String& description, const String& value = String()) |
| 541 | { |
cdumez@apple.com | 10d0f25 | 2016-04-23 21:33:49 +0000 | [diff] [blame] | 542 | if (!frame || !frame->page()) |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 543 | return; |
| 544 | if (value.isNull()) |
cdumez@apple.com | 10d0f25 | 2016-04-23 21:33:49 +0000 | [diff] [blame] | 545 | frame->page()->diagnosticLoggingClient().logDiagnosticMessage(DiagnosticLoggingKeys::resourceRequestKey(), description, ShouldSample::Yes); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 546 | else |
cdumez@apple.com | 10d0f25 | 2016-04-23 21:33:49 +0000 | [diff] [blame] | 547 | frame->page()->diagnosticLoggingClient().logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceRequestKey(), description, value, ShouldSample::Yes); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 548 | } |
| 549 | |
commit-queue@webkit.org | 4b342506 | 2015-04-02 00:22:54 +0000 | [diff] [blame] | 550 | CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(CachedResource::Type type, CachedResourceRequest& request) |
abarth@webkit.org | a796cc0 | 2008-11-01 09:31:42 +0000 | [diff] [blame] | 551 | { |
bfulgham@apple.com | b30d7f8 | 2016-06-07 15:47:30 +0000 | [diff] [blame] | 552 | if (Document* document = this->document()) |
| 553 | document->contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request.mutableResourceRequest(), ContentSecurityPolicy::InsecureRequestType::Load); |
| 554 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 555 | URL url = request.resourceRequest().url(); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 556 | |
antti@apple.com | 8c81c03 | 2015-04-29 18:51:23 +0000 | [diff] [blame] | 557 | LOG(ResourceLoading, "CachedResourceLoader::requestResource '%s', charset '%s', priority=%d, forPreload=%u", url.stringCenterEllipsizedToLength().latin1().data(), request.charset().latin1().data(), request.priority() ? static_cast<int>(request.priority().value()) : -1, request.forPreload()); |
antti@apple.com | 76bf3f2 | 2010-12-15 11:27:52 +0000 | [diff] [blame] | 558 | |
| 559 | // If only the fragment identifiers differ, it is the same resource. |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 560 | url = MemoryCache::removeFragmentIdentifierIfNeeded(url); |
abarth@webkit.org | a796cc0 | 2008-11-01 09:31:42 +0000 | [diff] [blame] | 561 | |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 562 | if (!url.isValid()) |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 563 | return nullptr; |
abarth@webkit.org | c17c959 | 2008-06-21 20:33:42 +0000 | [diff] [blame] | 564 | |
commit-queue@webkit.org | 4b342506 | 2015-04-02 00:22:54 +0000 | [diff] [blame] | 565 | if (!canRequest(type, url, request.options(), request.forPreload())) |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 566 | return nullptr; |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 567 | |
benjamin@webkit.org | 74bde2b | 2015-01-09 01:56:00 +0000 | [diff] [blame] | 568 | #if ENABLE(CONTENT_EXTENSIONS) |
weinig@apple.com | 1578a24 | 2016-03-15 00:48:40 +0000 | [diff] [blame] | 569 | if (frame() && frame()->mainFrame().page() && m_documentLoader) { |
| 570 | if (frame()->mainFrame().page()->userContentProvider().processContentExtensionRulesForLoad(request.mutableResourceRequest(), toResourceType(type), *m_documentLoader) == ContentExtensions::BlockedStatus::Blocked) { |
commit-queue@webkit.org | 8090ccb | 2015-10-06 08:18:08 +0000 | [diff] [blame] | 571 | if (type == CachedResource::Type::MainResource) { |
| 572 | auto resource = createResource(type, request.mutableResourceRequest(), request.charset(), sessionID()); |
| 573 | ASSERT(resource); |
| 574 | resource->error(CachedResource::Status::LoadError); |
ddkilzer@apple.com | 778bccc | 2015-12-25 12:22:16 +0000 | [diff] [blame] | 575 | resource->setResourceError(ResourceError(ContentExtensions::WebKitContentBlockerDomain, 0, request.resourceRequest().url(), WEB_UI_STRING("The URL was blocked by a content blocker", "WebKitErrorBlockedByContentBlocker description"))); |
commit-queue@webkit.org | 8090ccb | 2015-10-06 08:18:08 +0000 | [diff] [blame] | 576 | return resource; |
| 577 | } |
| 578 | return nullptr; |
| 579 | } |
commit-queue@webkit.org | cdda290 | 2015-12-23 10:04:44 +0000 | [diff] [blame] | 580 | url = request.resourceRequest().url(); // The content extension could have changed it from http to https. |
| 581 | url = MemoryCache::removeFragmentIdentifierIfNeeded(url); // Might need to remove fragment identifier again. |
commit-queue@webkit.org | 8090ccb | 2015-10-06 08:18:08 +0000 | [diff] [blame] | 582 | } |
benjamin@webkit.org | 74bde2b | 2015-01-09 01:56:00 +0000 | [diff] [blame] | 583 | #endif |
| 584 | |
cdumez@apple.com | 41d2d70 | 2015-02-02 20:50:16 +0000 | [diff] [blame] | 585 | auto& memoryCache = MemoryCache::singleton(); |
jer.noble@apple.com | 2ed319d | 2016-01-28 19:52:57 +0000 | [diff] [blame] | 586 | if (request.allowsCaching() && memoryCache.disabled()) { |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 587 | DocumentResourceMap::iterator it = m_documentResources.find(url.string()); |
antti@apple.com | 7dca246 | 2009-01-08 23:44:59 +0000 | [diff] [blame] | 588 | if (it != m_documentResources.end()) { |
hs85.jeong@samsung.com | 1317914 | 2015-10-21 02:12:32 +0000 | [diff] [blame] | 589 | it->value->setOwningCachedResourceLoader(nullptr); |
antti@apple.com | 7dca246 | 2009-01-08 23:44:59 +0000 | [diff] [blame] | 590 | m_documentResources.remove(it); |
oliver | 1e69b1b | 2007-08-03 22:37:08 +0000 | [diff] [blame] | 591 | } |
andersca | 8319793 | 2007-07-26 23:52:02 +0000 | [diff] [blame] | 592 | } |
eric@webkit.org | e0a5162 | 2008-06-26 22:45:47 +0000 | [diff] [blame] | 593 | |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 594 | // See if we can use an existing resource from the cache. |
jpfau@apple.com | 97c6a7f | 2013-02-26 00:49:17 +0000 | [diff] [blame] | 595 | CachedResourceHandle<CachedResource> resource; |
| 596 | #if ENABLE(CACHE_PARTITIONING) |
| 597 | if (document()) |
cdumez@apple.com | 8730b6e | 2014-10-21 02:10:47 +0000 | [diff] [blame] | 598 | request.mutableResourceRequest().setDomainForCachePartition(document()->topOrigin()->domainForCachePartition()); |
jpfau@apple.com | 97c6a7f | 2013-02-26 00:49:17 +0000 | [diff] [blame] | 599 | #endif |
| 600 | |
jer.noble@apple.com | 2ed319d | 2016-01-28 19:52:57 +0000 | [diff] [blame] | 601 | if (request.allowsCaching()) |
| 602 | resource = memoryCache.resourceForRequest(request.resourceRequest(), sessionID()); |
hyatt | c440449 | 2006-10-19 09:21:54 +0000 | [diff] [blame] | 603 | |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 604 | logMemoryCacheResourceRequest(frame(), resource ? DiagnosticLoggingKeys::inMemoryCacheKey() : DiagnosticLoggingKeys::notInMemoryCacheKey()); |
cdumez@apple.com | 06defb3 | 2014-12-19 19:15:36 +0000 | [diff] [blame] | 605 | |
antti@apple.com | 49ebfef | 2016-01-12 17:31:02 +0000 | [diff] [blame] | 606 | const RevalidationPolicy policy = determineRevalidationPolicy(type, request, resource.get()); |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 607 | switch (policy) { |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 608 | case Reload: |
cdumez@apple.com | 41d2d70 | 2015-02-02 20:50:16 +0000 | [diff] [blame] | 609 | memoryCache.remove(*resource); |
joepeck@webkit.org | aa676ee5 | 2014-01-28 04:04:52 +0000 | [diff] [blame] | 610 | FALLTHROUGH; |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 611 | case Load: |
cdumez@apple.com | 06defb3 | 2014-12-19 19:15:36 +0000 | [diff] [blame] | 612 | if (resource) |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 613 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::unusedKey()); |
andersca@apple.com | 7d7681a | 2014-04-09 01:45:43 +0000 | [diff] [blame] | 614 | resource = loadResource(type, request); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 615 | break; |
| 616 | case Revalidate: |
cdumez@apple.com | 06defb3 | 2014-12-19 19:15:36 +0000 | [diff] [blame] | 617 | if (resource) |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 618 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::revalidatingKey()); |
simonjam@chromium.org | 76cfc20 | 2012-12-03 22:20:47 +0000 | [diff] [blame] | 619 | resource = revalidateResource(request, resource.get()); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 620 | break; |
| 621 | case Use: |
commit-queue@webkit.org | 5eac608 | 2014-03-11 04:22:10 +0000 | [diff] [blame] | 622 | if (!shouldContinueAfterNotifyingLoadedFromMemoryCache(request, resource.get())) |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 623 | return nullptr; |
| 624 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::usedKey()); |
cdumez@apple.com | 41d2d70 | 2015-02-02 20:50:16 +0000 | [diff] [blame] | 625 | memoryCache.resourceAccessed(*resource); |
ossy@webkit.org | ec38e43 | 2016-05-17 17:07:19 +0000 | [diff] [blame] | 626 | #if ENABLE(WEB_TIMING) |
commit-queue@webkit.org | fab6e0d | 2016-06-01 20:15:23 +0000 | [diff] [blame] | 627 | if (document() && RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled()) { |
yoav@yoav.ws | 4685b02 | 2016-05-13 22:34:29 +0000 | [diff] [blame] | 628 | m_resourceTimingInfo.storeResourceTimingInitiatorInformation(resource, request, frame()); |
commit-queue@webkit.org | fab6e0d | 2016-06-01 20:15:23 +0000 | [diff] [blame] | 629 | m_resourceTimingInfo.addResourceTiming(resource.get(), *document()); |
yoav@yoav.ws | 4685b02 | 2016-05-13 22:34:29 +0000 | [diff] [blame] | 630 | } |
ossy@webkit.org | ec38e43 | 2016-05-17 17:07:19 +0000 | [diff] [blame] | 631 | #endif |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 632 | break; |
aliceli1 | bf1f391 | 2007-01-27 02:11:15 +0000 | [diff] [blame] | 633 | } |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 634 | |
| 635 | if (!resource) |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 636 | return nullptr; |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 637 | |
simonjam@chromium.org | 8573384 | 2013-02-02 07:40:08 +0000 | [diff] [blame] | 638 | if (!request.forPreload() || policy != Use) |
| 639 | resource->setLoadPriority(request.priority()); |
| 640 | |
commit-queue@webkit.org | 864dbda | 2012-11-16 12:21:57 +0000 | [diff] [blame] | 641 | if ((policy != Use || resource->stillNeedsLoad()) && CachedResourceRequest::NoDefer == request.defer()) { |
cdumez@apple.com | 0bb03ec | 2015-01-28 05:53:53 +0000 | [diff] [blame] | 642 | resource->load(*this, request.options()); |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 643 | |
beidson@apple.com | 94a692d | 2014-07-28 23:14:04 +0000 | [diff] [blame] | 644 | // We don't support immediate loads, but we do support immediate failure. |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 645 | if (resource->errorOccurred()) { |
jer.noble@apple.com | 2ed319d | 2016-01-28 19:52:57 +0000 | [diff] [blame] | 646 | if (resource->allowsCaching() && resource->inCache()) |
cdumez@apple.com | 41d2d70 | 2015-02-02 20:50:16 +0000 | [diff] [blame] | 647 | memoryCache.remove(*resource); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 648 | return nullptr; |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
akling@apple.com | 7b726d1 | 2014-04-24 04:11:50 +0000 | [diff] [blame] | 652 | if (document() && !document()->loadEventFinished() && !request.resourceRequest().url().protocolIsData()) |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 653 | m_validatedURLs.add(request.resourceRequest().url()); |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 654 | |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 655 | ASSERT(resource->url() == url.string()); |
commit-queue@webkit.org | 945e62b | 2014-03-30 06:01:08 +0000 | [diff] [blame] | 656 | m_documentResources.set(resource->url(), resource); |
hyatt | c440449 | 2006-10-19 09:21:54 +0000 | [diff] [blame] | 657 | return resource; |
| 658 | } |
ap@apple.com | 6edc42b | 2012-05-11 02:31:26 +0000 | [diff] [blame] | 659 | |
akling@apple.com | 7b726d1 | 2014-04-24 04:11:50 +0000 | [diff] [blame] | 660 | void CachedResourceLoader::documentDidFinishLoadEvent() |
| 661 | { |
| 662 | m_validatedURLs.clear(); |
| 663 | } |
| 664 | |
simonjam@chromium.org | 76cfc20 | 2012-12-03 22:20:47 +0000 | [diff] [blame] | 665 | CachedResourceHandle<CachedResource> CachedResourceLoader::revalidateResource(const CachedResourceRequest& request, CachedResource* resource) |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 666 | { |
| 667 | ASSERT(resource); |
| 668 | ASSERT(resource->inCache()); |
cdumez@apple.com | 41d2d70 | 2015-02-02 20:50:16 +0000 | [diff] [blame] | 669 | auto& memoryCache = MemoryCache::singleton(); |
| 670 | ASSERT(!memoryCache.disabled()); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 671 | ASSERT(resource->canUseCacheValidator()); |
| 672 | ASSERT(!resource->resourceToRevalidate()); |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 673 | ASSERT(resource->sessionID() == sessionID()); |
jer.noble@apple.com | 2ed319d | 2016-01-28 19:52:57 +0000 | [diff] [blame] | 674 | ASSERT(resource->allowsCaching()); |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 675 | |
commit-queue@webkit.org | 9ab4d3e | 2014-03-05 20:11:03 +0000 | [diff] [blame] | 676 | CachedResourceHandle<CachedResource> newResource = createResource(resource->type(), resource->resourceRequest(), resource->encoding(), resource->sessionID()); |
antti@apple.com | 86e0ce3 | 2015-08-12 21:07:49 +0000 | [diff] [blame] | 677 | |
ap@apple.com | 6edc42b | 2012-05-11 02:31:26 +0000 | [diff] [blame] | 678 | LOG(ResourceLoading, "Resource %p created to revalidate %p", newResource.get(), resource); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 679 | newResource->setResourceToRevalidate(resource); |
| 680 | |
cdumez@apple.com | 41d2d70 | 2015-02-02 20:50:16 +0000 | [diff] [blame] | 681 | memoryCache.remove(*resource); |
| 682 | memoryCache.add(*newResource); |
ossy@webkit.org | ec38e43 | 2016-05-17 17:07:19 +0000 | [diff] [blame] | 683 | #if ENABLE(WEB_TIMING) |
yoav@yoav.ws | 4ea6dc8 | 2016-05-02 08:21:50 +0000 | [diff] [blame] | 684 | if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled()) |
yoav@yoav.ws | 4685b02 | 2016-05-13 22:34:29 +0000 | [diff] [blame] | 685 | m_resourceTimingInfo.storeResourceTimingInitiatorInformation(resource, request, frame()); |
ossy@webkit.org | ec38e43 | 2016-05-17 17:07:19 +0000 | [diff] [blame] | 686 | #else |
| 687 | UNUSED_PARAM(request); |
| 688 | #endif |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 689 | return newResource; |
| 690 | } |
| 691 | |
andersca@apple.com | 7d7681a | 2014-04-09 01:45:43 +0000 | [diff] [blame] | 692 | CachedResourceHandle<CachedResource> CachedResourceLoader::loadResource(CachedResource::Type type, CachedResourceRequest& request) |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 693 | { |
cdumez@apple.com | 41d2d70 | 2015-02-02 20:50:16 +0000 | [diff] [blame] | 694 | auto& memoryCache = MemoryCache::singleton(); |
commit-queue@webkit.org | c5b6a80 | 2016-06-01 00:28:25 +0000 | [diff] [blame] | 695 | ASSERT(!request.allowsCaching() || !memoryCache.resourceForRequest(request.resourceRequest(), sessionID())); |
simonjam@chromium.org | 5f12716 | 2012-11-30 19:17:45 +0000 | [diff] [blame] | 696 | |
mkwst@chromium.org | 6f6d9bc | 2013-05-30 12:08:58 +0000 | [diff] [blame] | 697 | LOG(ResourceLoading, "Loading CachedResource for '%s'.", request.resourceRequest().url().stringCenterEllipsizedToLength().latin1().data()); |
simonjam@chromium.org | 5f12716 | 2012-11-30 19:17:45 +0000 | [diff] [blame] | 698 | |
andersca@apple.com | 7d7681a | 2014-04-09 01:45:43 +0000 | [diff] [blame] | 699 | CachedResourceHandle<CachedResource> resource = createResource(type, request.mutableResourceRequest(), request.charset(), sessionID()); |
simonjam@chromium.org | 5f12716 | 2012-11-30 19:17:45 +0000 | [diff] [blame] | 700 | |
jer.noble@apple.com | 2ed319d | 2016-01-28 19:52:57 +0000 | [diff] [blame] | 701 | if (request.allowsCaching() && !memoryCache.add(*resource)) |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 702 | resource->setOwningCachedResourceLoader(this); |
ossy@webkit.org | ec38e43 | 2016-05-17 17:07:19 +0000 | [diff] [blame] | 703 | #if ENABLE(WEB_TIMING) |
yoav@yoav.ws | 4ea6dc8 | 2016-05-02 08:21:50 +0000 | [diff] [blame] | 704 | if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled()) |
yoav@yoav.ws | 4685b02 | 2016-05-13 22:34:29 +0000 | [diff] [blame] | 705 | m_resourceTimingInfo.storeResourceTimingInitiatorInformation(resource, request, frame()); |
ossy@webkit.org | ec38e43 | 2016-05-17 17:07:19 +0000 | [diff] [blame] | 706 | #endif |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 707 | return resource; |
| 708 | } |
| 709 | |
antti@apple.com | 6e31387 | 2015-05-13 23:09:13 +0000 | [diff] [blame] | 710 | static void logRevalidation(const String& reason, DiagnosticLoggingClient& logClient) |
| 711 | { |
| 712 | logClient.logDiagnosticMessageWithValue(DiagnosticLoggingKeys::cachedResourceRevalidationKey(), DiagnosticLoggingKeys::reasonKey(), reason, ShouldSample::Yes); |
| 713 | } |
| 714 | |
| 715 | static void logResourceRevalidationDecision(CachedResource::RevalidationDecision reason, const Frame* frame) |
| 716 | { |
cdumez@apple.com | 10d0f25 | 2016-04-23 21:33:49 +0000 | [diff] [blame] | 717 | if (!frame || !frame->page()) |
antti@apple.com | 6e31387 | 2015-05-13 23:09:13 +0000 | [diff] [blame] | 718 | return; |
cdumez@apple.com | 10d0f25 | 2016-04-23 21:33:49 +0000 | [diff] [blame] | 719 | auto& logClient = frame->page()->diagnosticLoggingClient(); |
antti@apple.com | 6e31387 | 2015-05-13 23:09:13 +0000 | [diff] [blame] | 720 | switch (reason) { |
| 721 | case CachedResource::RevalidationDecision::No: |
| 722 | break; |
| 723 | case CachedResource::RevalidationDecision::YesDueToExpired: |
| 724 | logRevalidation(DiagnosticLoggingKeys::isExpiredKey(), logClient); |
| 725 | break; |
| 726 | case CachedResource::RevalidationDecision::YesDueToNoStore: |
| 727 | logRevalidation(DiagnosticLoggingKeys::noStoreKey(), logClient); |
| 728 | break; |
| 729 | case CachedResource::RevalidationDecision::YesDueToNoCache: |
| 730 | logRevalidation(DiagnosticLoggingKeys::noCacheKey(), logClient); |
| 731 | break; |
| 732 | case CachedResource::RevalidationDecision::YesDueToCachePolicy: |
| 733 | logRevalidation(DiagnosticLoggingKeys::reloadKey(), logClient); |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | |
antti@apple.com | 49ebfef | 2016-01-12 17:31:02 +0000 | [diff] [blame] | 738 | CachedResourceLoader::RevalidationPolicy CachedResourceLoader::determineRevalidationPolicy(CachedResource::Type type, CachedResourceRequest& cachedResourceRequest, CachedResource* existingResource) const |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 739 | { |
antti@apple.com | 49ebfef | 2016-01-12 17:31:02 +0000 | [diff] [blame] | 740 | auto& request = cachedResourceRequest.resourceRequest(); |
| 741 | |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 742 | if (!existingResource) |
| 743 | return Load; |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 744 | |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 745 | // We already have a preload going for this URL. |
antti@apple.com | 49ebfef | 2016-01-12 17:31:02 +0000 | [diff] [blame] | 746 | if (cachedResourceRequest.forPreload() && existingResource->isPreloaded()) |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 747 | return Use; |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 748 | |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 749 | // If the same URL has been loaded as a different type, we need to reload. |
| 750 | if (existingResource->type() != type) { |
| 751 | LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to type mismatch."); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 752 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::unusedReasonTypeMismatchKey()); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 753 | return Reload; |
| 754 | } |
japhet@chromium.org | b0444b3 | 2011-10-07 18:16:03 +0000 | [diff] [blame] | 755 | |
antti@apple.com | 7f81237 | 2016-06-08 13:37:04 +0000 | [diff] [blame] | 756 | if (!existingResource->varyHeaderValuesMatch(request, *this)) |
| 757 | return Reload; |
| 758 | |
antti@apple.com | 6d5c6b3 | 2016-04-22 17:37:09 +0000 | [diff] [blame] | 759 | auto* textDecoder = existingResource->textResourceDecoder(); |
| 760 | if (textDecoder && !textDecoder->hasEqualEncodingForCharset(cachedResourceRequest.charset())) |
antti@apple.com | 49ebfef | 2016-01-12 17:31:02 +0000 | [diff] [blame] | 761 | return Reload; |
| 762 | |
antti@apple.com | 2c4a2a2 | 2015-08-14 14:08:59 +0000 | [diff] [blame] | 763 | // FIXME: We should use the same cache policy for all resource types. The raw resource policy is overly strict |
| 764 | // while the normal subresource policy is too loose. |
commit-queue@webkit.org | 3dd4580 | 2016-03-22 19:12:32 +0000 | [diff] [blame] | 765 | if (existingResource->isMainOrMediaOrRawResource() && frame()) { |
antti@apple.com | 2c4a2a2 | 2015-08-14 14:08:59 +0000 | [diff] [blame] | 766 | bool strictPolicyDisabled = frame()->loader().isStrictRawResourceValidationPolicyDisabledForTesting(); |
| 767 | bool canReuseRawResource = strictPolicyDisabled || downcast<CachedRawResource>(*existingResource).canReuse(request); |
| 768 | if (!canReuseRawResource) |
| 769 | return Reload; |
| 770 | } |
japhet@chromium.org | 548ffc1 | 2012-02-14 06:45:41 +0000 | [diff] [blame] | 771 | |
ap@apple.com | 2a26234 | 2013-10-31 16:50:03 +0000 | [diff] [blame] | 772 | // Conditional requests should have failed canReuse check. |
| 773 | ASSERT(!request.isConditional()); |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 774 | |
| 775 | // Do not load from cache if images are not enabled. The load for this image will be blocked |
| 776 | // in CachedImage::load. |
antti@apple.com | 49ebfef | 2016-01-12 17:31:02 +0000 | [diff] [blame] | 777 | if (cachedResourceRequest.defer() == CachedResourceRequest::DeferredByClient) |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 778 | return Reload; |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 779 | |
| 780 | // Don't reload resources while pasting. |
| 781 | if (m_allowStaleResources) |
| 782 | return Use; |
| 783 | |
antti@apple.com | 86e0ce3 | 2015-08-12 21:07:49 +0000 | [diff] [blame] | 784 | // Always use preloads. |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 785 | if (existingResource->isPreloaded()) |
| 786 | return Use; |
cdumez@apple.com | 74ef985 | 2015-01-07 01:59:10 +0000 | [diff] [blame] | 787 | |
antti@apple.com | 86e0ce3 | 2015-08-12 21:07:49 +0000 | [diff] [blame] | 788 | // We can find resources that are being validated from cache only when validation is just successfully completing. |
| 789 | if (existingResource->validationCompleting()) |
| 790 | return Use; |
| 791 | ASSERT(!existingResource->validationInProgress()); |
| 792 | |
cdumez@apple.com | 74ef985 | 2015-01-07 01:59:10 +0000 | [diff] [blame] | 793 | // Validate the redirect chain. |
| 794 | bool cachePolicyIsHistoryBuffer = cachePolicy(type) == CachePolicyHistoryBuffer; |
| 795 | if (!existingResource->redirectChainAllowsReuse(cachePolicyIsHistoryBuffer ? ReuseExpiredRedirection : DoNotReuseExpiredRedirection)) { |
| 796 | LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to not cached or expired redirections."); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 797 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::unusedReasonRedirectChainKey()); |
cdumez@apple.com | 74ef985 | 2015-01-07 01:59:10 +0000 | [diff] [blame] | 798 | return Reload; |
| 799 | } |
| 800 | |
| 801 | // CachePolicyHistoryBuffer uses the cache except if this is a main resource with "cache-control: no-store". |
| 802 | if (cachePolicyIsHistoryBuffer) { |
| 803 | // FIXME: Ignoring "cache-control: no-cache" for sub-resources on history navigation but not the main |
| 804 | // resource is inconsistent. We should probably harmonize this. |
| 805 | if (!existingResource->response().cacheControlContainsNoStore() || type != CachedResource::MainResource) |
| 806 | return Use; |
| 807 | } |
antti@apple.com | 7d58d6e | 2011-01-10 18:32:58 +0000 | [diff] [blame] | 808 | |
| 809 | // Don't reuse resources with Cache-control: no-store. |
| 810 | if (existingResource->response().cacheControlContainsNoStore()) { |
| 811 | LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to Cache-control: no-store."); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 812 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::unusedReasonNoStoreKey()); |
antti@apple.com | 7d58d6e | 2011-01-10 18:32:58 +0000 | [diff] [blame] | 813 | return Reload; |
| 814 | } |
| 815 | |
kbr@google.com | 9a62ac3 | 2011-07-23 00:18:34 +0000 | [diff] [blame] | 816 | // If credentials were sent with the previous request and won't be |
| 817 | // with this one, or vice versa, re-fetch the resource. |
| 818 | // |
| 819 | // This helps with the case where the server sends back |
| 820 | // "Access-Control-Allow-Origin: *" all the time, but some of the |
| 821 | // client's requests are made without CORS and some with. |
| 822 | if (existingResource->resourceRequest().allowCookies() != request.allowCookies()) { |
| 823 | LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to difference in credentials settings."); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 824 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::unusedReasonCredentialSettingsKey()); |
kbr@google.com | 9a62ac3 | 2011-07-23 00:18:34 +0000 | [diff] [blame] | 825 | return Reload; |
| 826 | } |
| 827 | |
simonjam@chromium.org | 94d9cae | 2011-08-18 18:41:28 +0000 | [diff] [blame] | 828 | // During the initial load, avoid loading the same resource multiple times for a single document, even if the cache policies would tell us to. |
japhet@chromium.org | 97fe905 | 2012-10-09 21:58:25 +0000 | [diff] [blame] | 829 | if (document() && !document()->loadEventFinished() && m_validatedURLs.contains(existingResource->url())) |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 830 | return Use; |
simonjam@chromium.org | 94d9cae | 2011-08-18 18:41:28 +0000 | [diff] [blame] | 831 | |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 832 | // CachePolicyReload always reloads |
japhet@chromium.org | bb0c487 | 2013-02-13 02:42:02 +0000 | [diff] [blame] | 833 | if (cachePolicy(type) == CachePolicyReload) { |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 834 | LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to CachePolicyReload."); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 835 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::unusedReasonReloadKey()); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 836 | return Reload; |
| 837 | } |
| 838 | |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 839 | // We'll try to reload the resource if it failed last time. |
| 840 | if (existingResource->errorOccurred()) { |
| 841 | LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicye reloading due to resource being in the error state"); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 842 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::unusedReasonErrorKey()); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 843 | return Reload; |
| 844 | } |
| 845 | |
| 846 | // For resources that are not yet loaded we ignore the cache policy. |
| 847 | if (existingResource->isLoading()) |
| 848 | return Use; |
| 849 | |
antti@apple.com | 6e31387 | 2015-05-13 23:09:13 +0000 | [diff] [blame] | 850 | auto revalidationDecision = existingResource->makeRevalidationDecision(cachePolicy(type)); |
| 851 | logResourceRevalidationDecision(revalidationDecision, frame()); |
| 852 | |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 853 | // Check if the cache headers requires us to revalidate (cache expiration for example). |
antti@apple.com | 6e31387 | 2015-05-13 23:09:13 +0000 | [diff] [blame] | 854 | if (revalidationDecision != CachedResource::RevalidationDecision::No) { |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 855 | // See if the resource has usable ETag or Last-modified headers. |
| 856 | if (existingResource->canUseCacheValidator()) |
| 857 | return Revalidate; |
| 858 | |
| 859 | // No, must reload. |
cdumez@apple.com | 06defb3 | 2014-12-19 19:15:36 +0000 | [diff] [blame] | 860 | LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to missing cache validators."); |
cdumez@apple.com | 4c621c1 | 2015-01-22 22:19:46 +0000 | [diff] [blame] | 861 | logMemoryCacheResourceRequest(frame(), DiagnosticLoggingKeys::inMemoryCacheKey(), DiagnosticLoggingKeys::unusedReasonMustRevalidateNoValidatorKey()); |
antti@apple.com | f73108e | 2010-12-30 20:57:02 +0000 | [diff] [blame] | 862 | return Reload; |
| 863 | } |
| 864 | |
| 865 | return Use; |
| 866 | } |
hyatt | c440449 | 2006-10-19 09:21:54 +0000 | [diff] [blame] | 867 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 868 | void CachedResourceLoader::printAccessDeniedMessage(const URL& url) const |
abarth@webkit.org | 234c23a | 2008-06-21 20:33:53 +0000 | [diff] [blame] | 869 | { |
| 870 | if (url.isNull()) |
| 871 | return; |
| 872 | |
eric@webkit.org | e0a5162 | 2008-06-26 22:45:47 +0000 | [diff] [blame] | 873 | if (!frame()) |
abarth@webkit.org | 234c23a | 2008-06-21 20:33:53 +0000 | [diff] [blame] | 874 | return; |
| 875 | |
zimmermann@webkit.org | b5e11e6 | 2011-05-16 07:56:06 +0000 | [diff] [blame] | 876 | String message; |
japhet@chromium.org | 97fe905 | 2012-10-09 21:58:25 +0000 | [diff] [blame] | 877 | if (!m_document || m_document->url().isNull()) |
mkwst@chromium.org | 6f6d9bc | 2013-05-30 12:08:58 +0000 | [diff] [blame] | 878 | message = "Unsafe attempt to load URL " + url.stringCenterEllipsizedToLength() + '.'; |
zimmermann@webkit.org | b5e11e6 | 2011-05-16 07:56:06 +0000 | [diff] [blame] | 879 | else |
mkwst@chromium.org | 6f6d9bc | 2013-05-30 12:08:58 +0000 | [diff] [blame] | 880 | message = "Unsafe attempt to load URL " + url.stringCenterEllipsizedToLength() + " from frame with URL " + m_document->url().stringCenterEllipsizedToLength() + ". Domains, protocols and ports must match.\n"; |
abarth@webkit.org | 234c23a | 2008-06-21 20:33:53 +0000 | [diff] [blame] | 881 | |
joepeck@webkit.org | 1f45df9 | 2014-02-06 23:45:53 +0000 | [diff] [blame] | 882 | frame()->document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, message); |
abarth@webkit.org | 234c23a | 2008-06-21 20:33:53 +0000 | [diff] [blame] | 883 | } |
| 884 | |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 885 | void CachedResourceLoader::setAutoLoadImages(bool enable) |
hyatt | c440449 | 2006-10-19 09:21:54 +0000 | [diff] [blame] | 886 | { |
| 887 | if (enable == m_autoLoadImages) |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 888 | return; |
| 889 | |
hyatt | c440449 | 2006-10-19 09:21:54 +0000 | [diff] [blame] | 890 | m_autoLoadImages = enable; |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 891 | |
hyatt | c440449 | 2006-10-19 09:21:54 +0000 | [diff] [blame] | 892 | if (!m_autoLoadImages) |
eseidel | 2770796 | 2006-03-20 23:25:37 +0000 | [diff] [blame] | 893 | return; |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 894 | |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 895 | reloadImagesIfNotDeferred(); |
| 896 | } |
| 897 | |
| 898 | void CachedResourceLoader::setImagesEnabled(bool enable) |
| 899 | { |
| 900 | if (enable == m_imagesEnabled) |
| 901 | return; |
| 902 | |
| 903 | m_imagesEnabled = enable; |
| 904 | |
| 905 | if (!m_imagesEnabled) |
| 906 | return; |
| 907 | |
| 908 | reloadImagesIfNotDeferred(); |
| 909 | } |
| 910 | |
darin@apple.com | c50e047 | 2014-04-21 18:08:49 +0000 | [diff] [blame] | 911 | bool CachedResourceLoader::clientDefersImage(const URL&) const |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 912 | { |
darin@apple.com | c50e047 | 2014-04-21 18:08:49 +0000 | [diff] [blame] | 913 | return !m_imagesEnabled; |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 914 | } |
| 915 | |
commit-queue@webkit.org | 12f31ee | 2015-02-04 20:55:21 +0000 | [diff] [blame] | 916 | bool CachedResourceLoader::shouldPerformImageLoad(const URL& url) const |
| 917 | { |
| 918 | return m_autoLoadImages || url.protocolIsData(); |
| 919 | } |
| 920 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 921 | bool CachedResourceLoader::shouldDeferImageLoad(const URL& url) const |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 922 | { |
commit-queue@webkit.org | 12f31ee | 2015-02-04 20:55:21 +0000 | [diff] [blame] | 923 | return clientDefersImage(url) || !shouldPerformImageLoad(url); |
commit-queue@webkit.org | 67248b2 | 2012-09-25 06:51:39 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | void CachedResourceLoader::reloadImagesIfNotDeferred() |
| 927 | { |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 928 | for (auto& resource : m_documentResources.values()) { |
cdumez@apple.com | c4a3af6 | 2014-10-06 00:31:05 +0000 | [diff] [blame] | 929 | if (is<CachedImage>(*resource) && resource->stillNeedsLoad() && !clientDefersImage(resource->url())) |
cdumez@apple.com | 0bb03ec | 2015-01-28 05:53:53 +0000 | [diff] [blame] | 930 | downcast<CachedImage>(*resource).load(*this, defaultCachedResourceOptions()); |
mjs | dd13ab4 | 2006-05-09 07:23:24 +0000 | [diff] [blame] | 931 | } |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 932 | } |
| 933 | |
japhet@chromium.org | bb0c487 | 2013-02-13 02:42:02 +0000 | [diff] [blame] | 934 | CachePolicy CachedResourceLoader::cachePolicy(CachedResource::Type type) const |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 935 | { |
darin@apple.com | 0fdefc3 | 2016-03-18 07:56:20 +0000 | [diff] [blame] | 936 | Frame* frame = this->frame(); |
| 937 | if (!frame) |
japhet@chromium.org | bb0c487 | 2013-02-13 02:42:02 +0000 | [diff] [blame] | 938 | return CachePolicyVerify; |
| 939 | |
| 940 | if (type != CachedResource::MainResource) |
darin@apple.com | 0fdefc3 | 2016-03-18 07:56:20 +0000 | [diff] [blame] | 941 | return frame->loader().subresourceCachePolicy(); |
darin@apple.com | 30c194a | 2016-03-20 20:08:59 +0000 | [diff] [blame] | 942 | |
| 943 | if (Page* page = frame->page()) { |
| 944 | if (page->isResourceCachingDisabled()) |
| 945 | return CachePolicyReload; |
| 946 | } |
| 947 | |
darin@apple.com | 0fdefc3 | 2016-03-18 07:56:20 +0000 | [diff] [blame] | 948 | switch (frame->loader().loadType()) { |
cdumez@apple.com | 74ef985 | 2015-01-07 01:59:10 +0000 | [diff] [blame] | 949 | case FrameLoadType::ReloadFromOrigin: |
| 950 | case FrameLoadType::Reload: |
japhet@chromium.org | bb0c487 | 2013-02-13 02:42:02 +0000 | [diff] [blame] | 951 | return CachePolicyReload; |
cdumez@apple.com | 74ef985 | 2015-01-07 01:59:10 +0000 | [diff] [blame] | 952 | case FrameLoadType::Back: |
| 953 | case FrameLoadType::Forward: |
| 954 | case FrameLoadType::IndexedBackForward: |
| 955 | // Do not revalidate cached main resource on back/forward navigation. |
| 956 | return CachePolicyHistoryBuffer; |
| 957 | default: |
| 958 | return CachePolicyVerify; |
| 959 | } |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 960 | } |
| 961 | |
cdumez@apple.com | a2f2a5a | 2015-01-31 00:04:36 +0000 | [diff] [blame] | 962 | void CachedResourceLoader::removeCachedResource(CachedResource& resource) |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 963 | { |
ap@webkit.org | 50189dd | 2009-04-27 16:57:13 +0000 | [diff] [blame] | 964 | #ifndef NDEBUG |
cdumez@apple.com | a2f2a5a | 2015-01-31 00:04:36 +0000 | [diff] [blame] | 965 | DocumentResourceMap::iterator it = m_documentResources.find(resource.url()); |
ap@webkit.org | 50189dd | 2009-04-27 16:57:13 +0000 | [diff] [blame] | 966 | if (it != m_documentResources.end()) |
cdumez@apple.com | a2f2a5a | 2015-01-31 00:04:36 +0000 | [diff] [blame] | 967 | ASSERT(it->value.get() == &resource); |
ap@webkit.org | 50189dd | 2009-04-27 16:57:13 +0000 | [diff] [blame] | 968 | #endif |
cdumez@apple.com | a2f2a5a | 2015-01-31 00:04:36 +0000 | [diff] [blame] | 969 | m_documentResources.remove(resource.url()); |
eseidel | 94980f2 | 2006-01-09 09:29:48 +0000 | [diff] [blame] | 970 | } |
| 971 | |
dbates@webkit.org | 46a1c6f | 2013-12-19 18:10:07 +0000 | [diff] [blame] | 972 | void CachedResourceLoader::loadDone(CachedResource* resource, bool shouldPerformPostLoadActions) |
japhet@chromium.org | f8d4369 | 2010-12-10 19:26:15 +0000 | [diff] [blame] | 973 | { |
japhet@chromium.org | 97fe905 | 2012-10-09 21:58:25 +0000 | [diff] [blame] | 974 | RefPtr<DocumentLoader> protectDocumentLoader(m_documentLoader); |
| 975 | RefPtr<Document> protectDocument(m_document); |
simonjam@chromium.org | 76cfc20 | 2012-12-03 22:20:47 +0000 | [diff] [blame] | 976 | |
yoav@yoav.ws | 30bf4a4 | 2016-05-02 16:21:33 +0000 | [diff] [blame] | 977 | #if ENABLE(WEB_TIMING) |
commit-queue@webkit.org | fab6e0d | 2016-06-01 20:15:23 +0000 | [diff] [blame] | 978 | if (document() && RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled()) |
| 979 | m_resourceTimingInfo.addResourceTiming(resource, *document()); |
ossy@webkit.org | c110034 | 2016-05-10 20:42:33 +0000 | [diff] [blame] | 980 | #else |
| 981 | UNUSED_PARAM(resource); |
yoav@yoav.ws | 30bf4a4 | 2016-05-02 16:21:33 +0000 | [diff] [blame] | 982 | #endif |
simonjam@chromium.org | 76cfc20 | 2012-12-03 22:20:47 +0000 | [diff] [blame] | 983 | |
japhet@chromium.org | f8d4369 | 2010-12-10 19:26:15 +0000 | [diff] [blame] | 984 | if (frame()) |
andersca@apple.com | df550b9 | 2013-08-15 22:17:17 +0000 | [diff] [blame] | 985 | frame()->loader().loadDone(); |
dbates@webkit.org | 46a1c6f | 2013-12-19 18:10:07 +0000 | [diff] [blame] | 986 | |
| 987 | if (shouldPerformPostLoadActions) |
| 988 | performPostLoadActions(); |
commit-queue@webkit.org | 9acd5fe | 2011-08-01 21:22:08 +0000 | [diff] [blame] | 989 | |
| 990 | if (!m_garbageCollectDocumentResourcesTimer.isActive()) |
| 991 | m_garbageCollectDocumentResourcesTimer.startOneShot(0); |
| 992 | } |
| 993 | |
| 994 | // Garbage collecting m_documentResources is a workaround for the |
| 995 | // CachedResourceHandles on the RHS being strong references. Ideally this |
| 996 | // would be a weak map, however CachedResourceHandles perform additional |
| 997 | // bookkeeping on CachedResources, so instead pseudo-GC them -- when the |
| 998 | // reference count reaches 1, m_documentResources is the only reference, so |
| 999 | // remove it from the map. |
vsevik@chromium.org | 58d6bc3 | 2012-07-30 15:58:48 +0000 | [diff] [blame] | 1000 | void CachedResourceLoader::garbageCollectDocumentResources() |
| 1001 | { |
commit-queue@webkit.org | 9acd5fe | 2011-08-01 21:22:08 +0000 | [diff] [blame] | 1002 | typedef Vector<String, 10> StringVector; |
| 1003 | StringVector resourcesToDelete; |
| 1004 | |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1005 | for (auto& resource : m_documentResources) { |
| 1006 | if (resource.value->hasOneHandle()) { |
| 1007 | resourcesToDelete.append(resource.key); |
| 1008 | resource.value->setOwningCachedResourceLoader(nullptr); |
commit-queue@webkit.org | 9acd5fe | 2011-08-01 21:22:08 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1012 | for (auto& resource : resourcesToDelete) |
| 1013 | m_documentResources.remove(resource); |
bweinstein@apple.com | 3ce3939 | 2011-01-26 19:07:15 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | void CachedResourceLoader::performPostLoadActions() |
| 1017 | { |
japhet@chromium.org | f8d4369 | 2010-12-10 19:26:15 +0000 | [diff] [blame] | 1018 | checkForPendingPreloads(); |
beidson@apple.com | 3378556 | 2012-10-25 17:16:38 +0000 | [diff] [blame] | 1019 | |
antti@apple.com | c86c65b | 2015-12-03 12:51:17 +0000 | [diff] [blame] | 1020 | platformStrategies()->loaderStrategy()->servePendingRequests(); |
japhet@chromium.org | f8d4369 | 2010-12-10 19:26:15 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
youenn.fablet@crf.canon.fr | 55144c5 | 2016-06-02 08:45:14 +0000 | [diff] [blame] | 1023 | void CachedResourceLoader::incrementRequestCount(const CachedResource& resource) |
beidson | 22febd1 | 2007-05-04 04:35:28 +0000 | [diff] [blame] | 1024 | { |
youenn.fablet@crf.canon.fr | 55144c5 | 2016-06-02 08:45:14 +0000 | [diff] [blame] | 1025 | if (resource.ignoreForRequestCount()) |
commit-queue@webkit.org | a67f42c | 2010-07-10 02:41:10 +0000 | [diff] [blame] | 1026 | return; |
| 1027 | |
beidson | 22febd1 | 2007-05-04 04:35:28 +0000 | [diff] [blame] | 1028 | ++m_requestCount; |
| 1029 | } |
| 1030 | |
youenn.fablet@crf.canon.fr | 55144c5 | 2016-06-02 08:45:14 +0000 | [diff] [blame] | 1031 | void CachedResourceLoader::decrementRequestCount(const CachedResource& resource) |
beidson | 22febd1 | 2007-05-04 04:35:28 +0000 | [diff] [blame] | 1032 | { |
youenn.fablet@crf.canon.fr | 55144c5 | 2016-06-02 08:45:14 +0000 | [diff] [blame] | 1033 | if (resource.ignoreForRequestCount()) |
commit-queue@webkit.org | a67f42c | 2010-07-10 02:41:10 +0000 | [diff] [blame] | 1034 | return; |
| 1035 | |
beidson | 22febd1 | 2007-05-04 04:35:28 +0000 | [diff] [blame] | 1036 | --m_requestCount; |
| 1037 | ASSERT(m_requestCount > -1); |
| 1038 | } |
commit-queue@webkit.org | 864dbda | 2012-11-16 12:21:57 +0000 | [diff] [blame] | 1039 | |
abarth@webkit.org | 1f4721c | 2013-01-02 23:06:14 +0000 | [diff] [blame] | 1040 | void CachedResourceLoader::preload(CachedResource::Type type, CachedResourceRequest& request, const String& charset) |
antti@apple.com | a05e8b0 | 2008-08-15 22:58:06 +0000 | [diff] [blame] | 1041 | { |
dbates@webkit.org | 46a1c6f | 2013-12-19 18:10:07 +0000 | [diff] [blame] | 1042 | // We always preload resources on iOS. See <https://bugs.webkit.org/show_bug.cgi?id=91276>. |
| 1043 | // FIXME: We should consider adding a setting to toggle aggressive preloading behavior as opposed |
| 1044 | // to making this behavior specific to iOS. |
| 1045 | #if !PLATFORM(IOS) |
cdumez@apple.com | f978083 | 2015-01-27 00:26:21 +0000 | [diff] [blame] | 1046 | bool hasRendering = m_document->bodyOrFrameset() && m_document->renderView(); |
dbates@webkit.org | 46a1c6f | 2013-12-19 18:10:07 +0000 | [diff] [blame] | 1047 | bool canBlockParser = type == CachedResource::Script || type == CachedResource::CSSStyleSheet; |
| 1048 | if (!hasRendering && !canBlockParser) { |
| 1049 | // Don't preload subresources that can't block the parser before we have something to draw. |
| 1050 | // This helps prevent preloads from delaying first display when bandwidth is limited. |
| 1051 | PendingPreload pendingPreload = { type, request, charset }; |
| 1052 | m_pendingPreloads.append(pendingPreload); |
| 1053 | return; |
antti@apple.com | a05e8b0 | 2008-08-15 22:58:06 +0000 | [diff] [blame] | 1054 | } |
dbates@webkit.org | 46a1c6f | 2013-12-19 18:10:07 +0000 | [diff] [blame] | 1055 | #endif |
simonjam@chromium.org | 5f12716 | 2012-11-30 19:17:45 +0000 | [diff] [blame] | 1056 | requestPreload(type, request, charset); |
antti@apple.com | a05e8b0 | 2008-08-15 22:58:06 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 1059 | void CachedResourceLoader::checkForPendingPreloads() |
antti@apple.com | a05e8b0 | 2008-08-15 22:58:06 +0000 | [diff] [blame] | 1060 | { |
cdumez@apple.com | 15e3a34 | 2015-01-27 07:05:08 +0000 | [diff] [blame] | 1061 | if (m_pendingPreloads.isEmpty()) |
| 1062 | return; |
| 1063 | auto* body = m_document->bodyOrFrameset(); |
| 1064 | if (!body || !body->renderer()) |
antti@apple.com | a05e8b0 | 2008-08-15 22:58:06 +0000 | [diff] [blame] | 1065 | return; |
dbates@webkit.org | 46a1c6f | 2013-12-19 18:10:07 +0000 | [diff] [blame] | 1066 | #if PLATFORM(IOS) |
| 1067 | // We always preload resources on iOS. See <https://bugs.webkit.org/show_bug.cgi?id=91276>. |
| 1068 | // So, we should never have any pending preloads. |
| 1069 | // FIXME: We should look to avoid compiling this code entirely when building for iOS. |
| 1070 | ASSERT_NOT_REACHED(); |
| 1071 | #endif |
bweinstein@apple.com | 3ce3939 | 2011-01-26 19:07:15 +0000 | [diff] [blame] | 1072 | while (!m_pendingPreloads.isEmpty()) { |
| 1073 | PendingPreload preload = m_pendingPreloads.takeFirst(); |
ap@webkit.org | 86b1098 | 2009-05-18 15:30:10 +0000 | [diff] [blame] | 1074 | // Don't request preload if the resource already loaded normally (this will result in double load if the page is being reloaded with cached results ignored). |
simonjam@chromium.org | 5f12716 | 2012-11-30 19:17:45 +0000 | [diff] [blame] | 1075 | if (!cachedResource(preload.m_request.resourceRequest().url())) |
japhet@chromium.org | 7e4338d | 2011-05-25 00:24:38 +0000 | [diff] [blame] | 1076 | requestPreload(preload.m_type, preload.m_request, preload.m_charset); |
antti@apple.com | a05e8b0 | 2008-08-15 22:58:06 +0000 | [diff] [blame] | 1077 | } |
| 1078 | m_pendingPreloads.clear(); |
| 1079 | } |
| 1080 | |
simonjam@chromium.org | 5f12716 | 2012-11-30 19:17:45 +0000 | [diff] [blame] | 1081 | void CachedResourceLoader::requestPreload(CachedResource::Type type, CachedResourceRequest& request, const String& charset) |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1082 | { |
antti@apple.com | 5fcdfd2 | 2008-04-16 21:41:12 +0000 | [diff] [blame] | 1083 | String encoding; |
| 1084 | if (type == CachedResource::Script || type == CachedResource::CSSStyleSheet) |
paroga@webkit.org | 927c50bd | 2011-02-05 20:34:19 +0000 | [diff] [blame] | 1085 | encoding = charset.isEmpty() ? m_document->charset() : charset; |
antti@apple.com | 5fcdfd2 | 2008-04-16 21:41:12 +0000 | [diff] [blame] | 1086 | |
simonjam@chromium.org | 5f12716 | 2012-11-30 19:17:45 +0000 | [diff] [blame] | 1087 | request.setCharset(encoding); |
| 1088 | request.setForPreload(true); |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 1089 | |
simonjam@chromium.org | 5f12716 | 2012-11-30 19:17:45 +0000 | [diff] [blame] | 1090 | CachedResourceHandle<CachedResource> resource = requestResource(type, request); |
ap@apple.com | 6edc42b | 2012-05-11 02:31:26 +0000 | [diff] [blame] | 1091 | if (!resource || (m_preloads && m_preloads->contains(resource.get()))) |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1092 | return; |
| 1093 | resource->increasePreloadCount(); |
andersca@apple.com | 9922609 | 2010-05-18 19:25:01 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (!m_preloads) |
gyuyoung.kim@samsung.com | 56cf34c | 2014-11-17 06:46:24 +0000 | [diff] [blame] | 1096 | m_preloads = std::make_unique<ListHashSet<CachedResource*>>(); |
ap@apple.com | 6edc42b | 2012-05-11 02:31:26 +0000 | [diff] [blame] | 1097 | m_preloads->add(resource.get()); |
andersca@apple.com | 9922609 | 2010-05-18 19:25:01 +0000 | [diff] [blame] | 1098 | |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1099 | #if PRELOAD_DEBUG |
| 1100 | printf("PRELOADING %s\n", resource->url().latin1().data()); |
| 1101 | #endif |
| 1102 | } |
beidson | 22febd1 | 2007-05-04 04:35:28 +0000 | [diff] [blame] | 1103 | |
japhet@chromium.org | 94a630a | 2011-06-23 00:28:56 +0000 | [diff] [blame] | 1104 | bool CachedResourceLoader::isPreloaded(const String& urlString) const |
| 1105 | { |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 1106 | const URL& url = m_document->completeURL(urlString); |
japhet@chromium.org | 94a630a | 2011-06-23 00:28:56 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (m_preloads) { |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1109 | for (auto& resource : *m_preloads) { |
japhet@chromium.org | 94a630a | 2011-06-23 00:28:56 +0000 | [diff] [blame] | 1110 | if (resource->url() == url) |
| 1111 | return true; |
| 1112 | } |
| 1113 | } |
| 1114 | |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1115 | for (auto& pendingPreload : m_pendingPreloads) { |
simonjam@chromium.org | 5f12716 | 2012-11-30 19:17:45 +0000 | [diff] [blame] | 1116 | if (pendingPreload.m_request.resourceRequest().url() == url) |
japhet@chromium.org | 94a630a | 2011-06-23 00:28:56 +0000 | [diff] [blame] | 1117 | return true; |
| 1118 | } |
| 1119 | return false; |
| 1120 | } |
| 1121 | |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 1122 | void CachedResourceLoader::clearPreloads() |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1123 | { |
| 1124 | #if PRELOAD_DEBUG |
| 1125 | printPreloadStats(); |
| 1126 | #endif |
andersca@apple.com | 9922609 | 2010-05-18 19:25:01 +0000 | [diff] [blame] | 1127 | if (!m_preloads) |
| 1128 | return; |
| 1129 | |
cdumez@apple.com | a2f2a5a | 2015-01-31 00:04:36 +0000 | [diff] [blame] | 1130 | for (auto* resource : *m_preloads) { |
| 1131 | resource->decreasePreloadCount(); |
| 1132 | bool deleted = resource->deleteIfPossible(); |
| 1133 | if (!deleted && resource->preloadResult() == CachedResource::PreloadNotReferenced) |
cdumez@apple.com | 41d2d70 | 2015-02-02 20:50:16 +0000 | [diff] [blame] | 1134 | MemoryCache::singleton().remove(*resource); |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1135 | } |
gyuyoung.kim@samsung.com | 56cf34c | 2014-11-17 06:46:24 +0000 | [diff] [blame] | 1136 | m_preloads = nullptr; |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 1139 | void CachedResourceLoader::clearPendingPreloads() |
treat@webkit.org | 915ef89 | 2009-02-26 20:09:50 +0000 | [diff] [blame] | 1140 | { |
| 1141 | m_pendingPreloads.clear(); |
| 1142 | } |
| 1143 | |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1144 | #if PRELOAD_DEBUG |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 1145 | void CachedResourceLoader::printPreloadStats() |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1146 | { |
| 1147 | unsigned scripts = 0; |
| 1148 | unsigned scriptMisses = 0; |
| 1149 | unsigned stylesheets = 0; |
| 1150 | unsigned stylesheetMisses = 0; |
| 1151 | unsigned images = 0; |
| 1152 | unsigned imageMisses = 0; |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1153 | for (auto& resource : m_preloads) { |
| 1154 | if (resource->preloadResult() == CachedResource::PreloadNotReferenced) |
| 1155 | printf("!! UNREFERENCED PRELOAD %s\n", resource->url().latin1().data()); |
| 1156 | else if (resource->preloadResult() == CachedResource::PreloadReferencedWhileComplete) |
| 1157 | printf("HIT COMPLETE PRELOAD %s\n", resource->url().latin1().data()); |
| 1158 | else if (resource->preloadResult() == CachedResource::PreloadReferencedWhileLoading) |
| 1159 | printf("HIT LOADING PRELOAD %s\n", resource->url().latin1().data()); |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1160 | |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1161 | if (resource->type() == CachedResource::Script) { |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1162 | scripts++; |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1163 | if (resource->preloadResult() < CachedResource::PreloadReferencedWhileLoading) |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1164 | scriptMisses++; |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1165 | } else if (resource->type() == CachedResource::CSSStyleSheet) { |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1166 | stylesheets++; |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1167 | if (resource->preloadResult() < CachedResource::PreloadReferencedWhileLoading) |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1168 | stylesheetMisses++; |
| 1169 | } else { |
| 1170 | images++; |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1171 | if (resource->preloadResult() < CachedResource::PreloadReferencedWhileLoading) |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1172 | imageMisses++; |
| 1173 | } |
| 1174 | |
jer.noble@apple.com | 2ed319d | 2016-01-28 19:52:57 +0000 | [diff] [blame] | 1175 | if (resource->errorOccurred() && resource->preloadResult() == CachedResource::PreloadNotReferenced) |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1176 | MemoryCache::singleton().remove(resource); |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1177 | |
commit-queue@webkit.org | acddfad | 2015-09-30 05:53:51 +0000 | [diff] [blame] | 1178 | resource->decreasePreloadCount(); |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1179 | } |
gyuyoung.kim@samsung.com | 56cf34c | 2014-11-17 06:46:24 +0000 | [diff] [blame] | 1180 | m_preloads = nullptr; |
antti@apple.com | 6c76e54 | 2008-03-13 21:20:31 +0000 | [diff] [blame] | 1181 | |
| 1182 | if (scripts) |
| 1183 | printf("SCRIPTS: %d (%d hits, hit rate %d%%)\n", scripts, scripts - scriptMisses, (scripts - scriptMisses) * 100 / scripts); |
| 1184 | if (stylesheets) |
| 1185 | printf("STYLESHEETS: %d (%d hits, hit rate %d%%)\n", stylesheets, stylesheets - stylesheetMisses, (stylesheets - stylesheetMisses) * 100 / stylesheets); |
| 1186 | if (images) |
| 1187 | printf("IMAGES: %d (%d hits, hit rate %d%%)\n", images, images - imageMisses, (images - imageMisses) * 100 / images); |
| 1188 | } |
| 1189 | #endif |
loislo@chromium.org | 7b6f5d9 | 2012-08-06 09:12:01 +0000 | [diff] [blame] | 1190 | |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 1191 | const ResourceLoaderOptions& CachedResourceLoader::defaultCachedResourceOptions() |
| 1192 | { |
commit-queue@webkit.org | 16c43b4 | 2016-07-04 18:36:30 +0000 | [diff] [blame^] | 1193 | static ResourceLoaderOptions options(SendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, ClientRequestedCredentials, DoSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::AllowCaching); |
commit-queue@webkit.org | 5e7ea1b | 2012-10-22 23:35:28 +0000 | [diff] [blame] | 1194 | return options; |
| 1195 | } |
| 1196 | |
darin | 644b75e | 2006-02-21 06:59:15 +0000 | [diff] [blame] | 1197 | } |