Home | History | Annotate | Download | only in webdatabase
      1 /*
      2  * Copyright (C) 2013 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  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef Database_h
     27 #define Database_h
     28 
     29 #include "bindings/core/v8/ScriptWrappable.h"
     30 #include "modules/webdatabase/DatabaseBasicTypes.h"
     31 #include "modules/webdatabase/DatabaseError.h"
     32 #include "modules/webdatabase/sqlite/SQLiteDatabase.h"
     33 #include "platform/weborigin/SecurityOrigin.h"
     34 #include "wtf/Deque.h"
     35 #include "wtf/text/WTFString.h"
     36 
     37 namespace blink {
     38 
     39 class ChangeVersionData;
     40 class DatabaseAuthorizer;
     41 class DatabaseContext;
     42 class DatabaseServer;
     43 class ExecutionContext;
     44 class SQLTransaction;
     45 class SQLTransactionBackend;
     46 class SQLTransactionCallback;
     47 class SQLTransactionClient;
     48 class SQLTransactionCoordinator;
     49 class SQLTransactionErrorCallback;
     50 class VoidCallback;
     51 
     52 class Database FINAL : public ThreadSafeRefCountedWillBeGarbageCollectedFinalized<Database>, public ScriptWrappable {
     53     DEFINE_WRAPPERTYPEINFO();
     54 public:
     55     virtual ~Database();
     56     void trace(Visitor*);
     57 
     58     bool openAndVerifyVersion(bool setVersionInNewDatabase, DatabaseError&, String& errorMessage);
     59     void close();
     60 
     61     PassRefPtrWillBeRawPtr<SQLTransactionBackend> runTransaction(PassRefPtrWillBeRawPtr<SQLTransaction>, bool readOnly, const ChangeVersionData*);
     62     void scheduleTransactionStep(SQLTransactionBackend*);
     63     void inProgressTransactionCompleted();
     64 
     65     SQLTransactionClient* transactionClient() const;
     66     SQLTransactionCoordinator* transactionCoordinator() const;
     67 
     68     // Direct support for the DOM API
     69     String version() const;
     70     void changeVersion(
     71         const String& oldVersion,
     72         const String& newVersion,
     73         SQLTransactionCallback*,
     74         SQLTransactionErrorCallback*,
     75         VoidCallback* successCallback);
     76     void transaction(
     77         SQLTransactionCallback*,
     78         SQLTransactionErrorCallback*,
     79         VoidCallback* successCallback);
     80     void readTransaction(
     81         SQLTransactionCallback*,
     82         SQLTransactionErrorCallback*,
     83         VoidCallback* successCallback);
     84 
     85     bool opened() const { return m_opened; }
     86     bool isNew() const { return m_new; }
     87 
     88     SecurityOrigin* securityOrigin() const;
     89     String stringIdentifier() const;
     90     String displayName() const;
     91     unsigned long estimatedSize() const;
     92     String fileName() const;
     93     SQLiteDatabase& sqliteDatabase() { return m_sqliteDatabase; }
     94 
     95     unsigned long long maximumSize() const;
     96     void incrementalVacuumIfNeeded();
     97 
     98     void disableAuthorizer();
     99     void enableAuthorizer();
    100     void setAuthorizerPermissions(int);
    101     bool lastActionChangedDatabase();
    102     bool lastActionWasInsert();
    103     void resetDeletes();
    104     bool hadDeletes();
    105     void resetAuthorizer();
    106 
    107     Vector<String> tableNames();
    108     void scheduleTransactionCallback(SQLTransaction*);
    109     void closeImmediately();
    110     void closeDatabase();
    111 
    112     DatabaseContext* databaseContext() const { return m_databaseContext.get(); }
    113     ExecutionContext* executionContext() const;
    114 
    115 private:
    116     class DatabaseOpenTask;
    117     class DatabaseCloseTask;
    118     class DatabaseTransactionTask;
    119     class DatabaseTableNamesTask;
    120 
    121     Database(DatabaseContext*, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize);
    122     bool performOpenAndVerify(bool setVersionInNewDatabase, DatabaseError&, String& errorMessage);
    123 
    124     void scheduleTransaction();
    125 
    126     bool getVersionFromDatabase(String& version, bool shouldCacheVersion = true);
    127     bool setVersionInDatabase(const String& version, bool shouldCacheVersion = true);
    128     void setExpectedVersion(const String&);
    129     const String& expectedVersion() const { return m_expectedVersion; }
    130     String getCachedVersion()const;
    131     void setCachedVersion(const String&);
    132     bool getActualVersionForTransaction(String& version);
    133 
    134     void runTransaction(
    135         SQLTransactionCallback*,
    136         SQLTransactionErrorCallback*,
    137         VoidCallback* successCallback,
    138         bool readOnly,
    139         const ChangeVersionData* = 0);
    140     Vector<String> performGetTableNames();
    141 
    142     void reportOpenDatabaseResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    143     void reportChangeVersionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    144     void reportStartTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    145     void reportCommitTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    146     void reportExecuteStatementResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode);
    147     void reportVacuumDatabaseResult(int sqliteErrorCode);
    148     void logErrorMessage(const String&);
    149     static const char* databaseInfoTableName();
    150 #if !LOG_DISABLED || !ERROR_DISABLED
    151     String databaseDebugName() const { return m_contextThreadSecurityOrigin->toString() + "::" + m_name; }
    152 #endif
    153 
    154     RefPtr<SecurityOrigin> m_contextThreadSecurityOrigin;
    155     RefPtr<SecurityOrigin> m_databaseThreadSecurityOrigin;
    156     RefPtrWillBeMember<DatabaseContext> m_databaseContext; // Associated with m_executionContext.
    157 
    158     String m_name;
    159     String m_expectedVersion;
    160     String m_displayName;
    161     unsigned long m_estimatedSize;
    162     String m_filename;
    163 
    164     DatabaseGuid m_guid;
    165     bool m_opened;
    166     bool m_new;
    167 
    168     SQLiteDatabase m_sqliteDatabase;
    169 
    170     RefPtrWillBeMember<DatabaseAuthorizer> m_databaseAuthorizer;
    171 
    172     Deque<RefPtrWillBeMember<SQLTransactionBackend> > m_transactionQueue;
    173     Mutex m_transactionInProgressMutex;
    174     bool m_transactionInProgress;
    175     bool m_isTransactionQueueEnabled;
    176 
    177     friend class ChangeVersionWrapper;
    178     friend class DatabaseManager;
    179     friend class SQLStatementBackend;
    180     friend class SQLTransaction;
    181     friend class SQLTransactionBackend;
    182 };
    183 
    184 } // namespace blink
    185 
    186 #endif // Database_h
    187