[WKWebsiteDataStore removeDataOfTypes:...] doesn't work for IndexedDB if the data...
[WebKit-https.git] / Source / WebKit2 / DatabaseProcess / DatabaseProcess.cpp
index 05446f9..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
 #include "DatabaseProcessMessages.h"
 #include "DatabaseProcessProxyMessages.h"
 #include "DatabaseToWebProcessConnection.h"
-#include "WebCrossThreadCopier.h"
+#include "WebCoreArgumentCoders.h"
 #include "WebsiteData.h"
-#include <WebCore/CrossThreadTask.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;
@@ -75,10 +76,10 @@ bool DatabaseProcess::shouldTerminate()
 
 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;
@@ -89,42 +90,43 @@ void DatabaseProcess::didReceiveMessage(IPC::Connection& connection, IPC::Messag
     }
 }
 
-void DatabaseProcess::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
-{
-    RunLoop::current().stop();
-}
-
 #if ENABLE(INDEXED_DATABASE)
-IDBServer::IDBServer& DatabaseProcess::idbServer()
+IDBServer::IDBServer& DatabaseProcess::idbServer(SessionID sessionID)
 {
-    if (!m_idbServer)
-        m_idbServer = IDBServer::IDBServer::create(indexedDatabaseDirectory(), DatabaseProcess::singleton());
+    auto addResult = m_idbServers.add(sessionID, nullptr);
+    if (!addResult.isNewEntry) {
+        ASSERT(addResult.iterator->value);
+        return *addResult.iterator->value;
+    }
+
+    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());
 
-    return *m_idbServer;
+    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;
+    auto addResult = m_idbDatabasePaths.add(parameters.sessionID, String());
+    if (!addResult.isNewEntry)
+        return;
+
+    addResult.iterator->value = parameters.indexedDatabaseDirectory;
     SandboxExtension::consumePermanently(parameters.indexedDatabaseDirectoryExtensionHandle);
 
-    ensureIndexedDatabaseRelativePathExists(StringImpl::empty());
+    postDatabaseTask(createCrossThreadTask(*this, &DatabaseProcess::ensurePathExists, parameters.indexedDatabaseDirectory));
 #endif
 }
 
-#if ENABLE(INDEXED_DATABASE)
-void DatabaseProcess::ensureIndexedDatabaseRelativePathExists(const String& relativePath)
-{
-    postDatabaseTask(createCrossThreadTask(*this, &DatabaseProcess::ensurePathExists, absoluteIndexedDatabasePathFromDatabaseRelativePath(relativePath)));
-}
-#endif
-
 void DatabaseProcess::ensurePathExists(const String& path)
 {
     ASSERT(!RunLoop::isMain());
@@ -133,16 +135,7 @@ void DatabaseProcess::ensurePathExists(const String& path)
         LOG_ERROR("Failed to make all directories for path '%s'", path.utf8().data());
 }
 
-#if ENABLE(INDEXED_DATABASE)
-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);
-}
-#endif
-
-void DatabaseProcess::postDatabaseTask(std::unique_ptr<CrossThreadTask> task)
+void DatabaseProcess::postDatabaseTask(CrossThreadTask&& task)
 {
     ASSERT(RunLoop::isMain());
 
@@ -159,14 +152,14 @@ void DatabaseProcess::performNextDatabaseTask()
 {
     ASSERT(!RunLoop::isMain());
 
-    std::unique_ptr<CrossThreadTask> task;
+    CrossThreadTask task;
     {
         LockHolder locker(m_databaseTaskMutex);
         ASSERT(!m_databaseTasks.isEmpty());
         task = m_databaseTasks.takeFirst();
     }
 
-    task->performTask();
+    task.performTask();
 }
 
 void DatabaseProcess::createDatabaseToWebProcessConnection()
@@ -181,8 +174,8 @@ void DatabaseProcess::createDatabaseToWebProcessConnection()
     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,121 +184,67 @@ void DatabaseProcess::createDatabaseToWebProcessConnection()
 #endif
 }
 
