[WKWebsiteDataStore removeDataOfTypes:...] doesn't work for IndexedDB if the data...
[WebKit-https.git] / Source / WebKit2 / DatabaseProcess / DatabaseProcess.cpp
index 53b1e92..bffc382 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2013, 2014, 2015, 2016 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 
 #if ENABLE(DATABASE_PROCESS)
 
-#include "AsyncTask.h"
 #include "DatabaseProcessCreationParameters.h"
 #include "DatabaseProcessMessages.h"
 #include "DatabaseProcessProxyMessages.h"
 #include "DatabaseToWebProcessConnection.h"
-#include "UniqueIDBDatabase.h"
-#include "WebCrossThreadCopier.h"
-#include "WebOriginDataManager.h"
-#include "WebOriginDataManagerMessages.h"
-#include "WebOriginDataManagerProxyMessages.h"
+#include "WebCoreArgumentCoders.h"
+#include "WebsiteData.h"
 #include <WebCore/FileSystem.h>
+#include <WebCore/IDBKeyData.h>
+#include <WebCore/NotImplemented.h>
+#include <WebCore/SessionID.h>
 #include <WebCore/TextEncoding.h>
+#include <wtf/CrossThreadTask.h>
 #include <wtf/MainThread.h>
 
 using namespace WebCore;
 
 namespace WebKit {
 
-DatabaseProcess& DatabaseProcess::shared()
+DatabaseProcess& DatabaseProcess::singleton()
 {
     static NeverDestroyed<DatabaseProcess> databaseProcess;
     return databaseProcess;
 }
 
 DatabaseProcess::DatabaseProcess()
-    : m_queue(adoptRef(*WorkQueue::create("com.apple.WebKit.DatabaseProcess").leakRef()))
-    , m_webOriginDataManager(std::make_unique<WebOriginDataManager>(*this, *this))
+    : m_queue(WorkQueue::create("com.apple.WebKit.DatabaseProcess"))
 {
     // Make sure the UTF8Encoding encoding and the text encoding maps have been built on the main thread before a background thread needs it.
     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=135365 - Need a more explicit way of doing this besides accessing the UTF8Encoding.
@@ -75,12 +74,12 @@ bool DatabaseProcess::shouldTerminate()
     return true;
 }
 
-void DatabaseProcess::didClose(IPC::Connection*)
+void DatabaseProcess::didClose(IPC::Connection&)
 {
-    RunLoop::current().stop();
+    stopRunLoop();
 }
 
-void DatabaseProcess::didReceiveMessage(IPC::Connection* connection, IPC::MessageDecoder& decoder)
+void DatabaseProcess::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
 {
     if (messageReceiverMap().dispatchMessage(connection, decoder))
         return;
@@ -91,46 +90,41 @@ void DatabaseProcess::didReceiveMessage(IPC::Connection* connection, IPC::Messag
     }
 }
 
-void DatabaseProcess::didReceiveInvalidMessage(IPC::Connection*, IPC::StringReference, IPC::StringReference)
+#if ENABLE(INDEXED_DATABASE)
+IDBServer::IDBServer& DatabaseProcess::idbServer(SessionID sessionID)
 {
-    RunLoop::current().stop();
-}
-
-PassRefPtr<UniqueIDBDatabase> DatabaseProcess::getOrCreateUniqueIDBDatabase(const UniqueIDBDatabaseIdentifier& identifier)
-{
-    auto addResult = m_idbDatabases.add(identifier, nullptr);
-
-    if (!addResult.isNewEntry)
-        return addResult.iterator->value;
-
-    RefPtr<UniqueIDBDatabase> database = UniqueIDBDatabase::create(identifier);
-    addResult.iterator->value = database.get();
-    return database.release();
-}
+    auto addResult = m_idbServers.add(sessionID, nullptr);
+    if (!addResult.isNewEntry) {
+        ASSERT(addResult.iterator->value);
+        return *addResult.iterator->value;
+    }
 
-void DatabaseProcess::removeUniqueIDBDatabase(const UniqueIDBDatabase& database)
-{
-    const UniqueIDBDatabaseIdentifier& identifier = database.identifier();
-    ASSERT(m_idbDatabases.contains(identifier));
+    auto path = m_idbDatabasePaths.get(sessionID);
+    // There should already be a registered path for this SessionID.
+    // If there's not, then where did this SessionID come from?
+    ASSERT(!path.isEmpty());
 
-    m_idbDatabases.remove(identifier);
+    addResult.iterator->value = IDBServer::IDBServer::create(path, DatabaseProcess::singleton());
+    return *addResult.iterator->value;
 }
+#endif
 
-void DatabaseProcess::initializeDatabaseProcess(const DatabaseProcessCreationParameters& parameters)
+void DatabaseProcess::initializeWebsiteDataStore(const DatabaseProcessCreationParameters& parameters)
 {
+#if ENABLE(INDEXED_DATABASE)
     // *********
     // IMPORTANT: Do not change the directory structure for indexed databases on disk without first consulting a reviewer from Apple (<rdar://problem/17454712>)
     // *********
 
-    m_indexedDatabaseDirectory = parameters.indexedDatabaseDirectory;
-    SandboxExtension::consumePermanently(parameters.indexedDatabaseDirectoryExtensionHandle);
+    auto addResult = m_idbDatabasePaths.add(parameters.sessionID, String());
+    if (!addResult.isNewEntry)
+        return;
 
-    ensureIndexedDatabaseRelativePathExists(StringImpl::empty());
-}
+    addResult.iterator->value = parameters.indexedDatabaseDirectory;
+    SandboxExtension::consumePermanently(parameters.indexedDatabaseDirectoryExtensionHandle);
 
-void DatabaseProcess::ensureIndexedDatabaseRelativePathExists(const String& relativePath)
-{
-    postDatabaseTask(createAsyncTask(*this, &DatabaseProcess::ensurePathExists, absoluteIndexedDatabasePathFromDatabaseRelativePath(relativePath)));
+    postDatabaseTask(createCrossThreadTask(*this, &DatabaseProcess::ensurePathExists, parameters.indexedDatabaseDirectory));
+#endif
 }
 
 void DatabaseProcess::ensurePathExists(const String& path)
@@ -141,48 +135,47 @@ void DatabaseProcess::ensurePathExists(const String& path)
         LOG_ERROR("Failed to make all directories for path '%s'", path.utf8().data());
 }
 
