Home | History | Annotate | Download | only in storage
      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 "IDBObjectStore.h"
     28 
     29 #if ENABLE(INDEXED_DATABASE)
     30 
     31 #include "DOMStringList.h"
     32 #include "IDBAny.h"
     33 #include "IDBDatabaseException.h"
     34 #include "IDBIndex.h"
     35 #include "IDBKey.h"
     36 #include "IDBKeyRange.h"
     37 #include "IDBTransaction.h"
     38 #include "SerializedScriptValue.h"
     39 #include <wtf/UnusedParam.h>
     40 
     41 namespace WebCore {
     42 
     43 static const unsigned short defaultDirection = IDBCursor::NEXT;
     44 
     45 IDBObjectStore::IDBObjectStore(PassRefPtr<IDBObjectStoreBackendInterface> idbObjectStore, IDBTransaction* transaction)
     46     : m_objectStore(idbObjectStore)
     47     , m_transaction(transaction)
     48 {
     49     ASSERT(m_objectStore);
     50     ASSERT(m_transaction);
     51     // We pass a reference to this object before it can be adopted.
     52     relaxAdoptionRequirement();
     53 }
     54 
     55 String IDBObjectStore::name() const
     56 {
     57     return m_objectStore->name();
     58 }
     59 
     60 String IDBObjectStore::keyPath() const
     61 {
     62     return m_objectStore->keyPath();
     63 }
     64 
     65 PassRefPtr<DOMStringList> IDBObjectStore::indexNames() const
     66 {
     67     return m_objectStore->indexNames();
     68 }
     69 
     70 PassRefPtr<IDBRequest> IDBObjectStore::get(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
     71 {
     72     RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
     73     m_objectStore->get(key, request, m_transaction->backend(), ec);
     74     if (ec) {
     75         request->markEarlyDeath();
     76         return 0;
     77     }
     78     return request.release();
     79 }
     80 
     81 PassRefPtr<IDBRequest> IDBObjectStore::add(ScriptExecutionContext* context, PassRefPtr<SerializedScriptValue> value, PassRefPtr<IDBKey> key, ExceptionCode& ec)
     82 {
     83     RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
     84     m_objectStore->put(value, key, IDBObjectStoreBackendInterface::AddOnly, request, m_transaction->backend(), ec);
     85     if (ec) {
     86         request->markEarlyDeath();
     87         return 0;
     88     }
     89     return request.release();
     90 }
     91 
     92 PassRefPtr<IDBRequest> IDBObjectStore::put(ScriptExecutionContext* context, PassRefPtr<SerializedScriptValue> value, PassRefPtr<IDBKey> key, ExceptionCode& ec)
     93 {
     94     RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
     95     m_objectStore->put(value, key, IDBObjectStoreBackendInterface::AddOrUpdate, request, m_transaction->backend(), ec);
     96     if (ec) {
     97         request->markEarlyDeath();
     98         return 0;
     99     }
    100     return request.release();
    101 }
    102 
    103 PassRefPtr<IDBRequest> IDBObjectStore::deleteFunction(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
    104 {
    105     RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
    106     m_objectStore->deleteFunction(key, request, m_transaction->backend(), ec);
    107     if (ec) {
    108         request->markEarlyDeath();
    109         return 0;
    110     }
    111     return request.release();
    112 }
    113 
    114 PassRefPtr<IDBRequest> IDBObjectStore::clear(ScriptExecutionContext* context, ExceptionCode& ec)
    115 {
    116     RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
    117     m_objectStore->clear(request, m_transaction->backend(), ec);
    118     if (ec) {
    119         request->markEarlyDeath();
    120         return 0;
    121     }
    122     return request.release();
    123 }
    124 
    125 PassRefPtr<IDBIndex> IDBObjectStore::createIndex(const String& name, const String& keyPath, const OptionsObject& options, ExceptionCode& ec)
    126 {
    127     bool unique = false;
    128     options.getKeyBool("unique", unique);
    129 
    130     RefPtr<IDBIndexBackendInterface> index = m_objectStore->createIndex(name, keyPath, unique, m_transaction->backend(), ec);
    131     ASSERT(!index != !ec); // If we didn't get an index, we should have gotten an exception code. And vice versa.
    132     if (!index)
    133         return 0;
    134     return IDBIndex::create(index.release(), this, m_transaction.get());
    135 }
    136 
    137 PassRefPtr<IDBIndex> IDBObjectStore::index(const String& name, ExceptionCode& ec)
    138 {
    139     RefPtr<IDBIndexBackendInterface> index = m_objectStore->index(name, ec);
    140     ASSERT(!index != !ec); // If we didn't get an index, we should have gotten an exception code. And vice versa.
    141     if (!index)
    142         return 0;
    143     return IDBIndex::create(index.release(), this, m_transaction.get());
    144 }
    145 
    146 void IDBObjectStore::deleteIndex(const String& name, ExceptionCode& ec)
    147 {
    148     m_objectStore->deleteIndex(name, m_transaction->backend(), ec);
    149 }
    150 
    151 PassRefPtr<IDBRequest> IDBObjectStore::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> range, unsigned short direction, ExceptionCode& ec)
    152 {
    153     if (direction != IDBCursor::NEXT && direction != IDBCursor::NEXT_NO_DUPLICATE && direction != IDBCursor::PREV && direction != IDBCursor::PREV_NO_DUPLICATE) {
    154         // FIXME: May need to change when specced: http://www.w3.org/Bugs/Public/show_bug.cgi?id=11406
    155         ec = IDBDatabaseException::CONSTRAINT_ERR;
    156         return 0;
    157     }
    158 
    159     RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
    160     request->setCursorType(IDBCursorBackendInterface::ObjectStoreCursor);
    161     m_objectStore->openCursor(range, direction, request, m_transaction->backend(), ec);
    162     if (ec) {
    163         request->markEarlyDeath();
    164         return 0;
    165     }
    166     return request.release();
    167 }
    168 
    169 } // namespace WebCore
    170 
    171 #endif // ENABLE(INDEXED_DATABASE)
    172