1/*
2 * Copyright (C) 2007, 2008, 2013, 2015 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 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#pragma once
30
31#include "ExceptionOr.h"
32#include "SQLiteDatabase.h"
33#include <wtf/Deque.h>
34#include <wtf/Lock.h>
35
36namespace WebCore {
37
38class DatabaseCallback;
39class DatabaseContext;
40class DatabaseDetails;
41class DatabaseThread;
42class ScriptExecutionContext;
43class SecurityOrigin;
44class SQLTransaction;
45class SQLTransactionBackend;
46class SQLTransactionCallback;
47class SQLTransactionCoordinator;
48class SQLTransactionErrorCallback;
49class SQLTransactionWrapper;
50class VoidCallback;
51struct SecurityOriginData;
52
53using DatabaseGUID = int;
54
55class Database : public ThreadSafeRefCounted<Database> {
56public:
57 ~Database();
58
59 ExceptionOr<void> openAndVerifyVersion(bool setVersionInNewDatabase);
60 void close();
61
62 void interrupt();
63
64 bool opened() const { return m_opened; }
65 bool isNew() const { return m_new; }
66
67 unsigned long long maximumSize();
68
69 void scheduleTransactionStep(SQLTransaction&);
70 void inProgressTransactionCompleted();
71
72 bool hasPendingTransaction();
73
74 bool hasPendingCreationEvent() const { return m_hasPendingCreationEvent; }
75 void setHasPendingCreationEvent(bool value) { m_hasPendingCreationEvent = value; }
76
77 void didCommitWriteTransaction();
78 bool didExceedQuota();
79
80 SQLTransactionCoordinator* transactionCoordinator();
81
82 // Direct support for the DOM API
83 String version() const;
84 void changeVersion(const String& oldVersion, const String& newVersion, RefPtr<SQLTransactionCallback>&&, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<VoidCallback>&& successCallback);
85 void transaction(RefPtr<SQLTransactionCallback>&&, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<VoidCallback>&& successCallback);
86 void readTransaction(RefPtr<SQLTransactionCallback>&&, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<VoidCallback>&& successCallback);
87
88 // Internal engine support
89 String stringIdentifier() const;
90 String displayName() const;
91 String expectedVersion() const;
92 unsigned long long estimatedSize() const;
93 String fileName() const;
94 DatabaseDetails details() const;
95 SQLiteDatabase& sqliteDatabase() { return m_sqliteDatabase; }
96
97 void disableAuthorizer();
98 void enableAuthorizer();
99 void setAuthorizerPermissions(int);
100 bool lastActionChangedDatabase();
101 bool lastActionWasInsert();
102 void resetDeletes();
103 bool hadDeletes();
104 void resetAuthorizer();
105
106 DatabaseContext& databaseContext() { return m_databaseContext; }
107 DatabaseThread& databaseThread();
108 ScriptExecutionContext& scriptExecutionContext() { return m_scriptExecutionContext; }
109 void logErrorMessage(const String& message);
110
111 Vector<String> tableNames();
112
113 SecurityOriginData securityOrigin();
114
115 void markAsDeletedAndClose();
116 bool deleted() const { return m_deleted; }
117
118 void scheduleTransactionCallback(SQLTransaction*);
119
120 void incrementalVacuumIfNeeded();
121
122 // Called from DatabaseTask
123 ExceptionOr<void> performOpenAndVerify(bool shouldSetVersionInNewDatabase);
124 Vector<String> performGetTableNames();
125
126 // Called from DatabaseTask and DatabaseThread
127 void performClose();
128
129private:
130 Database(DatabaseContext&, const String& name, const String& expectedVersion, const String& displayName, unsigned long long estimatedSize);
131
132 void closeDatabase();
133
134 bool getVersionFromDatabase(String& version, bool shouldCacheVersion = true);
135 bool setVersionInDatabase(const String& version, bool shouldCacheVersion = true);
136 void setExpectedVersion(const String&);
137 String getCachedVersion() const;
138 void setCachedVersion(const String&);
139 bool getActualVersionForTransaction(String& version);
140 void setEstimatedSize(unsigned long long);
141
142 void scheduleTransaction();
143
144 void runTransaction(RefPtr<SQLTransactionCallback>&&, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionWrapper>&&, bool readOnly);
145
146#if !LOG_DISABLED || !ERROR_DISABLED
147 String databaseDebugName() const;
148#endif
149
150 Ref<ScriptExecutionContext> m_scriptExecutionContext;
151 Ref<SecurityOrigin> m_contextThreadSecurityOrigin;
152 Ref<SecurityOrigin> m_databaseThreadSecurityOrigin;
153 Ref<DatabaseContext> m_databaseContext;
154
155 bool m_deleted { false };
156 bool m_hasPendingCreationEvent { false };
157
158 String m_name;
159 String m_expectedVersion;
160 String m_displayName;
161 unsigned long long m_estimatedSize;
162 String m_filename;
163
164 DatabaseGUID m_guid;
165 bool m_opened { false };
166 bool m_new { false };
167
168 SQLiteDatabase m_sqliteDatabase;
169
170 Ref<DatabaseAuthorizer> m_databaseAuthorizer;
171
172 Deque<Ref<SQLTransaction>> m_transactionQueue;
173 Lock m_transactionInProgressMutex;
174 bool m_transactionInProgress { false };
175 bool m_isTransactionQueueEnabled { true };
176
177 friend class ChangeVersionWrapper;
178 friend class DatabaseManager;
179 friend class SQLTransaction;
180 friend class SQLTransactionBackend;
181};
182
183} // namespace WebCore
184