-String DatabaseProcess::absoluteIndexedDatabasePathFromDatabaseRelativePath(const String& relativePath)
-{
-    // FIXME: pathByAppendingComponent() was originally designed to append individual atomic components.
-    // We don't have a function designed to append a multi-component subpath, but we should.
-    return pathByAppendingComponent(m_indexedDatabaseDirectory, relativePath);
-}
-
-void DatabaseProcess::postDatabaseTask(std::unique_ptr<AsyncTask> task)
+void DatabaseProcess::postDatabaseTask(CrossThreadTask&& task)
 {
     ASSERT(RunLoop::isMain());
 
-    MutexLocker locker(m_databaseTaskMutex);
+    LockHolder locker(m_databaseTaskMutex);
 
-    m_databaseTasks.append(WTF::move(task));
+    m_databaseTasks.append(WTFMove(task));
 
-    m_queue->dispatch(bind(&DatabaseProcess::performNextDatabaseTask, this));
+    m_queue->dispatch([this] {
+        performNextDatabaseTask();
+    });
 }
 
 void DatabaseProcess::performNextDatabaseTask()
 {
     ASSERT(!RunLoop::isMain());
 
-    std::unique_ptr<AsyncTask> task;
+    CrossThreadTask task;
     {
-        MutexLocker locker(m_databaseTaskMutex);
+        LockHolder locker(m_databaseTaskMutex);
         ASSERT(!m_databaseTasks.isEmpty());
         task = m_databaseTasks.takeFirst();
     }
 
-    task->performTask();
+    task.performTask();
 }
 
 void DatabaseProcess::createDatabaseToWebProcessConnection()
 {
-#if OS(DARWIN)
+#if USE(UNIX_DOMAIN_SOCKETS)
+    IPC::Connection::SocketPair socketPair = IPC::Connection::createPlatformConnection();
+    m_databaseToWebProcessConnections.append(DatabaseToWebProcessConnection::create(socketPair.server));
+    parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidCreateDatabaseToWebProcessConnection(IPC::Attachment(socketPair.client)), 0);
+#elif OS(DARWIN)
     // Create the listening port.
     mach_port_t listeningPort;
     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
 
     // Create a listening connection.
