summaryrefslogtreecommitdiffstats
path: root/WebKit/gtk/webkit/webkitwebresource.cpp
blob: fb0acfc996f86299cc5caaff1333a5840107fd47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * Copyright (C) 2009 Jan Michael C. Alonzo
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include "webkitwebresource.h"
#include "webkitprivate.h"

#include "ArchiveResource.h"
#include "KURL.h"
#include "PlatformString.h"
#include "SharedBuffer.h"
#include "webkitenumtypes.h"
#include "webkitmarshal.h"
#include "wtf/Assertions.h"
#include <wtf/text/CString.h>

#include <glib.h>
#include <glib/gi18n-lib.h>

/**
 * SECTION:webkitwebresource
 * @short_description: Represents a downloaded URI.
 * @see_also: #WebKitWebDataSource
 *
 * A web resource encapsulates the data of the download as well as the URI,
 * MIME type and frame name of the resource.
 */

using namespace WebCore;
using namespace WebKit;

enum {
    PROP_0,

    PROP_URI,
    PROP_MIME_TYPE,
    PROP_ENCODING,
    PROP_FRAME_NAME
};

G_DEFINE_TYPE(WebKitWebResource, webkit_web_resource, G_TYPE_OBJECT);

static void webkit_web_resource_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);
static void webkit_web_resource_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);

static void webkit_web_resource_cleanup(WebKitWebResource* webResource)
{
    WebKitWebResourcePrivate* priv = webResource->priv;

    g_free(priv->uri);
    priv->uri = NULL;

    g_free(priv->mimeType);
    priv->mimeType = NULL;

    g_free(priv->textEncoding);
    priv->textEncoding = NULL;

    g_free(priv->frameName);
    priv->frameName = NULL;

    if (priv->data)
        g_string_free(priv->data, TRUE);
    priv->data = NULL;
}

static void webkit_web_resource_dispose(GObject* object)
{
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object);
    WebKitWebResourcePrivate* priv = webResource->priv;

    if (priv->resource) {
        priv->resource->deref();
        priv->resource = 0;
    }

    G_OBJECT_CLASS(webkit_web_resource_parent_class)->dispose(object);
}

static void webkit_web_resource_finalize(GObject* object)
{
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object);

    webkit_web_resource_cleanup(webResource);

    G_OBJECT_CLASS(webkit_web_resource_parent_class)->finalize(object);
}

