Title: [141431] trunk/Source/WebCore
Revision
141431
Author
[email protected]
Date
2013-01-31 10:12:23 -0800 (Thu, 31 Jan 2013)

Log Message

DatabaseContext needs to outlive the ScriptExecutionContext.
https://bugs.webkit.org/show_bug.cgi?id=108355.

Reviewed by Geoffrey Garen.

Added a RefPtr<DatabaseContext> in ScriptExecutionContext to keep the
DatabaseContext alive until after ScriptExecutionContext destructs.

No new tests.

* Modules/webdatabase/DatabaseContext.cpp:
(WebCore::DatabaseContext::DatabaseContext):
* dom/ScriptExecutionContext.cpp:
(WebCore::ScriptExecutionContext::setDatabaseContext):
* dom/ScriptExecutionContext.h:
(ScriptExecutionContext):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (141430 => 141431)


--- trunk/Source/WebCore/ChangeLog	2013-01-31 18:04:16 UTC (rev 141430)
+++ trunk/Source/WebCore/ChangeLog	2013-01-31 18:12:23 UTC (rev 141431)
@@ -1,3 +1,22 @@
+2013-01-30  Mark Lam  <[email protected]>
+
+        DatabaseContext needs to outlive the ScriptExecutionContext.
+        https://bugs.webkit.org/show_bug.cgi?id=108355.
+
+        Reviewed by Geoffrey Garen.
+
+        Added a RefPtr<DatabaseContext> in ScriptExecutionContext to keep the
+        DatabaseContext alive until after ScriptExecutionContext destructs.
+
+        No new tests.
+
+        * Modules/webdatabase/DatabaseContext.cpp:
+        (WebCore::DatabaseContext::DatabaseContext):
+        * dom/ScriptExecutionContext.cpp:
+        (WebCore::ScriptExecutionContext::setDatabaseContext):
+        * dom/ScriptExecutionContext.h:
+        (ScriptExecutionContext):
+
 2013-01-31  Grzegorz Czajkowski  <[email protected]>
 
         On Linux, can't get spelling suggestions without selection when unified text checker is enabled

Modified: trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.cpp (141430 => 141431)


--- trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.cpp	2013-01-31 18:04:16 UTC (rev 141430)
+++ trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.cpp	2013-01-31 18:12:23 UTC (rev 141431)
@@ -39,11 +39,63 @@
 #include "Document.h"
 #include "Page.h"
 #include "SchemeRegistry.h"
+#include "ScriptExecutionContext.h"
 #include "SecurityOrigin.h"
 #include "Settings.h"
 
 namespace WebCore {
 
+// How the DatabaseContext Life-Cycle works?
+// ========================================
+// ... in other words, who's keeping the DatabaseContext alive and how long does
+// it need to stay alive?
+//
+// The DatabaseContext is referenced from RefPtrs in:
+// 1. ScriptExecutionContext
+// 2. Database
+//
+// At Birth:
+// ========
+// We create a DatabaseContext only when there is a need i.e. the script tries to
+// open a Database via DatabaseManager::openDatabase().
+//
+// The DatabaseContext constructor will call setDatabaseContext() on the
+// the ScriptExecutionContext. This sets the RefPtr in the ScriptExecutionContext
+// for keeping the DatabaseContext alive. Since the DatabaseContext is only
+// created from the script thread, it is safe for the constructor to call
+// ScriptExecutionContext::setDatabaseContext().
+//
+// Once a DatabaseContext is associated with a ScriptExecutionContext, it will
+// live until after the ScriptExecutionContext destructs. This is true even if
+// we don't succeed in opening any Databases for that context. When we do
+// succeed in opening Databases for this ScriptExecutionContext, the Database
+// will re-use the same DatabaseContext.
+//
+// At Shutdown:
+// ===========
+// During shutdown, the DatabaseContext needs to:
+// 1. "outlive" the ScriptExecutionContext.
+//    - This is needed because the DatabaseContext needs to remove itself from the
+//      ScriptExecutionContext's ActiveDOMObject list and ContextDestructionObserver
+//      list. This removal needs to be executed on the script's thread. Hence, we
+//      rely on the ScriptExecutionContext's shutdown process to call
+//      stop() and contextDestroyed() to give us a chance to clean these up from
+//      the script thread.
+//
+// 2. "outlive" the Databases.
+//    - This is because they may make use of the DatabaseContext to execute a close
+//      task and shutdown in an orderly manner. When the Databases are destructed,
+//      they will deref the DatabaseContext from the DatabaseThread.
+//
+// During shutdown, the ScriptExecutionContext is shutting down on the script thread
+// while the Databases are shutting down on the DatabaseThread. Hence, there can be
+// a race condition as to whether the ScriptExecutionContext or the Databases
+// destruct first.
+//
+// The RefPtrs in the Databases and ScriptExecutionContext will ensure that the
+// DatabaseContext will outlive both regardless of which of the 2 destructs first.
+
+
 DatabaseContext::DatabaseContext(ScriptExecutionContext* context)
     : ActiveDOMObject(context, this)
     , m_hasOpenDatabases(false)
@@ -53,6 +105,8 @@
     // ActiveDOMObject expects this to be called to set internal flags.
     suspendIfNeeded();
 
+    context->setDatabaseContext(this);
+
     // For debug accounting only. We must do this before we register the
     // instance. The assertions assume this.
     DatabaseManager::manager().didConstructDatabaseContext(); 

Modified: trunk/Source/WebCore/dom/ScriptExecutionContext.cpp (141430 => 141431)


--- trunk/Source/WebCore/dom/ScriptExecutionContext.cpp	2013-01-31 18:04:16 UTC (rev 141430)
+++ trunk/Source/WebCore/dom/ScriptExecutionContext.cpp	2013-01-31 18:12:23 UTC (rev 141431)
@@ -53,6 +53,10 @@
 #include "JSDOMWindow.h"
 #endif
 
+#if ENABLE(SQL_DATABASE)
+#include "DatabaseContext.h"
+#endif
+
 namespace WTF {
 
 template<> struct SequenceMemoryInstrumentationTraits<WebCore::ContextDestructionObserver*> {
@@ -442,4 +446,12 @@
 }
 #endif
 
+#if ENABLE(SQL_DATABASE)
+void ScriptExecutionContext::setDatabaseContext(DatabaseContext* databaseContext)
+{
+    ASSERT(!m_databaseContext);
+    m_databaseContext = databaseContext;
+}
+#endif
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/dom/ScriptExecutionContext.h (141430 => 141431)


--- trunk/Source/WebCore/dom/ScriptExecutionContext.h	2013-01-31 18:04:16 UTC (rev 141430)
+++ trunk/Source/WebCore/dom/ScriptExecutionContext.h	2013-01-31 18:12:23 UTC (rev 141431)
@@ -51,6 +51,7 @@
 namespace WebCore {
 
 class CachedScript;
+class DatabaseContext;
 class DOMTimer;
 class EventListener;
 class EventQueue;
@@ -171,6 +172,10 @@
 
     virtual void reportMemoryUsage(MemoryObjectInfo*) const OVERRIDE;
 
+#if ENABLE(SQL_DATABASE)
+    void setDatabaseContext(DatabaseContext*);
+#endif
+
 protected:
     class AddConsoleMessageTask : public Task {
     public:
@@ -227,6 +232,10 @@
     OwnPtr<PublicURLManager> m_publicURLManager;
     RefPtr<FileThread> m_fileThread;
 #endif
+
+#if ENABLE(SQL_DATABASE)
+    RefPtr<DatabaseContext> m_databaseContext;
+#endif
 };
 
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to