Changeset 119700 in webkit


Ignore:
Timestamp:
Jun 7, 2012 3:16:03 AM (12 years ago)
Author:
Carlos Garcia Campos
Message:

[GTK] Add API to register custom URI schemes to WebKit2 GTK+ API
https://bugs.webkit.org/show_bug.cgi?id=84133

Reviewed by Martin Robinson.

  • GNUmakefile.list.am: Add new files to compilation.
  • UIProcess/API/gtk/WebKitPrivate.h:
  • UIProcess/API/gtk/WebKitRequestManagerClient.cpp: Added.

(didReceiveURIRequest): Callback emitted by the C API when a
request for a custom URI scheme has been received. Call
webkitWebContextReceivedURIRequest() to handle the request.
(didFailToLoadURIRequest): Callback emitted by the C API when a
request for a custom URI scheme failed to load. Call
webkitWebContextDidFailToLoadURIRequest() to noitfy the web
context.
(attachRequestManagerClientToContext): Initialize the
WKSoupRequestManagerClient.

  • UIProcess/API/gtk/WebKitRequestManagerClient.h: Added.
  • UIProcess/API/gtk/WebKitURISchemeRequest.cpp: Added.

(webkit_uri_scheme_request_init):
(webkitURISchemeRequestFinalize):
(webkit_uri_scheme_request_class_init):
(webkitURISchemeRequestCreate): Create a new
WebKitURISchemeRequest for the given URI and request identifier.
(webkitURISchemeRequestGetID): Return the request identifier.
(webkitURISchemeRequestCancel): Cancel the request operation.
(webkit_uri_scheme_request_get_scheme): Return the scheme of the
request.
(webkit_uri_scheme_request_get_uri): Return the full URI of the
request.
(webkit_uri_scheme_request_get_path): Return the path of the URI.
(webkitURISchemeRequestReadCallback): Read callback emitted when
reading the contents of the input stream.
(webkit_uri_scheme_request_finish): Finish the request by reading
the contents of the input stream and sending the data to the
WebProcess.

  • UIProcess/API/gtk/WebKitURISchemeRequest.h: Added.
  • UIProcess/API/gtk/WebKitURISchemeRequestPrivate.h: Added.
  • UIProcess/API/gtk/WebKitWebContext.cpp:

(WebKitURISchemeHandler::WebKitURISchemeHandler): Helper struct to
save callback and user data of registered URI schemes.
(createDefaultWebContext): Initialize the SoupRequestManager.
(webkit_web_context_register_uri_scheme): Public API to register a
custom URI scheme providing a callback that will be called to
handle requests for that URI scheme.
(webkitWebContextGetRequestManager): Helper private method to
return the SoupRequestManager.
(webkitWebContextReceivedURIRequest): Get the
WebKitURISchemeHandler for the given request identifier and call
the callback. Save the request in the request map.
(webkitWebContextDidFailToLoadURIRequest): Cancel the ongoing
request.
(webkitWebContextDidFinishURIRequest): Remove the request from the
map.

  • UIProcess/API/gtk/WebKitWebContext.h:
  • UIProcess/API/gtk/WebKitWebContextPrivate.h:
  • UIProcess/API/gtk/docs/webkit2gtk-docs.sgml: Add

WebKitURISchemeRequest section.

  • UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Add new symbols.
  • UIProcess/API/gtk/docs/webkit2gtk.types: Add

webkit_uri_scheme_request_get_type.

  • UIProcess/API/gtk/tests/TestWebKitWebContext.cpp:

(testWebContextURIScheme):
(beforeAll):

  • UIProcess/API/gtk/webkit2.h: Include WebKitURISchemeRequest.h.
