1/*
2 * Copyright (C) 2013-2019 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 "Connection.h"
29#include "LocalStorageDatabaseTracker.h"
30#include <WebCore/SecurityOriginData.h>
31#include <WebCore/StorageMap.h>
32#include <wtf/Forward.h>
33#include <wtf/Function.h>
34#include <wtf/HashSet.h>
35#include <wtf/text/StringHash.h>
36
37namespace WebCore {
38class SecurityOrigin;
39}
40
41namespace WebKit {
42
43class LocalStorageDatabaseTracker;
44class WebProcessProxy;
45
46using GetValuesCallback = CompletionHandler<void(const HashMap<String, String>&)>;
47
48class StorageManager : public IPC::Connection::WorkQueueMessageReceiver {
49public:
50 static Ref<StorageManager> create(const String& localStorageDirectory);
51 ~StorageManager();
52
53 void createSessionStorageNamespace(uint64_t storageNamespaceID, unsigned quotaInBytes);
54 void destroySessionStorageNamespace(uint64_t storageNamespaceID);
55 void addAllowedSessionStorageNamespaceConnection(uint64_t storageNamespaceID, IPC::Connection&);
56 void removeAllowedSessionStorageNamespaceConnection(uint64_t storageNamespaceID, IPC::Connection&);
57 void cloneSessionStorageNamespace(uint64_t storageNamespaceID, uint64_t newStorageNamespaceID);
58
59 void processDidCloseConnection(IPC::Connection&);
60 void waitUntilWritesFinished();
61 void suspend(CompletionHandler<void()>&&);
62 void resume();
63
64 void getSessionStorageOrigins(Function<void(HashSet<WebCore::SecurityOriginData>&&)>&& completionHandler);
65 void deleteSessionStorageOrigins(Function<void()>&& completionHandler);
66 void deleteSessionStorageEntriesForOrigins(const Vector<WebCore::SecurityOriginData>&, Function<void()>&& completionHandler);
67
68 void getLocalStorageOrigins(Function<void(HashSet<WebCore::SecurityOriginData>&&)>&& completionHandler);
69 void deleteLocalStorageEntriesForOrigin(const WebCore::SecurityOriginData&);
70
71 void deleteLocalStorageOriginsModifiedSince(WallTime, Function<void()>&& completionHandler);
72 void deleteLocalStorageEntriesForOrigins(const Vector<WebCore::SecurityOriginData>&, Function<void()>&& completionHandler);
73
74 void getLocalStorageOriginDetails(Function<void(Vector<LocalStorageDatabaseTracker::OriginDetails>&&)>&& completionHandler);
75
76 // IPC::Connection::WorkQueueMessageReceiver.
77 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
78 void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>& replyEncoder) override;
79
80private:
81 explicit StorageManager(const String& localStorageDirectory);
82
83 // Message handlers.
84 void createLocalStorageMap(IPC::Connection&, uint64_t storageMapID, uint64_t storageNamespaceID, WebCore::SecurityOriginData&&);
85 void createTransientLocalStorageMap(IPC::Connection&, uint64_t storageMapID, uint64_t storageNamespaceID, WebCore::SecurityOriginData&& topLevelOriginData, WebCore::SecurityOriginData&&);
86 void createSessionStorageMap(IPC::Connection&, uint64_t storageMapID, uint64_t storageNamespaceID, WebCore::SecurityOriginData&&);
87 void destroyStorageMap(IPC::Connection&, uint64_t storageMapID);
88
89 void getValues(IPC::Connection&, WebCore::SecurityOriginData&&, uint64_t storageMapID, uint64_t storageMapSeed, GetValuesCallback&&);
90 void setItem(IPC::Connection&, WebCore::SecurityOriginData&&, uint64_t storageMapID, uint64_t sourceStorageAreaID, uint64_t storageMapSeed, const String& key, const String& value, const String& urlString);
91 void setItems(IPC::Connection&, uint64_t storageMapID, const HashMap<String, String>& items);
92 void removeItem(IPC::Connection&, WebCore::SecurityOriginData&&, uint64_t storageMapID, uint64_t sourceStorageAreaID, uint64_t storageMapSeed, const String& key, const String& urlString);
93 void clear(IPC::Connection&, WebCore::SecurityOriginData&&, uint64_t storageMapID, uint64_t sourceStorageAreaID, uint64_t storageMapSeed, const String& urlString);
94
95 class StorageArea;
96 StorageArea* findStorageArea(IPC::Connection&, uint64_t) const;
97
98 class LocalStorageNamespace;
99 LocalStorageNamespace* getOrCreateLocalStorageNamespace(uint64_t storageNamespaceID);
100
101 class TransientLocalStorageNamespace;
102 TransientLocalStorageNamespace* getOrCreateTransientLocalStorageNamespace(uint64_t storageNamespaceID, WebCore::SecurityOriginData&& topLevelOrigin);
103
104 Ref<WorkQueue> m_queue;
105
106 RefPtr<LocalStorageDatabaseTracker> m_localStorageDatabaseTracker;
107 HashMap<uint64_t, RefPtr<LocalStorageNamespace>> m_localStorageNamespaces;
108
109 HashMap<std::pair<uint64_t, WebCore::SecurityOriginData>, RefPtr<TransientLocalStorageNamespace>> m_transientLocalStorageNamespaces;
110
111 class SessionStorageNamespace;
112 HashMap<uint64_t, RefPtr<SessionStorageNamespace>> m_sessionStorageNamespaces;
113
114 HashMap<std::pair<IPC::Connection::UniqueID, uint64_t>, RefPtr<StorageArea>> m_storageAreasByConnection;
115
116 enum class State {
117 Running,
118 WillSuspend,
119 Suspended
120 };
121 State m_state { State::Running };
122 Lock m_stateLock;
123 Condition m_stateChangeCondition;
124};
125
126} // namespace WebKit
127