2 * Copyright (C) 2014-2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "NetworkCacheEntry.h"
29 #include "NetworkCacheStorage.h"
30 #include "ShareableResource.h"
31 #include <WebCore/PageIdentifier.h>
32 #include <WebCore/ResourceResponse.h>
33 #include <wtf/CompletionHandler.h>
34 #include <wtf/OptionSet.h>
35 #include <wtf/Seconds.h>
36 #include <wtf/text/WTFString.h>
39 class LowPowerModeNotifier;
40 class ResourceRequest;
48 namespace NetworkCache {
51 class SpeculativeLoadManager;
54 #if ENABLE(SHAREABLE_RESOURCE)
55 RefPtr<ShareableResource> shareableResource;
56 ShareableResource::Handle shareableResourceHandle;
60 enum class RetrieveDecision {
63 NoDueToConditionalRequest,
64 NoDueToReloadIgnoringCache,
65 NoDueToStreamingMedia,
68 enum class StoreDecision {
72 NoDueToNoStoreResponse,
73 NoDueToHTTPStatusCode,
74 NoDueToNoStoreRequest,
75 NoDueToUnlikelyToReuse,
76 NoDueToStreamingMedia,
79 enum class UseDecision {
82 NoDueToVaryingHeaderMismatch,
83 NoDueToMissingValidatorFields,
85 NoDueToExpiredRedirect
88 using GlobalFrameID = std::pair<WebCore::PageIdentifier, uint64_t /*webFrameID*/>;
90 enum class CacheOption : uint8_t {
91 // In testing mode we try to eliminate sources of randomness. Cache does not shrink and there are no read timeouts.
93 RegisterNotify = 1 << 1,
94 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
95 SpeculativeRevalidation = 1 << 2,
99 class Cache : public RefCounted<Cache> {
101 static RefPtr<Cache> open(NetworkProcess&, const String& cachePath, OptionSet<CacheOption>);
103 void setCapacity(size_t);
105 // Completion handler may get called back synchronously on failure.
106 struct RetrieveInfo {
107 MonotonicTime startTime;
108 MonotonicTime completionTime;
110 Storage::Timings storageTimings;
111 bool wasSpeculativeLoad { false };
113 WTF_MAKE_FAST_ALLOCATED;
115 using RetrieveCompletionHandler = Function<void(std::unique_ptr<Entry>, const RetrieveInfo&)>;
116 void retrieve(const WebCore::ResourceRequest&, const GlobalFrameID&, RetrieveCompletionHandler&&);
117 std::unique_ptr<Entry> store(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, RefPtr<WebCore::SharedBuffer>&&, Function<void(MappedBody&)>&&);
118 std::unique_ptr<Entry> storeRedirect(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, const WebCore::ResourceRequest& redirectRequest, Optional<Seconds> maxAgeCap);
119 std::unique_ptr<Entry> update(const WebCore::ResourceRequest&, const GlobalFrameID&, const Entry&, const WebCore::ResourceResponse& validatingResponse);
121 struct TraversalEntry {
123 const Storage::RecordInfo& recordInfo;
125 void traverse(Function<void(const TraversalEntry*)>&&);
126 void remove(const Key&);
127 void remove(const WebCore::ResourceRequest&);
128 void remove(const Vector<Key>&, Function<void()>&&);
131 void clear(WallTime modifiedSince, Function<void()>&&);
133 void retrieveData(const DataKey&, Function<void(const uint8_t*, size_t)>);
134 void storeData(const DataKey&, const uint8_t* data, size_t);
136 std::unique_ptr<Entry> makeEntry(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, RefPtr<WebCore::SharedBuffer>&&);
137 std::unique_ptr<Entry> makeRedirectEntry(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, const WebCore::ResourceRequest& redirectRequest);
139 void dumpContentsToFile();
141 String recordsPath() const;
143 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
144 SpeculativeLoadManager* speculativeLoadManager() { return m_speculativeLoadManager.get(); }
147 NetworkProcess& networkProcess() { return m_networkProcess.get(); }
152 Cache(NetworkProcess&, Ref<Storage>&&, OptionSet<CacheOption>);
154 Key makeCacheKey(const WebCore::ResourceRequest&);
156 static void completeRetrieve(RetrieveCompletionHandler&&, std::unique_ptr<Entry>, RetrieveInfo&);
158 String dumpFilePath() const;
159 void deleteDumpFile();
161 Optional<Seconds> maxAgeCap(Entry&, const WebCore::ResourceRequest&, PAL::SessionID);
163 Ref<Storage> m_storage;
164 Ref<NetworkProcess> m_networkProcess;
166 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
167 std::unique_ptr<WebCore::LowPowerModeNotifier> m_lowPowerModeNotifier;
168 std::unique_ptr<SpeculativeLoadManager> m_speculativeLoadManager;
171 unsigned m_traverseCount { 0 };
174 } // namespace NetworkCache
175 } // namespace WebKit