Source/WebCore:
[WebKit-https.git] / Source / WebKit / StorageProcess / StorageProcess.h
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 "ChildProcess.h"
29 #include "SandboxExtension.h"
30 #include <WebCore/IDBBackingStore.h>
31 #include <WebCore/IDBServer.h>
32 #include <WebCore/UniqueIDBDatabase.h>
33 #include <pal/SessionID.h>
34 #include <wtf/CrossThreadTask.h>
35 #include <wtf/Function.h>
36
37 namespace WebCore {
38 class SWServer;
39 struct SecurityOriginData;
40 struct ServiceWorkerRegistrationKey;
41 }
42
43 namespace WebKit {
44
45 class StorageToWebProcessConnection;
46 class WebSWServerConnection;
47 enum class WebsiteDataType;
48 struct StorageProcessCreationParameters;
49
50 #if ENABLE(SERVICE_WORKER)
51 class WebSWOriginStore;
52 #endif
53
54 class StorageProcess : public ChildProcess
55 #if ENABLE(INDEXED_DATABASE)
56     , public WebCore::IDBServer::IDBBackingStoreTemporaryFileHandler
57 #endif
58 {
59     WTF_MAKE_NONCOPYABLE(StorageProcess);
60     friend class NeverDestroyed<StorageProcess>;
61 public:
62     static StorageProcess& singleton();
63     ~StorageProcess();
64
65     WorkQueue& queue() { return m_queue.get(); }
66     void postStorageTask(CrossThreadTask&&);
67
68 #if ENABLE(INDEXED_DATABASE)
69     WebCore::IDBServer::IDBServer& idbServer(PAL::SessionID);
70
71     // WebCore::IDBServer::IDBBackingStoreFileHandler
72     void prepareForAccessToTemporaryFile(const String& path) final;
73     void accessToTemporaryFileComplete(const String& path) final;
74 #endif
75
76 #if ENABLE(SANDBOX_EXTENSIONS)
77     void getSandboxExtensionsForBlobFiles(const Vector<String>& filenames, WTF::Function<void (SandboxExtension::HandleArray&&)>&& completionHandler);
78 #endif
79
80 #if ENABLE(SERVICE_WORKER)
81     IPC::Connection* workerContextProcessConnection();
82     void createWorkerContextProcessConnection();
83
84     WebSWOriginStore& ensureSWOriginStoreForSession(PAL::SessionID);
85     WebSWOriginStore* swOriginStoreForSession(PAL::SessionID) const;
86
87     WebCore::SWServer& swServerForSession(PAL::SessionID);
88     void registerSWServerConnection(WebSWServerConnection&);
89     void unregisterSWServerConnection(WebSWServerConnection&);
90 #endif
91
92 private:
93     StorageProcess();
94
95     // ChildProcess
96     void initializeProcess(const ChildProcessInitializationParameters&) override;
97     void initializeProcessName(const ChildProcessInitializationParameters&) override;
98     void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
99     void initializeConnection(IPC::Connection*) override;
100     bool shouldTerminate() override;
101
102     // IPC::Connection::Client
103     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
104     void didClose(IPC::Connection&) override;
105     void didReceiveStorageProcessMessage(IPC::Connection&, IPC::Decoder&);
106
107     // Message Handlers
108     void initializeWebsiteDataStore(const StorageProcessCreationParameters&);
109     void createStorageToWebProcessConnection();
110
111     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType> websiteDataTypes, uint64_t callbackID);
112     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType> websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID);
113     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebsiteDataType> websiteDataTypes, const Vector<WebCore::SecurityOriginData>& origins, uint64_t callbackID);
114 #if ENABLE(SANDBOX_EXTENSIONS)
115     void grantSandboxExtensionsForBlobs(const Vector<String>& paths, const SandboxExtension::HandleArray&);
116     void didGetSandboxExtensionsForBlobFiles(uint64_t requestID, SandboxExtension::HandleArray&&);
117 #endif
118 #if ENABLE(SERVICE_WORKER)
119     void didGetWorkerContextProcessConnection(const IPC::Attachment& encodedConnectionIdentifier);
120     void serviceWorkerContextFailedToStart(uint64_t serverConnectionIdentifier, const WebCore::ServiceWorkerRegistrationKey&, const String& workerID, const String& message);
121     void serviceWorkerContextStarted(uint64_t serverConnectionIdentifier, const WebCore::ServiceWorkerRegistrationKey&, uint64_t identifier, const String& workerID);
122
123     void didReceiveFetchResponse(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier, const WebCore::ResourceResponse&);
124     void didReceiveFetchData(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier, const IPC::DataReference&, int64_t encodedDataLength);
125     void didFinishFetch(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier);
126     void didFailFetch(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier);
127     void didNotHandleFetch(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier);
128 #endif
129 #if ENABLE(INDEXED_DATABASE)
130     Vector<WebCore::SecurityOriginData> indexedDatabaseOrigins(const String& path);
131 #endif
132
133     // For execution on work queue thread only
134     void performNextStorageTask();
135     void ensurePathExists(const String&);
136
137     Vector<Ref<StorageToWebProcessConnection>> m_storageToWebProcessConnections;
138
139     Ref<WorkQueue> m_queue;
140
141 #if ENABLE(INDEXED_DATABASE)
142     HashMap<PAL::SessionID, String> m_idbDatabasePaths;
143     HashMap<PAL::SessionID, RefPtr<WebCore::IDBServer::IDBServer>> m_idbServers;
144 #endif
145     HashMap<String, RefPtr<SandboxExtension>> m_blobTemporaryFileSandboxExtensions;
146     HashMap<uint64_t, WTF::Function<void (SandboxExtension::HandleArray&&)>> m_sandboxExtensionForBlobsCompletionHandlers;
147
148     Deque<CrossThreadTask> m_storageTasks;
149     Lock m_storageTaskMutex;
150     
151 #if ENABLE(SERVICE_WORKER)
152     void didCreateWorkerContextProcessConnection(const IPC::Attachment&);
153
154     RefPtr<IPC::Connection> m_workerContextProcessConnection;
155     bool m_waitingForWorkerContextProcessConnection { false };
156     HashMap<PAL::SessionID, std::unique_ptr<WebCore::SWServer>> m_swServers;
157     HashMap<uint64_t, WebSWServerConnection*> m_swServerConnections;
158     HashMap<PAL::SessionID, std::unique_ptr<WebSWOriginStore>> m_swOriginStores;
159 #endif
160 };
161
162 } // namespace WebKit