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 "CacheStorageEngineCache.h"
29#include "NetworkCacheStorage.h"
30#include <WebCore/ClientOrigin.h>
31#include <WebCore/StorageQuotaUser.h>
32#include <wtf/CompletionHandler.h>
33#include <wtf/Deque.h>
34
35namespace WebCore {
36class StorageQuotaManager;
37}
38
39namespace WebKit {
40
41namespace CacheStorage {
42
43class Engine;
44
45class Caches final : public RefCounted<Caches>, private WebCore::StorageQuotaUser {
46public:
47 static Ref<Caches> create(Engine&, WebCore::ClientOrigin&&, String&& rootPath, WebCore::StorageQuotaManager&);
48 ~Caches();
49
50 static void retrieveOriginFromDirectory(const String& folderPath, WorkQueue&, WTF::CompletionHandler<void(Optional<WebCore::ClientOrigin>&&)>&&);
51
52 void initialize(WebCore::DOMCacheEngine::CompletionCallback&&);
53 void open(const String& name, WebCore::DOMCacheEngine::CacheIdentifierCallback&&);
54 void remove(uint64_t identifier, WebCore::DOMCacheEngine::CacheIdentifierCallback&&);
55 void dispose(Cache&);
56
57 void detach();
58
59 bool isInitialized() const { return m_isInitialized; }
60 void cacheInfos(uint64_t updateCounter, WebCore::DOMCacheEngine::CacheInfosCallback&&);
61
62 Cache* find(uint64_t identifier);
63 void appendRepresentation(StringBuilder&) const;
64
65 void readRecordsList(Cache&, NetworkCache::Storage::TraverseHandler&&);
66 void readRecord(const NetworkCache::Key&, WTF::Function<void(Expected<WebCore::DOMCacheEngine::Record, WebCore::DOMCacheEngine::Error>&&)>&&);
67
68 void requestSpace(uint64_t spaceRequired, WebCore::DOMCacheEngine::CompletionCallback&&);
69 void writeRecord(const Cache&, const RecordInformation&, WebCore::DOMCacheEngine::Record&&, uint64_t previousRecordSize, WebCore::DOMCacheEngine::CompletionCallback&&);
70
71 void removeCacheEntry(const NetworkCache::Key&);
72 void removeRecord(const RecordInformation&);
73
74 const NetworkCache::Salt& salt() const;
75 const WebCore::ClientOrigin& origin() const { return m_origin; }
76
77 bool shouldPersist() const { return !m_rootPath.isNull(); }
78
79 void clear(WTF::CompletionHandler<void()>&&);
80 void clearMemoryRepresentation();
81 void resetSpaceUsed();
82
83 uint64_t storageSize() const;
84
85private:
86 Caches(Engine&, WebCore::ClientOrigin&&, String&& rootPath, WebCore::StorageQuotaManager&);
87
88 // StorageQuotaUser API.
89 uint64_t spaceUsed() const final { return m_size; }
90
91 void initializeSize();
92 void readCachesFromDisk(WTF::Function<void(Expected<Vector<Cache>, WebCore::DOMCacheEngine::Error>&&)>&&);
93 void writeCachesToDisk(WebCore::DOMCacheEngine::CompletionCallback&&);
94
95 void whenInitialized(CompletionHandler<void()>&&) final;
96
97 void storeOrigin(WebCore::DOMCacheEngine::CompletionCallback&&);
98 static Optional<WebCore::ClientOrigin> readOrigin(const NetworkCache::Data&);
99
100 Cache* find(const String& name);
101 void clearPendingWritingCachesToDiskCallbacks();
102
103 void makeDirty() { ++m_updateCounter; }
104 bool isDirty(uint64_t updateCounter) const;
105
106 bool hasActiveCache() const;
107
108 bool m_isInitialized { false };
109 Engine* m_engine { nullptr };
110 uint64_t m_updateCounter { 0 };
111 WebCore::ClientOrigin m_origin;
112 String m_rootPath;
113 uint64_t m_size { 0 };
114 Vector<Cache> m_caches;
115 Vector<Cache> m_removedCaches;
116 RefPtr<NetworkCache::Storage> m_storage;
117 HashMap<NetworkCache::Key, WebCore::DOMCacheEngine::Record> m_volatileStorage;
118 mutable Optional<NetworkCache::Salt> m_volatileSalt;
119 Vector<WebCore::DOMCacheEngine::CompletionCallback> m_pendingInitializationCallbacks;
120 bool m_isWritingCachesToDisk { false };
121 Deque<CompletionHandler<void(Optional<WebCore::DOMCacheEngine::Error>)>> m_pendingWritingCachesToDiskCallbacks;
122 WeakPtr<WebCore::StorageQuotaManager> m_quotaManager;
123};
124
125} // namespace CacheStorage
126
127} // namespace WebKit
128