Title: [118786] trunk/Source/WebKit2
Revision
118786
Author
carlo...@webkit.org
Date
2012-05-29 09:09:46 -0700 (Tue, 29 May 2012)

Log Message

[GTK] Add webkit_web_view_can_show_mime_type() to WebKit2 GTK+ API
https://bugs.webkit.org/show_bug.cgi?id=85998

Reviewed by Martin Robinson.

And use it in the default handler of WebKitWebView::decide-policy
signal to decide whether to use or ignore a response policy
decision depending on whether the mime type of the response can be
displayed in the WebView or not.

* UIProcess/API/gtk/WebKitURIResponse.cpp:
(webkitURIResponseGetProperty): Add getter for mime-type property.
(webkit_uri_response_class_init): Add mime-type property.
(webkit_uri_response_get_mime_type): Return the mime type of the
response.
* UIProcess/API/gtk/WebKitURIResponse.h:
* UIProcess/API/gtk/WebKitWebView.cpp:
(webkitWebViewDecidePolicy): In case of response policy decisions,
use the URI response to decide what to do: download if the
response is an attachment, use if the mime type is supported by
the web view or ignore otherwise.
(webkit_web_view_can_show_mime_type): Returns whether the given
mime type can be displayed in the WebView or not.
* UIProcess/API/gtk/WebKitWebView.h:
* UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Add new symbols.
* UIProcess/API/gtk/tests/TestResources.cpp:
(testWebResourceResponse):
(testWebResourceMimeType):
(serverCallback):
(beforeAll):
* UIProcess/API/gtk/tests/TestWebKitWebView.cpp:
(testWebViewCanShowMIMEType):
(beforeAll):

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (118785 => 118786)


--- trunk/Source/WebKit2/ChangeLog	2012-05-29 16:06:16 UTC (rev 118785)
+++ trunk/Source/WebKit2/ChangeLog	2012-05-29 16:09:46 UTC (rev 118786)
@@ -1,3 +1,39 @@
+2012-05-29  Carlos Garcia Campos  <cgar...@igalia.com>
+
+        [GTK] Add webkit_web_view_can_show_mime_type() to WebKit2 GTK+ API
+        https://bugs.webkit.org/show_bug.cgi?id=85998
+
+        Reviewed by Martin Robinson.
+
+        And use it in the default handler of WebKitWebView::decide-policy
+        signal to decide whether to use or ignore a response policy
+        decision depending on whether the mime type of the response can be
+        displayed in the WebView or not.
+
+        * UIProcess/API/gtk/WebKitURIResponse.cpp:
+        (webkitURIResponseGetProperty): Add getter for mime-type property.
+        (webkit_uri_response_class_init): Add mime-type property.
+        (webkit_uri_response_get_mime_type): Return the mime type of the
+        response.
+        * UIProcess/API/gtk/WebKitURIResponse.h:
+        * UIProcess/API/gtk/WebKitWebView.cpp:
+        (webkitWebViewDecidePolicy): In case of response policy decisions,
+        use the URI response to decide what to do: download if the
+        response is an attachment, use if the mime type is supported by
+        the web view or ignore otherwise.
+        (webkit_web_view_can_show_mime_type): Returns whether the given
+        mime type can be displayed in the WebView or not.
+        * UIProcess/API/gtk/WebKitWebView.h:
+        * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Add new symbols.
+        * UIProcess/API/gtk/tests/TestResources.cpp:
+        (testWebResourceResponse):
+        (testWebResourceMimeType):
+        (serverCallback):
+        (beforeAll):
+        * UIProcess/API/gtk/tests/TestWebKitWebView.cpp:
+        (testWebViewCanShowMIMEType):
+        (beforeAll):
+
 2012-05-29  David Barr  <davidb...@chromium.org>
 
         Introduce ENABLE_CSS_IMAGE_RESOLUTION compile flag

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitURIResponse.cpp (118785 => 118786)


--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitURIResponse.cpp	2012-05-29 16:06:16 UTC (rev 118785)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitURIResponse.cpp	2012-05-29 16:09:46 UTC (rev 118786)
@@ -30,7 +30,8 @@
 
     PROP_URI,
     PROP_STATUS_CODE,
