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/v8/ExceptionState.h"
33 #include "bindings/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/IDBFactoryBackendInterface.h"
39 #include "modules/indexeddb/IDBHistograms.h"
40 #include "modules/indexeddb/IDBKey.h"
41 #include "modules/indexeddb/IDBTracing.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(IDBFactoryBackendInterface* permissionClient)
54 : m_permissionClient(permissionClient)
56 // We pass a reference to this object before it can be adopted.
57 relaxAdoptionRequirement();
58 ScriptWrappable::init(this);
61 IDBFactory::~IDBFactory()
65 static bool isContextValid(ExecutionContext* context)
67 ASSERT(context->isDocument() || context->isWorkerGlobalScope());
68 if (context->isDocument()) {
69 Document* document = toDocument(context);
70 return document->frame() && document->page();
75 PassRefPtr<IDBRequest> IDBFactory::getDatabaseNames(ExecutionContext* context, ExceptionState& exceptionState)
77 IDB_TRACE("IDBFactory::getDatabaseNames");
78 if (!isContextValid(context))
80 if (!context->securityOrigin()->canAccessDatabase()) {
81 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context.");
85 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::createNull(), 0);
87 if (!m_permissionClient->allowIndexedDB(context, "Database Listing")) {
88 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage));
92 blink::Platform::current()->idbFactory()->getDatabaseNames(WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()));
96 PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ExecutionContext* context, const String& name, unsigned long long version, ExceptionState& exceptionState)
98 IDB_TRACE("IDBFactory::open");
100 exceptionState.throwTypeError("The version provided must not be 0.");
103 return openInternal(context, name, version, exceptionState);
106 PassRefPtr<IDBOpenDBRequest> IDBFactory::openInternal(ExecutionContext* context, const String& name, int64_t version, ExceptionState& exceptionState)
108 blink::Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBOpenCall, IDBMethodsMax);
109 ASSERT(version >= 1 || version == IDBDatabaseMetadata::NoIntVersion);
111 exceptionState.throwTypeError("The name provided must not be empty.");
114 if (!isContextValid(context))
116 if (!context->securityOrigin()->canAccessDatabase()) {
117 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context.");
121 RefPtr<IDBDatabaseCallbacks> databaseCallbacks = IDBDatabaseCallbacks::create();
122 int64_t transactionId = IDBDatabase::nextTransactionId();
123 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, databaseCallbacks, transactionId, version);
125 if (!m_permissionClient->allowIndexedDB(context, name)) {
126 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage));
130 blink::Platform::current()->idbFactory()->open(name, version, transactionId, WebIDBCallbacksImpl::create(request).leakPtr(), WebIDBDatabaseCallbacksImpl::create(databaseCallbacks.release()).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()));
134 PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ExecutionContext* context, const String& name, ExceptionState& exceptionState)
136 IDB_TRACE("IDBFactory::open");
137 return openInternal(context, name, IDBDatabaseMetadata::NoIntVersion, exceptionState);
140 PassRefPtr<IDBOpenDBRequest> IDBFactory::deleteDatabase(ExecutionContext* context, const String& name, ExceptionState& exceptionState)
142 IDB_TRACE("IDBFactory::deleteDatabase");
143 blink::Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBDeleteDatabaseCall, IDBMethodsMax);
145 exceptionState.throwTypeError("The name provided must not be empty.");
148 if (!isContextValid(context))
150 if (!context->securityOrigin()->canAccessDatabase()) {
151 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context.");
155 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, 0, 0, IDBDatabaseMetadata::DefaultIntVersion);
157 if (!m_permissionClient->allowIndexedDB(context, name)) {
158 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage));
162 blink::Platform::current()->idbFactory()->deleteDatabase(name, WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()));
166 short IDBFactory::cmp(ExecutionContext* context, const ScriptValue& firstValue, const ScriptValue& secondValue, ExceptionState& exceptionState)
168 DOMRequestState requestState(context);
169 RefPtr<IDBKey> first = scriptValueToIDBKey(&requestState, firstValue);
170 RefPtr<IDBKey> second = scriptValueToIDBKey(&requestState, secondValue);
175 if (!first->isValid() || !second->isValid()) {
176 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage);
180 return static_cast<short>(first->compare(second.get()));
183 } // namespace WebCore