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#ifndef NetworkCacheSpeculativeLoadManager_h
27#define NetworkCacheSpeculativeLoadManager_h
28
29#if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
30
31#include "NetworkCache.h"
32#include "NetworkCacheStorage.h"
33#include <WebCore/ResourceRequest.h>
34#include <wtf/HashMap.h>
35#include <wtf/Vector.h>
36
37namespace WebKit {
38
39namespace NetworkCache {
40
41class Entry;
42class SpeculativeLoad;
43class SubresourceInfo;
44class SubresourcesEntry;
45
46class SpeculativeLoadManager {
47 WTF_MAKE_FAST_ALLOCATED;
48public:
49 explicit SpeculativeLoadManager(Cache&, Storage&);
50 ~SpeculativeLoadManager();
51
52 void registerLoad(const GlobalFrameID&, const WebCore::ResourceRequest&, const Key& resourceKey);
53
54 typedef Function<void (std::unique_ptr<Entry>)> RetrieveCompletionHandler;
55
56 bool canRetrieve(const Key& storageKey, const WebCore::ResourceRequest&, const GlobalFrameID&) const;
57 void retrieve(const Key& storageKey, RetrieveCompletionHandler&&);
58
59private:
60 class PreloadedEntry;
61
62 void addPreloadedEntry(std::unique_ptr<Entry>, const GlobalFrameID&, Optional<WebCore::ResourceRequest>&& revalidationRequest = WTF::nullopt);
63 void preloadEntry(const Key&, const SubresourceInfo&, const GlobalFrameID&);
64 void retrieveEntryFromStorage(const SubresourceInfo&, RetrieveCompletionHandler&&);
65 void revalidateSubresource(const SubresourceInfo&, std::unique_ptr<Entry>, const GlobalFrameID&);
66 bool satisfyPendingRequests(const Key&, Entry*);
67 void retrieveSubresourcesEntry(const Key& storageKey, WTF::Function<void (std::unique_ptr<SubresourcesEntry>)>&&);
68 void startSpeculativeRevalidation(const GlobalFrameID&, SubresourcesEntry&);
69
70 static bool canUsePreloadedEntry(const PreloadedEntry&, const WebCore::ResourceRequest& actualRequest);
71 static bool canUsePendingPreload(const SpeculativeLoad&, const WebCore::ResourceRequest& actualRequest);
72
73 Cache& m_cache;
74 Storage& m_storage;
75
76 class PendingFrameLoad;
77 HashMap<GlobalFrameID, RefPtr<PendingFrameLoad>> m_pendingFrameLoads;
78
79 HashMap<Key, std::unique_ptr<SpeculativeLoad>> m_pendingPreloads;
80 HashMap<Key, std::unique_ptr<Vector<RetrieveCompletionHandler>>> m_pendingRetrieveRequests;
81
82 HashMap<Key, std::unique_ptr<PreloadedEntry>> m_preloadedEntries;
83
84 class ExpiringEntry;
85 HashMap<Key, std::unique_ptr<ExpiringEntry>> m_notPreloadedEntries; // For logging.
86};
87
88} // namespace NetworkCache
89
90} // namespace WebKit
91
92#endif // ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
93
94#endif // NetworkCacheSpeculativeLoadManager_h
95