Refactoring the Database class to not depend on
authordumi@chromium.org <dumi@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 27 Oct 2009 20:14:29 +0000 (20:14 +0000)
committerdumi@chromium.org <dumi@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 27 Oct 2009 20:14:29 +0000 (20:14 +0000)
OriginQuotaManager. Also, adding a SecurityOrigin copy to each
Database instance, that is safe to use on the file
thread. Finally, adding new simple fields and getters to the
Database object for storing/getting the display name and estimated
size specified by the user in the openDatabase() call.

Patch by Dumitru Daniliuc <dumi@chromium.org> on 2009-10-27
Reviewed by Adam Barth.

https://bugs.webkit.org/show_bug.cgi?id=30548

* page/SecurityOrigin.h:
* storage/Database.cpp:
(WebCore::Database::openDatabase): Storing the display name and
the estimated size in the Database object.
(WebCore::Database::Database): Storing the display name and the
estimated size in the Database object, as well as a SecurityOrigin
instance that is safe to use on the DB thread.
(WebCore::Database::maximumSize): Delegate the call to
DatabaseTracker::getMaxSizeForDatabase().
(WebCore::Database::databaseThreadSecurityOrigin): Return the
SecurityOrigin instance that's safe to use on the DB thread.
(WebCore::Database::threadSafeSecurityOrigin): Return the
SecurityOrigin instance that's safe to use on the current thread.
(WebCore::Database::displayName): Return the display name
specified by the user in the openDatabase() call.
(WebCore::Database::estimatedSize): Return the estimated size
specified by the user in the openDatabase() call.
(WebCore::Database::fileName): Return the name of the file where
the current Database is tored.
* storage/Database.h:
* storage/DatabaseTracker.cpp:
(WebCore::DatabaseTracker::getMaxSizeForDatabase): Returns the
maximum size for a DB file based on the current size of that file
and the space available for that origin.
* storage/DatabaseTracker.h:
* storage/SQLTransaction.cpp:
* storage/SQLTransactionClient.cpp:
(WebCore::SQLTransactionClient::didCommitTransaction): Use the
correct SecurityOrigin instance.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@50169 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/page/SecurityOrigin.h
WebCore/storage/Database.cpp
WebCore/storage/Database.h
WebCore/storage/DatabaseTracker.cpp
WebCore/storage/DatabaseTracker.h
WebCore/storage/OriginQuotaManager.cpp
WebCore/storage/SQLTransaction.cpp
WebCore/storage/SQLTransactionClient.cpp

index 9be5fa2c961643dc63818730a365a1c4d353864a..9a0149e6a25b971d8e00d70f14915697d6d7c1b7 100644 (file)
@@ -1,3 +1,46 @@
+2009-10-27  Dumitru Daniliuc  <dumi@chromium.org>
+
+        Reviewed by Adam Barth.
+
+        Refactoring the Database class to not depend on
+        OriginQuotaManager. Also, adding a SecurityOrigin copy to each
+        Database instance, that is safe to use on the file
+        thread. Finally, adding new simple fields and getters to the
+        Database object for storing/getting the display name and estimated
+        size specified by the user in the openDatabase() call.
+
+        https://bugs.webkit.org/show_bug.cgi?id=30548
+
+        * page/SecurityOrigin.h:
+        * storage/Database.cpp:
+        (WebCore::Database::openDatabase): Storing the display name and
+        the estimated size in the Database object.
+        (WebCore::Database::Database): Storing the display name and the
+        estimated size in the Database object, as well as a SecurityOrigin
+        instance that is safe to use on the DB thread.
+        (WebCore::Database::maximumSize): Delegate the call to
+        DatabaseTracker::getMaxSizeForDatabase().
+        (WebCore::Database::databaseThreadSecurityOrigin): Return the
+        SecurityOrigin instance that's safe to use on the DB thread.
+        (WebCore::Database::threadSafeSecurityOrigin): Return the
+        SecurityOrigin instance that's safe to use on the current thread.
+        (WebCore::Database::displayName): Return the display name
+        specified by the user in the openDatabase() call.
+        (WebCore::Database::estimatedSize): Return the estimated size
+        specified by the user in the openDatabase() call.
+        (WebCore::Database::fileName): Return the name of the file where
+        the current Database is tored.
+        * storage/Database.h:
+        * storage/DatabaseTracker.cpp:
+        (WebCore::DatabaseTracker::getMaxSizeForDatabase): Returns the
+        maximum size for a DB file based on the current size of that file
+        and the space available for that origin.
+        * storage/DatabaseTracker.h:
+        * storage/SQLTransaction.cpp:
+        * storage/SQLTransactionClient.cpp:
+        (WebCore::SQLTransactionClient::didCommitTransaction): Use the
+        correct SecurityOrigin instance.
+
 2009-10-27  Joseph Pecoraro  <joepeck@webkit.org>
 
         Reviewed by Timothy Hatcher.
