1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15 *     its contributors may be used to endorse or promote products derived
16 *     from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef DatabaseBackendBase_h
31#define DatabaseBackendBase_h
32
33#if ENABLE(SQL_DATABASE)
34
35#include "DatabaseBasicTypes.h"
36#include "DatabaseDetails.h"
37#include "DatabaseError.h"
38#include "SQLiteDatabase.h"
39#include <wtf/Forward.h>
40#include <wtf/RefPtr.h>
41#include <wtf/ThreadSafeRefCounted.h>
42#include <wtf/text/WTFString.h>
43
44namespace WebCore {
45
46class DatabaseAuthorizer;
47class DatabaseBackendContext;
48class DatabaseBase;
49class SecurityOrigin;
50
51class DatabaseBackendBase : public ThreadSafeRefCounted<DatabaseBackendBase> {
52public:
53    virtual ~DatabaseBackendBase();
54
55    virtual String version() const;
56
57    bool opened() const { return m_opened; }
58    bool isNew() const { return m_new; }
59    bool isSyncDatabase() const { return m_isSyncDatabase; }
60
61    virtual SecurityOrigin* securityOrigin() const;
62    virtual String stringIdentifier() const;
63    virtual String displayName() const;
64    virtual unsigned long estimatedSize() const;
65    virtual String fileName() const;
66    virtual DatabaseDetails details() const;
67    SQLiteDatabase& sqliteDatabase() { return m_sqliteDatabase; }
68
69    unsigned long long maximumSize() const;
70    void incrementalVacuumIfNeeded();
71    void interrupt();
72    bool isInterrupted();
73
74    void disableAuthorizer();
75    void enableAuthorizer();
76    void setAuthorizerReadOnly();
77    void setAuthorizerPermissions(int permissions);
78    bool lastActionChangedDatabase();
79    bool lastActionWasInsert();
80    void resetDeletes();
81    bool hadDeletes();
82    void resetAuthorizer();
83
84    virtual void markAsDeletedAndClose() = 0;
85    virtual void closeImmediately() = 0;
86
87    DatabaseBackendContext* databaseContext() const { return m_databaseContext.get(); }
88    void setFrontend(DatabaseBase* frontend) { m_frontend = frontend; }
89
90protected:
91    friend class ChangeVersionWrapper;
92    friend class SQLStatementBackend;
93    friend class SQLStatementSync;
94    friend class SQLTransactionBackend;
95    friend class SQLTransactionBackendSync;
96
97    DatabaseBackendBase(PassRefPtr<DatabaseBackendContext>, const String& name, const String& expectedVersion,
98        const String& displayName, unsigned long estimatedSize, DatabaseType);
99
100    void closeDatabase();
101
102    virtual bool openAndVerifyVersion(bool setVersionInNewDatabase, DatabaseError&, String& errorMessage) = 0;
103    virtual bool performOpenAndVerify(bool shouldSetVersionInNewDatabase, DatabaseError&, String& errorMessage);
104
105    bool getVersionFromDatabase(String& version, bool shouldCacheVersion = true);
106    bool setVersionInDatabase(const String& version, bool shouldCacheVersion = true);
107    void setExpectedVersion(const String&);
108    const String& expectedVersion() const { return m_expectedVersion; }
109    String getCachedVersion()const;
110    void setCachedVersion(const String&);
111    bool getActualVersionForTransaction(String& version);
112
113    static const char* databaseInfoTableName();
114
115#if !LOG_DISABLED || !ERROR_DISABLED
116    String databaseDebugName() const;
117#endif
118
119    RefPtr<SecurityOrigin> m_contextThreadSecurityOrigin;
120    RefPtr<DatabaseBackendContext> m_databaseContext; // Associated with m_scriptExecutionContext.
121
122    String m_name;
123    String m_expectedVersion;
124    String m_displayName;
125    unsigned long m_estimatedSize;
126    String m_filename;
127
128    DatabaseBase* m_frontend;
129
130private:
131    DatabaseGuid m_guid;
132    bool m_opened;
133    bool m_new;
134    const bool m_isSyncDatabase;
135
136    SQLiteDatabase m_sqliteDatabase;
137
138    RefPtr<DatabaseAuthorizer> m_databaseAuthorizer;
139
140    friend class DatabaseServer;
141};
142
143} // namespace WebCore
144
145#endif // ENABLE(SQL_DATABASE)
146
147#endif // DatabaseBackendBase_h
148