cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION) |
cdumez@apple.com | bc42b74 | 2015-11-10 19:20:33 +0000 | [diff] [blame] | 29 | #include "NetworkCacheSpeculativeLoadManager.h" |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 30 | |
| 31 | #include "Logging.h" |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 32 | #include "NetworkCacheEntry.h" |
| 33 | #include "NetworkCacheSpeculativeLoad.h" |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 34 | #include "NetworkCacheSubresourcesEntry.h" |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 35 | #include "NetworkProcess.h" |
| 36 | #include <WebCore/DiagnosticLoggingKeys.h> |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 37 | #include <WebCore/HysteresisActivity.h> |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 38 | #include <wtf/HashCountedSet.h> |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 39 | #include <wtf/NeverDestroyed.h> |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 40 | #include <wtf/RefCounted.h> |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 41 | #include <wtf/RunLoop.h> |
| 42 | |
| 43 | namespace WebKit { |
| 44 | |
| 45 | namespace NetworkCache { |
| 46 | |
| 47 | using namespace WebCore; |
| 48 | |
beidson@apple.com | c9860ae | 2016-05-22 20:09:57 +0000 | [diff] [blame] | 49 | static const auto preloadedEntryLifetime = 10s; |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 50 | |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 51 | #if !LOG_DISABLED |
| 52 | static HashCountedSet<String>& allSpeculativeLoadingDiagnosticMessages() |
| 53 | { |
| 54 | static NeverDestroyed<HashCountedSet<String>> messages; |
| 55 | return messages; |
| 56 | } |
| 57 | |
| 58 | static void printSpeculativeLoadingDiagnosticMessageCounts() |
| 59 | { |
| 60 | LOG(NetworkCacheSpeculativePreloading, "-- Speculative loading statistics --"); |
| 61 | for (auto& pair : allSpeculativeLoadingDiagnosticMessages()) |
| 62 | LOG(NetworkCacheSpeculativePreloading, "%s: %u", pair.key.utf8().data(), pair.value); |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | static void logSpeculativeLoadingDiagnosticMessage(const GlobalFrameID& frameID, const String& message) |
| 67 | { |
| 68 | #if !LOG_DISABLED |
| 69 | if (WebKit2LogNetworkCacheSpeculativePreloading.state == WTFLogChannelOn) |
| 70 | allSpeculativeLoadingDiagnosticMessages().add(message); |
| 71 | #endif |
| 72 | NetworkProcess::singleton().logDiagnosticMessage(frameID.first, WebCore::DiagnosticLoggingKeys::networkCacheKey(), message, WebCore::ShouldSample::Yes); |
| 73 | } |
| 74 | |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 75 | static const AtomicString& subresourcesType() |
| 76 | { |
| 77 | ASSERT(RunLoop::isMain()); |
cdumez@apple.com | 271388c | 2016-06-16 23:39:29 +0000 | [diff] [blame] | 78 | static NeverDestroyed<const AtomicString> resource("SubResources", AtomicString::ConstructFromLiteral); |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 79 | return resource; |
| 80 | } |
| 81 | |
antti@apple.com | 91466fb | 2016-11-20 00:38:09 +0000 | [diff] [blame] | 82 | static inline Key makeSubresourcesKey(const Key& resourceKey, const Salt& salt) |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 83 | { |
antti@apple.com | 91466fb | 2016-11-20 00:38:09 +0000 | [diff] [blame] | 84 | return Key(resourceKey.partition(), subresourcesType(), resourceKey.range(), resourceKey.identifier(), salt); |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
cdumez@apple.com | 8f1cbe0 | 2016-05-25 10:59:06 +0000 | [diff] [blame] | 87 | static inline ResourceRequest constructRevalidationRequest(const Entry& entry, const SubresourceInfo& subResourceInfo) |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 88 | { |
cdumez@apple.com | 8f1cbe0 | 2016-05-25 10:59:06 +0000 | [diff] [blame] | 89 | ResourceRequest revalidationRequest(entry.key().identifier()); |
cdumez@apple.com | 271388c | 2016-06-16 23:39:29 +0000 | [diff] [blame] | 90 | revalidationRequest.setHTTPHeaderFields(subResourceInfo.requestHeaders()); |
| 91 | revalidationRequest.setFirstPartyForCookies(subResourceInfo.firstPartyForCookies()); |
cdumez@apple.com | 16dcb2d | 2016-03-09 21:22:36 +0000 | [diff] [blame] | 92 | #if ENABLE(CACHE_PARTITIONING) |
antti@apple.com | 91466fb | 2016-11-20 00:38:09 +0000 | [diff] [blame] | 93 | if (!entry.key().partition().isEmpty()) |
cdumez@apple.com | 8f1cbe0 | 2016-05-25 10:59:06 +0000 | [diff] [blame] | 94 | revalidationRequest.setCachePartition(entry.key().partition()); |
cdumez@apple.com | 16dcb2d | 2016-03-09 21:22:36 +0000 | [diff] [blame] | 95 | #endif |
| 96 | ASSERT_WITH_MESSAGE(entry.key().range().isEmpty(), "range is not supported"); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 97 | |
cdumez@apple.com | 8f1cbe0 | 2016-05-25 10:59:06 +0000 | [diff] [blame] | 98 | revalidationRequest.makeUnconditional(); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 99 | String eTag = entry.response().httpHeaderField(HTTPHeaderName::ETag); |
| 100 | if (!eTag.isEmpty()) |
cdumez@apple.com | 8f1cbe0 | 2016-05-25 10:59:06 +0000 | [diff] [blame] | 101 | revalidationRequest.setHTTPHeaderField(HTTPHeaderName::IfNoneMatch, eTag); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 102 | |
| 103 | String lastModified = entry.response().httpHeaderField(HTTPHeaderName::LastModified); |
| 104 | if (!lastModified.isEmpty()) |
cdumez@apple.com | 8f1cbe0 | 2016-05-25 10:59:06 +0000 | [diff] [blame] | 105 | revalidationRequest.setHTTPHeaderField(HTTPHeaderName::IfModifiedSince, lastModified); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 106 | |
| 107 | return revalidationRequest; |
| 108 | } |
| 109 | |
| 110 | static bool responseNeedsRevalidation(const ResourceResponse& response, std::chrono::system_clock::time_point timestamp) |
| 111 | { |
| 112 | if (response.cacheControlContainsNoCache()) |
| 113 | return true; |
| 114 | |
| 115 | auto age = computeCurrentAge(response, timestamp); |
| 116 | auto lifetime = computeFreshnessLifetimeForHTTPFamily(response, timestamp); |
beidson@apple.com | c9860ae | 2016-05-22 20:09:57 +0000 | [diff] [blame] | 117 | return age - lifetime > 0ms; |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 118 | } |
| 119 | |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 120 | class SpeculativeLoadManager::ExpiringEntry { |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 121 | WTF_MAKE_FAST_ALLOCATED; |
| 122 | public: |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 123 | explicit ExpiringEntry(std::function<void()>&& expirationHandler) |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 124 | : m_lifetimeTimer(WTFMove(expirationHandler)) |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 125 | { |
| 126 | m_lifetimeTimer.startOneShot(preloadedEntryLifetime); |
| 127 | } |
| 128 | |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 129 | private: |
| 130 | Timer m_lifetimeTimer; |
| 131 | }; |
| 132 | |
| 133 | class SpeculativeLoadManager::PreloadedEntry : private ExpiringEntry { |
| 134 | WTF_MAKE_FAST_ALLOCATED; |
| 135 | public: |
utatane.tea@gmail.com | 4392696 | 2016-11-27 06:08:16 +0000 | [diff] [blame^] | 136 | PreloadedEntry(std::unique_ptr<Entry> entry, std::optional<ResourceRequest>&& speculativeValidationRequest, std::function<void()>&& lifetimeReachedHandler) |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 137 | : ExpiringEntry(WTFMove(lifetimeReachedHandler)) |
| 138 | , m_entry(WTFMove(entry)) |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 139 | , m_speculativeValidationRequest(WTFMove(speculativeValidationRequest)) |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 140 | { } |
| 141 | |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 142 | std::unique_ptr<Entry> takeCacheEntry() |
| 143 | { |
| 144 | ASSERT(m_entry); |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 145 | return WTFMove(m_entry); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 146 | } |
| 147 | |
utatane.tea@gmail.com | 4392696 | 2016-11-27 06:08:16 +0000 | [diff] [blame^] | 148 | const std::optional<ResourceRequest>& revalidationRequest() const { return m_speculativeValidationRequest; } |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 149 | bool wasRevalidated() const { return !!m_speculativeValidationRequest; } |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 150 | |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 151 | private: |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 152 | std::unique_ptr<Entry> m_entry; |
utatane.tea@gmail.com | 4392696 | 2016-11-27 06:08:16 +0000 | [diff] [blame^] | 153 | std::optional<ResourceRequest> m_speculativeValidationRequest; |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 156 | class SpeculativeLoadManager::PendingFrameLoad : public RefCounted<PendingFrameLoad> { |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 157 | public: |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 158 | static Ref<PendingFrameLoad> create(Storage& storage, const Key& mainResourceKey, std::function<void()>&& loadCompletionHandler) |
| 159 | { |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 160 | return adoptRef(*new PendingFrameLoad(storage, mainResourceKey, WTFMove(loadCompletionHandler))); |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | ~PendingFrameLoad() |
| 164 | { |
| 165 | ASSERT(m_didFinishLoad); |
| 166 | ASSERT(m_didRetrieveExistingEntry); |
| 167 | } |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 168 | |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 169 | void registerSubresourceLoad(const ResourceRequest& request, const Key& subresourceKey) |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 170 | { |
| 171 | ASSERT(RunLoop::isMain()); |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 172 | m_subresourceLoads.append(std::make_unique<SubresourceLoad>(request, subresourceKey)); |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 173 | m_loadHysteresisActivity.impulse(); |
| 174 | } |
| 175 | |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 176 | void markLoadAsCompleted() |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 177 | { |
| 178 | ASSERT(RunLoop::isMain()); |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 179 | if (m_didFinishLoad) |
| 180 | return; |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 181 | |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 182 | #if !LOG_DISABLED |
| 183 | printSpeculativeLoadingDiagnosticMessageCounts(); |
| 184 | #endif |
| 185 | |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 186 | m_didFinishLoad = true; |
| 187 | saveToDiskIfReady(); |
| 188 | m_loadCompletionHandler(); |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 189 | } |
| 190 | |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 191 | void setExistingSubresourcesEntry(std::unique_ptr<SubresourcesEntry> entry) |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 192 | { |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 193 | ASSERT(!m_existingEntry); |
| 194 | ASSERT(!m_didRetrieveExistingEntry); |
| 195 | |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 196 | m_existingEntry = WTFMove(entry); |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 197 | m_didRetrieveExistingEntry = true; |
| 198 | saveToDiskIfReady(); |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | private: |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 202 | PendingFrameLoad(Storage& storage, const Key& mainResourceKey, std::function<void()>&& loadCompletionHandler) |
| 203 | : m_storage(storage) |
| 204 | , m_mainResourceKey(mainResourceKey) |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 205 | , m_loadCompletionHandler(WTFMove(loadCompletionHandler)) |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 206 | , m_loadHysteresisActivity([this](HysteresisState state) { if (state == HysteresisState::Stopped) markLoadAsCompleted(); }) |
cdumez@apple.com | e2f3958 | 2016-10-17 23:27:36 +0000 | [diff] [blame] | 207 | { |
| 208 | m_loadHysteresisActivity.impulse(); |
| 209 | } |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 210 | |
| 211 | void saveToDiskIfReady() |
| 212 | { |
| 213 | if (!m_didFinishLoad || !m_didRetrieveExistingEntry) |
| 214 | return; |
| 215 | |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 216 | if (m_subresourceLoads.isEmpty()) |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 217 | return; |
| 218 | |
| 219 | #if !LOG_DISABLED |
| 220 | LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Saving to disk list of subresources for '%s':", m_mainResourceKey.identifier().utf8().data()); |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 221 | for (auto& subresourceLoad : m_subresourceLoads) |
| 222 | LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) * Subresource: '%s'.", subresourceLoad->key.identifier().utf8().data()); |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 223 | #endif |
| 224 | |
| 225 | if (m_existingEntry) { |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 226 | m_existingEntry->updateSubresourceLoads(m_subresourceLoads); |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 227 | m_storage.store(m_existingEntry->encodeAsStorageRecord(), [](const Data&) { }); |
| 228 | } else { |
antti@apple.com | 91466fb | 2016-11-20 00:38:09 +0000 | [diff] [blame] | 229 | SubresourcesEntry entry(makeSubresourcesKey(m_mainResourceKey, m_storage.salt()), m_subresourceLoads); |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 230 | m_storage.store(entry.encodeAsStorageRecord(), [](const Data&) { }); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | Storage& m_storage; |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 235 | Key m_mainResourceKey; |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 236 | Vector<std::unique_ptr<SubresourceLoad>> m_subresourceLoads; |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 237 | std::function<void()> m_loadCompletionHandler; |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 238 | HysteresisActivity m_loadHysteresisActivity; |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 239 | std::unique_ptr<SubresourcesEntry> m_existingEntry; |
| 240 | bool m_didFinishLoad { false }; |
| 241 | bool m_didRetrieveExistingEntry { false }; |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 242 | }; |
| 243 | |
cdumez@apple.com | bc42b74 | 2015-11-10 19:20:33 +0000 | [diff] [blame] | 244 | SpeculativeLoadManager::SpeculativeLoadManager(Storage& storage) |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 245 | : m_storage(storage) |
| 246 | { |
| 247 | } |
| 248 | |
cdumez@apple.com | bc42b74 | 2015-11-10 19:20:33 +0000 | [diff] [blame] | 249 | SpeculativeLoadManager::~SpeculativeLoadManager() |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 250 | { |
| 251 | } |
| 252 | |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 253 | #if !LOG_DISABLED |
| 254 | |
| 255 | static void dumpHTTPHeadersDiff(const HTTPHeaderMap& headersA, const HTTPHeaderMap& headersB) |
| 256 | { |
| 257 | auto aEnd = headersA.end(); |
| 258 | for (auto it = headersA.begin(); it != aEnd; ++it) { |
| 259 | String valueB = headersB.get(it->key); |
| 260 | if (valueB.isNull()) |
| 261 | LOG(NetworkCacheSpeculativePreloading, "* '%s' HTTP header is only in first request (value: %s)", it->key.utf8().data(), it->value.utf8().data()); |
| 262 | else if (it->value != valueB) |
| 263 | LOG(NetworkCacheSpeculativePreloading, "* '%s' HTTP header differs in both requests: %s != %s", it->key.utf8().data(), it->value.utf8().data(), valueB.utf8().data()); |
| 264 | } |
| 265 | auto bEnd = headersB.end(); |
| 266 | for (auto it = headersB.begin(); it != bEnd; ++it) { |
| 267 | if (!headersA.contains(it->key)) |
| 268 | LOG(NetworkCacheSpeculativePreloading, "* '%s' HTTP header is only in second request (value: %s)", it->key.utf8().data(), it->value.utf8().data()); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | #endif |
| 273 | |
cdumez@apple.com | 43ab71b | 2016-06-02 16:40:05 +0000 | [diff] [blame] | 274 | static bool requestsHeadersMatch(const ResourceRequest& speculativeValidationRequest, const ResourceRequest& actualRequest) |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 275 | { |
cdumez@apple.com | 43ab71b | 2016-06-02 16:40:05 +0000 | [diff] [blame] | 276 | ASSERT(!actualRequest.isConditional()); |
| 277 | ResourceRequest speculativeRequest = speculativeValidationRequest; |
| 278 | speculativeRequest.makeUnconditional(); |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 279 | |
cdumez@apple.com | 43ab71b | 2016-06-02 16:40:05 +0000 | [diff] [blame] | 280 | if (speculativeRequest.httpHeaderFields() != actualRequest.httpHeaderFields()) { |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 281 | LOG(NetworkCacheSpeculativePreloading, "Cannot reuse speculatively validated entry because HTTP headers used for validation do not match"); |
| 282 | #if !LOG_DISABLED |
cdumez@apple.com | 43ab71b | 2016-06-02 16:40:05 +0000 | [diff] [blame] | 283 | dumpHTTPHeadersDiff(speculativeRequest.httpHeaderFields(), actualRequest.httpHeaderFields()); |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 284 | #endif |
| 285 | return false; |
| 286 | } |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | bool SpeculativeLoadManager::canUsePreloadedEntry(const PreloadedEntry& entry, const ResourceRequest& actualRequest) |
| 291 | { |
| 292 | if (!entry.wasRevalidated()) |
| 293 | return true; |
| 294 | |
| 295 | ASSERT(entry.revalidationRequest()); |
| 296 | return requestsHeadersMatch(*entry.revalidationRequest(), actualRequest); |
| 297 | } |
| 298 | |
| 299 | bool SpeculativeLoadManager::canUsePendingPreload(const SpeculativeLoad& load, const ResourceRequest& actualRequest) |
| 300 | { |
| 301 | return requestsHeadersMatch(load.originalRequest(), actualRequest); |
| 302 | } |
| 303 | |
cdumez@apple.com | 3de719a | 2016-05-25 19:17:57 +0000 | [diff] [blame] | 304 | bool SpeculativeLoadManager::retrieve(const GlobalFrameID& frameID, const Key& storageKey, const WebCore::ResourceRequest& request, RetrieveCompletionHandler&& completionHandler) |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 305 | { |
| 306 | // Check already preloaded entries. |
| 307 | if (auto preloadedEntry = m_preloadedEntries.take(storageKey)) { |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 308 | if (!canUsePreloadedEntry(*preloadedEntry, request)) { |
| 309 | LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Retrieval: Could not use preloaded entry to satisfy request for '%s' due to HTTP headers mismatch:", storageKey.identifier().utf8().data()); |
| 310 | logSpeculativeLoadingDiagnosticMessage(frameID, preloadedEntry->wasRevalidated() ? DiagnosticLoggingKeys::wastedSpeculativeWarmupWithRevalidationKey() : DiagnosticLoggingKeys::wastedSpeculativeWarmupWithoutRevalidationKey()); |
| 311 | return false; |
| 312 | } |
| 313 | |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 314 | LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Retrieval: Using preloaded entry to satisfy request for '%s':", storageKey.identifier().utf8().data()); |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 315 | logSpeculativeLoadingDiagnosticMessage(frameID, preloadedEntry->wasRevalidated() ? DiagnosticLoggingKeys::successfulSpeculativeWarmupWithRevalidationKey() : DiagnosticLoggingKeys::successfulSpeculativeWarmupWithoutRevalidationKey()); |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 316 | |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 317 | completionHandler(preloadedEntry->takeCacheEntry()); |
| 318 | return true; |
| 319 | } |
| 320 | |
| 321 | // Check pending speculative revalidations. |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 322 | auto* pendingPreload = m_pendingPreloads.get(storageKey); |
| 323 | if (!pendingPreload) { |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 324 | if (m_notPreloadedEntries.remove(storageKey)) |
| 325 | logSpeculativeLoadingDiagnosticMessage(frameID, DiagnosticLoggingKeys::entryWronglyNotWarmedUpKey()); |
| 326 | else |
| 327 | logSpeculativeLoadingDiagnosticMessage(frameID, DiagnosticLoggingKeys::unknownEntryRequestKey()); |
| 328 | |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 329 | return false; |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 330 | } |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 331 | |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 332 | if (!canUsePendingPreload(*pendingPreload, request)) { |
| 333 | LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Retrieval: revalidation already in progress for '%s' but unusable due to HTTP headers mismatch:", storageKey.identifier().utf8().data()); |
| 334 | logSpeculativeLoadingDiagnosticMessage(frameID, DiagnosticLoggingKeys::wastedSpeculativeWarmupWithRevalidationKey()); |
| 335 | return false; |
| 336 | } |
| 337 | |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 338 | LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Retrieval: revalidation already in progress for '%s':", storageKey.identifier().utf8().data()); |
| 339 | |
| 340 | // FIXME: This breaks incremental loading when the revalidation is not successful. |
cdumez@apple.com | 3de719a | 2016-05-25 19:17:57 +0000 | [diff] [blame] | 341 | auto addResult = m_pendingRetrieveRequests.ensure(storageKey, [] { return std::make_unique<Vector<RetrieveCompletionHandler>>(); }); |
| 342 | addResult.iterator->value->append(WTFMove(completionHandler)); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | |
| 346 | void SpeculativeLoadManager::registerLoad(const GlobalFrameID& frameID, const ResourceRequest& request, const Key& resourceKey) |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 347 | { |
| 348 | ASSERT(RunLoop::isMain()); |
cdumez@apple.com | 297a552 | 2016-04-30 03:36:48 +0000 | [diff] [blame] | 349 | ASSERT(request.url().protocolIsInHTTPFamily()); |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 350 | |
cdumez@apple.com | 297a552 | 2016-04-30 03:36:48 +0000 | [diff] [blame] | 351 | if (request.httpMethod() != "GET") |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 352 | return; |
| 353 | |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 354 | auto isMainResource = request.requester() == ResourceRequest::Requester::Main; |
| 355 | if (isMainResource) { |
| 356 | // Mark previous load in this frame as completed if necessary. |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 357 | if (auto* pendingFrameLoad = m_pendingFrameLoads.get(frameID)) |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 358 | pendingFrameLoad->markLoadAsCompleted(); |
| 359 | |
| 360 | ASSERT(!m_pendingFrameLoads.contains(frameID)); |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 361 | |
| 362 | // Start tracking loads in this frame. |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 363 | RefPtr<PendingFrameLoad> pendingFrameLoad = PendingFrameLoad::create(m_storage, resourceKey, [this, frameID] { |
| 364 | bool wasRemoved = m_pendingFrameLoads.remove(frameID); |
| 365 | ASSERT_UNUSED(wasRemoved, wasRemoved); |
| 366 | }); |
| 367 | m_pendingFrameLoads.add(frameID, pendingFrameLoad); |
| 368 | |
| 369 | // Retrieve the subresources entry if it exists to start speculative revalidation and to update it. |
| 370 | retrieveSubresourcesEntry(resourceKey, [this, frameID, pendingFrameLoad](std::unique_ptr<SubresourcesEntry> entry) { |
| 371 | if (entry) |
| 372 | startSpeculativeRevalidation(frameID, *entry); |
| 373 | |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 374 | pendingFrameLoad->setExistingSubresourcesEntry(WTFMove(entry)); |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 375 | }); |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 376 | return; |
| 377 | } |
| 378 | |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 379 | if (auto* pendingFrameLoad = m_pendingFrameLoads.get(frameID)) |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 380 | pendingFrameLoad->registerSubresourceLoad(request, resourceKey); |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 381 | } |
| 382 | |
utatane.tea@gmail.com | 4392696 | 2016-11-27 06:08:16 +0000 | [diff] [blame^] | 383 | void SpeculativeLoadManager::addPreloadedEntry(std::unique_ptr<Entry> entry, const GlobalFrameID& frameID, std::optional<ResourceRequest>&& revalidationRequest) |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 384 | { |
| 385 | ASSERT(entry); |
| 386 | ASSERT(!entry->needsValidation()); |
| 387 | auto key = entry->key(); |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 388 | m_preloadedEntries.add(key, std::make_unique<PreloadedEntry>(WTFMove(entry), WTFMove(revalidationRequest), [this, key, frameID] { |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 389 | auto preloadedEntry = m_preloadedEntries.take(key); |
| 390 | ASSERT(preloadedEntry); |
| 391 | if (preloadedEntry->wasRevalidated()) |
| 392 | logSpeculativeLoadingDiagnosticMessage(frameID, DiagnosticLoggingKeys::wastedSpeculativeWarmupWithRevalidationKey()); |
| 393 | else |
| 394 | logSpeculativeLoadingDiagnosticMessage(frameID, DiagnosticLoggingKeys::wastedSpeculativeWarmupWithoutRevalidationKey()); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 395 | })); |
| 396 | } |
| 397 | |
cdumez@apple.com | 3de719a | 2016-05-25 19:17:57 +0000 | [diff] [blame] | 398 | void SpeculativeLoadManager::retrieveEntryFromStorage(const Key& key, RetrieveCompletionHandler&& completionHandler) |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 399 | { |
cdumez@apple.com | 3de719a | 2016-05-25 19:17:57 +0000 | [diff] [blame] | 400 | m_storage.retrieve(key, static_cast<unsigned>(ResourceLoadPriority::Medium), [completionHandler = WTFMove(completionHandler)](auto record) { |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 401 | if (!record) { |
| 402 | completionHandler(nullptr); |
| 403 | return false; |
| 404 | } |
| 405 | auto entry = Entry::decodeStorageRecord(*record); |
| 406 | if (!entry) { |
| 407 | completionHandler(nullptr); |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | auto& response = entry->response(); |
| 412 | if (!response.hasCacheValidatorFields()) { |
| 413 | completionHandler(nullptr); |
| 414 | return true; |
| 415 | } |
| 416 | |
cdumez@apple.com | b4b78a5 | 2016-04-14 00:14:05 +0000 | [diff] [blame] | 417 | if (responseNeedsRevalidation(response, entry->timeStamp())) { |
| 418 | // Do not use cached redirects that have expired. |
| 419 | if (entry->redirectRequest()) { |
| 420 | completionHandler(nullptr); |
| 421 | return true; |
| 422 | } |
cdumez@apple.com | 16dcb2d | 2016-03-09 21:22:36 +0000 | [diff] [blame] | 423 | entry->setNeedsValidation(true); |
cdumez@apple.com | b4b78a5 | 2016-04-14 00:14:05 +0000 | [diff] [blame] | 424 | } |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 425 | |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 426 | completionHandler(WTFMove(entry)); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 427 | return true; |
| 428 | }); |
| 429 | } |
| 430 | |
| 431 | bool SpeculativeLoadManager::satisfyPendingRequests(const Key& key, Entry* entry) |
| 432 | { |
| 433 | auto completionHandlers = m_pendingRetrieveRequests.take(key); |
| 434 | if (!completionHandlers) |
| 435 | return false; |
| 436 | |
| 437 | for (auto& completionHandler : *completionHandlers) |
| 438 | completionHandler(entry ? std::make_unique<Entry>(*entry) : nullptr); |
| 439 | |
| 440 | return true; |
| 441 | } |
| 442 | |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 443 | void SpeculativeLoadManager::revalidateEntry(std::unique_ptr<Entry> entry, const SubresourceInfo& subresourceInfo, const GlobalFrameID& frameID) |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 444 | { |
| 445 | ASSERT(entry); |
| 446 | ASSERT(entry->needsValidation()); |
| 447 | |
| 448 | auto key = entry->key(); |
cdumez@apple.com | 16dcb2d | 2016-03-09 21:22:36 +0000 | [diff] [blame] | 449 | |
| 450 | // Range is not supported. |
| 451 | if (!key.range().isEmpty()) |
| 452 | return; |
| 453 | |
cdumez@apple.com | 8f1cbe0 | 2016-05-25 10:59:06 +0000 | [diff] [blame] | 454 | ResourceRequest revalidationRequest = constructRevalidationRequest(*entry, subresourceInfo); |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 455 | |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 456 | LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Speculatively revalidating '%s':", key.identifier().utf8().data()); |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 457 | |
cdumez@apple.com | 8f1cbe0 | 2016-05-25 10:59:06 +0000 | [diff] [blame] | 458 | auto revalidator = std::make_unique<SpeculativeLoad>(frameID, revalidationRequest, WTFMove(entry), [this, key, revalidationRequest, frameID](std::unique_ptr<Entry> revalidatedEntry) { |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 459 | ASSERT(!revalidatedEntry || !revalidatedEntry->needsValidation()); |
cdumez@apple.com | 16dcb2d | 2016-03-09 21:22:36 +0000 | [diff] [blame] | 460 | ASSERT(!revalidatedEntry || revalidatedEntry->key() == key); |
| 461 | |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 462 | auto protectRevalidator = m_pendingPreloads.take(key); |
| 463 | LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Speculative revalidation completed for '%s':", key.identifier().utf8().data()); |
| 464 | |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 465 | if (satisfyPendingRequests(key, revalidatedEntry.get())) { |
| 466 | if (revalidatedEntry) |
| 467 | logSpeculativeLoadingDiagnosticMessage(frameID, DiagnosticLoggingKeys::successfulSpeculativeWarmupWithRevalidationKey()); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 468 | return; |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 469 | } |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 470 | |
| 471 | if (revalidatedEntry) |
cdumez@apple.com | 8f1cbe0 | 2016-05-25 10:59:06 +0000 | [diff] [blame] | 472 | addPreloadedEntry(WTFMove(revalidatedEntry), frameID, revalidationRequest); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 473 | }); |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 474 | m_pendingPreloads.add(key, WTFMove(revalidator)); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 475 | } |
| 476 | |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 477 | void SpeculativeLoadManager::preloadEntry(const Key& key, const SubresourceInfo& subResourceInfo, const GlobalFrameID& frameID) |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 478 | { |
cdumez@apple.com | f99b108 | 2016-05-10 17:45:47 +0000 | [diff] [blame] | 479 | if (m_pendingPreloads.contains(key)) |
| 480 | return; |
| 481 | |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 482 | m_pendingPreloads.add(key, nullptr); |
cdumez@apple.com | 4da914e | 2016-05-25 05:18:00 +0000 | [diff] [blame] | 483 | retrieveEntryFromStorage(key, [this, key, subResourceInfo, frameID](std::unique_ptr<Entry> entry) { |
cdumez@apple.com | f99b108 | 2016-05-10 17:45:47 +0000 | [diff] [blame] | 484 | ASSERT(!m_pendingPreloads.get(key)); |
| 485 | bool removed = m_pendingPreloads.remove(key); |
| 486 | ASSERT_UNUSED(removed, removed); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 487 | |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 488 | if (satisfyPendingRequests(key, entry.get())) { |
| 489 | if (entry) |
| 490 | logSpeculativeLoadingDiagnosticMessage(frameID, DiagnosticLoggingKeys::successfulSpeculativeWarmupWithoutRevalidationKey()); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 491 | return; |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 492 | } |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 493 | |
| 494 | if (!entry) |
| 495 | return; |
| 496 | |
| 497 | if (entry->needsValidation()) |
cdumez@apple.com | 4da914e | 2016-05-25 05:18:00 +0000 | [diff] [blame] | 498 | revalidateEntry(WTFMove(entry), subResourceInfo, frameID); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 499 | else |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 500 | addPreloadedEntry(WTFMove(entry), frameID); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 501 | }); |
| 502 | } |
| 503 | |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 504 | void SpeculativeLoadManager::startSpeculativeRevalidation(const GlobalFrameID& frameID, SubresourcesEntry& entry) |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 505 | { |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 506 | for (auto& subresourcePair : entry.subresources()) { |
cdumez@apple.com | 498265c | 2016-03-28 16:04:33 +0000 | [diff] [blame] | 507 | auto& key = subresourcePair.key; |
| 508 | auto& subresourceInfo = subresourcePair.value; |
cdumez@apple.com | 271388c | 2016-06-16 23:39:29 +0000 | [diff] [blame] | 509 | if (!subresourceInfo.isTransient()) |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 510 | preloadEntry(key, subresourceInfo, frameID); |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 511 | else { |
cdumez@apple.com | afe9371 | 2016-03-10 16:57:11 +0000 | [diff] [blame] | 512 | LOG(NetworkCacheSpeculativePreloading, "(NetworkProcess) Not preloading '%s' because it is marked as transient", key.identifier().utf8().data()); |
cdumez@apple.com | 8c98773 | 2015-12-08 17:39:46 +0000 | [diff] [blame] | 513 | m_notPreloadedEntries.add(key, std::make_unique<ExpiringEntry>([this, key, frameID] { |
| 514 | logSpeculativeLoadingDiagnosticMessage(frameID, DiagnosticLoggingKeys::entryRightlyNotWarmedUpKey()); |
| 515 | m_notPreloadedEntries.remove(key); |
| 516 | })); |
| 517 | } |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 520 | |
cdumez@apple.com | 3de719a | 2016-05-25 19:17:57 +0000 | [diff] [blame] | 521 | void SpeculativeLoadManager::retrieveSubresourcesEntry(const Key& storageKey, std::function<void (std::unique_ptr<SubresourcesEntry>)>&& completionHandler) |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 522 | { |
cdumez@apple.com | 271388c | 2016-06-16 23:39:29 +0000 | [diff] [blame] | 523 | ASSERT(storageKey.type() == "Resource"); |
antti@apple.com | 91466fb | 2016-11-20 00:38:09 +0000 | [diff] [blame] | 524 | auto subresourcesStorageKey = makeSubresourcesKey(storageKey, m_storage.salt()); |
cdumez@apple.com | 3de719a | 2016-05-25 19:17:57 +0000 | [diff] [blame] | 525 | m_storage.retrieve(subresourcesStorageKey, static_cast<unsigned>(ResourceLoadPriority::Medium), [completionHandler = WTFMove(completionHandler)](auto record) { |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 526 | if (!record) { |
| 527 | completionHandler(nullptr); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 528 | return false; |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 529 | } |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 530 | |
| 531 | auto subresourcesEntry = SubresourcesEntry::decodeStorageRecord(*record); |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 532 | if (!subresourcesEntry) { |
| 533 | completionHandler(nullptr); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 534 | return false; |
cdumez@apple.com | 351ca6a | 2015-11-30 19:06:15 +0000 | [diff] [blame] | 535 | } |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 536 | |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 537 | completionHandler(WTFMove(subresourcesEntry)); |
cdumez@apple.com | 5f09414 | 2015-11-11 23:01:30 +0000 | [diff] [blame] | 538 | return true; |
| 539 | }); |
| 540 | } |
| 541 | |
cdumez@apple.com | ee20abe | 2015-11-06 19:29:24 +0000 | [diff] [blame] | 542 | } // namespace NetworkCache |
| 543 | |
| 544 | } // namespace WebKit |
| 545 | |
| 546 | #endif // ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION) |