2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GenericCallback_h
27 #define GenericCallback_h
29 #include "WKAPICast.h"
32 #include <wtf/HashMap.h>
33 #include <wtf/PassRefPtr.h>
34 #include <wtf/RefCounted.h>
38 class CallbackBase : public RefCounted<CallbackBase> {
40 virtual ~CallbackBase()
44 uint64_t callbackID() const { return m_callbackID; }
47 CallbackBase(void* context)
49 , m_callbackID(generateCallbackID())
53 void* context() const { return m_context; }
56 static uint64_t generateCallbackID()
58 static uint64_t uniqueCallbackID = 1;
59 return uniqueCallbackID++;
63 uint64_t m_callbackID;
66 class VoidCallback : public CallbackBase {
68 typedef void (*CallbackFunction)(WKErrorRef, void*);
70 static PassRefPtr<VoidCallback> create(void* context, CallbackFunction callback)
72 return adoptRef(new VoidCallback(context, callback));
75 virtual ~VoidCallback()
80 void performCallback()
84 m_callback(0, context());
93 RefPtr<WebError> error = WebError::create();
94 m_callback(toAPI(error.get()), context());
100 VoidCallback(void* context, CallbackFunction callback)
101 : CallbackBase(context)
102 , m_callback(callback)
106 CallbackFunction m_callback;
109 template<typename APIReturnValueType, typename InternalReturnValueType = typename APITypeInfo<APIReturnValueType>::ImplType>
110 class GenericCallback : public CallbackBase {
112 typedef void (*CallbackFunction)(APIReturnValueType, WKErrorRef, void*);
114 static PassRefPtr<GenericCallback> create(void* context, CallbackFunction callback)
116 return adoptRef(new GenericCallback(context, callback));
119 virtual ~GenericCallback()
124 void performCallbackWithReturnValue(InternalReturnValueType returnValue)
128 m_callback(toAPI(returnValue), 0, context());
137 RefPtr<WebError> error = WebError::create();
138 m_callback(0, toAPI(error.get()), context());
144 GenericCallback(void* context, CallbackFunction callback)
145 : CallbackBase(context)
146 , m_callback(callback)
150 CallbackFunction m_callback;
153 // FIXME: Make a version of CallbackBase with two arguments, and define ComputedPagesCallback as a specialization.
154 class ComputedPagesCallback : public CallbackBase {
156 typedef void (*CallbackFunction)(const Vector<WebCore::IntRect>&, double, WKErrorRef, void*);
158 static PassRefPtr<ComputedPagesCallback> create(void* context, CallbackFunction callback)
160 return adoptRef(new ComputedPagesCallback(context, callback));
163 virtual ~ComputedPagesCallback()
168 void performCallbackWithReturnValue(const Vector<WebCore::IntRect>& returnValue1, double returnValue2)
172 m_callback(returnValue1, returnValue2, 0, context());
181 RefPtr<WebError> error = WebError::create();
182 m_callback(Vector<WebCore::IntRect>(), 0, toAPI(error.get()), context());
189 ComputedPagesCallback(void* context, CallbackFunction callback)
190 : CallbackBase(context)
191 , m_callback(callback)
195 CallbackFunction m_callback;
199 void invalidateCallbackMap(HashMap<uint64_t, T>& map)
201 Vector<T> callbacksVector;
202 copyValuesToVector(map, callbacksVector);
203 for (size_t i = 0, size = callbacksVector.size(); i < size; ++i)
204 callbacksVector[i]->invalidate();
208 } // namespace WebKit
210 #endif // GenericCallback_h