1/*
2 * Copyright (C) 2012 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitWebResource.h"
22
23#include "APIData.h"
24#include "WebFrameProxy.h"
25#include "WebKitURIRequest.h"
26#include "WebKitWebResourcePrivate.h"
27#include "WebPageProxy.h"
28#include <glib/gi18n-lib.h>
29#include <wtf/glib/GRefPtr.h>
30#include <wtf/glib/WTFGType.h>
31#include <wtf/text/CString.h>
32
33using namespace WebKit;
34
35/**
36 * SECTION: WebKitWebResource
37 * @Short_description: Represents a resource at the end of a URI
38 * @Title: WebKitWebResource
39 *
40 * A #WebKitWebResource encapsulates content for each resource at the
41 * end of a particular URI. For example, one #WebKitWebResource will
42 * be created for each separate image and stylesheet when a page is
43 * loaded.
44 *
45 * You can access the response and the URI for a given
46 * #WebKitWebResource, using webkit_web_resource_get_uri() and
47 * webkit_web_resource_get_response(), as well as the raw data, using
48 * webkit_web_resource_get_data().
49 *
50 */
51
52enum {
53 SENT_REQUEST,
54 RECEIVED_DATA,
55 FINISHED,
56 FAILED,
57 FAILED_WITH_TLS_ERRORS,
58
59 LAST_SIGNAL
60};
61
62enum {
63 PROP_0,
64
65 PROP_URI,
66 PROP_RESPONSE
67};
68
69
70struct _WebKitWebResourcePrivate {
71 RefPtr<WebFrameProxy> frame;
72 CString uri;
73 GRefPtr<WebKitURIResponse> response;
74 bool isMainResource;
75};
76
77WEBKIT_DEFINE_TYPE(WebKitWebResource, webkit_web_resource, G_TYPE_OBJECT)
78
79static guint signals[LAST_SIGNAL] = { 0, };
80
81static void webkitWebResourceGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
82{
83 WebKitWebResource* resource = WEBKIT_WEB_RESOURCE(object);
84
85 switch (propId) {
86 case PROP_URI:
87 g_value_set_string(value, webkit_web_resource_get_uri(resource));
88 break;
89 case PROP_RESPONSE:
90 g_value_set_object(value, webkit_web_resource_get_response(resource));
91 break;
92 default:
93 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
94 }
95}
96
97static void webkit_web_resource_class_init(WebKitWebResourceClass* resourceClass)
98{
99 GObjectClass* objectClass = G_OBJECT_CLASS(resourceClass);
100 objectClass->get_property = webkitWebResourceGetProperty;
101
102 /**
103 * WebKitWebResource:uri:
104 *
105 * The current active URI of the #WebKitWebResource.
106 * See webkit_web_resource_get_uri() for more details.
107 */
108 g_object_class_install_property(objectClass,
109 PROP_URI,
110 g_param_spec_string("uri",
111 _("URI"),
112 _("The current active URI of the resource"),
113 0,
114 WEBKIT_PARAM_READABLE));
115
116 /**
117 * WebKitWebResource:response:
118 *
119 * The #WebKitURIResponse associated with this resource.
120 */
121 g_object_class_install_property(objectClass,
122 PROP_RESPONSE,
123 g_param_spec_object("response",
124 _("Response"),
125 _("The response of the resource"),
126 WEBKIT_TYPE_URI_RESPONSE,
127 WEBKIT_PARAM_READABLE));
128
129 /**
130 * WebKitWebResource::sent-request:
131 * @resource: the #WebKitWebResource
132 * @request: a #WebKitURIRequest
133 * @redirected_response: a #WebKitURIResponse, or %NULL
134 *
135 * This signal is emitted when @request has been sent to the
136 * server. In case of a server redirection this signal is
137 * emitted again with the @request argument containing the new
138 * request sent to the server due to the redirection and the
139 * @redirected_response parameter containing the response
140 * received by the server for the initial request.
141 */
142 signals[SENT_REQUEST] = g_signal_new(
143 "sent-request",
144 G_TYPE_FROM_CLASS(objectClass),
145 G_SIGNAL_RUN_LAST,
146 0, nullptr, nullptr,
147 g_cclosure_marshal_generic,
148 G_TYPE_NONE, 2,
149 WEBKIT_TYPE_URI_REQUEST,
150 WEBKIT_TYPE_URI_RESPONSE);
151
152 /**
153 * WebKitWebResource::received-data:
154 * @resource: the #WebKitWebResource
155 * @data_length: the length of data received in bytes
156 *
157 * This signal is emitted after response is received,
158 * every time new data has been received. It's
159 * useful to know the progress of the resource load operation.
160 */
161 signals[RECEIVED_DATA] = g_signal_new(
162 "received-data",
163 G_TYPE_FROM_CLASS(objectClass),
164 G_SIGNAL_RUN_LAST,
165 0, nullptr, nullptr,
166 g_cclosure_marshal_generic,
167 G_TYPE_NONE, 1,
168 G_TYPE_UINT64);
169
170 /**
171 * WebKitWebResource::finished:
172 * @resource: the #WebKitWebResource
173 *
174 * This signal is emitted when the resource load finishes successfully
175 * or due to an error. In case of errors #WebKitWebResource::failed signal
176 * is emitted before this one.
177 */
178 signals[FINISHED] =
179 g_signal_new("finished",
180 G_TYPE_FROM_CLASS(objectClass),
181 G_SIGNAL_RUN_LAST,
182 0, 0, 0,
183 g_cclosure_marshal_VOID__VOID,
184 G_TYPE_NONE, 0);
185
186 /**
187 * WebKitWebResource::failed:
188 * @resource: the #WebKitWebResource
189 * @error: the #GError that was triggered
190 *
191 * This signal is emitted when an error occurs during the resource
192 * load operation.
193 */
194 signals[FAILED] =
195 g_signal_new(
196 "failed",
197 G_TYPE_FROM_CLASS(objectClass),
198 G_SIGNAL_RUN_LAST,
199 0, 0, 0,
200 g_cclosure_marshal_VOID__BOXED,
201 G_TYPE_NONE, 1,
202 G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE);
203
204 /**
205 * WebKitWebResource::failed-with-tls-errors:
206 * @resource: the #WebKitWebResource
207 * @certificate: a #GTlsCertificate
208 * @errors: a #GTlsCertificateFlags with the verification status of @certificate
209 *
210 * This signal is emitted when a TLS error occurs during the resource load operation.
211 *
212 * Since: 2.8
213 */
214 signals[FAILED_WITH_TLS_ERRORS] =
215 g_signal_new("failed-with-tls-errors",
216 G_TYPE_FROM_CLASS(objectClass),
217 G_SIGNAL_RUN_LAST,
218 0, nullptr, nullptr,
219 g_cclosure_marshal_generic,
220 G_TYPE_NONE, 2,
221 G_TYPE_TLS_CERTIFICATE,
222 G_TYPE_TLS_CERTIFICATE_FLAGS);
223}
224
225static void webkitWebResourceUpdateURI(WebKitWebResource* resource, const CString& requestURI)
226{
227 if (resource->priv->uri == requestURI)
228 return;
229
230 resource->priv->uri = requestURI;
231 g_object_notify(G_OBJECT(resource), "uri");
232}
233
234WebKitWebResource* webkitWebResourceCreate(WebFrameProxy* frame, WebKitURIRequest* request, bool isMainResource)
235{
236 ASSERT(frame);
237 WebKitWebResource* resource = WEBKIT_WEB_RESOURCE(g_object_new(WEBKIT_TYPE_WEB_RESOURCE, NULL));
238 resource->priv->frame = frame;
239 resource->priv->uri = webkit_uri_request_get_uri(request);
240 resource->priv->isMainResource = isMainResource;
241 return resource;
242}
243
244void webkitWebResourceSentRequest(WebKitWebResource* resource, WebKitURIRequest* request, WebKitURIResponse* redirectResponse)
245{
246 webkitWebResourceUpdateURI(resource, webkit_uri_request_get_uri(request));
247 g_signal_emit(resource, signals[SENT_REQUEST], 0, request, redirectResponse);
248}
249
250void webkitWebResourceSetResponse(WebKitWebResource* resource, WebKitURIResponse* response)
251{
252 resource->priv->response = response;
253 g_object_notify(G_OBJECT(resource), "response");
254}
255
256void webkitWebResourceNotifyProgress(WebKitWebResource* resource, guint64 bytesReceived)
257{
258 g_signal_emit(resource, signals[RECEIVED_DATA], 0, bytesReceived);
259}
260
261void webkitWebResourceFinished(WebKitWebResource* resource)
262{
263 g_signal_emit(resource, signals[FINISHED], 0, NULL);
264}
265
266void webkitWebResourceFailed(WebKitWebResource* resource, GError* error)
267{
268 g_signal_emit(resource, signals[FAILED], 0, error);
269 g_signal_emit(resource, signals[FINISHED], 0, NULL);
270}
271
272void webkitWebResourceFailedWithTLSErrors(WebKitWebResource* resource, GTlsCertificateFlags tlsErrors, GTlsCertificate* certificate)
273{
274 g_signal_emit(resource, signals[FAILED_WITH_TLS_ERRORS], 0, certificate, tlsErrors);
275 g_signal_emit(resource, signals[FINISHED], 0, nullptr);
276}
277
278WebFrameProxy* webkitWebResourceGetFrame(WebKitWebResource* resource)
279{
280 return resource->priv->frame.get();
281}
282
283/**
284 * webkit_web_resource_get_uri:
285 * @resource: a #WebKitWebResource
286 *
287 * Returns the current active URI of @resource. The active URI might change during
288 * a load operation:
289 *
290 * <orderedlist>
291 * <listitem><para>
292 * When the resource load starts, the active URI is the requested URI
293 * </para></listitem>
294 * <listitem><para>
295 * When the initial request is sent to the server, #WebKitWebResource::sent-request
296 * signal is emitted without a redirected response, the active URI is the URI of
297 * the request sent to the server.
298 * </para></listitem>
299 * <listitem><para>
300 * In case of a server redirection, #WebKitWebResource::sent-request signal
301 * is emitted again with a redirected response, the active URI is the URI the request
302 * was redirected to.
303 * </para></listitem>
304 * <listitem><para>
305 * When the response is received from the server, the active URI is the final
306 * one and it will not change again.
307 * </para></listitem>
308 * </orderedlist>
309 *
310 * You can monitor the active URI by connecting to the notify::uri
311 * signal of @resource.
312 *
313 * Returns: the current active URI of @resource
314 */
315const char* webkit_web_resource_get_uri(WebKitWebResource* resource)
316{
317 g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(resource), 0);
318
319 return resource->priv->uri.data();
320}
321
322/**
323 * webkit_web_resource_get_response:
324 * @resource: a #WebKitWebResource
325 *
326 * Retrieves the #WebKitURIResponse of the resource load operation.
327 * This method returns %NULL if called before the response
328 * is received from the server. You can connect to notify::response
329 * signal to be notified when the response is received.
330 *
331 * Returns: (transfer none): the #WebKitURIResponse, or %NULL if
332 * the response hasn't been received yet.
333 */
334WebKitURIResponse* webkit_web_resource_get_response(WebKitWebResource* resource)
335{
336 g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(resource), 0);
337
338 return resource->priv->response.get();
339}
340
341struct ResourceGetDataAsyncData {
342 RefPtr<API::Data> webData;
343};
344WEBKIT_DEFINE_ASYNC_DATA_STRUCT(ResourceGetDataAsyncData)
345
346static void resourceDataCallback(API::Data* wkData, CallbackBase::Error error, GTask* task)
347{
348 if (error != CallbackBase::Error::None) {
349 // This fails when the page is closed or frame is destroyed, so we can just cancel the operation.
350 g_task_return_new_error(task, G_IO_ERROR, G_IO_ERROR_CANCELLED, _("Operation was cancelled"));
351 return;
352 }
353 ResourceGetDataAsyncData* data = static_cast<ResourceGetDataAsyncData*>(g_task_get_task_data(task));
354 data->webData = wkData;
355 if (!wkData->bytes())
356 data->webData = API::Data::create(reinterpret_cast<const unsigned char*>(""), 1);
357 g_task_return_boolean(task, TRUE);
358}
359
360/**
361 * webkit_web_resource_get_data:
362 * @resource: a #WebKitWebResource
363 * @cancellable: (allow-none): a #GCancellable or %NULL to ignore
364 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
365 * @user_data: (closure): the data to pass to callback function
366 *
367 * Asynchronously get the raw data for @resource.
368 *
369 * When the operation is finished, @callback will be called. You can then call
370 * webkit_web_resource_get_data_finish() to get the result of the operation.
371 */
372void webkit_web_resource_get_data(WebKitWebResource* resource, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
373{
374 g_return_if_fail(WEBKIT_IS_WEB_RESOURCE(resource));
375
376 GRefPtr<GTask> task = adoptGRef(g_task_new(resource, cancellable, callback, userData));
377 g_task_set_task_data(task.get(), createResourceGetDataAsyncData(), reinterpret_cast<GDestroyNotify>(destroyResourceGetDataAsyncData));
378 if (resource->priv->isMainResource)
379 resource->priv->frame->getMainResourceData([task = WTFMove(task)](API::Data* data, CallbackBase::Error error) {
380 resourceDataCallback(data, error, task.get());
381 });
382 else {
383 String url = String::fromUTF8(resource->priv->uri.data());
384 resource->priv->frame->getResourceData(API::URL::create(url).ptr(), [task = WTFMove(task)](API::Data* data, CallbackBase::Error error) {
385 resourceDataCallback(data, error, task.get());
386 });
387 }
388}
389
390/**
391 * webkit_web_resource_get_data_finish:
392 * @resource: a #WebKitWebResource
393 * @result: a #GAsyncResult
394 * @length: (out) (allow-none): return location for the length of the resource data
395 * @error: return location for error or %NULL to ignore
396 *
397 * Finish an asynchronous operation started with webkit_web_resource_get_data().
398 *
399 * Returns: (transfer full) (array length=length) (element-type guint8): a
400 * string with the data of @resource, or %NULL in case of error. if @length
401 * is not %NULL, the size of the data will be assigned to it.
402 */
403guchar* webkit_web_resource_get_data_finish(WebKitWebResource* resource, GAsyncResult* result, gsize* length, GError** error)
404{
405 g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(resource), 0);
406 g_return_val_if_fail(g_task_is_valid(result, resource), 0);
407
408 GTask* task = G_TASK(result);
409 if (!g_task_propagate_boolean(task, error))
410 return 0;
411
412 ResourceGetDataAsyncData* data = static_cast<ResourceGetDataAsyncData*>(g_task_get_task_data(task));
413 if (length)
414 *length = data->webData->size();
415 return static_cast<guchar*>(g_memdup(data->webData->bytes(), data->webData->size()));
416}
417