Purge PassRefPtr in Modules/webdatabase
[WebKit-https.git] / Source / WebCore / Modules / webdatabase / SQLCallbackWrapper.h
index c184a37..a65ccb7 100644 (file)
@@ -10,7 +10,7 @@
  * 2.  Redistributions in binary form must reproduce the above copyright
  *     notice, this list of conditions and the following disclaimer in the
  *     documentation and/or other materials provided with the distribution.
- * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
  *     its contributors may be used to endorse or promote products derived
  *     from this software without specific prior written permission.
  *
 #ifndef SQLCallbackWrapper_h
 #define SQLCallbackWrapper_h
 
-#if ENABLE(SQL_DATABASE)
-
 #include "ScriptExecutionContext.h"
-#include <wtf/ThreadingPrimitives.h>
+#include <wtf/Lock.h>
 
 namespace WebCore {
 
@@ -43,8 +41,8 @@ namespace WebCore {
 // - by unwrapping and then dereferencing normally - on context thread only
 template<typename T> class SQLCallbackWrapper {
 public:
-    SQLCallbackWrapper(PassRefPtr<T> callback, ScriptExecutionContext* scriptExecutionContext)
-        : m_callback(callback)
+    SQLCallbackWrapper(RefPtr<T>&& callback, ScriptExecutionContext* scriptExecutionContext)
+        : m_callback(WTFMove(callback))
         , m_scriptExecutionContext(m_callback ? scriptExecutionContext : 0)
     {
         ASSERT(!m_callback || (m_scriptExecutionContext.get() && m_scriptExecutionContext->isContextThread()));
@@ -57,69 +55,49 @@ public:
 
     void clear()
     {
-        ScriptExecutionContext* context;
+        ScriptExecutionContext* scriptExecutionContextPtr;
         T* callback;
         {
-            MutexLocker locker(m_mutex);
+            LockHolder locker(m_mutex);
             if (!m_callback) {
                 ASSERT(!m_scriptExecutionContext);
                 return;
             }
             if (m_scriptExecutionContext->isContextThread()) {
-                m_callback = 0;
-                m_scriptExecutionContext = 0;
+                m_callback = nullptr;
+                m_scriptExecutionContext = nullptr;
                 return;
             }
-            context = m_scriptExecutionContext.release().leakRef();
-            callback = m_callback.release().leakRef();
+            scriptExecutionContextPtr = m_scriptExecutionContext.leakRef();
+            callback = m_callback.leakRef();
         }
-        context->postTask(SafeReleaseTask::create(callback));
+        scriptExecutionContextPtr->postTask({
+            ScriptExecutionContext::Task::CleanupTask,
+            [callback, scriptExecutionContextPtr] (ScriptExecutionContext& context) {
+                ASSERT_UNUSED(context, &context == scriptExecutionContextPtr && context.isContextThread());
+                callback->deref();
+                scriptExecutionContextPtr->deref();
+            }
+        });
     }
 
-    PassRefPtr<T> unwrap()
+    RefPtr<T> unwrap()
     {
-        MutexLocker locker(m_mutex);
+        LockHolder locker(m_mutex);
         ASSERT(!m_callback || m_scriptExecutionContext->isContextThread());
-        m_scriptExecutionContext = 0;
-        return m_callback.release();
+        m_scriptExecutionContext = nullptr;
+        return WTFMove(m_callback);
     }
 
     // Useful for optimizations only, please test the return value of unwrap to be sure.
     bool hasCallback() const { return m_callback; }
 
 private:
-    class SafeReleaseTask : public ScriptExecutionContext::Task {
-    public:
-        static PassOwnPtr<SafeReleaseTask> create(T* callbackToRelease)
-        {
-            return adoptPtr(new SafeReleaseTask(callbackToRelease));
-        }
-
-        virtual void performTask(ScriptExecutionContext* context)
-        {
-            ASSERT(m_callbackToRelease && context && context->isContextThread());
-            m_callbackToRelease->deref();
-            context->deref();
-        }
-
-        virtual bool isCleanupTask() const { return true; }
-
-    private:
-        explicit SafeReleaseTask(T* callbackToRelease)
-            : m_callbackToRelease(callbackToRelease)
-        {
-        }
-
-        T* m_callbackToRelease;
-    };
-
-    Mutex m_mutex;
+    Lock m_mutex;
     RefPtr<T> m_callback;
     RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
 };
 
 } // namespace WebCore
 
-#endif // ENABLE(SQL_DATABASE)
-
 #endif // SQLCallbackWrapper_h