• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008 Apple 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  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef Database_h
30 #define Database_h
31 
32 #if ENABLE(DATABASE)
33 #include <wtf/MessageQueue.h>
34 #include "PlatformString.h"
35 #include "SecurityOrigin.h"
36 #include "SQLiteDatabase.h"
37 #include "SQLTransaction.h"
38 #include "StringHash.h"
39 #include "Timer.h"
40 #include "VoidCallback.h"
41 
42 #include <wtf/Forward.h>
43 #include <wtf/HashSet.h>
44 #include <wtf/OwnPtr.h>
45 #include <wtf/PassRefPtr.h>
46 #include <wtf/RefPtr.h>
47 #include <wtf/Deque.h>
48 #else
49 #include "PlatformString.h"
50 #endif
51 
52 #if ENABLE(DATABASE)
53 namespace WebCore {
54 
55 class DatabaseAuthorizer;
56 class DatabaseThread;
57 class Document;
58 class SQLResultSet;
59 class SQLTransactionCallback;
60 class SQLTransactionErrorCallback;
61 class SQLValue;
62 
63 typedef int ExceptionCode;
64 
65 class Database : public ThreadSafeShared<Database> {
66     friend class DatabaseTransactionTask;
67     friend class SQLStatement;
68     friend class SQLTransaction;
69 public:
70     ~Database();
71 
72 // Direct support for the DOM API
73     static PassRefPtr<Database> openDatabase(Document* document, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize, ExceptionCode&);
74     String version() const;
75     void changeVersion(const String& oldVersion, const String& newVersion,
76                        PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback,
77                        PassRefPtr<VoidCallback> successCallback);
78     void transaction(PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback,
79                      PassRefPtr<VoidCallback> successCallback);
80 
81 // Internal engine support
82     static const String& databaseInfoTableName();
83 
84     void disableAuthorizer();
85     void enableAuthorizer();
86     void setAuthorizerReadOnly();
87 
88     Vector<String> tableNames();
89 
document()90     Document* document() const { return m_document.get(); }
91     PassRefPtr<SecurityOrigin> securityOriginCopy() const;
92     String stringIdentifier() const;
93 
94     bool getVersionFromDatabase(String&);
95     bool setVersionInDatabase(const String&);
96     void setExpectedVersion(const String&);
97     bool versionMatchesExpected() const;
98 
99     void markAsDeletedAndClose();
deleted()100     bool deleted() const { return m_deleted; }
101 
102     void close();
opened()103     bool opened() const { return m_opened; }
104 
105     void stop();
stopped()106     bool stopped() const { return m_stopped; }
107 
108     unsigned long long databaseSize() const;
109     unsigned long long maximumSize() const;
110 
111 // Called from DatabaseThread, must be prepared to work on the background thread
112     void resetAuthorizer();
113     void performPolicyChecks();
114 
115     bool performOpenAndVerify(ExceptionCode&);
116 
117     Vector<String> performGetTableNames();
118 
119 private:
120     Database(Document* document, const String& name, const String& expectedVersion);
121 
122     bool openAndVerifyVersion(ExceptionCode&);
123 
124     void scheduleTransaction();
125     void scheduleTransactionCallback(SQLTransaction*);
126     void scheduleTransactionStep(SQLTransaction* transaction);
127 
128     MessageQueue<RefPtr<SQLTransaction> > m_transactionQueue;
129     Mutex m_transactionInProgressMutex;
130     bool m_transactionInProgress;
131 
132     static void deliverPendingCallback(void*);
133 
134     RefPtr<Document> m_document;
135     RefPtr<SecurityOrigin> m_securityOrigin;
136     String m_name;
137     int m_guid;
138     String m_expectedVersion;
139     String m_filename;
140 
141     bool m_deleted;
142 
143     bool m_stopped;
144 
145     bool m_opened;
146 
147     SQLiteDatabase m_sqliteDatabase;
148     RefPtr<DatabaseAuthorizer> m_databaseAuthorizer;
149 
150 #ifndef NDEBUG
databaseDebugName()151     String databaseDebugName() const { return m_securityOrigin->toString() + "::" + m_name; }
152 #endif
153 };
154 
155 } // namespace WebCore
156 
157 #else
158 
159 namespace WebCore {
160 class Database : public ThreadSafeShared<Database> {
161 public:
162     static const String& databaseInfoTableName();
163 };
164 } // namespace WebCore
165 
166 #endif // ENABLE(DATABASE)
167 
168 #endif // Database_h
169