-    PROP_CONTENT_LENGTH
+    PROP_CONTENT_LENGTH,
+    PROP_MIME_TYPE
 };
 
 using namespace WebCore;
@@ -40,6 +41,7 @@
 struct _WebKitURIResponsePrivate {
     WebCore::ResourceResponse resourceResponse;
     CString uri;
+    CString mimeType;
 };
 
 static void webkitURIResponseFinalize(GObject* object)
@@ -62,6 +64,9 @@
     case PROP_CONTENT_LENGTH:
         g_value_set_uint64(value, webkit_uri_response_get_content_length(response));
         break;
+    case PROP_MIME_TYPE:
+        g_value_set_string(value, webkit_uri_response_get_mime_type(response));
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
     }
@@ -112,6 +117,19 @@
                                                         0, G_MAXUINT64, 0,
                                                         WEBKIT_PARAM_READABLE));
 
+    /**
+     * WebKitURIResponse:mime-type:
+     *
+     * The MIME type of the response.
+     */
+    g_object_class_install_property(objectClass,
+                                    PROP_MIME_TYPE,
+                                    g_param_spec_string("mime-type",
+                                                        _("MIME Type"),
+                                                        _("The MIME type of the response"),
+                                                        0,
+                                                        WEBKIT_PARAM_READABLE));
+
     g_type_class_add_private(responseClass, sizeof(WebKitURIResponsePrivate));
 }
 
@@ -170,6 +188,20 @@
     return response->priv->resourceResponse.expectedContentLength();
 }
 
+/**
+ * webkit_uri_response_get_mime_type:
+ * @response: a #WebKitURIResponse
+ *
+ * Returns: the MIME type of the #WebKitURIResponse
+ */
+const gchar* webkit_uri_response_get_mime_type(WebKitURIResponse* response)
+{
+    g_return_val_if_fail(WEBKIT_IS_URI_RESPONSE(response), 0);
+
+    response->priv->mimeType = response->priv->resourceResponse.mimeType().utf8();
+    return response->priv->mimeType.data();
+}
+
 WebKitURIResponse* webkitURIResponseCreateForResourceResponse(const WebCore::ResourceResponse& resourceResponse)
 {
     WebKitURIResponse* uriResponse = WEBKIT_URI_RESPONSE(g_object_new(WEBKIT_TYPE_URI_RESPONSE, NULL));

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitURIResponse.h (118785 => 118786)


--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitURIResponse.h	2012-05-29 16:06:16 UTC (rev 118785)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitURIResponse.h	2012-05-29 16:09:46 UTC (rev 118786)
@@ -64,6 +64,9 @@
 WEBKIT_API guint64
 webkit_uri_response_get_content_length (WebKitURIResponse *response);
 
+WEBKIT_API const gchar *
+webkit_uri_response_get_mime_type      (WebKitURIResponse *response);
+
 G_END_DECLS
 
 #endif

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp (118785 => 118786)


--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp	2012-05-29 16:06:16 UTC (rev 118785)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp	2012-05-29 16:09:46 UTC (rev 118786)
@@ -33,9 +33,11 @@
 #include "WebKitPrintOperationPrivate.h"
 #include "WebKitPrivate.h"
 #include "WebKitResourceLoadClient.h"
+#include "WebKitResponsePolicyDecision.h"
 #include "WebKitScriptDialogPrivate.h"
 #include "WebKitSettingsPrivate.h"
 #include "WebKitUIClient.h"
+#include "WebKitURIResponsePrivate.h"
 #include "WebKitWebContextPrivate.h"
 #include "WebKitWebInspectorPrivate.h"
 #include "WebKitWebResourcePrivate.h"
@@ -190,9 +192,25 @@
     return TRUE;
 }
 
