Title: [235310] releases/WebKitGTK/webkit-2.22
Revision
235310
Author
carlo...@webkit.org
Date
2018-08-24 01:10:58 -0700 (Fri, 24 Aug 2018)

Log Message

Merge r235282 - [GTK][WPE] Add API to inject/register user content in isolated worlds
https://bugs.webkit.org/show_bug.cgi?id=188883

Reviewed by Michael Catanzaro.

Source/WebKit:

Add new API to create user scripts/stylesheets for a given script world and to register/unregister user script
messages in a given script world.

* UIProcess/API/glib/WebKitUserContent.cpp:
(webkitUserContentWorld):
(_WebKitUserStyleSheet::_WebKitUserStyleSheet):
(webkit_user_style_sheet_new):
(webkit_user_style_sheet_new_for_world):
(_WebKitUserScript::_WebKitUserScript):
(webkit_user_script_new):
(webkit_user_script_new_for_world):
* UIProcess/API/glib/WebKitUserContentManager.cpp:
(webkit_user_content_manager_register_script_message_handler_in_world):
(webkit_user_content_manager_unregister_script_message_handler_in_world):
* UIProcess/API/glib/WebKitUserContentPrivate.h:
* UIProcess/API/gtk/WebKitUserContent.h:
* UIProcess/API/gtk/WebKitUserContentManager.h:
* UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt:
* UIProcess/API/wpe/WebKitUserContent.h:
* UIProcess/API/wpe/WebKitUserContentManager.h:
* WebProcess/UserContent/WebUserContentController.cpp:
(WebKit::WebUserContentController::addUserContentWorlds):

Tools:

Add test cases for the new API.

* TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentManager.cpp:
(isStyleSheetInjectedForURLAtPath):
(isScriptInjectedForURLAtPath):
(testUserContentManagerInjectedStyleSheet):
(testUserContentManagerInjectedScript):
(UserScriptMessageTest::registerHandler):
(UserScriptMessageTest::unregisterHandler):
(UserScriptMessageTest::postMessageAndWaitUntilReceived):
(testUserContentManagerScriptMessageInWorldReceived):
(beforeAll):

Modified Paths

Diff

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/ChangeLog (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/ChangeLog	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/ChangeLog	2018-08-24 08:10:58 UTC (rev 235310)
@@ -1,3 +1,33 @@
+2018-08-23  Carlos Garcia Campos  <cgar...@igalia.com>
+
+        [GTK][WPE] Add API to inject/register user content in isolated worlds
+        https://bugs.webkit.org/show_bug.cgi?id=188883
+
+        Reviewed by Michael Catanzaro.
+
+        Add new API to create user scripts/stylesheets for a given script world and to register/unregister user script
+        messages in a given script world.
+
+        * UIProcess/API/glib/WebKitUserContent.cpp:
+        (webkitUserContentWorld):
+        (_WebKitUserStyleSheet::_WebKitUserStyleSheet):
+        (webkit_user_style_sheet_new):
+        (webkit_user_style_sheet_new_for_world):
+        (_WebKitUserScript::_WebKitUserScript):
+        (webkit_user_script_new):
+        (webkit_user_script_new_for_world):
+        * UIProcess/API/glib/WebKitUserContentManager.cpp:
+        (webkit_user_content_manager_register_script_message_handler_in_world):
+        (webkit_user_content_manager_unregister_script_message_handler_in_world):
+        * UIProcess/API/glib/WebKitUserContentPrivate.h:
+        * UIProcess/API/gtk/WebKitUserContent.h:
+        * UIProcess/API/gtk/WebKitUserContentManager.h:
+        * UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt:
+        * UIProcess/API/wpe/WebKitUserContent.h:
+        * UIProcess/API/wpe/WebKitUserContentManager.h:
+        * WebProcess/UserContent/WebUserContentController.cpp:
+        (WebKit::WebUserContentController::addUserContentWorlds):
+
 2018-08-21  Ryosuke Niwa  <rn...@webkit.org>
 
         Replace booleans for modifier keys in UIEventWithKeyState with OptionSet<Modifier>

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/glib/WebKitUserContent.cpp (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/glib/WebKitUserContent.cpp	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/glib/WebKitUserContent.cpp	2018-08-24 08:10:58 UTC (rev 235310)
@@ -21,6 +21,8 @@
 #include "WebKitUserContent.h"
 
 #include "WebKitUserContentPrivate.h"
+#include <wtf/HashMap.h>
+#include <wtf/NeverDestroyed.h>
 #include <wtf/text/CString.h>
 
 using namespace WebCore;
@@ -35,6 +37,13 @@
  * Since: 2.6
  */
 
+
+API::UserContentWorld& webkitUserContentWorld(const char* worldName)
+{
+    static NeverDestroyed<HashMap<CString, RefPtr<API::UserContentWorld>>> map;
+    return *map.get().ensure(worldName, [worldName = String::fromUTF8(worldName)] { return API::UserContentWorld::worldWithName(worldName); }).iterator->value;
+}
+
 static inline UserContentInjectedFrames toUserContentInjectedFrames(WebKitUserContentInjectedFrames injectedFrames)
 {
     switch (injectedFrames) {
@@ -86,12 +95,12 @@
 }
 
 struct _WebKitUserStyleSheet {
-    _WebKitUserStyleSheet(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserStyleLevel level, const char* const* whitelist, const char* const* blacklist)
+    _WebKitUserStyleSheet(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserStyleLevel level, const char* const* whitelist, const char* const* blacklist, API::UserContentWorld& world)
         : userStyleSheet(adoptRef(new API::UserStyleSheet(UserStyleSheet {
             String::fromUTF8(source), URL { },
             toStringVector(whitelist), toStringVector(blacklist),
             toUserContentInjectedFrames(injectedFrames),
-            toUserStyleLevel(level) }, API::UserContentWorld::normalWorld())))
+            toUserStyleLevel(level) }, world)))
         , referenceCount(1)
     {
     }