Location:
trunk/Source/WebKit2
Files:
5 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r119697 r119700  
     12012-06-07  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        [GTK] Add API to register custom URI schemes to WebKit2 GTK+ API
     4        https://bugs.webkit.org/show_bug.cgi?id=84133
     5
     6        Reviewed by Martin Robinson.
     7
     8        * GNUmakefile.list.am: Add new files to compilation.
     9        * UIProcess/API/gtk/WebKitPrivate.h:
     10        * UIProcess/API/gtk/WebKitRequestManagerClient.cpp: Added.
     11        (didReceiveURIRequest): Callback emitted by the C API when a
     12        request for a custom URI scheme has been received. Call
     13        webkitWebContextReceivedURIRequest() to handle the request.
     14        (didFailToLoadURIRequest): Callback emitted by the C API when a
     15        request for a custom URI scheme failed to load. Call
     16        webkitWebContextDidFailToLoadURIRequest() to noitfy the web
     17        context.
     18        (attachRequestManagerClientToContext): Initialize the
     19        WKSoupRequestManagerClient.
     20        * UIProcess/API/gtk/WebKitRequestManagerClient.h: Added.
     21        * UIProcess/API/gtk/WebKitURISchemeRequest.cpp: Added.
     22        (webkit_uri_scheme_request_init):
     23        (webkitURISchemeRequestFinalize):
     24        (webkit_uri_scheme_request_class_init):
     25        (webkitURISchemeRequestCreate): Create a new
     26        WebKitURISchemeRequest for the given URI and request identifier.
     27        (webkitURISchemeRequestGetID): Return the request identifier.
     28        (webkitURISchemeRequestCancel): Cancel the request operation.
     29        (webkit_uri_scheme_request_get_scheme): Return the scheme of the
     30        request.
     31        (webkit_uri_scheme_request_get_uri): Return the full URI of the
     32        request.
     33        (webkit_uri_scheme_request_get_path): Return the path of the URI.
     34        (webkitURISchemeRequestReadCallback): Read callback emitted when
     35        reading the contents of the input stream.
     36        (webkit_uri_scheme_request_finish): Finish the request by reading
     37        the contents of the input stream and sending the data to the
     38        WebProcess.
     39        * UIProcess/API/gtk/WebKitURISchemeRequest.h: Added.
     40        * UIProcess/API/gtk/WebKitURISchemeRequestPrivate.h: Added.
     41        * UIProcess/API/gtk/WebKitWebContext.cpp:
     42        (WebKitURISchemeHandler::WebKitURISchemeHandler): Helper struct to
     43        save callback and user data of registered URI schemes.
     44        (createDefaultWebContext): Initialize the SoupRequestManager.
     45        (webkit_web_context_register_uri_scheme): Public API to register a
     46        custom URI scheme providing a callback that will be called to
     47        handle requests for that URI scheme.
     48        (webkitWebContextGetRequestManager): Helper private method to
     49        return the SoupRequestManager.
     50        (webkitWebContextReceivedURIRequest): Get the
     51        WebKitURISchemeHandler for the given request identifier and call
     52        the callback. Save the request in the request map.
     53        (webkitWebContextDidFailToLoadURIRequest): Cancel the ongoing
     54        request.
     55        (webkitWebContextDidFinishURIRequest): Remove the request from the
     56        map.
     57        * UIProcess/API/gtk/WebKitWebContext.h:
     58        * UIProcess/API/gtk/WebKitWebContextPrivate.h:
     59        * UIProcess/API/gtk/docs/webkit2gtk-docs.sgml: Add
     60        WebKitURISchemeRequest section.
     61        * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Add new symbols.
     62        * UIProcess/API/gtk/docs/webkit2gtk.types: Add
     63        webkit_uri_scheme_request_get_type.
     64        * UIProcess/API/gtk/tests/TestWebKitWebContext.cpp:
     65        (testWebContextURIScheme):
     66        (beforeAll):
     67        * UIProcess/API/gtk/webkit2.h: Include WebKitURISchemeRequest.h.
     68
    1692012-06-07  Andras Becsi  <andras.becsi@nokia.com>
    270
  • trunk/Source/WebKit2/GNUmakefile.list.am

    r119475 r119700  
    110110        $(WebKit2)/UIProcess/API/gtk/WebKitURIRequest.h \
    111111        $(WebKit2)/UIProcess/API/gtk/WebKitURIResponse.h \
     112        $(WebKit2)/UIProcess/API/gtk/WebKitURISchemeRequest.h \
    112113        $(WebKit2)/UIProcess/API/gtk/WebKitWebContext.h \
    113114        $(WebKit2)/UIProcess/API/gtk/WebKitWebInspector.h \
     
    623624        Source/WebKit2/UIProcess/API/gtk/WebKitPrivate.h \
    624625        Source/WebKit2/UIProcess/API/gtk/WebKitPrivate.cpp \
     626        Source/WebKit2/UIProcess/API/gtk/WebKitRequestManagerClient.cpp \
     627        Source/WebKit2/UIProcess/API/gtk/WebKitRequestManagerClient.h \
    625628        Source/WebKit2/UIProcess/API/gtk/WebKitResourceLoadClient.cpp \
    626629        Source/WebKit2/UIProcess/API/gtk/WebKitResourceLoadClient.h \
     
    642645        Source/WebKit2/UIProcess/API/gtk/WebKitURIResponse.h \
    643646        Source/WebKit2/UIProcess/API/gtk/WebKitURIResponsePrivate.h \
     647        Source/WebKit2/UIProcess/API/gtk/WebKitURISchemeRequest.cpp \
     648        Source/WebKit2/UIProcess/API/gtk/WebKitURISchemeRequest.h \
     649        Source/WebKit2/UIProcess/API/gtk/WebKitURISchemeRequestPrivate.h \
    644650        Source/WebKit2/UIProcess/API/gtk/WebKitWebContext.h \
    645651        Source/WebKit2/UIProcess/API/gtk/WebKitWebContext.cpp \
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitPrivate.h

    r119475 r119700  
    2828
    2929#include <WebKit2/WKAPICast.h>
     30#include <WebKit2/WKContextSoup.h>
    3031#include <WebKit2/WKDownload.h>
    3132#include <WebKit2/WKFindOptions.h>
     
    3839#include <WebKit2/WKRetainPtr.h>
    3940#include <WebKit2/WKSerializedScriptValue.h>
     41#include <WebKit2/WKSoupRequestManager.h>
    4042#include <WebKit2/WKString.h>
    4143#include <WebKit2/WebKit2.h>
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebContext.cpp

    r119404 r119700  
    2828#include "WebKitPluginPrivate.h"
    2929#include "WebKitPrivate.h"
     30#include "WebKitRequestManagerClient.h"
     31#include "WebKitURISchemeRequestPrivate.h"
    3032#include "WebKitWebContextPrivate.h"
    3133#include <WebCore/FileSystem.h>
     
    4244};
    4345
     46struct WebKitURISchemeHandler {
     47    WebKitURISchemeHandler()
     48        : callback(0)
     49        , userData(0)
     50    {
     51    }
     52    WebKitURISchemeHandler(WebKitURISchemeRequestCallback callback, void* userData)
     53        : callback(callback)
     54        , userData(userData)
     55    {
     56    }
     57
     58    WebKitURISchemeRequestCallback callback;
     59    void* userData;
     60};
     61
     62typedef HashMap<String, WebKitURISchemeHandler> URISchemeHandlerMap;
     63typedef HashMap<uint64_t, GRefPtr<WebKitURISchemeRequest> > URISchemeRequestMap;
     64
    4465struct _WebKitWebContextPrivate {
    4566    WKRetainPtr<WKContextRef> context;
    4667
    4768    GRefPtr<WebKitCookieManager> cookieManager;
     69    WKRetainPtr<WKSoupRequestManagerRef> requestManager;
     70    URISchemeHandlerMap uriSchemeHandlers;
     71    URISchemeRequestMap uriSchemeRequests;
    4872#if ENABLE(GEOLOCATION)
    4973    RefPtr<WebKitGeolocationProvider> geolocationProvider;
     
    96120    static GRefPtr<WebKitWebContext> webContext = adoptGRef(WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, NULL)));
    97121    webContext->priv->context = WKContextGetSharedProcessContext();
     122    webContext->priv->requestManager = WKContextGetSoupRequestManager(webContext->priv->context.get());
    98123    WKContextSetCacheModel(webContext->priv->context.get(), kWKCacheModelPrimaryWebBrowser);
    99124    attachDownloadClientToContext(webContext.get());
     125    attachRequestManagerClientToContext(webContext.get());
    100126#if ENABLE(GEOLOCATION)
    101127    WKGeolocationManagerRef wkGeolocationManager = WKContextGetGeolocationManager(webContext->priv->context.get());
     
    326352}
    327353
     354/**
     355 * webkit_web_context_register_uri_scheme:
     356 * @context: a #WebKitWebContext
     357 * @scheme: the network scheme to register
     358 * @callback: a #WebKitURISchemeRequestCallback
     359 * @user_data: data to pass to callback function
     360 *
     361 * Register @scheme in @context, so that when an URI request with @scheme is made in the
     362 * #WebKitWebContext, the #WebKitURISchemeRequestCallback registered will be called with a
     363 * #WebKitURISchemeRequest.
     364 * It is possible to handle URI scheme requests asynchronously, by calling g_object_ref() on the
     365 * #WebKitURISchemeRequest and calling webkit_uri_scheme_request_finish() later when the data of
     366 * the request is available.
     367 *
     368 * <informalexample><programlisting>
     369 * static void
     370 * about_uri_scheme_request_cb (WebKitURISchemeRequest *request,
     371 *                              gpointer                user_data)
     372 * {
     373 *     GInputStream *stream;
     374 *     gsize         stream_length;
     375 *     const gchar  *path;
     376 *
     377 *     path = webkit_uri_scheme_request_get_path (request);
     378 *     if (!g_strcmp0 (path, "plugins")) {
     379 *         /<!-- -->* Create a GInputStream with the contents of plugins about page, and set its length to stream_length *<!-- -->/
     380 *     } else if (!g_strcmp0 (path, "memory")) {
     381 *         /<!-- -->* Create a GInputStream with the contents of memory about page, and set its length to stream_length *<!-- -->/
     382 *     } else if (!g_strcmp0 (path, "applications")) {
     383 *         /<!-- -->* Create a GInputStream with the contents of applications about page, and set its length to stream_length *<!-- -->/
     384 *     } else {
     385 *         gchar *contents;
     386 *
     387 *         contents = g_strdup_printf ("&lt;html&gt;&lt;body&gt;&lt;p&gt;Invalid about:%s page&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;", path);
     388 *         stream_length = strlen (contents);
     389 *         stream = g_memory_input_stream_new_from_data (contents, stream_length, g_free);
     390 *     }
     391 *     webkit_uri_scheme_request_finish (request, stream, stream_length, "text/html");
     392 *     g_object_unref (stream);
     393 * }
     394 * </programlisting></informalexample>
     395 */
     396void webkit_web_context_register_uri_scheme(WebKitWebContext* context, const char* scheme, WebKitURISchemeRequestCallback callback, gpointer userData)
     397{
     398    g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
     399    g_return_if_fail(scheme);
     400    g_return_if_fail(callback);
     401
     402    context->priv->uriSchemeHandlers.set(String::fromUTF8(scheme), WebKitURISchemeHandler(callback, userData));
     403    WKRetainPtr<WKStringRef> wkScheme(AdoptWK, WKStringCreateWithUTF8CString(scheme));
     404    WKSoupRequestManagerRegisterURIScheme(context->priv->requestManager.get(), wkScheme.get());
     405}
     406
    328407WebKitDownload* webkitWebContextGetOrCreateDownload(WKDownloadRef wkDownload)
    329408{
     
    354433}
    355434
     435WKSoupRequestManagerRef webkitWebContextGetRequestManager(WebKitWebContext* context)
     436{
     437    return context->priv->requestManager.get();
     438}
     439
     440void webkitWebContextReceivedURIRequest(WebKitWebContext* context, WebKitURISchemeRequest* request)
     441{
     442    WebKitURISchemeHandler handler = context->priv->uriSchemeHandlers.get(webkit_uri_scheme_request_get_scheme(request));
     443    if (!handler.callback)
     444        return;
     445
     446    context->priv->uriSchemeRequests.set(webkitURISchemeRequestGetID(request), request);
     447    handler.callback(request, handler.userData);
     448}
     449
     450void webkitWebContextDidFailToLoadURIRequest(WebKitWebContext* context, uint64_t requestID)
     451{
     452    GRefPtr<WebKitURISchemeRequest> request = context->priv->uriSchemeRequests.get(requestID);
     453    if (!request.get())
     454        return;
     455    webkitURISchemeRequestCancel(request.get());
     456}
     457
     458void webkitWebContextDidFinishURIRequest(WebKitWebContext* context, uint64_t requestID)
     459{
     460    context->priv->uriSchemeRequests.remove(requestID);
     461}
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebContext.h

    r117305 r119700  
    2929#include <webkit2/WebKitDefines.h>
    3030#include <webkit2/WebKitDownload.h>
     31#include <webkit2/WebKitURISchemeRequest.h>
    3132
    3233G_BEGIN_DECLS
     
    5960} WebKitCacheModel;
    6061
     62/**
     63 * WebKitURISchemeRequestCallback:
     64 * @request: the #WebKitURISchemeRequest
     65 * @user_data: user data passed to the callback
     66 *
     67 * Type definition for a function that will be called back when an URI request is
     68 * made for a user registered URI scheme.
     69 */
     70typedef void (* WebKitURISchemeRequestCallback) (WebKitURISchemeRequest *request,
     71                                                 gpointer                user_data);
     72
    6173typedef struct _WebKitWebContext        WebKitWebContext;
    6274typedef struct _WebKitWebContextClass   WebKitWebContextClass;
     
    8799
    88100WEBKIT_API void
    89 webkit_web_context_set_cache_model                  (WebKitWebContext   *context,
    90                                                      WebKitCacheModel    cache_model);
     101webkit_web_context_set_cache_model                  (WebKitWebContext              *context,
     102                                                     WebKitCacheModel               cache_model);
    91103WEBKIT_API WebKitCacheModel
    92 webkit_web_context_get_cache_model                  (WebKitWebContext   *context);
     104webkit_web_context_get_cache_model                  (WebKitWebContext              *context);
    93105
    94106WEBKIT_API WebKitDownload *
    95 webkit_web_context_download_uri                     (WebKitWebContext   *context,
    96                                                      const gchar        *uri);
     107webkit_web_context_download_uri                     (WebKitWebContext              *context,
     108                                                     const gchar                   *uri);
    97109
    98110WEBKIT_API WebKitCookieManager *
    99 webkit_web_context_get_cookie_manager               (WebKitWebContext   *context);
     111webkit_web_context_get_cookie_manager               (WebKitWebContext              *context);
    100112
    101113WEBKIT_API void
    102 webkit_web_context_set_additional_plugins_directory (WebKitWebContext   *context,
    103                                                      const gchar        *directory);
     114webkit_web_context_set_additional_plugins_directory (WebKitWebContext              *context,
     115                                                     const gchar                   *directory);
    104116
    105117WEBKIT_API void
    106 webkit_web_context_get_plugins                      (WebKitWebContext   *context,
    107                                                      GCancellable       *cancellable,
    108                                                      GAsyncReadyCallback callback,
    109                                                      gpointer            user_data);
     118webkit_web_context_get_plugins                      (WebKitWebContext              *context,
     119                                                     GCancellable                  *cancellable,
     120                                                     GAsyncReadyCallback            callback,
     121                                                     gpointer                       user_data);
    110122
    111123WEBKIT_API GList *
    112 webkit_web_context_get_plugins_finish               (WebKitWebContext   *context,
    113                                                      GAsyncResult       *result,
    114                                                      GError            **error);
    115 
     124webkit_web_context_get_plugins_finish               (WebKitWebContext              *context,
     125                                                     GAsyncResult                  *result,
     126                                                     GError                       **error);
     127WEBKIT_API void
     128webkit_web_context_register_uri_scheme              (WebKitWebContext              *context,
     129                                                     const gchar                   *scheme,
     130                                                     WebKitURISchemeRequestCallback callback,
     131                                                     gpointer                       user_data);
    116132G_END_DECLS
    117133
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebContextPrivate.h

    r105708 r119700  
    2727#define WebKitWebContextPrivate_h
    2828
     29#include "WebKitPrivate.h"
     30#include "WebKitURISchemeRequest.h"
    2931#include "WebKitWebContext.h"
    30 #include <WebKit2/WebKit2.h>
    31 
    32 G_BEGIN_DECLS
    3332
    3433WKContextRef webkitWebContextGetWKContext(WebKitWebContext*);
     
    3635void webkitWebContextRemoveDownload(WKDownloadRef);
    3736void webkitWebContextDownloadStarted(WebKitWebContext*, WebKitDownload*);
    38 
    39 G_END_DECLS
     37WKSoupRequestManagerRef webkitWebContextGetRequestManager(WebKitWebContext*);
     38void webkitWebContextReceivedURIRequest(WebKitWebContext*, WebKitURISchemeRequest*);
     39void webkitWebContextDidFailToLoadURIRequest(WebKitWebContext*, uint64_t requestID);
     40void webkitWebContextDidFinishURIRequest(WebKitWebContext*, uint64_t requestID);
    4041
    4142#endif // WebKitWebContextPrivate_h
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-docs.sgml

    r119475 r119700  
    3636    <xi:include href="xml/WebKitPlugin.xml"/>
    3737    <xi:include href="xml/WebKitWebInspector.xml"/>
     38    <xi:include href="xml/WebKitURISchemeRequest.xml"/>
    3839  </chapter>
    3940
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt

    r119493 r119700  
    3333webkit_web_context_get_plugins
    3434webkit_web_context_get_plugins_finish
     35
     36<SUBSECTION URI Scheme>
     37WebKitURISchemeRequestCallback
     38webkit_web_context_register_uri_scheme
    3539
    3640<SUBSECTION Standard>
     
    705709WebKitWebInspectorPrivate
    706710</SECTION>
     711
     712<FILE>WebKitURISchemeRequest</FILE>
     713WebKitURISchemeRequest
     714webkit_uri_scheme_request_get_scheme
     715webkit_uri_scheme_request_get_uri
     716webkit_uri_scheme_request_get_path
     717webkit_uri_scheme_request_finish
     718
     719<SUBSECTION Standard>
     720WebKitURISchemeRequestClass
     721WEBKIT_TYPE_URI_SCHEME_REQUEST
     722WEBKIT_URI_SCHEME_REQUEST
     723WEBKIT_IS_URI_SCHEME_REQUEST
     724WEBKIT_URI_SCHEME_REQUEST_CLASS
     725WEBKIT_IS_URI_SCHEME_REQUEST_CLASS
     726WEBKIT_URI_SCHEME_REQUEST_GET_CLASS
     727
     728<SUBSECTION Private>
     729WebKitURISchemeRequestPrivate
     730webkit_uri_scheme_request_get_type
     731</SECTION>
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk.types

    r118146 r119700  
    1919webkit_mime_info_get_type
    2020webkit_web_inspector_get_type
     21webkit_uri_scheme_request_get_type
  • trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebContext.cpp

    r117305 r119700  
    2020#include "config.h"
    2121
    22 #include "TestMain.h"
     22#include "LoadTrackingTest.h"
    2323#include <gtk/gtk.h>
    2424#include <webkit2/webkit2.h>
     25#include <wtf/HashMap.h>
     26#include <wtf/gobject/GOwnPtr.h>
    2527#include <wtf/gobject/GRefPtr.h>
     28#include <wtf/text/StringHash.h>
    2629
    2730static void testWebContextDefault(Test* test, gconstpointer)
     
    107110}
    108111
     112static const char* kBarHTML = "<html><body>Bar</body></html>";
     113static const char* kEchoHTMLFormat = "<html><body>%s</body></html>";
     114
     115class URISchemeTest: public LoadTrackingTest {
     116public:
     117    MAKE_GLIB_TEST_FIXTURE(URISchemeTest);
     118
     119    struct URISchemeHandler {
     120        URISchemeHandler()
     121            : replyLength(0)
     122            , replyWithPath(false)
     123        {
     124        }
     125
     126        URISchemeHandler(const char* reply, int replyLength, const char* mimeType, bool replyWithPath = false)
     127            : reply(reply)
     128            , replyLength(replyLength)
     129            , mimeType(mimeType)
     130            , replyWithPath(replyWithPath)
     131        {
     132        }
     133
     134        CString reply;
     135        int replyLength;
     136        CString mimeType;
     137        bool replyWithPath;
     138    };
     139
     140    static void uriSchemeRequestCallback(WebKitURISchemeRequest* request, gpointer userData)
     141    {
     142        URISchemeTest* test = static_cast<URISchemeTest*>(userData);
     143        test->m_uriSchemeRequest = request;
     144        test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(request));
     145
     146        GRefPtr<GInputStream> inputStream = adoptGRef(g_memory_input_stream_new());
     147        test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(inputStream.get()));
     148
     149        String scheme(String::fromUTF8(webkit_uri_scheme_request_get_scheme(request)));
     150        g_assert(!scheme.isEmpty());
     151        g_assert(test->m_handlersMap.contains(scheme));
     152        const URISchemeHandler& handler = test->m_handlersMap.get(scheme);
     153
     154        if (handler.replyWithPath) {
     155            char* replyHTML = g_strdup_printf(handler.reply.data(), webkit_uri_scheme_request_get_path(request));
     156            g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(inputStream.get()), replyHTML, strlen(replyHTML), g_free);
     157        } else if (!handler.reply.isNull())
     158            g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(inputStream.get()), handler.reply.data(), handler.reply.length(), 0);
     159        webkit_uri_scheme_request_finish(request, inputStream.get(), handler.replyLength, handler.mimeType.data());
     160    }
     161
     162    void registerURISchemeHandler(const char* scheme, const char* reply, int replyLength, const char* mimeType, bool replyWithPath = false)
     163    {
     164        m_handlersMap.set(String::fromUTF8(scheme), URISchemeHandler(reply, replyLength, mimeType, replyWithPath));
     165        webkit_web_context_register_uri_scheme(webkit_web_context_get_default(), scheme, uriSchemeRequestCallback, this);
     166    }
     167
     168    static void resourceGetDataCallback(GObject* object, GAsyncResult* result, gpointer userData)
     169    {
     170        size_t dataSize;
     171        GOwnPtr<GError> error;
     172        unsigned char* data = webkit_web_resource_get_data_finish(WEBKIT_WEB_RESOURCE(object), result, &dataSize, &error.outPtr());
     173        g_assert(data);
     174
     175        URISchemeTest* test = static_cast<URISchemeTest*>(userData);
     176        test->m_resourceData.set(reinterpret_cast<char*>(data));
     177        test->m_resourceDataSize = dataSize;
     178        g_main_loop_quit(test->m_mainLoop);
     179    }
     180
     181    const char* mainResourceData(size_t& mainResourceDataSize)
     182    {
     183        m_resourceDataSize = 0;
     184        m_resourceData.clear();
     185        WebKitWebResource* resource = webkit_web_view_get_main_resource(m_webView);
     186        g_assert(resource);
     187
     188        webkit_web_resource_get_data(resource, 0, resourceGetDataCallback, this);
     189        g_main_loop_run(m_mainLoop);
     190
     191        mainResourceDataSize = m_resourceDataSize;
     192        return m_resourceData.get();
     193    }
     194
     195    GOwnPtr<char> m_resourceData;
     196    size_t m_resourceDataSize;
     197    GRefPtr<WebKitURISchemeRequest> m_uriSchemeRequest;
     198    HashMap<String, URISchemeHandler> m_handlersMap;
     199};
     200
     201static void testWebContextURIScheme(URISchemeTest* test, gconstpointer)
     202{
     203    test->registerURISchemeHandler("foo", kBarHTML, strlen(kBarHTML), "text/html");
     204    test->loadURI("foo:blank");
     205    test->waitUntilLoadFinished();
     206    size_t mainResourceDataSize = 0;
     207    const char* mainResourceData = test->mainResourceData(mainResourceDataSize);
     208    g_assert_cmpint(mainResourceDataSize, ==, strlen(kBarHTML));
     209    g_assert(!strncmp(mainResourceData, kBarHTML, mainResourceDataSize));
     210
     211    test->registerURISchemeHandler("echo", kEchoHTMLFormat, -1, "text/html", true);
     212    test->loadURI("echo:hello world");
     213    test->waitUntilLoadFinished();
     214    GOwnPtr<char> echoHTML(g_strdup_printf(kEchoHTMLFormat, webkit_uri_scheme_request_get_path(test->m_uriSchemeRequest.get())));
     215    mainResourceDataSize = 0;
     216    mainResourceData = test->mainResourceData(mainResourceDataSize);
     217    g_assert_cmpint(mainResourceDataSize, ==, strlen(echoHTML.get()));
     218    g_assert(!strncmp(mainResourceData, echoHTML.get(), mainResourceDataSize));
     219
     220    test->registerURISchemeHandler("nomime", kBarHTML, -1, 0);
     221    test->m_loadEvents.clear();
     222    test->loadURI("nomime:foo bar");
     223    test->waitUntilLoadFinished();
     224    g_assert(test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
     225
     226    test->registerURISchemeHandler("empty", 0, 0, "text/html");
     227    test->m_loadEvents.clear();
     228    test->loadURI("empty:nothing");
     229    test->waitUntilLoadFinished();
     230    g_assert(!test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
     231    g_assert(!test->m_loadEvents.contains(LoadTrackingTest::LoadFailed));
     232}
     233
    109234void beforeAll()
    110235{
    111236    Test::add("WebKitWebContext", "default-context", testWebContextDefault);
    112237    PluginsTest::add("WebKitWebContext", "get-plugins", testWebContextGetPlugins);
     238    URISchemeTest::add("WebKitWebContext", "uri-scheme", testWebContextURIScheme);
    113239}
    114240
  • trunk/Source/WebKit2/UIProcess/API/gtk/webkit2.h

    r119475 r119700  
    4747#include <webkit2/WebKitURIRequest.h>
    4848#include <webkit2/WebKitURIResponse.h>
     49#include <webkit2/WebKitURISchemeRequest.h>
    4950#include <webkit2/WebKitWebContext.h>
    5051#include <webkit2/WebKitWebInspector.h>
Note: See TracChangeset for help on using the changeset viewer.