-static gboolean webkitWebViewDecidePolicy(WebKitWebView*, WebKitPolicyDecision* decision, WebKitPolicyDecisionType)
+static gboolean webkitWebViewDecidePolicy(WebKitWebView* webView, WebKitPolicyDecision* decision, WebKitPolicyDecisionType decisionType)
 {
-    webkit_policy_decision_use(decision);
+    if (decisionType != WEBKIT_POLICY_DECISION_TYPE_RESPONSE) {
+        webkit_policy_decision_use(decision);
+        return TRUE;
+    }
+
+    WebKitURIResponse* response = webkit_response_policy_decision_get_response(WEBKIT_RESPONSE_POLICY_DECISION(decision));
+    const ResourceResponse resourceResponse = webkitURIResponseGetResourceResponse(response);
+    if (resourceResponse.isAttachment()) {
+        webkit_policy_decision_download(decision);
+        return TRUE;
+    }
+
+    if (webkit_web_view_can_show_mime_type(webView, webkit_uri_response_get_mime_type(response)))
+        webkit_policy_decision_use(decision);
+    else
+        webkit_policy_decision_ignore(decision);
+
     return TRUE;
 }
 
@@ -2026,3 +2044,21 @@
 
     return webView->priv->inspector.get();
 }
+
+/**
+ * webkit_web_view_can_show_mime_type:
+ * @web_view: a #WebKitWebView
+ * @mime_type: a MIME type
+ *
+ * Whether or not a MIME type can be displayed in @web_view.
+ *
+ * Returns: %TRUE if the MIME type @mime_type can be displayed or %FALSE otherwise
+ */
+gboolean webkit_web_view_can_show_mime_type(WebKitWebView* webView, const char* mimeType)
+{
+    g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
+    g_return_val_if_fail(mimeType, FALSE);
+
+    WebPageProxy* page = webkitWebViewBaseGetPage(WEBKIT_WEB_VIEW_BASE(webView));
+    return page->canShowMIMEType(String::fromUTF8(mimeType));
+}

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.h (118785 => 118786)


--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.h	2012-05-29 16:06:16 UTC (rev 118785)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.h	2012-05-29 16:09:46 UTC (rev 118786)
@@ -306,6 +306,10 @@
 WEBKIT_API WebKitWebInspector *
 webkit_web_view_get_inspector                      (WebKitWebView             *web_view);
 
+WEBKIT_API gboolean
+webkit_web_view_can_show_mime_type                 (WebKitWebView             *web_view,
+                                                    const gchar               *mime_type);
+
 G_END_DECLS
 
 #endif

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt (118785 => 118786)


--- trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt	2012-05-29 16:06:16 UTC (rev 118785)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt	2012-05-29 16:09:46 UTC (rev 118786)
@@ -95,6 +95,7 @@
 webkit_web_view_get_javascript_global_context
 webkit_web_view_run_javascript
 webkit_web_view_run_javascript_finish
+webkit_web_view_can_show_mime_type
 
 <SUBSECTION WebKitJavascriptResult>
 WebKitJavascriptResult
@@ -302,6 +303,7 @@
 webkit_uri_response_get_uri
 webkit_uri_response_get_status_code
 webkit_uri_response_get_content_length
+webkit_uri_response_get_mime_type
 
 <SUBSECTION Standard>
 WebKitURIResponseClass

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestResources.cpp (118785 => 118786)


--- trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestResources.cpp	2012-05-29 16:06:16 UTC (rev 118785)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestResources.cpp	2012-05-29 16:09:46 UTC (rev 118786)
@@ -300,13 +300,11 @@
         g_main_loop_run(m_mainLoop);
     }
 
-    int waitUntilResourceLoadFinsihedAndReturnHTTPStatusResponse()
+    WebKitURIResponse* waitUntilResourceLoadFinsihedAndReturnURIResponse()
     {
         waitUntilResourceLoadFinsihed();
         g_assert(m_resource);
-        WebKitURIResponse* response = webkit_web_resource_get_response(m_resource.get());
-        g_assert(response);
-        return webkit_uri_response_get_status_code(response);
+        return webkit_web_resource_get_response(m_resource.get());
     }
 
     GRefPtr<WebKitWebResource> m_resource;
