15af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke/*
22daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch * Copyright (C) 2011 Google Inc. All rights reserved.
35af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke *
45af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * Redistribution and use in source and binary forms, with or without
55af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * modification, are permitted provided that the following conditions
65af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * are met:
75af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke *
85af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * 1.  Redistributions of source code must retain the above copyright
95af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke *     notice, this list of conditions and the following disclaimer.
105af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * 2.  Redistributions in binary form must reproduce the above copyright
115af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke *     notice, this list of conditions and the following disclaimer in the
125af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke *     documentation and/or other materials provided with the distribution.
135af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke *
145af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
155af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
165af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
175af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
185af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
195af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
205af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
215af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
225af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
235af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke */
255af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
26dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#ifndef IDBObjectStoreBackendImpl_h
27dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#define IDBObjectStoreBackendImpl_h
285af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
29dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#include "IDBObjectStoreBackendInterface.h"
30545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch#include <wtf/HashMap.h>
31f486d19d62f1bc33246748b14b14a9dfa617b57fIain Merrick#include <wtf/text/StringHash.h>
325af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
335af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke#if ENABLE(INDEXED_DATABASE)
345af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
355af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkenamespace WebCore {
365af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
3781bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdochclass IDBBackingStore;
38e8b154fd68f9b33be40a3590e58347f353835f5cSteve Blockclass IDBDatabaseBackendImpl;
395abb8606fa57c3ebfc8b3c3dbc3fa4a25d2ae306Iain Merrickclass IDBIndexBackendImpl;
40bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsenclass IDBTransactionBackendInterface;
41a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochclass ScriptExecutionContext;
42e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
43dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdochclass IDBObjectStoreBackendImpl : public IDBObjectStoreBackendInterface {
44545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochpublic:
452daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    static PassRefPtr<IDBObjectStoreBackendImpl> create(IDBBackingStore* backingStore, int64_t databaseId, int64_t id, const String& name, const String& keyPath, bool autoIncrement)
46545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    {
472daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        return adoptRef(new IDBObjectStoreBackendImpl(backingStore, databaseId, id, name, keyPath, autoIncrement));
485af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    }
492daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    static PassRefPtr<IDBObjectStoreBackendImpl> create(IDBBackingStore* backingStore, int64_t databaseId, const String& name, const String& keyPath, bool autoIncrement)
50a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    {
512daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch        return adoptRef(new IDBObjectStoreBackendImpl(backingStore, databaseId, name, keyPath, autoIncrement));
52a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    }
53a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual ~IDBObjectStoreBackendImpl();
545af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
55e14391e94c850b8bd03680c23b38978db68687a8John Reck    static const int64_t InvalidId = 0;
56a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    int64_t id() const
57a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    {
58a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        ASSERT(m_id != InvalidId);
59a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        return m_id;
60a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    }
61a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    void setId(int64_t id) { m_id = id; }
622daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    int64_t databaseId() const { return m_databaseId; }
635af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
64a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual String name() const { return m_name; }
65a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual String keyPath() const { return m_keyPath; }
66a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual PassRefPtr<DOMStringList> indexNames() const;
67a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual bool autoIncrement() const { return m_autoIncrement; }
68e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
69a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual void get(PassRefPtr<IDBKey> key, PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
702fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    virtual void put(PassRefPtr<SerializedScriptValue>, PassRefPtr<IDBKey>, PutMode, PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
714576aa36e9a9671459299c7963ac95aa94beaea9Shimeng (Simon) Wang    virtual void deleteFunction(PassRefPtr<IDBKey> key, PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
722fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    virtual void clear(PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
735af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
74a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual PassRefPtr<IDBIndexBackendInterface> createIndex(const String& name, const String& keyPath, bool unique, IDBTransactionBackendInterface*, ExceptionCode&);
75a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual PassRefPtr<IDBIndexBackendInterface> index(const String& name, ExceptionCode&);
764576aa36e9a9671459299c7963ac95aa94beaea9Shimeng (Simon) Wang    virtual void deleteIndex(const String& name, IDBTransactionBackendInterface*, ExceptionCode&);
77a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
78a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual void openCursor(PassRefPtr<IDBKeyRange> range, unsigned short direction, PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
79dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch
80545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochprivate:
812daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    IDBObjectStoreBackendImpl(IDBBackingStore*, int64_t databaseId, int64_t id, const String& name, const String& keyPath, bool autoIncrement);
822daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    IDBObjectStoreBackendImpl(IDBBackingStore*, int64_t databaseId, const String& name, const String& keyPath, bool autoIncrement);
83e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
84e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    void loadIndexes();
85ab9e7a118cf1ea2e3a93dce683b2ded3e7291ddbBen Murdoch    PassRefPtr<IDBKey> genAutoIncrementKey();
862fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    void resetAutoIncrementKeyCache() { m_autoIncrementNumber = -1; }
8781bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch    static PassRefPtr<IDBKey> selectKeyForPut(IDBObjectStoreBackendImpl*, IDBKey*, PutMode, IDBCallbacks*, RefPtr<SerializedScriptValue>&);
88545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch
89a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    static void getInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBKey> key, PassRefPtr<IDBCallbacks>);
902fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    static void putInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<SerializedScriptValue>, PassRefPtr<IDBKey>, PutMode, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendInterface>);
914576aa36e9a9671459299c7963ac95aa94beaea9Shimeng (Simon) Wang    static void deleteInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBKey> key, PassRefPtr<IDBCallbacks>);
922fc2651226baac27029e38c9d6ef883fa32084dbSteve Block    static void clearInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBCallbacks>);
93a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    static void createIndexInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBTransactionBackendInterface>);
944576aa36e9a9671459299c7963ac95aa94beaea9Shimeng (Simon) Wang    static void deleteIndexInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBTransactionBackendInterface>);
95a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    static void openCursorInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBKeyRange> range, unsigned short direction, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendInterface>);
96a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
97a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    // These are used as setVersion transaction abort tasks.
98a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    static void removeIndexFromMap(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBIndexBackendImpl>);
99a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    static void addIndexToMap(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBIndexBackendImpl>);
100a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
10181bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch    RefPtr<IDBBackingStore> m_backingStore;
102e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
1032daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch    int64_t m_databaseId;
104e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    int64_t m_id;
105545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    String m_name;
106545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    String m_keyPath;
107545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    bool m_autoIncrement;
1085af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
1095abb8606fa57c3ebfc8b3c3dbc3fa4a25d2ae306Iain Merrick    typedef HashMap<String, RefPtr<IDBIndexBackendImpl> > IndexMap;
110545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    IndexMap m_indexes;
111ab9e7a118cf1ea2e3a93dce683b2ded3e7291ddbBen Murdoch    int m_autoIncrementNumber;
112545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch};
1135af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
1145af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke} // namespace WebCore
1155af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
116545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch#endif
1175af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
118dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#endif // IDBObjectStoreBackendImpl_h
119