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