@@ -355,35 +353,50 @@
 {
     // No cached resource: First load.
     test->loadURI(kServer->getURIForPath("/_javascript_.html").data());
-    gint statusCode = test->waitUntilResourceLoadFinsihedAndReturnHTTPStatusResponse();
-    g_assert_cmpint(statusCode, ==, SOUP_STATUS_OK);
+    WebKitURIResponse* response = test->waitUntilResourceLoadFinsihedAndReturnURIResponse();
+    g_assert_cmpint(webkit_uri_response_get_status_code(response), ==, SOUP_STATUS_OK);
 
     // No cached resource: Second load.
     test->loadURI(kServer->getURIForPath("/_javascript_.html").data());
-    statusCode = test->waitUntilResourceLoadFinsihedAndReturnHTTPStatusResponse();
-    g_assert_cmpint(statusCode, ==, SOUP_STATUS_OK);
+    response = test->waitUntilResourceLoadFinsihedAndReturnURIResponse();
+    g_assert_cmpint(webkit_uri_response_get_status_code(response), ==, SOUP_STATUS_OK);
 
     // No cached resource: Reload.
     webkit_web_view_reload(test->m_webView);
-    statusCode = test->waitUntilResourceLoadFinsihedAndReturnHTTPStatusResponse();
-    g_assert_cmpint(statusCode, ==, SOUP_STATUS_OK);
+    response = test->waitUntilResourceLoadFinsihedAndReturnURIResponse();
+    g_assert_cmpint(webkit_uri_response_get_status_code(response), ==, SOUP_STATUS_OK);
 
     // Cached resource: First load.
     test->loadURI(kServer->getURIForPath("/image.html").data());
-    statusCode = test->waitUntilResourceLoadFinsihedAndReturnHTTPStatusResponse();
-    g_assert_cmpint(statusCode, ==, SOUP_STATUS_OK);
+    response = test->waitUntilResourceLoadFinsihedAndReturnURIResponse();
+    g_assert_cmpint(webkit_uri_response_get_status_code(response), ==, SOUP_STATUS_OK);
 
     // Cached resource: Second load.
     test->loadURI(kServer->getURIForPath("/image.html").data());
-    statusCode = test->waitUntilResourceLoadFinsihedAndReturnHTTPStatusResponse();
-    g_assert_cmpint(statusCode, ==, SOUP_STATUS_OK);
+    response = test->waitUntilResourceLoadFinsihedAndReturnURIResponse();
+    g_assert_cmpint(webkit_uri_response_get_status_code(response), ==, SOUP_STATUS_OK);
 
     // Cached resource: Reload.
     webkit_web_view_reload(test->m_webView);
-    statusCode = test->waitUntilResourceLoadFinsihedAndReturnHTTPStatusResponse();
-    g_assert_cmpint(statusCode, ==, SOUP_STATUS_NOT_MODIFIED);
+    response = test->waitUntilResourceLoadFinsihedAndReturnURIResponse();
+    g_assert_cmpint(webkit_uri_response_get_status_code(response), ==, SOUP_STATUS_NOT_MODIFIED);
 }
 
+static void testWebResourceMimeType(SingleResourceLoadTest* test, gconstpointer)
+{
+    test->loadURI(kServer->getURIForPath("/_javascript_.html").data());
+    WebKitURIResponse* response = test->waitUntilResourceLoadFinsihedAndReturnURIResponse();
+    g_assert_cmpstr(webkit_uri_response_get_mime_type(response), ==, "text/_javascript_");
+
+    test->loadURI(kServer->getURIForPath("/image.html").data());
+    response = test->waitUntilResourceLoadFinsihedAndReturnURIResponse();
+    g_assert_cmpstr(webkit_uri_response_get_mime_type(response), ==, "image/vnd.microsoft.icon");
+
+    test->loadURI(kServer->getURIForPath("/redirected-css.html").data());
+    response = test->waitUntilResourceLoadFinsihedAndReturnURIResponse();
+    g_assert_cmpstr(webkit_uri_response_get_mime_type(response), ==, "text/css");
+}
+
 class ResourceURITrackingTest: public SingleResourceLoadTest {
 public:
     MAKE_GLIB_TEST_FIXTURE(ResourceURITrackingTest);
@@ -555,6 +568,7 @@
         addCacheHTTPHeadersToResponse(message);
     } else if (g_str_equal(path, "/_javascript_.js")) {
         soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, kJavascript, strlen(kJavascript));
