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/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" 48 49 namespace WebCore { 50 51 static const char permissionDeniedErrorMessage[] = "The user denied permission to access the database."; 52 53 IDBFactory::IDBFactory(IDBFactoryBackendInterface* permissionClient) 54 : m_permissionClient(permissionClient) 55 { 56 // We pass a reference to this object before it can be adopted. 57 relaxAdoptionRequirement(); 58 ScriptWrappable::init(this); 59 } 60 61 IDBFactory::~IDBFactory() 62 { 63 } 64 65 static bool isContextValid(ExecutionContext* context) 66 { 67 ASSERT(context->isDocument() || context->isWorkerGlobalScope()); 68 if (context->isDocument()) { 69 Document* document = toDocument(context); 70 return document->frame() && document->page(); 71 } 72 return true; 73 } 74 75 PassRefPtr<IDBRequest> IDBFactory::getDatabaseNames(ExecutionContext* context, ExceptionState& exceptionState) 76 { 77 IDB_TRACE("IDBFactory::getDatabaseNames"); 78 if (!isContextValid(context)) 79 return 0; 80 if (!context->securityOrigin()->canAccessDatabase()) { 81 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context."); 82 return 0; 83 } 84 85 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::createNull(), 0); 86 87 if (!m_permissionClient->allowIndexedDB(context, "Database Listing")) { 88 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage)); 89 return request; 90 } 91 92 blink::Platform::current()->idbFactory()->getDatabaseNames(WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin())); 93 return request; 94 } 95 96 PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ExecutionContext* context, const String& name, unsigned long long version, ExceptionState& exceptionState) 97 { 98 IDB_TRACE("IDBFactory::open"); 99 if (!version) { 100 exceptionState.throwUninformativeAndGenericTypeError(); 101 return 0; 102 } 103 return openInternal(context, name, version, exceptionState); 104 } 105 106 PassRefPtr<IDBOpenDBRequest> IDBFactory::openInternal(ExecutionContext* context, const String& name, int64_t version, ExceptionState& exceptionState) 107 { 108 blink::Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBOpenCall, IDBMethodsMax); 109 ASSERT(version >= 1 || version == IDBDatabaseMetadata::NoIntVersion); 110 if (name.isNull()) { 111 exceptionState.throwUninformativeAndGenericTypeError(); 112 return 0; 113 } 114 if (!isContextValid(context)) 115 return 0; 116 if (!context->securityOrigin()->canAccessDatabase()) { 117 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context."); 118 return 0; 119 } 120 121 RefPtr<IDBDatabaseCallbacks> databaseCallbacks = IDBDatabaseCallbacks::create(); 122 int64_t transactionId = IDBDatabase::nextTransactionId(); 123 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, databaseCallbacks, transactionId, version); 124 125 if (!m_permissionClient->allowIndexedDB(context, name)) { 126 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage)); 127 return request; 128 } 129 130 blink::Platform::current()->idbFactory()->open(name, version, transactionId, WebIDBCallbacksImpl::create(request).leakPtr(), WebIDBDatabaseCallbacksImpl::create(databaseCallbacks.release()).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin())); 131 return request; 132 } 133 134 PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ExecutionContext* context, const String& name, ExceptionState& exceptionState) 135 { 136 IDB_TRACE("IDBFactory::open"); 137 return openInternal(context, name, IDBDatabaseMetadata::NoIntVersion, exceptionState); 138 } 139 140 PassRefPtr<IDBOpenDBRequest> IDBFactory::deleteDatabase(ExecutionContext* context, const String& name, ExceptionState& exceptionState) 141 { 142 IDB_TRACE("IDBFactory::deleteDatabase"); 143 blink::Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBDeleteDatabaseCall, IDBMethodsMax); 144 if (name.isNull()) { 145 exceptionState.throwUninformativeAndGenericTypeError(); 146 return 0; 147 } 148 if (!isContextValid(context)) 149 return 0; 150 if (!context->securityOrigin()->canAccessDatabase()) { 151 exceptionState.throwSecurityError("access to the Indexed Database API is denied in this context."); 152 return 0; 153 } 154 155 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, 0, 0, IDBDatabaseMetadata::DefaultIntVersion); 156 157 if (!m_permissionClient->allowIndexedDB(context, name)) { 158 request->onError(DOMError::create(UnknownError, permissionDeniedErrorMessage)); 159 return request; 160 } 161 162 blink::Platform::current()->idbFactory()->deleteDatabase(name, WebIDBCallbacksImpl::create(request).leakPtr(), createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin())); 163 return request; 164 } 165 166 short IDBFactory::cmp(ExecutionContext* context, const ScriptValue& firstValue, const ScriptValue& secondValue, ExceptionState& exceptionState) 167 { 168 DOMRequestState requestState(context); 169 RefPtr<IDBKey> first = scriptValueToIDBKey(&requestState, firstValue); 170 RefPtr<IDBKey> second = scriptValueToIDBKey(&requestState, secondValue); 171 172 ASSERT(first); 173 ASSERT(second); 174 175 if (!first->isValid() || !second->isValid()) { 176 exceptionState.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage); 177 return 0; 178 } 179 180 return static_cast<short>(first->compare(second.get())); 181 } 182 183 } // namespace WebCore 184