~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebKit/gtk/webkit/webkitwebresource.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Jan Michael C. Alonzo
 
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 "ArchiveResource.h"
 
24
#include "KURL.h"
 
25
#include "SharedBuffer.h"
 
26
#include "webkitenumtypes.h"
 
27
#include "webkitglobalsprivate.h"
 
28
#include "webkitmarshal.h"
 
29
#include "webkitnetworkresponse.h"
 
30
#include "webkitwebresourceprivate.h"
 
31
#include <glib.h>
 
32
#include <glib/gi18n-lib.h>
 
33
#include <wtf/Assertions.h>
 
34
#include <wtf/text/CString.h>
 
35
#include <wtf/text/WTFString.h>
 
36
 
 
37
/**
 
38
 * SECTION:webkitwebresource
 
39
 * @short_description: Represents a downloaded URI.
 
40
 * @see_also: #WebKitWebDataSource
 
41
 *
 
42
 * A web resource encapsulates the data of the download as well as the URI,
 
43
 * MIME type and frame name of the resource.
 
44
 */
 
45
 
 
46
using namespace WebCore;
 
47
 
 
48
enum {
 
49
    // Resource loading
 
50
    RESPONSE_RECEIVED,
 
51
    LOAD_FINISHED,
 
52
    CONTENT_LENGTH_RECEIVED,
 
53
    LOAD_FAILED,
 
54
 
 
55
    LAST_SIGNAL
 
56
};
 
57
 
 
58
enum {
 
59
    PROP_0,
 
60
    PROP_URI,
 
61
    PROP_MIME_TYPE,
 
62
    PROP_ENCODING,
 
63
    PROP_FRAME_NAME
 
64
};
 
65
 
 
66
static guint webkit_web_resource_signals[LAST_SIGNAL] = { 0, };
 
67
 
 
68
G_DEFINE_TYPE(WebKitWebResource, webkit_web_resource, G_TYPE_OBJECT);
 
69
 
 
70
static void webkit_web_resource_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);
 
71
static void webkit_web_resource_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
 
72
 
 
73
static void webkit_web_resource_cleanup(WebKitWebResource* webResource)
 
74
{
 
75
    WebKitWebResourcePrivate* priv = webResource->priv;
 
76
 
 
77
    g_free(priv->uri);
 
78
    priv->uri = NULL;
 
79
 
 
80
    g_free(priv->mimeType);
 
81
    priv->mimeType = NULL;
 
82
 
 
83
    g_free(priv->textEncoding);
 
84
    priv->textEncoding = NULL;
 
85
 
 
86
    g_free(priv->frameName);
 
87
    priv->frameName = NULL;
 
88
 
 
89
    if (priv->data)
 
90
        g_string_free(priv->data, TRUE);
 
91
    priv->data = NULL;
 
92
}
 
93
 
 
94
static void webkit_web_resource_dispose(GObject* object)
 
95
{
 
96
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object);
 
97
    WebKitWebResourcePrivate* priv = webResource->priv;
 
98
 
 
99
    if (priv->resource) {
 
100
        priv->resource->deref();
 
101
        priv->resource = 0;
 
102
    }
 
103
 
 
104
    G_OBJECT_CLASS(webkit_web_resource_parent_class)->dispose(object);
 
105
}
 
106
 
 
107
static void webkit_web_resource_finalize(GObject* object)
 
108
{
 
109
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object);
 
110
 
 
111
    webkit_web_resource_cleanup(webResource);
 
112
 
 
113
    G_OBJECT_CLASS(webkit_web_resource_parent_class)->finalize(object);
 
114
}
 
115
 
 
116
static void webkit_web_resource_class_init(WebKitWebResourceClass* webResourceClass)
 
