Remove LegacyIDB.
[WebKit-https.git] / Source / WebKit2 / DatabaseProcess / DatabaseProcess.cpp
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 #include "config.h"
27 #include "DatabaseProcess.h"
28
29 #if ENABLE(DATABASE_PROCESS)
30
31 #include "DatabaseProcessCreationParameters.h"
32 #include "DatabaseProcessMessages.h"
33 #include "DatabaseProcessProxyMessages.h"
34 #include "DatabaseToWebProcessConnection.h"
35 #include "WebCrossThreadCopier.h"
36 #include "WebsiteData.h"
37 #include <WebCore/CrossThreadTask.h>
38 #include <WebCore/FileSystem.h>
39 #include <WebCore/NotImplemented.h>
40 #include <WebCore/SessionID.h>
41 #include <WebCore/TextEncoding.h>
42 #include <wtf/MainThread.h>
43
44 using namespace WebCore;
45
46 namespace WebKit {
47
48 DatabaseProcess& DatabaseProcess::singleton()
49 {
50     static NeverDestroyed<DatabaseProcess> databaseProcess;
51     return databaseProcess;
52 }
53
54 DatabaseProcess::DatabaseProcess()
55     : m_queue(WorkQueue::create("com.apple.WebKit.DatabaseProcess"))
56 {
57     // Make sure the UTF8Encoding encoding and the text encoding maps have been built on the main thread before a background thread needs it.
58     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=135365 - Need a more explicit way of doing this besides accessing the UTF8Encoding.
59     UTF8Encoding();
60 }
61
62 DatabaseProcess::~DatabaseProcess()
63 {
64 }
65
66 void DatabaseProcess::initializeConnection(IPC::Connection* connection)
67 {
68     ChildProcess::initializeConnection(connection);
69 }
70
71 bool DatabaseProcess::shouldTerminate()
72 {
73     return true;
74 }
75
76 void DatabaseProcess::didClose(IPC::Connection&)
77 {
78     RunLoop::current().stop();
79 }
80
81 void DatabaseProcess::didReceiveMessage(IPC::Connection& connection, IPC::MessageDecoder& decoder)
82 {
83     if (messageReceiverMap().dispatchMessage(connection, decoder))
84         return;
85
86     if (decoder.messageReceiverName() == Messages::DatabaseProcess::messageReceiverName()) {
87         didReceiveDatabaseProcessMessage(connection, decoder);
88         return;
89     }
90 }
91
92 void DatabaseProcess::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
93 {
94     RunLoop::current().stop();
95 }
96
97 #if ENABLE(INDEXED_DATABASE)
98 IDBServer::IDBServer& DatabaseProcess::idbServer()
99 {
100     if (!m_idbServer)
101         m_idbServer = IDBServer::IDBServer::create(indexedDatabaseDirectory());
102
103     return *m_idbServer;
104 }
105 #endif
106
107 void DatabaseProcess::initializeDatabaseProcess(const DatabaseProcessCreationParameters& parameters)
108 {
109 #if ENABLE(INDEXED_DATABASE)
110     // *********
111     // IMPORTANT: Do not change the directory structure for indexed databases on disk without first consulting a reviewer from Apple (<rdar://problem/17454712>)
112     // *********
113
114     m_indexedDatabaseDirectory = parameters.indexedDatabaseDirectory;
115     SandboxExtension::consumePermanently(parameters.indexedDatabaseDirectoryExtensionHandle);
116
117     ensureIndexedDatabaseRelativePathExists(StringImpl::empty());
118 #endif
119 }
120
121 #if ENABLE(INDEXED_DATABASE)
122 void DatabaseProcess::ensureIndexedDatabaseRelativePathExists(const String& relativePath)
123 {
124     postDatabaseTask(createCrossThreadTask(*this, &DatabaseProcess::ensurePathExists, absoluteIndexedDatabasePathFromDatabaseRelativePath(relativePath)));
125 }
126 #endif
127
128 void DatabaseProcess::ensurePathExists(const String& path)
129 {
130     ASSERT(!RunLoop::isMain());
131
132     if (!makeAllDirectories(path))
133         LOG_ERROR("Failed to make all directories for path '%s'", path.utf8().data());
134 }
135
136 #if ENABLE(INDEXED_DATABASE)
137 String DatabaseProcess::absoluteIndexedDatabasePathFromDatabaseRelativePath(const String& relativePath)
138 {
139     // FIXME: pathByAppendingComponent() was originally designed to append individual atomic components.
140     // We don't have a function designed to append a multi-component subpath, but we should.
141     return pathByAppendingComponent(m_indexedDatabaseDirectory, relativePath);
142 }
143 #endif
144
145 void DatabaseProcess::postDatabaseTask(std::unique_ptr<CrossThreadTask> task)
146 {
147     ASSERT(RunLoop::isMain());
148
149     LockHolder locker(m_databaseTaskMutex);
150
151     m_databaseTasks.append(WTFMove(task));
152
153     m_queue->dispatch([this] {
154         performNextDatabaseTask();
155     });
156 }
157
158 void DatabaseProcess::performNextDatabaseTask()
159 {
160     ASSERT(!RunLoop::isMain());
161
162     std::unique_ptr<CrossThreadTask> task;
163     {
164         LockHolder locker(m_databaseTaskMutex);
165         ASSERT(!m_databaseTasks.isEmpty());
166         task = m_databaseTasks.takeFirst();
167     }
168
169     task->performTask();
170 }
171
172 void DatabaseProcess::createDatabaseToWebProcessConnection()
173 {
174 #if USE(UNIX_DOMAIN_SOCKETS)
175     IPC::Connection::SocketPair socketPair = IPC::Connection::createPlatformConnection();
176     m_databaseToWebProcessConnections.append(DatabaseToWebProcessConnection::create(socketPair.server));
177     parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidCreateDatabaseToWebProcessConnection(IPC::Attachment(socketPair.client)), 0);
178 #elif OS(DARWIN)
179     // Create the listening port.
180     mach_port_t listeningPort;
181     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
182
183     // Create a listening connection.
184     RefPtr<DatabaseToWebProcessConnection> connection = DatabaseToWebProcessConnection::create(IPC::Connection::Identifier(listeningPort));
185     m_databaseToWebProcessConnections.append(connection.release());
186
187     IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
188     parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidCreateDatabaseToWebProcessConnection(clientPort), 0);
189 #else
190     notImplemented();
191 #endif
192 }
193
194 void DatabaseProcess::fetchWebsiteData(SessionID, uint64_t websiteDataTypes, uint64_t callbackID)
195 {
196     struct CallbackAggregator final : public ThreadSafeRefCounted<CallbackAggregator> {
197         explicit CallbackAggregator(std::function<void (WebsiteData)> completionHandler)
198             : m_completionHandler(WTFMove(completionHandler))
199         {
200         }
201
202         ~CallbackAggregator()
203         {
204             ASSERT(RunLoop::isMain());
205
206             auto completionHandler = WTFMove(m_completionHandler);
207             auto websiteData = WTFMove(m_websiteData);
208
209             RunLoop::main().dispatch([completionHandler, websiteData] {
210                 completionHandler(websiteData);
211             });
212         }
213
214         std::function<void (WebsiteData)> m_completionHandler;
215         WebsiteData m_websiteData;
216     };
217
218     RefPtr<CallbackAggregator> callbackAggregator = adoptRef(new CallbackAggregator([this, callbackID](WebsiteData websiteData) {
219         parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidFetchWebsiteData(callbackID, websiteData), 0);
220     }));
221
222 #if ENABLE(INDEXED_DATABASE)
223     if (websiteDataTypes & WebsiteDataTypeIndexedDBDatabases) {
224         // FIXME: Pick the right database store based on the session ID.
225         postDatabaseTask(std::make_unique<CrossThreadTask>([callbackAggregator, websiteDataTypes, this] {
226
227             Vector<RefPtr<SecurityOrigin>> securityOrigins = indexedDatabaseOrigins();
228
229             RunLoop::main().dispatch([callbackAggregator, securityOrigins] {
230                 for (const auto& securityOrigin : securityOrigins)
231                     callbackAggregator->m_websiteData.entries.append(WebsiteData::Entry { securityOrigin, WebsiteDataTypeIndexedDBDatabases });
232             });
233         }));
234     }
235 #endif
236 }
237
238 void DatabaseProcess::deleteWebsiteData(WebCore::SessionID, uint64_t websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID)
239 {
240     struct CallbackAggregator final : public ThreadSafeRefCounted<CallbackAggregator> {
241         explicit CallbackAggregator(std::function<void ()> completionHandler)
242             : m_completionHandler(WTFMove(completionHandler))
243         {
244         }
245
246         ~CallbackAggregator()
247         {
248             ASSERT(RunLoop::isMain());
249
250             RunLoop::main().dispatch(WTFMove(m_completionHandler));
251         }
252
253         std::function<void ()> m_completionHandler;
254     };
255
256     RefPtr<CallbackAggregator> callbackAggregator = adoptRef(new CallbackAggregator([this, callbackID]() {
257         parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidDeleteWebsiteData(callbackID), 0);
258     }));
259
260 #if ENABLE(INDEXED_DATABASE)
261     if (websiteDataTypes & WebsiteDataTypeIndexedDBDatabases) {
262         postDatabaseTask(std::make_unique<CrossThreadTask>([this, callbackAggregator, modifiedSince] {
263
264             deleteIndexedDatabaseEntriesModifiedSince(modifiedSince);
265             RunLoop::main().dispatch([callbackAggregator] { });
266         }));
267     }
268 #endif
269 }
270
271 void DatabaseProcess::deleteWebsiteDataForOrigins(WebCore::SessionID, uint64_t websiteDataTypes, const Vector<SecurityOriginData>& securityOriginDatas, uint64_t callbackID)
272 {
273     struct CallbackAggregator final : public ThreadSafeRefCounted<CallbackAggregator> {
274         explicit CallbackAggregator(std::function<void ()> completionHandler)
275             : m_completionHandler(WTFMove(completionHandler))
276         {
277         }
278
279         ~CallbackAggregator()
280         {
281             ASSERT(RunLoop::isMain());
282
283             RunLoop::main().dispatch(WTFMove(m_completionHandler));
284         }
285
286         std::function<void ()> m_completionHandler;
287     };
288
289     RefPtr<CallbackAggregator> callbackAggregator = adoptRef(new CallbackAggregator([this, callbackID]() {
290         parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidDeleteWebsiteDataForOrigins(callbackID), 0);
291     }));
292
293 #if ENABLE(INDEXED_DATABASE)
294     if (websiteDataTypes & WebsiteDataTypeIndexedDBDatabases) {
295         Vector<RefPtr<WebCore::SecurityOrigin>> securityOrigins;
296         for (const auto& securityOriginData : securityOriginDatas)
297             securityOrigins.append(securityOriginData.securityOrigin());
298
299         postDatabaseTask(std::make_unique<CrossThreadTask>([this, securityOrigins, callbackAggregator] {
300             deleteIndexedDatabaseEntriesForOrigins(securityOrigins);
301
302             RunLoop::main().dispatch([callbackAggregator] { });
303         }));
304     }
305 #endif
306 }
307
308 #if ENABLE(INDEXED_DATABASE)
309 Vector<RefPtr<WebCore::SecurityOrigin>> DatabaseProcess::indexedDatabaseOrigins()
310 {
311     if (m_indexedDatabaseDirectory.isEmpty())
312         return { };
313
314     Vector<RefPtr<WebCore::SecurityOrigin>> securityOrigins;
315     for (auto& originPath : listDirectory(m_indexedDatabaseDirectory, "*")) {
316         String databaseIdentifier = pathGetFileName(originPath);
317
318         if (auto securityOrigin = SecurityOrigin::maybeCreateFromDatabaseIdentifier(databaseIdentifier))
319             securityOrigins.append(WTFMove(securityOrigin));
320     }
321
322     return securityOrigins;
323 }
324 #endif
325
326 #if ENABLE(INDEXED_DATABASE)
327 static void removeAllDatabasesForOriginPath(const String& originPath, std::chrono::system_clock::time_point modifiedSince)
328 {
329     // FIXME: We should also close/invalidate any live handles to the database files we are about to delete.
330     // Right now:
331     //     - For read-only operations, they will continue functioning as normal on the unlinked file.
332     //     - For write operations, they will start producing errors as SQLite notices the missing backing store.
333     // This is tracked by https://bugs.webkit.org/show_bug.cgi?id=135347
334
335     Vector<String> databasePaths = listDirectory(originPath, "*");
336
337     for (auto& databasePath : databasePaths) {
338         String databaseFile = pathByAppendingComponent(databasePath, "IndexedDB.sqlite3");
339
340         if (!fileExists(databaseFile))
341             continue;
342
343         if (modifiedSince > std::chrono::system_clock::time_point::min()) {
344             time_t modificationTime;
345             if (!getFileModificationTime(databaseFile, modificationTime))
346                 continue;
347
348             if (std::chrono::system_clock::from_time_t(modificationTime) < modifiedSince)
349                 continue;
350         }
351
352         deleteFile(databaseFile);
353         deleteEmptyDirectory(databasePath);
354     }
355
356     deleteEmptyDirectory(originPath);
357 }
358
359 void DatabaseProcess::deleteIndexedDatabaseEntriesForOrigins(const Vector<RefPtr<WebCore::SecurityOrigin>>& securityOrigins)
360 {
361     if (m_indexedDatabaseDirectory.isEmpty())
362         return;
363
364     for (const auto& securityOrigin : securityOrigins) {
365         String originPath = pathByAppendingComponent(m_indexedDatabaseDirectory, securityOrigin->databaseIdentifier());
366
367         removeAllDatabasesForOriginPath(originPath, std::chrono::system_clock::time_point::min());
368     }
369 }
370
371 void DatabaseProcess::deleteIndexedDatabaseEntriesModifiedSince(std::chrono::system_clock::time_point modifiedSince)
372 {
373     if (m_indexedDatabaseDirectory.isEmpty())
374         return;
375
376     Vector<String> originPaths = listDirectory(m_indexedDatabaseDirectory, "*");
377     for (auto& originPath : originPaths)
378         removeAllDatabasesForOriginPath(originPath, modifiedSince);
379 }
380 #endif
381
382 #if !PLATFORM(COCOA)
383 void DatabaseProcess::initializeProcess(const ChildProcessInitializationParameters&)
384 {
385 }
386
387 void DatabaseProcess::initializeProcessName(const ChildProcessInitializationParameters&)
388 {
389 }
390
391 void DatabaseProcess::initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&)
392 {
393 }
394 #endif
395
396 } // namespace WebKit
397
398 #endif // ENABLE(DATABASE_PROCESS)