Changeset 128960 in webkit
- Timestamp:
- Sep 18, 2012 7:37:21 PM (12 years ago)
- Location:
- trunk/Source/WebKit2
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebKit2/ChangeLog
r128958 r128960 1 2012-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 1 37 2012-09-18 Ryuan Choi <ryuan.choi@samsung.com> 2 38 -
trunk/Source/WebKit2/UIProcess/API/gtk/WebKitSettings.cpp
r122113 r128960 35 35 #include "WebKitSettingsPrivate.h" 36 36 #include "WebPageProxy.h" 37 #include <WebCore/UserAgentGtk.h> 37 38 #include <glib/gi18n-lib.h> 38 39 #include <wtf/text/CString.h> … … 48 49 CString pictographFontFamily; 49 50 CString defaultCharset; 51 CString userAgent; 50 52 bool allowModalDialogs; 51 53 bool zoomTextOnly; … … 115 117 PROP_DRAW_COMPOSITING_INDICATORS, 116 118 PROP_ENABLE_SITE_SPECIFIC_QUIRKS, 117 PROP_ENABLE_PAGE_CACHE 119 PROP_ENABLE_PAGE_CACHE, 120 PROP_USER_AGENT 118 121 }; 119 122 … … 246 249 webkit_settings_set_enable_page_cache(settings, g_value_get_boolean(value)); 247 250 break; 251 case PROP_USER_AGENT: 252 webkit_settings_set_user_agent(settings, g_value_get_string(value)); 253 break; 248 254 default: 249 255 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); … … 379 385 case PROP_ENABLE_PAGE_CACHE: 380 386 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)); 381 390 break; 382 391 default: … … 1011 1020 readWriteConstructParamFlags)); 1012 1021 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 1013 1042 g_type_class_add_private(klass, sizeof(WebKitSettingsPrivate)); 1014 1043 } … … 1049 1078 void webkitSettingsAttachSettingsToPage(WebKitSettings* settings, WKPageRef wkPage) 1050 1079 { 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()); 1053 1087 } 1054 1088 … … 2556 2590 g_object_notify(G_OBJECT(settings), "enable-page-cache"); 2557 2591 } 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 */ 2601 const 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 */ 2617 void 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 */ 2640 void 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 363 363 gboolean enabled); 364 364 365 WEBKIT_API const gchar * 366 webkit_settings_get_user_agent (WebKitSettings *settings); 367 368 WEBKIT_API void 369 webkit_settings_set_user_agent (WebKitSettings *settings, 370 const gchar *user_agent); 371 WEBKIT_API void 372 webkit_settings_set_user_agent_with_application_details (WebKitSettings *settings, 373 const gchar *application_name, 374 const gchar *application_version); 375 365 376 G_END_DECLS 366 377 -
trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp
r128871 r128960 249 249 } 250 250 251 static 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 251 257 static void webkitWebViewSetSettings(WebKitWebView* webView, WebKitSettings* settings, WKPageRef wkPage) 252 258 { … … 255 261 g_signal_connect(settings, "notify::allow-modal-dialogs", G_CALLBACK(allowModalDialogsChanged), webView); 256 262 g_signal_connect(settings, "notify::zoom-text-only", G_CALLBACK(zoomTextOnlyChanged), webView); 263 g_signal_connect(settings, "notify::user-agent", G_CALLBACK(userAgentChanged), webView); 257 264 } 258 265 … … 262 269 g_signal_handlers_disconnect_by_func(settings, reinterpret_cast<gpointer>(allowModalDialogsChanged), webView); 263 270 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); 265 272 } 266 273 -
trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt
r126030 r128960 284 284 webkit_settings_get_enable_page_cache 285 285 webkit_settings_set_enable_page_cache 286 webkit_settings_get_user_agent 287 webkit_settings_set_user_agent 288 webkit_settings_set_user_agent_with_application_details 286 289 287 290 <SUBSECTION Standard> -
trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitSettings.cpp
r122113 r128960 32 32 33 33 #include "TestMain.h" 34 #include "WebViewTest.h" 35 #include "WebKitTestServer.h" 34 36 #include <gtk/gtk.h> 35 37 #include <webkit2/webkit2.h> 36 38 #include <wtf/gobject/GRefPtr.h> 39 40 static WebKitTestServer* gServer; 37 41 38 42 static void testWebKitSettings(Test*, gconstpointer) … … 255 259 } 256 260 261 static CString convertWebViewMainResourceDataToCString(WebViewTest* test) 262 { 263 size_t mainResourceDataSize = 0; 264 const char* mainResourceData = test->mainResourceData(mainResourceDataSize); 265 return CString(mainResourceData, mainResourceDataSize); 266 } 267 268 static 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 275 static 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 311 static 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 257 324 void beforeAll() 258 325 { 326 gServer = new WebKitTestServer(); 327 gServer->run(serverCallback); 328 259 329 Test::add("WebKitSettings", "webkit-settings", testWebKitSettings); 260 330 Test::add("WebKitSettings", "new-with-settings", testWebKitSettingsNewWithSettings); 331 WebViewTest::add("WebKitSettings", "user-agent", testWebKitSettingsUserAgent); 261 332 } 262 333 263 334 void afterAll() 264 335 { 265 } 266 336 delete gServer; 337 } 338 -
trunk/Source/WebKit2/UIProcess/gtk/WebPageProxyGtk.cpp
r128907 r128960 34 34 #include "WebPageMessages.h" 35 35 #include "WebProcessProxy.h" 36 #include <WebCore/UserAgentGtk.h> 36 37 #include <gtk/gtkx.h> 37 38 … … 45 46 String WebPageProxy::standardUserAgent(const String& applicationNameForUserAgent) 46 47 { 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); 49 49 } 50 50
Note: See TracChangeset
for help on using the changeset viewer.