static void webkit_web_resource_class_init(WebKitWebResourceClass* klass)
{
    GObjectClass* gobject_class = G_OBJECT_CLASS(klass);

    gobject_class->dispose = webkit_web_resource_dispose;
    gobject_class->finalize = webkit_web_resource_finalize;
    gobject_class->get_property = webkit_web_resource_get_property;
    gobject_class->set_property = webkit_web_resource_set_property;

    /**
     * WebKitWebResource:uri:
     *
     * The URI of the web resource
     *
     * Since: 1.1.14
     */
    g_object_class_install_property(gobject_class,
                                    PROP_URI,
                                    g_param_spec_string(
                                    "uri",
                                    _("URI"),
                                    _("The uri of the resource"),
                                    NULL,
                                    (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
    /**
     * WebKitWebResource:mime-type:
     *
     * The MIME type of the web resource.
     *
     * Since: 1.1.14
     */
    g_object_class_install_property(gobject_class,
                                    PROP_MIME_TYPE,
                                    g_param_spec_string(
                                    "mime-type",
                                    _("MIME Type"),
                                    _("The MIME type of the resource"),
                                    NULL,
                                    WEBKIT_PARAM_READABLE));
    /**
     * WebKitWebResource:encoding:
     *
     * The encoding name to which the web resource was encoded in.
     *
     * Since: 1.1.14
     */
    g_object_class_install_property(gobject_class,
                                    PROP_ENCODING,
                                    g_param_spec_string(
                                    "encoding",
                                    _("Encoding"),
                                    _("The text encoding name of the resource"),
                                    NULL,
                                    WEBKIT_PARAM_READABLE));

    /**
     * WebKitWebResource:frame-name:
     *
     * The frame name for the web resource.
     *
     * Since: 1.1.14
     */
    g_object_class_install_property(gobject_class,
                                    PROP_FRAME_NAME,
                                    g_param_spec_string(
                                    "frame-name",
                                    _("Frame Name"),
                                    _("The frame name of the resource"),
                                    NULL,
                                    WEBKIT_PARAM_READABLE));

    g_type_class_add_private(gobject_class, sizeof(WebKitWebResourcePrivate));
}

static void webkit_web_resource_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
{
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object);

    switch (prop_id) {
    case PROP_URI:
        g_value_set_string(value, webkit_web_resource_get_uri(webResource));
        break;
    case PROP_MIME_TYPE:
        g_value_set_string(value, webkit_web_resource_get_mime_type(webResource));
        break;
    case PROP_ENCODING:
        g_value_set_string(value, webkit_web_resource_get_encoding(webResource));
        break;
    case PROP_FRAME_NAME:
        g_value_set_string(value, webkit_web_resource_get_frame_name(webResource));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void webkit_web_resource_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
{
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object);

    switch (prop_id) {
    case PROP_URI:
        g_free(webResource->priv->uri);
        webResource->priv->uri = g_value_dup_string(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void webkit_web_resource_init(WebKitWebResource* webResource)
{
    webResource->priv = WEBKIT_WEB_RESOURCE_GET_PRIVATE(webResource);
}

// internal use only
WebKitWebResource* webkit_web_resource_new_with_core_resource(PassRefPtr<ArchiveResource> resource)
{
    WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(g_object_new(WEBKIT_TYPE_WEB_RESOURCE, NULL));
    WebKitWebResourcePrivate* priv = webResource->priv;
    priv->resource = resource.releaseRef();

    return webResource;
}

void webkit_web_resource_init_with_core_resource(WebKitWebResource* webResource, PassRefPtr<ArchiveResource> resource)
{
    ASSERT(resource);

    WebKitWebResourcePrivate* priv = webResource->priv;

    if (priv->resource)
        priv->resource->deref();

    priv->resource = resource.releaseRef();
}

/**
 * webkit_web_resource_new:
 * @data: the data to initialize the #WebKitWebResource
 * @size: the length of @data
 * @uri: the uri of the #WebKitWebResource
 * @mime_type: the MIME type of the #WebKitWebResource
 * @encoding: the text encoding name of the #WebKitWebResource
 * @frame_name: the frame name of the #WebKitWebResource
 *
 * Returns a new #WebKitWebResource. The @encoding can be %NULL. The
 * @frame_name argument can be used if the resource represents contents of an
 * entire HTML frame, otherwise pass %NULL.
 *
 * Return value: a new #WebKitWebResource
 *
 * Since: 1.1.14
 */
WebKitWebResource* webkit_web_resource_new(const gchar* data,
                                           gssize size,
                                           const gchar* uri,
                                           const gchar* mimeType,
                                           const gchar* encoding,
                                           const gchar* frameName)
{
    g_return_val_if_fail(data, NULL);
    g_return_val_if_fail(uri, NULL);
    g_return_val_if_fail(mimeType, NULL);

    if (size < 0)
        size = strlen(data);

    RefPtr<SharedBuffer> buffer = SharedBuffer::create(data, size);
    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)));

    return webResource;
}

/**
 * webkit_web_resource_get_data:
 * @web_resource: a #WebKitWebResource
 *
 * Returns the data of the @webResource.
 *
 * Return value: (transfer none): a #GString containing the character
 * data of the @webResource.  The string is owned by WebKit and should
 * not be freed or destroyed.
 *
 * Since: 1.1.14
 */
GString* webkit_web_resource_get_data(WebKitWebResource* webResource)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);

    WebKitWebResourcePrivate* priv = webResource->priv;

    if (!priv->resource)
        return NULL;

    if (!priv->data)
        priv->data = g_string_new_len(priv->resource->data()->data(), priv->resource->data()->size());

    return priv->data;
}

/**
 * webkit_web_resource_get_uri:
 * @web_resource: a #WebKitWebResource
 *
 * Return value: the URI of the resource
 *
 * Since: 1.1.14
 */
G_CONST_RETURN gchar* webkit_web_resource_get_uri(WebKitWebResource* webResource)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);

    WebKitWebResourcePrivate* priv = webResource->priv;


    // We may have an URI without having a resource assigned to us (e.g., if the
    // FrameLoaderClient only had a ResourceRequest when we got created
    if (priv->uri)
        return priv->uri;

    if (!priv->resource)
        return NULL;

    priv->uri = g_strdup(priv->resource->url().string().utf8().data());

    return priv->uri;
}

/**
 * webkit_web_resource_get_mime_type:
 * @web_resource: a #WebKitWebResource
 *
 * Return value: the MIME type of the resource
 *
 * Since: 1.1.14
 */
G_CONST_RETURN gchar* webkit_web_resource_get_mime_type(WebKitWebResource* webResource)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);

    WebKitWebResourcePrivate* priv = webResource->priv;
    if (!priv->resource)
        return NULL;

    if (!priv->mimeType)
        priv->mimeType = g_strdup(priv->resource->mimeType().utf8().data());

    return priv->mimeType;
}

/**
 * webkit_web_resource_get_encoding:
 * @web_resource: a #WebKitWebResource
 *
 * Return value: the encoding name of the resource
 *
 * Since: 1.1.14
 */
G_CONST_RETURN gchar* webkit_web_resource_get_encoding(WebKitWebResource* webResource)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);

    WebKitWebResourcePrivate* priv = webResource->priv;
    if (!priv->resource)
        return NULL;

    if (!priv->textEncoding)
        priv->textEncoding = g_strdup(priv->resource->textEncoding().utf8().data());

    return priv->textEncoding;
}

/**
 * webkit_web_resource_get_frame_name:
 * @web_resource: a #WebKitWebResource
 *
 * Return value: the frame name of the resource.
 *
 * Since: 1.1.14
 */
G_CONST_RETURN gchar* webkit_web_resource_get_frame_name(WebKitWebResource* webResource)
{
    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL);

    WebKitWebResourcePrivate* priv = webResource->priv;
    if (!priv->resource)
        return NULL;

    if (!priv->frameName)
        priv->frameName = g_strdup(priv->resource->frameName().utf8().data());

    return priv->frameName;
}