1 | /* |
2 | * Copyright (C) 2019 Igalia S.L. |
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 | #include "PrefetchCache.h" |
28 | |
29 | #include <WebCore/HTTPHeaderNames.h> |
30 | |
31 | namespace WebKit { |
32 | |
33 | PrefetchCache::Entry::Entry(WebCore::ResourceResponse&& response, RefPtr<WebCore::SharedBuffer>&& buffer) |
34 | : response(WTFMove(response)), buffer(WTFMove(buffer)) |
35 | { |
36 | } |
37 | |
38 | PrefetchCache::Entry::Entry(WebCore::ResourceResponse&& redirectResponse, WebCore::ResourceRequest&& redirectRequest) |
39 | : response(WTFMove(redirectResponse)), redirectRequest(WTFMove(redirectRequest)) |
40 | { |
41 | } |
42 | |
43 | PrefetchCache::PrefetchCache() |
44 | : m_expirationTimer(*this, &PrefetchCache::clearExpiredEntries) |
45 | { |
46 | } |
47 | |
48 | PrefetchCache::~PrefetchCache() |
49 | { |
50 | } |
51 | |
52 | void PrefetchCache::clear() |
53 | { |
54 | m_expirationTimer.stop(); |
55 | m_sessionExpirationList.clear(); |
56 | if (m_sessionPrefetches) |
57 | m_sessionPrefetches->clear(); |
58 | } |
59 | |
60 | std::unique_ptr<PrefetchCache::Entry> PrefetchCache::take(const URL& url) |
61 | { |
62 | auto* resources = m_sessionPrefetches.get(); |
63 | if (!resources) |
64 | return nullptr; |
65 | m_sessionExpirationList.removeAllMatching([&url] (const auto& tuple) { |
66 | return std::get<0>(tuple) == url; |
67 | }); |
68 | return resources->take(url); |
69 | } |
70 | |
71 | static const Seconds expirationTimeout { 5_s }; |
72 | |
73 | void PrefetchCache::store(const URL& requestUrl, WebCore::ResourceResponse&& response, RefPtr<WebCore::SharedBuffer>&& buffer) |
74 | { |
75 | if (!m_sessionPrefetches) |
76 | m_sessionPrefetches = std::make_unique<PrefetchEntriesMap>(); |
77 | auto addResult = m_sessionPrefetches->add(requestUrl, std::make_unique<PrefetchCache::Entry>(WTFMove(response), WTFMove(buffer))); |
78 | // Limit prefetches for same url to 1. |
79 | if (!addResult.isNewEntry) |
80 | return; |
81 | m_sessionExpirationList.append(std::make_tuple(requestUrl, WallTime::now())); |
82 | if (!m_expirationTimer.isActive()) |
83 | m_expirationTimer.startOneShot(expirationTimeout); |
84 | } |
85 | |
86 | void PrefetchCache::storeRedirect(const URL& requestUrl, WebCore::ResourceResponse&& redirectResponse, WebCore::ResourceRequest&& redirectRequest) |
87 | { |
88 | if (!m_sessionPrefetches) |
89 | m_sessionPrefetches = std::make_unique<PrefetchEntriesMap>(); |
90 | redirectRequest.clearPurpose(); |
91 | m_sessionPrefetches->set(requestUrl, std::make_unique<PrefetchCache::Entry>(WTFMove(redirectResponse), WTFMove(redirectRequest))); |
92 | m_sessionExpirationList.append(std::make_tuple(requestUrl, WallTime::now())); |
93 | if (!m_expirationTimer.isActive()) |
94 | m_expirationTimer.startOneShot(expirationTimeout); |
95 | } |
96 | |
97 | void PrefetchCache::clearExpiredEntries() |
98 | { |
99 | URL requestUrl; |
100 | WallTime timestamp; |
101 | auto timeout = WallTime::now(); |
102 | while (!m_sessionExpirationList.isEmpty()) { |
103 | std::tie(requestUrl, timestamp) = m_sessionExpirationList.first(); |
104 | auto* resources = m_sessionPrefetches.get(); |
105 | ASSERT(resources); |
106 | ASSERT(resources->contains(requestUrl)); |
107 | auto elapsed = timeout - timestamp; |
108 | if (elapsed > expirationTimeout) { |
109 | resources->remove(requestUrl); |
110 | m_sessionExpirationList.removeFirst(); |
111 | } else { |
112 | m_expirationTimer.startOneShot(expirationTimeout - elapsed); |
113 | break; |
114 | } |
115 | } |
116 | } |
117 | |
118 | } |
119 | |