[GTK] Expose user script messages to GObject DOM bindings
authorcarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 12 Nov 2014 12:30:38 +0000 (12:30 +0000)
committercarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 12 Nov 2014 12:30:38 +0000 (12:30 +0000)
https://bugs.webkit.org/show_bug.cgi?id=138411

Reviewed by Philippe Normand.

Source/WebCore:

* PlatformGTK.cmake: Add WebKitDOMCustomUnstable.h to the list of
installed headers and make a symlink for it in the DerivedSources dir.
* bindings/gobject/WebKitDOMCustomUnstable.h: Added.
* bindings/gobject/WebKitDOMCustom.cpp:
(webkit_dom_dom_window_get_webkit_namespace):
(webkit_dom_user_message_handlers_namespace_get_handler):
* bindings/scripts/CodeGeneratorGObject.pm:
(HasUnstableCustomAPI): Helper function to check if the given
class has custom unstable API.
(WriteData): Include WebKitDOMCustomUnstable.h header if the class
has API defined there.

Tools:

Add a test case to check that user script messages sent using the
DOM bindings API are also received in the UI process.

* TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitUserContentManager.cpp:
(UserScriptMessageTest::waitUntilMessageReceived):
(UserScriptMessageTest::postMessageAndWaitUntilReceived):
(testUserContentManagerScriptMessageFromDOMBindings):
(beforeAll):
* TestWebKitAPI/Tests/WebKit2Gtk/WebExtensionTest.cpp:
(documentLoadedCallback):

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

Source/WebCore/ChangeLog
Source/WebCore/PlatformGTK.cmake
Source/WebCore/bindings/gobject/WebKitDOMCustom.cpp
Source/WebCore/bindings/gobject/WebKitDOMCustomUnstable.h [new file with mode: 0644]
Source/WebCore/bindings/scripts/CodeGeneratorGObject.pm
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitUserContentManager.cpp
Tools/TestWebKitAPI/Tests/WebKit2Gtk/WebExtensionTest.cpp

index c975ae3..6b1490e 100644 (file)
@@ -1,5 +1,24 @@
 2014-11-12  Carlos Garcia Campos  <cgarcia@igalia.com>
 
+        [GTK] Expose user script messages to GObject DOM bindings
+        https://bugs.webkit.org/show_bug.cgi?id=138411
+
+        Reviewed by Philippe Normand.
+
+        * PlatformGTK.cmake: Add WebKitDOMCustomUnstable.h to the list of
+        installed headers and make a symlink for it in the DerivedSources dir.
+        * bindings/gobject/WebKitDOMCustomUnstable.h: Added.
+        * bindings/gobject/WebKitDOMCustom.cpp:
+        (webkit_dom_dom_window_get_webkit_namespace):
+        (webkit_dom_user_message_handlers_namespace_get_handler):
+        * bindings/scripts/CodeGeneratorGObject.pm:
+        (HasUnstableCustomAPI): Helper function to check if the given
+        class has custom unstable API.
+        (WriteData): Include WebKitDOMCustomUnstable.h header if the class
+        has API defined there.
+
+2014-11-12  Carlos Garcia Campos  <cgarcia@igalia.com>
+
         [GTK] Fix how SerializedScriptValue is exposed to GObject DOM bindings
         https://bugs.webkit.org/show_bug.cgi?id=138409
 
