• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 IDBObjectStoreBackendImpl_h
27 #define IDBObjectStoreBackendImpl_h
28 
29 #include "IDBObjectStoreBackendInterface.h"
30 #include <wtf/HashMap.h>
31 #include <wtf/text/StringHash.h>
32 
33 #if ENABLE(INDEXED_DATABASE)
34 
35 namespace WebCore {
36 
37 class IDBBackingStore;
38 class IDBDatabaseBackendImpl;
39 class IDBIndexBackendImpl;
40 class IDBTransactionBackendInterface;
41 class ScriptExecutionContext;
42 
43 class IDBObjectStoreBackendImpl : public IDBObjectStoreBackendInterface {
44 public:
create(IDBBackingStore * backingStore,int64_t databaseId,int64_t id,const String & name,const String & keyPath,bool autoIncrement)45     static PassRefPtr<IDBObjectStoreBackendImpl> create(IDBBackingStore* backingStore, int64_t databaseId, int64_t id, const String& name, const String& keyPath, bool autoIncrement)
46     {
47         return adoptRef(new IDBObjectStoreBackendImpl(backingStore, databaseId, id, name, keyPath, autoIncrement));
48     }
create(IDBBackingStore * backingStore,int64_t databaseId,const String & name,const String & keyPath,bool autoIncrement)49     static PassRefPtr<IDBObjectStoreBackendImpl> create(IDBBackingStore* backingStore, int64_t databaseId, const String& name, const String& keyPath, bool autoIncrement)
50     {
51         return adoptRef(new IDBObjectStoreBackendImpl(backingStore, databaseId, name, keyPath, autoIncrement));
52     }
53     virtual ~IDBObjectStoreBackendImpl();
54 
55     static const int64_t InvalidId = 0;
id()56     int64_t id() const
57     {
58         ASSERT(m_id != InvalidId);
59         return m_id;
60     }
setId(int64_t id)61     void setId(int64_t id) { m_id = id; }
databaseId()62     int64_t databaseId() const { return m_databaseId; }
63 
name()64     virtual String name() const { return m_name; }
keyPath()65     virtual String keyPath() const { return m_keyPath; }
66     virtual PassRefPtr<DOMStringList> indexNames() const;
autoIncrement()67     virtual bool autoIncrement() const { return m_autoIncrement; }
68 
69     virtual void get(PassRefPtr<IDBKey> key, PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
70     virtual void put(PassRefPtr<SerializedScriptValue>, PassRefPtr<IDBKey>, PutMode, PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
71     virtual void deleteFunction(PassRefPtr<IDBKey> key, PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
72     virtual void clear(PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
73 
74     virtual PassRefPtr<IDBIndexBackendInterface> createIndex(const String& name, const String& keyPath, bool unique, IDBTransactionBackendInterface*, ExceptionCode&);
75     virtual PassRefPtr<IDBIndexBackendInterface> index(const String& name, ExceptionCode&);
76     virtual void deleteIndex(const String& name, IDBTransactionBackendInterface*, ExceptionCode&);
77 
78     virtual void openCursor(PassRefPtr<IDBKeyRange> range, unsigned short direction, PassRefPtr<IDBCallbacks>, IDBTransactionBackendInterface*, ExceptionCode&);
79 
80 private:
81     IDBObjectStoreBackendImpl(IDBBackingStore*, int64_t databaseId, int64_t id, const String& name, const String& keyPath, bool autoIncrement);
82     IDBObjectStoreBackendImpl(IDBBackingStore*, int64_t databaseId, const String& name, const String& keyPath, bool autoIncrement);
83 
84     void loadIndexes();
85     PassRefPtr<IDBKey> genAutoIncrementKey();
resetAutoIncrementKeyCache()86     void resetAutoIncrementKeyCache() { m_autoIncrementNumber = -1; }
87     static PassRefPtr<IDBKey> selectKeyForPut(IDBObjectStoreBackendImpl*, IDBKey*, PutMode, IDBCallbacks*, RefPtr<SerializedScriptValue>&);
88 
89     static void getInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBKey> key, PassRefPtr<IDBCallbacks>);
90     static void putInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<SerializedScriptValue>, PassRefPtr<IDBKey>, PutMode, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendInterface>);
91     static void deleteInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBKey> key, PassRefPtr<IDBCallbacks>);
92     static void clearInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBCallbacks>);
93     static void createIndexInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBTransactionBackendInterface>);
94     static void deleteIndexInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBTransactionBackendInterface>);
95     static void openCursorInternal(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBKeyRange> range, unsigned short direction, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendInterface>);
96 
97     // These are used as setVersion transaction abort tasks.
98     static void removeIndexFromMap(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBIndexBackendImpl>);
99     static void addIndexToMap(ScriptExecutionContext*, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBIndexBackendImpl>);
100 
101     RefPtr<IDBBackingStore> m_backingStore;
102 
103     int64_t m_databaseId;
104     int64_t m_id;
105     String m_name;
106     String m_keyPath;
107     bool m_autoIncrement;
108 
109     typedef HashMap<String, RefPtr<IDBIndexBackendImpl> > IndexMap;
110     IndexMap m_indexes;
111     int m_autoIncrementNumber;
112 };
113 
114 } // namespace WebCore
115 
116 #endif
117 
118 #endif // IDBObjectStoreBackendImpl_h
119