-void DatabaseProcess::fetchWebsiteData(SessionID, OptionSet<WebsiteDataType> websiteDataTypes, uint64_t callbackID)
+void DatabaseProcess::fetchWebsiteData(SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, uint64_t callbackID)
 {
-    struct CallbackAggregator final : public ThreadSafeRefCounted<CallbackAggregator> {
-        explicit CallbackAggregator(std::function<void (WebsiteData)> completionHandler)
-            : m_completionHandler(WTFMove(completionHandler))
-        {
-        }
-
-        ~CallbackAggregator()
-        {
-            ASSERT(RunLoop::isMain());
-
-            auto completionHandler = WTFMove(m_completionHandler);
-            auto websiteData = WTFMove(m_websiteData);
-
-            RunLoop::main().dispatch([completionHandler, websiteData] {
-                completionHandler(websiteData);
-            });
-        }
-
-        std::function<void (WebsiteData)> m_completionHandler;
-        WebsiteData m_websiteData;
+#if ENABLE(INDEXED_DATABASE)
+    auto completionHandler = [this, callbackID](const WebsiteData& websiteData) {
+        parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidFetchWebsiteData(callbackID, websiteData), 0);
     };
 
-    RefPtr<CallbackAggregator> callbackAggregator = adoptRef(new CallbackAggregator([this, callbackID](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;
+    }
 
-#if ENABLE(INDEXED_DATABASE)
     if (websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
         // FIXME: Pick the right database store based on the session ID.
-        postDatabaseTask(std::make_unique<CrossThreadTask>([callbackAggregator, websiteDataTypes, this] {
-
-            Vector<RefPtr<SecurityOrigin>> securityOrigins = indexedDatabaseOrigins();
-
-            RunLoop::main().dispatch([callbackAggregator, securityOrigins] {
+        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)
-                    callbackAggregator->m_websiteData.entries.append(WebsiteData::Entry { securityOrigin, WebsiteDataType::IndexedDBDatabases, 0 });
+                    websiteData.entries.append({ securityOrigin, WebsiteDataType::IndexedDBDatabases, 0 });
+
+                completionHandler(websiteData);
             });
         }));
     }
 #endif
 }
 
-void DatabaseProcess::deleteWebsiteData(WebCore::SessionID, OptionSet<WebsiteDataType> websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID)
+void DatabaseProcess::deleteWebsiteData(WebCore::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID)
 {
-    struct CallbackAggregator final : public ThreadSafeRefCounted<CallbackAggregator> {
-        explicit CallbackAggregator(std::function<void ()> completionHandler)
-            : m_completionHandler(WTFMove(completionHandler))
-        {
-        }
-
-        ~CallbackAggregator()
-        {
-            ASSERT(RunLoop::isMain());
-
-            RunLoop::main().dispatch(WTFMove(m_completionHandler));
-        }
-
-        std::function<void ()> m_completionHandler;
-    };
-
-    RefPtr<CallbackAggregator> callbackAggregator = adoptRef(new CallbackAggregator([this, callbackID]() {
-        parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidDeleteWebsiteData(callbackID), 0);
-    }));
-
 #if ENABLE(INDEXED_DATABASE)
-    if (websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
-        postDatabaseTask(std::make_unique<CrossThreadTask>([this, callbackAggregator, modifiedSince] {
+    auto completionHandler = [this, callbackID]() {
+        parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidDeleteWebsiteData(callbackID), 0);
+    };
 
-            deleteIndexedDatabaseEntriesModifiedSince(modifiedSince);
-            RunLoop::main().dispatch([callbackAggregator] { });
-        }));
+    if (!websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
+        completionHandler();
+        return;
     }
+
+    idbServer(sessionID).closeAndDeleteDatabasesModifiedSince(modifiedSince, WTFMove(completionHandler));
 #endif
 }
 
-void DatabaseProcess::deleteWebsiteDataForOrigins(WebCore::SessionID, OptionSet<WebsiteDataType> websiteDataTypes, const Vector<SecurityOriginData>& securityOriginDatas, uint64_t callbackID)
+void DatabaseProcess::deleteWebsiteDataForOrigins(WebCore::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, const Vector<SecurityOriginData>& securityOriginDatas, uint64_t callbackID)
 {
-    struct CallbackAggregator final : public ThreadSafeRefCounted<CallbackAggregator> {
-        explicit CallbackAggregator(std::function<void ()> completionHandler)
-            : m_completionHandler(WTFMove(completionHandler))
-        {
-        }
-
-        ~CallbackAggregator()
-        {
-            ASSERT(RunLoop::isMain());
-
-            RunLoop::main().dispatch(WTFMove(m_completionHandler));
-        }
-
-        std::function<void ()> m_completionHandler;
-    };
-
-    RefPtr<CallbackAggregator> callbackAggregator = adoptRef(new CallbackAggregator([this, callbackID]() {
-        parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidDeleteWebsiteDataForOrigins(callbackID), 0);
-    }));
-
 #if ENABLE(INDEXED_DATABASE)
-    if (websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
-        Vector<RefPtr<WebCore::SecurityOrigin>> securityOrigins;
-        for (const auto& securityOriginData : securityOriginDatas)
-            securityOrigins.append(securityOriginData.securityOrigin());
-
-        postDatabaseTask(std::make_unique<CrossThreadTask>([this, securityOrigins, callbackAggregator] {
-            deleteIndexedDatabaseEntriesForOrigins(securityOrigins);
+    auto completionHandler = [this, callbackID]() {
+        parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidDeleteWebsiteDataForOrigins(callbackID), 0);
+    };
 
-            RunLoop::main().dispatch([callbackAggregator] { });
-        }));
+    if (!websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
+        completionHandler();
+        return;
     }
+
+    idbServer(sessionID).closeAndDeleteDatabasesForOrigins(securityOriginDatas, WTFMove(completionHandler));
 #endif
 }
 
