REGRESSION (r225789): API tests WKProcessPool.InitialWarmedProcessUsed and WebKit...
[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/ServiceWorkerIdentifier.h>
33 #include <WebCore/ServiceWorkerTypes.h>
34 #include <WebCore/UniqueIDBDatabase.h>
35 #include <pal/SessionID.h>
36 #include <wtf/CrossThreadTask.h>
37 #include <wtf/Function.h>
38
39 namespace IPC {
40 class FormDataReference;
41 }
42
43 namespace WebCore {
44 class SWServer;
45 struct SecurityOriginData;
46 struct ServiceWorkerClientIdentifier;
47 class ServiceWorkerRegistrationKey;
48 }
49
50 namespace WebKit {
51
52 class StorageToWebProcessConnection;
53 class WebSWServerConnection;
54 class WebSWServerToContextConnection;
55 enum class WebsiteDataType;
56 struct StorageProcessCreationParameters;
57
58 #if ENABLE(SERVICE_WORKER)
59 class WebSWOriginStore;
60 #endif
61
62 class StorageProcess : public ChildProcess
63 #if ENABLE(INDEXED_DATABASE)
64     , public WebCore::IDBServer::IDBBackingStoreTemporaryFileHandler
65 #endif
66 {
67     WTF_MAKE_NONCOPYABLE(StorageProcess);
68     friend class NeverDestroyed<StorageProcess>;
69 public:
70     static StorageProcess& singleton();
71     ~StorageProcess();
72
73     WorkQueue& queue() { return m_queue.get(); }
74     void postStorageTask(CrossThreadTask&&);
75
76 #if ENABLE(INDEXED_DATABASE)
77     WebCore::IDBServer::IDBServer& idbServer(PAL::SessionID);
78
79     // WebCore::IDBServer::IDBBackingStoreFileHandler
80     void prepareForAccessToTemporaryFile(const String& path) final;
81     void accessToTemporaryFileComplete(const String& path) final;
82 #endif
83
84 #if ENABLE(SANDBOX_EXTENSIONS)
85     void getSandboxExtensionsForBlobFiles(const Vector<String>& filenames, WTF::Function<void (SandboxExtension::HandleArray&&)>&& completionHandler);
86 #endif
87
88 #if ENABLE(SERVICE_WORKER)
89     // For now we just have one global connection to service worker context processes.
90     // This will change in the future.
91     WebSWServerToContextConnection* globalServerToContextConnection();
92     void createServerToContextConnection(std::optional<PAL::SessionID>);
93
94     WebCore::SWServer& swServerForSession(PAL::SessionID);
95     void registerSWServerConnection(WebSWServerConnection&);
96     void unregisterSWServerConnection(WebSWServerConnection&);
97 #endif
98
99     void didReceiveStorageProcessMessage(IPC::Connection&, IPC::Decoder&);
100
101 #if ENABLE(SERVICE_WORKER)
102     void connectionToContextProcessWasClosed();
103 #endif
104
105 private:
106     StorageProcess();
107
108     // ChildProcess
109     void initializeProcess(const ChildProcessInitializationParameters&) override;
110     void initializeProcessName(const ChildProcessInitializationParameters&) override;
111     void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
112     void initializeConnection(IPC::Connection*) override;
113     bool shouldTerminate() override;
114
115     // IPC::Connection::Client
116     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
117     void didClose(IPC::Connection&) override;
118
119     // Message Handlers
120     void initializeWebsiteDataStore(const StorageProcessCreationParameters&);
121     void createStorageToWebProcessConnection(bool isServiceWorkerProcess);
122
123     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType> websiteDataTypes, uint64_t callbackID);
124     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType> websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID);
125     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebsiteDataType> websiteDataTypes, const Vector<WebCore::SecurityOriginData>& origins, uint64_t callbackID);
126 #if ENABLE(SANDBOX_EXTENSIONS)
127     void grantSandboxExtensionsForBlobs(const Vector<String>& paths, SandboxExtension::HandleArray&&);
128     void didGetSandboxExtensionsForBlobFiles(uint64_t requestID, SandboxExtension::HandleArray&&);
129 #endif
130 #if ENABLE(SERVICE_WORKER)
131     void didReceiveFetchResponse(WebCore::SWServerConnectionIdentifier, uint64_t fetchIdentifier, const WebCore::ResourceResponse&);
132     void didReceiveFetchData(WebCore::SWServerConnectionIdentifier, uint64_t fetchIdentifier, const IPC::DataReference&, int64_t encodedDataLength);
133     void didReceiveFetchFormData(WebCore::SWServerConnectionIdentifier, uint64_t fetchIdentifier, const IPC::FormDataReference&);
134     void didFinishFetch(WebCore::SWServerConnectionIdentifier, uint64_t fetchIdentifier);
135     void didFailFetch(WebCore::SWServerConnectionIdentifier, uint64_t fetchIdentifier);
136     void didNotHandleFetch(WebCore::SWServerConnectionIdentifier, uint64_t fetchIdentifier);
137
138     void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, const IPC::DataReference& message, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin);
139     WebSWOriginStore& swOriginStoreForSession(PAL::SessionID);
140     bool needsServerToContextConnection() const;
141 #endif
142 #if ENABLE(INDEXED_DATABASE)
143     Vector<WebCore::SecurityOriginData> indexedDatabaseOrigins(const String& path);
144 #endif
145
146     // For execution on work queue thread only
147     void performNextStorageTask();
148     void ensurePathExists(const String&);
149
150     Vector<Ref<StorageToWebProcessConnection>> m_storageToWebProcessConnections;
151
152     Ref<WorkQueue> m_queue;
153
154 #if ENABLE(INDEXED_DATABASE)
155     HashMap<PAL::SessionID, String> m_idbDatabasePaths;
156     HashMap<PAL::SessionID, RefPtr<WebCore::IDBServer::IDBServer>> m_idbServers;
157 #endif
158     HashMap<String, RefPtr<SandboxExtension>> m_blobTemporaryFileSandboxExtensions;
159     HashMap<uint64_t, WTF::Function<void (SandboxExtension::HandleArray&&)>> m_sandboxExtensionForBlobsCompletionHandlers;
160
161     Deque<CrossThreadTask> m_storageTasks;
162     Lock m_storageTaskMutex;
163     
164 #if ENABLE(SERVICE_WORKER)
165     void didCreateWorkerContextProcessConnection(const IPC::Attachment&);
166
167     RefPtr<WebSWServerToContextConnection> m_serverToContextConnection;
168     bool m_waitingForServerToContextProcessConnection { false };
169     HashMap<PAL::SessionID, String> m_swDatabasePaths;
170     HashMap<PAL::SessionID, std::unique_ptr<WebCore::SWServer>> m_swServers;
171     HashMap<WebCore::SWServerConnectionIdentifier, WebSWServerConnection*> m_swServerConnections;
172 #endif
173 };
174
175 } // namespace WebKit