1/*
2 * Copyright (C) 2017 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#pragma once
27
28#include "CacheStorageEngineCaches.h"
29#include "NetworkCacheData.h"
30#include "WebsiteData.h"
31#include <WebCore/ClientOrigin.h>
32#include <wtf/HashMap.h>
33#include <wtf/RefCounted.h>
34#include <wtf/WeakPtr.h>
35#include <wtf/WorkQueue.h>
36
37namespace IPC {
38class Connection;
39}
40
41namespace WebCore {
42class SessionID;
43}
44
45namespace WTF {
46class CallbackAggregator;
47};
48
49namespace WebKit {
50
51class NetworkProcess;
52
53namespace CacheStorage {
54
55using CacheIdentifier = uint64_t;
56using LockCount = uint64_t;
57
58class Engine : public RefCounted<Engine>, public CanMakeWeakPtr<Engine> {
59public:
60 ~Engine();
61
62 static void from(NetworkProcess&, PAL::SessionID, Function<void(Engine&)>&&);
63 static void destroyEngine(NetworkProcess&, PAL::SessionID);
64 static void fetchEntries(NetworkProcess&, PAL::SessionID, bool shouldComputeSize, CompletionHandler<void(Vector<WebsiteData::Entry>)>&&);
65
66 static void open(NetworkProcess&, PAL::SessionID, WebCore::ClientOrigin&&, String&& cacheName, WebCore::DOMCacheEngine::CacheIdentifierCallback&&);
67 static void remove(NetworkProcess&, PAL::SessionID, uint64_t cacheIdentifier, WebCore::DOMCacheEngine::CacheIdentifierCallback&&);
68 static void retrieveCaches(NetworkProcess&, PAL::SessionID, WebCore::ClientOrigin&&, uint64_t updateCounter, WebCore::DOMCacheEngine::CacheInfosCallback&&);
69
70 static void retrieveRecords(NetworkProcess&, PAL::SessionID, uint64_t cacheIdentifier, URL&&, WebCore::DOMCacheEngine::RecordsCallback&&);
71 static void putRecords(NetworkProcess&, PAL::SessionID, uint64_t cacheIdentifier, Vector<WebCore::DOMCacheEngine::Record>&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
72 static void deleteMatchingRecords(NetworkProcess&, PAL::SessionID, uint64_t cacheIdentifier, WebCore::ResourceRequest&&, WebCore::CacheQueryOptions&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
73
74 static void lock(NetworkProcess&, PAL::SessionID, uint64_t cacheIdentifier);
75 static void unlock(NetworkProcess&, PAL::SessionID, uint64_t cacheIdentifier);
76
77 static void clearMemoryRepresentation(NetworkProcess&, PAL::SessionID, WebCore::ClientOrigin&&, WebCore::DOMCacheEngine::CompletionCallback&&);
78 static void representation(NetworkProcess&, PAL::SessionID, CompletionHandler<void(String&&)>&&);
79
80 static void clearAllCaches(NetworkProcess&, PAL::SessionID, CompletionHandler<void()>&&);
81 static void clearCachesForOrigin(NetworkProcess&, PAL::SessionID, WebCore::SecurityOriginData&&, CompletionHandler<void()>&&);
82
83 static void initializeQuotaUser(NetworkProcess&, PAL::SessionID, const WebCore::ClientOrigin&, CompletionHandler<void()>&&);
84
85 bool shouldPersist() const { return !!m_ioQueue;}
86
87 void writeFile(const String& filename, NetworkCache::Data&&, WebCore::DOMCacheEngine::CompletionCallback&&);
88 void readFile(const String& filename, CompletionHandler<void(const NetworkCache::Data&, int error)>&&);
89 void removeFile(const String& filename);
90
91 const String& rootPath() const { return m_rootPath; }
92 const NetworkCache::Salt& salt() const { return m_salt.value(); }
93 uint64_t nextCacheIdentifier() { return ++m_nextCacheIdentifier; }
94
95private:
96 Engine(PAL::SessionID, NetworkProcess&, String&& rootPath);
97
98 void open(const WebCore::ClientOrigin&, const String& cacheName, WebCore::DOMCacheEngine::CacheIdentifierCallback&&);
99 void remove(uint64_t cacheIdentifier, WebCore::DOMCacheEngine::CacheIdentifierCallback&&);
100 void retrieveCaches(const WebCore::ClientOrigin&, uint64_t updateCounter, WebCore::DOMCacheEngine::CacheInfosCallback&&);
101
102 void clearAllCaches(CompletionHandler<void()>&&);
103 void clearAllCachesFromDisk(CompletionHandler<void()>&&);
104 void clearCachesForOrigin(const WebCore::SecurityOriginData&, CompletionHandler<void()>&&);
105 void clearCachesForOriginFromDisk(const WebCore::SecurityOriginData&, CompletionHandler<void()>&&);
106 void deleteDirectoryRecursivelyOnBackgroundThread(const String& path, CompletionHandler<void()>&&);
107
108 void clearMemoryRepresentation(const WebCore::ClientOrigin&, WebCore::DOMCacheEngine::CompletionCallback&&);
109 String representation();
110
111 void retrieveRecords(uint64_t cacheIdentifier, URL&&, WebCore::DOMCacheEngine::RecordsCallback&&);
112 void putRecords(uint64_t cacheIdentifier, Vector<WebCore::DOMCacheEngine::Record>&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
113 void deleteMatchingRecords(uint64_t cacheIdentifier, WebCore::ResourceRequest&&, WebCore::CacheQueryOptions&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
114
115 void lock(uint64_t cacheIdentifier);
116 void unlock(uint64_t cacheIdentifier);
117
118 String cachesRootPath(const WebCore::ClientOrigin&);
119
120 void fetchEntries(bool /* shouldComputeSize */, CompletionHandler<void(Vector<WebsiteData::Entry>)>&&);
121
122 void initialize(WebCore::DOMCacheEngine::CompletionCallback&&);
123
124 using CachesOrError = Expected<std::reference_wrapper<Caches>, WebCore::DOMCacheEngine::Error>;
125 using CachesCallback = Function<void(CachesOrError&&)>;
126 void readCachesFromDisk(const WebCore::ClientOrigin&, CachesCallback&&);
127
128 using CacheOrError = Expected<std::reference_wrapper<Cache>, WebCore::DOMCacheEngine::Error>;
129 using CacheCallback = Function<void(CacheOrError&&)>;
130 void readCache(uint64_t cacheIdentifier, CacheCallback&&);
131
132 Cache* cache(uint64_t cacheIdentifier);
133
134 PAL::SessionID m_sessionID;
135 WeakPtr<NetworkProcess> m_networkProcess;
136 HashMap<WebCore::ClientOrigin, RefPtr<Caches>> m_caches;
137 uint64_t m_nextCacheIdentifier { 0 };
138 String m_rootPath;
139 RefPtr<WorkQueue> m_ioQueue;
140 Optional<NetworkCache::Salt> m_salt;
141 HashMap<CacheIdentifier, LockCount> m_cacheLocks;
142 Vector<WebCore::DOMCacheEngine::CompletionCallback> m_initializationCallbacks;
143 HashMap<uint64_t, WebCore::DOMCacheEngine::CompletionCallback> m_pendingWriteCallbacks;
144 HashMap<uint64_t, CompletionHandler<void(const NetworkCache::Data&, int error)>> m_pendingReadCallbacks;
145 uint64_t m_pendingCallbacksCounter { 0 };
146};
147
148} // namespace CacheStorage
149
150} // namespace WebKit
151