<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[172147] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/172147">172147</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2014-08-06 08:07:08 -0700 (Wed, 06 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>[GTK] Add support for user scripts to WebKitUserContentManager
https://bugs.webkit.org/show_bug.cgi?id=134738

Patch by Adrian Perez de Castro &lt;aperez@igalia.com&gt; on 2014-08-06
Reviewed by Carlos Garcia Campos.

Add support for user scripts, to complement the user style sheet
support already present in WebKitUserContentManager. Most of the
moving parts are already present, so this just adds a boxed type
for user scripts (WebKitUserScript) and the corresponding methods
to add and remove scripts from the WebKitUserContentManager.

Source/WebKit2:

* UIProcess/API/gtk/WebKitUserContent.cpp: Add a WebKitUserScript
boxed type and its corresponding methods and enums.
(toUserScriptInjectionTime): Needed to convert
WebKitUserScriptInjectionTime values into its WebCore counterparts.
(_WebKitUserScript::_WebKitUserScript): Added.
(_WebKitUserScript::referenceCount): Ditto.
(webkit_user_script_ref):
(webkit_user_script_unref):
(webkit_user_script_new):
(webkitUserScriptGetUserScript): Internal method to obtain the
boxed WebCore::UserScript value.
* UIProcess/API/gtk/WebKitUserContent.h: Add the new public API
methods.
* UIProcess/API/gtk/WebKitUserContentManager.cpp: Implement the
methods for adding and removing user scripts.
(webkit_user_content_manager_add_script):
(webkit_user_content_manager_remove_all_scripts):
* UIProcess/API/gtk/WebKitUserContentManager.h: Add the new public
API methods.
* UIProcess/API/gtk/WebKitUserContentPrivate.h: Add the definition
for the new private function.
* UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Include the
new public methods in the API documentation.

Tools:

* TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitUserContentManager.cpp:
Add test case for injected user scripts.
(isScriptInjectedForURLAtPath):
(removeOldInjectedContentAndResetLists):
(testUserContentManagerInjectedStyleSheet):
(testUserContentManagerInjectedScript):
(beforeAll):
(removeOldInjectedStyleSheetsAndResetLists): Deleted.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPIgtkWebKitUserContentcpp">trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContent.cpp</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPIgtkWebKitUserContenth">trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContent.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPIgtkWebKitUserContentManagercpp">trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.cpp</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPIgtkWebKitUserContentManagerh">trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPIgtkWebKitUserContentPrivateh">trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentPrivate.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessAPIgtkdocswebkit2gtksectionstxt">trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKit2GtkTestWebKitUserContentManagercpp">trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitUserContentManager.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (172146 => 172147)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2014-08-06 15:06:08 UTC (rev 172146)
+++ trunk/Source/WebKit2/ChangeLog        2014-08-06 15:07:08 UTC (rev 172147)
</span><span class="lines">@@ -1,3 +1,40 @@
</span><ins>+2014-08-06  Adrian Perez de Castro  &lt;aperez@igalia.com&gt;
+
+        [GTK] Add support for user scripts to WebKitUserContentManager
+        https://bugs.webkit.org/show_bug.cgi?id=134738
+
+        Reviewed by Carlos Garcia Campos.
+
+        Add support for user scripts, to complement the user style sheet
+        support already present in WebKitUserContentManager. Most of the
+        moving parts are already present, so this just adds a boxed type
+        for user scripts (WebKitUserScript) and the corresponding methods
+        to add and remove scripts from the WebKitUserContentManager.
+
+        * UIProcess/API/gtk/WebKitUserContent.cpp: Add a WebKitUserScript
+        boxed type and its corresponding methods and enums.
+        (toUserScriptInjectionTime): Needed to convert
+        WebKitUserScriptInjectionTime values into its WebCore counterparts.
+        (_WebKitUserScript::_WebKitUserScript): Added.
+        (_WebKitUserScript::referenceCount): Ditto.
+        (webkit_user_script_ref):
+        (webkit_user_script_unref):
+        (webkit_user_script_new):
+        (webkitUserScriptGetUserScript): Internal method to obtain the
+        boxed WebCore::UserScript value.
+        * UIProcess/API/gtk/WebKitUserContent.h: Add the new public API
+        methods.
+        * UIProcess/API/gtk/WebKitUserContentManager.cpp: Implement the
+        methods for adding and removing user scripts.
+        (webkit_user_content_manager_add_script):
+        (webkit_user_content_manager_remove_all_scripts):
+        * UIProcess/API/gtk/WebKitUserContentManager.h: Add the new public
+        API methods.
+        * UIProcess/API/gtk/WebKitUserContentPrivate.h: Add the definition
+        for the new private function.
+        * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Include the
+        new public methods in the API documentation.
+
</ins><span class="cx"> 2014-08-06  Carlos Garcia Campos  &lt;cgarcia@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [GTK] Be able to disable gtk2 dependency
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPIgtkWebKitUserContentcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContent.cpp (172146 => 172147)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContent.cpp        2014-08-06 15:06:08 UTC (rev 172146)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContent.cpp        2014-08-06 15:07:08 UTC (rev 172147)
</span><span class="lines">@@ -63,6 +63,19 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static inline UserScriptInjectionTime toUserScriptInjectionTime(WebKitUserScriptInjectionTime injectionTime)
+{
+    switch (injectionTime) {
+    case WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START:
+        return InjectAtDocumentStart;
+    case WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END:
+        return InjectAtDocumentEnd;
+    default:
+        ASSERT_NOT_REACHED();
+        return InjectAtDocumentStart;
+    }
+}
+
</ins><span class="cx"> static inline Vector&lt;String&gt; toStringVector(const char* const* strv)
</span><span class="cx"> {
</span><span class="cx">     if (!strv)
</span><span class="lines">@@ -159,3 +172,89 @@
</span><span class="cx"> {
</span><span class="cx">     return *userStyleSheet-&gt;userStyleSheet;
</span><span class="cx"> }
</span><ins>+
+struct _WebKitUserScript {
+    _WebKitUserScript(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserScriptInjectionTime injectionTime, const gchar* const* whitelist, const gchar* const* blacklist)
+        : userScript(std::make_unique&lt;UserScript&gt;(
+            String::fromUTF8(source), URL { },
+            toStringVector(whitelist), toStringVector(blacklist),
+            toUserScriptInjectionTime(injectionTime),
+            toUserContentInjectedFrames(injectedFrames)))
+        , referenceCount(1)
+    {
+    }
+
+    std::unique_ptr&lt;UserScript&gt; userScript;
+    int referenceCount;
+};
+
+G_DEFINE_BOXED_TYPE(WebKitUserScript, webkit_user_script, webkit_user_script_ref, webkit_user_script_unref)
+
+/**
+ * webkit_user_script_ref:
+ * @user_script: a #WebKitUserScript
+ *
+ * Atomically increments the reference count of @user_script by one.
+ * This function is MT-safe and may be called from any thread.
+ *
+ * Returns: The passed #WebKitUserScript
+ *
+ * Since: 2.6
+ */
+WebKitUserScript* webkit_user_script_ref(WebKitUserScript* userScript)
+{
+    g_atomic_int_inc(&amp;userScript-&gt;referenceCount);
+    return userScript;
+}
+
+/**
+ * webkit_user_script_unref:
+ * @user_script: a #WebKitUserScript
+ *
+ * Atomically decrements the reference count of @user_script by one.
+ * If the reference count drops to 0, all memory allocated by
+ * #WebKitUserScript is released. This function is MT-safe and may be called
+ * from any thread.
+ *
+ * Since: 2.6
+ */
+void webkit_user_script_unref(WebKitUserScript* userScript)
+{
+    if (g_atomic_int_dec_and_test(&amp;userScript-&gt;referenceCount)) {
+        userScript-&gt;~WebKitUserScript();
+        g_slice_free(WebKitUserScript, userScript);
+    }
+}
+
+/**
+ * webkit_user_script_new:
+ * @source: Source code of the user script.
+ * @injected_frames: A #WebKitUserContentInjectedFrames value
+ * @injection_time: A #WebKitUserScriptInjectionTime value
+ * @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. Scripts can be applied to some URIs
+ * only by passing non-null values for @whitelist or @blacklist. Passing a
+ * %NULL whitelist implies that all URIs are on the whitelist. The script
+ * is applied if an URI matches the whitelist and not the blacklist.
+ * URI patterns must be of the form `[protocol]://[host]/[path]`, where the
+ * *host* and *path* components can contain the wildcard character (`*`) to
+ * represent zero or more other characters.
+ *
+ * Returns: A new #WebKitScript
+ *
+ * Since: 2.6
+ */
+WebKitUserScript* webkit_user_script_new(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserScriptInjectionTime injectionTime, const gchar* const* whitelist, const gchar* const* blacklist)
+{
+    g_return_val_if_fail(source, nullptr);
+    WebKitUserScript* userScript = g_slice_new(WebKitUserScript);
+    new (userScript) WebKitUserScript(source, injectedFrames, injectionTime, whitelist, blacklist);
+    return userScript;
+}
+
+const UserScript&amp; webkitUserScriptGetUserScript(WebKitUserScript* userScript)
+{
+    return *userScript-&gt;userScript;
+}
</ins></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPIgtkWebKitUserContenth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContent.h (172146 => 172147)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContent.h        2014-08-06 15:06:08 UTC (rev 172146)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContent.h        2014-08-06 15:07:08 UTC (rev 172147)
</span><span class="lines">@@ -84,6 +84,42 @@
</span><span class="cx">                                   const gchar* const             *whitelist,
</span><span class="cx">                                   const gchar* const             *blacklist);
</span><span class="cx"> 
</span><ins>+/**
+ * WebKitUserScriptInjectionTime:
+ * @WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START: Insert the code of the user
+ *   script at the beginning of loaded documents. This is the default.
+ * @WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END: Insert the code of the user
+ *   script at the end of the loaded documents.
+ *
+ * Specifies at which place of documents an user script will be inserted.
+ *
+ * Since: 2.6
+ */
+typedef enum {
+    WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
+    WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END,
+} WebKitUserScriptInjectionTime;
+
+#define WEBKIT_TYPE_USER_SCRIPT (webkit_user_script_get_type())
+
+typedef struct _WebKitUserScript WebKitUserScript;
+
+WEBKIT_API GType
+webkit_user_script_get_type      (void);
+
+WEBKIT_API WebKitUserScript *
+webkit_user_script_ref           (WebKitUserScript               *user_script);
+
+WEBKIT_API void
+webkit_user_script_unref         (WebKitUserScript               *user_script);
+
+WEBKIT_API WebKitUserScript *
+webkit_user_script_new           (const gchar                    *source,
+                                  WebKitUserContentInjectedFrames injected_frames,
+                                  WebKitUserScriptInjectionTime   injection_time,
+                                  const gchar* const             *whitelist,
+                                  const gchar* const             *blacklist);
+
</ins><span class="cx"> G_END_DECLS
</span><span class="cx"> 
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPIgtkWebKitUserContentManagercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.cpp (172146 => 172147)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.cpp        2014-08-06 15:06:08 UTC (rev 172146)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.cpp        2014-08-06 15:07:08 UTC (rev 172147)
</span><span class="lines">@@ -109,6 +109,38 @@
</span><span class="cx">     manager-&gt;priv-&gt;userContentController-&gt;removeAllUserStyleSheets();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * webkit_user_content_manager_add_script:
+ * @manager: A #WebKitUserContentManager
+ * @script: A #WebKitUserScript
+ *
+ * Adds a #WebKitUserScript to the given #WebKitUserContentManager.
+ * The same #WebKitUserScript can be reused with multiple
+ * #WebKitUserContentManager instances.
+ *
+ * Since: 2.6
+ */
+void webkit_user_content_manager_add_script(WebKitUserContentManager* manager, WebKitUserScript* script)
+{
+    g_return_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(manager));
+    g_return_if_fail(script);
+    manager-&gt;priv-&gt;userContentController-&gt;addUserScript(webkitUserScriptGetUserScript(script));
+}
+
+/**
+ * webkit_user_content_manager_remove_all_scripts:
+ * @manager: A #WebKitUserContentManager
+ *
+ * Removes all user scripts from the given #WebKitUserContentManager
+ *
+ * Since: 2.6
+ */
+void webkit_user_content_manager_remove_all_scripts(WebKitUserContentManager* manager)
+{
+    g_return_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(manager));
+    manager-&gt;priv-&gt;userContentController-&gt;removeAllUserScripts();
+}
+
</ins><span class="cx"> WebUserContentControllerProxy* webkitUserContentManagerGetUserContentControllerProxy(WebKitUserContentManager* manager)
</span><span class="cx"> {
</span><span class="cx">     return manager-&gt;priv-&gt;userContentController.get();
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPIgtkWebKitUserContentManagerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.h (172146 => 172147)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.h        2014-08-06 15:06:08 UTC (rev 172146)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentManager.h        2014-08-06 15:07:08 UTC (rev 172147)
</span><span class="lines">@@ -71,6 +71,13 @@
</span><span class="cx"> WEBKIT_API void
</span><span class="cx"> webkit_user_content_manager_remove_all_style_sheets (WebKitUserContentManager *manager);
</span><span class="cx"> 
</span><ins>+WEBKIT_API void
+webkit_user_content_manager_add_script              (WebKitUserContentManager *manager,
+                                                     WebKitUserScript         *script);
+
+WEBKIT_API void
+webkit_user_content_manager_remove_all_scripts      (WebKitUserContentManager *manager);
+
</ins><span class="cx"> G_END_DECLS
</span><span class="cx"> 
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPIgtkWebKitUserContentPrivateh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentPrivate.h (172146 => 172147)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentPrivate.h        2014-08-06 15:06:08 UTC (rev 172146)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitUserContentPrivate.h        2014-08-06 15:07:08 UTC (rev 172147)
</span><span class="lines">@@ -21,8 +21,10 @@
</span><span class="cx"> #define WebKitUserContentPrivate_h
</span><span class="cx"> 
</span><span class="cx"> #include &quot;WebKitUserContent.h&quot;
</span><ins>+#include &lt;WebCore/UserScript.h&gt;
</ins><span class="cx"> #include &lt;WebCore/UserStyleSheet.h&gt;
</span><span class="cx"> 
</span><ins>+const WebCore::UserScript&amp; webkitUserScriptGetUserScript(WebKitUserScript*);
</ins><span class="cx"> const WebCore::UserStyleSheet&amp; webkitUserStyleSheetGetUserStyleSheet(WebKitUserStyleSheet*);
</span><span class="cx"> 
</span><span class="cx"> #endif // WebKitUserContentPrivate_h
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessAPIgtkdocswebkit2gtksectionstxt"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt (172146 => 172147)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt        2014-08-06 15:06:08 UTC (rev 172146)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt        2014-08-06 15:07:08 UTC (rev 172147)
</span><span class="lines">@@ -76,17 +76,24 @@
</span><span class="cx"> &lt;FILE&gt;WebKitUserContent&lt;/FILE&gt;
</span><span class="cx"> &lt;TITLE&gt;WebKitUserContent&lt;/TITLE&gt;
</span><span class="cx"> WebKitUserStyleSheet
</span><ins>+WebKitUserScript
</ins><span class="cx"> WebKitUserContentInjectedFrames
</span><span class="cx"> WebKitUserStyleLevel
</span><ins>+WebKitUserScriptInjectionTime
</ins><span class="cx"> webkit_user_style_sheet_ref
</span><span class="cx"> webkit_user_style_sheet_unref
</span><span class="cx"> webkit_user_style_sheet_new
</span><ins>+webkit_user_script_ref
+webkit_user_script_unref
+webkit_user_script_new
</ins><span class="cx"> 
</span><span class="cx"> &lt;SUBSECTION Standard&gt;
</span><span class="cx"> WEBKIT_TYPE_USER_STYLE_SHEET
</span><ins>+WEBKIT_TYPE_USER_SCRIPT
</ins><span class="cx"> 
</span><span class="cx"> &lt;SUBSECTION Private&gt;
</span><span class="cx"> webkit_user_style_sheet_get_type
</span><ins>+webkit_user_script_get_type
</ins><span class="cx"> &lt;/SECTION&gt;
</span><span class="cx"> 
</span><span class="cx"> &lt;SECTION&gt;
</span><span class="lines">@@ -96,6 +103,8 @@
</span><span class="cx"> webkit_user_content_manager_new
</span><span class="cx"> webkit_user_content_manager_add_style_sheet
</span><span class="cx"> webkit_user_content_manager_remove_all_style_sheets
</span><ins>+webkit_user_content_manager_add_script
+webkit_user_content_manager_remove_all_scripts
</ins><span class="cx"> 
</span><span class="cx"> &lt;SUBSECTION Standard&gt;
</span><span class="cx"> WEBKIT_IS_USER_CONTENT_MANAGER
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (172146 => 172147)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog        2014-08-06 15:06:08 UTC (rev 172146)
+++ trunk/Tools/ChangeLog        2014-08-06 15:07:08 UTC (rev 172147)
</span><span class="lines">@@ -1,3 +1,25 @@
</span><ins>+2014-08-06  Adrian Perez de Castro  &lt;aperez@igalia.com&gt;
+
+        [GTK] Add support for user scripts to WebKitUserContentManager
+        https://bugs.webkit.org/show_bug.cgi?id=134738
+
+        Reviewed by Carlos Garcia Campos.
+
+        Add support for user scripts, to complement the user style sheet
+        support already present in WebKitUserContentManager. Most of the
+        moving parts are already present, so this just adds a boxed type
+        for user scripts (WebKitUserScript) and the corresponding methods
+        to add and remove scripts from the WebKitUserContentManager.
+
+        * TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitUserContentManager.cpp:
+        Add test case for injected user scripts.
+        (isScriptInjectedForURLAtPath):
+        (removeOldInjectedContentAndResetLists):
+        (testUserContentManagerInjectedStyleSheet):
+        (testUserContentManagerInjectedScript):
+        (beforeAll):
+        (removeOldInjectedStyleSheetsAndResetLists): Deleted.
+
</ins><span class="cx"> 2014-08-06  Philippe Normand  &lt;pnormand@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [GTK] run-launcher --gtk still fails
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKit2GtkTestWebKitUserContentManagercpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitUserContentManager.cpp (172146 => 172147)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitUserContentManager.cpp        2014-08-06 15:06:08 UTC (rev 172146)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitUserContentManager.cpp        2014-08-06 15:07:08 UTC (rev 172147)
</span><span class="lines">@@ -50,6 +50,9 @@
</span><span class="cx"> static const char* kInjectedStyleSheet = &quot;#styledElement { font-weight: bold; }&quot;;
</span><span class="cx"> static const char* kStyleSheetTestScript = &quot;getComputedStyle(document.getElementById('styledElement'))['font-weight']&quot;;
</span><span class="cx"> static const char* kStyleSheetTestScriptResult = &quot;bold&quot;;
</span><ins>+static const char* kInjectedScript = &quot;document.write('&lt;div id=\&quot;item\&quot;&gt;Generated by a script&lt;/div&gt;')&quot;;
+static const char* kScriptTestScript = &quot;getElementById('item').innerText&quot;;
+static const char* kScriptTestScriptResult = &quot;Generated by a script&quot;;
</ins><span class="cx"> 
</span><span class="cx"> static void testWebViewNewWithUserContentManager(Test* test, gconstpointer)
</span><span class="cx"> {
</span><span class="lines">@@ -76,6 +79,20 @@
</span><span class="cx">     return !g_strcmp0(resultString.get(), kStyleSheetTestScriptResult);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static bool isScriptInjectedForURLAtPath(WebViewTest* test, const char* path)
+{
+    test-&gt;loadURI(kServer-&gt;getURIForPath(path).data());
+    test-&gt;waitUntilLoadFinished();
+
+    GUniqueOutPtr&lt;GError&gt; error;
+    WebKitJavascriptResult* javascriptResult = test-&gt;runJavaScriptAndWaitUntilFinished(kScriptTestScript, &amp;error.outPtr());
+    g_assert(javascriptResult);
+    g_assert(!error.get());
+
+    GUniquePtr&lt;char&gt; resultString(WebViewTest::javascriptResultToCString(javascriptResult));
+    return !g_strcmp0(resultString.get(), kScriptTestScriptResult);
+}
+
</ins><span class="cx"> static void fillURLListFromPaths(char** list, const char* path, ...)
</span><span class="cx"> {
</span><span class="cx">     va_list argumentList;
</span><span class="lines">@@ -90,9 +107,10 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static void removeOldInjectedStyleSheetsAndResetLists(WebKitUserContentManager* userContentManager, char** whitelist, char** blacklist)
</del><ins>+static void removeOldInjectedContentAndResetLists(WebKitUserContentManager* userContentManager, char** whitelist, char** blacklist)
</ins><span class="cx"> {
</span><span class="cx">     webkit_user_content_manager_remove_all_style_sheets(userContentManager);
</span><ins>+    webkit_user_content_manager_remove_all_scripts(userContentManager);
</ins><span class="cx"> 
</span><span class="cx">     while (*whitelist) {
</span><span class="cx">         g_free(*whitelist);
</span><span class="lines">@@ -112,7 +130,7 @@
</span><span class="cx">     char* whitelist[3] = { 0, 0, 0 };
</span><span class="cx">     char* blacklist[3] = { 0, 0, 0 };
</span><span class="cx"> 
</span><del>-    removeOldInjectedStyleSheetsAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
</del><ins>+    removeOldInjectedContentAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
</ins><span class="cx"> 
</span><span class="cx">     // Without a whitelist or a blacklist all URLs should have the injected style sheet.
</span><span class="cx">     static const char* randomPath = &quot;somerandompath&quot;;
</span><span class="lines">@@ -122,7 +140,7 @@
</span><span class="cx">     webkit_user_style_sheet_unref(styleSheet);
</span><span class="cx">     g_assert(isStyleSheetInjectedForURLAtPath(test, randomPath));
</span><span class="cx"> 
</span><del>-    removeOldInjectedStyleSheetsAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
</del><ins>+    removeOldInjectedContentAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
</ins><span class="cx"> 
</span><span class="cx">     fillURLListFromPaths(blacklist, randomPath, 0);
</span><span class="cx">     styleSheet = webkit_user_style_sheet_new(kInjectedStyleSheet, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, nullptr, blacklist);
</span><span class="lines">@@ -131,7 +149,7 @@
</span><span class="cx">     g_assert(!isStyleSheetInjectedForURLAtPath(test, randomPath));
</span><span class="cx">     g_assert(isStyleSheetInjectedForURLAtPath(test, &quot;someotherrandompath&quot;));
</span><span class="cx"> 
</span><del>-    removeOldInjectedStyleSheetsAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
</del><ins>+    removeOldInjectedContentAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
</ins><span class="cx"> 
</span><span class="cx">     static const char* inTheWhiteList = &quot;inthewhitelist&quot;;
</span><span class="cx">     static const char* notInWhitelist = &quot;notinthewhitelist&quot;;
</span><span class="lines">@@ -147,9 +165,52 @@
</span><span class="cx">     g_assert(!isStyleSheetInjectedForURLAtPath(test, notInWhitelist));
</span><span class="cx"> 
</span><span class="cx">     // It's important to clean up the environment before other tests.
</span><del>-    removeOldInjectedStyleSheetsAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
</del><ins>+    removeOldInjectedContentAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void testUserContentManagerInjectedScript(UserContentManagerTest* test, gconstpointer)
+{
+    char* whitelist[3] = { 0, 0, 0 };
+    char* blacklist[3] = { 0, 0, 0 };
+
+    removeOldInjectedContentAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
+
+    // Without a whitelist or a blacklist all URLs should have the injected script.
+    static const char* randomPath = &quot;somerandompath&quot;;
+    g_assert(!isScriptInjectedForURLAtPath(test, randomPath));
+    WebKitUserScript* script = webkit_user_script_new(kInjectedScript, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END, nullptr, nullptr);
+    webkit_user_content_manager_add_script(test-&gt;m_userContentManager.get(), script);
+    webkit_user_script_unref(script);
+    g_assert(isScriptInjectedForURLAtPath(test, randomPath));
+
+    removeOldInjectedContentAndResetLists(test-&gt;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-&gt;m_userContentManager.get(), script);
+    webkit_user_script_unref(script);
+    g_assert(!isScriptInjectedForURLAtPath(test, randomPath));
+    g_assert(isScriptInjectedForURLAtPath(test, &quot;someotherrandompath&quot;));
+
+    removeOldInjectedContentAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
+
+    static const char* inTheWhiteList = &quot;inthewhitelist&quot;;
+    static const char* notInWhitelist = &quot;notinthewhitelist&quot;;
+    static const char* inTheWhiteListAndBlackList = &quot;inthewhitelistandblacklist&quot;;
+
+    fillURLListFromPaths(whitelist, inTheWhiteList, inTheWhiteListAndBlackList, 0);
+    fillURLListFromPaths(blacklist, inTheWhiteListAndBlackList, 0);
+    script = webkit_user_script_new(kInjectedScript, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END, whitelist, blacklist);
+    webkit_user_content_manager_add_script(test-&gt;m_userContentManager.get(), script);
+    webkit_user_script_unref(script);
+    g_assert(isScriptInjectedForURLAtPath(test, inTheWhiteList));
+    g_assert(!isScriptInjectedForURLAtPath(test, inTheWhiteListAndBlackList));
+    g_assert(!isScriptInjectedForURLAtPath(test, notInWhitelist));
+
+    // It's important to clean up the environment before other tests.
+    removeOldInjectedContentAndResetLists(test-&gt;m_userContentManager.get(), whitelist, blacklist);
+}
+
</ins><span class="cx"> static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
</span><span class="cx"> {
</span><span class="cx">     soup_message_set_status(message, SOUP_STATUS_OK);
</span><span class="lines">@@ -164,6 +225,7 @@
</span><span class="cx"> 
</span><span class="cx">     Test::add(&quot;WebKitWebView&quot;, &quot;new-with-user-content-manager&quot;, testWebViewNewWithUserContentManager);
</span><span class="cx">     UserContentManagerTest::add(&quot;WebKitUserContentManager&quot;, &quot;injected-style-sheet&quot;, testUserContentManagerInjectedStyleSheet);
</span><ins>+    UserContentManagerTest::add(&quot;WebKitUserContentManager&quot;, &quot;injected-script&quot;, testUserContentManagerInjectedScript);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void afterAll()
</span></span></pre>
</div>
</div>

</body>
</html>