Changeset 128960 in webkit


Ignore:
Timestamp:
Sep 18, 2012 7:37:21 PM (12 years ago)
Author:
Martin Robinson
Message:

[WebKit2] [GTK] Add API for controlling the user agent
https://bugs.webkit.org/show_bug.cgi?id=95697

Reviewed by Carlos Garcia Campos.

Add API for changing the user agent in WebKit2. This adds two styles of
setting the user agent: complete override and a method that just inserts
the application name and version, but preserves the carefully crafted user agent
in the library.

  • UIProcess/API/gtk/WebKitSettings.cpp:

(_WebKitSettingsPrivate): Added a new field to store the user agent.
This is stored in the private data structure, because we can only
set the user agent when attaching the settings to the page.
(webKitSettingsSetProperty): Add hooks for the new user agent property.
(webKitSettingsGetProperty): Ditto.
(webkit_settings_class_init): Ditto.
(webkitSettingsAttachSettingsToPage): Ditto.
(webkit_settings_get_user_agent): Added.
(webkit_settings_set_user_agent): Added.
(webkit_settings_set_user_agent_with_application_name): Added.

  • UIProcess/API/gtk/WebKitSettings.h: Added new methods.
  • UIProcess/API/gtk/WebKitWebView.cpp: Update the glue for the settings

when attaching and detaching from WebViews.

  • UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Added new methods to

the documentation.

  • UIProcess/API/gtk/tests/TestWebKitSettings.cpp: Test the new user agent

property.
(testWebKitSettingsUserAgent): Ditto.
(beforeAll): Ditto.

  • UIProcess/gtk/WebPageProxyGtk.cpp:

(WebKit::WebPageProxy::standardUserAgent): Now use the shared WebCore
code when setting the user agent.

