Home | History | Annotate | Download | only in webkit
      1 /*
      2  * Copyright (C) 2007, 2008 Holger Hans Peter Freyther
      3  * Copyright (C) 2009 Gustavo Noronha Silva
      4  * Copyright (C) 2009 Collabora Ltd.
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Library General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Library General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Library General Public License
     17  * along with this library; see the file COPYING.LIB.  If not, write to
     18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19  * Boston, MA 02110-1301, USA.
     20  */
     21 
     22 #include "config.h"
     23 #include "webkitnetworkresponse.h"
     24 
     25 #include "GRefPtr.h"
     26 #include "ResourceResponse.h"
     27 #include "webkitglobalsprivate.h"
     28 #include <glib/gi18n-lib.h>
     29 #include <wtf/text/CString.h>
     30 
     31 /**
     32  * SECTION:webkitnetworkresponse
     33  * @short_description: the response given to a network request
     34  * @see_also: #WebKitNetworkRequest
     35  *
     36  * This class represents the network related aspects of a navigation
     37  * response.
     38  *
     39  * Since: 1.1.14
     40  */
     41 
     42 G_DEFINE_TYPE(WebKitNetworkResponse, webkit_network_response, G_TYPE_OBJECT);
     43 
     44 struct _WebKitNetworkResponsePrivate {
     45     gchar* uri;
     46     SoupMessage* message;
     47 };
     48 
     49 #define WEBKIT_NETWORK_RESPONSE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_NETWORK_RESPONSE, WebKitNetworkResponsePrivate))
     50 
     51 enum {
     52     PROP_0,
     53 
     54     PROP_URI,
     55     PROP_MESSAGE,
     56 };
     57 
     58 static void webkit_network_response_dispose(GObject* object)
     59 {
     60     WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object);
     61     WebKitNetworkResponsePrivate* priv = response->priv;
     62 
     63     if (priv->message) {
     64         g_object_unref(priv->message);
     65         priv->message = NULL;
     66     }
     67 
     68     G_OBJECT_CLASS(webkit_network_response_parent_class)->dispose(object);
     69 }
     70 
     71 static void webkit_network_response_finalize(GObject* object)
     72 {
     73     WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object);
     74     WebKitNetworkResponsePrivate* priv = response->priv;
     75 
     76     g_free(priv->uri);
     77 
     78     G_OBJECT_CLASS(webkit_network_response_parent_class)->finalize(object);
     79 }
     80 
     81 static void webkit_network_response_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec)
     82 {
     83     WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object);
     84 
     85     switch(propertyID) {
     86     case PROP_URI:
     87         g_value_set_string(value, webkit_network_response_get_uri(response));
     88         break;
     89     case PROP_MESSAGE:
     90         g_value_set_object(value, webkit_network_response_get_message(response));
     91         break;
     92     default:
     93         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
     94     }
     95 }
     96 
     97 static void webkit_network_response_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec)
     98 {
     99     WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object);
    100     WebKitNetworkResponsePrivate* priv = response->priv;
    101 
    102     switch(propertyID) {
    103     case PROP_URI:
    104         webkit_network_response_set_uri(response, g_value_get_string(value));
    105         break;
    106     case PROP_MESSAGE:
    107         priv->message = SOUP_MESSAGE(g_value_dup_object(value));
    108         break;
    109     default:
    110         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
    111     }
    112 }
    113 
    114 static void webkit_network_response_class_init(WebKitNetworkResponseClass* responseClass)
    115 {
    116     GObjectClass* objectClass = G_OBJECT_CLASS(responseClass);
    117 
    118     objectClass->dispose = webkit_network_response_dispose;
    119     objectClass->finalize = webkit_network_response_finalize;
    120     objectClass->get_property = webkit_network_response_get_property;
    121     objectClass->set_property = webkit_network_response_set_property;
    122 
    123     webkitInit();
    124 
    125     /**
    126      * WebKitNetworkResponse:uri:
    127      *
    128      * The URI to which the response will be made.
    129      *
    130      * Since: 1.1.14
    131      */
    132     g_object_class_install_property(objectClass, PROP_URI,
    133                                     g_param_spec_string("uri",
    134                                                         _("URI"),
    135                                                         _("The URI to which the response will be made."),
    136                                                         NULL,
    137                                                         (GParamFlags)(WEBKIT_PARAM_READWRITE)));
    138 
    139     /**
    140      * WebKitNetworkResponse:message:
    141      *
    142      * The #SoupMessage that backs the response.
    143      *
    144      * Since: 1.1.14
    145      */
    146     g_object_class_install_property(objectClass, PROP_MESSAGE,
    147                                     g_param_spec_object("message",
    148                                                         _("Message"),
    149                                                         _("The SoupMessage that backs the response."),
    150                                                         SOUP_TYPE_MESSAGE,
    151                                                         (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
    152 
    153     g_type_class_add_private(responseClass, sizeof(WebKitNetworkResponsePrivate));
    154 }
    155 
    156 static void webkit_network_response_init(WebKitNetworkResponse* response)
    157 {
    158     response->priv = WEBKIT_NETWORK_RESPONSE_GET_PRIVATE(response);
    159 }
    160 
    161 /**
    162  * webkit_network_response_new:
    163  * @uri: an URI
    164  *
    165  * Creates a new #WebKitNetworkResponse initialized with an URI.
    166  *
    167  * Returns: a new #WebKitNetworkResponse, or %NULL if the URI is
    168  * invalid.
    169  *
    170  * Since: 1.1.14
    171  */
    172 WebKitNetworkResponse* webkit_network_response_new(const gchar* uri)
    173 {
    174     g_return_val_if_fail(uri, NULL);
    175 
    176     return WEBKIT_NETWORK_RESPONSE(g_object_new(WEBKIT_TYPE_NETWORK_RESPONSE, "uri", uri, NULL));
    177 }
    178 
    179 /**
    180  * webkit_network_response_set_uri:
    181  * @response: a #WebKitNetworkResponse
    182  * @uri: an URI
    183  *
    184  * Sets the URI held and used by the given response. When the response
    185  * has an associated #SoupMessage, its URI will also be set by this
    186  * call.
    187  *
    188  * Since: 1.1.14
    189  */
    190 void webkit_network_response_set_uri(WebKitNetworkResponse* response, const gchar* uri)
    191 {
    192     g_return_if_fail(WEBKIT_IS_NETWORK_RESPONSE(response));
    193     g_return_if_fail(uri);
    194 
    195     WebKitNetworkResponsePrivate* priv = response->priv;
    196 
    197     if (priv->uri)
    198         g_free(priv->uri);
    199     priv->uri = g_strdup(uri);
    200 
    201     if (!priv->message)
    202         return;
    203 
    204     SoupURI* soupURI = soup_uri_new(uri);
    205     g_return_if_fail(soupURI);
    206 
    207     soup_message_set_uri(priv->message, soupURI);
    208     soup_uri_free(soupURI);
    209 }
    210 
    211 /**
    212  * webkit_network_response_get_uri:
    213  * @response: a #WebKitNetworkResponse
    214  *
    215  * Returns: the uri of the #WebKitNetworkResponse
    216  *
    217  * Since: 1.1.14
    218  */
    219 G_CONST_RETURN gchar* webkit_network_response_get_uri(WebKitNetworkResponse* response)
    220 {
    221     g_return_val_if_fail(WEBKIT_IS_NETWORK_RESPONSE(response), NULL);
    222 
    223     WebKitNetworkResponsePrivate* priv = response->priv;
    224 
    225     if (priv->uri)
    226         return priv->uri;
    227 
    228     SoupURI* soupURI = soup_message_get_uri(priv->message);
    229     priv->uri = soup_uri_to_string(soupURI, FALSE);
    230     return priv->uri;
    231 }
    232 
    233 /**
    234  * webkit_network_response_get_message:
    235  * @response: a #WebKitNetworkResponse
    236  *
    237  * Obtains the #SoupMessage that represents the given response. Notice
    238  * that only the response side of the HTTP conversation is
    239  * represented.
    240  *
    241  * Returns: (transfer none): the #SoupMessage
    242  * Since: 1.1.14
    243  */
    244 SoupMessage* webkit_network_response_get_message(WebKitNetworkResponse* response)
    245 {
    246     g_return_val_if_fail(WEBKIT_IS_NETWORK_RESPONSE(response), NULL);
    247 
    248     WebKitNetworkResponsePrivate* priv = response->priv;
    249 
    250     return priv->message;
    251 }
    252 
    253 namespace WebKit {
    254 
    255 WebCore::ResourceResponse core(WebKitNetworkResponse* response)
    256 {
    257     SoupMessage* soupMessage = webkit_network_response_get_message(response);
    258     if (soupMessage)
    259         return WebCore::ResourceResponse(soupMessage);
    260 
    261     return WebCore::ResourceResponse();
    262 }
    263 
    264 WebKitNetworkResponse* kitNew(const WebCore::ResourceResponse& resourceResponse)
    265 {
    266     GRefPtr<SoupMessage> soupMessage(adoptGRef(resourceResponse.toSoupMessage()));
    267     if (soupMessage)
    268         return WEBKIT_NETWORK_RESPONSE(g_object_new(WEBKIT_TYPE_NETWORK_RESPONSE, "message", soupMessage.get(), NULL));
    269 
    270     return WEBKIT_NETWORK_RESPONSE(g_object_new(WEBKIT_TYPE_NETWORK_RESPONSE, "uri", resourceResponse.url().string().utf8().data(), NULL));
    271 }
    272 
    273 }
    274