2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "modules/indexeddb/IDBFactory.h"
32 #include "bindings/core/v8/ExceptionState.h"
33 #include "bindings/modules/v8/IDBBindingUtilities.h"
34 #include "core/dom/Document.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "modules/indexeddb/IDBDatabase.h"
37 #include "modules/indexeddb/IDBDatabaseCallbacks.h"
38 #include "modules/indexeddb/IDBHistograms.h"
39 #include "modules/indexeddb/IDBKey.h"
40 #include "modules/indexeddb/IDBTracing.h"
41 #include "modules/indexeddb/IndexedDBClient.h"
42 #include "modules/indexeddb/WebIDBCallbacksImpl.h"
43 #include "modules/indexeddb/WebIDBDatabaseCallbacksImpl.h"
44 #include "platform/weborigin/DatabaseIdentifier.h"
45 #include "platform/weborigin/SecurityOrigin.h"
46 #include "public/platform/Platform.h"
47 #include "public/platform/WebIDBFactory.h"
51 static const char permissionDeniedErrorMessage[] = "The user denied permission to access the database.";
53 IDBFactory::IDBFactory(IndexedDBClient* permissionClient)
54 : m_permissionClient(permissionClient)
58 void IDBFactory::trace(Visitor* visitor)
60 visitor->trace(m_permissionClient);
63 static bool isContextValid(ExecutionContext* context)
65 ASSERT(context->isDocument() || context->isWorkerGlobalScope());
66 if (context->isDocument()) {
67 Document* document = toDocument(context);
68 return document->frame() && document->page();
73 IDBRequest* IDBFactory::getDatabaseNames(ScriptState* scriptState, ExceptionState& exceptionState)
75 IDB_TRACE("IDBFactory::getDatabaseNames");
76 if (!isContextValid(scriptState->executionContext()))
78 if (!scriptState->executionContext()->securityOrigin()->canAccessDatabase()) {
79 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context.");
83 IDBRequest* request = IDBRequest::create(scriptState, IDBAny::createNull(), 0);
85 if (!m_permissionClient->allowIndexedDB(scriptState->executionContext(), "Database Listing")) {
86 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage));
90 Platform::current()->idbFactory()->getDatabaseNames(WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(scriptState->executionContext()->securityOrigin()));
94 IDBOpenDBRequest* IDBFactory::open(ScriptState* scriptState, const String& name, unsigned long long version, ExceptionState& exceptionState)
96 IDB_TRACE("IDBFactory::open");
98 exceptionState.throwTypeError("The version provided must not be 0.");
101 return openInternal(scriptState, name, version, exceptionState);
104 IDBOpenDBRequest* IDBFactory::openInternal(ScriptState* scriptState, const String& name, int64_t version, ExceptionState& exceptionState)
106 Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBOpenCall, IDBMethodsMax);
107 ASSERT(version >= 1 || version == IDBDatabaseMetadata::NoIntVersion);
109 exceptionState.throwTypeError("The name provided must not be empty.");
112 if (!isContextValid(scriptState->executionContext()))
114 if (!scriptState->executionContext()->securityOrigin()->canAccessDatabase()) {
115 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context.");
119 IDBDatabaseCallbacks* databaseCallbacks = IDBDatabaseCallbacks::create();
120 int64_t transactionId = IDBDatabase::nextTransactionId();
121 IDBOpenDBRequest* request = IDBOpenDBRequest::create(scriptState, databaseCallbacks, transactionId, version);
123 if (!m_permissionClient->allowIndexedDB(scriptState->executionContext(), name)) {
124 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage));
128 Platform::current()->idbFactory()->open(name, version, transactionId, WebIDBCallbacksImpl::create(request).leakPtr(), WebIDBDatabaseCallbacksImpl::create(databaseCallbacks).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(scriptState->executionContext()->securityOrigin()));
132 IDBOpenDBRequest* IDBFactory::open(ScriptState* scriptState, const String& name, ExceptionState& exceptionState)
134 IDB_TRACE("IDBFactory::open");
135 return openInternal(scriptState, name, IDBDatabaseMetadata::NoIntVersion, exceptionState);
138 IDBOpenDBRequest* IDBFactory::deleteDatabase(ScriptState* scriptState, const String& name, ExceptionState& exceptionState)
140 IDB_TRACE("IDBFactory::deleteDatabase");
141 Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBDeleteDatabaseCall, IDBMethodsMax);
143 exceptionState.throwTypeError("The name provided must not be empty.");
146 if (!isContextValid(scriptState->executionContext()))
148 if (!scriptState->executionContext()->securityOrigin()->canAccessDatabase()) {
149 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context.");
153 IDBOpenDBRequest* request = IDBOpenDBRequest::create(scriptState, nullptr, 0, IDBDatabaseMetadata::DefaultIntVersion);
155 if (!m_permissionClient->allowIndexedDB(scriptState->executionContext(), name)) {
156 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage));
160 Platform::current()->idbFactory()->deleteDatabase(name, WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(scriptState->executionContext()->securityOrigin()));
164 short IDBFactory::cmp(ScriptState* scriptState, const ScriptValue& firstValue, const ScriptValue& secondValue, ExceptionState& exceptionState)
166 IDBKey* first = scriptValueToIDBKey(scriptState->isolate(), firstValue);
167 IDBKey* second = scriptValueToIDBKey(scriptState->isolate(), secondValue);
172 if (!first->isValid() || !second->isValid()) {
173 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
177 return static_cast<short>(first->compare(second));