1 /* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 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. 16 * 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. 27 */ 28 29 #include "config.h" 30 #include "modules/indexeddb/IDBFactory.h" 31 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" 48 49 namespace blink { 50 51 static const char permissionDeniedErrorMessage[] = "The user denied permission to access the database."; 52 53 IDBFactory::IDBFactory(IndexedDBClient* permissionClient) 54 : m_permissionClient(permissionClient) 55 { 56 } 57 58 void IDBFactory::trace(Visitor* visitor) 59 { 60 visitor->trace(m_permissionClient); 61 } 62 63 static bool isContextValid(ExecutionContext* context) 64 { 65 ASSERT(context->isDocument() || context->isWorkerGlobalScope()); 66 if (context->isDocument()) { 67 Document* document = toDocument(context); 68 return document->frame() && document->page(); 69 } 70 return true; 71 } 72 73 IDBRequest* IDBFactory::getDatabaseNames(ScriptState* scriptState, ExceptionState& exceptionState) 74 { 75 IDB_TRACE("IDBFactory::getDatabaseNames"); 76 if (!isContextValid(scriptState->executionContext())) 77 return nullptr; 78 if (!scriptState->executionContext()->securityOrigin()->canAccessDatabase()) { 79 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context."); 80 return 0; 81 } 82 83 IDBRequest* request = IDBRequest::create(scriptState, IDBAny::createNull(), 0); 84 85 if (!m_permissionClient->allowIndexedDB(scriptState->executionContext(), "Database Listing")) { 86 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage)); 87 return request; 88 } 89 90 Platform::current()->idbFactory()->getDatabaseNames(WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(scriptState->executionContext()->securityOrigin())); 91 return request; 92 } 93 94 IDBOpenDBRequest* IDBFactory::open(ScriptState* scriptState, const String& name, unsigned long long version, ExceptionState& exceptionState) 95 { 96 IDB_TRACE("IDBFactory::open"); 97 if (!version) { 98 exceptionState.throwTypeError("The version provided must not be 0."); 99 return 0; 100 } 101 return openInternal(scriptState, name, version, exceptionState); 102 } 103 104 IDBOpenDBRequest* IDBFactory::openInternal(ScriptState* scriptState, const String& name, int64_t version, ExceptionState& exceptionState) 105 { 106 Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBOpenCall, IDBMethodsMax); 107 ASSERT(version >= 1 || version == IDBDatabaseMetadata::NoIntVersion); 108 if (name.isNull()) { 109 exceptionState.throwTypeError("The name provided must not be empty."); 110 return 0; 111 } 112 if (!isContextValid(scriptState->executionContext())) 113 return nullptr; 114 if (!scriptState->executionContext()->securityOrigin()->canAccessDatabase()) { 115 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context."); 116 return 0; 117 } 118 119 IDBDatabaseCallbacks* databaseCallbacks = IDBDatabaseCallbacks::create(); 120 int64_t transactionId = IDBDatabase::nextTransactionId(); 121 IDBOpenDBRequest* request = IDBOpenDBRequest::create(scriptState, databaseCallbacks, transactionId, version); 122 123 if (!m_permissionClient->allowIndexedDB(scriptState->executionContext(), name)) { 124 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage)); 125 return request; 126 } 127 128 Platform::current()->idbFactory()->open(name, version, transactionId, WebIDBCallbacksImpl::create(request).leakPtr(), WebIDBDatabaseCallbacksImpl::create(databaseCallbacks).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(scriptState->executionContext()->securityOrigin())); 129 return request; 130 } 131 132 IDBOpenDBRequest* IDBFactory::open(ScriptState* scriptState, const String& name, ExceptionState& exceptionState) 133 { 134 IDB_TRACE("IDBFactory::open"); 135 return openInternal(scriptState, name, IDBDatabaseMetadata::NoIntVersion, exceptionState); 136 } 137 138 IDBOpenDBRequest* IDBFactory::deleteDatabase(ScriptState* scriptState, const String& name, ExceptionState& exceptionState) 139 { 140 IDB_TRACE("IDBFactory::deleteDatabase"); 141 Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBDeleteDatabaseCall, IDBMethodsMax); 142 if (name.isNull()) { 143 exceptionState.throwTypeError("The name provided must not be empty."); 144 return 0; 145 } 146 if (!isContextValid(scriptState->executionContext())) 147 return nullptr; 148 if (!scriptState->executionContext()->securityOrigin()->canAccessDatabase()) { 149 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context."); 150 return 0; 151 } 152 153 IDBOpenDBRequest* request = IDBOpenDBRequest::create(scriptState, nullptr, 0, IDBDatabaseMetadata::DefaultIntVersion); 154 155 if (!m_permissionClient->allowIndexedDB(scriptState->executionContext(), name)) { 156 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage)); 157 return request; 158 } 159 160 Platform::current()->idbFactory()->deleteDatabase(name, WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(scriptState->executionContext()->securityOrigin())); 161 return request; 162 } 163 164 short IDBFactory::cmp(ScriptState* scriptState, const ScriptValue& firstValue, const ScriptValue& secondValue, ExceptionState& exceptionState) 165 { 166 IDBKey* first = scriptValueToIDBKey(scriptState->isolate(), firstValue); 167 IDBKey* second = scriptValueToIDBKey(scriptState->isolate(), secondValue); 168 169 ASSERT(first); 170 ASSERT(second); 171 172 if (!first->isValid() || !second->isValid()) { 173 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage); 174 return 0; 175 } 176 177 return static_cast<short>(first->compare(second)); 178 } 179 180 } // namespace blink 181