WebKit/gtk/webkit/webkitnetworkrequest.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2007, 2008 Holger Hans Peter Freyther
       
     3  * Copyright (C) 2009 Gustavo Noronha Silva
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Library General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Library General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Library General Public License
       
    16  * along with this library; see the file COPYING.LIB.  If not, write to
       
    17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    18  * Boston, MA 02110-1301, USA.
       
    19  */
       
    20 
       
    21 #include "config.h"
       
    22 #include "webkitnetworkrequest.h"
       
    23 
       
    24 #include "ResourceRequest.h"
       
    25 #include "webkitprivate.h"
       
    26 #include <wtf/text/CString.h>
       
    27 
       
    28 #include <glib/gi18n-lib.h>
       
    29 
       
    30 /**
       
    31  * SECTION:webkitnetworkrequest
       
    32  * @short_description: The target of a navigation request
       
    33  * @see_also: #WebKitWebView::navigation-policy-decision-requested
       
    34  *
       
    35  * This class represents the network related aspects of a navigation
       
    36  * request. It is used whenever WebKit wants to provide information
       
    37  * about a request that will be sent, or has been sent. Inside it you
       
    38  * can find the URI of the request, and, for valid URIs, a
       
    39  * #SoupMessage object, which provides access to further information
       
    40  * such as headers.
       
    41  *
       
    42  */
       
    43 
       
    44 G_DEFINE_TYPE(WebKitNetworkRequest, webkit_network_request, G_TYPE_OBJECT);
       
    45 
       
    46 struct _WebKitNetworkRequestPrivate {
       
    47     gchar* uri;
       
    48     SoupMessage* message;
       
    49 };
       
    50 
       
    51 #define WEBKIT_NETWORK_REQUEST_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_NETWORK_REQUEST, WebKitNetworkRequestPrivate))
       
    52 
       
    53 enum {
       
    54     PROP_0,
       
    55 
       
    56     PROP_URI,
       
    57     PROP_MESSAGE,
       
    58 };
       
    59 
       
    60 static void webkit_network_request_dispose(GObject* object)
       
    61 {
       
    62     WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
       
    63     WebKitNetworkRequestPrivate* priv = request->priv;
       
    64 
       
    65     if (priv->message) {
       
    66         g_object_unref(priv->message);
       
    67         priv->message = NULL;
       
    68     }
       
    69 
       
    70     G_OBJECT_CLASS(webkit_network_request_parent_class)->dispose(object);
       
    71 }
       
    72 
       
    73 static void webkit_network_request_finalize(GObject* object)
       
    74 {
       
    75     WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
       
    76     WebKitNetworkRequestPrivate* priv = request->priv;
       
    77 
       
    78     g_free(priv->uri);
       
    79 
       
    80     G_OBJECT_CLASS(webkit_network_request_parent_class)->finalize(object);
       
    81 }
       
    82 
       
    83 static void webkit_network_request_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec)
       
    84 {
       
    85     WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
       
    86 
       
    87     switch(propertyID) {
       
    88     case PROP_URI:
       
    89         g_value_set_string(value, webkit_network_request_get_uri(request));
       
    90         break;
       
    91     case PROP_MESSAGE:
       
    92         g_value_set_object(value, webkit_network_request_get_message(request));
       
    93         break;
       
    94     default:
       
    95         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
       
    96     }
       
    97 }
       
    98 
       
    99 static void webkit_network_request_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec)
       
   100 {
       
   101     WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
       
   102     WebKitNetworkRequestPrivate* priv = request->priv;
       
   103 
       
   104     switch(propertyID) {
       
   105     case PROP_URI:
       
   106         webkit_network_request_set_uri(request, g_value_get_string(value));
       
   107         break;
       
   108     case PROP_MESSAGE:
       
   109         priv->message = SOUP_MESSAGE(g_value_dup_object(value));
       
   110         break;
       
   111     default:
       
   112         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
       
   113     }
       
   114 }
       
   115 
       
   116 static void webkit_network_request_class_init(WebKitNetworkRequestClass* requestClass)
       
   117 {
       
   118     GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
       
   119 
       
   120     objectClass->dispose = webkit_network_request_dispose;
       
   121     objectClass->finalize = webkit_network_request_finalize;
       
   122     objectClass->get_property = webkit_network_request_get_property;
       
   123     objectClass->set_property = webkit_network_request_set_property;
       
   124 
       
   125     webkit_init();
       
   126 
       
   127     /**
       
   128      * WebKitNetworkRequest:uri:
       
   129      *
       
   130      * The URI to which the request will be made.
       
   131      *
       
   132      * Since: 1.1.10
       
   133      */
       
   134     g_object_class_install_property(objectClass, PROP_URI,
       
   135                                     g_param_spec_string("uri",
       
   136                                                         _("URI"),
       
   137                                                         _("The URI to which the request will be made."),
       
   138                                                         NULL,
       
   139                                                         (GParamFlags)(WEBKIT_PARAM_READWRITE)));
       
   140 
       
   141     /**
       
   142      * WebKitNetworkRequest:message:
       
   143      *
       
   144      * The #SoupMessage that backs the request.
       
   145      *
       
   146      * Since: 1.1.10
       
   147      */
       
   148     g_object_class_install_property(objectClass, PROP_MESSAGE,
       
   149                                     g_param_spec_object("message",
       
   150                                                         _("Message"),
       
   151                                                         _("The SoupMessage that backs the request."),
       
   152                                                         SOUP_TYPE_MESSAGE,
       
   153                                                         (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
       
   154 
       
   155     g_type_class_add_private(requestClass, sizeof(WebKitNetworkRequestPrivate));
       
   156 }
       
   157 
       
   158 static void webkit_network_request_init(WebKitNetworkRequest* request)
       
   159 {
       
   160     WebKitNetworkRequestPrivate* priv = WEBKIT_NETWORK_REQUEST_GET_PRIVATE(request);
       
   161     request->priv = priv;
       
   162 }
       
   163 
       
   164 // for internal use only
       
   165 WebKitNetworkRequest* webkit_network_request_new_with_core_request(const WebCore::ResourceRequest& resourceRequest)
       
   166 {
       
   167     GRefPtr<SoupMessage> soupMessage(adoptGRef(resourceRequest.toSoupMessage()));
       
   168     if (soupMessage)
       
   169         return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "message", soupMessage.get(), NULL));
       
   170 
       
   171     return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", resourceRequest.url().string().utf8().data(), NULL));
       
   172 }
       
   173 
       
   174 /**
       
   175  * webkit_network_request_new:
       
   176  * @uri: an URI
       
   177  *
       
   178  * Creates a new #WebKitNetworkRequest initialized with an URI.
       
   179  *
       
   180  * Returns: a new #WebKitNetworkRequest, or %NULL if the URI is
       
   181  * invalid.
       
   182  */
       
   183 WebKitNetworkRequest* webkit_network_request_new(const gchar* uri)
       
   184 {
       
   185     g_return_val_if_fail(uri, NULL);
       
   186 
       
   187     return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", uri, NULL));
       
   188 }
       
   189 
       
   190 /**
       
   191  * webkit_network_request_set_uri:
       
   192  * @request: a #WebKitNetworkRequest
       
   193  * @uri: an URI
       
   194  *
       
   195  * Sets the URI held and used by the given request. When the request
       
   196  * has an associated #SoupMessage, its URI will also be set by this
       
   197  * call.
       
   198  *
       
   199  */
       
   200 void webkit_network_request_set_uri(WebKitNetworkRequest* request, const gchar* uri)
       
   201 {
       
   202     g_return_if_fail(WEBKIT_IS_NETWORK_REQUEST(request));
       
   203     g_return_if_fail(uri);
       
   204 
       
   205     WebKitNetworkRequestPrivate* priv = request->priv;
       
   206 
       
   207     if (priv->uri)
       
   208         g_free(priv->uri);
       
   209     priv->uri = g_strdup(uri);
       
   210 
       
   211     if (!priv->message)
       
   212         return;
       
   213 
       
   214     SoupURI* soupURI = soup_uri_new(uri);
       
   215     g_return_if_fail(soupURI);
       
   216 
       
   217     soup_message_set_uri(priv->message, soupURI);
       
   218     soup_uri_free(soupURI);
       
   219 }
       
   220 
       
   221 /**
       
   222  * webkit_network_request_get_uri:
       
   223  * @request: a #WebKitNetworkRequest
       
   224  *
       
   225  * Returns: the uri of the #WebKitNetworkRequest
       
   226  *
       
   227  * Since: 1.0.0
       
   228  */
       
   229 G_CONST_RETURN gchar* webkit_network_request_get_uri(WebKitNetworkRequest* request)
       
   230 {
       
   231     g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
       
   232 
       
   233     WebKitNetworkRequestPrivate* priv = request->priv;
       
   234 
       
   235     if (priv->uri)
       
   236         return priv->uri;
       
   237 
       
   238     SoupURI* soupURI = soup_message_get_uri(priv->message);
       
   239     priv->uri = soup_uri_to_string(soupURI, FALSE);
       
   240     return priv->uri;
       
   241 }
       
   242 
       
   243 /**
       
   244  * webkit_network_request_get_soup_message:
       
   245  * @request: a #WebKitNetworkRequest
       
   246  *
       
   247  * Obtains the #SoupMessage held and used by the given request. Notice
       
   248  * that modification of the SoupMessage of a request by signal
       
   249  * handlers is only supported (as in, will only affect what is
       
   250  * actually sent to the server) where explicitly documented.
       
   251  *
       
   252  * Returns: the #SoupMessage
       
   253  * Since: 1.1.9
       
   254  */
       
   255 SoupMessage* webkit_network_request_get_message(WebKitNetworkRequest* request)
       
   256 {
       
   257     g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
       
   258 
       
   259     WebKitNetworkRequestPrivate* priv = request->priv;
       
   260 
       
   261     return priv->message;
       
   262 }