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