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 IDBDatabase_h
27 #define IDBDatabase_h
28 
29 #include "ActiveDOMObject.h"
30 #include "DOMStringList.h"
31 #include "Event.h"
32 #include "EventTarget.h"
33 #include "ExceptionCode.h"
34 #include "IDBDatabaseBackendInterface.h"
35 #include "IDBDatabaseCallbacksImpl.h"
36 #include "IDBObjectStore.h"
37 #include "IDBTransaction.h"
38 #include "OptionsObject.h"
39 #include <wtf/PassRefPtr.h>
40 #include <wtf/RefCounted.h>
41 #include <wtf/RefPtr.h>
42 
43 #if ENABLE(INDEXED_DATABASE)
44 
45 namespace WebCore {
46 
47 class IDBVersionChangeRequest;
48 class ScriptExecutionContext;
49 
50 class IDBDatabase : public RefCounted<IDBDatabase>, public EventTarget, public ActiveDOMObject {
51 public:
52     static PassRefPtr<IDBDatabase> create(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackendInterface>);
53     ~IDBDatabase();
54 
55     void setSetVersionTransaction(IDBTransaction*);
56 
57     // Implement the IDL
name()58     String name() const { return m_backend->name(); }
version()59     String version() const { return m_backend->version(); }
objectStoreNames()60     PassRefPtr<DOMStringList> objectStoreNames() const { return m_backend->objectStoreNames(); }
61 
62     // FIXME: Try to modify the code generator so this is unneeded.
createObjectStore(const String & name,ExceptionCode & ec)63     PassRefPtr<IDBObjectStore> createObjectStore(const String& name, ExceptionCode& ec) { return createObjectStore(name, OptionsObject(), ec); }
transaction(ScriptExecutionContext * context,ExceptionCode & ec)64     PassRefPtr<IDBTransaction> transaction(ScriptExecutionContext* context, ExceptionCode& ec) { return transaction(context, 0, ec); }
transaction(ScriptExecutionContext * context,PassRefPtr<DOMStringList> storeNames,ExceptionCode & ec)65     PassRefPtr<IDBTransaction> transaction(ScriptExecutionContext* context, PassRefPtr<DOMStringList> storeNames, ExceptionCode& ec) { return transaction(context, storeNames, IDBTransaction::READ_ONLY, ec); }
66     PassRefPtr<IDBTransaction> transaction(ScriptExecutionContext*, PassRefPtr<DOMStringList>, unsigned short mode, ExceptionCode&);
67 
68     PassRefPtr<IDBObjectStore> createObjectStore(const String& name, const OptionsObject&, ExceptionCode&);
69     void deleteObjectStore(const String& name, ExceptionCode&);
70     PassRefPtr<IDBVersionChangeRequest> setVersion(ScriptExecutionContext*, const String& version, ExceptionCode&);
71     void close();
72 
73     DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
74     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
75     DEFINE_ATTRIBUTE_EVENT_LISTENER(versionchange);
76 
77     // IDBDatabaseCallbacks
78     virtual void onVersionChange(const String& requestedVersion);
79 
80     // ActiveDOMObject
81     virtual bool hasPendingActivity() const;
82     virtual void stop();
83 
84     // EventTarget
toIDBDatabase()85     virtual IDBDatabase* toIDBDatabase() { return this; }
86     virtual ScriptExecutionContext* scriptExecutionContext() const;
87 
88 
89     void open();
90     void enqueueEvent(PassRefPtr<Event>);
dispatchEvent(PassRefPtr<Event> event,ExceptionCode & ec)91     bool dispatchEvent(PassRefPtr<Event> event, ExceptionCode& ec) { return EventTarget::dispatchEvent(event, ec); }
92     virtual bool dispatchEvent(PassRefPtr<Event>);
93 
94     using RefCounted<IDBDatabase>::ref;
95     using RefCounted<IDBDatabase>::deref;
96 
97 private:
98     IDBDatabase(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackendInterface>);
99 
100     // EventTarget
refEventTarget()101     virtual void refEventTarget() { ref(); }
derefEventTarget()102     virtual void derefEventTarget() { deref(); }
103     virtual EventTargetData* eventTargetData();
104     virtual EventTargetData* ensureEventTargetData();
105 
106     RefPtr<IDBDatabaseBackendInterface> m_backend;
107     RefPtr<IDBTransaction> m_setVersionTransaction;
108 
109     bool m_noNewTransactions;
110     bool m_stopped;
111 
112     EventTargetData m_eventTargetData;
113 
114     // Keep track of the versionchange events waiting to be fired on this
115     // database so that we can cancel them if the database closes.
116     Vector<RefPtr<Event> > m_enqueuedEvents;
117 
118     RefPtr<IDBDatabaseCallbacksImpl> m_databaseCallbacks;
119 };
120 
121 } // namespace WebCore
122 
123 #endif
124 
125 #endif // IDBDatabase_h
126