@@ -162,10 +171,36 @@
 {
     g_return_val_if_fail(source, nullptr);
     WebKitUserStyleSheet* userStyleSheet = static_cast<WebKitUserStyleSheet*>(fastMalloc(sizeof(WebKitUserStyleSheet)));
-    new (userStyleSheet) WebKitUserStyleSheet(source, injectedFrames, level, whitelist, blacklist);
+    new (userStyleSheet) WebKitUserStyleSheet(source, injectedFrames, level, whitelist, blacklist, API::UserContentWorld::normalWorld());
     return userStyleSheet;
 }
 
+/**
+ * webkit_user_style_sheet_new_for_world:
+ * @source: Source code of the user style sheet.
+ * @injected_frames: A #WebKitUserContentInjectedFrames value
+ * @level: A #WebKitUserStyleLevel
+ * @world_name: the name of a #WebKitScriptWorld
+ * @whitelist: (array zero-terminated=1) (allow-none): A whitelist of URI patterns or %NULL
+ * @blacklist: (array zero-terminated=1) (allow-none): A blacklist of URI patterns or %NULL
+ *
+ * Creates a new user style sheet for script world with name @world_name.
+ * See webkit_user_style_sheet_new() for a full description.
+ *
+ * Returns: A new #WebKitUserStyleSheet
+ *
+ * Since: 2.22
+ */
+WebKitUserStyleSheet* webkit_user_style_sheet_new_for_world(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserStyleLevel level, const char* worldName, const char* const* whitelist, const char* const* blacklist)
+{
+    g_return_val_if_fail(source, nullptr);
+    g_return_val_if_fail(worldName, nullptr);
+
+    WebKitUserStyleSheet* userStyleSheet = static_cast<WebKitUserStyleSheet*>(fastMalloc(sizeof(WebKitUserStyleSheet)));
+    new (userStyleSheet) WebKitUserStyleSheet(source, injectedFrames, level, whitelist, blacklist, webkitUserContentWorld(worldName));
+    return userStyleSheet;
+}
+
 API::UserStyleSheet& webkitUserStyleSheetGetUserStyleSheet(WebKitUserStyleSheet* userStyleSheet)
 {
     return *userStyleSheet->userStyleSheet;
@@ -172,12 +207,12 @@
 }
 
 struct _WebKitUserScript {
-    _WebKitUserScript(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserScriptInjectionTime injectionTime, const gchar* const* whitelist, const gchar* const* blacklist)
+    _WebKitUserScript(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserScriptInjectionTime injectionTime, const gchar* const* whitelist, const gchar* const* blacklist, API::UserContentWorld& world)
         : userScript(adoptRef(new API::UserScript(UserScript {
             String::fromUTF8(source), URL { },
             toStringVector(whitelist), toStringVector(blacklist),
             toUserScriptInjectionTime(injectionTime),
-            toUserContentInjectedFrames(injectedFrames) }, API::UserContentWorld::normalWorld())))
+            toUserContentInjectedFrames(injectedFrames) }, world)))
         , referenceCount(1)
     {
     }
@@ -248,10 +283,36 @@
 {
     g_return_val_if_fail(source, nullptr);
     WebKitUserScript* userScript = static_cast<WebKitUserScript*>(fastMalloc(sizeof(WebKitUserScript)));
-    new (userScript) WebKitUserScript(source, injectedFrames, injectionTime, whitelist, blacklist);
+    new (userScript) WebKitUserScript(source, injectedFrames, injectionTime, whitelist, blacklist, API::UserContentWorld::normalWorld());
     return userScript;
 }
 
