This source file includes following definitions.
- isContextValid
- getDatabaseNames
- open
- openInternal
- open
- deleteDatabase
- cmp
#include "config.h"
#include "modules/indexeddb/IDBFactory.h"
#include "bindings/v8/ExceptionState.h"
#include "bindings/v8/IDBBindingUtilities.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "modules/indexeddb/IDBDatabase.h"
#include "modules/indexeddb/IDBDatabaseCallbacks.h"
#include "modules/indexeddb/IDBFactoryBackendInterface.h"
#include "modules/indexeddb/IDBHistograms.h"
#include "modules/indexeddb/IDBKey.h"
#include "modules/indexeddb/IDBTracing.h"
#include "modules/indexeddb/WebIDBCallbacksImpl.h"
#include "modules/indexeddb/WebIDBDatabaseCallbacksImpl.h"
#include "platform/weborigin/DatabaseIdentifier.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "public/platform/Platform.h"
#include "public/platform/WebIDBFactory.h"
namespace WebCore {
static const char permissionDeniedErrorMessage[] = "The user denied permission to access the database.";
IDBFactory::IDBFactory(IDBFactoryBackendInterface* permissionClient)
: m_permissionClient(permissionClient)
{
relaxAdoptionRequirement();
ScriptWrappable::init(this);
}
IDBFactory::~IDBFactory()
{
}
static bool isContextValid(ExecutionContext* context)
{
ASSERT(context->isDocument() || context->isWorkerGlobalScope());
if (context->isDocument()) {
Document* document = toDocument(context);
return document->frame() && document->page();
}
return true;
}
PassRefPtr<IDBRequest> IDBFactory::getDatabaseNames(ExecutionContext* context, ExceptionState& exceptionState)
{
IDB_TRACE("IDBFactory::getDatabaseNames");
if (!isContextValid(context))
return nullptr;
if (!context->securityOrigin()->canAccessDatabase()) {
exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context.");
return nullptr;
}
RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::createNull(), 0);
if (!m_permissionClient->allowIndexedDB(context, "Database Listing")) {
request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage));
return request;
}
blink::Platform::current()->idbFactory()->getDatabaseNames(WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()));
return request;
}
PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ExecutionContext* context, const String& name, unsigned long long version, ExceptionState& exceptionState)
{
IDB_TRACE("IDBFactory::open");
if (!version) {
exceptionState.throwTypeError("The version provided must not be 0.");
return nullptr;
}
return openInternal(context, name, version, exceptionState);
}
PassRefPtr<IDBOpenDBRequest> IDBFactory::openInternal(ExecutionContext* context, const String& name, int64_t version, ExceptionState& exceptionState)
{
blink::Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBOpenCall, IDBMethodsMax);
ASSERT(version >= 1 || version == IDBDatabaseMetadata::NoIntVersion);
if (name.isNull()) {
exceptionState.throwTypeError("The name provided must not be empty.");
return nullptr;
}
if (!isContextValid(context))
return nullptr;
if (!context->securityOrigin()->canAccessDatabase()) {
exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context.");
return nullptr;
}
RefPtr<IDBDatabaseCallbacks> databaseCallbacks = IDBDatabaseCallbacks::create();
int64_t transactionId = IDBDatabase::nextTransactionId();
RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, databaseCallbacks, transactionId, version);
if (!m_permissionClient->allowIndexedDB(context, name)) {
request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage));
return request;
}
blink::Platform::current()->idbFactory()->open(name, version, transactionId, WebIDBCallbacksImpl::create(request).leakPtr(), WebIDBDatabaseCallbacksImpl::create(databaseCallbacks.release()).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()));
return request;
}
PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ExecutionContext* context, const String& name, ExceptionState& exceptionState)
{
IDB_TRACE("IDBFactory::open");
return openInternal(context, name, IDBDatabaseMetadata::NoIntVersion, exceptionState);
}
PassRefPtr<IDBOpenDBRequest> IDBFactory::deleteDatabase(ExecutionContext* context, const String& name, ExceptionState& exceptionState)
{
IDB_TRACE("IDBFactory::deleteDatabase");
blink::Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBDeleteDatabaseCall, IDBMethodsMax);
if (name.isNull()) {
exceptionState.throwTypeError("The name provided must not be empty.");
return nullptr;
}
if (!isContextValid(context))
return nullptr;
if (!context->securityOrigin()->canAccessDatabase()) {
exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context.");
return nullptr;
}
RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, nullptr, 0, IDBDatabaseMetadata::DefaultIntVersion);
if (!m_permissionClient->allowIndexedDB(context, name)) {
request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage));
return request;
}
blink::Platform::current()->idbFactory()->deleteDatabase(name, WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()));
return request;
}
short IDBFactory::cmp(ExecutionContext* context, const ScriptValue& firstValue, const ScriptValue& secondValue, ExceptionState& exceptionState)
{
DOMRequestState requestState(context);
RefPtr<IDBKey> first = scriptValueToIDBKey(&requestState, firstValue);
RefPtr<IDBKey> second = scriptValueToIDBKey(&requestState, secondValue);
ASSERT(first);
ASSERT(second);
if (!first->isValid() || !second->isValid()) {
exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
return 0;
}
return static_cast<short>(first->compare(second.get()));
}
}