Home | History | Annotate | Download | only in indexeddb
      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  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "modules/indexeddb/IDBDatabase.h"
     28 
     29 #include "bindings/v8/ExceptionState.h"
     30 #include "bindings/v8/ExceptionStatePlaceholder.h"
     31 #include "bindings/v8/IDBBindingUtilities.h"
     32 #include "bindings/v8/Nullable.h"
     33 #include "bindings/v8/SerializedScriptValue.h"
     34 #include "core/dom/ExecutionContext.h"
     35 #include "core/events/EventQueue.h"
     36 #include "core/inspector/ScriptCallStack.h"
     37 #include "modules/indexeddb/IDBAny.h"
     38 #include "modules/indexeddb/IDBEventDispatcher.h"
     39 #include "modules/indexeddb/IDBHistograms.h"
     40 #include "modules/indexeddb/IDBIndex.h"
     41 #include "modules/indexeddb/IDBKeyPath.h"
     42 #include "modules/indexeddb/IDBTracing.h"
     43 #include "modules/indexeddb/IDBVersionChangeEvent.h"
     44 #include "modules/indexeddb/WebIDBDatabaseCallbacksImpl.h"
     45 #include "public/platform/Platform.h"
     46 #include "public/platform/WebIDBKeyPath.h"
     47 #include "public/platform/WebIDBTypes.h"
     48 #include "wtf/Atomics.h"
     49 #include <limits>
     50 
     51 using blink::WebIDBDatabase;
     52 
     53 namespace WebCore {
     54 
     55 const char IDBDatabase::indexDeletedErrorMessage[] = "The index or its object store has been deleted.";
     56 const char IDBDatabase::isKeyCursorErrorMessage[] = "The cursor is a key cursor.";
     57 const char IDBDatabase::noKeyOrKeyRangeErrorMessage[] = "No key or key range specified.";
     58 const char IDBDatabase::noSuchIndexErrorMessage[] = "The specified index was not found.";
     59 const char IDBDatabase::noSuchObjectStoreErrorMessage[] = "The specified object store was not found.";
     60 const char IDBDatabase::noValueErrorMessage[] = "The cursor is being iterated or has iterated past its end.";
     61 const char IDBDatabase::notValidKeyErrorMessage[] = "The parameter is not a valid key.";
     62 const char IDBDatabase::notVersionChangeTransactionErrorMessage[] = "The database is not running a version change transaction.";
     63 const char IDBDatabase::objectStoreDeletedErrorMessage[] = "The object store has been deleted.";
     64 const char IDBDatabase::requestNotFinishedErrorMessage[] = "The request has not finished.";
     65 const char IDBDatabase::sourceDeletedErrorMessage[] = "The cursor's source or effective object store has been deleted.";
     66 const char IDBDatabase::transactionInactiveErrorMessage[] = "The transaction is not active.";
     67 const char IDBDatabase::transactionFinishedErrorMessage[] = "The transaction has finished.";
     68 const char IDBDatabase::transactionReadOnlyErrorMessage[] = "The transaction is read-only.";
     69 const char IDBDatabase::databaseClosedErrorMessage[] = "The database connection is closed.";
     70 
     71 IDBDatabase* IDBDatabase::create(ExecutionContext* context, PassOwnPtr<WebIDBDatabase> database, IDBDatabaseCallbacks* callbacks)
     72 {
     73     IDBDatabase* idbDatabase = adoptRefCountedGarbageCollectedWillBeNoop(new IDBDatabase(context, database, callbacks));
     74     idbDatabase->suspendIfNeeded();
     75     return idbDatabase;
     76 }
     77 
     78 IDBDatabase::IDBDatabase(ExecutionContext* context, PassOwnPtr<WebIDBDatabase> backend, IDBDatabaseCallbacks* callbacks)
     79     : ActiveDOMObject(context)
     80     , m_backend(backend)
     81     , m_closePending(false)
     82     , m_contextStopped(false)
     83     , m_databaseCallbacks(callbacks)
     84 {
     85     ScriptWrappable::init(this);
     86     m_databaseCallbacks->connect(this);
     87 }
     88 
     89 IDBDatabase::~IDBDatabase()
     90 {
     91     if (!m_closePending && m_backend)
     92         m_backend->close();
     93 }
     94 
     95 void IDBDatabase::trace(Visitor* visitor)
     96 {
     97     visitor->trace(m_versionChangeTransaction);
     98     visitor->trace(m_transactions);
     99 #if ENABLE(OILPAN)
    100     visitor->trace(m_enqueuedEvents);
    101 #endif
    102     visitor->trace(m_databaseCallbacks);
    103     EventTargetWithInlineData::trace(visitor);
    104 }
    105 
    106 int64_t IDBDatabase::nextTransactionId()
    107 {
    108     // Only keep a 32-bit counter to allow ports to use the other 32
    109     // bits of the id.
    110     AtomicallyInitializedStatic(int, currentTransactionId = 0);
    111     return atomicIncrement(&currentTransactionId);
    112 }
    113 
    114 void IDBDatabase::ackReceivedBlobs(const Vector<blink::WebBlobInfo>* blobInfo)
    115 {
    116     ASSERT(blobInfo);
    117     if (!blobInfo->size() || !m_backend)
    118         return;
    119     Vector<blink::WebBlobInfo>::const_iterator iter;
    120     Vector<String> uuids;
    121     uuids.reserveCapacity(blobInfo->size());
    122     for (iter = blobInfo->begin(); iter != blobInfo->end(); ++iter)
    123         uuids.append(iter->uuid());
    124     m_backend->ackReceivedBlobs(uuids);
    125 }
    126 
    127 void IDBDatabase::indexCreated(int64_t objectStoreId, const IDBIndexMetadata& metadata)
    128 {
    129     IDBDatabaseMetadata::ObjectStoreMap::iterator it = m_metadata.objectStores.find(objectStoreId);
    130     ASSERT_WITH_SECURITY_IMPLICATION(it != m_metadata.objectStores.end());
    131     it->value.indexes.set(metadata.id, metadata);
    132 }
    133 
    134 void IDBDatabase::indexDeleted(int64_t objectStoreId, int64_t indexId)
    135 {
    136     IDBDatabaseMetadata::ObjectStoreMap::iterator it = m_metadata.objectStores.find(objectStoreId);
    137     ASSERT_WITH_SECURITY_IMPLICATION(it != m_metadata.objectStores.end());
    138     it->value.indexes.remove(indexId);
    139 }
    140 
    141 void IDBDatabase::transactionCreated(IDBTransaction* transaction)
    142 {
    143     ASSERT(transaction);
    144     ASSERT(!m_transactions.contains(transaction->id()));
    145     m_transactions.add(transaction->id(), transaction);
    146 
    147     if (transaction->isVersionChange()) {
    148         ASSERT(!m_versionChangeTransaction);
    149         m_versionChangeTransaction = transaction;
    150     }
    151 }
    152 
    153 void IDBDatabase::transactionFinished(const IDBTransaction* transaction)
    154 {
    155     ASSERT(transaction);
    156     ASSERT(m_transactions.contains(transaction->id()));
    157     ASSERT(m_transactions.get(transaction->id()) == transaction);
    158     m_transactions.remove(transaction->id());
    159 
    160     if (transaction->isVersionChange()) {
    161         ASSERT(m_versionChangeTransaction == transaction);
    162         m_versionChangeTransaction = nullptr;
    163     }
    164 
    165     if (m_closePending && m_transactions.isEmpty())
    166         closeConnection();
    167 }
    168 
    169 void IDBDatabase::onAbort(int64_t transactionId, PassRefPtrWillBeRawPtr<DOMError> error)
    170 {
    171     ASSERT(m_transactions.contains(transactionId));
    172     m_transactions.get(transactionId)->onAbort(error);
    173 }
    174 
    175 void IDBDatabase::onComplete(int64_t transactionId)
    176 {
    177     ASSERT(m_transactions.contains(transactionId));
    178     m_transactions.get(transactionId)->onComplete();
    179 }
    180 
    181 PassRefPtrWillBeRawPtr<DOMStringList> IDBDatabase::objectStoreNames() const
    182 {
    183     RefPtrWillBeRawPtr<DOMStringList> objectStoreNames = DOMStringList::create();
    184     for (IDBDatabaseMetadata::ObjectStoreMap::const_iterator it = m_metadata.objectStores.begin(); it != m_metadata.objectStores.end(); ++it)
    185         objectStoreNames->append(it->value.name);
    186     objectStoreNames->sort();
    187     return objectStoreNames.release();
    188 }
    189 
    190 ScriptValue IDBDatabase::version(ScriptState* scriptState) const
    191 {
    192     int64_t intVersion = m_metadata.intVersion;
    193     if (intVersion == IDBDatabaseMetadata::NoIntVersion)
    194         return idbAnyToScriptValue(scriptState, IDBAny::createString(m_metadata.version));
    195 
    196     return idbAnyToScriptValue(scriptState, IDBAny::create(intVersion));
    197 }
    198 
    199 IDBObjectStore* IDBDatabase::createObjectStore(const String& name, const Dictionary& options, ExceptionState& exceptionState)
    200 {
    201     IDBKeyPath keyPath;
    202     bool autoIncrement = false;
    203     if (!options.isUndefinedOrNull()) {
    204         String keyPathString;
    205         Vector<String> keyPathArray;
    206         if (options.get("keyPath", keyPathArray))
    207             keyPath = IDBKeyPath(keyPathArray);
    208         else if (options.getWithUndefinedOrNullCheck("keyPath", keyPathString))
    209             keyPath = IDBKeyPath(keyPathString);
    210 
    211         options.get("autoIncrement", autoIncrement);
    212     }
    213 
    214     return createObjectStore(name, keyPath, autoIncrement, exceptionState);
    215 }
    216 
    217 IDBObjectStore* IDBDatabase::createObjectStore(const String& name, const IDBKeyPath& keyPath, bool autoIncrement, ExceptionState& exceptionState)
    218 {
    219     IDB_TRACE("IDBDatabase::createObjectStore");
    220     blink::Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBCreateObjectStoreCall, IDBMethodsMax);
    221     if (!m_versionChangeTransaction) {
    222         exceptionState.throwDOMException(InvalidStateError, IDBDatabase::notVersionChangeTransactionErrorMessage);
    223         return 0;
    224     }
    225     if (m_versionChangeTransaction->isFinished() || m_versionChangeTransaction->isFinishing()) {
    226         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
    227         return 0;
    228     }
    229     if (!m_versionChangeTransaction->isActive()) {
    230         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
    231         return 0;
    232     }
    233 
    234     if (containsObjectStore(name)) {
    235         exceptionState.throwDOMException(ConstraintError, "An object store with the specified name already exists.");
    236         return 0;
    237     }
    238 
    239     if (!keyPath.isNull() && !keyPath.isValid()) {
    240         exceptionState.throwDOMException(SyntaxError, "The keyPath option is not a valid key path.");
    241         return 0;
    242     }
    243 
    244     if (autoIncrement && ((keyPath.type() == IDBKeyPath::StringType && keyPath.string().isEmpty()) || keyPath.type() == IDBKeyPath::ArrayType)) {
    245         exceptionState.throwDOMException(InvalidAccessError, "The autoIncrement option was set but the keyPath option was empty or an array.");
    246         return 0;
    247     }
    248 
    249     if (!m_backend) {
    250         exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databaseClosedErrorMessage);
    251         return 0;
    252     }
    253 
    254     int64_t objectStoreId = m_metadata.maxObjectStoreId + 1;
    255     m_backend->createObjectStore(m_versionChangeTransaction->id(), objectStoreId, name, keyPath, autoIncrement);
    256 
    257     IDBObjectStoreMetadata metadata(name, objectStoreId, keyPath, autoIncrement, WebIDBDatabase::minimumIndexId);
    258     IDBObjectStore* objectStore = IDBObjectStore::create(metadata, m_versionChangeTransaction.get());
    259     m_metadata.objectStores.set(metadata.id, metadata);
    260     ++m_metadata.maxObjectStoreId;
    261 
    262     m_versionChangeTransaction->objectStoreCreated(name, objectStore);
    263     return objectStore;
    264 }
    265 
    266 void IDBDatabase::deleteObjectStore(const String& name, ExceptionState& exceptionState)
    267 {
    268     IDB_TRACE("IDBDatabase::deleteObjectStore");
    269     blink::Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBDeleteObjectStoreCall, IDBMethodsMax);
    270     if (!m_versionChangeTransaction) {
    271         exceptionState.throwDOMException(InvalidStateError, IDBDatabase::notVersionChangeTransactionErrorMessage);
    272         return;
    273     }
    274     if (m_versionChangeTransaction->isFinished() || m_versionChangeTransaction->isFinishing()) {
    275         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage);
    276         return;
    277     }
    278     if (!m_versionChangeTransaction->isActive()) {
    279         exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage);
    280         return;
    281     }
    282 
    283     int64_t objectStoreId = findObjectStoreId(name);
    284     if (objectStoreId == IDBObjectStoreMetadata::InvalidId) {
    285         exceptionState.throwDOMException(NotFoundError, "The specified object store was not found.");
    286         return;
    287     }
    288 
    289     if (!m_backend) {
    290         exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databaseClosedErrorMessage);
    291         return;
    292     }
    293 
    294     m_backend->deleteObjectStore(m_versionChangeTransaction->id(), objectStoreId);
    295     m_versionChangeTransaction->objectStoreDeleted(name);
    296     m_metadata.objectStores.remove(objectStoreId);
    297 }
    298 
    299 IDBTransaction* IDBDatabase::transaction(ExecutionContext* context, const Vector<String>& scope, const String& modeString, ExceptionState& exceptionState)
    300 {
    301     IDB_TRACE("IDBDatabase::transaction");
    302     blink::Platform::current()->histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBTransactionCall, IDBMethodsMax);
    303     if (!scope.size()) {
    304         exceptionState.throwDOMException(InvalidAccessError, "The storeNames parameter was empty.");
    305         return 0;
    306     }
    307 
    308     blink::WebIDBTransactionMode mode = IDBTransaction::stringToMode(modeString, exceptionState);
    309     if (exceptionState.hadException())
    310         return 0;
    311 
    312     if (m_versionChangeTransaction) {
    313         exceptionState.throwDOMException(InvalidStateError, "A version change transaction is running.");
    314         return 0;
    315     }
    316 
    317     if (m_closePending) {
    318         exceptionState.throwDOMException(InvalidStateError, "The database connection is closing.");
    319         return 0;
    320     }
    321 
    322     Vector<int64_t> objectStoreIds;
    323     for (size_t i = 0; i < scope.size(); ++i) {
    324         int64_t objectStoreId = findObjectStoreId(scope[i]);
    325         if (objectStoreId == IDBObjectStoreMetadata::InvalidId) {
    326             exceptionState.throwDOMException(NotFoundError, "One of the specified object stores was not found.");
    327             return 0;
    328         }
    329         objectStoreIds.append(objectStoreId);
    330     }
    331 
    332     if (!m_backend) {
    333         exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databaseClosedErrorMessage);
    334         return 0;
    335     }
    336 
    337     int64_t transactionId = nextTransactionId();
    338     m_backend->createTransaction(transactionId, WebIDBDatabaseCallbacksImpl::create(m_databaseCallbacks).leakPtr(), objectStoreIds, mode);
    339 
    340     return IDBTransaction::create(context, transactionId, scope, mode, this);
    341 }
    342 
    343 IDBTransaction* IDBDatabase::transaction(ExecutionContext* context, const String& storeName, const String& mode, ExceptionState& exceptionState)
    344 {
    345     RefPtrWillBeRawPtr<DOMStringList> storeNames = DOMStringList::create();
    346     storeNames->append(storeName);
    347     return transaction(context, storeNames, mode, exceptionState);
    348 }
    349 
    350 void IDBDatabase::forceClose()
    351 {
    352     for (TransactionMap::const_iterator::Values it = m_transactions.begin().values(), end = m_transactions.end().values(); it != end; ++it)
    353         (*it)->abort(IGNORE_EXCEPTION);
    354     this->close();
    355     enqueueEvent(Event::create(EventTypeNames::close));
    356 }
    357 
    358 void IDBDatabase::close()
    359 {
    360     IDB_TRACE("IDBDatabase::close");
    361     if (m_closePending)
    362         return;
    363 
    364     m_closePending = true;
    365 
    366     if (m_transactions.isEmpty())
    367         closeConnection();
    368 }
    369 
    370 void IDBDatabase::closeConnection()
    371 {
    372     ASSERT(m_closePending);
    373     ASSERT(m_transactions.isEmpty());
    374 
    375     if (m_backend) {
    376         m_backend->close();
    377         m_backend.clear();
    378     }
    379 
    380     if (m_contextStopped || !executionContext())
    381         return;
    382 
    383     EventQueue* eventQueue = executionContext()->eventQueue();
    384     // Remove any pending versionchange events scheduled to fire on this
    385     // connection. They would have been scheduled by the backend when another
    386     // connection called setVersion, but the frontend connection is being
    387     // closed before they could fire.
    388     for (size_t i = 0; i < m_enqueuedEvents.size(); ++i) {
    389         bool removed = eventQueue->cancelEvent(m_enqueuedEvents[i].get());
    390         ASSERT_UNUSED(removed, removed);
    391     }
    392 }
    393 
    394 void IDBDatabase::onVersionChange(int64_t oldVersion, int64_t newVersion)
    395 {
    396     IDB_TRACE("IDBDatabase::onVersionChange");
    397     if (m_contextStopped || !executionContext())
    398         return;
    399 
    400     if (m_closePending)
    401         return;
    402 
    403     Nullable<unsigned long long> newVersionNullable = (newVersion == IDBDatabaseMetadata::NoIntVersion) ? Nullable<unsigned long long>() : Nullable<unsigned long long>(newVersion);
    404     enqueueEvent(IDBVersionChangeEvent::create(EventTypeNames::versionchange, oldVersion, newVersionNullable));
    405 }
    406 
    407 void IDBDatabase::enqueueEvent(PassRefPtrWillBeRawPtr<Event> event)
    408 {
    409     ASSERT(!m_contextStopped);
    410     ASSERT(executionContext());
    411     EventQueue* eventQueue = executionContext()->eventQueue();
    412     event->setTarget(this);
    413     eventQueue->enqueueEvent(event.get());
    414     m_enqueuedEvents.append(event);
    415 }
    416 
    417 bool IDBDatabase::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
    418 {
    419     IDB_TRACE("IDBDatabase::dispatchEvent");
    420     if (m_contextStopped || !executionContext())
    421         return false;
    422     ASSERT(event->type() == EventTypeNames::versionchange || event->type() == EventTypeNames::close);
    423     for (size_t i = 0; i < m_enqueuedEvents.size(); ++i) {
    424         if (m_enqueuedEvents[i].get() == event.get())
    425             m_enqueuedEvents.remove(i);
    426     }
    427     return EventTarget::dispatchEvent(event.get());
    428 }
    429 
    430 int64_t IDBDatabase::findObjectStoreId(const String& name) const
    431 {
    432     for (IDBDatabaseMetadata::ObjectStoreMap::const_iterator it = m_metadata.objectStores.begin(); it != m_metadata.objectStores.end(); ++it) {
    433         if (it->value.name == name) {
    434             ASSERT(it->key != IDBObjectStoreMetadata::InvalidId);
    435             return it->key;
    436         }
    437     }
    438     return IDBObjectStoreMetadata::InvalidId;
    439 }
    440 
    441 bool IDBDatabase::hasPendingActivity() const
    442 {
    443     // The script wrapper must not be collected before the object is closed or
    444     // we can't fire a "versionchange" event to let script manually close the connection.
    445     return !m_closePending && hasEventListeners() && !m_contextStopped;
    446 }
    447 
    448 void IDBDatabase::stop()
    449 {
    450     m_contextStopped = true;
    451 
    452     // Immediately close the connection to the back end. Don't attempt a
    453     // normal close() since that may wait on transactions which require a
    454     // round trip to the back-end to abort.
    455     if (m_backend) {
    456         m_backend->close();
    457         m_backend.clear();
    458     }
    459 }
    460 
    461 const AtomicString& IDBDatabase::interfaceName() const
    462 {
    463     return EventTargetNames::IDBDatabase;
    464 }
    465 
    466 ExecutionContext* IDBDatabase::executionContext() const
    467 {
    468     return ActiveDOMObject::executionContext();
    469 }
    470 
    471 } // namespace WebCore
    472