CallbackMap::put should be able to take an unwrapped function
authormitz@apple.com <mitz@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 21 Jun 2014 22:25:34 +0000 (22:25 +0000)
committermitz@apple.com <mitz@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 21 Jun 2014 22:25:34 +0000 (22:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=134104

Reviewed by Anders Carlsson.

* UIProcess/GenericCallback.h:
(WebKit::CallbackMap::put): Added an overload that takes a function and creates and puts a
callback. Changed both overloads of put() to return the callback ID.

* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::validateCommand):
(WebKit::WebPageProxy::runJavaScriptInMainFrame):
(WebKit::WebPageProxy::getRenderTreeExternalRepresentation):
(WebKit::WebPageProxy::getSourceForFrame):
(WebKit::WebPageProxy::getContentsAsString):
(WebKit::WebPageProxy::getBytecodeProfile):
(WebKit::WebPageProxy::getContentsAsMHTMLData):
(WebKit::WebPageProxy::getSelectionOrContentsAsString):
(WebKit::WebPageProxy::getSelectionAsWebArchiveData):
(WebKit::WebPageProxy::getMainResourceDataOfFrame):
(WebKit::WebPageProxy::getResourceDataFromFrame):
(WebKit::WebPageProxy::getWebArchiveOfFrame):
(WebKit::WebPageProxy::getMarkedRangeAsync):
(WebKit::WebPageProxy::getSelectedRangeAsync):
(WebKit::WebPageProxy::characterIndexForPointAsync):
(WebKit::WebPageProxy::firstRectForCharacterRangeAsync):
(WebKit::WebPageProxy::takeSnapshot):

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

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/GenericCallback.h
Source/WebKit2/UIProcess/WebPageProxy.cpp

index ad1d77a..1c3d4a5 100644 (file)
@@ -1,3 +1,33 @@
+2014-06-21  Dan Bernstein  <mitz@apple.com>
+
+        CallbackMap::put should be able to take an unwrapped function
+        https://bugs.webkit.org/show_bug.cgi?id=134104
+
+        Reviewed by Anders Carlsson.
+
+        * UIProcess/GenericCallback.h:
+        (WebKit::CallbackMap::put): Added an overload that takes a function and creates and puts a
+        callback. Changed both overloads of put() to return the callback ID.
+
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::validateCommand):
+        (WebKit::WebPageProxy::runJavaScriptInMainFrame):
+        (WebKit::WebPageProxy::getRenderTreeExternalRepresentation):
+        (WebKit::WebPageProxy::getSourceForFrame):
+        (WebKit::WebPageProxy::getContentsAsString):
+        (WebKit::WebPageProxy::getBytecodeProfile):
+        (WebKit::WebPageProxy::getContentsAsMHTMLData):
+        (WebKit::WebPageProxy::getSelectionOrContentsAsString):
+        (WebKit::WebPageProxy::getSelectionAsWebArchiveData):
+        (WebKit::WebPageProxy::getMainResourceDataOfFrame):
+        (WebKit::WebPageProxy::getResourceDataFromFrame):
+        (WebKit::WebPageProxy::getWebArchiveOfFrame):
+        (WebKit::WebPageProxy::getMarkedRangeAsync):
+        (WebKit::WebPageProxy::getSelectedRangeAsync):
+        (WebKit::WebPageProxy::characterIndexForPointAsync):
+        (WebKit::WebPageProxy::firstRectForCharacterRangeAsync):
+        (WebKit::WebPageProxy::takeSnapshot):
+
 2014-06-21  Anders Carlsson  <andersca@apple.com>
 
         Make it possible to override debug preferences on a per-identifier basis as well as globally
