• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "CString.h"
25 #include "GOwnPtr.h"
26 #include "ResourceRequest.h"
27 #include "webkitprivate.h"
28 
29 #include <glib/gi18n-lib.h>
30 
31 namespace WTF {
32 
freeOwnedGPtr(SoupMessage * soupMessage)33 template <> void freeOwnedGPtr<SoupMessage>(SoupMessage* soupMessage)
34 {
35     if (soupMessage)
36         g_object_unref(soupMessage);
37 }
38 
39 }
40 
41 /**
42  * SECTION:webkitnetworkrequest
43  * @short_description: The target of a navigation request
44  * @see_also: #WebKitWebView::navigation-requested
45  *
46  * This class represents the network related aspects of a navigation
47  * request. Currently this is only the uri of the target. In the future
48  * the state of the web form might be added.
49  * Currently this object is only used along with the
50  * #WebKitWebView::navigation-requested signal.
51  *
52  */
53 
54 G_DEFINE_TYPE(WebKitNetworkRequest, webkit_network_request, G_TYPE_OBJECT);
55 
56 struct _WebKitNetworkRequestPrivate {
57     gchar* uri;
58     SoupMessage* message;
59 };
60 
61 #define WEBKIT_NETWORK_REQUEST_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_NETWORK_REQUEST, WebKitNetworkRequestPrivate))
62 
63 enum {
64     PROP_0,
65 
66     PROP_URI,
67     PROP_MESSAGE,
68 };
69 
webkit_network_request_finalize(GObject * object)70 static void webkit_network_request_finalize(GObject* object)
71 {
72     WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
73     WebKitNetworkRequestPrivate* priv = request->priv;
74 
75     g_free(priv->uri);
76 
77     if (priv->message) {
78         g_object_unref(priv->message);
79         priv->message = NULL;
80     }
81 
82     G_OBJECT_CLASS(webkit_network_request_parent_class)->finalize(object);
83 }
84 
webkit_network_request_get_property(GObject * object,guint propertyID,GValue * value,GParamSpec * pspec)85 static void webkit_network_request_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec)
86 {
87     WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
88 
89     switch(propertyID) {
90     case PROP_URI:
91         g_value_set_string(value, webkit_network_request_get_uri(request));
92         break;
93     case PROP_MESSAGE:
94         g_value_set_object(value, webkit_network_request_get_message(request));
95         break;
96     default:
97         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
98     }
99 }
100 
webkit_network_request_set_property(GObject * object,guint propertyID,const GValue * value,GParamSpec * pspec)101 static void webkit_network_request_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec)
102 {
103     WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
104     WebKitNetworkRequestPrivate* priv = request->priv;
105 
106     switch(propertyID) {
107     case PROP_URI:
108         webkit_network_request_set_uri(request, g_value_get_string(value));
109         break;
110     case PROP_MESSAGE:
111         priv->message = SOUP_MESSAGE(g_value_dup_object(value));
112         break;
113     default:
114         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
115     }
116 }
117 
webkit_network_request_class_init(WebKitNetworkRequestClass * requestClass)118 static void webkit_network_request_class_init(WebKitNetworkRequestClass* requestClass)
119 {
120     GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
121 
122     objectClass->finalize = webkit_network_request_finalize;
123     objectClass->get_property = webkit_network_request_get_property;
124     objectClass->set_property = webkit_network_request_set_property;
125 
126     webkit_init();
127 
128     /**
129      * WebKitNetworkRequest:uri:
130      *
131      * The URI to which the request will be made.
132      *
133      * Since: 1.1.10
134      */
135     g_object_class_install_property(objectClass, PROP_URI,
136                                     g_param_spec_string("uri",
137                                                         _("URI"),
138                                                         _("The URI to which the request will be made."),
139                                                         NULL,
140                                                         (GParamFlags)(WEBKIT_PARAM_READWRITE)));
141 
142     /**
143      * WebKitNetworkRequest:message:
144      *
145      * The #SoupMessage that backs the request.
146      *
147      * Since: 1.1.10
148      */
149     g_object_class_install_property(objectClass, PROP_MESSAGE,
150                                     g_param_spec_object("message",
151                                                         _("Message"),
152                                                         _("The SoupMessage that backs the request."),
153                                                         SOUP_TYPE_MESSAGE,
154                                                         (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
155 
156     g_type_class_add_private(requestClass, sizeof(WebKitNetworkRequestPrivate));
157 }
158 
webkit_network_request_init(WebKitNetworkRequest * request)159 static void webkit_network_request_init(WebKitNetworkRequest* request)
160 {
161     WebKitNetworkRequestPrivate* priv = WEBKIT_NETWORK_REQUEST_GET_PRIVATE(request);
162     request->priv = priv;
163 }
164 
165 // for internal use only
webkit_network_request_new_with_core_request(const WebCore::ResourceRequest & resourceRequest)166 WebKitNetworkRequest* webkit_network_request_new_with_core_request(const WebCore::ResourceRequest& resourceRequest)
167 {
168     GOwnPtr<SoupMessage> soupMessage(resourceRequest.toSoupMessage());
169     if (soupMessage)
170         return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "message", soupMessage.get(), NULL));
171 
172     return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", resourceRequest.url().string().utf8().data(), NULL));
173 }
174 
175 /**
176  * webkit_network_request_new:
177  * @uri: an URI
178  *
179  * Creates a new #WebKitNetworkRequest initialized with an URI.
180  *
181  * Returns: a new #WebKitNetworkRequest, or %NULL if the URI is
182  * invalid.
183  */
webkit_network_request_new(const gchar * uri)184 WebKitNetworkRequest* webkit_network_request_new(const gchar* uri)
185 {
186     g_return_val_if_fail(uri, NULL);
187 
188     return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", uri, NULL));
189 }
190 
191 /**
192  * webkit_network_request_set_uri:
193  * @request: a #WebKitNetworkRequest
194  * @uri: an URI
195  *
196  * Sets the URI held and used by the given request. When the request
197  * has an associated #SoupMessage, its URI will also be set by this
198  * call.
199  *
200  */
webkit_network_request_set_uri(WebKitNetworkRequest * request,const gchar * uri)201 void webkit_network_request_set_uri(WebKitNetworkRequest* request, const gchar* uri)
202 {
203     g_return_if_fail(WEBKIT_IS_NETWORK_REQUEST(request));
204     g_return_if_fail(uri);
205 
206     WebKitNetworkRequestPrivate* priv = request->priv;
207 
208     if (priv->uri)
209         g_free(priv->uri);
210     priv->uri = g_strdup(uri);
211 
212     if (!priv->message)
213         return;
214 
215     SoupURI* soupURI = soup_uri_new(uri);
216     if (!soupURI) {
217         g_warning("Invalid URI: %s", uri);
218         return;
219     }
220 
221     soup_message_set_uri(priv->message, soupURI);
222     soup_uri_free(soupURI);
223 }
224 
225 /**
226  * webkit_network_request_get_uri:
227  * @request: a #WebKitNetworkRequest
228  *
229  * Returns: the uri of the #WebKitNetworkRequest
230  *
231  * Since: 1.0.0
232  */
webkit_network_request_get_uri(WebKitNetworkRequest * request)233 G_CONST_RETURN gchar* webkit_network_request_get_uri(WebKitNetworkRequest* request)
234 {
235     g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
236 
237     WebKitNetworkRequestPrivate* priv = request->priv;
238 
239     if (priv->uri)
240         return priv->uri;
241 
242     SoupURI* soupURI = soup_message_get_uri(priv->message);
243     priv->uri = soup_uri_to_string(soupURI, FALSE);
244     return priv->uri;
245 }
246 
247 /**
248  * webkit_network_request_get_soup_message:
249  * @request: a #WebKitNetworkRequest
250  *
251  * Obtains the #SoupMessage held and used by the given request. Notice
252  * that modification of the SoupMessage of a request by signal
253  * handlers is only supported (as in, will only affect what is
254  * actually sent to the server) where explicitly documented.
255  *
256  * Returns: the #SoupMessage
257  * Since: 1.1.9
258  */
webkit_network_request_get_message(WebKitNetworkRequest * request)259 SoupMessage* webkit_network_request_get_message(WebKitNetworkRequest* request)
260 {
261     g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
262 
263     WebKitNetworkRequestPrivate* priv = request->priv;
264 
265     return priv->message;
266 }
267