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 Computer, 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 #include "modules/webdatabase/sqlite/SQLiteDatabase.h" 34 #include "modules/webdatabase/DatabaseBasicTypes.h" 35 #include "modules/webdatabase/DatabaseDetails.h" 36 #include "modules/webdatabase/DatabaseError.h" 37 #include "wtf/Forward.h" 38 #include "wtf/RefPtr.h" 39 #include "wtf/ThreadSafeRefCounted.h" 40 #include "wtf/text/WTFString.h" 41 42 #if !LOG_DISABLED || !ERROR_DISABLED 43 #include "platform/weborigin/SecurityOrigin.h" 44 #endif 45 46 namespace WebCore { 47 48 class DatabaseAuthorizer; 49 class DatabaseContext; 50 class DatabaseBase; 51 class SecurityOrigin; 52 53 class DatabaseBackendBase : public ThreadSafeRefCounted<DatabaseBackendBase> { 54 public: 55 virtual ~DatabaseBackendBase(); 56 57 virtual String version() const; 58 59 bool opened() const { return m_opened; } 60 bool isNew() const { return m_new; } 61 bool isSyncDatabase() const { return m_isSyncDatabase; } 62 63 virtual SecurityOrigin* securityOrigin() const; 64 virtual String stringIdentifier() const; 65 virtual String displayName() const; 66 virtual unsigned long estimatedSize() const; 67 virtual String fileName() const; 68 virtual DatabaseDetails details() const; 69 SQLiteDatabase& sqliteDatabase() { return m_sqliteDatabase; } 70 71 unsigned long long maximumSize() const; 72 void incrementalVacuumIfNeeded(); 73 void interrupt(); 74 bool isInterrupted(); 75 76 void disableAuthorizer(); 77 void enableAuthorizer(); 78 void setAuthorizerReadOnly(); 79 void setAuthorizerPermissions(int permissions); 80 bool lastActionChangedDatabase(); 81 bool lastActionWasInsert(); 82 void resetDeletes(); 83 bool hadDeletes(); 84 void resetAuthorizer(); 85 86 virtual void closeImmediately() = 0; 87 88 DatabaseContext* databaseContext() const { return m_databaseContext.get(); } 89 void setFrontend(DatabaseBase* frontend) { m_frontend = frontend; } 90 91 protected: 92 friend class ChangeVersionWrapper; 93 friend class SQLStatementBackend; 94 friend class SQLStatementSync; 95 friend class SQLTransactionBackend; 96 friend class SQLTransactionBackendSync; 97 98 DatabaseBackendBase(PassRefPtr<DatabaseContext>, const String& name, const String& expectedVersion, 99 const String& displayName, unsigned long estimatedSize, DatabaseType); 100 101 void closeDatabase(); 102 103 virtual bool openAndVerifyVersion(bool setVersionInNewDatabase, DatabaseError&, String& errorMessage) = 0; 104 virtual bool performOpenAndVerify(bool shouldSetVersionInNewDatabase, DatabaseError&, String& errorMessage); 105 106 bool getVersionFromDatabase(String& version, bool shouldCacheVersion = true); 107 bool setVersionInDatabase(const String& version, bool shouldCacheVersion = true); 108 void setExpectedVersion(const String&); 109 const String& expectedVersion() const { return m_expectedVersion; } 110 String getCachedVersion()const; 111 void setCachedVersion(const String&); 112 bool getActualVersionForTransaction(String& version); 113 114 void reportOpenDatabaseResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode); 115 void reportChangeVersionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode); 116 void reportStartTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode); 117 void reportCommitTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode); 118 void reportExecuteStatementResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode); 119 void reportVacuumDatabaseResult(int sqliteErrorCode); 120 121 static const char* databaseInfoTableName(); 122 123 RefPtr<SecurityOrigin> m_contextThreadSecurityOrigin; 124 RefPtr<DatabaseContext> m_databaseContext; // Associated with m_executionContext. 125 126 String m_name; 127 String m_expectedVersion; 128 String m_displayName; 129 unsigned long m_estimatedSize; 130 String m_filename; 131 132 DatabaseBase* m_frontend; 133 134 #if !LOG_DISABLED || !ERROR_DISABLED 135 String databaseDebugName() const { return m_contextThreadSecurityOrigin->toString() + "::" + m_name; } 136 #endif 137 138 private: 139 DatabaseGuid m_guid; 140 bool m_opened; 141 bool m_new; 142 const bool m_isSyncDatabase; 143 144 SQLiteDatabase m_sqliteDatabase; 145 146 RefPtr<DatabaseAuthorizer> m_databaseAuthorizer; 147 148 friend class DatabaseServer; 149 }; 150 151 } // namespace WebCore 152 153 #endif // DatabaseBackendBase_h 154