+/**
+ * webkit_user_script_new_for_world:
+ * @source: Source code of the user script.
+ * @injected_frames: A #WebKitUserContentInjectedFrames value
+ * @injection_time: A #WebKitUserScriptInjectionTime value
+ * @world_name: the name of a #WebKitScriptWorld
+ * @whitelist: (array zero-terminated=1) (allow-none): A whitelist of URI patterns or %NULL
+ * @blacklist: (array zero-terminated=1) (allow-none): A blacklist of URI patterns or %NULL
+ *
+ * Creates a new user script for script world with name @world_name.
+ * See webkit_user_script_new() for a full description.
+ *
+ * Returns: A new #WebKitUserScript
+ *
+ * Since: 2.22
+ */
+WebKitUserScript* webkit_user_script_new_for_world(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserScriptInjectionTime injectionTime, const char* worldName, const gchar* const* whitelist, const gchar* const* blacklist)
+{
+    g_return_val_if_fail(source, nullptr);
+    g_return_val_if_fail(worldName, nullptr);
+
+    WebKitUserScript* userScript = static_cast<WebKitUserScript*>(fastMalloc(sizeof(WebKitUserScript)));
+    new (userScript) WebKitUserScript(source, injectedFrames, injectionTime, whitelist, blacklist, webkitUserContentWorld(worldName));
+    return userScript;
+}
+
 API::UserScript& webkitUserScriptGetUserScript(WebKitUserScript* userScript)
 {
     return *userScript->userScript;

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/glib/WebKitUserContentManager.cpp (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/glib/WebKitUserContentManager.cpp	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/glib/WebKitUserContentManager.cpp	2018-08-24 08:10:58 UTC (rev 235310)
@@ -250,11 +250,11 @@
  * Unregisters a previously registered message handler.
  *
  * Note that this does *not* disconnect handlers for the
- * #WebKitUserContentManager::script-message-received signal,
+ * #WebKitUserContentManager::script-message-received signal;
  * they will be kept connected, but the signal will not be emitted
  * unless the handler name is registered again.
  *
- * See also webkit_user_content_manager_register_script_message_handler()
+ * See also webkit_user_content_manager_register_script_message_handler().
  *
  * Since: 2.8
  */
@@ -265,6 +265,59 @@
     manager->priv->userContentController->removeUserMessageHandlerForName(String::fromUTF8(name), API::UserContentWorld::normalWorld());
 }
 
+/**
+ * webkit_user_content_manager_register_script_message_handler_in_world:
+ * @manager: A #WebKitUserContentManager
+ * @name: Name of the script message channel
+ * @world_name: the name of a #WebKitScriptWorld
+ *
+ * Registers a new user script message handler in script world with name @world_name.
+ * See webkit_user_content_manager_register_script_message_handler() for full description.
+ *
+ * Registering a script message handler will fail if the requested
+ * name has been already registered before.
+ *
+ * Returns: %TRUE if message handler was registered successfully, or %FALSE otherwise.
+ *
+ * Since: 2.22
+ */
+gboolean webkit_user_content_manager_register_script_message_handler_in_world(WebKitUserContentManager* manager, const char* name, const char* worldName)
+{
+    g_return_val_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(manager), FALSE);
+    g_return_val_if_fail(name, FALSE);
+    g_return_val_if_fail(worldName, FALSE);
+
+    Ref<WebScriptMessageHandler> handler =
+        WebScriptMessageHandler::create(std::make_unique<ScriptMessageClientGtk>(manager, name), String::fromUTF8(name), webkitUserContentWorld(worldName));
+    return manager->priv->userContentController->addUserScriptMessageHandler(handler.get());
+}
+
+/**
+ * webkit_user_content_manager_unregister_script_message_handler_in_world:
+ * @manager: A #WebKitUserContentManager
+ * @name: Name of the script message channel
+ * @world_name: the name of a #WebKitScriptWorld
+ *
+ * Unregisters a previously registered message handler in script world with name @world_name.
+ *
+ * Note that this does *not* disconnect handlers for the
+ * #WebKitUserContentManager::script-message-received signal;
+ * they will be kept connected, but the signal will not be emitted
+ * unless the handler name is registered again.
+ *
+ * See also webkit_user_content_manager_register_script_message_handler_in_world().
+ *
+ * Since: 2.22
+ */
+void webkit_user_content_manager_unregister_script_message_handler_in_world(WebKitUserContentManager* manager, const char* name, const char* worldName)
+{
+    g_return_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(manager));
+    g_return_if_fail(name);
+    g_return_if_fail(worldName);
+
+    manager->priv->userContentController->removeUserMessageHandlerForName(String::fromUTF8(name), webkitUserContentWorld(worldName));
+}
+
 WebUserContentControllerProxy* webkitUserContentManagerGetUserContentControllerProxy(WebKitUserContentManager* manager)
 {
     return manager->priv->userContentController.get();

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/glib/WebKitUserContentPrivate.h (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/glib/WebKitUserContentPrivate.h	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/glib/WebKitUserContentPrivate.h	2018-08-24 08:10:58 UTC (rev 235310)
@@ -20,6 +20,7 @@
 #ifndef WebKitUserContentPrivate_h
 #define WebKitUserContentPrivate_h
 
+#include "APIUserContentWorld.h"
 #include "APIUserScript.h"
 #include "APIUserStyleSheet.h"
 #include "WebKitUserContent.h"
@@ -28,5 +29,6 @@
 
 API::UserScript& webkitUserScriptGetUserScript(WebKitUserScript*);
 API::UserStyleSheet& webkitUserStyleSheetGetUserStyleSheet(WebKitUserStyleSheet*);
+API::UserContentWorld& webkitUserContentWorld(const char*);
 
 #endif // WebKitUserContentPrivate_h

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/gtk/WebKitUserContent.h (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/gtk/WebKitUserContent.h	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/gtk/WebKitUserContent.h	2018-08-24 08:10:58 UTC (rev 235310)
@@ -69,21 +69,29 @@
 typedef struct _WebKitUserStyleSheet WebKitUserStyleSheet;
 
 WEBKIT_API GType
-webkit_user_style_sheet_get_type (void);
+webkit_user_style_sheet_get_type      (void);
 
 WEBKIT_API WebKitUserStyleSheet *
-webkit_user_style_sheet_ref      (WebKitUserStyleSheet           *user_style_sheet);
+webkit_user_style_sheet_ref           (WebKitUserStyleSheet           *user_style_sheet);
 
 WEBKIT_API void
-webkit_user_style_sheet_unref    (WebKitUserStyleSheet           *user_style_sheet);
+webkit_user_style_sheet_unref         (WebKitUserStyleSheet           *user_style_sheet);
 
 WEBKIT_API WebKitUserStyleSheet *
-webkit_user_style_sheet_new      (const gchar                    *source,
-                                  WebKitUserContentInjectedFrames injected_frames,
-                                  WebKitUserStyleLevel            level,
-                                  const gchar* const             *whitelist,
-                                  const gchar* const             *blacklist);
+webkit_user_style_sheet_new           (const gchar                    *source,
+                                       WebKitUserContentInjectedFrames injected_frames,
+                                       WebKitUserStyleLevel            level,
+                                       const gchar* const             *whitelist,
+                                       const gchar* const             *blacklist);
 
+WEBKIT_API WebKitUserStyleSheet *
+webkit_user_style_sheet_new_for_world (const gchar                    *source,
+                                       WebKitUserContentInjectedFrames injected_frames,
+                                       WebKitUserStyleLevel            level,
+                                       const gchar                    *world_name,
+                                       const gchar* const             *whitelist,
+                                       const gchar* const             *blacklist);
+
 /**
  * WebKitUserScriptInjectionTime:
  * @WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START: Insert the code of the user
@@ -120,6 +128,14 @@
                                   const gchar* const             *whitelist,
                                   const gchar* const             *blacklist);
 
+WEBKIT_API WebKitUserScript *
+webkit_user_script_new_for_world (const gchar                    *source,
+                                  WebKitUserContentInjectedFrames injected_frames,
+                                  WebKitUserScriptInjectionTime   injection_time,
+                                  const gchar                    *world_name,
+                                  const gchar* const             *whitelist,
+                                  const gchar* const             *blacklist);
+
 G_END_DECLS
 
 #endif

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/gtk/WebKitUserContentManager.h (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/gtk/WebKitUserContentManager.h	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/gtk/WebKitUserContentManager.h	2018-08-24 08:10:58 UTC (rev 235310)
@@ -58,33 +58,41 @@
     void (*_webkit_reserved3) (void);
 };
 
-
 WEBKIT_API GType
-webkit_user_content_manager_get_type                          (void);
+webkit_user_content_manager_get_type                                   (void);
 
 WEBKIT_API WebKitUserContentManager *
-webkit_user_content_manager_new                               (void);
+webkit_user_content_manager_new                                        (void);
 
 WEBKIT_API void
-webkit_user_content_manager_add_style_sheet                   (WebKitUserContentManager *manager,
-                                                               WebKitUserStyleSheet     *stylesheet);
+webkit_user_content_manager_add_style_sheet                            (WebKitUserContentManager *manager,
+                                                                        WebKitUserStyleSheet     *stylesheet);
 WEBKIT_API void
-webkit_user_content_manager_remove_all_style_sheets           (WebKitUserContentManager *manager);
+webkit_user_content_manager_remove_all_style_sheets                    (WebKitUserContentManager *manager);
 
 WEBKIT_API gboolean
-webkit_user_content_manager_register_script_message_handler   (WebKitUserContentManager *manager,
-                                                               const gchar              *name);
+webkit_user_content_manager_register_script_message_handler            (WebKitUserContentManager *manager,
+                                                                        const gchar              *name);
 WEBKIT_API void
-webkit_user_content_manager_unregister_script_message_handler (WebKitUserContentManager *manager,
-                                                               const gchar              *name);
+webkit_user_content_manager_unregister_script_message_handler          (WebKitUserContentManager *manager,
+                                                                        const gchar              *name);
 
+WEBKIT_API gboolean
+webkit_user_content_manager_register_script_message_handler_in_world   (WebKitUserContentManager *manager,
+                                                                        const gchar              *name,
+                                                                        const gchar              *world_name);
 WEBKIT_API void
-webkit_user_content_manager_add_script              (WebKitUserContentManager *manager,
-                                                     WebKitUserScript         *script);
+webkit_user_content_manager_unregister_script_message_handler_in_world (WebKitUserContentManager *manager,
+                                                                        const gchar              *name,
+                                                                        const gchar              *world_name);
 
 WEBKIT_API void
-webkit_user_content_manager_remove_all_scripts      (WebKitUserContentManager *manager);
+webkit_user_content_manager_add_script                                 (WebKitUserContentManager *manager,
+                                                                        WebKitUserScript         *script);
 
+WEBKIT_API void
+webkit_user_content_manager_remove_all_scripts                         (WebKitUserContentManager *manager);
+
 G_END_DECLS
 
 #endif

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt	2018-08-24 08:10:58 UTC (rev 235310)
@@ -96,9 +96,11 @@
 webkit_user_style_sheet_ref
 webkit_user_style_sheet_unref
 webkit_user_style_sheet_new
+webkit_user_style_sheet_new_for_world
 webkit_user_script_ref
 webkit_user_script_unref
 webkit_user_script_new
+webkit_user_script_new_for_world
 
 <SUBSECTION Standard>
 WEBKIT_TYPE_USER_STYLE_SHEET
@@ -120,6 +122,8 @@
 webkit_user_content_manager_remove_all_scripts
 webkit_user_content_manager_register_script_message_handler
 webkit_user_content_manager_unregister_script_message_handler
+webkit_user_content_manager_register_script_message_handler_in_world
+webkit_user_content_manager_unregister_script_message_handler_in_world
 
 <SUBSECTION Standard>
 WEBKIT_IS_USER_CONTENT_MANAGER

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/wpe/WebKitUserContent.h (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/wpe/WebKitUserContent.h	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/wpe/WebKitUserContent.h	2018-08-24 08:10:58 UTC (rev 235310)
@@ -69,21 +69,29 @@
 typedef struct _WebKitUserStyleSheet WebKitUserStyleSheet;
 
 WEBKIT_API GType
-webkit_user_style_sheet_get_type (void);
+webkit_user_style_sheet_get_type      (void);
 
 WEBKIT_API WebKitUserStyleSheet *
-webkit_user_style_sheet_ref      (WebKitUserStyleSheet           *user_style_sheet);
+webkit_user_style_sheet_ref           (WebKitUserStyleSheet           *user_style_sheet);
 
 WEBKIT_API void
-webkit_user_style_sheet_unref    (WebKitUserStyleSheet           *user_style_sheet);
+webkit_user_style_sheet_unref         (WebKitUserStyleSheet           *user_style_sheet);
 
 WEBKIT_API WebKitUserStyleSheet *
-webkit_user_style_sheet_new      (const gchar                    *source,
-                                  WebKitUserContentInjectedFrames injected_frames,
-                                  WebKitUserStyleLevel            level,
-                                  const gchar* const             *whitelist,
-                                  const gchar* const             *blacklist);
+webkit_user_style_sheet_new           (const gchar                    *source,
+                                       WebKitUserContentInjectedFrames injected_frames,
+                                       WebKitUserStyleLevel            level,
+                                       const gchar* const             *whitelist,
+                                       const gchar* const             *blacklist);
 
+WEBKIT_API WebKitUserStyleSheet *
+webkit_user_style_sheet_new_for_world (const gchar                    *source,
+                                       WebKitUserContentInjectedFrames injected_frames,
+                                       WebKitUserStyleLevel            level,
+                                       const gchar                    *world_name,
+                                       const gchar* const             *whitelist,
+                                       const gchar* const             *blacklist);
+
 /**
  * WebKitUserScriptInjectionTime:
  * @WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START: Insert the code of the user
@@ -120,6 +128,14 @@
                                   const gchar* const             *whitelist,
                                   const gchar* const             *blacklist);
 
+WEBKIT_API WebKitUserScript *
+webkit_user_script_new_for_world (const gchar                    *source,
+                                  WebKitUserContentInjectedFrames injected_frames,
+                                  WebKitUserScriptInjectionTime   injection_time,
+                                  const gchar                    *world_name,
+                                  const gchar* const             *whitelist,
+                                  const gchar* const             *blacklist);
+
 G_END_DECLS
 
 #endif

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/wpe/WebKitUserContentManager.h (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/wpe/WebKitUserContentManager.h	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/UIProcess/API/wpe/WebKitUserContentManager.h	2018-08-24 08:10:58 UTC (rev 235310)
@@ -58,33 +58,41 @@
     void (*_webkit_reserved3) (void);
 };
 
-
 WEBKIT_API GType
-webkit_user_content_manager_get_type                          (void);
+webkit_user_content_manager_get_type                                   (void);
 
 WEBKIT_API WebKitUserContentManager *
-webkit_user_content_manager_new                               (void);
+webkit_user_content_manager_new                                        (void);
 
 WEBKIT_API void
-webkit_user_content_manager_add_style_sheet                   (WebKitUserContentManager *manager,
-                                                               WebKitUserStyleSheet     *stylesheet);
+webkit_user_content_manager_add_style_sheet                            (WebKitUserContentManager *manager,
+                                                                        WebKitUserStyleSheet     *stylesheet);
 WEBKIT_API void
-webkit_user_content_manager_remove_all_style_sheets           (WebKitUserContentManager *manager);
+webkit_user_content_manager_remove_all_style_sheets                    (WebKitUserContentManager *manager);
 
 WEBKIT_API gboolean
-webkit_user_content_manager_register_script_message_handler   (WebKitUserContentManager *manager,
-                                                               const gchar              *name);
+webkit_user_content_manager_register_script_message_handler            (WebKitUserContentManager *manager,
+                                                                        const gchar              *name);
 WEBKIT_API void
-webkit_user_content_manager_unregister_script_message_handler (WebKitUserContentManager *manager,
-                                                               const gchar              *name);
+webkit_user_content_manager_unregister_script_message_handler          (WebKitUserContentManager *manager,
+                                                                        const gchar              *name);
 
+WEBKIT_API gboolean
+webkit_user_content_manager_register_script_message_handler_in_world   (WebKitUserContentManager *manager,
+                                                                        const gchar              *name,
+                                                                        const gchar              *world_name);
 WEBKIT_API void
-webkit_user_content_manager_add_script              (WebKitUserContentManager *manager,
-                                                     WebKitUserScript         *script);
+webkit_user_content_manager_unregister_script_message_handler_in_world (WebKitUserContentManager *manager,
+                                                                        const gchar              *name,
+                                                                        const gchar              *world_name);
 
 WEBKIT_API void
-webkit_user_content_manager_remove_all_scripts      (WebKitUserContentManager *manager);
+webkit_user_content_manager_add_script                                 (WebKitUserContentManager *manager,
+                                                                        WebKitUserScript         *script);
 
+WEBKIT_API void
+webkit_user_content_manager_remove_all_scripts                         (WebKitUserContentManager *manager);
+
 G_END_DECLS
 
 #endif

Modified: releases/WebKitGTK/webkit-2.22/Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp	2018-08-24 08:10:58 UTC (rev 235310)
@@ -101,7 +101,16 @@
         ASSERT(world.first);
         ASSERT(world.first != 1);
 
-        worldMap().ensure(world.first, [&] { return std::make_pair(InjectedBundleScriptWorld::create(world.second), 1); });
+        worldMap().ensure(world.first, [&] {
+#if PLATFORM(GTK) || PLATFORM(WPE)
+            // The GLib API doesn't allow to create script worlds from the UI process. We need to
+            // use the existing world created by the web extension if any. The world name is used
+            // as the identifier.
+            if (auto* existingWorld = InjectedBundleScriptWorld::find(world.second))
+                return std::make_pair(Ref<InjectedBundleScriptWorld>(*existingWorld), 1);
+#endif
+            return std::make_pair(InjectedBundleScriptWorld::create(world.second), 1);
+        });
     }
 }
 

Modified: releases/WebKitGTK/webkit-2.22/Tools/ChangeLog (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Tools/ChangeLog	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Tools/ChangeLog	2018-08-24 08:10:58 UTC (rev 235310)
@@ -1,3 +1,23 @@
+2018-08-23  Carlos Garcia Campos  <cgar...@igalia.com>
+
+        [GTK][WPE] Add API to inject/register user content in isolated worlds
+        https://bugs.webkit.org/show_bug.cgi?id=188883
+
+        Reviewed by Michael Catanzaro.
+
+        Add test cases for the new API.
+
+        * TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentManager.cpp:
+        (isStyleSheetInjectedForURLAtPath):
+        (isScriptInjectedForURLAtPath):
+        (testUserContentManagerInjectedStyleSheet):
+        (testUserContentManagerInjectedScript):
+        (UserScriptMessageTest::registerHandler):
+        (UserScriptMessageTest::unregisterHandler):
+        (UserScriptMessageTest::postMessageAndWaitUntilReceived):
+        (testUserContentManagerScriptMessageInWorldReceived):
+        (beforeAll):
+
 2018-08-21  Alex Christensen  <achristen...@webkit.org>
 
         Transition ResizeReversePaginatedWebView API test from WKPageLoaderClient to WKPageNavigationClient

Modified: releases/WebKitGTK/webkit-2.22/Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentManager.cpp (235309 => 235310)


--- releases/WebKitGTK/webkit-2.22/Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentManager.cpp	2018-08-24 08:10:48 UTC (rev 235309)
+++ releases/WebKitGTK/webkit-2.22/Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentManager.cpp	2018-08-24 08:10:58 UTC (rev 235310)
@@ -47,13 +47,14 @@
     g_assert(webkit_web_view_get_user_content_manager(webView2.get()) != userContentManager1.get());
 }
 
-static bool isStyleSheetInjectedForURLAtPath(WebViewTest* test, const char* path)
+static bool isStyleSheetInjectedForURLAtPath(WebViewTest* test, const char* path, const char* world = nullptr)
 {
     test->loadURI(kServer->getURIForPath(path).data());
     test->waitUntilLoadFinished();
 
     GUniqueOutPtr<GError> error;
-    WebKitJavascriptResult* _javascript_Result = test->runJavaScriptAndWaitUntilFinished(kStyleSheetTestScript, &error.outPtr());
+    WebKitJavascriptResult* _javascript_Result = world ? test->runJavaScriptInWorldAndWaitUntilFinished(kStyleSheetTestScript, world, &error.outPtr())
+        : test->runJavaScriptAndWaitUntilFinished(kStyleSheetTestScript, &error.outPtr());
     g_assert(_javascript_Result);
     g_assert(!error.get());
 
@@ -61,13 +62,15 @@
     return !g_strcmp0(resultString.get(), kStyleSheetTestScriptResult);
 }
 
-static bool isScriptInjectedForURLAtPath(WebViewTest* test, const char* path)
+static bool isScriptInjectedForURLAtPath(WebViewTest* test, const char* path, const char* world = nullptr)
 {
     test->loadURI(kServer->getURIForPath(path).data());
     test->waitUntilLoadFinished();
 
     GUniqueOutPtr<GError> error;
-    if (WebKitJavascriptResult* _javascript_Result = test->runJavaScriptAndWaitUntilFinished(kScriptTestScript, &error.outPtr())) {
+    WebKitJavascriptResult* _javascript_Result = world ? test->runJavaScriptInWorldAndWaitUntilFinished(kScriptTestScript, world, &error.outPtr())
+        : test->runJavaScriptAndWaitUntilFinished(kScriptTestScript, &error.outPtr());
+    if (_javascript_Result) {
         g_assert(!error.get());
 
         GUniquePtr<char> resultString(WebViewTest::_javascript_ResultToCString(_javascript_Result));
@@ -125,6 +128,14 @@
 
     removeOldInjectedContentAndResetLists(test->m_userContentManager.get(), whitelist, blacklist);
 
+    g_assert(!isStyleSheetInjectedForURLAtPath(test, randomPath, "WebExtensionTestScriptWorld"));
+    styleSheet = webkit_user_style_sheet_new_for_world(kInjectedStyleSheet, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, "WebExtensionTestScriptWorld", nullptr, nullptr);
+    webkit_user_content_manager_add_style_sheet(test->m_userContentManager.get(), styleSheet);
+    webkit_user_style_sheet_unref(styleSheet);
+    g_assert(isStyleSheetInjectedForURLAtPath(test, randomPath, "WebExtensionTestScriptWorld"));
+
+    removeOldInjectedContentAndResetLists(test->m_userContentManager.get(), whitelist, blacklist);
+
     fillURLListFromPaths(blacklist, randomPath, 0);
     styleSheet = webkit_user_style_sheet_new(kInjectedStyleSheet, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, nullptr, blacklist);
     webkit_user_content_manager_add_style_sheet(test->m_userContentManager.get(), styleSheet);
@@ -168,6 +179,14 @@
 
     removeOldInjectedContentAndResetLists(test->m_userContentManager.get(), whitelist, blacklist);
 
+    g_assert(!isScriptInjectedForURLAtPath(test, randomPath, "WebExtensionTestScriptWorld"));
+    script = webkit_user_script_new_for_world(kInjectedScript, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END, "WebExtensionTestScriptWorld", nullptr, nullptr);
+    webkit_user_content_manager_add_script(test->m_userContentManager.get(), script);
+    webkit_user_script_unref(script);
+    g_assert(isScriptInjectedForURLAtPath(test, randomPath, "WebExtensionTestScriptWorld"));
+
+    removeOldInjectedContentAndResetLists(test->m_userContentManager.get(), whitelist, blacklist);
+
     fillURLListFromPaths(blacklist, randomPath, 0);
     script = webkit_user_script_new(kInjectedScript, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END, nullptr, blacklist);
     webkit_user_content_manager_add_script(test->m_userContentManager.get(), script);
@@ -209,14 +228,16 @@
             webkit_javascript_result_unref(m_userScriptMessage);
     }
 
-    bool registerHandler(const char* handlerName)
+    bool registerHandler(const char* handlerName, const char* worldName = nullptr)
     {
-        return webkit_user_content_manager_register_script_message_handler(m_userContentManager.get(), handlerName);
+        return worldName ? webkit_user_content_manager_register_script_message_handler_in_world(m_userContentManager.get(), handlerName, worldName)
+            : webkit_user_content_manager_register_script_message_handler(m_userContentManager.get(), handlerName);
     }
 
-    void unregisterHandler(const char* handlerName)
+    void unregisterHandler(const char* handlerName, const char* worldName = nullptr)
     {
-        webkit_user_content_manager_unregister_script_message_handler(m_userContentManager.get(), handlerName);
+        return worldName ? webkit_user_content_manager_unregister_script_message_handler_in_world(m_userContentManager.get(), handlerName, worldName)
+            : webkit_user_content_manager_unregister_script_message_handler(m_userContentManager.get(), handlerName);
     }
 
     static void scriptMessageReceived(WebKitUserContentManager* userContentManager, WebKitJavascriptResult* jsResult, UserScriptMessageTest* test)
@@ -252,11 +273,14 @@
         g_main_loop_quit(test->m_mainLoop);
     }
 
-    WebKitJavascriptResult* postMessageAndWaitUntilReceived(const char* handlerName, const char* _javascript_ValueAsText)
+    WebKitJavascriptResult* postMessageAndWaitUntilReceived(const char* handlerName, const char* _javascript_ValueAsText, const char* worldName = nullptr)
     {
         GUniquePtr<char> _javascript_Snippet(g_strdup_printf("window.webkit.messageHandlers.%s.postMessage(%s);", handlerName, _javascript_ValueAsText));
         m_waitForScriptRun = true;
-        webkit_web_view_run_javascript(m_webView, _javascript_Snippet.get(), nullptr, reinterpret_cast<GAsyncReadyCallback>(runJavaScriptFinished), this);
+        if (worldName)
+            webkit_web_view_run_javascript_in_world(m_webView, _javascript_Snippet.get(), worldName, nullptr, reinterpret_cast<GAsyncReadyCallback>(runJavaScriptFinished), this);
+        else
+            webkit_web_view_run_javascript(m_webView, _javascript_Snippet.get(), nullptr, reinterpret_cast<GAsyncReadyCallback>(runJavaScriptFinished), this);
         return waitUntilMessageReceived(handlerName);
     }
 
@@ -331,6 +355,40 @@
     test->unregisterHandler("anotherHandler");
 }
 
+static void testUserContentManagerScriptMessageInWorldReceived(UserScriptMessageTest* test, gconstpointer)
+{
+    g_assert(test->registerHandler("msg"));
+
+    test->loadHtml("<html></html>", nullptr);
+    test->waitUntilLoadFinished();
+
+    // Check that the "window.webkit.messageHandlers" namespace doesn't exist in isolated worlds.
+    GUniqueOutPtr<GError> error;
+    WebKitJavascriptResult* _javascript_Result = test->runJavaScriptInWorldAndWaitUntilFinished("window.webkit.messageHandlers ? 'y' : 'n';", "WebExtensionTestScriptWorld", &error.outPtr());
+    g_assert(!_javascript_Result);
+    g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
+    test->unregisterHandler("msg");
+
+    g_assert(test->registerHandler("msg", "WebExtensionTestScriptWorld"));
+
+    // Check that the "window.webkit.messageHandlers" namespace exists in the world.
+    _javascript_Result = test->runJavaScriptInWorldAndWaitUntilFinished("window.webkit.messageHandlers ? 'y' : 'n';", "WebExtensionTestScriptWorld", &error.outPtr());
+    g_assert(_javascript_Result);
+    g_assert(!error.get());
+    GUniquePtr<char> valueString(WebViewTest::_javascript_ResultToCString(_javascript_Result));
+    g_assert_cmpstr(valueString.get(), ==, "y");
+
+    valueString.reset(WebViewTest::_javascript_ResultToCString(test->postMessageAndWaitUntilReceived("msg", "'user message'", "WebExtensionTestScriptWorld")));
+    g_assert_cmpstr(valueString.get(), ==, "user message");
+
+    // Post message in main world should fail.
+    _javascript_Result = test->runJavaScriptAndWaitUntilFinished("window.webkit.messageHandlers.msg.postMessage('42');", &error.outPtr());
+    g_assert(!_javascript_Result);
+    g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
+
+    test->unregisterHandler("msg", "WebExtensionTestScriptWorld");
+}
+
 #if PLATFORM(GTK)
 static void testUserContentManagerScriptMessageFromDOMBindings(UserScriptMessageTest* test, gconstpointer)
 {
@@ -362,6 +420,7 @@
     WebViewTest::add("WebKitUserContentManager", "injected-style-sheet", testUserContentManagerInjectedStyleSheet);
     WebViewTest::add("WebKitUserContentManager", "injected-script", testUserContentManagerInjectedScript);
     UserScriptMessageTest::add("WebKitUserContentManager", "script-message-received", testUserContentManagerScriptMessageReceived);
+    UserScriptMessageTest::add("WebKitUserContentManager", "script-message-in-world-received", testUserContentManagerScriptMessageInWorldReceived);
 #if PLATFORM(GTK)
     UserScriptMessageTest::add("WebKitUserContentManager", "script-message-from-dom-bindings", testUserContentManagerScriptMessageFromDOMBindings);
 #endif
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to