Changeset 149219 in webkit


Ignore:
Timestamp:
Apr 26, 2013 4:06:10 PM (11 years ago)
Author:
Martin Robinson
Message:

[GTK] Add methods to add a user style sheet to the WebKit2 GTK+ API
https://bugs.webkit.org/show_bug.cgi?id=99081

Reviewed by Carlos Garcia Campos, Gustavo Noronha Silva, and Benjamin Poulain.

Add methods to WebKitWebViewGroup to add and remove user style sheets.
This allows clients to inject style sheets into pages with a set of
rules for when those style sheets apply.

  • UIProcess/API/gtk/WebKitWebViewGroup.cpp:

(toImmutableArray): Added this helper which converts the GList* parameters into
ImmutableArrays for use with the WebKit2 internal API.
(webkit_web_view_group_add_user_style_sheet): Added new API for adding a style sheet.
(webkit_web_view_group_remove_all_user_style_sheets): Add new API for clearing out all style sheets.

  • UIProcess/API/gtk/WebKitWebViewGroup.h: Added new method declarations.
  • UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Added new API to the documentation.
  • UIProcess/API/gtk/tests/TestWebKitWebViewGroup.cpp: Added a test for the new API.

(isStyleSheetInjectedForURLAtPath): Function to check whether the style sheet has been injected for a given URL.
(fillURLListFromPaths): Helper which converts paths passed via varargs into a whitelist or blacklist.
(removeOldInjectedStyleSheetsAndResetLists): Function to start afresh.
(testWebViewGroupInjectedStyleSheet): The actual test.
(serverCallback): Server callback for use with the test. We cannot use loadHTML or
loadAlternateHTML, because that checks the whitelist and blacklist against about:blank.
(beforeAll): Initialize the server and new test.
(afterAll): Clean up the server.

