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 "core/page/Frame.h" 37 #include "core/page/Page.h" 38 #include "core/page/PageGroup.h" 39 #include "core/platform/HistogramSupport.h" 40 #include "core/workers/WorkerGlobalScope.h" 41 #include "core/workers/WorkerLoaderProxy.h" 42 #include "core/workers/WorkerThread.h" 43 #include "modules/indexeddb/IDBDatabase.h" 44 #include "modules/indexeddb/IDBDatabaseCallbacksImpl.h" 45 #include "modules/indexeddb/IDBFactoryBackendInterface.h" 46 #include "modules/indexeddb/IDBHistograms.h" 47 #include "modules/indexeddb/IDBKey.h" 48 #include "modules/indexeddb/IDBKeyRange.h" 49 #include "modules/indexeddb/IDBOpenDBRequest.h" 50 #include "modules/indexeddb/IDBTracing.h" 51 #include "weborigin/DatabaseIdentifier.h" 52 #include "weborigin/SecurityOrigin.h" 53 54 namespace WebCore { 55 56 IDBFactory::IDBFactory(IDBFactoryBackendInterface* factory) 57 : m_backend(factory) 58 { 59 // We pass a reference to this object before it can be adopted. 60 relaxAdoptionRequirement(); 61 ScriptWrappable::init(this); 62 } 63 64 IDBFactory::~IDBFactory() 65 { 66 } 67 68 static bool isContextValid(ScriptExecutionContext* context) 69 { 70 ASSERT(context->isDocument() || context->isWorkerGlobalScope()); 71 if (context->isDocument()) { 72 Document* document = toDocument(context); 73 return document->frame() && document->page(); 74 } 75 return true; 76 } 77 78 PassRefPtr<IDBRequest> IDBFactory::getDatabaseNames(ScriptExecutionContext* context, ExceptionState& es) 79 { 80 IDB_TRACE("IDBFactory::getDatabaseNames"); 81 if (!isContextValid(context)) 82 return 0; 83 if (!context->securityOrigin()->canAccessDatabase()) { 84 es.throwDOMException(SecurityError); 85 return 0; 86 } 87 88 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), 0); 89 m_backend->getDatabaseNames(request, createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()), context); 90 return request; 91 } 92 93 PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ScriptExecutionContext* context, const String& name, unsigned long long version, ExceptionState& es) 94 { 95 IDB_TRACE("IDBFactory::open"); 96 if (!version) { 97 es.throwTypeError(); 98 return 0; 99 } 100 return openInternal(context, name, version, es); 101 } 102 103 PassRefPtr<IDBOpenDBRequest> IDBFactory::openInternal(ScriptExecutionContext* context, const String& name, int64_t version, ExceptionState& es) 104 { 105 HistogramSupport::histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBOpenCall, IDBMethodsMax); 106 ASSERT(version >= 1 || version == IDBDatabaseMetadata::NoIntVersion); 107 if (name.isNull()) { 108 es.throwTypeError(); 109 return 0; 110 } 111 if (!isContextValid(context)) 112 return 0; 113 if (!context->securityOrigin()->canAccessDatabase()) { 114 es.throwDOMException(SecurityError); 115 return 0; 116 } 117 118 RefPtr<IDBDatabaseCallbacksImpl> databaseCallbacks = IDBDatabaseCallbacksImpl::create(); 119 int64_t transactionId = IDBDatabase::nextTransactionId(); 120 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, databaseCallbacks, transactionId, version); 121 m_backend->open(name, version, transactionId, request, databaseCallbacks, createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()), context); 122 return request; 123 } 124 125 PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ScriptExecutionContext* context, const String& name, ExceptionState& es) 126 { 127 IDB_TRACE("IDBFactory::open"); 128 return openInternal(context, name, IDBDatabaseMetadata::NoIntVersion, es); 129 } 130 131 PassRefPtr<IDBOpenDBRequest> IDBFactory::deleteDatabase(ScriptExecutionContext* context, const String& name, ExceptionState& es) 132 { 133 IDB_TRACE("IDBFactory::deleteDatabase"); 134 HistogramSupport::histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBDeleteDatabaseCall, IDBMethodsMax); 135 if (name.isNull()) { 136 es.throwTypeError(); 137 return 0; 138 } 139 if (!isContextValid(context)) 140 return 0; 141 if (!context->securityOrigin()->canAccessDatabase()) { 142 es.throwDOMException(SecurityError); 143 return 0; 144 } 145 146 RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, 0, 0, IDBDatabaseMetadata::DefaultIntVersion); 147 m_backend->deleteDatabase(name, request, createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()), context); 148 return request; 149 } 150 151 short IDBFactory::cmp(ScriptExecutionContext* context, const ScriptValue& firstValue, const ScriptValue& secondValue, ExceptionState& es) 152 { 153 DOMRequestState requestState(context); 154 RefPtr<IDBKey> first = scriptValueToIDBKey(&requestState, firstValue); 155 RefPtr<IDBKey> second = scriptValueToIDBKey(&requestState, secondValue); 156 157 ASSERT(first); 158 ASSERT(second); 159 160 if (!first->isValid() || !second->isValid()) { 161 es.throwDOMException(DataError, IDBDatabase::notValidKeyErrorMessage); 162 return 0; 163 } 164 165 return static_cast<short>(first->compare(second.get())); 166 } 167 168 } // namespace WebCore 169