-#if ENABLE(INDEXED_DATABASE)
+#if ENABLE(SANDBOX_EXTENSIONS)
 void DatabaseProcess::grantSandboxExtensionsForBlobs(const Vector<String>& paths, const SandboxExtension::HandleArray& handles)
 {
     ASSERT(paths.size() == handles.size());
@@ -315,7 +254,9 @@ void DatabaseProcess::grantSandboxExtensionsForBlobs(const Vector<String>& paths
         ASSERT_UNUSED(result, result.isNewEntry);
     }
 }
+#endif
 
+#if ENABLE(INDEXED_DATABASE)
 void DatabaseProcess::prepareForAccessToTemporaryFile(const String& path)
 {
     if (auto extension = m_blobTemporaryFileSandboxExtensions.get(path))
@@ -333,76 +274,38 @@ void DatabaseProcess::accessToTemporaryFileComplete(const String& path)
         extension->revoke();
 }
 
-Vector<RefPtr<WebCore::SecurityOrigin>> DatabaseProcess::indexedDatabaseOrigins()
+Vector<WebCore::SecurityOriginData> DatabaseProcess::indexedDatabaseOrigins(const String& path)
 {
-    if (m_indexedDatabaseDirectory.isEmpty())
+    if (path.isEmpty())
         return { };
 
-    Vector<RefPtr<WebCore::SecurityOrigin>> securityOrigins;
-    for (auto& originPath : listDirectory(m_indexedDatabaseDirectory, "*")) {
+    Vector<WebCore::SecurityOriginData> securityOrigins;
+    for (auto& originPath : listDirectory(path, "*")) {
         String databaseIdentifier = pathGetFileName(originPath);
 
-        if (auto securityOrigin = SecurityOrigin::maybeCreateFromDatabaseIdentifier(databaseIdentifier))
-            securityOrigins.append(WTFMove(securityOrigin));
+        if (auto securityOrigin = SecurityOriginData::fromDatabaseIdentifier(databaseIdentifier))
+            securityOrigins.append(WTFMove(*securityOrigin));
     }
 
     return securityOrigins;
 }
-#endif
-
-#if ENABLE(INDEXED_DATABASE)
-static void removeAllDatabasesForOriginPath(const String& originPath, std::chrono::system_clock::time_point modifiedSince)
-{
-    // 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, "*");
-
-    for (auto& databasePath : databasePaths) {
-        String databaseFile = pathByAppendingComponent(databasePath, "IndexedDB.sqlite3");
-
-        if (!fileExists(databaseFile))
-            continue;
-
-        if (modifiedSince > std::chrono::system_clock::time_point::min()) {
-            time_t modificationTime;
-            if (!getFileModificationTime(databaseFile, modificationTime))
-                continue;
 
-            if (std::chrono::system_clock::from_time_t(modificationTime) < modifiedSince)
-                continue;
-        }
-
-        deleteFile(databaseFile);
-        deleteEmptyDirectory(databasePath);
-    }
-
-    deleteEmptyDirectory(originPath);
-}
+#endif
 
-void DatabaseProcess::deleteIndexedDatabaseEntriesForOrigins(const Vector<RefPtr<WebCore::SecurityOrigin>>& securityOrigins)
+#if ENABLE(SANDBOX_EXTENSIONS)
+void DatabaseProcess::getSandboxExtensionsForBlobFiles(const Vector<String>& filenames, std::function<void (SandboxExtension::HandleArray&&)> completionHandler)
 {
-    if (m_indexedDatabaseDirectory.isEmpty())
-        return;
-
-    for (const auto& securityOrigin : securityOrigins) {
-        String originPath = pathByAppendingComponent(m_indexedDatabaseDirectory, securityOrigin->databaseIdentifier());
+    static uint64_t lastRequestID;
 
-        removeAllDatabasesForOriginPath(originPath, std::chrono::system_clock::time_point::min());
-    }
+    uint64_t requestID = ++lastRequestID;
+    m_sandboxExtensionForBlobsCompletionHandlers.set(requestID, completionHandler);
+    parentProcessConnection()->send(Messages::DatabaseProcessProxy::GetSandboxExtensionsForBlobFiles(requestID, filenames), 0);
 }
 
-void DatabaseProcess::deleteIndexedDatabaseEntriesModifiedSince(std::chrono::system_clock::time_point modifiedSince)
+void DatabaseProcess::didGetSandboxExtensionsForBlobFiles(uint64_t requestID, SandboxExtension::HandleArray&& handles)
 {
-    if (m_indexedDatabaseDirectory.isEmpty())
-        return;
-
-    Vector<String> originPaths = listDirectory(m_indexedDatabaseDirectory, "*");
-    for (auto& originPath : originPaths)
-        removeAllDatabasesForOriginPath(originPath, modifiedSince);
+    if (auto handler = m_sandboxExtensionForBlobsCompletionHandlers.take(requestID))
+        handler(WTFMove(handles));
 }
 #endif