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) |
29 | #include "NetworkCacheSpeculativeLoad.h" |
30 | |
31 | #include "Logging.h" |
32 | #include "NetworkCache.h" |
33 | #include "NetworkLoad.h" |
34 | #include "NetworkProcess.h" |
35 | #include "NetworkSession.h" |
36 | #include <WebCore/NetworkStorageSession.h> |
37 | #include <pal/SessionID.h> |
38 | #include <wtf/RunLoop.h> |
39 | |
40 | namespace WebKit { |
41 | namespace NetworkCache { |
42 | |
43 | using namespace WebCore; |
44 | |
45 | SpeculativeLoad::SpeculativeLoad(Cache& cache, const GlobalFrameID& globalFrameID, const ResourceRequest& request, std::unique_ptr<NetworkCache::Entry> cacheEntryForValidation, RevalidationCompletionHandler&& completionHandler) |
46 | : m_cache(cache) |
47 | , m_globalFrameID(globalFrameID) |
48 | , m_completionHandler(WTFMove(completionHandler)) |
49 | , m_originalRequest(request) |
50 | , m_bufferedDataForCache(SharedBuffer::create()) |
51 | , m_cacheEntry(WTFMove(cacheEntryForValidation)) |
52 | { |
53 | ASSERT(!m_cacheEntry || m_cacheEntry->needsValidation()); |
54 | |
55 | NetworkLoadParameters parameters; |
56 | parameters.webPageID = globalFrameID.first; |
57 | parameters.webFrameID = globalFrameID.second; |
58 | parameters.sessionID = PAL::SessionID::defaultSessionID(); |
59 | parameters.storedCredentialsPolicy = StoredCredentialsPolicy::Use; |
60 | parameters.contentSniffingPolicy = ContentSniffingPolicy::DoNotSniffContent; |
61 | parameters.contentEncodingSniffingPolicy = ContentEncodingSniffingPolicy::Sniff; |
62 | parameters.request = m_originalRequest; |
63 | m_networkLoad = std::make_unique<NetworkLoad>(*this, nullptr, WTFMove(parameters), *cache.networkProcess().networkSession(PAL::SessionID::defaultSessionID())); |
64 | } |
65 | |
66 | SpeculativeLoad::~SpeculativeLoad() |
67 | { |
68 | ASSERT(!m_networkLoad); |
69 | } |
70 | |
71 | void SpeculativeLoad::willSendRedirectedRequest(ResourceRequest&& request, ResourceRequest&& redirectRequest, ResourceResponse&& redirectResponse) |
72 | { |
73 | LOG(NetworkCacheSpeculativePreloading, "Speculative redirect %s -> %s" , request.url().string().utf8().data(), redirectRequest.url().string().utf8().data()); |
74 | |
75 | Optional<Seconds> maxAgeCap; |
76 | #if ENABLE(RESOURCE_LOAD_STATISTICS) |
77 | if (auto* networkStorageSession = m_cache->networkProcess().storageSession(PAL::SessionID::defaultSessionID())) |
78 | maxAgeCap = networkStorageSession->maxAgeCacheCap(request); |
79 | #endif |
80 | m_cacheEntry = m_cache->storeRedirect(request, redirectResponse, redirectRequest, maxAgeCap); |
81 | // Create a synthetic cache entry if we can't store. |
82 | if (!m_cacheEntry) |
83 | m_cacheEntry = m_cache->makeRedirectEntry(request, redirectResponse, redirectRequest); |
84 | |
85 | // Don't follow the redirect. The redirect target will be registered for speculative load when it is loaded. |
86 | didComplete(); |
87 | } |
88 | |
89 | void SpeculativeLoad::didReceiveResponse(ResourceResponse&& receivedResponse, ResponseCompletionHandler&& completionHandler) |
90 | { |
91 | m_response = receivedResponse; |
92 | |
93 | if (m_response.isMultipart()) |
94 | m_bufferedDataForCache = nullptr; |
95 | |
96 | bool validationSucceeded = m_response.httpStatusCode() == 304; // 304 Not Modified |
97 | if (validationSucceeded && m_cacheEntry) |
98 | m_cacheEntry = m_cache->update(m_originalRequest, m_globalFrameID, *m_cacheEntry, m_response); |
99 | else |
100 | m_cacheEntry = nullptr; |
101 | |
102 | completionHandler(PolicyAction::Use); |
103 | } |
104 | |
105 | void SpeculativeLoad::didReceiveBuffer(Ref<SharedBuffer>&& buffer, int reportedEncodedDataLength) |
106 | { |
107 | ASSERT(!m_cacheEntry); |
108 | |
109 | if (m_bufferedDataForCache) { |
110 | // Prevent memory growth in case of streaming data. |
111 | const size_t maximumCacheBufferSize = 10 * 1024 * 1024; |
112 | if (m_bufferedDataForCache->size() + buffer->size() <= maximumCacheBufferSize) |
113 | m_bufferedDataForCache->append(buffer.get()); |
114 | else |
115 | m_bufferedDataForCache = nullptr; |
116 | } |
117 | } |
118 | |
119 | void SpeculativeLoad::didFinishLoading(const WebCore::NetworkLoadMetrics&) |
120 | { |
121 | if (m_didComplete) |
122 | return; |
123 | if (!m_cacheEntry && m_bufferedDataForCache) { |
124 | m_cacheEntry = m_cache->store(m_originalRequest, m_response, m_bufferedDataForCache.copyRef(), [](auto& mappedBody) { }); |
125 | // Create a synthetic cache entry if we can't store. |
126 | if (!m_cacheEntry && isStatusCodeCacheableByDefault(m_response.httpStatusCode())) |
127 | m_cacheEntry = m_cache->makeEntry(m_originalRequest, m_response, WTFMove(m_bufferedDataForCache)); |
128 | } |
129 | |
130 | didComplete(); |
131 | } |
132 | |
133 | void SpeculativeLoad::didFailLoading(const ResourceError&) |
134 | { |
135 | if (m_didComplete) |
136 | return; |
137 | m_cacheEntry = nullptr; |
138 | |
139 | didComplete(); |
140 | } |
141 | |
142 | void SpeculativeLoad::didComplete() |
143 | { |
144 | RELEASE_ASSERT(RunLoop::isMain()); |
145 | |
146 | if (m_didComplete) |
147 | return; |
148 | m_didComplete = true; |
149 | m_networkLoad = nullptr; |
150 | |
151 | // Make sure speculatively revalidated resources do not get validated by the NetworkResourceLoader again. |
152 | if (m_cacheEntry) |
153 | m_cacheEntry->setNeedsValidation(false); |
154 | |
155 | m_completionHandler(WTFMove(m_cacheEntry)); |
156 | } |
157 | |
158 | } // namespace NetworkCache |
159 | } // namespace WebKit |
160 | |
161 | #endif // ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION) |
162 | |