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#ifndef IDBObjectStore_h
27#define IDBObjectStore_h
28
29#include "bindings/v8/Dictionary.h"
30#include "bindings/v8/ScriptWrappable.h"
31#include "bindings/v8/SerializedScriptValue.h"
32#include "modules/indexeddb/IDBCursor.h"
33#include "modules/indexeddb/IDBIndex.h"
34#include "modules/indexeddb/IDBKey.h"
35#include "modules/indexeddb/IDBKeyRange.h"
36#include "modules/indexeddb/IDBMetadata.h"
37#include "modules/indexeddb/IDBRequest.h"
38#include "modules/indexeddb/IDBTransaction.h"
39#include "wtf/PassRefPtr.h"
40#include "wtf/RefCounted.h"
41#include "wtf/RefPtr.h"
42#include "wtf/text/WTFString.h"
43
44namespace WebCore {
45
46class DOMStringList;
47class IDBAny;
48class ExceptionState;
49
50class IDBObjectStore : public ScriptWrappable, public RefCounted<IDBObjectStore> {
51public:
52    static PassRefPtr<IDBObjectStore> create(const IDBObjectStoreMetadata& metadata, IDBTransaction* transaction)
53    {
54        return adoptRef(new IDBObjectStore(metadata, transaction));
55    }
56    ~IDBObjectStore() { }
57
58    // Implement the IDBObjectStore IDL
59    int64_t id() const { return m_metadata.id; }
60    const String name() const { return m_metadata.name; }
61    PassRefPtr<IDBAny> keyPathAny() const { return IDBAny::create(m_metadata.keyPath); }
62    const IDBKeyPath keyPath() const { return m_metadata.keyPath; }
63    PassRefPtr<DOMStringList> indexNames() const;
64    PassRefPtr<IDBTransaction> transaction() const { return m_transaction; }
65    bool autoIncrement() const { return m_metadata.autoIncrement; }
66
67    PassRefPtr<IDBRequest> openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> range, const String& direction, ExceptionState& es) { return openCursor(context, range, direction, IDBDatabaseBackendInterface::NormalTask, es); }
68    PassRefPtr<IDBRequest> openCursor(ScriptExecutionContext*, const ScriptValue& key, const String& direction, ExceptionState&);
69    PassRefPtr<IDBRequest> get(ScriptExecutionContext*, const ScriptValue& key, ExceptionState&);
70    PassRefPtr<IDBRequest> get(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, ExceptionState&);
71    PassRefPtr<IDBRequest> add(ScriptState*, ScriptValue&, const ScriptValue& key, ExceptionState&);
72    PassRefPtr<IDBRequest> put(ScriptState*, ScriptValue&, const ScriptValue& key, ExceptionState&);
73    PassRefPtr<IDBRequest> deleteFunction(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, ExceptionState&);
74    PassRefPtr<IDBRequest> deleteFunction(ScriptExecutionContext*, const ScriptValue& key, ExceptionState&);
75    PassRefPtr<IDBRequest> clear(ScriptExecutionContext*, ExceptionState&);
76
77    PassRefPtr<IDBIndex> createIndex(ScriptExecutionContext* context, const String& name, const String& keyPath, const Dictionary& options, ExceptionState& es) { return createIndex(context, name, IDBKeyPath(keyPath), options, es); }
78    PassRefPtr<IDBIndex> createIndex(ScriptExecutionContext* context, const String& name, const Vector<String>& keyPath, const Dictionary& options, ExceptionState& es) { return createIndex(context, name, IDBKeyPath(keyPath), options, es); }
79    PassRefPtr<IDBIndex> index(const String& name, ExceptionState&);
80    void deleteIndex(const String& name, ExceptionState&);
81
82    PassRefPtr<IDBRequest> count(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, ExceptionState&);
83    PassRefPtr<IDBRequest> count(ScriptExecutionContext*, const ScriptValue& key, ExceptionState&);
84
85    // Used by IDBCursor::update():
86    PassRefPtr<IDBRequest> put(IDBDatabaseBackendInterface::PutMode, PassRefPtr<IDBAny> source, ScriptState*, ScriptValue&, PassRefPtr<IDBKey>, ExceptionState&);
87
88    void markDeleted() { m_deleted = true; }
89    bool isDeleted() const { return m_deleted; }
90    void transactionFinished();
91
92    IDBObjectStoreMetadata metadata() const { return m_metadata; }
93    void setMetadata(const IDBObjectStoreMetadata& metadata) { m_metadata = metadata; }
94
95    typedef Vector<RefPtr<IDBKey> > IndexKeys;
96    typedef HashMap<String, IndexKeys> IndexKeyMap;
97
98    IDBDatabaseBackendInterface* backendDB() const;
99
100private:
101    IDBObjectStore(const IDBObjectStoreMetadata&, IDBTransaction*);
102
103    PassRefPtr<IDBRequest> openCursor(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, const String& direction, IDBDatabaseBackendInterface::TaskType, ExceptionState&);
104    PassRefPtr<IDBIndex> createIndex(ScriptExecutionContext*, const String& name, const IDBKeyPath&, const Dictionary&, ExceptionState&);
105    PassRefPtr<IDBIndex> createIndex(ScriptExecutionContext*, const String& name, const IDBKeyPath&, bool unique, bool multiEntry, ExceptionState&);
106    PassRefPtr<IDBRequest> put(IDBDatabaseBackendInterface::PutMode, PassRefPtr<IDBAny> source, ScriptState*, ScriptValue&, const ScriptValue& key, ExceptionState&);
107
108    int64_t findIndexId(const String& name) const;
109    bool containsIndex(const String& name) const
110    {
111        return findIndexId(name) != IDBIndexMetadata::InvalidId;
112    }
113
114    IDBObjectStoreMetadata m_metadata;
115    RefPtr<IDBTransaction> m_transaction;
116    bool m_deleted;
117
118    typedef HashMap<String, RefPtr<IDBIndex> > IDBIndexMap;
119    IDBIndexMap m_indexMap;
120};
121
122} // namespace WebCore
123
124#endif // IDBObjectStore_h
125