index 98aa15c..9807088 100644 (file)
@@ -167,10 +167,30 @@ void invalidateCallbackMap(HashMap<uint64_t, T>& callbackMap, CallbackBase::Erro
 
 class CallbackMap {
 public:
-    void put(PassRefPtr<CallbackBase> callback)
+    uint64_t put(PassRefPtr<CallbackBase> callback)
     {
         ASSERT(!m_map.contains(callback->callbackID()));
-        m_map.set(callback->callbackID(), callback);
+
+        uint64_t callbackID = callback->callbackID();
+        m_map.set(callbackID, callback);
+        return callbackID;
+    }
+
+    template<unsigned I, typename T, typename... U>
+    struct GenericCallbackType {
+        typedef typename GenericCallbackType<I - 1, U..., T>::type type;
+    };
+
+    template<typename... U>
+    struct GenericCallbackType<1, CallbackBase::Error, U...> {
+        typedef GenericCallback<U...> type;
+    };
+
+    template<typename... T>
+    uint64_t put(std::function<void (T...)> function)
+    {
+        auto callback = GenericCallbackType<sizeof...(T), T...>::type::create(std::move(function));
+        return put(callback);
     }
 
     template<class T>
index 2ab6631..a4ea25e 100644 (file)
@@ -1241,15 +1241,12 @@ void WebPageProxy::clearSelection()
 
 void WebPageProxy::validateCommand(const String& commandName, std::function<void (const String&, bool, int32_t, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<ValidateCommandCallback> callback = ValidateCommandCallback::create(std::move(callbackFunction));
-
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(String(), false, 0, CallbackBase::Error::Unknown);
         return;
     }
 
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::ValidateCommand(commandName, callbackID), m_pageID);
 }
 
@@ -2187,149 +2184,127 @@ void WebPageProxy::countStringMatches(const String& string, FindOptions options,
 
 void WebPageProxy::runJavaScriptInMainFrame(const String& script, std::function<void (WebSerializedScriptValue*, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<ScriptValueCallback> callback = ScriptValueCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(nullptr, CallbackBase::Error::Unknown);
         return;
     }
 
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::RunJavaScriptInMainFrame(script, callbackID), m_pageID);
 }
 
 void WebPageProxy::getRenderTreeExternalRepresentation(std::function<void (const String&, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<StringCallback> callback = StringCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(String(), CallbackBase::Error::Unknown);
         return;
     }
     
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::GetRenderTreeExternalRepresentation(callbackID), m_pageID);
 }
 
 void WebPageProxy::getSourceForFrame(WebFrameProxy* frame, std::function<void (const String&, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<StringCallback> callback = StringCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(String(), CallbackBase::Error::Unknown);
         return;
     }
     
-    uint64_t callbackID = callback->callbackID();
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_loadDependentStringCallbackIDs.add(callbackID);
-    m_callbacks.put(callback);
     m_process->send(Messages::WebPage::GetSourceForFrame(frame->frameID(), callbackID), m_pageID);
 }
 
 void WebPageProxy::getContentsAsString(std::function<void (const String&, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<StringCallback> callback = StringCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(String(), CallbackBase::Error::Unknown);
         return;
     }
     
-    uint64_t callbackID = callback->callbackID();
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_loadDependentStringCallbackIDs.add(callbackID);
-    m_callbacks.put(callback);
     m_process->send(Messages::WebPage::GetContentsAsString(callbackID), m_pageID);
 }
 
 void WebPageProxy::getBytecodeProfile(std::function<void (const String&, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<StringCallback> callback = StringCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(String(), CallbackBase::Error::Unknown);
         return;
     }
     
-    uint64_t callbackID = callback->callbackID();
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_loadDependentStringCallbackIDs.add(callbackID);
-    m_callbacks.put(callback);
     m_process->send(Messages::WebPage::GetBytecodeProfile(callbackID), m_pageID);
 }
     
 #if ENABLE(MHTML)
 void WebPageProxy::getContentsAsMHTMLData(std::function<void (API::Data*, CallbackBase::Error)> callbackFunction, bool useBinaryEncoding)
 {
-    RefPtr<DataCallback> callback = DataCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(nullptr, CallbackBase::Error::Unknown);
         return;
     }
 
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::GetContentsAsMHTMLData(callbackID, useBinaryEncoding), m_pageID);
 }
 #endif
 
 void WebPageProxy::getSelectionOrContentsAsString(std::function<void (const String&, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<StringCallback> callback = StringCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(String(), CallbackBase::Error::Unknown);
         return;
     }
     
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::GetSelectionOrContentsAsString(callbackID), m_pageID);
 }
 
 void WebPageProxy::getSelectionAsWebArchiveData(std::function<void (API::Data*, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<DataCallback> callback = DataCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(nullptr, CallbackBase::Error::Unknown);
         return;
     }
     
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::GetSelectionAsWebArchiveData(callbackID), m_pageID);
 }
 
 void WebPageProxy::getMainResourceDataOfFrame(WebFrameProxy* frame, std::function<void (API::Data*, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<DataCallback> callback = DataCallback::create(std::move(callbackFunction));
     if (!isValid() || !frame) {
-        callback->invalidate();
+        callbackFunction(nullptr, CallbackBase::Error::Unknown);
         return;
     }
     
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::GetMainResourceDataOfFrame(frame->frameID(), callbackID), m_pageID);
 }
 
 void WebPageProxy::getResourceDataFromFrame(WebFrameProxy* frame, API::URL* resourceURL, std::function<void (API::Data*, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<DataCallback> callback = DataCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(nullptr, CallbackBase::Error::Unknown);
         return;
     }
     
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::GetResourceDataFromFrame(frame->frameID(), resourceURL->string(), callbackID), m_pageID);
 }
 
 void WebPageProxy::getWebArchiveOfFrame(WebFrameProxy* frame, std::function<void (API::Data*, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<DataCallback> callback = DataCallback::create(std::move(callbackFunction));
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(nullptr, CallbackBase::Error::Unknown);
         return;
     }
     
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::GetWebArchiveOfFrame(frame->frameID(), callbackID), m_pageID);
 }
 