index 46e6fada4476e6d4496583910a3d809ef6583838..6d4ce1ffb65901147e1e7fbe4ad580906d27dbe0 100644 (file)
@@ -127,9 +127,8 @@ namespace WebCore {
         // SecurityOrigin is represented with the string "null".
         String toString() const;
 
-        // Serialize the security origin for storage in the database. This format is
-        // deprecated and should be used only for compatibility with old databases;
-        // use toString() and createFromString() instead.
+        // Serialize the security origin to a string that could be used as part of
+        // file names. This format should be used in storage APIs only.
         String databaseIdentifier() const;
 
         // This method checks for equality between SecurityOrigins, not whether
index 08ea289a3b2b350953875ef0ff6ce3f9d1ba342c..dabb4165bb6acd2fb5c77250c7abb36962105c94 100644 (file)
@@ -128,7 +128,7 @@ PassRefPtr<Database> Database::openDatabase(Document* document, const String& na
         return 0;
     }
 
-    RefPtr<Database> database = adoptRef(new Database(document, name, expectedVersion));
+    RefPtr<Database> database = adoptRef(new Database(document, name, expectedVersion, displayName, estimatedSize));
 
     if (!database->openAndVerifyVersion(e)) {
        LOG(StorageAPI, "Failed to open and verify version (expected %s) of database %s", expectedVersion.ascii().data(), database->databaseDebugName().ascii().data());
@@ -147,18 +147,21 @@ PassRefPtr<Database> Database::openDatabase(Document* document, const String& na
     return database;
 }
 
-Database::Database(Document* document, const String& name, const String& expectedVersion)
+Database::Database(Document* document, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize)
     : m_transactionInProgress(false)
     , m_document(document)
     , m_name(name.crossThreadString())
     , m_guid(0)
     , m_expectedVersion(expectedVersion)
+    , m_displayName(displayName)
+    , m_estimatedSize(estimatedSize)
     , m_deleted(false)
     , m_stopped(false)
     , m_opened(false)
 {
     ASSERT(document);
-    m_securityOrigin = document->securityOrigin();
+    m_mainThreadSecurityOrigin = document->securityOrigin();
+    m_databaseThreadSecurityOrigin = m_mainThreadSecurityOrigin->threadsafeCopy();
 
     if (m_name.isNull())
         m_name = "";
@@ -167,7 +170,7 @@ Database::Database(Document* document, const String& name, const String& expecte
     JSC::initializeThreading();
 #endif
 
-    m_guid = guidForOriginAndName(m_securityOrigin->toString(), name);
+    m_guid = guidForOriginAndName(m_mainThreadSecurityOrigin->toString(), name);
 
     {
         MutexLocker locker(guidMutex());
@@ -183,7 +186,7 @@ Database::Database(Document* document, const String& name, const String& expecte
 
     ASSERT(m_document->databaseThread());
 
-    m_filename = DatabaseTracker::tracker().fullPathForDatabase(m_securityOrigin.get(), m_name);
+    m_filename = DatabaseTracker::tracker().fullPathForDatabase(m_mainThreadSecurityOrigin.get(), m_name);
 
     DatabaseTracker::tracker().addOpenDatabase(this);
     m_document->addOpenDatabase(this);
@@ -366,20 +369,9 @@ void Database::stop()
     }
 }
 
-unsigned long long Database::databaseSize() const
-{
-    return SQLiteFileSystem::getDatabaseFileSize(m_filename);
-}
-
 unsigned long long Database::maximumSize() const
 {
-    // The maximum size for this database is the full quota for this origin, minus the current usage within this origin,
-    // except for the current usage of this database
-
-    OriginQuotaManager& manager(DatabaseTracker::tracker().originQuotaManager());
-    Locker<OriginQuotaManager> locker(manager);
-
-    return DatabaseTracker::tracker().quotaForOrigin(m_securityOrigin.get()) - manager.diskUsage(m_securityOrigin.get()) + databaseSize();
+    return DatabaseTracker::tracker().getMaxSizeForDatabase(this);
 }
 
 void Database::disableAuthorizer()
@@ -647,9 +639,13 @@ void Database::setExpectedVersion(const String& version)
     updateGuidVersionMap(m_guid, version);
 }
 
-PassRefPtr<SecurityOrigin> Database::securityOriginCopy() const
+SecurityOrigin* Database::securityOrigin() const
 {
-    return m_securityOrigin->threadsafeCopy();
+    if (isMainThread())
+        return m_mainThreadSecurityOrigin.get();
+    if (currentThread() == document()->databaseThread()->getThreadID())
+        return m_databaseThreadSecurityOrigin.get();
+    return 0;
 }
 
 String Database::stringIdentifier() const
@@ -658,6 +654,23 @@ String Database::stringIdentifier() const
     return m_name.threadsafeCopy();
 }
 
+String Database::displayName() const
+{
+    // Return a deep copy for ref counting thread safety
+    return m_displayName.threadsafeCopy();
+}
+
+unsigned long Database::estimatedSize() const
+{
+    return m_estimatedSize;
+}
+
+String Database::fileName() const
+{
+    // Return a deep copy for ref counting thread safety
+    return m_filename.threadsafeCopy();
+}
+
 #endif
 
 }
index b8506863ebf5fa16c95bfe5661a01f792af08f8c..098845c223996008ea5ea96d32076c0704763ebc 100644 (file)
@@ -89,8 +89,11 @@ public:
     Vector<String> tableNames();
 
     Document* document() const { return m_document.get(); }
-    PassRefPtr<SecurityOrigin> securityOriginCopy() const;
+    SecurityOrigin* securityOrigin() const;
     String stringIdentifier() const;
+    String displayName() const;
+    unsigned long estimatedSize() const;
+    String fileName() const;
 
     bool getVersionFromDatabase(String&);
     bool setVersionInDatabase(const String&);
@@ -121,7 +124,8 @@ public:
     SQLTransactionCoordinator* transactionCoordinator() const;
 
 private:
-    Database(Document* document, const String& name, const String& expectedVersion);
+    Database(Document* document, const String& name, const String& expectedVersion,
+             const String& displayName, unsigned long estimatedSize);
 
     bool openAndVerifyVersion(ExceptionCode&);
 
@@ -136,10 +140,13 @@ private:
     static void deliverPendingCallback(void*);
 
     RefPtr<Document> m_document;
-    RefPtr<SecurityOrigin> m_securityOrigin;
+    RefPtr<SecurityOrigin> m_mainThreadSecurityOrigin;
+    RefPtr<SecurityOrigin> m_databaseThreadSecurityOrigin;
     String m_name;
     int m_guid;
     String m_expectedVersion;
+    String m_displayName;
+    unsigned long long m_estimatedSize;
     String m_filename;
 
     bool m_deleted;
@@ -152,7 +159,7 @@ private:
     RefPtr<DatabaseAuthorizer> m_databaseAuthorizer;
 
 #ifndef NDEBUG
-    String databaseDebugName() const { return m_securityOrigin->toString() + "::" + m_name; }
+    String databaseDebugName() const { return m_mainThreadSecurityOrigin->toString() + "::" + m_name; }
 #endif
 };
 
