Changeset 241790 in webkit


Ignore:
Timestamp:
Feb 20, 2019 2:16:00 AM (5 years ago)
Author:
Adrian Perez de Castro
Message:

[WPE][GTK] Enable support for CONTENT_EXTENSIONS
https://bugs.webkit.org/show_bug.cgi?id=167941

Reviewed by Carlos Garcia Campos.

Source/WebCore:

  • platform/gtk/po/POTFILES.in: Added WebKitUserContentFilterStore.cpp

to the list of files with translatable strings.

Source/WebKit:

Adds new API to manage a collection of content extensions on disk (including compilation
of new ones) using WebKitUserContentFilterStore; the associated WebKitUserContentFilter
type (which represents a compiled content extension); and the functions needed to enable
and disable them for a given WebKitUserContentManager.

The WebKitUserContentFilterStore public API is expressed in abstract terms of "saving"
filters into the store (which involves compiling the JSON source rule set); and "loading"
them back as main operations. This way we do not disclose to users of the API any detail
about compilation, nor how contents are laid out on disk, and the documentation explicitly
tells about only using the provided functions to manipulate the on-disk contents. This
way we allow ourselves some leeway if the implementation needs changing in the future.

  • PlatformGTK.cmake: Added WebKitUserContentFilterStore.h to the list of public API headers.
  • PlatformWPE.cmake: Ditto.
  • SourcesGTK.txt: Added WebKitUserContentFilterStore.cpp
  • SourcesWPE.txt: Ditto.
  • UIProcess/API/glib/WebKitError.cpp: Add definition of webkit_user_content_filter_error_quark().
  • UIProcess/API/glib/WebKitUserContent.cpp: Added WebKitUserContentFilter.

(_WebKitUserContentFilter::_WebKitUserContentFilter):
(webkit_user_content_filter_ref):
(webkit_user_content_filter_unref):
(webkit_user_content_filter_get_identifier):
(webkitUserContentFilterCreate):
(webkitUserContentFilterGetContentRuleList):

  • UIProcess/API/glib/WebKitUserContentFilterStore.cpp: Added.

(toGError): Utility function to convert content extension error codes to GError.
(webkit_user_content_filter_store_class_init):
(webkit_user_content_filter_store_new):
(webkit_user_content_filter_store_get_path):
(webkitUserContentFilterStoreSaveBytes): Common function used as final step for all the
functions which save (compile) JSON rule sets into the store, to avoid duplicating code.
(webkit_user_content_filter_store_save):
(webkit_user_content_filter_store_save_finish):
(webkit_user_content_filter_store_save_from_file):
(webkit_user_content_filter_store_save_from_file_finish):
(webkit_user_content_filter_store_remove):
(webkit_user_content_filter_store_remove_finish):
(webkit_user_content_filter_store_load):
(webkit_user_content_filter_store_lookup_finish):
(webkit_user_content_filter_store_fetch_identifiers):
(webkit_user_content_filter_store_fetch_identifiers_finish):

  • UIProcess/API/glib/WebKitUserContentManager.cpp: Added definitions for the new API

functions to add and remove filters from an user content manager.
(webkit_user_content_manager_add_filter):
(webkit_user_content_manager_remove_filter):
(webkit_user_content_manager_remove_all_filters):

  • UIProcess/API/glib/WebKitUserContentPrivate.h: Added declarations for

webkitUserContentFilterCreate() and webkitUserContentFilterGetContentRuleList().

  • UIProcess/API/gtk/WebKitAutocleanups.h: Added autocleanups for WebKitUserContentFilter

and WebKitUserContentFilterStore.

  • UIProcess/API/gtk/WebKitError.h: Added declarations for WEBKIT_USER_CONTENT_FILTER_ERROR

plus the associated webkit_user_content_filter_error_quark() function and
WebKitUserContentFilterErrror enum.

  • UIProcess/API/gtk/WebKitUserContent.h: Added declarations for WebKitUserContentFilter

and its associated functions.

  • UIProcess/API/gtk/WebKitUserContentFilterStore.h: Added.
  • UIProcess/API/gtk/WebKitUserContentManager.h: Added declarations for the functions to

add and remove filters from the user content manager.

  • UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt: Added new API functions and types to

be included in the documentation.

  • UIProcess/API/gtk/webkit2.h: Added inclusion of WebKitUserContentFilterStore.h
  • UIProcess/API/wpe/WebKitError.h: Same as for the GTK port header.
  • UIProcess/API/wpe/WebKitUserContent.h: Same as for the GTK port header.
  • UIProcess/API/wpe/WebKitUserContentFilterStore.h: Added.
  • UIProcess/API/wpe/WebKitUserContentManager.h: Same as for the GTK port header.
  • UIProcess/API/wpe/docs/wpe-0.1-sections.txt: Added new API functions and types to be

included in the documentation.

  • UIProcess/API/wpe/webkit.h: Added inclusion of WebKitUserContentFilterStore.h

Source/WTF:

Add specialization of the refGPtr() and derefGPtr() templates for GMappedFile.

  • wtf/glib/GRefPtr.cpp:

(WTF::refGPtr): Added.
(WTF::derefGPtr): Added.

  • wtf/glib/GRefPtr.h: Declare template specializations.

Tools:

  • MiniBrowser/gtk/main.c:

(filterSavedCallback): Added.
(main): Support loading a JSON rule set file for content filtering.

  • MiniBrowser/wpe/main.cpp:

(filterSavedCallback): Added.
(main): Support loading a JSON rule set file for content filtering.

  • TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentFilterStore.cpp: Added.

(WTF::refGPtr): Added locally for WebKitUserContentFilter, as it would not be used anywhere else.
(WTF::derefGPtr): Ditto.
(testEmptyStore):
(testSaveInvalidFilter):
(testSaveLoadFilter):
(testSavedFilterIdentifierMatch):
(testRemoveFilter):
(testSaveMultipleFilters):
(testSaveFilterFromFile):
(testFilterPersistence):
(beforeAll):
(afterAll):

  • TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentManager.cpp:

(removeOldInjectedContentAndResetLists): Also reset content filters.
(isCSSBlockedForURLAtPath): Added. Tests whether the test filter, which blocks a
CSS style sheet, has blocked the load of the CSS by looking at the resulting style of
the element affected by the style sheet.
(getUserContentFilter): Added. Stores the test filter in a WebKitUserContentFilterStore
and returns it to be used by tests.
(testUserContentManagerContentFilter): Added. Tests whether adding and removing a filter
from the WebKitUserContentManager results in the a CSS style sheet being blocked.
(serverCallback): Add support for serving a CSS style sheet for testing.
(beforeAll): Add call to testUserContentManagerContentFilter().

  • TestWebKitAPI/glib/CMakeLists.txt: Added TestWebKitUserContentFilterStore.
