Home | History | Annotate | Download | only in webdatabase
      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 "core/platform/sql/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 "weborigin/SecurityOrigin.h"
     44 #endif
     45 
     46 namespace WebCore {
     47 
     48 class DatabaseAuthorizer;
     49 class DatabaseBackendContext;
     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 markAsDeletedAndClose() = 0;
     87     virtual void closeImmediately() = 0;
     88 
     89     DatabaseBackendContext* databaseContext() const { return m_databaseContext.get(); }
     90     void setFrontend(DatabaseBase* frontend) { m_frontend = frontend; }
     91 
     92 protected:
     93     friend class ChangeVersionWrapper;
     94     friend class SQLStatementBackend;
     95     friend class SQLStatementSync;
     96     friend class SQLTransactionBackend;
     97     friend class SQLTransactionBackendSync;
     98 
     99     DatabaseBackendBase(PassRefPtr<DatabaseBackendContext>, const String& name, const String& expectedVersion,
    100         const String& displayName, unsigned long estimatedSize, DatabaseType);
    101 
    102     void closeDatabase();
    103 
    104     virtual bool openAndVerifyVersion(bool setVersionInNewDatabase, DatabaseError&, String& errorMessage) = 0;
    105     virtual bool performOpenAndVerify(bool shouldSetVersionInNewDatabase, DatabaseError&, String& errorMessage);
    106 
    107     bool getVersionFromDatabase(String& version, bool shouldCacheVersion = true);
    108     bool setVersionInDatabase(const String& version, bool shouldCacheVersion = true);
    109     void setExpectedVersion(const String&);
    110     const String& expectedVersion() const { return m_expectedVersion; }
    111     String getCachedVersion()const;
    112     void setCachedVersion(const String&);
    113     bool getActualVersionForTransaction(String& version);
    114 
    115     void reportOpenDatabaseResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    116     void reportChangeVersionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    117     void reportStartTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    118     void reportCommitTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    119     void reportExecuteStatementResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    120     void reportVacuumDatabaseResult(int sqliteErrorCode);
    121 
    122     static const char* databaseInfoTableName();
    123 
    124     RefPtr<SecurityOrigin> m_contextThreadSecurityOrigin;
    125     RefPtr<DatabaseBackendContext> m_databaseContext; // Associated with m_scriptExecutionContext.
    126 
    127     String m_name;
    128     String m_expectedVersion;
    129     String m_displayName;
    130     unsigned long m_estimatedSize;
    131     String m_filename;
    132 
    133     DatabaseBase* m_frontend;
    134 
    135 #if !LOG_DISABLED || !ERROR_DISABLED
    136     String databaseDebugName() const { return m_contextThreadSecurityOrigin->toString() + "::" + m_name; }
    137 #endif
    138 
    139 private:
    140     DatabaseGuid m_guid;
    141     bool m_opened;
    142     bool m_new;
    143     const bool m_isSyncDatabase;
    144 
    145     SQLiteDatabase m_sqliteDatabase;
    146 
    147     RefPtr<DatabaseAuthorizer> m_databaseAuthorizer;
    148 
    149     friend class DatabaseServer;
    150 };
    151 
    152 } // namespace WebCore
    153 
    154 #endif // DatabaseBackendBase_h
    155