DatabaseManager.cpp [plain text]
#include "config.h"
#include "DatabaseManager.h"
#include "AbstractDatabaseServer.h"
#include "Database.h"
#include "DatabaseBackend.h"
#include "DatabaseBackendBase.h"
#include "DatabaseCallback.h"
#include "DatabaseContext.h"
#include "DatabaseServer.h"
#include "DatabaseTask.h"
#include "ExceptionCode.h"
#include "InspectorDatabaseInstrumentation.h"
#include "Logging.h"
#include "PlatformStrategies.h"
#include "ScriptController.h"
#include "ScriptExecutionContext.h"
#include "SecurityOrigin.h"
#include <wtf/NeverDestroyed.h>
namespace WebCore {
DatabaseManager::ProposedDatabase::ProposedDatabase(DatabaseManager& manager, SecurityOrigin* origin, const String& name, const String& displayName, unsigned long estimatedSize)
: m_manager(manager)
, m_origin(origin->isolatedCopy())
, m_details(name.isolatedCopy(), displayName.isolatedCopy(), estimatedSize, 0, 0, 0)
{
m_manager.addProposedDatabase(this);
}
DatabaseManager::ProposedDatabase::~ProposedDatabase()
{
m_manager.removeProposedDatabase(this);
}
DatabaseManager& DatabaseManager::singleton()
{
static NeverDestroyed<DatabaseManager> instance;
return instance;
}
DatabaseManager::DatabaseManager()
: m_server(new DatabaseServer)
, m_client(nullptr)
, m_databaseIsAvailable(true)
#if !ASSERT_DISABLED
, m_databaseContextRegisteredCount(0)
, m_databaseContextInstanceCount(0)
#endif
{
ASSERT(m_server); }
void DatabaseManager::initialize(const String& databasePath)
{
m_server->initialize(databasePath);
}
void DatabaseManager::setClient(DatabaseManagerClient* client)
{
m_client = client;
m_server->setClient(client);
}
String DatabaseManager::databaseDirectoryPath() const
{
return m_server->databaseDirectoryPath();
}
void DatabaseManager::setDatabaseDirectoryPath(const String& path)
{
m_server->setDatabaseDirectoryPath(path);
}
bool DatabaseManager::isAvailable()
{
return m_databaseIsAvailable;
}
void DatabaseManager::setIsAvailable(bool available)
{
m_databaseIsAvailable = available;
}
RefPtr<DatabaseContext> DatabaseManager::existingDatabaseContextFor(ScriptExecutionContext* context)
{
std::lock_guard<std::mutex> lock(m_mutex);
ASSERT(m_databaseContextRegisteredCount >= 0);
ASSERT(m_databaseContextInstanceCount >= 0);
ASSERT(m_databaseContextRegisteredCount <= m_databaseContextInstanceCount);
RefPtr<DatabaseContext> databaseContext = adoptRef(m_contextMap.get(context));
if (databaseContext) {
databaseContext->ref();
}
return databaseContext;
}
RefPtr<DatabaseContext> DatabaseManager::databaseContextFor(ScriptExecutionContext* context)
{
RefPtr<DatabaseContext> databaseContext = existingDatabaseContextFor(context);
if (!databaseContext)
databaseContext = adoptRef(*new DatabaseContext(context));
return databaseContext;
}
void DatabaseManager::registerDatabaseContext(DatabaseContext* databaseContext)
{
std::lock_guard<std::mutex> lock(m_mutex);
ScriptExecutionContext* context = databaseContext->scriptExecutionContext();
m_contextMap.set(context, databaseContext);
#if !ASSERT_DISABLED
m_databaseContextRegisteredCount++;
#endif
}
void DatabaseManager::unregisterDatabaseContext(DatabaseContext* databaseContext)
{
std::lock_guard<std::mutex> lock(m_mutex);
ScriptExecutionContext* context = databaseContext->scriptExecutionContext();
ASSERT(m_contextMap.get(context));
#if !ASSERT_DISABLED
m_databaseContextRegisteredCount--;
#endif
m_contextMap.remove(context);
}
#if !ASSERT_DISABLED
void DatabaseManager::didConstructDatabaseContext()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_databaseContextInstanceCount++;
}
void DatabaseManager::didDestructDatabaseContext()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_databaseContextInstanceCount--;
ASSERT(m_databaseContextRegisteredCount <= m_databaseContextInstanceCount);
}
#endif
ExceptionCode DatabaseManager::exceptionCodeForDatabaseError(DatabaseError error)
{
switch (error) {
case DatabaseError::None:
return 0;
case DatabaseError::DatabaseIsBeingDeleted:
case DatabaseError::DatabaseSizeExceededQuota:
case DatabaseError::DatabaseSizeOverflowed:
case DatabaseError::GenericSecurityError:
return SECURITY_ERR;
case DatabaseError::InvalidDatabaseState:
return INVALID_STATE_ERR;
}
ASSERT_NOT_REACHED();
return 0; }
static void logOpenDatabaseError(ScriptExecutionContext* context, const String& name)
{
UNUSED_PARAM(context);
UNUSED_PARAM(name);
LOG(StorageAPI, "Database %s for origin %s not allowed to be established", name.ascii().data(),
context->securityOrigin()->toString().ascii().data());
}
PassRefPtr<DatabaseBackendBase> DatabaseManager::openDatabaseBackend(ScriptExecutionContext* context, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize, bool setVersionInNewDatabase, DatabaseError& error, String& errorMessage)
{
ASSERT(error == DatabaseError::None);
RefPtr<DatabaseContext> databaseContext = databaseContextFor(context);
RefPtr<DatabaseBackendBase> backend = m_server->openDatabase(databaseContext, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, error, errorMessage);
if (!backend) {
ASSERT(error != DatabaseError::None);
switch (error) {
case DatabaseError::DatabaseIsBeingDeleted:
case DatabaseError::DatabaseSizeOverflowed:
case DatabaseError::GenericSecurityError:
logOpenDatabaseError(context, name);
return 0;
case DatabaseError::InvalidDatabaseState:
logErrorMessage(context, errorMessage);
return 0;
case DatabaseError::DatabaseSizeExceededQuota:
{
ProposedDatabase proposedDb(*this, context->securityOrigin(), name, displayName, estimatedSize);
databaseContext->databaseExceededQuota(name, proposedDb.details());
}
error = DatabaseError::None;
backend = m_server->openDatabase(databaseContext, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, error, errorMessage, AbstractDatabaseServer::RetryOpenDatabase);
break;
default:
ASSERT_NOT_REACHED();
}
if (!backend) {
ASSERT(error != DatabaseError::None);
if (error == DatabaseError::InvalidDatabaseState) {
logErrorMessage(context, errorMessage);
return 0;
}
logOpenDatabaseError(context, name);
return 0;
}
}
return backend.release();
}
void DatabaseManager::addProposedDatabase(ProposedDatabase* proposedDb)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_proposedDatabases.add(proposedDb);
}
void DatabaseManager::removeProposedDatabase(ProposedDatabase* proposedDb)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_proposedDatabases.remove(proposedDb);
}
RefPtr<Database> DatabaseManager::openDatabase(ScriptExecutionContext* context,
const String& name, const String& expectedVersion, const String& displayName,
unsigned long estimatedSize, PassRefPtr<DatabaseCallback> creationCallback,
DatabaseError& error)
{
ScriptController::initializeThreading();
ASSERT(error == DatabaseError::None);
bool setVersionInNewDatabase = !creationCallback;
String errorMessage;
RefPtr<DatabaseBackendBase> backend = openDatabaseBackend(context, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, error, errorMessage);
if (!backend)
return nullptr;
RefPtr<Database> database = Database::create(context, backend);
RefPtr<DatabaseContext> databaseContext = databaseContextFor(context);
databaseContext->setHasOpenDatabases();
InspectorInstrumentation::didOpenDatabase(context, database.copyRef(), context->securityOrigin()->host(), name, expectedVersion);
if (backend->isNew() && creationCallback.get()) {
LOG(StorageAPI, "Scheduling DatabaseCreationCallbackTask for database %p\n", database.get());
database->setHasPendingCreationEvent(true);
database->m_scriptExecutionContext->postTask([creationCallback, database] (ScriptExecutionContext&) {
creationCallback->handleEvent(database.get());
database->setHasPendingCreationEvent(false);
});
}
ASSERT(database);
return database;
}
bool DatabaseManager::hasOpenDatabases(ScriptExecutionContext* context)
{
RefPtr<DatabaseContext> databaseContext = existingDatabaseContextFor(context);
if (!databaseContext)
return false;
return databaseContext->hasOpenDatabases();
}
void DatabaseManager::stopDatabases(ScriptExecutionContext* context, DatabaseTaskSynchronizer* synchronizer)
{
RefPtr<DatabaseContext> databaseContext = existingDatabaseContextFor(context);
if (!databaseContext || !databaseContext->stopDatabases(synchronizer))
if (synchronizer)
synchronizer->taskCompleted();
}
String DatabaseManager::fullPathForDatabase(SecurityOrigin* origin, const String& name, bool createIfDoesNotExist)
{
{
std::lock_guard<std::mutex> lock(m_mutex);
for (auto* proposedDatabase : m_proposedDatabases) {
if (proposedDatabase->details().name() == name && proposedDatabase->origin()->equal(origin))
return String();
}
}
return m_server->fullPathForDatabase(origin, name, createIfDoesNotExist);
}
bool DatabaseManager::hasEntryForOrigin(SecurityOrigin* origin)
{
return m_server->hasEntryForOrigin(origin);
}
void DatabaseManager::origins(Vector<RefPtr<SecurityOrigin>>& result)
{
m_server->origins(result);
}
bool DatabaseManager::databaseNamesForOrigin(SecurityOrigin* origin, Vector<String>& result)
{
return m_server->databaseNamesForOrigin(origin, result);
}
DatabaseDetails DatabaseManager::detailsForNameAndOrigin(const String& name, SecurityOrigin* origin)
{
{
std::lock_guard<std::mutex> lock(m_mutex);
for (auto* proposedDatabase : m_proposedDatabases) {
if (proposedDatabase->details().name() == name && proposedDatabase->origin()->equal(origin)) {
ASSERT(proposedDatabase->details().threadID() == std::this_thread::get_id() || isMainThread());
return proposedDatabase->details();
}
}
}
return m_server->detailsForNameAndOrigin(name, origin);
}
unsigned long long DatabaseManager::usageForOrigin(SecurityOrigin* origin)
{
return m_server->usageForOrigin(origin);
}
unsigned long long DatabaseManager::quotaForOrigin(SecurityOrigin* origin)
{
return m_server->quotaForOrigin(origin);
}
void DatabaseManager::setQuota(SecurityOrigin* origin, unsigned long long quotaSize)
{
m_server->setQuota(origin, quotaSize);
}
void DatabaseManager::deleteAllDatabases()
{
m_server->deleteAllDatabases();
}
bool DatabaseManager::deleteOrigin(SecurityOrigin* origin)
{
return m_server->deleteOrigin(origin);
}
bool DatabaseManager::deleteDatabase(SecurityOrigin* origin, const String& name)
{
return m_server->deleteDatabase(origin, name);
}
void DatabaseManager::closeAllDatabases()
{
m_server->closeAllDatabases();
}
void DatabaseManager::interruptAllDatabasesForContext(ScriptExecutionContext* context)
{
RefPtr<DatabaseContext> databaseContext = existingDatabaseContextFor(context);
if (databaseContext)
m_server->interruptAllDatabasesForContext(databaseContext.get());
}
void DatabaseManager::logErrorMessage(ScriptExecutionContext* context, const String& message)
{
context->addConsoleMessage(MessageSource::Storage, MessageLevel::Error, message);
}
}