Location:
trunk/Source/WebKit2
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r149217 r149219  
     12013-04-26  Martin Robinson  <mrobinson@igalia.com>
     2
     3        [GTK] Add methods to add a user style sheet to the WebKit2 GTK+ API
     4        https://bugs.webkit.org/show_bug.cgi?id=99081
     5
     6        Reviewed by Carlos Garcia Campos, Gustavo Noronha Silva, and Benjamin Poulain.
     7
     8        Add methods to WebKitWebViewGroup to add and remove user style sheets.
     9        This allows clients to inject style sheets into pages with a set of
     10        rules for when those style sheets apply.
     11
     12        * UIProcess/API/gtk/WebKitWebViewGroup.cpp:
     13        (toImmutableArray): Added this helper which converts the GList* parameters into
     14        ImmutableArrays for use with the WebKit2 internal API.
     15        (webkit_web_view_group_add_user_style_sheet): Added new API for adding a style sheet.
     16        (webkit_web_view_group_remove_all_user_style_sheets): Add new API for clearing out all style sheets.
     17        * UIProcess/API/gtk/WebKitWebViewGroup.h: Added new method declarations.
     18        * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Added new API to the documentation.
     19        * UIProcess/API/gtk/tests/TestWebKitWebViewGroup.cpp: Added a test for the new API.
     20        (isStyleSheetInjectedForURLAtPath): Function to check whether the style sheet has been injected for a given URL.
     21        (fillURLListFromPaths): Helper which converts paths passed via varargs into a whitelist or blacklist.
     22        (removeOldInjectedStyleSheetsAndResetLists): Function to start afresh.
     23        (testWebViewGroupInjectedStyleSheet): The actual test.
     24        (serverCallback): Server callback for use with the test. We cannot use loadHTML or
     25        loadAlternateHTML, because that checks the whitelist and blacklist against about:blank.
     26        (beforeAll): Initialize the server and new test.
     27        (afterAll): Clean up the server.
     28
    1292013-04-26  Simon Cooper  <scooper@apple.com>
    230
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebViewGroup.cpp

    r149117 r149219  
    2121#include "WebKitWebViewGroup.h"
    2222
     23#include "ImmutableArray.h"
    2324#include "WebKitPrivate.h"
    2425#include "WebKitSettingsPrivate.h"
     
    222223    g_object_notify(G_OBJECT(group), "settings");
    223224}
     225
     226COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_INJECTED_CONTENT_FRAMES_ALL, WebCore::InjectInAllFrames);
     227COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_INJECTED_CONTENT_FRAMES_TOP_ONLY, WebCore::InjectInTopFrameOnly);
     228
     229static PassRefPtr<ImmutableArray> toImmutableArray(const char* const* list)
     230{
     231    if (!list)
     232        return 0;
     233
     234    Vector<RefPtr<APIObject> > entries;
     235    while (*list) {
     236        entries.append(WebString::createFromUTF8String(*list));
     237        list++;
     238    }
     239    return ImmutableArray::adopt(entries);
     240}
     241
     242/**
     243 * webkit_web_view_group_add_user_style_sheet:
     244 * @group: a #WebKitWebViewGroup
     245 * @source: the source of the style_sheet to inject
     246 * @base_uri: (allow-none): the base URI to use when processing the style_sheet contents or %NULL for about:blank
     247 * @whitelist: (array zero-terminated=1) (allow-none): a whitelist of URI patterns or %NULL
     248 * @blacklist: (array zero-terminated=1) (allow-none): a blacklist of URI patterns or %NULL
     249 * @injected_frames: a #WebKitInjectedContentFrames describing to which frames the style_sheet should apply
     250 *
     251 * Inject an external style sheet into pages. It is possible to only apply the style sheet
     252 * to some URIs by passing non-null values for @whitelist or @blacklist. Passing a %NULL
     253 * whitelist implies that all URIs are on the whitelist. The style sheet is applied if a URI matches
     254 * the whitelist and not the blacklist. URI patterns must be of the form [protocol]://[host]/[path]
     255 * where the host and path components can contain the wildcard character ('*') to represent zero
     256 * or more other characters.
     257 */
     258void webkit_web_view_group_add_user_style_sheet(WebKitWebViewGroup* group, const char* source, const char* baseURI, const char* const* whitelist, const char* const* blacklist, WebKitInjectedContentFrames injectedFrames)
     259{
     260    g_return_if_fail(WEBKIT_IS_WEB_VIEW_GROUP(group));
     261    g_return_if_fail(source);
     262
     263    RefPtr<ImmutableArray> webWhitelist = toImmutableArray(whitelist);
     264    RefPtr<ImmutableArray> webBlacklist = toImmutableArray(blacklist);
     265
     266    // We always use UserStyleUserLevel to match the behavior of WKPageGroupAddUserStyleSheet.
     267    group->priv->pageGroup->addUserStyleSheet(
     268        String::fromUTF8(source),
     269        String::fromUTF8(baseURI),
     270        webWhitelist.get(),
     271        webBlacklist.get(),
     272        static_cast<WebCore::UserContentInjectedFrames>(injectedFrames),
     273        WebCore::UserStyleUserLevel);
     274}
     275
     276/**
     277 * webkit_web_view_group_remove_all_user_style_sheets:
     278 * @group: a #WebKitWebViewGroup
     279 *
     280 * Remove all style sheets previously injected into this #WebKitWebViewGroup
     281 * via webkit_web_view_group_add_user_style_sheet().
     282 */
     283void webkit_web_view_group_remove_all_user_style_sheets(WebKitWebViewGroup* group)
     284{
     285    g_return_if_fail(WEBKIT_IS_WEB_VIEW_GROUP(group));
     286    group->priv->pageGroup->removeAllUserStyleSheets();
     287}
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebViewGroup.h

    r149117 r149219  
    5252};
    5353
     54/**
     55 * WebKitInjectedContentFrames:
     56 * @WEBKIT_INJECTED_CONTENT_FRAMES_ALL: Content will be injected into all frames.
     57 * @WEBKIT_INJECTED_CONTENT_FRAMES_TOP_ONLY: Content will only be injected into the main frame.
     58 *
     59 * Enum values used for determining into which frames content is injected.
     60 */
     61typedef enum {
     62    WEBKIT_INJECTED_CONTENT_FRAMES_ALL,
     63    WEBKIT_INJECTED_CONTENT_FRAMES_TOP_ONLY,
     64} WebKitInjectedContentFrames;
     65
    5466WEBKIT_API GType
    55 webkit_web_view_group_get_type     (void);
     67webkit_web_view_group_get_type                     (void);
    5668
    5769WEBKIT_API WebKitWebViewGroup *
    58 webkit_web_view_group_new          (const gchar        *name);
     70webkit_web_view_group_new                          (const gchar                 *name);
    5971
    6072WEBKIT_API const gchar *
    61 webkit_web_view_group_get_name     (WebKitWebViewGroup *group);
     73webkit_web_view_group_get_name                     (WebKitWebViewGroup          *group);
    6274
    6375WEBKIT_API WebKitSettings *
    64 webkit_web_view_group_get_settings (WebKitWebViewGroup *group);
     76webkit_web_view_group_get_settings                 (WebKitWebViewGroup          *group);
    6577
    6678WEBKIT_API void
    67 webkit_web_view_group_set_settings (WebKitWebViewGroup *group,
    68                                     WebKitSettings     *settings);
     79webkit_web_view_group_set_settings                 (WebKitWebViewGroup          *group,
     80                                                    WebKitSettings              *settings);
     81
     82WEBKIT_API void
     83webkit_web_view_group_add_user_style_sheet         (WebKitWebViewGroup           *group,
     84                                                    const gchar                  *source,
     85                                                    const gchar                  *base_uri,
     86                                                    const gchar * const          *whitelist,
     87                                                    const gchar * const          *blacklist,
     88                                                    WebKitInjectedContentFrames   injected_frames);
     89
     90WEBKIT_API void
     91webkit_web_view_group_remove_all_user_style_sheets (WebKitWebViewGroup          *group);
    6992
    7093G_END_DECLS
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt

    r149117 r149219  
    946946<FILE>WebKitWebViewGroup</FILE>
    947947WebKitWebViewGroup
     948WebKitInjectedContentFrames
    948949webkit_web_view_group_new
    949950webkit_web_view_group_get_name
    950951webkit_web_view_group_get_settings
    951952webkit_web_view_group_set_settings
     953webkit_web_view_group_add_user_style_sheet
     954webkit_web_view_group_remove_all_user_style_sheets
    952955
    953956<SUBSECTION Standard>
  • trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebViewGroup.cpp

    r149117 r149219  
    2020#include "config.h"
    2121
     22#include "WebKitTestServer.h"
    2223#include "WebViewTest.h"
     24#include <cstdarg>
    2325#include <gtk/gtk.h>
    2426#include <webkit2/webkit2.h>
    2527#include <wtf/gobject/GRefPtr.h>
     28
     29static WebKitTestServer* kServer;
     30
     31// These are all here so that they can be changed easily, if necessary.
     32static const char* kStyleSheetHTML = "<html><div id=\"styledElement\">Sweet stylez!</div></html>";
     33static const char* kInjectedStyleSheet = "#styledElement { font-weight: bold; }";
     34static const char* kStyleSheetTestScript = "getComputedStyle(document.getElementById('styledElement'))['font-weight']";
     35static const char* kStyleSheetTestScriptResult = "bold";
    2636
    2737static void testWebViewGroupDefault(Test* test, gconstpointer)
     
    8999}
    90100
     101static bool isStyleSheetInjectedForURLAtPath(WebViewTest* test, const char* path)
     102{
     103    test->loadURI(kServer->getURIForPath(path).data());
     104    test->waitUntilLoadFinished();
     105
     106    GOwnPtr<GError> error;
     107    WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished(kStyleSheetTestScript, &error.outPtr());
     108    g_assert(javascriptResult);
     109    g_assert(!error.get());
     110
     111    GOwnPtr<char> resultString(WebViewTest::javascriptResultToCString(javascriptResult));
     112    return !g_strcmp0(resultString.get(), kStyleSheetTestScriptResult);
     113}
     114
     115static void fillURLListFromPaths(char** list, const char* path, ...)
     116{
     117    va_list argumentList;
     118    va_start(argumentList, path);
     119
     120    int i = 0;
     121    while (path) {
     122        // FIXME: We must use a wildcard for the host here until http://wkbug.com/112476 is fixed.
     123        // Until that time patterns with port numbers in them will not properly match URLs with port numbers.
     124        list[i++] = g_strdup_printf("http://*/%s*", path);
     125        path = va_arg(argumentList, const char*);
     126    }
     127}
     128
     129static void removeOldInjectedStyleSheetsAndResetLists(WebKitWebViewGroup* group, char** whitelist, char** blacklist)
     130{
     131    webkit_web_view_group_remove_all_user_style_sheets(group);
     132
     133    while (*whitelist) {
     134        g_free(*whitelist);
     135        *whitelist = 0;
     136        whitelist++;
     137    }
     138
     139    while (*blacklist) {
     140        g_free(*blacklist);
     141        *blacklist = 0;
     142        blacklist++;
     143    }
     144}
     145
     146static void testWebViewGroupInjectedStyleSheet(WebViewTest* test, gconstpointer)
     147{
     148    WebKitWebViewGroup* group = webkit_web_view_get_group(test->m_webView);
     149    char* whitelist[3] = { 0, 0, 0 };
     150    char* blacklist[3] = { 0, 0, 0 };
     151
     152    removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist);
     153
     154    // Without a whitelist or a blacklist all URLs should have the injected style sheet.
     155    static const char* randomPath = "somerandompath";
     156    g_assert(!isStyleSheetInjectedForURLAtPath(test, randomPath));
     157    webkit_web_view_group_add_user_style_sheet(group, kInjectedStyleSheet, 0, 0, 0, WEBKIT_INJECTED_CONTENT_FRAMES_ALL);
     158    g_assert(isStyleSheetInjectedForURLAtPath(test, randomPath));
     159
     160    removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist);
     161
     162    fillURLListFromPaths(blacklist, randomPath, 0);
     163    webkit_web_view_group_add_user_style_sheet(group, kInjectedStyleSheet, 0, 0, blacklist, WEBKIT_INJECTED_CONTENT_FRAMES_ALL);
     164    g_assert(!isStyleSheetInjectedForURLAtPath(test, randomPath));
     165    g_assert(isStyleSheetInjectedForURLAtPath(test, "someotherrandompath"));
     166
     167    removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist);
     168
     169    static const char* inTheWhiteList = "inthewhitelist";
     170    static const char* notInWhitelist = "notinthewhitelist";
     171    static const char* inTheWhiteListAndBlackList = "inthewhitelistandblacklist";
     172
     173    fillURLListFromPaths(whitelist, inTheWhiteList, inTheWhiteListAndBlackList, 0);
     174    fillURLListFromPaths(blacklist, inTheWhiteListAndBlackList, 0);
     175    webkit_web_view_group_add_user_style_sheet(group, kInjectedStyleSheet, 0, whitelist, blacklist, WEBKIT_INJECTED_CONTENT_FRAMES_ALL);
     176    g_assert(isStyleSheetInjectedForURLAtPath(test, inTheWhiteList));
     177    g_assert(!isStyleSheetInjectedForURLAtPath(test, inTheWhiteListAndBlackList));
     178    g_assert(!isStyleSheetInjectedForURLAtPath(test, notInWhitelist));
     179
     180    // It's important to clean up the environment before other tests.
     181    removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist);
     182}
     183
     184static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
     185{
     186    soup_message_set_status(message, SOUP_STATUS_OK);
     187    soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, kStyleSheetHTML, strlen(kStyleSheetHTML));
     188    soup_message_body_complete(message->response_body);
     189}
     190
    91191void beforeAll()
    92192{
     193    kServer = new WebKitTestServer();
     194    kServer->run(serverCallback);
     195
    93196    Test::add("WebKitWebViewGroup", "default-group", testWebViewGroupDefault);
    94197    Test::add("WebKitWebViewGroup", "new-group", testWebViewGroupNewGroup);
    95198    Test::add("WebKitWebView", "new-with-group", testWebViewNewWithGroup);
    96199    Test::add("WebKitWebViewGroup", "settings", testWebViewGroupSettings);
     200    WebViewTest::add("WebKitWebViewGroup", "injected-style-sheet", testWebViewGroupInjectedStyleSheet);
    97201}
    98202
    99203void afterAll()
    100204{
    101 
    102 }
     205    delete kServer;
     206}
Note: See TracChangeset for help on using the changeset viewer.