Location:
trunk
Files:
4 added
30 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r241782 r241790  
     12019-02-20  Adrian Perez de Castro  <aperez@igalia.com>
     2
     3        [WPE][GTK] Enable support for CONTENT_EXTENSIONS
     4        https://bugs.webkit.org/show_bug.cgi?id=167941
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        Add specialization of the refGPtr() and derefGPtr() templates for GMappedFile.
     9
     10        * wtf/glib/GRefPtr.cpp:
     11        (WTF::refGPtr): Added.
     12        (WTF::derefGPtr): Added.
     13        * wtf/glib/GRefPtr.h: Declare template specializations.
     14
    1152019-02-19  Commit Queue  <commit-queue@webkit.org>
    216
  • trunk/Source/WTF/wtf/glib/GRefPtr.cpp

    r237099 r241790  
    170170}
    171171
     172template <> GMappedFile* refGPtr(GMappedFile* ptr)
     173{
     174    if (ptr)
     175        g_mapped_file_ref(ptr);
     176    return ptr;
     177}
     178
     179template <> void derefGPtr(GMappedFile* ptr)
     180{
     181    if (ptr)
     182        g_mapped_file_unref(ptr);
     183}
     184
    172185} // namespace WTF
    173186
  • trunk/Source/WTF/wtf/glib/GRefPtr.h

    r234141 r241790  
    240240template <> WTF_EXPORT_PRIVATE GRegex* refGPtr(GRegex*);
    241241template <> WTF_EXPORT_PRIVATE void derefGPtr(GRegex*);
     242template <> WTF_EXPORT_PRIVATE GMappedFile* refGPtr(GMappedFile*);
     243template <> WTF_EXPORT_PRIVATE void derefGPtr(GMappedFile*);
    242244
    243245template <typename T> inline T* refGPtr(T* ptr)
  • trunk/Source/WebCore/ChangeLog

    r241788 r241790  
     12019-02-20  Adrian Perez de Castro  <aperez@igalia.com>
     2
     3        [WPE][GTK] Enable support for CONTENT_EXTENSIONS
     4        https://bugs.webkit.org/show_bug.cgi?id=167941
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        * platform/gtk/po/POTFILES.in: Added WebKitUserContentFilterStore.cpp
     9        to the list of files with translatable strings.
     10
    1112019-02-19  Simon Fraser  <simon.fraser@apple.com>
    212
  • trunk/Source/WebCore/platform/gtk/po/POTFILES.in

    r240431 r241790  
    2020../../../WebKit/UIProcess/API/glib/WebKitResponsePolicyDecision.cpp
    2121../../../WebKit/UIProcess/API/glib/WebKitSettings.cpp
     22../../../WebKit/UIProcess/API/glib/WebKitUserContentFilterStore.cpp
    2223../../../WebKit/UIProcess/API/glib/WebKitUserMediaPermissionRequest.cpp
    2324../../../WebKit/UIProcess/API/glib/WebKitWebContext.cpp
  • trunk/Source/WebKit/ChangeLog

    r241779 r241790  
     12019-02-20  Adrian Perez de Castro  <aperez@igalia.com>
     2
     3        [WPE][GTK] Enable support for CONTENT_EXTENSIONS
     4        https://bugs.webkit.org/show_bug.cgi?id=167941
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        Adds new API to manage a collection of content extensions on disk (including compilation
     9        of new ones) using WebKitUserContentFilterStore; the associated WebKitUserContentFilter
     10        type (which represents a compiled content extension); and the functions needed to enable
     11        and disable them for a given WebKitUserContentManager.
     12
     13        The WebKitUserContentFilterStore public API is expressed in abstract terms of "saving"
     14        filters into the store (which involves compiling the JSON source rule set); and "loading"
     15        them back as main operations. This way we do not disclose to users of the API any detail
     16        about compilation, nor how contents are laid out on disk, and the documentation explicitly
     17        tells about only using the provided functions to manipulate the on-disk contents. This
     18        way we allow ourselves some leeway if the implementation needs changing in the future.
     19
     20        * PlatformGTK.cmake: Added WebKitUserContentFilterStore.h to the list of public API headers.
     21        * PlatformWPE.cmake: Ditto.
     22        * SourcesGTK.txt: Added WebKitUserContentFilterStore.cpp
     23        * SourcesWPE.txt: Ditto.
     24        * UIProcess/API/glib/WebKitError.cpp: Add definition of webkit_user_content_filter_error_quark().
     25        * UIProcess/API/glib/WebKitUserContent.cpp: Added WebKitUserContentFilter.
     26        (_WebKitUserContentFilter::_WebKitUserContentFilter):
     27        (webkit_user_content_filter_ref):
     28        (webkit_user_content_filter_unref):
     29        (webkit_user_content_filter_get_identifier):
     30        (webkitUserContentFilterCreate):
     31        (webkitUserContentFilterGetContentRuleList):
     32        * UIProcess/API/glib/WebKitUserContentFilterStore.cpp: Added.
     33        (toGError): Utility function to convert content extension error codes to GError.
     34        (webkit_user_content_filter_store_class_init):
     35        (webkit_user_content_filter_store_new):
     36        (webkit_user_content_filter_store_get_path):
     37        (webkitUserContentFilterStoreSaveBytes): Common function used as final step for all the
     38        functions which save (compile) JSON rule sets into the store, to avoid duplicating code.
     39        (webkit_user_content_filter_store_save):
     40        (webkit_user_content_filter_store_save_finish):
     41        (webkit_user_content_filter_store_save_from_file):
     42        (webkit_user_content_filter_store_save_from_file_finish):
     43        (webkit_user_content_filter_store_remove):
     44        (webkit_user_content_filter_store_remove_finish):
     45        (webkit_user_content_filter_store_load):
     46        (webkit_user_content_filter_store_lookup_finish):
     47        (webkit_user_content_filter_store_fetch_identifiers):
     48        (webkit_user_content_filter_store_fetch_identifiers_finish):
     49        * UIProcess/API/glib/WebKitUserContentManager.cpp: Added definitions for the new API
     50        functions to add and remove filters from an user content manager.
     51        (webkit_user_content_manager_add_filter):
     52        (webkit_user_content_manager_remove_filter):
     53        (webkit_user_content_manager_remove_all_filters):
     54        * UIProcess/API/glib/WebKitUserContentPrivate.h: Added declarations for
     55        webkitUserContentFilterCreate() and webkitUserContentFilterGetContentRuleList().
     56        * UIProcess/API/gtk/WebKitAutocleanups.h: Added autocleanups for WebKitUserContentFilter
     57        and WebKitUserContentFilterStore.
     58        * UIProcess/API/gtk/WebKitError.h: Added declarations for WEBKIT_USER_CONTENT_FILTER_ERROR
     59        plus the associated webkit_user_content_filter_error_quark() function and
     60        WebKitUserContentFilterErrror enum.
     61        * UIProcess/API/gtk/WebKitUserContent.h: Added declarations for WebKitUserContentFilter
     62        and its associated functions.
     63        * UIProcess/API/gtk/WebKitUserContentFilterStore.h: Added.
     64        * UIProcess/API/gtk/WebKitUserContentManager.h: Added declarations for the functions to
     65        add and remove filters from the user content manager.
     66        * UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt: Added new API functions and types to
     67        be included in the documentation.
     68        * UIProcess/API/gtk/webkit2.h: Added inclusion of WebKitUserContentFilterStore.h
     69        * UIProcess/API/wpe/WebKitError.h: Same as for the GTK port header.
     70        * UIProcess/API/wpe/WebKitUserContent.h: Same as for the GTK port header.
     71        * UIProcess/API/wpe/WebKitUserContentFilterStore.h: Added.
     72        * UIProcess/API/wpe/WebKitUserContentManager.h: Same as for the GTK port header.
     73        * UIProcess/API/wpe/docs/wpe-0.1-sections.txt: Added new API functions and types to be
     74        included in the documentation.
     75        * UIProcess/API/wpe/webkit.h: Added inclusion of WebKitUserContentFilterStore.h
     76
    1772019-02-19  Antti Koivisto  <antti@apple.com>
    278
  • trunk/Source/WebKit/PlatformGTK.cmake

    r241224 r241790  
    110110    ${WEBKIT_DIR}/UIProcess/API/gtk/WebKitURIUtilities.h
    111111    ${WEBKIT_DIR}/UIProcess/API/gtk/WebKitUserContent.h
     112    ${WEBKIT_DIR}/UIProcess/API/gtk/WebKitUserContentFilterStore.h
    112113    ${WEBKIT_DIR}/UIProcess/API/gtk/WebKitUserContentManager.h
    113114    ${WEBKIT_DIR}/UIProcess/API/gtk/WebKitUserMediaPermissionRequest.h
  • trunk/Source/WebKit/PlatformWPE.cmake

    r241260 r241790  
    134134    ${WEBKIT_DIR}/UIProcess/API/wpe/WebKitURIUtilities.h
    135135    ${WEBKIT_DIR}/UIProcess/API/wpe/WebKitUserContent.h
     136    ${WEBKIT_DIR}/UIProcess/API/gtk/WebKitUserContentFilterStore.h
    136137    ${WEBKIT_DIR}/UIProcess/API/wpe/WebKitUserContentManager.h
    137138    ${WEBKIT_DIR}/UIProcess/API/wpe/WebKitUserMediaPermissionRequest.h
  • trunk/Source/WebKit/SourcesGTK.txt

    r241474 r241790  
    176176UIProcess/API/glib/WebKitURIUtilities.cpp @no-unify
    177177UIProcess/API/glib/WebKitUserContent.cpp @no-unify
     178UIProcess/API/glib/WebKitUserContentFilterStore.cpp @no-unify
    178179UIProcess/API/glib/WebKitUserContentManager.cpp @no-unify
    179180UIProcess/API/glib/WebKitUserMediaPermissionRequest.cpp @no-unify
  • trunk/Source/WebKit/SourcesWPE.txt

    r241474 r241790  
    158158UIProcess/API/glib/WebKitURIUtilities.cpp @no-unify
    159159UIProcess/API/glib/WebKitUserContent.cpp @no-unify
     160UIProcess/API/glib/WebKitUserContentFilterStore.cpp @no-unify
    160161UIProcess/API/glib/WebKitUserContentManager.cpp @no-unify
    161162UIProcess/API/glib/WebKitUserMediaPermissionRequest.cpp @no-unify
  • trunk/Source/WebKit/UIProcess/API/glib/WebKitError.cpp

    r218553 r241790  
    7272    return g_quark_from_static_string("WebKitSnapshotError");
    7373}
     74
     75G_DEFINE_QUARK(WebKitUserContentFilterError, webkit_user_content_filter_error)
  • trunk/Source/WebKit/UIProcess/API/glib/WebKitUserContent.cpp

    r235282 r241790  
    11/*
    2  * Copyright (C) 2014 Igalia S.L.
     2 * Copyright (C) 2014, 2018-2019 Igalia S.L.
    33 *
    44 * This library is free software; you can redistribute it and/or
     
    318318    return *userScript->userScript;
    319319}
     320
     321
     322struct _WebKitUserContentFilter {
     323    _WebKitUserContentFilter(RefPtr<API::ContentRuleList>&& contentRuleList)
     324        : identifier(contentRuleList->name().utf8())
     325        , contentRuleList(WTFMove(contentRuleList))
     326        , referenceCount(1)
     327    {
     328    }
     329
     330    CString identifier;
     331    RefPtr<API::ContentRuleList> contentRuleList;
     332    int referenceCount;
     333};
     334
     335G_DEFINE_BOXED_TYPE(WebKitUserContentFilter, webkit_user_content_filter, webkit_user_content_filter_ref, webkit_user_content_filter_unref)
     336
     337/**
     338 * webkit_user_content_filter_ref:
     339 * @user_content_filter: A #WebKitUserContentFilter
     340 *
     341 * Atomically increments the reference count of @user_content_filter by one.
     342 * This function is MT-safe and may be called from any thread.
     343 *
     344 * Since: 2.24
     345 */
     346WebKitUserContentFilter* webkit_user_content_filter_ref(WebKitUserContentFilter* userContentFilter)
     347{
     348    g_return_val_if_fail(userContentFilter, nullptr);
     349    g_atomic_int_inc(&userContentFilter->referenceCount);
     350    return userContentFilter;
     351}
     352
     353/**
     354 * webkit_user_content_filter_unref:
     355 * @user_content_filter: A #WebKitUserContentFilter
     356 *
     357 * Atomically decrements the reference count of @user_content_filter by one.
     358 * If the reference count drops to 0, all the memory allocated by the
     359 * #WebKitUserContentFilter is released. This function is MT-safe and may
     360 * be called from any thread.
     361 *
     362 * Since: 2.24
     363 */
     364void webkit_user_content_filter_unref(WebKitUserContentFilter* userContentFilter)
     365{
     366    g_return_if_fail(userContentFilter);
     367    if (g_atomic_int_dec_and_test(&userContentFilter->referenceCount)) {
     368        userContentFilter->~WebKitUserContentFilter();
     369        fastFree(userContentFilter);
     370    }
     371}
     372
     373/**
     374 * webkit_user_content_filter_get_identifier:
     375 * @user_content_filter: A #WebKitUserContentFilter
     376 *
     377 * Obtain the identifier previously used to save the @user_content_filter in the
     378 * #WebKitUserContentFilterStore.
     379 *
     380 * Returns: (transfer none): the identifier for the filter
     381 *
     382 * Since: 2.24
     383 */
     384const char* webkit_user_content_filter_get_identifier(WebKitUserContentFilter* userContentFilter)
     385{
     386    g_return_val_if_fail(userContentFilter, nullptr);
     387    return userContentFilter->identifier.data();
     388}
     389
     390WebKitUserContentFilter* webkitUserContentFilterCreate(RefPtr<API::ContentRuleList>&& contentRuleList)
     391{
     392    WebKitUserContentFilter* userContentFilter = static_cast<WebKitUserContentFilter*>(fastMalloc(sizeof(WebKitUserContentFilter)));
     393    new (userContentFilter) WebKitUserContentFilter(WTFMove(contentRuleList));
     394    return userContentFilter;
     395}
     396
     397API::ContentRuleList& webkitUserContentFilterGetContentRuleList(WebKitUserContentFilter* userContentFilter)
     398{
     399    ASSERT(userContentFilter);
     400    return *userContentFilter->contentRuleList;
     401}
  • trunk/Source/WebKit/UIProcess/API/glib/WebKitUserContentManager.cpp

    r235282 r241790  
    319319}
    320320
     321/**
     322 * webkit_user_content_manager_add_filter:
     323 * @manager: A #WebKitUserContentManager
     324 * @filter: A #WebKitUserContentFilter
     325 *
     326 * Adds a #WebKitUserContentFilter to the given #WebKitUserContentManager.
     327 * The same #WebKitUserContentFilter can be reused with multiple
     328 * #WebKitUserContentManager instances.
     329 *
     330 * Filters need to be saved and loaded from #WebKitUserContentFilterStore.
     331 *
     332 * Since: 2.24
     333 */
     334void webkit_user_content_manager_add_filter(WebKitUserContentManager* manager, WebKitUserContentFilter* filter)
     335{
     336    g_return_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(manager));
     337    g_return_if_fail(filter);
     338    manager->priv->userContentController->addContentRuleList(webkitUserContentFilterGetContentRuleList(filter));
     339}
     340
     341/**
     342 * webkit_user_content_manager_remove_filter:
     343 * @manager: A #WebKitUserContentManager
     344 * @filter: A #WebKitUserContentFilter
     345 *
     346 * Removes a filter from the given #WebKitUserContentManager.
     347 *
     348 * Since 2.24
     349 */
     350void webkit_user_content_manager_remove_filter(WebKitUserContentManager* manager, WebKitUserContentFilter* filter)
     351{
     352    g_return_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(manager));
     353    g_return_if_fail(filter);
     354    manager->priv->userContentController->removeContentRuleList(webkitUserContentFilterGetContentRuleList(filter).name());
     355}
     356
     357/**
     358 * webkit_user_content_manager_remove_all_filters:
     359 * @manager: A #WebKitUserContentManager
     360 *
     361 * Removes all content filters from the given #WebKitUserContentManager.
     362 *
     363 * Since: 2.24
     364 */
     365void webkit_user_content_manager_remove_all_filters(WebKitUserContentManager* manager)
     366{
     367    g_return_if_fail(WEBKIT_IS_USER_CONTENT_MANAGER(manager));
     368    manager->priv->userContentController->removeAllContentRuleLists();
     369}
     370
    321371WebUserContentControllerProxy* webkitUserContentManagerGetUserContentControllerProxy(WebKitUserContentManager* manager)
    322372{
  • trunk/Source/WebKit/UIProcess/API/glib/WebKitUserContentPrivate.h

    r235282 r241790  
    11/*
    2  * Copyright (C) 2014 Igalia S.L.
     2 * Copyright (C) 2014, 2018 Igalia S.L.
    33 *
    44 * This library is free software; you can redistribute it and/or
     
    2121#define WebKitUserContentPrivate_h
    2222
     23#include "APIContentRuleList.h"
    2324#include "APIUserContentWorld.h"
    2425#include "APIUserScript.h"
     
    3132API::UserStyleSheet& webkitUserStyleSheetGetUserStyleSheet(WebKitUserStyleSheet*);
    3233API::UserContentWorld& webkitUserContentWorld(const char*);
     34API::ContentRuleList& webkitUserContentFilterGetContentRuleList(WebKitUserContentFilter*);
     35WebKitUserContentFilter* webkitUserContentFilterCreate(RefPtr<API::ContentRuleList>&&);
    3336
    3437#endif // WebKitUserContentPrivate_h
  • trunk/Source/WebKit/UIProcess/API/gtk/WebKitAutocleanups.h

    r239770 r241790  
    6666G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitURIResponse, g_object_unref)
    6767G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitURISchemeRequest, g_object_unref)
     68G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitUserContentFilterStore, g_object_unref)
    6869G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitUserContentManager, g_object_unref)
    6970G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitUserMediaPermissionRequest, g_object_unref)
     
    8485G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitUserScript, webkit_user_script_unref)
    8586G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitUserStyleSheet, webkit_user_style_sheet_unref)
     87G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitUserContentFilter, webkit_user_content_filter_unref)
    8688G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitWebsiteData, webkit_website_data_unref)
    8789G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebKitWebViewSessionState, webkit_web_view_session_state_unref)
  • trunk/Source/WebKit/UIProcess/API/gtk/WebKitError.h

    r218326 r241790  
    3030G_BEGIN_DECLS
    3131
    32 #define WEBKIT_NETWORK_ERROR    webkit_network_error_quark ()
    33 #define WEBKIT_POLICY_ERROR     webkit_policy_error_quark ()
    34 #define WEBKIT_PLUGIN_ERROR     webkit_plugin_error_quark ()
    35 #define WEBKIT_DOWNLOAD_ERROR   webkit_download_error_quark ()
    36 #define WEBKIT_PRINT_ERROR      webkit_print_error_quark ()
    37 #define WEBKIT_JAVASCRIPT_ERROR webkit_javascript_error_quark ()
    38 #define WEBKIT_SNAPSHOT_ERROR   webkit_snapshot_error_quark ()
     32#define WEBKIT_NETWORK_ERROR             webkit_network_error_quark ()
     33#define WEBKIT_POLICY_ERROR              webkit_policy_error_quark ()
     34#define WEBKIT_PLUGIN_ERROR              webkit_plugin_error_quark ()
     35#define WEBKIT_DOWNLOAD_ERROR            webkit_download_error_quark ()
     36#define WEBKIT_PRINT_ERROR               webkit_print_error_quark ()
     37#define WEBKIT_JAVASCRIPT_ERROR          webkit_javascript_error_quark ()
     38#define WEBKIT_SNAPSHOT_ERROR            webkit_snapshot_error_quark ()
     39#define WEBKIT_USER_CONTENT_FILTER_ERROR webkit_user_content_filter_error_quark ()
    3940
    4041/**
     
    142143} WebKitSnapshotError;
    143144
    144 WEBKIT_API GQuark
    145 webkit_network_error_quark    (void);
     145/**
     146 * WebKitUserContentFilterError:
     147 * @WEBKIT_USER_CONTENT_FILTER_ERROR_INVALID_SOURCE: The JSON source for a content filter is invalid.
     148 * @WEBKIT_USER_CONTENT_FILTER_ERROR_NOT_FOUND: The requested content filter could not be found.
     149 *
     150 * Since: 2.24
     151 */
     152typedef enum {
     153    WEBKIT_USER_CONTENT_FILTER_ERROR_INVALID_SOURCE,
     154    WEBKIT_USER_CONTENT_FILTER_ERROR_NOT_FOUND,
     155} WebKitUserContentFilterError;
    146156
    147157WEBKIT_API GQuark
    148 webkit_policy_error_quark     (void);
     158webkit_network_error_quark             (void);
    149159
    150160WEBKIT_API GQuark
    151 webkit_plugin_error_quark     (void);
     161webkit_policy_error_quark              (void);
    152162
    153163WEBKIT_API GQuark
    154 webkit_download_error_quark   (void);
     164webkit_plugin_error_quark              (void);
    155165
    156166WEBKIT_API GQuark
    157 webkit_print_error_quark      (void);
     167webkit_download_error_quark            (void);
    158168
    159169WEBKIT_API GQuark
    160 webkit_javascript_error_quark (void);
     170webkit_print_error_quark              (void);
    161171
    162172WEBKIT_API GQuark
    163 webkit_snapshot_error_quark   (void);
     173webkit_javascript_error_quark          (void);
     174
     175WEBKIT_API GQuark
     176webkit_snapshot_error_quark            (void);
     177
     178WEBKIT_API GQuark
     179webkit_user_content_filter_error_quark (void);
    164180
    165181G_END_DECLS
  • trunk/Source/WebKit/UIProcess/API/gtk/WebKitUserContent.h

    r235282 r241790  
    137137                                  const gchar* const             *blacklist);
    138138
     139#define WEBKIT_TYPE_USER_CONTENT_FILTER   (webkit_user_content_filter_get_type())
     140
     141typedef struct _WebKitUserContentFilter WebKitUserContentFilter;
     142
     143WEBKIT_API GType
     144webkit_user_content_filter_get_type       (void);
     145
     146WEBKIT_API const char*
     147webkit_user_content_filter_get_identifier (WebKitUserContentFilter     *user_content_filter);
     148
     149WEBKIT_API WebKitUserContentFilter *
     150webkit_user_content_filter_ref            (WebKitUserContentFilter     *user_content_filter);
     151
     152WEBKIT_API void
     153webkit_user_content_filter_unref          (WebKitUserContentFilter     *user_content_filter);
     154
    139155G_END_DECLS
    140156
  • trunk/Source/WebKit/UIProcess/API/gtk/WebKitUserContentManager.h

    r235282 r241790  
    9494webkit_user_content_manager_remove_all_scripts                         (WebKitUserContentManager *manager);
    9595
     96WEBKIT_API void
     97webkit_user_content_manager_add_filter                                 (WebKitUserContentManager *manager,
     98                                                                        WebKitUserContentFilter  *filter);
     99
     100WEBKIT_API void
     101webkit_user_content_manager_remove_filter                              (WebKitUserContentManager *manager,
     102                                                                        WebKitUserContentFilter  *filter);
     103
     104WEBKIT_API void
     105webkit_user_content_manager_remove_all_filters                         (WebKitUserContentManager *manager);
     106
    96107G_END_DECLS
    97108
  • trunk/Source/WebKit/UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt

    r241258 r241790  
    9797WebKitUserStyleLevel
    9898WebKitUserScriptInjectionTime
     99WebKitUserContentFilter
    99100webkit_user_style_sheet_ref
    100101webkit_user_style_sheet_unref
     
    105106webkit_user_script_new
    106107webkit_user_script_new_for_world
     108webkit_user_content_filter_ref
     109webkit_user_content_filter_unref
     110webkit_user_content_filter_get_identifier
    107111
    108112<SUBSECTION Standard>
    109113WEBKIT_TYPE_USER_STYLE_SHEET
    110114WEBKIT_TYPE_USER_SCRIPT
     115WEBKIT_TYPE_USER_CONTENT_FILTER
    111116
    112117<SUBSECTION Private>
    113118webkit_user_style_sheet_get_type
    114119webkit_user_script_get_type
     120webkit_user_content_filter_get_type
    115121</SECTION>
    116122
     
    128134webkit_user_content_manager_register_script_message_handler_in_world
    129135webkit_user_content_manager_unregister_script_message_handler_in_world
     136webkit_user_content_manager_add_filter
     137webkit_user_content_manager_remove_filter
     138webkit_user_content_manager_remove_all_filters
    130139
    131140<SUBSECTION Standard>
     
    141150WebKitUserContentManagerPrivate
    142151webkit_user_content_manager_get_type
     152</SECTION>
     153
     154<SECTION>
     155<FILE>WebKitUserContentFilterStore</FILE>
     156<TITLE>WebKitUserContentFilterStore</TITLE>
     157WebKitUserContentFilterStore
     158webkit_user_content_filter_store_new
     159webkit_user_content_filter_store_get_path
     160webkit_user_content_filter_store_save
     161webkit_user_content_filter_store_save_finish
     162webkit_user_content_filter_store_save_from_file
     163webkit_user_content_filter_store_save_from_file_finish
     164webkit_user_content_filter_store_load
     165webkit_user_content_filter_store_load_finish
     166webkit_user_content_filter_store_remove
     167webkit_user_content_filter_store_remove_finish
     168webkit_user_content_filter_store_fetch_identifiers
     169webkit_user_content_filter_store_fetch_identifiers_finish
     170
     171<SUBSECTION Standard>
     172WEBKIT_IS_USER_CONTENT_FILTER_STORE
     173WEBKIT_IS_USER_CONTENT_FILTER_STORE_CLASS
     174WEBKIT_TYPE_USER_CONTENT_FILTER_STORE
     175WEBKIT_USER_CONTENT_FILTER_STORE
     176WEBKIT_USER_CONTENT_FILTER_STORE_CLASS
     177WEBKIT_USER_CONTENT_FILTER_STORE_GET_CLASS
     178WebKitUserContentFilterStoreClass
     179
     180<SUBSECTION Private>
     181WebKitUserContentFilterStorePrivate
     182webkit_user_content_filter_store_get_type
    143183</SECTION>
    144184
     
    945985WEBKIT_JAVASCRIPT_ERROR
    946986WEBKIT_SNAPSHOT_ERROR
     987WEBKIT_USER_CONTENT_FILTER_ERROR
    947988WebKitNetworkError
    948989WebKitPluginError
     
    952993WebKitJavascriptError
    953994WebKitSnapshotError
     995WebKitUserContentFilterError
    954996webkit_network_error_quark
    955997webkit_plugin_error_quark
     
    9591001webkit_javascript_error_quark
    9601002webkit_snapshot_error_quark
     1003webkit_user_content_filter_error_quark
    9611004</SECTION>
    9621005
  • trunk/Source/WebKit/UIProcess/API/gtk/webkit2.h

    r240962 r241790  
    7676#include <webkit2/WebKitURIUtilities.h>
    7777#include <webkit2/WebKitUserContent.h>
     78#include <webkit2/WebKitUserContentFilterStore.h>
    7879#include <webkit2/WebKitUserContentManager.h>
    7980#include <webkit2/WebKitUserMediaPermissionRequest.h>
  • trunk/Source/WebKit/UIProcess/API/wpe/WebKitError.h

    r218553 r241790  
    3030G_BEGIN_DECLS
    3131
    32 #define WEBKIT_NETWORK_ERROR    webkit_network_error_quark ()
    33 #define WEBKIT_POLICY_ERROR     webkit_policy_error_quark ()
    34 #define WEBKIT_PLUGIN_ERROR     webkit_plugin_error_quark ()
    35 #define WEBKIT_DOWNLOAD_ERROR   webkit_download_error_quark ()
    36 #define WEBKIT_JAVASCRIPT_ERROR webkit_javascript_error_quark ()
    37 #define WEBKIT_SNAPSHOT_ERROR   webkit_snapshot_error_quark ()
     32#define WEBKIT_NETWORK_ERROR             webkit_network_error_quark ()
     33#define WEBKIT_POLICY_ERROR              webkit_policy_error_quark ()
     34#define WEBKIT_PLUGIN_ERROR              webkit_plugin_error_quark ()
     35#define WEBKIT_DOWNLOAD_ERROR            webkit_download_error_quark ()
     36#define WEBKIT_JAVASCRIPT_ERROR          webkit_javascript_error_quark ()
     37#define WEBKIT_SNAPSHOT_ERROR            webkit_snapshot_error_quark ()
     38#define WEBKIT_USER_CONTENT_FILTER_ERROR webkit_user_content_filter_error_quark ()
    3839
    3940/**
     
    127128} WebKitSnapshotError;
    128129
    129 WEBKIT_API GQuark
    130 webkit_network_error_quark    (void);
     130/**
     131 * WebKitUserContentFilterError:
     132 * @WEBKIT_USER_CONTENT_FILTER_ERROR_INVALID_SOURCE: The JSON source for a content filter is invalid.
     133 * @WEBKIT_USER_CONTENT_FILTER_ERROR_NOT_FOUND: The requested content filter could not be found.
     134 *
     135 * Since: 2.24
     136 */
     137typedef enum {
     138    WEBKIT_USER_CONTENT_FILTER_ERROR_INVALID_SOURCE,
     139    WEBKIT_USER_CONTENT_FILTER_ERROR_NOT_FOUND,
     140} WebKitUserContentFilterError;
    131141
    132142WEBKIT_API GQuark
    133 webkit_policy_error_quark     (void);
     143webkit_network_error_quark             (void);
    134144
    135145WEBKIT_API GQuark
    136 webkit_plugin_error_quark     (void);
     146webkit_policy_error_quark              (void);
    137147
    138148WEBKIT_API GQuark
    139 webkit_download_error_quark   (void);
     149webkit_plugin_error_quark              (void);
    140150
    141151WEBKIT_API GQuark
    142 webkit_javascript_error_quark (void);
     152webkit_download_error_quark            (void);
    143153
    144154WEBKIT_API GQuark
    145 webkit_snapshot_error_quark   (void);
     155webkit_javascript_error_quark          (void);
     156
     157WEBKIT_API GQuark
     158webkit_snapshot_error_quark            (void);
     159
     160WEBKIT_API GQuark
     161webkit_user_content_filter_error_quark (void);
    146162
    147163G_END_DECLS
  • trunk/Source/WebKit/UIProcess/API/wpe/WebKitUserContent.h

    r235282 r241790  
    137137                                  const gchar* const             *blacklist);
    138138
     139#define WEBKIT_TYPE_USER_CONTENT_FILTER   (webkit_user_content_filter_get_type())
     140
     141typedef struct _WebKitUserContentFilter WebKitUserContentFilter;
     142
     143WEBKIT_API GType
     144webkit_user_content_filter_get_type       (void);
     145
     146WEBKIT_API const char*
     147webkit_user_content_filter_get_identifier (WebKitUserContentFilter     *user_content_filter);
     148
     149WEBKIT_API WebKitUserContentFilter *
     150webkit_user_content_filter_ref            (WebKitUserContentFilter     *user_content_filter);
     151
     152WEBKIT_API void
     153webkit_user_content_filter_unref          (WebKitUserContentFilter     *user_content_filter);
     154
    139155G_END_DECLS
    140156
  • trunk/Source/WebKit/UIProcess/API/wpe/WebKitUserContentManager.h

    r235282 r241790  
    9494webkit_user_content_manager_remove_all_scripts                         (WebKitUserContentManager *manager);
    9595
     96WEBKIT_API void
     97webkit_user_content_manager_add_filter                                 (WebKitUserContentManager *manager,
     98                                                                        WebKitUserContentFilter  *filter);
     99
     100WEBKIT_API void
     101webkit_user_content_manager_remove_filter                              (WebKitUserContentManager *manager,
     102                                                                        WebKitUserContentFilter  *filter);
     103
     104WEBKIT_API void
     105webkit_user_content_manager_remove_all_filters                         (WebKitUserContentManager *manager);
     106
    96107G_END_DECLS
    97108
  • trunk/Source/WebKit/UIProcess/API/wpe/docs/wpe-0.1-sections.txt

    r241258 r241790  
    7474WebKitUserStyleLevel
    7575WebKitUserScriptInjectionTime
     76WebKitUserContentFilter
    7677webkit_user_style_sheet_ref
    7778webkit_user_style_sheet_unref
     
    8283webkit_user_script_new
    8384webkit_user_script_new_for_world
     85webkit_user_content_filter_ref
     86webkit_user_content_filter_unref
     87webkit_user_content_filter_get_identifier
    8488
    8589<SUBSECTION Standard>
    8690WEBKIT_TYPE_USER_STYLE_SHEET
    8791WEBKIT_TYPE_USER_SCRIPT
     92WEBKIT_TYPE_USER_CONTENT_FILTER
    8893
    8994<SUBSECTION Private>
    9095webkit_user_style_sheet_get_type
    9196webkit_user_script_get_type
     97webkit_user_content_filter_get_type
    9298</SECTION>
    9399
     
    105111webkit_user_content_manager_register_script_message_handler_in_world
    106112webkit_user_content_manager_unregister_script_message_handler_in_world
     113webkit_user_content_manager_add_filter
     114webkit_user_content_manager_remove_filter
     115webkit_user_content_manager_remove_all_filters
    107116
    108117<SUBSECTION Standard>
     
    118127WebKitUserContentManagerPrivate
    119128webkit_user_content_manager_get_type
     129</SECTION>
     130
     131<SECTION>
     132<FILE>WebKitUserContentFilterStore</FILE>
     133<TITLE>WebKitUserContentFilterStore</TITLE>
     134WebKitUserContentFilterStore
     135webkit_user_content_filter_store_new
     136webkit_user_content_filter_store_get_path
     137webkit_user_content_filter_store_save
     138webkit_user_content_filter_store_save_finish
     139webkit_user_content_filter_store_save_from_file
     140webkit_user_content_filter_store_save_from_file_finish
     141webkit_user_content_filter_store_load
     142webkit_user_content_filter_store_load_finish
     143webkit_user_content_filter_store_remove
     144webkit_user_content_filter_store_remove_finish
     145webkit_user_content_filter_store_fetch_identifiers
     146webkit_user_content_filter_store_fetch_identifiers_finish
     147
     148<SUBSECTION Standard>
     149WEBKIT_IS_USER_CONTENT_FILTER_STORE
     150WEBKIT_IS_USER_CONTENT_FILTER_STORE_CLASS
     151WEBKIT_TYPE_USER_CONTENT_FILTER_STORE
     152WEBKIT_USER_CONTENT_FILTER_STORE
     153WEBKIT_USER_CONTENT_FILTER_STORE_CLASS
     154WEBKIT_USER_CONTENT_FILTER_STORE_GET_CLASS
     155WebKitUserContentFilterStoreClass
     156
     157<SUBSECTION Private>
     158WebKitUserContentFilterStorePrivate
     159webkit_user_content_filter_store_get_type
    120160</SECTION>
    121161
     
    888928WEBKIT_JAVASCRIPT_ERROR
    889929WEBKIT_SNAPSHOT_ERROR
     930WEBKIT_USER_CONTENT_FILTER_ERROR
    890931WebKitNetworkError
    891932WebKitPluginError
     
    894935WebKitJavascriptError
    895936WebKitSnapshotError
     937WebKitUserContentFilterError
    896938webkit_network_error_quark
    897939webkit_plugin_error_quark
     
    900942webkit_javascript_error_quark
    901943webkit_snapshot_error_quark
     944webkit_user_content_filter_error_quark
    902945</SECTION>
    903946
  • trunk/Source/WebKit/UIProcess/API/wpe/webkit.h

    r240962 r241790  
    7070#include <wpe/WebKitURIUtilities.h>
    7171#include <wpe/WebKitUserContent.h>
     72#include <wpe/WebKitUserContentFilterStore.h>
    7273#include <wpe/WebKitUserContentManager.h>
    7374#include <wpe/WebKitUserMediaPermissionRequest.h>
  • trunk/Tools/ChangeLog

    r241786 r241790  
     12019-02-20  Adrian Perez de Castro  <aperez@igalia.com>
     2
     3        [WPE][GTK] Enable support for CONTENT_EXTENSIONS
     4        https://bugs.webkit.org/show_bug.cgi?id=167941
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        * MiniBrowser/gtk/main.c:
     9        (filterSavedCallback): Added.
     10        (main): Support loading a JSON rule set file for content filtering.
     11        * MiniBrowser/wpe/main.cpp:
     12        (filterSavedCallback): Added.
     13        (main): Support loading a JSON rule set file for content filtering.
     14        * TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentFilterStore.cpp: Added.
     15        (WTF::refGPtr): Added locally for WebKitUserContentFilter, as it would not be used anywhere else.
     16        (WTF::derefGPtr): Ditto.
     17        (testEmptyStore):
     18        (testSaveInvalidFilter):
     19        (testSaveLoadFilter):
     20        (testSavedFilterIdentifierMatch):
     21        (testRemoveFilter):
     22        (testSaveMultipleFilters):
     23        (testSaveFilterFromFile):
     24        (testFilterPersistence):
     25        (beforeAll):
     26        (afterAll):
     27        * TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentManager.cpp:
     28        (removeOldInjectedContentAndResetLists): Also reset content filters.
     29        (isCSSBlockedForURLAtPath): Added. Tests whether the test filter, which blocks a
     30        CSS style sheet, has blocked the load of the CSS by looking at the resulting style of
     31        the element affected by the style sheet.
     32        (getUserContentFilter): Added. Stores the test filter in a WebKitUserContentFilterStore
     33        and returns it to be used by tests.
     34        (testUserContentManagerContentFilter): Added. Tests whether adding and removing a filter
     35        from the WebKitUserContentManager results in the a CSS style sheet being blocked.
     36        (serverCallback): Add support for serving a CSS style sheet for testing.
     37        (beforeAll): Add call to testUserContentManagerContentFilter().
     38        * TestWebKitAPI/glib/CMakeLists.txt: Added TestWebKitUserContentFilterStore.
     39
    1402019-02-19  Keith Rollin  <krollin@apple.com>
    241
  • trunk/Tools/MiniBrowser/gtk/main.c

    r239027 r241790  
    4949static gboolean fullScreen;
    5050static gboolean ignoreTLSErrors;
     51static const char *contentFilter;
    5152static const char *cookiesFile;
    5253static const char *cookiesPolicy;
     
    113114    { "ignore-host", 0, 0, G_OPTION_ARG_STRING_ARRAY, &ignoreHosts, "Set proxy ignore hosts", "HOSTS" },
    114115    { "ignore-tls-errors", 0, 0, G_OPTION_ARG_NONE, &ignoreTLSErrors, "Ignore TLS errors", NULL },
     116    { "content-filter", 0, 0, G_OPTION_ARG_FILENAME, &contentFilter, "JSON with content filtering rules", "FILE" },
    115117    { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &uriArguments, 0, "[URL…]" },
    116118    { 0, 0, 0, 0, 0, 0, 0 }
     
    472474}
    473475
     476typedef struct {
     477    GMainLoop *mainLoop;
     478    WebKitUserContentFilter *filter;
     479    GError *error;
     480} FilterSaveData;
     481
     482static void filterSavedCallback(WebKitUserContentFilterStore *store, GAsyncResult *result, FilterSaveData *data)
     483{
     484    data->filter = webkit_user_content_filter_store_save_finish(store, result, &data->error);
     485    g_main_loop_quit(data->mainLoop);
     486}
     487
    474488int main(int argc, char *argv[])
    475489{
     
    540554    g_signal_connect(userContentManager, "script-message-received::aboutData", G_CALLBACK(aboutDataScriptMessageReceivedCallback), webContext);
    541555
     556    if (contentFilter) {
     557        GFile *contentFilterFile = g_file_new_for_commandline_arg(contentFilter);
     558
     559        FilterSaveData saveData = { NULL, NULL, NULL };
     560        gchar *filtersPath = g_build_filename(g_get_user_cache_dir(), g_get_prgname(), "filters", NULL);
     561        WebKitUserContentFilterStore *store = webkit_user_content_filter_store_new(filtersPath);
     562        g_free(filtersPath);
     563
     564        webkit_user_content_filter_store_save_from_file(store, "GTKMiniBrowserFilter", contentFilterFile, NULL, (GAsyncReadyCallback)filterSavedCallback, &saveData);
     565        saveData.mainLoop = g_main_loop_new(NULL, FALSE);
     566        g_main_loop_run(saveData.mainLoop);
     567        g_object_unref(store);
     568
     569        if (saveData.filter)
     570            webkit_user_content_manager_add_filter(userContentManager, saveData.filter);
     571        else
     572            g_printerr("Cannot save filter '%s': %s\n", contentFilter, saveData.error->message);
     573
     574        g_clear_pointer(&saveData.error, g_error_free);
     575        g_clear_pointer(&saveData.filter, webkit_user_content_filter_unref);
     576        g_main_loop_unref(saveData.mainLoop);
     577        g_object_unref(contentFilterFile);
     578    }
     579
    542580    webkit_web_context_set_automation_allowed(webContext, automationMode);
    543581    g_signal_connect(webContext, "automation-started", G_CALLBACK(automationStartedCallback), NULL);
  • trunk/Tools/MiniBrowser/wpe/main.cpp

    r240843 r241790  
    4040static gboolean automationMode;
    4141static gboolean ignoreTLSErrors;
     42static const char* contentFilter;
    4243static const char* cookiesFile;
    4344static const char* cookiesPolicy;
     
    5455    { "ignore-host", 0, 0, G_OPTION_ARG_STRING_ARRAY, &ignoreHosts, "Set proxy ignore hosts", "HOSTS" },
    5556    { "ignore-tls-errors", 0, 0, G_OPTION_ARG_NONE, &ignoreTLSErrors, "Ignore TLS errors", nullptr },
     57    { "content-filter", 0, 0, G_OPTION_ARG_FILENAME, &contentFilter, "JSON with content filtering rules", "FILE" },
    5658    { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &uriArguments, nullptr, "[URL]" },
    5759    { nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr }
     
    124126        return std::make_unique<WPEToolingBackends::HeadlessViewBackend>(width, height);
    125127    return std::make_unique<WPEToolingBackends::WindowViewBackend>(width, height);
     128}
     129
     130typedef struct {
     131    GMainLoop* mainLoop { nullptr };
     132    WebKitUserContentFilter* filter { nullptr };
     133    GError* error { nullptr };
     134} FilterSaveData;
     135
     136static void filterSavedCallback(WebKitUserContentFilterStore *store, GAsyncResult *result, FilterSaveData *data)
     137{
     138    data->filter = webkit_user_content_filter_store_save_finish(store, result, &data->error);
     139    g_main_loop_quit(data->mainLoop);
    126140}
    127141
     
    185199        WEBKIT_PROCESS_MODEL_SHARED_SECONDARY_PROCESS : WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
    186200
     201    WebKitUserContentManager* userContentManager = nullptr;
     202    if (contentFilter) {
     203        GFile* contentFilterFile = g_file_new_for_commandline_arg(contentFilter);
     204
     205        FilterSaveData saveData = { nullptr, };
     206        gchar* filtersPath = g_build_filename(g_get_user_cache_dir(), g_get_prgname(), "filters", nullptr);
     207        WebKitUserContentFilterStore* store = webkit_user_content_filter_store_new(filtersPath);
     208        g_free(filtersPath);
     209
     210        webkit_user_content_filter_store_save_from_file(store, "WPEMiniBrowserFilter", contentFilterFile, nullptr, (GAsyncReadyCallback)filterSavedCallback, &saveData);
     211        saveData.mainLoop = g_main_loop_new(nullptr, FALSE);
     212        g_main_loop_run(saveData.mainLoop);
     213        g_object_unref(store);
     214
     215        if (saveData.filter) {
     216            userContentManager = webkit_user_content_manager_new();
     217            webkit_user_content_manager_add_filter(userContentManager, saveData.filter);
     218        } else
     219            g_printerr("Cannot save filter '%s': %s\n", contentFilter, saveData.error->message);
     220
     221        g_clear_pointer(&saveData.error, g_error_free);
     222        g_clear_pointer(&saveData.filter, webkit_user_content_filter_unref);
     223        g_main_loop_unref(saveData.mainLoop);
     224        g_object_unref(contentFilterFile);
     225    }
     226
    187227    auto* settings = webkit_settings_new_with_settings(
    188228        "enable-developer-extras", TRUE,
     
    201241        "web-context", webContext,
    202242        "settings", settings,
     243        "user-content-manager", userContentManager,
    203244        "is-controlled-by-automation", automationMode,
    204245        nullptr));
  • trunk/Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentManager.cpp

    r239772 r241790  
    2929
    3030// These are all here so that they can be changed easily, if necessary.
    31 static const char* kStyleSheetHTML = "<html><div id=\"styledElement\">Sweet stylez!</div></html>";
     31static const char* kTestHTML =
     32"<html>\n"
     33"  <head><link rel=\"stylesheet\" type=\"text/css\" href=\"/extra.css\"></head>\n"
     34"  <body>\n"
     35"    <div id=\"styledElement\">Sweet stylez!</div>\n"
     36"    <div id=\"otherElement\">Blocked stylez!</div>\n"
     37"  </body>\n"
     38"</html>";
     39
     40static const char* kTestCSS =
     41"div#otherElement {\n"
     42"  font-weight: bold;\n"
     43"}\n";
     44
    3245static const char* kInjectedStyleSheet = "#styledElement { font-weight: bold; }";
    3346static const char* kStyleSheetTestScript = "getComputedStyle(document.getElementById('styledElement'))['font-weight']";
     
    3649static const char* kScriptTestScript = "document.getElementById('item').innerText";
    3750static const char* kScriptTestScriptResult = "Generated by a script";
     51static const char* kScriptTestCSSBlocked = "getComputedStyle(document.getElementById('otherElement'))['font-weight']";
     52static const char* kScriptTestCSSBlockedResult = "normal";
     53static const char* kJSONFilter =
     54"[\n"
     55"  {\n"
     56"    \"trigger\": {\n"
     57"      \"url-filter\": \".*\",\n"
     58"      \"resource-type\": [\"style-sheet\"]\n"
     59"    },\n"
     60"    \"action\": {\n"
     61"      \"type\": \"block\"\n"
     62"    }\n"
     63"  }\n"
     64"]\n";
    3865
    3966static void testWebViewNewWithUserContentManager(Test* test, gconstpointer)
     
    98125    webkit_user_content_manager_remove_all_style_sheets(userContentManager);
    99126    webkit_user_content_manager_remove_all_scripts(userContentManager);
     127    webkit_user_content_manager_remove_all_filters(userContentManager);
    100128
    101129    while (*whitelist) {
     
    405433#endif
    406434
     435static bool isCSSBlockedForURLAtPath(WebViewTest* test, const char* path)
     436{
     437    test->loadURI(kServer->getURIForPath(path).data());
     438    test->waitUntilLoadFinished();
     439
     440    GUniqueOutPtr<GError> error;
     441    WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished(kScriptTestCSSBlocked, &error.outPtr());
     442    g_assert_nonnull(javascriptResult);
     443    g_assert_no_error(error.get());
     444
     445    GUniquePtr<char> valueString(WebViewTest::javascriptResultToCString(javascriptResult));
     446    return !g_strcmp0(valueString.get(), kScriptTestCSSBlockedResult);
     447}
     448
     449static WebKitUserContentFilter* getUserContentFilter(WebViewTest* test)
     450{
     451    GUniquePtr<char> filtersPath(g_build_filename(test->dataDirectory(), "filters", nullptr));
     452    WebKitUserContentFilterStore* store = webkit_user_content_filter_store_new(filtersPath.get());
     453    test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(store));
     454
     455    struct Data {
     456        GMainLoop* mainLoop;
     457        WebKitUserContentFilter* filter;
     458    };
     459    Data data { test->m_mainLoop, nullptr, };
     460
     461    GRefPtr<GBytes> source = adoptGRef(g_bytes_new_static(kJSONFilter, strlen(kJSONFilter)));
     462    webkit_user_content_filter_store_save(store, "TestFilter", source.get(), nullptr, [](GObject* sourceObject, GAsyncResult* result, void* userData) {
     463        auto* data = static_cast<Data*>(userData);
     464        GUniqueOutPtr<GError> error;
     465        data->filter = webkit_user_content_filter_store_save_finish(WEBKIT_USER_CONTENT_FILTER_STORE(sourceObject), result, &error.outPtr());
     466        g_assert_nonnull(data->filter);
     467        g_assert_no_error(error.get());
     468        g_main_loop_quit(data->mainLoop);
     469    }, &data);
     470    g_main_loop_run(data.mainLoop);
     471
     472    g_object_unref(store);
     473
     474    g_assert_nonnull(data.filter);
     475    return data.filter;
     476}
     477
     478static void testUserContentManagerContentFilter(WebViewTest* test, gconstpointer)
     479{
     480    char* whitelist[] = { nullptr, nullptr, nullptr };
     481    char* blacklist[] = { nullptr, nullptr, nullptr };
     482
     483    removeOldInjectedContentAndResetLists(test->m_userContentManager.get(), whitelist, blacklist);
     484
     485    static const char* somePath = "somepath";
     486    g_assert_false(isCSSBlockedForURLAtPath(test, somePath));
     487
     488    WebKitUserContentFilter* filter = getUserContentFilter(test);
     489    webkit_user_content_manager_add_filter(test->m_userContentManager.get(), filter);
     490    g_assert_true(isCSSBlockedForURLAtPath(test, somePath));
     491
     492    webkit_user_content_manager_remove_filter(test->m_userContentManager.get(), filter);
     493    g_assert_false(isCSSBlockedForURLAtPath(test, somePath));
     494
     495    webkit_user_content_filter_unref(filter);
     496}
     497
    407498static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
    408499{
    409500    soup_message_set_status(message, SOUP_STATUS_OK);
    410     soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, kStyleSheetHTML, strlen(kStyleSheetHTML));
     501    if (!g_strcmp0(path, "/extra.css"))
     502        soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, kTestCSS, strlen(kTestCSS));
     503    else
     504        soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, kTestHTML, strlen(kTestHTML));
    411505    soup_message_body_complete(message->response_body);
    412506}
     
    425519    UserScriptMessageTest::add("WebKitUserContentManager", "script-message-from-dom-bindings", testUserContentManagerScriptMessageFromDOMBindings);
    426520#endif
     521    WebViewTest::add("WebKitUserContentManager", "content-filter", testUserContentManagerContentFilter);
    427522}
    428523
  • trunk/Tools/TestWebKitAPI/glib/CMakeLists.txt

    r240962 r241790  
    142142ADD_WK2_TEST(TestWebKitWebContext ${TOOLS_DIR}/TestWebKitAPI/Tests/WebKitGLib/TestWebKitWebContext.cpp)
    143143ADD_WK2_TEST(TestWebKitWebView ${TOOLS_DIR}/TestWebKitAPI/Tests/WebKitGLib/TestWebKitWebView.cpp)
     144ADD_WK2_TEST(TestWebKitUserContentFilterStore ${TOOLS_DIR}/TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentFilterStore.cpp)
    144145ADD_WK2_TEST(TestWebKitUserContentManager ${TOOLS_DIR}/TestWebKitAPI/Tests/WebKitGLib/TestWebKitUserContentManager.cpp)
    145146ADD_WK2_TEST(TestWebsiteData ${TOOLS_DIR}/TestWebKitAPI/Tests/WebKitGLib/TestWebsiteData.cpp)
Note: See TracChangeset for help on using the changeset viewer.