117
{
 
118
    GObjectClass* gobject_class = G_OBJECT_CLASS(webResourceClass);
 
119
 
 
120
    gobject_class->dispose = webkit_web_resource_dispose;
 
121
    gobject_class->finalize = webkit_web_resource_finalize;
 
122
    gobject_class->get_property = webkit_web_resource_get_property;
 
123
    gobject_class->set_property = webkit_web_resource_set_property;
 
124
 
 
125
    /**
 
126
     * WebKitWebResource::response-received:
 
127
     * @web_resource: the #WebKitWebResource being loaded
 
128
     * @response: the #WebKitNetworkResponse that was received
 
129
     *
 
130
     * Emitted when the response is received from the server.
 
131
     *
 
132
     * Since: 1.7.5
 
133
     */
 
134
    webkit_web_resource_signals[RESPONSE_RECEIVED] = g_signal_new("response-received",
 
135
            G_TYPE_FROM_CLASS(webResourceClass),
 
136
            G_SIGNAL_RUN_LAST,
 
137
            0,
 
138
            0, 0,
 
139
            g_cclosure_marshal_VOID__OBJECT,
 
140
            G_TYPE_NONE, 1,
 
141
            WEBKIT_TYPE_NETWORK_RESPONSE);
 
142
 
 
143
    /**
 
144
     * WebKitWebResource::load-failed:
 
145
     * @web_resource: the #WebKitWebResource that was loaded
 
146
     * @error: the #GError that was triggered
 
147
     *
 
148
     * Invoked when the @web_resource failed to load
 
149
     *
 
150
     * Since: 1.7.5
 
151
     */
 
152
    webkit_web_resource_signals[LOAD_FAILED] = g_signal_new("load-failed",
 
153
            G_TYPE_FROM_CLASS(webResourceClass),
 
154
            G_SIGNAL_RUN_LAST,
 
155
            0,
 
156
            0, 0,
 
157
            g_cclosure_marshal_VOID__POINTER,
 
158
            G_TYPE_NONE, 1,
 
159
            G_TYPE_POINTER);
 
160
 
 
161
    /**
 
162
     * WebKitWebResource::load-finished:
 
163
     * @web_resource: the #WebKitWebResource being loaded
 
164
     *
 
165
     * Emitted when all the data for the resource was loaded
 
166
     *
 
167
     * Since: 1.7.5
 
168
     */
 
169
    webkit_web_resource_signals[LOAD_FINISHED] = g_signal_new("load-finished",
 
170
            G_TYPE_FROM_CLASS(webResourceClass),
 
171
            G_SIGNAL_RUN_LAST,
 
172
            0,
 
173
            0, 0,
 
174
            g_cclosure_marshal_VOID__VOID,
 
175
            G_TYPE_NONE, 0);
 
176
 
 
177
    /**
 
178
     * WebKitWebResource::content-length-received:
 
179
     * @web_resource: the #WebKitWebResource that was loaded
 
180
     * @length_received: the amount of data received since the last signal emission
 
181
     *
 
182
     * Emitted when new resource data has been received. The
 
183
     * @length_received variable stores the amount of bytes received
 
184
     * since the last time this signal was emitted. This is useful to
 
185
     * provide progress information about the resource load operation.
 
186
     *
 
187
     * Since: 1.7.5
 
188
     */
 
189
    webkit_web_resource_signals[CONTENT_LENGTH_RECEIVED] = g_signal_new("content-length-received",
 
190
            G_TYPE_FROM_CLASS(webResourceClass),
 
191
            G_SIGNAL_RUN_LAST,
 
192
            0,
 
193
            0, 0,
 
194
            g_cclosure_marshal_VOID__INT,
 
195
            G_TYPE_NONE, 1,
 
196
            G_TYPE_INT);
 
197
 
 
198
    /**
 
199
     * WebKitWebResource:uri:
 
200
     *
 
201
     * The URI of the web resource
 
202
     *
 
203
     * Since: 1.1.14
 
204
     */
 
205
    g_object_class_install_property(gobject_class,
 
206
                                    PROP_URI,
 
207
                                    g_param_spec_string(
 
208
                                    "uri",
 
209
                                    _("URI"),
 
210
                                    _("The URI of the resource"),
 
211
                                    NULL,
 
212
                                    (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
 
213
    /**
 
214
     * WebKitWebResource:mime-type:
 
215
     *
 
216
     * The MIME type of the web resource.
 
217
     *
 
218
     * Since: 1.1.14
 
219
     */
 
220
    g_object_class_install_property(gobject_class,
 
221
                                    PROP_MIME_TYPE,
 
222
                                    g_param_spec_string(
 
223
                                    "mime-type",
 
224
                                    _("MIME Type"),
 
225
                                    _("The MIME type of the resource"),
 
226
                                    NULL,
 
227
                                    WEBKIT_PARAM_READABLE));
 
228
    /**
 
229
     * WebKitWebResource:encoding:
 
230
     *
 
231
     * The encoding name to which the web resource was encoded in.
 
232
     *
 
233
     * Since: 1.1.14
 
234
     */
 
235
    g_object_class_install_property(gobject_class,
 
236
                                    PROP_ENCODING,
 
237
                                    g_param_spec_string(
 
238
                                    "encoding",
 
239
                                    _("Encoding"),
 
240
                                    _("The text encoding name of the resource"),
 
241
                                    NULL,
 
242
                                    WEBKIT_PARAM_READABLE));
 
243
 
 
244
    /**
 
245
     * WebKitWebResource:frame-name:
 
246
     *
 
247
     * The frame name for the web resource.
 
248
     *
 
249
     * Since: 1.1.14
 
250
     */
 
251
    g_object_class_install_property(gobject_class,
 
252
                                    PROP_FRAME_NAME,
 
253
                                    g_param_spec_string(
 
254
                                    "frame-name",
 
255
                                    _("Frame Name"),
 
256
                                    _("The frame name of the resource"),
 
257
                                    NULL,
 
258
                                    WEBKIT_PARAM_READABLE));
 
259
 
 
260
    g_type_class_add_private(gobject_class, sizeof(WebKitWebResourcePrivate));
 
261
}
 
262
 
 
263
static void webkit_web_resource_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
 
264
{
 
265
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object);
 
266
 
 
267
    switch (prop_id) {
 
268
    case PROP_URI:
 
269
        g_value_set_string(value, webkit_web_resource_get_uri(webResource));
 
270
        break;
 
271
    case PROP_MIME_TYPE:
 
272
        g_value_set_string(value, webkit_web_resource_get_mime_type(webResource));
 
273
        break;
 
274
    case PROP_ENCODING:
 
275
        g_value_set_string(value, webkit_web_resource_get_encoding(webResource));
 
276
        break;
 
277
    case PROP_FRAME_NAME:
 
278
        g_value_set_string(value, webkit_web_resource_get_frame_name(webResource));
 
279
        break;
 
280
    default:
 
281
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
 
282
        break;
 
283
    }
 
284
}
 
285
 
 
286
static void webkit_web_resource_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
 
287
{
 
288
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object);
 
289
 
 
290
    switch (prop_id) {
 
291
    case PROP_URI:
 
292
        g_free(webResource->priv->uri);
 
293
        webResource->priv->uri = g_value_dup_string(value);
 
294
        break;
 
295
    default:
 
296
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
 
297
        break;
 
298
    }
 
299
}
 
300
 
 
301
static void webkit_web_resource_init(WebKitWebResource* webResource)
 
302
{
 
303
    webResource->priv = G_TYPE_INSTANCE_GET_PRIVATE(webResource, WEBKIT_TYPE_WEB_RESOURCE, WebKitWebResourcePrivate);
 
304
}
 
305
 
 
306
// internal use only
 
307
WebKitWebResource* webkit_web_resource_new_with_core_resource(PassRefPtr<ArchiveResource> resource)
 
308
{
 
309
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(g_object_new(WEBKIT_TYPE_WEB_RESOURCE, NULL));
 
310
    WebKitWebResourcePrivate* priv = webResource->priv;
 
311
    priv->resource = resource.leakRef();
 
312
 
 
313
    return webResource;
 
314
}
 
315
 
 
316
void webkit_web_resource_init_with_core_resource(WebKitWebResource* webResource, PassRefPtr<ArchiveResource> resource)
 
317
{
 
318
    ASSERT(resource);
 
319
 
 
320
    WebKitWebResourcePrivate* priv = webResource->priv;
 
321
 
 
322
    if (priv->resource)
 
323
        priv->resource->deref();
 
324
 
 
325
    priv->resource = resource.leakRef();
 
326
}
 
327
 
 
328
/**
 
329
 * webkit_web_resource_new:
 
330
 * @data: the data to initialize the #WebKitWebResource
 
331
 * @size: the length of @data
 
332
 * @uri: the URI of the #WebKitWebResource
 
333
 * @mime_type: the MIME type of the #WebKitWebResource
 
334
 * @encoding: the text encoding name of the #WebKitWebResource
 
335
 * @frame_name: the frame name of the #WebKitWebResource
 
336
 *
 
337
 * Returns a new #WebKitWebResource. The @encoding can be %NULL. The
 
338
 * @frame_name argument can be used if the resource represents contents of an
 
339
 * entire HTML frame, otherwise pass %NULL.
 
340
 *
 
341
 * Return value: a new #WebKitWebResource
 
342
 *
 
343
 * Since: 1.1.14
 
344
 */
 
345
WebKitWebResource* webkit_web_resource_new(const gchar* data,
 
346
                                           gssize size,
 
347
                                           const gchar* uri,
 
348
                                           const gchar* mimeType,
 
349
                                           const gchar* encoding,
 
350
                                           const gchar* frameName)
 
351
{
 
352
    g_return_val_if_fail(data, NULL);
 
353
    g_return_val_if_fail(uri, NULL);
 
354
    g_return_val_if_fail(mimeType, NULL);
 
355
 
 
356
    if (size < 0)
 
357
        size = strlen(data);
 
358
 
 
359
    RefPtr<SharedBuffer> buffer = SharedBuffer::create(data, size);
 
360
    WebKitWebResource* webResource = webkit_web_resource_new_with_core_resource(ArchiveResource::create(buffer, KURL(KURL(), String::fromUTF8(uri)), String::fromUTF8(mimeType), String::fromUTF8(encoding), String::fromUTF8(frameName)));
 
361
 
 
362
    return webResource;
 
363
}
 
364
 
 
365
/**
 
366
 * webkit_web_resource_get_data:
 
367
 * @web_resource: a #WebKitWebResource
 
368
 *
 
369
 * Returns the data of the @webResource.
 
370
 *
 
371
 * Return value: (transfer none): a #GString containing the character
 
372
 * data of the @webResource.  The string is owned by WebKit and should
 
373
 * not be freed or destroyed.
 
374
 *
 
375
 * Since: 1.1.14
 
376
 */
 
377
GString* webkit_web_resource_get_data(WebKitWebResource* webResource)
 
378
{
 
379
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);
 
380
 
 
381
    WebKitWebResourcePrivate* priv = webResource->priv;
 
382
 
 
383
    if (!priv->resource)
 
384
        return NULL;
 
385
 
 
386
    if (!priv->data)
 
387
        priv->data = g_string_new_len(priv->resource->data()->data(), priv->resource->data()->size());
 
388
 
 
389
    return priv->data;
 
390
}
 
