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 IDBDatabaseBackendImpl_h
27 #define IDBDatabaseBackendImpl_h
28 
29 #include "IDBCallbacks.h"
30 #include "IDBDatabase.h"
31 #include <wtf/Deque.h>
32 #include <wtf/HashMap.h>
33 #include <wtf/ListHashSet.h>
34 
35 #if ENABLE(INDEXED_DATABASE)
36 
37 namespace WebCore {
38 
39 class IDBBackingStore;
40 class IDBDatabase;
41 class IDBFactoryBackendImpl;
42 class IDBObjectStoreBackendImpl;
43 class IDBTransactionCoordinator;
44 
45 class IDBDatabaseBackendImpl : public IDBDatabaseBackendInterface {
46 public:
create(const String & name,IDBBackingStore * database,IDBTransactionCoordinator * coordinator,IDBFactoryBackendImpl * factory,const String & uniqueIdentifier)47     static PassRefPtr<IDBDatabaseBackendImpl> create(const String& name, IDBBackingStore* database, IDBTransactionCoordinator* coordinator, IDBFactoryBackendImpl* factory, const String& uniqueIdentifier)
48     {
49         return adoptRef(new IDBDatabaseBackendImpl(name, database, coordinator, factory, uniqueIdentifier));
50     }
51     virtual ~IDBDatabaseBackendImpl();
52 
53     PassRefPtr<IDBBackingStore> backingStore() const;
54 
55     static const int64_t InvalidId = 0;
id()56     int64_t id() const { return m_id; }
57     void open(PassRefPtr<IDBDatabaseCallbacks>);
58 
name()59     virtual String name() const { return m_name; }
version()60     virtual String version() const { return m_version; }
61     virtual PassRefPtr<DOMStringList> objectStoreNames() const;
62 
63     virtual PassRefPtr<IDBObjectStoreBackendInterface> createObjectStore(const String& name, const String& keyPath, bool autoIncrement, IDBTransactionBackendInterface*, ExceptionCode&);
64     virtual void deleteObjectStore(const String& name, IDBTransactionBackendInterface*, ExceptionCode&);
65     virtual void setVersion(const String& version, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, ExceptionCode&);
66     virtual PassRefPtr<IDBTransactionBackendInterface> transaction(DOMStringList* objectStoreNames, unsigned short mode, ExceptionCode&);
67     virtual void close(PassRefPtr<IDBDatabaseCallbacks>);
68 
69     PassRefPtr<IDBObjectStoreBackendInterface> objectStore(const String& name);
transactionCoordinator()70     IDBTransactionCoordinator* transactionCoordinator() const { return m_transactionCoordinator.get(); }
71 
72 private:
73     IDBDatabaseBackendImpl(const String& name, IDBBackingStore* database, IDBTransactionCoordinator*, IDBFactoryBackendImpl*, const String& uniqueIdentifier);
74 
75     void loadObjectStores();
76 
77     static void createObjectStoreInternal(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackendImpl>, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBTransactionBackendInterface>);
78     static void deleteObjectStoreInternal(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackendImpl>, PassRefPtr<IDBObjectStoreBackendImpl>, PassRefPtr<IDBTransactionBackendInterface>);
79     static void setVersionInternal(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackendImpl>, const String& version, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendInterface>);
80 
81     // These are used as setVersion transaction abort tasks.
82     static void removeObjectStoreFromMap(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackendImpl>, PassRefPtr<IDBObjectStoreBackendImpl>);
83     static void addObjectStoreToMap(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackendImpl>, PassRefPtr<IDBObjectStoreBackendImpl>);
84     static void resetVersion(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackendImpl>, const String& version);
85 
86     RefPtr<IDBBackingStore> m_backingStore;
87     int64 m_id;
88     String m_name;
89     String m_version;
90 
91     String m_identifier;
92     // This might not need to be a RefPtr since the factory's lifetime is that of the page group, but it's better to be conservitive than sorry.
93     RefPtr<IDBFactoryBackendImpl> m_factory;
94 
95     typedef HashMap<String, RefPtr<IDBObjectStoreBackendImpl> > ObjectStoreMap;
96     ObjectStoreMap m_objectStores;
97 
98     RefPtr<IDBTransactionCoordinator> m_transactionCoordinator;
99 
100     class PendingSetVersionCall;
101     Deque<RefPtr<PendingSetVersionCall> > m_pendingSetVersionCalls;
102 
103     typedef ListHashSet<RefPtr<IDBDatabaseCallbacks> > DatabaseCallbacksSet;
104     DatabaseCallbacksSet m_databaseCallbacksSet;
105 };
106 
107 } // namespace WebCore
108 
109 #endif
110 
111 #endif // IDBDatabaseBackendImpl_h
112