Fix for https://bugs.webkit.org/show_bug.cgi?id=38471
[WebKit-https.git] / WebKit2 / UIProcess / GenericCallback.h
similarity index 50%
rename from WebKit2/UIProcess/ScriptReturnValueCallback.h
rename to WebKit2/UIProcess/GenericCallback.h
index 505871f..c3684a8 100644 (file)
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef ScriptReturnValueCallback_h
-#define ScriptReturnValueCallback_h
+#ifndef GenericCallback_h
+#define GenericCallback_h
 
-#include "WKBase.h"
+#include "WKAPICast.h"
 #include <wtf/PassRefPtr.h>
 #include <wtf/RefCounted.h>
 
-namespace WebCore {
-    class String;
-}
-
 namespace WebKit {
 
-class ScriptReturnValueCallback : public RefCounted<ScriptReturnValueCallback> {
+template<typename APIReturnValueType, typename InternalReturnValueType = typename APITypeInfo<APIReturnValueType>::ImplType>
+class GenericCallback : public RefCounted<GenericCallback<APIReturnValueType, InternalReturnValueType> > {
 public:
-    typedef void (*ScriptReturnValueCallbackFunction)(void*, WKStringRef);
-    typedef void (*ScriptReturnValueCallbackDisposeFunction)(void*);
+    typedef void (*CallbackFunction)(APIReturnValueType, void*);
+    typedef void (*CallbackDisposeFunction)(void*);
 
-    static PassRefPtr<ScriptReturnValueCallback> create(void* context, ScriptReturnValueCallbackFunction callback, ScriptReturnValueCallbackDisposeFunction disposeCallback)
+    static PassRefPtr<GenericCallback> create(void* context, CallbackFunction callback, CallbackDisposeFunction disposeCallback)
     {
-        return adoptRef(new ScriptReturnValueCallback(context, callback, disposeCallback));
+        return adoptRef(new GenericCallback(context, callback, disposeCallback));
     }
-    ~ScriptReturnValueCallback();
 
-    uint64_t callbackID() const { return m_callbackID; }
+    ~GenericCallback()
+    {
+        ASSERT(!m_callback);
+    }
+
+    void performCallbackWithReturnValue(InternalReturnValueType returnValue)
+    {
+        ASSERT(m_callback);
+
+        m_callback(toRef(returnValue), m_context);
+
+        m_callback = 0;
+        m_disposeCallback = 0;
+    }
+    
+    void invalidate()
+    {
+        ASSERT(m_callback);
 
-    void performCallbackWithReturnValue(const WebCore::String&);
-    void invalidate();
+        m_disposeCallback(m_context);
+
+        m_callback = 0;
+        m_disposeCallback = 0;
+    }
+
+    uint64_t callbackID() const { return m_callbackID; }
 
 private:
-    ScriptReturnValueCallback(void*, ScriptReturnValueCallbackFunction, ScriptReturnValueCallbackDisposeFunction);
+    static uint64_t generateCallbackID()
+    {
+        static uint64_t uniqueCallbackID = 1;
+        return uniqueCallbackID++;
+    }
+
+    GenericCallback(void* context, CallbackFunction callback, CallbackDisposeFunction disposeCallback)
+        : m_context(context)
+        , m_callback(callback)
+        , m_disposeCallback(disposeCallback)
+        , m_callbackID(generateCallbackID())
+    {
+    }
 
     void* m_context;
-    ScriptReturnValueCallbackFunction m_callback;
-    ScriptReturnValueCallbackDisposeFunction m_disposeCallback;
+    CallbackFunction m_callback;
+    CallbackDisposeFunction m_disposeCallback;
     uint64_t m_callbackID;
 };
 
 } // namespace WebKit
 
-#endif // ScriptReturnValueCallback_h
+#endif // GenericCallback_h