@@ -4968,16 +4943,12 @@ void WebPageProxy::insertTextAsync(const String& text, const EditingRange& repla
 
 void WebPageProxy::getMarkedRangeAsync(std::function<void (EditingRange, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<EditingRangeCallback> callback = EditingRangeCallback::create(std::move(callbackFunction));
-
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(EditingRange(), CallbackBase::Error::Unknown);
         return;
     }
 
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
-
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     process().send(Messages::WebPage::GetMarkedRangeAsync(callbackID), m_pageID);
 }
 
@@ -4990,39 +4961,29 @@ void WebPageProxy::getSelectedRangeAsync(std::function<void (EditingRange, Callb
         return;
     }
 
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
-
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     process().send(Messages::WebPage::GetSelectedRangeAsync(callbackID), m_pageID);
 }
 
 void WebPageProxy::characterIndexForPointAsync(const WebCore::IntPoint& point, std::function<void (uint64_t, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<UnsignedCallback> callback = UnsignedCallback::create(std::move(callbackFunction));
-
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(0, CallbackBase::Error::Unknown);
         return;
     }
 
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
-
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     process().send(Messages::WebPage::CharacterIndexForPointAsync(point, callbackID), m_pageID);
 }
 
 void WebPageProxy::firstRectForCharacterRangeAsync(const EditingRange& range, std::function<void (const WebCore::IntRect&, const EditingRange&, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<RectForCharacterRangeCallback> callback = RectForCharacterRangeCallback::create(std::move(callbackFunction));
-
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(WebCore::IntRect(), EditingRange(), CallbackBase::Error::Unknown);
         return;
     }
 
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
-
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     process().send(Messages::WebPage::FirstRectForCharacterRangeAsync(range, callbackID), m_pageID);
 }
 
@@ -5049,16 +5010,12 @@ void WebPageProxy::confirmCompositionAsync()
 
 void WebPageProxy::takeSnapshot(IntRect rect, IntSize bitmapSize, SnapshotOptions options, std::function<void (const ShareableBitmap::Handle&, CallbackBase::Error)> callbackFunction)
 {
-    RefPtr<ImageCallback> callback = ImageCallback::create(std::move(callbackFunction));
-
     if (!isValid()) {
-        callback->invalidate();
+        callbackFunction(ShareableBitmap::Handle(), CallbackBase::Error::Unknown);
         return;
     }
 
-    uint64_t callbackID = callback->callbackID();
-    m_callbacks.put(callback);
-
+    uint64_t callbackID = m_callbacks.put(std::move(callbackFunction));
     m_process->send(Messages::WebPage::TakeSnapshot(rect, bitmapSize, options, callbackID), m_pageID);
 }