391
 
 
392
/**
 
393
 * webkit_web_resource_get_uri:
 
394
 * @web_resource: a #WebKitWebResource
 
395
 *
 
396
 * Return value: the URI of the resource
 
397
 *
 
398
 * Since: 1.1.14
 
399
 */
 
400
const gchar* webkit_web_resource_get_uri(WebKitWebResource* webResource)
 
401
{
 
402
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);
 
403
 
 
404
    WebKitWebResourcePrivate* priv = webResource->priv;
 
405
 
 
406
 
 
407
    // We may have an URI without having a resource assigned to us (e.g., if the
 
408
    // FrameLoaderClient only had a ResourceRequest when we got created
 
409
    if (priv->uri)
 
410
        return priv->uri;
 
411
 
 
412
    if (!priv->resource)
 
413
        return NULL;
 
414
 
 
415
    priv->uri = g_strdup(priv->resource->url().string().utf8().data());
 
416
 
 
417
    return priv->uri;
 
418
}
 
419
 
 
420
/**
 
421
 * webkit_web_resource_get_mime_type:
 
422
 * @web_resource: a #WebKitWebResource
 
423
 *
 
424
 * Return value: the MIME type of the resource
 
425
 *
 
426
 * Since: 1.1.14
 
427
 */
 
428
const gchar* webkit_web_resource_get_mime_type(WebKitWebResource* webResource)
 
