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 "modules/webdatabase/sqlite/SQLiteDatabase.h"
     34 #include "modules/webdatabase/DatabaseBasicTypes.h"
     35 #include "modules/webdatabase/DatabaseError.h"
     36 #include "platform/heap/Handle.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 ExecutionContext;
     52 class SecurityOrigin;
     53 
     54 class DatabaseBackendBase : public ThreadSafeRefCountedWillBeGarbageCollectedFinalized<DatabaseBackendBase> {
     55 public:
     56     virtual ~DatabaseBackendBase();
     57     virtual void trace(Visitor*);
     58 
     59     virtual String version() const;
     60 
     61     bool opened() const { return m_opened; }
     62     bool isNew() const { return m_new; }
     63     bool isSyncDatabase() const { return m_isSyncDatabase; }
     64 
     65     virtual SecurityOrigin* securityOrigin() const;
     66     virtual String stringIdentifier() const;
     67     virtual String displayName() const;
     68     virtual unsigned long estimatedSize() const;
     69     virtual String fileName() const;
     70     SQLiteDatabase& sqliteDatabase() { return m_sqliteDatabase; }
     71 
     72     unsigned long long maximumSize() const;
     73     void incrementalVacuumIfNeeded();
     74     void interrupt();
     75     bool isInterrupted();
     76 
     77     void disableAuthorizer();
     78     void enableAuthorizer();
     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     ExecutionContext* executionContext() const;
     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(DatabaseContext*, 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     void logErrorMessage(const String&);
    122     static const char* databaseInfoTableName();
    123 
    124     RefPtr<SecurityOrigin> m_contextThreadSecurityOrigin;
    125     RefPtrWillBeMember<DatabaseContext> m_databaseContext; // Associated with m_executionContext.
    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     RefPtrWillBeMember<DatabaseAuthorizer> m_databaseAuthorizer;
    148 
    149     friend class DatabaseServer;
    150 };
    151 
    152 } // namespace WebCore
    153 
    154 #endif // DatabaseBackendBase_h
    155