[GTK] Use API::InjectedBundle::Client in WebKitWebExtension
authorcarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Jun 2017 09:10:09 +0000 (09:10 +0000)
committercarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Jun 2017 09:10:09 +0000 (09:10 +0000)
https://bugs.webkit.org/show_bug.cgi?id=173358

Reviewed by Michael Catanzaro.

Instead of the C API.

* WebProcess/InjectedBundle/API/gtk/WebKitWebExtension.cpp:
(webkitWebExtensionCreate):
(webkitWebExtensionPageCreated): Deleted.
(webkitWebExtensionPageDestroy): Deleted.
(webkitWebExtensionDidReceiveMessage): Deleted.
(didCreatePage): Deleted.
(willDestroyPage): Deleted.
(didReceiveMessage): Deleted.
(didReceiveMessageToPage): Deleted.

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

Source/WebKit2/ChangeLog
Source/WebKit2/WebProcess/InjectedBundle/API/gtk/WebKitWebExtension.cpp

index 563dd1c..399cfdc 100644 (file)
@@ -1,3 +1,22 @@
+2017-06-14  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK] Use API::InjectedBundle::Client in WebKitWebExtension
+        https://bugs.webkit.org/show_bug.cgi?id=173358
+
+        Reviewed by Michael Catanzaro.
+
+        Instead of the C API.
+
+        * WebProcess/InjectedBundle/API/gtk/WebKitWebExtension.cpp:
+        (webkitWebExtensionCreate):
+        (webkitWebExtensionPageCreated): Deleted.
+        (webkitWebExtensionPageDestroy): Deleted.
+        (webkitWebExtensionDidReceiveMessage): Deleted.
+        (didCreatePage): Deleted.
+        (willDestroyPage): Deleted.
+        (didReceiveMessage): Deleted.
+        (didReceiveMessageToPage): Deleted.
+
 2017-06-15  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         [GTK] Use API::NotificationProvider
index 73dda43..60d841a 100644 (file)
 #include "WebKitWebExtension.h"
 
 #include "APIDictionary.h"
+#include "APIInjectedBundleBundleClient.h"
 #include "APIString.h"
-#include "WKBundle.h"
-#include "WKBundleAPICast.h"
-#include "WKBundlePage.h"
 #include "WebKitPrivate.h"
 #include "WebKitWebExtensionPrivate.h"
 #include "WebKitWebPagePrivate.h"
@@ -146,67 +144,51 @@ static void webkit_web_extension_class_init(WebKitWebExtensionClass* klass)
         WEBKIT_TYPE_WEB_PAGE);
 }
 
-static void webkitWebExtensionPageCreated(WebKitWebExtension* extension, WebPage* page)
-{
-    GRefPtr<WebKitWebPage> webPage = adoptGRef(webkitWebPageCreate(page));
-    extension->priv->pages.add(page, webPage);
-    g_signal_emit(extension, signals[PAGE_CREATED], 0, webPage.get());
-}
-
-static void webkitWebExtensionPageDestroy(WebKitWebExtension* extension, WebPage* page)
-{
-    extension->priv->pages.remove(page);
-}
-
-static void webkitWebExtensionDidReceiveMessage(WebKitWebExtension*, const String& messageName, API::Dictionary& message)
-{
-    if (messageName == String::fromUTF8("PrefetchDNS")) {
-        API::String* hostname = static_cast<API::String*>(message.get(String::fromUTF8("Hostname")));
-        WebProcess::singleton().prefetchDNS(hostname->string());
-    } else
-        ASSERT_NOT_REACHED();
-}
-
-static void didCreatePage(WKBundleRef, WKBundlePageRef page, const void* clientInfo)
-{
-    webkitWebExtensionPageCreated(WEBKIT_WEB_EXTENSION(clientInfo), toImpl(page));
-}
-
-static void willDestroyPage(WKBundleRef, WKBundlePageRef page, const void* clientInfo)
-{
-    webkitWebExtensionPageDestroy(WEBKIT_WEB_EXTENSION(clientInfo), toImpl(page));
-}
-
-static void didReceiveMessage(WKBundleRef, WKStringRef name, WKTypeRef messageBody, const void* clientInfo)
-{
-    ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
-    webkitWebExtensionDidReceiveMessage(WEBKIT_WEB_EXTENSION(clientInfo), toImpl(name)->string(), *toImpl(static_cast<WKDictionaryRef>(messageBody)));
-}
-
-static void didReceiveMessageToPage(WKBundleRef, WKBundlePageRef page, WKStringRef name, WKTypeRef messageBody, const void* clientInfo)
-{
-    ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
-    if (WebKitWebPage* webPage = WEBKIT_WEB_EXTENSION(clientInfo)->priv->pages.get(toImpl(page)))
-        webkitWebPageDidReceiveMessage(webPage, toImpl(name)->string(), *toImpl(static_cast<WKDictionaryRef>(messageBody)));
-}
+class WebExtensionInjectedBundleClient final : public API::InjectedBundle::Client {
+public:
+    explicit WebExtensionInjectedBundleClient(WebKitWebExtension* extension)
+        : m_extension(extension)
+    {
+    }
+
+private:
+    void didCreatePage(InjectedBundle&, WebPage& page) override
+    {
+        GRefPtr<WebKitWebPage> webPage = adoptGRef(webkitWebPageCreate(&page));
+        m_extension->priv->pages.add(&page, webPage);
+        g_signal_emit(m_extension, signals[PAGE_CREATED], 0, webPage.get());
+    }
+
+    void willDestroyPage(InjectedBundle&, WebPage& page) override
+    {
+        m_extension->priv->pages.remove(&page);
+    }
+
+    void didReceiveMessage(InjectedBundle&, const String& messageName, API::Object* messageBody) override
+    {
+        ASSERT(messageBody->type() == API::Object::Type::Dictionary);
+        API::Dictionary& message = *static_cast<API::Dictionary*>(messageBody);
+        if (messageName == String::fromUTF8("PrefetchDNS")) {
+            API::String* hostname = static_cast<API::String*>(message.get(String::fromUTF8("Hostname")));
+            WebProcess::singleton().prefetchDNS(hostname->string());
+        } else
+            ASSERT_NOT_REACHED();
+    }
+
+    void didReceiveMessageToPage(InjectedBundle&, WebPage& page, const String& messageName, API::Object* messageBody) override
+    {
+        ASSERT(messageBody->type() == API::Object::Type::Dictionary);
+        if (auto* webPage = m_extension->priv->pages.get(&page))
+            webkitWebPageDidReceiveMessage(webPage, messageName, *static_cast<API::Dictionary*>(messageBody));
+    }
+
+    WebKitWebExtension* m_extension;
+};
 
 WebKitWebExtension* webkitWebExtensionCreate(InjectedBundle* bundle)
 {
     WebKitWebExtension* extension = WEBKIT_WEB_EXTENSION(g_object_new(WEBKIT_TYPE_WEB_EXTENSION, NULL));
-
-    WKBundleClientV1 wkBundleClient = {
-        {
-            1, // version
-            extension, // clientInfo
-        },
-        didCreatePage,
-        willDestroyPage,
-        0, // didInitializePageGroup
-        didReceiveMessage,
-        didReceiveMessageToPage
-    };
-    WKBundleSetClient(toAPI(bundle), &wkBundleClient.base);
-
+    bundle->setClient(std::make_unique<WebExtensionInjectedBundleClient>(extension));
     return extension;
 }