429
{
 
430
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);
 
431
 
 
432
    WebKitWebResourcePrivate* priv = webResource->priv;
 
433
    if (!priv->resource)
 
434
        return NULL;
 
435
 
 
436
    if (!priv->mimeType)
 
437
        priv->mimeType = g_strdup(priv->resource->mimeType().utf8().data());
 
438
 
 
439
    return priv->mimeType;
 
440
}
 
441
 
 
442
/**
 
443
 * webkit_web_resource_get_encoding:
 
444
 * @web_resource: a #WebKitWebResource
 
445
 *
 
446
 * Return value: the encoding name of the resource
 
447
 *
 
448
 * Since: 1.1.14
 
449
 */
 
450
const gchar* webkit_web_resource_get_encoding(WebKitWebResource* webResource)
 
451
{
 
452
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);
 
453
 
 
454
    WebKitWebResourcePrivate* priv = webResource->priv;
 
455
    if (!priv->resource)
 
456
        return NULL;
 
457
 
 
458
    if (!priv->textEncoding)
 
459
        priv->textEncoding = g_strdup(priv->resource->textEncoding().utf8().data());
 
460
 
 
461
    return priv->textEncoding;
 
462
}
 
463
 
 
464
/**
 
465
 * webkit_web_resource_get_frame_name:
 
466
 * @web_resource: a #WebKitWebResource
 
467
 *
 
468
 * Return value: the frame name of the resource.
 
469
 *
 
470
 * Since: 1.1.14
 
471
 */
 
472
const gchar* webkit_web_resource_get_frame_name(WebKitWebResource* webResource)
 
473
{
 
474
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);
 
475
 
 
476
    WebKitWebResourcePrivate* priv = webResource->priv;
 
477
    if (!priv->resource)
 
478
        return NULL;
 
479
 
 
480
    if (!priv->frameName)
 
481
        priv->frameName = g_strdup(priv->resource->frameName().utf8().data());
 
482
 
 
483
    return priv->frameName;
 
484
}
 
485