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::PrefetchCache() |
39 | : m_expirationTimer(*this, &PrefetchCache::clearExpiredEntries) |
40 | { |
41 | } |
42 | |
43 | PrefetchCache::~PrefetchCache() |
44 | { |
45 | } |
46 | |
47 | void PrefetchCache::clear() |
48 | { |
49 | m_expirationTimer.stop(); |
50 | m_sessionExpirationList.clear(); |
51 | if (m_sessionPrefetches) |
52 | m_sessionPrefetches->clear(); |
53 | } |
54 | |
55 | std::unique_ptr<PrefetchCache::Entry> PrefetchCache::take(const URL& url) |
56 | { |
57 | auto* resources = m_sessionPrefetches.get(); |
58 | if (!resources) |
59 | return nullptr; |
60 | m_sessionExpirationList.removeAllMatching([&url] (const auto& tuple) { |
61 | return std::get<0>(tuple) == url; |
62 | }); |
63 | return resources->take(url); |
64 | } |
65 | |
66 | static const Seconds expirationTimeout { 5_s }; |
67 | |
68 | void PrefetchCache::store(const URL& requestUrl, WebCore::ResourceResponse&& response, RefPtr<WebCore::SharedBuffer>&& buffer) |
69 | { |
70 | if (!m_sessionPrefetches) |
71 | m_sessionPrefetches = std::make_unique<PrefetchEntriesMap>(); |
72 | auto addResult = m_sessionPrefetches->add(requestUrl, std::make_unique<PrefetchCache::Entry>(WTFMove(response), WTFMove(buffer))); |
73 | // Limit prefetches for same url to 1. |
74 | if (!addResult.isNewEntry) |
75 | return; |
76 | m_sessionExpirationList.append(std::make_tuple(requestUrl, WallTime::now())); |
77 | if (!m_expirationTimer.isActive()) |
78 | m_expirationTimer.startOneShot(expirationTimeout); |
79 | } |
80 | |
81 | void PrefetchCache::clearExpiredEntries() |
82 | { |
83 | URL requestUrl; |
84 | WallTime timestamp; |
85 | auto timeout = WallTime::now(); |
86 | while (!m_sessionExpirationList.isEmpty()) { |
87 | std::tie(requestUrl, timestamp) = m_sessionExpirationList.first(); |
88 | auto* resources = m_sessionPrefetches.get(); |
89 | ASSERT(resources); |
90 | ASSERT(resources->contains(requestUrl)); |
91 | auto elapsed = timeout - timestamp; |
92 | if (elapsed > expirationTimeout) { |
93 | resources->remove(requestUrl); |
94 | m_sessionExpirationList.removeFirst(); |
95 | } else { |
96 | m_expirationTimer.startOneShot(expirationTimeout - elapsed); |
97 | break; |
98 | } |
99 | } |
100 | } |
101 | |
102 | } |
103 | |