index 265cd0d51caadcb50d4971d679c9d03da2dc46db..c0c424209b7aa3d6780d1d46146ad08d1e1f5afd 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "ChromeClient.h"
 #include "Database.h"
+#include "DatabaseThread.h"
 #include "DatabaseTrackerClient.h"
 #include "Document.h"
 #include "Logging.h"
@@ -182,6 +183,16 @@ bool DatabaseTracker::hasEntryForDatabase(SecurityOrigin* origin, const String&
     return statement.step() == SQLResultRow;
 }
 
+unsigned long long DatabaseTracker::getMaxSizeForDatabase(const Database* database)
+{
+    ASSERT(currentThread() == database->document()->databaseThread()->getThreadID());
+    // The maximum size for a database is the full quota for its origin, minus the current usage within the origin,
+    // plus the current usage of the given database
+    Locker<OriginQuotaManager> locker(originQuotaManager());
+    SecurityOrigin* origin = database->securityOrigin();
+    return quotaForOrigin(origin) - originQuotaManager().diskUsage(origin) + SQLiteFileSystem::getDatabaseFileSize(database->fileName());
+}
+
 String DatabaseTracker::originPath(SecurityOrigin* origin) const
 {
     ASSERT(currentThread() == m_thread);
@@ -409,13 +420,11 @@ void DatabaseTracker::addOpenDatabase(Database* database)
     if (!m_openDatabaseMap)
         m_openDatabaseMap.set(new DatabaseOriginMap);
 
-    RefPtr<SecurityOrigin> origin(database->securityOriginCopy());
     String name(database->stringIdentifier());
-
-    DatabaseNameMap* nameMap = m_openDatabaseMap->get(origin);
+    DatabaseNameMap* nameMap = m_openDatabaseMap->get(database->securityOrigin());
     if (!nameMap) {
         nameMap = new DatabaseNameMap;
-        m_openDatabaseMap->set(origin, nameMap);
+        m_openDatabaseMap->set(database->securityOrigin(), nameMap);
     }
 
     DatabaseSet* databaseSet = nameMap->get(name);
@@ -441,10 +450,8 @@ void DatabaseTracker::removeOpenDatabase(Database* database)
         return;
     }
 
