DatabaseProcess.cpp [plain text]
#include "config.h"
#include "DatabaseProcess.h"
#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 <WebCore/FileSystem.h>
#include <WebCore/TextEncoding.h>
#include <wtf/MainThread.h>
using namespace WebCore;
namespace WebKit {
DatabaseProcess& DatabaseProcess::shared()
{
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))
{
UTF8Encoding();
}
DatabaseProcess::~DatabaseProcess()
{
}
void DatabaseProcess::initializeConnection(IPC::Connection* connection)
{
ChildProcess::initializeConnection(connection);
}
bool DatabaseProcess::shouldTerminate()
{
return true;
}
void DatabaseProcess::didClose(IPC::Connection*)
{
RunLoop::current().stop();
}
void DatabaseProcess::didReceiveMessage(IPC::Connection* connection, IPC::MessageDecoder& decoder)
{
if (messageReceiverMap().dispatchMessage(connection, decoder))
return;
if (decoder.messageReceiverName() == Messages::DatabaseProcess::messageReceiverName()) {
didReceiveDatabaseProcessMessage(connection, decoder);
return;
}
}
void DatabaseProcess::didReceiveInvalidMessage(IPC::Connection*, IPC::StringReference, IPC::StringReference)
{
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();
}
void DatabaseProcess::removeUniqueIDBDatabase(const UniqueIDBDatabase& database)
{
const UniqueIDBDatabaseIdentifier& identifier = database.identifier();
ASSERT(m_idbDatabases.contains(identifier));
m_idbDatabases.remove(identifier);
}
void DatabaseProcess::initializeDatabaseProcess(const DatabaseProcessCreationParameters& parameters)
{
m_indexedDatabaseDirectory = parameters.indexedDatabaseDirectory;
SandboxExtension::consumePermanently(parameters.indexedDatabaseDirectoryExtensionHandle);
ensureIndexedDatabaseRelativePathExists(StringImpl::empty());
}
void DatabaseProcess::ensureIndexedDatabaseRelativePathExists(const String& relativePath)
{
postDatabaseTask(createAsyncTask(*this, &DatabaseProcess::ensurePathExists, absoluteIndexedDatabasePathFromDatabaseRelativePath(relativePath)));
}
void DatabaseProcess::ensurePathExists(const String& path)
{
ASSERT(!RunLoop::isMain());
if (!makeAllDirectories(path))
LOG_ERROR("Failed to make all directories for path '%s'", path.utf8().data());
}
String DatabaseProcess::absoluteIndexedDatabasePathFromDatabaseRelativePath(const String& relativePath)
{
return pathByAppendingComponent(m_indexedDatabaseDirectory, relativePath);
}
void DatabaseProcess::postDatabaseTask(std::unique_ptr<AsyncTask> task)
{
ASSERT(RunLoop::isMain());
MutexLocker locker(m_databaseTaskMutex);
m_databaseTasks.append(WTF::move(task));
m_queue->dispatch(bind(&DatabaseProcess::performNextDatabaseTask, this));
}
void DatabaseProcess::performNextDatabaseTask()
{
ASSERT(!RunLoop::isMain());
std::unique_ptr<AsyncTask> task;
{
MutexLocker locker(m_databaseTaskMutex);
ASSERT(!m_databaseTasks.isEmpty());
task = m_databaseTasks.takeFirst();
}
task->performTask();
}
void DatabaseProcess::createDatabaseToWebProcessConnection()
{
#if OS(DARWIN)
mach_port_t listeningPort;
mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
RefPtr<DatabaseToWebProcessConnection> connection = DatabaseToWebProcessConnection::create(IPC::Connection::Identifier(listeningPort));
m_databaseToWebProcessConnections.append(connection.release());
IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidCreateDatabaseToWebProcessConnection(clientPort), 0);
#else
notImplemented();
#endif
}
void DatabaseProcess::getIndexedDatabaseOrigins(uint64_t callbackID)
{
postDatabaseTask(createAsyncTask(*this, &DatabaseProcess::doGetIndexedDatabaseOrigins, callbackID));
}
void DatabaseProcess::doGetIndexedDatabaseOrigins(uint64_t callbackID)
{
Vector<SecurityOriginData> results;
if (m_indexedDatabaseDirectory.isEmpty()) {
send(Messages::WebOriginDataManagerProxy::DidGetOrigins(results, callbackID), 0);
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()));
}
send(Messages::WebOriginDataManagerProxy::DidGetOrigins(results, callbackID), 0);
}
static void removeAllDatabasesForOriginPath(const String& originPath, double startDate, double endDate)
{
Vector<String> databasePaths = listDirectory(originPath, "*");
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);
}
deleteEmptyDirectory(originPath);
}
void DatabaseProcess::deleteIndexedDatabaseEntriesForOrigin(const SecurityOriginData& origin, uint64_t callbackID)
{
postDatabaseTask(createAsyncTask(*this, &DatabaseProcess::doDeleteIndexedDatabaseEntriesForOrigin, origin, callbackID));
}
void DatabaseProcess::doDeleteIndexedDatabaseEntriesForOrigin(const SecurityOriginData& originData, uint64_t callbackID)
{
if (m_indexedDatabaseDirectory.isEmpty()) {
send(Messages::WebOriginDataManagerProxy::DidDeleteEntries(callbackID), 0);
return;
}
RefPtr<SecurityOrigin> origin = originData.securityOrigin();
String databaseIdentifier = origin->databaseIdentifier();
String originPath = pathByAppendingComponent(m_indexedDatabaseDirectory, databaseIdentifier);
removeAllDatabasesForOriginPath(originPath, std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());
send(Messages::WebOriginDataManagerProxy::DidDeleteEntries(callbackID), 0);
}
void DatabaseProcess::deleteIndexedDatabaseEntriesModifiedBetweenDates(double startDate, double endDate, uint64_t callbackID)
{
postDatabaseTask(createAsyncTask(*this, &DatabaseProcess::doDeleteIndexedDatabaseEntriesModifiedBetweenDates, startDate, endDate, callbackID));
}
void DatabaseProcess::doDeleteIndexedDatabaseEntriesModifiedBetweenDates(double startDate, double endDate, uint64_t callbackID)
{
if (m_indexedDatabaseDirectory.isEmpty()) {
send(Messages::WebOriginDataManagerProxy::DidDeleteEntries(callbackID), 0);
return;
}
Vector<String> originPaths = listDirectory(m_indexedDatabaseDirectory, "*");
for (auto& originPath : originPaths)
removeAllDatabasesForOriginPath(originPath, startDate, endDate);
send(Messages::WebOriginDataManagerProxy::DidDeleteEntries(callbackID), 0);
}
void DatabaseProcess::deleteAllIndexedDatabaseEntries(uint64_t callbackID)
{
postDatabaseTask(createAsyncTask(*this, &DatabaseProcess::doDeleteAllIndexedDatabaseEntries, callbackID));
}
void DatabaseProcess::doDeleteAllIndexedDatabaseEntries(uint64_t callbackID)
{
if (m_indexedDatabaseDirectory.isEmpty()) {
send(Messages::WebOriginDataManagerProxy::DidDeleteAllEntries(callbackID), 0);
return;
}
Vector<String> originPaths = listDirectory(m_indexedDatabaseDirectory, "*");
for (auto& originPath : originPaths)
removeAllDatabasesForOriginPath(originPath, std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max());
send(Messages::WebOriginDataManagerProxy::DidDeleteAllEntries(callbackID), 0);
}
#if !PLATFORM(COCOA)
void DatabaseProcess::initializeProcess(const ChildProcessInitializationParameters&)
{
}
void DatabaseProcess::initializeProcessName(const ChildProcessInitializationParameters&)
{
}
void DatabaseProcess::initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&)
{
}
#endif
}
#endif // ENABLE(DATABASE_PROCESS)