-    RefPtr<DatabaseToWebProcessConnection> connection = DatabaseToWebProcessConnection::create(IPC::Connection::Identifier(listeningPort));
-    m_databaseToWebProcessConnections.append(connection.release());
+    auto connection = DatabaseToWebProcessConnection::create(IPC::Connection::Identifier(listeningPort));
+    m_databaseToWebProcessConnections.append(WTFMove(connection));
 
     IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
     parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidCreateDatabaseToWebProcessConnection(clientPort), 0);
@@ -191,143 +184,130 @@ void DatabaseProcess::createDatabaseToWebProcessConnection()
 #endif
 }
 
-Vector<SecurityOriginData> DatabaseProcess::getIndexedDatabaseOrigins()
+void DatabaseProcess::fetchWebsiteData(SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, uint64_t callbackID)
 {
-    Vector<SecurityOriginData> results;
-
-    if (m_indexedDatabaseDirectory.isEmpty()) {
-        return results;
+#if ENABLE(INDEXED_DATABASE)
+    auto completionHandler = [this, callbackID](const WebsiteData& websiteData) {
+        parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidFetchWebsiteData(callbackID, websiteData), 0);
+    };
+
+    String path = m_idbDatabasePaths.get(sessionID);
+    if (path.isEmpty() || !websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
+        completionHandler({ });
+        return;
     }
 
-    Vector<String> originPaths = listDirectory(m_indexedDatabaseDirectory, "*");
-    for (auto& originPath : originPaths) {
-        URL url;
-        url.setProtocol(ASCIILiteral("file"));
-        url.setPath(originPath);
-
-        String databaseIdentifier = url.lastPathComponent();
-
-        RefPtr<SecurityOrigin> securityOrigin = SecurityOrigin::maybeCreateFromDatabaseIdentifier(databaseIdentifier);
-        if (!securityOrigin)
-            continue;
-
-        results.append(SecurityOriginData::fromSecurityOrigin(securityOrigin.get()));
+    if (websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
+        // FIXME: Pick the right database store based on the session ID.
+        postDatabaseTask(CrossThreadTask([this, completionHandler = WTFMove(completionHandler), path = WTFMove(path)]() mutable {
+            RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler), securityOrigins = indexedDatabaseOrigins(path)] {
+                WebsiteData websiteData;
+                for (const auto& securityOrigin : securityOrigins)
+                    websiteData.entries.append({ securityOrigin, WebsiteDataType::IndexedDBDatabases, 0 });
+
+                completionHandler(websiteData);
+            });
+        }));
     }
-
-    return results;
+#endif
 }
 