Location:
trunk/Source/WebKit2
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r128958 r128960  
     12012-09-17  Martin Robinson  <mrobinson@igalia.com>
     2
     3        [WebKit2] [GTK] Add API for controlling the user agent
     4        https://bugs.webkit.org/show_bug.cgi?id=95697
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        Add API for changing the user agent in WebKit2. This adds two styles of
     9        setting the user agent: complete override and a method that just inserts
     10        the application name and version, but preserves the carefully crafted user agent
     11        in the library.
     12
     13        * UIProcess/API/gtk/WebKitSettings.cpp:
     14        (_WebKitSettingsPrivate): Added a new field to store the user agent.
     15        This is stored in the private data structure, because we can only
     16        set the user agent when attaching the settings to the page.
     17        (webKitSettingsSetProperty): Add hooks for the new user agent property.
     18        (webKitSettingsGetProperty): Ditto.
     19        (webkit_settings_class_init): Ditto.
     20        (webkitSettingsAttachSettingsToPage): Ditto.
     21        (webkit_settings_get_user_agent): Added.
     22        (webkit_settings_set_user_agent): Added.
     23        (webkit_settings_set_user_agent_with_application_name): Added.
     24        * UIProcess/API/gtk/WebKitSettings.h: Added new methods.
     25        * UIProcess/API/gtk/WebKitWebView.cpp: Update the glue for the settings
     26        when attaching and detaching from WebViews.
     27        * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Added new methods to
     28        the documentation.
     29        * UIProcess/API/gtk/tests/TestWebKitSettings.cpp: Test the new user agent
     30        property.
     31        (testWebKitSettingsUserAgent): Ditto.
     32        (beforeAll): Ditto.
     33        * UIProcess/gtk/WebPageProxyGtk.cpp:
     34        (WebKit::WebPageProxy::standardUserAgent): Now use the shared WebCore
     35        code when setting the user agent.
     36
    1372012-09-18  Ryuan Choi  <ryuan.choi@samsung.com>
    238
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitSettings.cpp

    r122113 r128960  
    3535#include "WebKitSettingsPrivate.h"
    3636#include "WebPageProxy.h"
     37#include <WebCore/UserAgentGtk.h>
    3738#include <glib/gi18n-lib.h>
    3839#include <wtf/text/CString.h>
     
    4849    CString pictographFontFamily;
    4950    CString defaultCharset;
     51    CString userAgent;
    5052    bool allowModalDialogs;
    5153    bool zoomTextOnly;
     
    115117    PROP_DRAW_COMPOSITING_INDICATORS,
    116118    PROP_ENABLE_SITE_SPECIFIC_QUIRKS,
    117     PROP_ENABLE_PAGE_CACHE
     119    PROP_ENABLE_PAGE_CACHE,
     120    PROP_USER_AGENT
    118121};
    119122
     
    246249        webkit_settings_set_enable_page_cache(settings, g_value_get_boolean(value));
    247250        break;
     251    case PROP_USER_AGENT:
     252        webkit_settings_set_user_agent(settings, g_value_get_string(value));
     253        break;
    248254    default:
    249255        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
     
    379385    case PROP_ENABLE_PAGE_CACHE:
    380386        g_value_set_boolean(value, webkit_settings_get_enable_page_cache(settings));
     387        break;
     388    case PROP_USER_AGENT:
     389        g_value_set_string(value, webkit_settings_get_user_agent(settings));
    381390        break;
    382391    default:
     
    10111020                                                         readWriteConstructParamFlags));
    10121021
     1022    /**
     1023     * WebKitSettings:user-agent:
     1024     *
     1025     * The user-agent string used by WebKit. Unusual user-agent strings may cause web
     1026     * content to render incorrectly or fail to run, as many web pages are written to
     1027     * parse the user-agent strings of only the most popular browsers. Therefore, it's
     1028     * typically better to not completely override the standard user-agent, but to use
     1029     * webkit_settings_set_user_agent_with_application_details() instead.
     1030     *
     1031     * If this property is set to the empty string or %NULL, it will revert to the standard
     1032     * user-agent.
     1033     */
     1034    g_object_class_install_property(gObjectClass,
     1035                                    PROP_USER_AGENT,
     1036                                    g_param_spec_string("user-agent",
     1037                                                        _("User agent string"),
     1038                                                        _("The user agent string"),
     1039                                                        0, // A null string forces the standard user agent.
     1040                                                        readWriteConstructParamFlags));
     1041
    10131042    g_type_class_add_private(klass, sizeof(WebKitSettingsPrivate));
    10141043}
     
    10491078void webkitSettingsAttachSettingsToPage(WebKitSettings* settings, WKPageRef wkPage)
    10501079{
    1051     WKPageGroupSetPreferences(WKPageGetPageGroup(wkPage), settings->priv->preferences.get());
    1052     WebKit::toImpl(wkPage)->setCanRunModal(settings->priv->allowModalDialogs);
     1080    WebKitSettingsPrivate* priv = settings->priv;
     1081    WKPageGroupSetPreferences(WKPageGetPageGroup(wkPage), priv->preferences.get());
     1082    WebKit::toImpl(wkPage)->setCanRunModal(priv->allowModalDialogs);
     1083
     1084    ASSERT(!priv->userAgent.isNull());
     1085    WKRetainPtr<WKStringRef> userAgent = adoptWK(WKStringCreateWithUTF8CString(priv->userAgent.data()));
     1086    WKPageSetCustomUserAgent(wkPage, userAgent.get());
    10531087}
    10541088
     
    25562590    g_object_notify(G_OBJECT(settings), "enable-page-cache");
    25572591}
     2592
     2593/**
     2594 * webkit_settings_get_user_agent:
     2595 * @settings: a #WebKitSettings
     2596 *
     2597 * Get the #WebKitSettings:user-agent property.
     2598 *
     2599 * Returns: The current value of the user-agent property.
     2600 */
     2601const char* webkit_settings_get_user_agent(WebKitSettings* settings)
     2602{
     2603    g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), 0);
     2604
     2605    WebKitSettingsPrivate* priv = settings->priv;
     2606    ASSERT(!priv->userAgent.isNull());
     2607    return priv->userAgent.data();
     2608}
     2609
     2610/**
     2611 * webkit_settings_set_user_agent:
     2612 * @settings: a #WebKitSettings
     2613 * @user_agent: (allow-none): The new custom user agent string or %NULL to use the default user agent
     2614 *
     2615 * Set the #WebKitSettings:user-agent property.
     2616 */
     2617void webkit_settings_set_user_agent(WebKitSettings* settings, const char* userAgent)
     2618{
     2619    g_return_if_fail(WEBKIT_IS_SETTINGS(settings));
     2620
     2621    WebKitSettingsPrivate* priv = settings->priv;
     2622    CString newUserAgent = (!userAgent || !strlen(userAgent)) ? WebCore::standardUserAgent("").utf8() : userAgent;
     2623    if (newUserAgent == priv->userAgent)
     2624        return;
     2625
     2626    priv->userAgent = newUserAgent;
     2627    g_object_notify(G_OBJECT(settings), "user-agent");
     2628}
     2629
     2630/**
     2631 * webkit_settings_set_user_agent_with_application_details:
     2632 * @settings: a #WebKitSettings
     2633 * @application_name: (allow-none): The application name used for the user agent or %NULL to use the default user agent.
     2634 * @application_version: (allow-none): The application version for the user agent or %NULL to user the default version.
     2635 *
     2636 * Set the #WebKitSettings:user-agent property by appending the application details to the default user
     2637 * agent. If no application name or version is given, the default user agent used will be used. If only
     2638 * the version is given, the default engine version is used with the given application name.
     2639 */
     2640void webkit_settings_set_user_agent_with_application_details(WebKitSettings* settings, const char* applicationName, const char* applicationVersion)
     2641{
     2642    g_return_if_fail(WEBKIT_IS_SETTINGS(settings));
     2643
     2644    CString newUserAgent = WebCore::standardUserAgent(String::fromUTF8(applicationName), String::fromUTF8(applicationVersion)).utf8();
     2645    webkit_settings_set_user_agent(settings, newUserAgent.data());
     2646}
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitSettings.h

    r122113 r128960  
    363363                                                                gboolean        enabled);
    364364
     365WEBKIT_API const gchar *
     366webkit_settings_get_user_agent                                 (WebKitSettings *settings);
     367
     368WEBKIT_API void
     369webkit_settings_set_user_agent                                 (WebKitSettings *settings,
     370                                                                const gchar    *user_agent);
     371WEBKIT_API void
     372webkit_settings_set_user_agent_with_application_details        (WebKitSettings *settings,
     373                                                                const gchar    *application_name,
     374                                                                const gchar    *application_version);
     375
    365376G_END_DECLS
    366377
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp

    r128871 r128960  
    249249}
    250250
     251static void userAgentChanged(WebKitSettings* settings, GParamSpec*, WebKitWebView* webView)
     252{
     253    WKRetainPtr<WKStringRef> userAgent = adoptWK(WKStringCreateWithUTF8CString(webkit_settings_get_user_agent(settings)));
     254    WKPageSetCustomUserAgent(toAPI(webkitWebViewBaseGetPage(WEBKIT_WEB_VIEW_BASE(webView))), userAgent.get());
     255}
     256
    251257static void webkitWebViewSetSettings(WebKitWebView* webView, WebKitSettings* settings, WKPageRef wkPage)
    252258{
     
    255261    g_signal_connect(settings, "notify::allow-modal-dialogs", G_CALLBACK(allowModalDialogsChanged), webView);
    256262    g_signal_connect(settings, "notify::zoom-text-only", G_CALLBACK(zoomTextOnlyChanged), webView);
     263    g_signal_connect(settings, "notify::user-agent", G_CALLBACK(userAgentChanged), webView);
    257264}
    258265
     
    262269    g_signal_handlers_disconnect_by_func(settings, reinterpret_cast<gpointer>(allowModalDialogsChanged), webView);
    263270    g_signal_handlers_disconnect_by_func(settings, reinterpret_cast<gpointer>(zoomTextOnlyChanged), webView);
    264 
     271    g_signal_handlers_disconnect_by_func(settings, reinterpret_cast<gpointer>(userAgentChanged), webView);
    265272}
    266273
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt

    r126030 r128960  
    284284webkit_settings_get_enable_page_cache
    285285webkit_settings_set_enable_page_cache
     286webkit_settings_get_user_agent
     287webkit_settings_set_user_agent
     288webkit_settings_set_user_agent_with_application_details
    286289
    287290<SUBSECTION Standard>
  • trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitSettings.cpp

    r122113 r128960  
    3232
    3333#include "TestMain.h"
     34#include "WebViewTest.h"
     35#include "WebKitTestServer.h"
    3436#include <gtk/gtk.h>
    3537#include <webkit2/webkit2.h>
    3638#include <wtf/gobject/GRefPtr.h>
     39
     40static WebKitTestServer* gServer;
    3741
    3842static void testWebKitSettings(Test*, gconstpointer)
     
    255259}
    256260
     261static CString convertWebViewMainResourceDataToCString(WebViewTest* test)
     262{
     263    size_t mainResourceDataSize = 0;
     264    const char* mainResourceData = test->mainResourceData(mainResourceDataSize);
     265    return CString(mainResourceData, mainResourceDataSize);
     266}
     267
     268static void assertThatUserAgentIsSentInHeaders(WebViewTest* test, const CString& userAgent)
     269{
     270    test->loadURI(gServer->getURIForPath("/").data());
     271    test->waitUntilLoadFinished();
     272    g_assert_cmpstr(convertWebViewMainResourceDataToCString(test).data(), ==, userAgent.data());
     273}
     274
     275static void testWebKitSettingsUserAgent(WebViewTest* test, gconstpointer)
     276{
     277    GRefPtr<WebKitSettings> settings = adoptGRef(webkit_settings_new());
     278    CString defaultUserAgent = webkit_settings_get_user_agent(settings.get());
     279    webkit_web_view_set_settings(test->m_webView, settings.get());
     280
     281    g_assert(g_strstr_len(defaultUserAgent.data(), -1, "Safari"));
     282    g_assert(g_strstr_len(defaultUserAgent.data(), -1, "Chromium"));
     283    g_assert(g_strstr_len(defaultUserAgent.data(), -1, "Chrome"));
     284
     285    webkit_settings_set_user_agent(settings.get(), 0);
     286    g_assert_cmpstr(defaultUserAgent.data(), ==, webkit_settings_get_user_agent(settings.get()));
     287    assertThatUserAgentIsSentInHeaders(test, defaultUserAgent.data());
     288
     289    webkit_settings_set_user_agent(settings.get(), "");
     290    g_assert_cmpstr(defaultUserAgent.data(), ==, webkit_settings_get_user_agent(settings.get()));
     291
     292    const char* funkyUserAgent = "Funky!";
     293    webkit_settings_set_user_agent(settings.get(), funkyUserAgent);
     294    g_assert_cmpstr(funkyUserAgent, ==, webkit_settings_get_user_agent(settings.get()));
     295    assertThatUserAgentIsSentInHeaders(test, funkyUserAgent);
     296
     297    webkit_settings_set_user_agent_with_application_details(settings.get(), "WebKitGTK+", 0);
     298    CString userAgentWithNullVersion = webkit_settings_get_user_agent(settings.get());
     299    g_assert_cmpstr(g_strstr_len(userAgentWithNullVersion.data(), -1, defaultUserAgent.data()), ==, userAgentWithNullVersion.data());
     300    g_assert(g_strstr_len(userAgentWithNullVersion.data(), -1, "WebKitGTK+"));
     301
     302    webkit_settings_set_user_agent_with_application_details(settings.get(), "WebKitGTK+", "");
     303    g_assert_cmpstr(webkit_settings_get_user_agent(settings.get()), ==, userAgentWithNullVersion.data());
     304
     305    webkit_settings_set_user_agent_with_application_details(settings.get(), "WebCatGTK+", "3.4.5");
     306    const char* newUserAgent = webkit_settings_get_user_agent(settings.get());
     307    g_assert(g_strstr_len(newUserAgent, -1, "3.4.5"));
     308    g_assert(g_strstr_len(newUserAgent, -1, "WebCatGTK+"));
     309}
     310
     311static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
     312{
     313    if (message->method != SOUP_METHOD_GET) {
     314        soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
     315        return;
     316    }
     317
     318    soup_message_set_status(message, SOUP_STATUS_OK);
     319    const char* userAgent = soup_message_headers_get_one(message->request_headers, "User-Agent");
     320    soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, userAgent, strlen(userAgent));
     321    soup_message_body_complete(message->response_body);
     322}
     323
    257324void beforeAll()
    258325{
     326    gServer = new WebKitTestServer();
     327    gServer->run(serverCallback);
     328
    259329    Test::add("WebKitSettings", "webkit-settings", testWebKitSettings);
    260330    Test::add("WebKitSettings", "new-with-settings", testWebKitSettingsNewWithSettings);
     331    WebViewTest::add("WebKitSettings", "user-agent", testWebKitSettingsUserAgent);
    261332}
    262333
    263334void afterAll()
    264335{
    265 }
    266 
     336    delete gServer;
     337}
     338
  • trunk/Source/WebKit2/UIProcess/gtk/WebPageProxyGtk.cpp

    r128907 r128960  
    3434#include "WebPageMessages.h"
    3535#include "WebProcessProxy.h"
     36#include <WebCore/UserAgentGtk.h>
    3637#include <gtk/gtkx.h>
    3738
     
    4546String WebPageProxy::standardUserAgent(const String& applicationNameForUserAgent)
    4647{
    47     // FIXME: This should not be hard coded.
    48     return "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.7 (KHTML, like Gecko) Version/5.0 Safari/534.7";
     48    return WebCore::standardUserAgent(applicationNameForUserAgent);
    4949}
    5050
Note: See TracChangeset for help on using the changeset viewer.