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