+        soup_message_headers_append(message->response_headers, "Content-Type", "text/_javascript_");
     } else if (g_str_equal(path, "/blank.ico")) {
         GOwnPtr<char> filePath(g_build_filename(Test::getWebKit1TestResoucesDir().data(), path, NULL));
         char* contents;
@@ -569,6 +583,7 @@
             "    padding: 0px;"
             "}";
         soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, simpleCSS, strlen(simpleCSS));
+        soup_message_headers_append(message->response_headers, "Content-Type", "text/css");
     } else if (g_str_equal(path, "/redirected.css")) {
         soup_message_set_status(message, SOUP_STATUS_MOVED_PERMANENTLY);
         soup_message_headers_append(message->response_headers, "Location", "/simple-style.css");
@@ -585,6 +600,7 @@
     ResourcesTest::add("WebKitWebView", "resources", testWebViewResources);
     SingleResourceLoadTest::add("WebKitWebResource", "loading", testWebResourceLoading);
     SingleResourceLoadTest::add("WebKitWebResource", "response", testWebResourceResponse);
+    SingleResourceLoadTest::add("WebKitWebResource", "mime-type", testWebResourceMimeType);
     ResourceURITrackingTest::add("WebKitWebResource", "active-uri", testWebResourceActiveURI);
     ResourcesTest::add("WebKitWebResource", "get-data", testWebResourceGetData);
     ResourcesTest::add("WebKitWebView", "replaced-content", testWebViewResourcesReplacedContent);

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebView.cpp (118785 => 118786)


--- trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebView.cpp	2012-05-29 16:06:16 UTC (rev 118785)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebView.cpp	2012-05-29 16:09:46 UTC (rev 118786)
@@ -706,6 +706,23 @@
     g_assert_cmpint(test->m_event, ==, FullScreenClientTest::Leave);
 }
 
+static void testWebViewCanShowMIMEType(WebViewTest* test, gconstpointer)
+{
+    // Supported MIME types.
+    g_assert(webkit_web_view_can_show_mime_type(test->m_webView, "text/html"));
+    g_assert(webkit_web_view_can_show_mime_type(test->m_webView, "text/plain"));
+    g_assert(webkit_web_view_can_show_mime_type(test->m_webView, "image/jpeg"));
+    g_assert(webkit_web_view_can_show_mime_type(test->m_webView, "audio/ogg"));
+    g_assert(webkit_web_view_can_show_mime_type(test->m_webView, "video/ogg"));
+    g_assert(webkit_web_view_can_show_mime_type(test->m_webView, "application/x-shockwave-flash"));
+
+    // Unsupported MIME types.
+    g_assert(!webkit_web_view_can_show_mime_type(test->m_webView, "text/vcard"));
+    g_assert(!webkit_web_view_can_show_mime_type(test->m_webView, "application/pdf"));
+    g_assert(!webkit_web_view_can_show_mime_type(test->m_webView, "application/zip"));
+    g_assert(!webkit_web_view_can_show_mime_type(test->m_webView, "application/octet-stream"));
+}
+
 void beforeAll()
 {
     WebViewTest::add("WebKitWebView", "default-context", testWebViewDefaultContext);
@@ -720,6 +737,7 @@
     WebViewTest::add("WebKitWebView", "run-_javascript_", testWebViewRunJavaScript);
     FileChooserTest::add("WebKitWebView", "file-chooser-request", testWebViewFileChooserRequest);
     FullScreenClientTest::add("WebKitWebView", "fullscreen", testWebViewFullScreen);
+    WebViewTest::add("WebKitWebView", "can-show-mime-type", testWebViewCanShowMIMEType);
 }
 
 void afterAll()
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to