-    RefPtr<SecurityOrigin> origin(database->securityOriginCopy());
     String name(database->stringIdentifier());
-
-    DatabaseNameMap* nameMap = m_openDatabaseMap->get(origin);
+    DatabaseNameMap* nameMap = m_openDatabaseMap->get(database->securityOrigin());
     if (!nameMap) {
         ASSERT_NOT_REACHED();
         return;
@@ -469,7 +476,7 @@ void DatabaseTracker::removeOpenDatabase(Database* database)
     if (!nameMap->isEmpty())
         return;
 
-    m_openDatabaseMap->remove(origin);
+    m_openDatabaseMap->remove(database->securityOrigin());
     delete nameMap;
 }
 
index 2f6e06dfbb8e7e29beee3639fa319af00260ffb7..85e4858ad0ad709c9d1bf5d5f5e2c01ef0539b66 100644 (file)
@@ -86,6 +86,8 @@ public:
 
     bool hasEntryForOrigin(SecurityOrigin*);
 
+    unsigned long long getMaxSizeForDatabase(const Database*);
+
 private:
     DatabaseTracker();
 
index 20bb34d1f50f24e415a698cfcc2b8b73ee46e3c3..30b32717ac64345034521f4682aa62e9dd598f71 100644 (file)
@@ -104,8 +104,7 @@ void OriginQuotaManager::markDatabase(Database* database)
 {
     ASSERT(database);
     ASSERT(m_usageRecordGuardLocked);
-    RefPtr<SecurityOrigin> origin = database->securityOriginCopy();
-    OriginUsageRecord* usageRecord = m_usageMap.get(origin);
+    OriginUsageRecord* usageRecord = m_usageMap.get(database->securityOrigin());
     ASSERT(usageRecord);
 
     usageRecord->markDatabase(database->stringIdentifier());
index 149b384c56e2e5cbc15e34d2093877df59519371..165685b15e98c00b73cd780e51698e6b3932310c 100644 (file)
@@ -40,7 +40,6 @@
 #include "Logging.h"
 #include "Page.h"
 #include "PlatformString.h"
-#include "SecurityOrigin.h"
 #include "Settings.h"
 #include "SQLError.h"
 #include "SQLiteTransaction.h"
index e72f5ed79ea83d0fe777426c29d4f89d129810a5..5918bd8ef0e9177f5e51c493f119bd1eaf9c5fcd 100644 (file)
@@ -47,7 +47,7 @@ void SQLTransactionClient::didCommitTransaction(SQLTransaction* transaction)
     ASSERT(currentThread() == transaction->database()->document()->databaseThread()->getThreadID());
     Database* database = transaction->database();
     DatabaseTracker::tracker().scheduleNotifyDatabaseChanged(
-        database->document()->securityOrigin(), database->stringIdentifier());
+        database->securityOrigin(), database->stringIdentifier());
 }
 
 void SQLTransactionClient::didExecuteStatement(SQLTransaction* transaction)
@@ -65,11 +65,9 @@ bool SQLTransactionClient::didExceedQuota(SQLTransaction* transaction)
     Page* page = database->document()->page();
     ASSERT(page);
 
-    RefPtr<SecurityOrigin> origin = database->securityOriginCopy();
-
-    unsigned long long currentQuota = DatabaseTracker::tracker().quotaForOrigin(origin.get());
+    unsigned long long currentQuota = DatabaseTracker::tracker().quotaForOrigin(database->securityOrigin());
     page->chrome()->client()->exceededDatabaseQuota(database->document()->frame(), database->stringIdentifier());
-    unsigned long long newQuota = DatabaseTracker::tracker().quotaForOrigin(origin.get());
+    unsigned long long newQuota = DatabaseTracker::tracker().quotaForOrigin(database->securityOrigin());
     return (newQuota > currentQuota);
 }