index 10c9c40..37e07f6 100644 (file)
@@ -635,7 +635,10 @@ list(APPEND GObjectDOMBindingsUnstable_IDL_FILES
     page/PerformanceNavigation.idl
     page/PerformanceTiming.idl
     page/Screen.idl
+    page/UserMessageHandler.idl
+    page/UserMessageHandlersNamespace.idl
     page/WebKitPoint.idl
+    page/WebKitNamespace.idl
 
     plugins/DOMMimeType.idl
     plugins/DOMMimeTypeArray.idl
@@ -692,6 +695,7 @@ set(GObjectDOMBindingsStable_INSTALLED_HEADERS
 
 set(GObjectDOMBindingsUnstable_INSTALLED_HEADERS
      ${DERIVED_SOURCES_GOBJECT_DOM_BINDINGS_DIR}/webkitdomdefines-unstable.h
+     ${WEBCORE_DIR}/bindings/gobject/WebKitDOMCustomUnstable.h
 )
 
 foreach (file ${GObjectDOMBindingsStable_IDL_FILES})
@@ -733,7 +737,8 @@ add_custom_command(
 
 # Some of the static headers are included by generated public headers with include <webkitdom/WebKitDOMFoo.h>.
 # We need those headers in the derived sources to be in webkitdom directory.
-foreach (classname ${GObjectDOMBindings_STATIC_CLASS_LIST})
+set(GObjectDOMBindings_STATIC_HEADER_NAMES ${GObjectDOMBindings_STATIC_CLASS_LIST} CustomUnstable)
+foreach (classname ${GObjectDOMBindings_STATIC_HEADER_NAMES})
     add_custom_command(
         OUTPUT ${DERIVED_SOURCES_GOBJECT_DOM_BINDINGS_DIR}/WebKitDOM${classname}.h
         DEPENDS ${WEBCORE_DIR}/bindings/gobject/WebKitDOM${classname}.h
index 0f0794c..c77190d 100644 (file)
 #include "WebKitDOMCustom.h"
 
 #include "JSMainThreadExecState.h"
+#include "WebKitDOMDOMWindowPrivate.h"
 #include "WebKitDOMHTMLInputElement.h"
 #include "WebKitDOMHTMLInputElementPrivate.h"
-#include "WebKitDOMHTMLMediaElementPrivate.h"
 #include "WebKitDOMHTMLTextAreaElement.h"
 #include "WebKitDOMHTMLTextAreaElementPrivate.h"
 #include "WebKitDOMPrivate.h"
-#include "gobject/ConvertToUTF8String.h"
-
-#if ENABLE(VIDEO)
-#include "TextTrack.h"
-#include "WebKitDOMTextTrackPrivate.h"
-#endif
+#include "WebKitDOMUserMessageHandlerPrivate.h"
+#include "WebKitDOMUserMessageHandlersNamespacePrivate.h"
+#include "WebKitDOMWebKitNamespacePrivate.h"
 
 using namespace WebKit;
 
@@ -49,4 +46,21 @@ gboolean webkit_dom_html_input_element_is_edited(WebKitDOMHTMLInputElement* inpu
     return core(input)->lastChangeWasUserEdit();
 }
 
+WebKitDOMWebKitNamespace* webkit_dom_dom_window_get_webkit_namespace(WebKitDOMDOMWindow* window)
+{
+    g_return_val_if_fail(WEBKIT_DOM_IS_DOM_WINDOW(window), nullptr);
+
+    WebCore::DOMWindow* domWindow = core(window);
+    if (!domWindow->shouldHaveWebKitNamespaceForWorld(WebCore::mainThreadNormalWorld()))
+        return nullptr;
+    return kit(domWindow->webkitNamespace());
+}
+
+WebKitDOMUserMessageHandler* webkit_dom_user_message_handlers_namespace_get_handler(WebKitDOMUserMessageHandlersNamespace* handlersNamespace, const gchar* name)
+{
+    g_return_val_if_fail(WEBKIT_DOM_IS_USER_MESSAGE_HANDLERS_NAMESPACE(handlersNamespace), nullptr);
+    g_return_val_if_fail(name, nullptr);
+
+    return kit(core(handlersNamespace)->handler(String::fromUTF8(name), WebCore::mainThreadNormalWorld()));
+}
 
diff --git a/Source/WebCore/bindings/gobject/WebKitDOMCustomUnstable.h b/Source/WebCore/bindings/gobject/WebKitDOMCustomUnstable.h
new file mode 100644 (file)
index 0000000..79d0f56
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ *  Copyright (C) 2014 Igalia S.L.
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef WebKitDOMCustomUnstable_h
+#define WebKitDOMCustomUnstable_h
+
+#ifdef WEBKIT_DOM_USE_UNSTABLE_API
+
+#include <webkitdom/webkitdomdefines-unstable.h>
+
+G_BEGIN_DECLS
+
+/**
+ * webkit_dom_dom_window_get_webkit_namespace:
+ * @self: A #WebKitDOMDOMWindow
+ *
+ * Returns: (transfer none): A #WebKitDOMWebKitNamespace
+ *
+ * Stability: Unstable
+ * Since: 2.8
+ */
+WEBKIT_API WebKitDOMWebKitNamespace *
+webkit_dom_dom_window_get_webkit_namespace(WebKitDOMDOMWindow* self);
+
+/**
+ * webkit_dom_user_message_handlers_namespace_get_handler:
+ * @self: A #WebKitDOMUserMessageHandlersNamespace
+ * @name: a #gchar
+ *
+ * Returns: (transfer none): A #WebKitDOMUserMessageHandler
+ *
+ * Stability: Unstable
+ * Since: 2.8
+ */
+WEBKIT_API WebKitDOMUserMessageHandler *
+webkit_dom_user_message_handlers_namespace_get_handler(WebKitDOMUserMessageHandlersNamespace* self, const gchar* name);
+
+G_END_DECLS
+
+#endif /* WEBKIT_DOM_USE_UNSTABLE_API */
+
+#endif
index 5ddf102..d570a90 100644 (file)
@@ -1580,6 +1580,12 @@ sub Generate {
     $object->GenerateEndHeader();
 }
 
+sub HasUnstableCustomAPI {
+    my $domClassName = shift;
+
+    return scalar(grep {$_ eq $domClassName} qw(WebKitDOMDOMWindow WebKitDOMUserMessageHandlersNamespace));
+}
+
 sub WriteData {
     my $object = shift;
     my $interface = shift;
@@ -1647,6 +1653,9 @@ EOF
     if ($isStableClass) {
         print HEADER "#include <webkitdom/webkitdomdefines.h>\n\n";
     } else {
+        if (HasUnstableCustomAPI($className)) {
+            print HEADER "#include <webkitdom/WebKitDOMCustomUnstable.h>\n";
+        }
         print HEADER "#include <webkitdom/webkitdomdefines-unstable.h>\n\n";
     }
     print HEADER @hBodyPre;
@@ -1670,11 +1679,13 @@ EOF
 
 #ifdef WEBKIT_DOM_USE_UNSTABLE_API
 
-#include <webkitdom/webkitdomdefines-unstable.h>
 EOF
-
         print UNSTABLE $text;
-        print UNSTABLE "\n";
+        if (HasUnstableCustomAPI($className)) {
+            print UNSTABLE "#include <webkitdom/WebKitDOMCustomUnstable.h>\n";
+        }
+        print UNSTABLE "#include <webkitdom/webkitdomdefines-unstable.h>\n\n";
+
         print UNSTABLE "#if ${conditionalString}\n\n" if $conditionalString;
         print UNSTABLE "G_BEGIN_DECLS\n";
         print UNSTABLE "\n";
index f9e5a6c..e1741b3 100644 (file)
@@ -1,5 +1,23 @@
 2014-11-12  Carlos Garcia Campos  <cgarcia@igalia.com>
 
+        [GTK] Expose user script messages to GObject DOM bindings
+        https://bugs.webkit.org/show_bug.cgi?id=138411
+
+        Reviewed by Philippe Normand.
+
+        Add a test case to check that user script messages sent using the
+        DOM bindings API are also received in the UI process.
+
+        * TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitUserContentManager.cpp:
+        (UserScriptMessageTest::waitUntilMessageReceived):
+        (UserScriptMessageTest::postMessageAndWaitUntilReceived):
+        (testUserContentManagerScriptMessageFromDOMBindings):
+        (beforeAll):
+        * TestWebKitAPI/Tests/WebKit2Gtk/WebExtensionTest.cpp:
+        (documentLoadedCallback):
+
+2014-11-12  Carlos Garcia Campos  <cgarcia@igalia.com>
+
         Unreviewed. Fix GTK+ clean build after r175930.
 
         Partial rollout of r175930 to not build the new unit test that
index a31f3ca..1cee9ad 100644 (file)
@@ -248,7 +248,7 @@ public:
         test->m_userScriptMessage = webkit_javascript_result_ref(jsResult);
     }
 
-    WebKitJavascriptResult* postMessageAndWaitUntilReceived(const char* handlerName, const char* javascriptValueAsText)
+    WebKitJavascriptResult* waitUntilMessageReceived(const char* handlerName)
     {
         if (m_userScriptMessage) {
             webkit_javascript_result_unref(m_userScriptMessage);
@@ -258,14 +258,18 @@ public:
         GUniquePtr<char> signalName(g_strdup_printf("script-message-received::%s", handlerName));
         g_signal_connect(m_userContentManager.get(), signalName.get(), G_CALLBACK(scriptMessageReceived), this);
 
-        GUniquePtr<char> javascriptSnippet(g_strdup_printf("window.webkit.messageHandlers.%s.postMessage(%s);", handlerName, javascriptValueAsText));
-        webkit_web_view_run_javascript(m_webView, javascriptSnippet.get(), nullptr, nullptr, nullptr);
         g_main_loop_run(m_mainLoop);
-
         g_assert(m_userScriptMessage);
         return m_userScriptMessage;
     }
 
+    WebKitJavascriptResult* postMessageAndWaitUntilReceived(const char* handlerName, const char* javascriptValueAsText)
+    {
+        GUniquePtr<char> javascriptSnippet(g_strdup_printf("window.webkit.messageHandlers.%s.postMessage(%s);", handlerName, javascriptValueAsText));
+        webkit_web_view_run_javascript(m_webView, javascriptSnippet.get(), nullptr, nullptr, nullptr);
+        return waitUntilMessageReceived(handlerName);
+    }
+
 private:
     WebKitJavascriptResult* m_userScriptMessage;
 };
@@ -341,6 +345,19 @@ static void testUserContentManagerScriptMessageReceived(UserScriptMessageTest* t
     test->unregisterHandler("anotherHandler");
 }
 
+static void testUserContentManagerScriptMessageFromDOMBindings(UserScriptMessageTest* test, gconstpointer)
+{
+    g_assert(test->registerHandler("dom"));
+
+    test->loadHtml("<html></html>", nullptr);
+    WebKitJavascriptResult* javascriptResult = test->waitUntilMessageReceived("dom");
+    g_assert(javascriptResult);
+    GUniquePtr<char> valueString(WebViewTest::javascriptResultToCString(javascriptResult));
+    g_assert_cmpstr(valueString.get(), ==, "DocumentLoaded");
+
+    test->unregisterHandler("dom");
+}
+
 static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
 {
     soup_message_set_status(message, SOUP_STATUS_OK);
@@ -350,6 +367,7 @@ static void serverCallback(SoupServer* server, SoupMessage* message, const char*
 
 void beforeAll()
 {
+    webkit_web_context_set_web_extensions_directory(webkit_web_context_get_default(), WEBKIT_TEST_WEB_EXTENSIONS_DIR);
     kServer = new WebKitTestServer();
     kServer->run(serverCallback);
 
@@ -357,6 +375,7 @@ void beforeAll()
     UserContentManagerTest::add("WebKitUserContentManager", "injected-style-sheet", testUserContentManagerInjectedStyleSheet);
     UserContentManagerTest::add("WebKitUserContentManager", "injected-script", testUserContentManagerInjectedScript);
     UserScriptMessageTest::add("WebKitUserContentManager", "script-message-received", testUserContentManagerScriptMessageReceived);
+    UserScriptMessageTest::add("WebKitUserContentManager", "script-message-from-dom-bindings", testUserContentManagerScriptMessageFromDOMBindings);
 }
 
 void afterAll()
index 515902d..1926b74 100644 (file)
 #include <wtf/gobject/GUniquePtr.h>
 #include <wtf/text/CString.h>
 
+#define WEBKIT_DOM_USE_UNSTABLE_API
+#include <webkitdom/WebKitDOMWebKitNamespace.h>
+#include <webkitdom/WebKitDOMUserMessageHandlersNamespace.h>
+#include <webkitdom/WebKitDOMUserMessageHandler.h>
+
 static const char introspectionXML[] =
     "<node>"
     " <interface name='org.webkit.gtk.WebExtensionTest'>"
@@ -98,8 +103,18 @@ static void emitDocumentLoaded(GDBusConnection* connection)
     g_assert(ok);
 }
 
-static void documentLoadedCallback(WebKitWebPage*, WebKitWebExtension* extension)
+static void documentLoadedCallback(WebKitWebPage* webPage, WebKitWebExtension* extension)
 {
+    // FIXME: Too much code just to send a message, we need convenient custom API for this.
+    WebKitDOMDocument* document = webkit_web_page_get_dom_document(webPage);
+    WebKitDOMDOMWindow* window = webkit_dom_document_get_default_view(document);
+    if (WebKitDOMWebKitNamespace* webkit = webkit_dom_dom_window_get_webkit_namespace(window)) {
+        WebKitDOMUserMessageHandlersNamespace* messageHandlers = webkit_dom_webkit_namespace_get_message_handlers(webkit);
+        if (WebKitDOMUserMessageHandler* handler = webkit_dom_user_message_handlers_namespace_get_handler(messageHandlers, "dom"))
+            webkit_dom_user_message_handler_post_message(handler, "DocumentLoaded");
+    }
+
+
     gpointer data = g_object_get_data(G_OBJECT(extension), "dbus-connection");
     if (data)
         emitDocumentLoaded(G_DBUS_CONNECTION(data));