2 * Copyright (C) 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 "CacheStorageEngineCaches.h"
29 #include "NetworkCacheData.h"
30 #include "WebsiteData.h"
31 #include <wtf/HashMap.h>
32 #include <wtf/ThreadSafeRefCounted.h>
33 #include <wtf/WorkQueue.h>
44 class CallbackAggregator;
49 namespace CacheStorage {
51 using CacheIdentifier = uint64_t;
52 using LockCount = uint64_t;
54 class Engine : public ThreadSafeRefCounted<Engine> {
58 static Engine& from(PAL::SessionID);
59 static void destroyEngine(PAL::SessionID);
60 static void fetchEntries(PAL::SessionID, bool shouldComputeSize, WTF::CompletionHandler<void(Vector<WebsiteData::Entry>)>&&);
62 static Ref<Engine> create(String&& rootPath) { return adoptRef(*new Engine(WTFMove(rootPath))); }
64 bool shouldPersist() const { return !!m_ioQueue;}
66 void open(const String& origin, const String& cacheName, WebCore::DOMCacheEngine::CacheIdentifierCallback&&);
67 void remove(uint64_t cacheIdentifier, WebCore::DOMCacheEngine::CacheIdentifierCallback&&);
68 void retrieveCaches(const String& origin, uint64_t updateCounter, WebCore::DOMCacheEngine::CacheInfosCallback&&);
70 void retrieveRecords(uint64_t cacheIdentifier, WebCore::URL&&, WebCore::DOMCacheEngine::RecordsCallback&&);
71 void putRecords(uint64_t cacheIdentifier, Vector<WebCore::DOMCacheEngine::Record>&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
72 void deleteMatchingRecords(uint64_t cacheIdentifier, WebCore::ResourceRequest&&, WebCore::CacheQueryOptions&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
74 void lock(uint64_t cacheIdentifier);
75 void unlock(uint64_t cacheIdentifier);
77 void writeFile(const String& filename, NetworkCache::Data&&, WebCore::DOMCacheEngine::CompletionCallback&&);
78 void readFile(const String& filename, WTF::Function<void(const NetworkCache::Data&, int error)>&&);
79 void removeFile(const String& filename);
81 const String& rootPath() const { return m_rootPath; }
82 const NetworkCache::Salt& salt() const { return m_salt.value(); }
83 uint64_t nextCacheIdentifier() { return ++m_nextCacheIdentifier; }
85 void removeCaches(const String& origin);
87 void clearMemoryRepresentation(const String& origin, WebCore::DOMCacheEngine::CompletionCallback&&);
88 String representation();
90 void clearAllCaches(WTF::CallbackAggregator&);
91 void clearCachesForOrigin(const String& origin, WTF::CallbackAggregator&);
94 static Engine& defaultEngine();
95 explicit Engine(String&& rootPath);
97 String cachesRootPath(const String& origin);
99 void fetchEntries(bool /* shouldComputeSize */, WTF::CompletionHandler<void(Vector<WebsiteData::Entry>)>&&);
101 void initialize(WTF::Function<void(std::optional<WebCore::DOMCacheEngine::Error>&&)>&&);
103 using CachesOrError = Expected<std::reference_wrapper<Caches>, WebCore::DOMCacheEngine::Error>;
104 using CachesCallback = WTF::Function<void(CachesOrError&&)>;
105 void readCachesFromDisk(const String& origin, CachesCallback&&);
107 using CacheOrError = Expected<std::reference_wrapper<Cache>, WebCore::DOMCacheEngine::Error>;
108 using CacheCallback = WTF::Function<void(CacheOrError&&)>;
109 void readCache(uint64_t cacheIdentifier, CacheCallback&&);
111 Cache* cache(uint64_t cacheIdentifier);
113 HashMap<String, Ref<Caches>> m_caches;
114 uint64_t m_nextCacheIdentifier { 0 };
116 RefPtr<WorkQueue> m_ioQueue;
117 std::optional<NetworkCache::Salt> m_salt;
118 HashMap<CacheIdentifier, LockCount> m_cacheLocks;
121 } // namespace CacheStorage
123 } // namespace WebKit