• 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 "PlatformString.h"
34 #include "SecurityOrigin.h"
35 #include "SQLiteDatabase.h"
36 #include "SQLTransaction.h"
37 #include "StringHash.h"
38 #include "Timer.h"
39 #include "VoidCallback.h"
40 
41 #include <wtf/Forward.h>
42 #include <wtf/HashSet.h>
43 #include <wtf/PassRefPtr.h>
44 #include <wtf/RefPtr.h>
45 #include <wtf/Deque.h>
46 #else
47 #include "PlatformString.h"
48 #endif
49 
50 #if ENABLE(DATABASE)
51 namespace WebCore {
52 
53 class DatabaseAuthorizer;
54 class DatabaseThread;
55 class ScriptExecutionContext;
56 class SQLResultSet;
57 class SQLTransactionCallback;
58 class SQLTransactionClient;
59 class SQLTransactionCoordinator;
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     static void setIsAvailable(bool);
71     static bool isAvailable();
72 
73     ~Database();
74 
75 // Direct support for the DOM API
76     static PassRefPtr<Database> openDatabase(ScriptExecutionContext* context, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize, ExceptionCode&);
77     String version() const;
78     void changeVersion(const String& oldVersion, const String& newVersion,
79                        PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback,
80                        PassRefPtr<VoidCallback> successCallback);
81     void transaction(PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback,
82                      PassRefPtr<VoidCallback> successCallback, bool readOnly);
83 
84 // Internal engine support
85     static const String& databaseInfoTableName();
86 
87     void disableAuthorizer();
88     void enableAuthorizer();
89     void setAuthorizerReadOnly();
90 
91     Vector<String> tableNames();
92 
scriptExecutionContext()93     ScriptExecutionContext* scriptExecutionContext() const { return m_scriptExecutionContext.get(); }
94     SecurityOrigin* securityOrigin() const;
95     String stringIdentifier() const;
96     String displayName() const;
97     unsigned long estimatedSize() const;
98     String fileName() const;
99 
100     bool getVersionFromDatabase(String&);
101     bool setVersionInDatabase(const String&);
102     void setExpectedVersion(const String&);
103     bool versionMatchesExpected() const;
104 
105     void markAsDeletedAndClose();
deleted()106     bool deleted() const { return m_deleted; }
107 
108     void close();
opened()109     bool opened() const { return m_opened; }
110 
111     void stop();
stopped()112     bool stopped() const { return m_stopped; }
113 
114     unsigned long long databaseSize() const;
115     unsigned long long maximumSize() const;
116 
117 // Called from DatabaseThread, must be prepared to work on the background thread
118     void resetAuthorizer();
119     void performPolicyChecks();
120 
121     bool performOpenAndVerify(ExceptionCode&);
122 
123     Vector<String> performGetTableNames();
124 
125     SQLTransactionClient* transactionClient() const;
126     SQLTransactionCoordinator* transactionCoordinator() const;
127 
128 private:
129     Database(ScriptExecutionContext* context, const String& name,
130              const String& expectedVersion, const String& displayName,
131              unsigned long estimatedSize);
132 
133     bool openAndVerifyVersion(ExceptionCode&);
134 
135     void scheduleTransaction();
136     void scheduleTransactionCallback(SQLTransaction*);
137     void scheduleTransactionStep(SQLTransaction* transaction, bool immediately = false);
138 
139     Deque<RefPtr<SQLTransaction> > m_transactionQueue;
140     Mutex m_transactionInProgressMutex;
141     bool m_transactionInProgress;
142     bool m_isTransactionQueueEnabled;
143 
144     static void deliverPendingCallback(void*);
145 
146     RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
147     RefPtr<SecurityOrigin> m_mainThreadSecurityOrigin;
148     RefPtr<SecurityOrigin> m_databaseThreadSecurityOrigin;
149     String m_name;
150     int m_guid;
151     String m_expectedVersion;
152     String m_displayName;
153     unsigned long m_estimatedSize;
154     String m_filename;
155 
156     bool m_deleted;
157 
158     bool m_stopped;
159 
160     bool m_opened;
161 
162     SQLiteDatabase m_sqliteDatabase;
163     RefPtr<DatabaseAuthorizer> m_databaseAuthorizer;
164 
165 #ifndef NDEBUG
databaseDebugName()166     String databaseDebugName() const { return m_mainThreadSecurityOrigin->toString() + "::" + m_name; }
167 #endif
168 };
169 
170 } // namespace WebCore
171 
172 #else
173 
174 namespace WebCore {
175 class Database : public ThreadSafeShared<Database> {
176 public:
177     static const String& databaseInfoTableName();
178 };
179 } // namespace WebCore
180 
181 #endif // ENABLE(DATABASE)
182 
183 #endif // Database_h
184