-static void removeAllDatabasesForOriginPath(const String& originPath, double startDate, double endDate)
+void DatabaseProcess::deleteWebsiteData(WebCore::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID)
 {
-    // FIXME: We should also close/invalidate any live handles to the database files we are about to delete.
-    // Right now:
-    //     - For read-only operations, they will continue functioning as normal on the unlinked file.
-    //     - For write operations, they will start producing errors as SQLite notices the missing backing store.
-    // This is tracked by https://bugs.webkit.org/show_bug.cgi?id=135347
-
-    Vector<String> databasePaths = listDirectory(originPath, "*");
+#if ENABLE(INDEXED_DATABASE)
+    auto completionHandler = [this, callbackID]() {
+        parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidDeleteWebsiteData(callbackID), 0);
+    };
 
-    for (auto& databasePath : databasePaths) {
-        String databaseFile = pathByAppendingComponent(databasePath, "IndexedDB.sqlite3");
-
-        if (!fileExists(databaseFile))
-            continue;
-
-        time_t modTime;
-        getFileModificationTime(databaseFile, modTime);
-
-        if (modTime < startDate || modTime > endDate)
-            continue;
-
-        deleteFile(databaseFile);
-        deleteEmptyDirectory(databasePath);
+    if (!websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
+        completionHandler();
+        return;
     }
 
-    deleteEmptyDirectory(originPath);
+    idbServer(sessionID).closeAndDeleteDatabasesModifiedSince(modifiedSince, WTFMove(completionHandler));
+#endif
 }
 
-void DatabaseProcess::deleteIndexedDatabaseEntriesForOrigin(const SecurityOriginData& originData)
+void DatabaseProcess::deleteWebsiteDataForOrigins(WebCore::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, const Vector<SecurityOriginData>& securityOriginDatas, uint64_t callbackID)
 {
-    if (m_indexedDatabaseDirectory.isEmpty())
-        return;
+#if ENABLE(INDEXED_DATABASE)
+    auto completionHandler = [this, callbackID]() {
+        parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidDeleteWebsiteDataForOrigins(callbackID), 0);
+    };
 
-    RefPtr<SecurityOrigin> origin = originData.securityOrigin();
-    String databaseIdentifier = origin->databaseIdentifier();
-    String originPath = pathByAppendingComponent(m_indexedDatabaseDirectory, databaseIdentifier);
+    if (!websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
+        completionHandler();
+        return;
+    }
 
-    removeAllDatabasesForOriginPath(originPath, std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());
+    idbServer(sessionID).closeAndDeleteDatabasesForOrigins(securityOriginDatas, WTFMove(completionHandler));
+#endif
 }
 
-void DatabaseProcess::deleteIndexedDatabaseEntriesModifiedBetweenDates(double startDate, double endDate)
+#if ENABLE(SANDBOX_EXTENSIONS)
+void DatabaseProcess::grantSandboxExtensionsForBlobs(const Vector<String>& paths, const SandboxExtension::HandleArray& handles)
 {
-    if (m_indexedDatabaseDirectory.isEmpty())
-        return;
+    ASSERT(paths.size() == handles.size());
 
-    Vector<String> originPaths = listDirectory(m_indexedDatabaseDirectory, "*");
-    for (auto& originPath : originPaths)
-        removeAllDatabasesForOriginPath(originPath, startDate, endDate);
+    for (size_t i = 0; i < paths.size(); ++i) {
+        auto result = m_blobTemporaryFileSandboxExtensions.add(paths[i], SandboxExtension::create(handles[i]));
+        ASSERT_UNUSED(result, result.isNewEntry);
+    }
 }
+#endif
 
-void DatabaseProcess::deleteAllIndexedDatabaseEntries()
+#if ENABLE(INDEXED_DATABASE)
+void DatabaseProcess::prepareForAccessToTemporaryFile(const String& path)
 {
-    if (m_indexedDatabaseDirectory.isEmpty())
-        return;
-
-    Vector<String> originPaths = listDirectory(m_indexedDatabaseDirectory, "*");
-    for (auto& originPath : originPaths)
-        removeAllDatabasesForOriginPath(originPath, std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());
+    if (auto extension = m_blobTemporaryFileSandboxExtensions.get(path))
+        extension->consume();
 }
 
-void DatabaseProcess::getOrigins(WKOriginDataTypes types, std::function<void(const Vector<SecurityOriginData>&)> completion)
+void DatabaseProcess::accessToTemporaryFileComplete(const String& path)
 {
-    if (!(types & kWKWebSQLDatabaseOriginData)) {
-        completion(Vector<SecurityOriginData>());
-        return;
-    }
+    // We've either hard linked the temporary blob file to the database directory, copied it there,
+    // or the transaction is being aborted.
+    // In any of those cases, we can delete the temporary blob file now.
+    deleteFile(path);
 
-    postDatabaseTask(std::make_unique<AsyncTask>([completion, this] {
-        completion(getIndexedDatabaseOrigins());
-    }));
+    if (auto extension = m_blobTemporaryFileSandboxExtensions.take(path))
+        extension->revoke();
 }
 
-void DatabaseProcess::deleteEntriesForOrigin(WKOriginDataTypes types, const SecurityOriginData& origin, std::function<void()> completion)
+Vector<WebCore::SecurityOriginData> DatabaseProcess::indexedDatabaseOrigins(const String& path)
 {
-    if (!(types & kWKWebSQLDatabaseOriginData)) {
-        completion();
-        return;
+    if (path.isEmpty())
+        return { };
+
+    Vector<WebCore::SecurityOriginData> securityOrigins;
+    for (auto& originPath : listDirectory(path, "*")) {
+        String databaseIdentifier = pathGetFileName(originPath);
+
+        if (auto securityOrigin = SecurityOriginData::fromDatabaseIdentifier(databaseIdentifier))
+            securityOrigins.append(WTFMove(*securityOrigin));
     }
 
-    postDatabaseTask(std::make_unique<AsyncTask>([this, origin, completion] {
-        deleteIndexedDatabaseEntriesForOrigin(origin);
-        completion();
-    }));
+    return securityOrigins;
 }
 
-void DatabaseProcess::deleteEntriesModifiedBetweenDates(WKOriginDataTypes types, double startDate, double endDate, std::function<void()> completion)
+#endif
+
+#if ENABLE(SANDBOX_EXTENSIONS)
+void DatabaseProcess::getSandboxExtensionsForBlobFiles(const Vector<String>& filenames, std::function<void (SandboxExtension::HandleArray&&)> completionHandler)
 {
-    if (!(types & kWKWebSQLDatabaseOriginData)) {
-        completion();
-        return;
-    }
+    static uint64_t lastRequestID;
 
-    postDatabaseTask(std::make_unique<AsyncTask>([this, startDate, endDate, completion] {
-        deleteIndexedDatabaseEntriesModifiedBetweenDates(startDate, endDate);
-        completion();
-    }));
+    uint64_t requestID = ++lastRequestID;
+    m_sandboxExtensionForBlobsCompletionHandlers.set(requestID, completionHandler);
+    parentProcessConnection()->send(Messages::DatabaseProcessProxy::GetSandboxExtensionsForBlobFiles(requestID, filenames), 0);
 }
 
-void DatabaseProcess::deleteAllEntries(WKOriginDataTypes types, std::function<void()> completion)
+void DatabaseProcess::didGetSandboxExtensionsForBlobFiles(uint64_t requestID, SandboxExtension::HandleArray&& handles)
 {
-    if (!(types & kWKWebSQLDatabaseOriginData)) {
-        completion();
-        return;
-    }
-
-    postDatabaseTask(std::make_unique<AsyncTask>([this, completion] {
-        deleteAllIndexedDatabaseEntries();
-        completion();
-    }));
+    if (auto handler = m_sandboxExtensionForBlobsCompletionHandlers.take(requestID))
+        handler(WTFMove(handles));
 }
+#endif
 
 #if !PLATFORM(COCOA)
 void DatabaseProcess::initializeProcess(const ChildProcessInitializationParameters&)