summaryrefslogtreecommitdiffstats
path: root/WebKit/gtk/webkit/webkitsecurityorigin.cpp
blob: 67ad0cf32fc99b2fc8ae25045fd951576a699be8 (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright (C) 2009 Martin Robinson, 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 "webkitwebdatabase.h"

#include "webkitprivate.h"

#include "PlatformString.h"
#include "DatabaseTracker.h"
#include <wtf/text/CString.h>

#include <glib/gi18n-lib.h>

/**
 * SECTION:webkitsecurityorigin
 * @short_description: A security boundary for web sites
 *
 * #WebKitSecurityOrigin is a representation of a security domain defined
 * by web sites. An origin consists of a host name, a protocol, and a port
 * number. Web sites with the same security origin can access each other's
 * resources for client-side scripting or database access.
 * 
 * Use #webkit_web_frame_get_security_origin to get the security origin of a
 * #WebKitWebFrame.
 *
 * Database quotas and usages are also defined per security origin. The
 * cumulative disk usage of an origin's databases may be retrieved with
 * #webkit_security_origin_get_web_database_usage. An origin's quota can be
 * adjusted with #webkit_security_origin_set_web_database_quota.
 */

using namespace WebKit;

enum {
    PROP_0,

    PROP_PROTOCOL,
    PROP_HOST,
    PROP_PORT,
    PROP_DATABASE_USAGE,
    PROP_DATABASE_QUOTA
};

G_DEFINE_TYPE(WebKitSecurityOrigin, webkit_security_origin, G_TYPE_OBJECT)

static void webkit_security_origin_finalize(GObject* object)
{
    WebKitSecurityOrigin* securityOrigin = WEBKIT_SECURITY_ORIGIN(object);
    WebKitSecurityOriginPrivate* priv = securityOrigin->priv;

    g_free(priv->protocol);
    g_free(priv->host);

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

static void webkit_security_origin_dispose(GObject* object)
{
    WebKitSecurityOrigin* securityOrigin = WEBKIT_SECURITY_ORIGIN(object);
    WebKitSecurityOriginPrivate* priv = securityOrigin->priv;

    if (!priv->disposed) {
        priv->coreOrigin->deref();
        g_hash_table_destroy(priv->webDatabases);
        priv->disposed = true;
    }

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

static void webkit_security_origin_set_property(GObject* object, guint propId, const GValue* value, GParamSpec* pspec)
{
    WebKitSecurityOrigin* securityOrigin = WEBKIT_SECURITY_ORIGIN(object);

    switch (propId) {
    case PROP_DATABASE_QUOTA:
        webkit_security_origin_set_web_database_quota(securityOrigin, g_value_get_uint64(value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, pspec);
        break;
    }
}

static void webkit_security_origin_get_property(GObject* object, guint propId, GValue* value, GParamSpec* pspec)
{
    WebKitSecurityOrigin* securityOrigin = WEBKIT_SECURITY_ORIGIN(object);

    switch (propId) {
    case PROP_PROTOCOL:
        g_value_set_string(value, webkit_security_origin_get_protocol(securityOrigin));
        break;
    case PROP_HOST:
        g_value_set_string(value, webkit_security_origin_get_host(securityOrigin));
        break;
    case PROP_PORT:
        g_value_set_uint(value, webkit_security_origin_get_port(securityOrigin));
        break;
    case PROP_DATABASE_USAGE:
        g_value_set_uint64(value, webkit_security_origin_get_web_database_usage(securityOrigin));
        break;
    case PROP_DATABASE_QUOTA:
        g_value_set_uint64(value, webkit_security_origin_get_web_database_quota(securityOrigin));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, pspec);
        break;
    }
}

static GHashTable* webkit_security_origins()
{
    static GHashTable* securityOrigins = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_object_unref);
    return securityOrigins;
}

static void webkit_security_origin_class_init(WebKitSecurityOriginClass* klass)
{
    GObjectClass* gobjectClass = G_OBJECT_CLASS(klass);
    gobjectClass->dispose = webkit_security_origin_dispose;
    gobjectClass->finalize = webkit_security_origin_finalize;
    gobjectClass->set_property = webkit_security_origin_set_property;
    gobjectClass->get_property = webkit_security_origin_get_property;

     /**
      * WebKitSecurityOrigin:protocol:
      *
      * The protocol of the security origin.
      *
      * Since: 1.1.14
      */
     g_object_class_install_property(gobjectClass, PROP_PROTOCOL,
                                     g_param_spec_string("protocol",
                                                         _("Protocol"),
                                                         _("The protocol of the security origin"),
                                                         NULL,
                                                         WEBKIT_PARAM_READABLE));

     /**
      * WebKitSecurityOrigin:host:
      *
      * The host of the security origin.
      *
      * Since: 1.1.14
      */
     g_object_class_install_property(gobjectClass, PROP_HOST,
                                     g_param_spec_string("host",
                                                         _("Host"),
                                                         _("The host of the security origin"),
                                                         NULL,
                                                         WEBKIT_PARAM_READABLE));

     /**
      * WebKitSecurityOrigin:port:
      *
      * The port of the security origin.
      *
      * Since: 1.1.14
      */
     g_object_class_install_property(gobjectClass, PROP_PORT,
                                     g_param_spec_uint("port",
                                                       _("Port"),
                                                       _("The port of the security origin"),
                                                       0, G_MAXUSHORT, 0,
                                                       WEBKIT_PARAM_READABLE));

      /**
      * WebKitSecurityOrigin:web-database-usage:
      *
      * The cumulative size of all web databases in the security origin in bytes.
      *
      * Since: 1.1.14
      */
      g_object_class_install_property(gobjectClass, PROP_DATABASE_USAGE,
                                      g_param_spec_uint64("web-database-usage",
                                                          _("Web Database Usage"),
                                                          _("The cumulative size of all web databases in the security origin"),
                                                          0, G_MAXUINT64, 0,
                                                          WEBKIT_PARAM_READABLE));
      /**
      * WebKitSecurityOrigin:web-database-quota:
      *
      * The web database qouta of the security origin in bytes.
      *
      * Since: 1.1.14
      */
      g_object_class_install_property(gobjectClass, PROP_DATABASE_QUOTA,
                                      g_param_spec_uint64("web-database-quota",
                                                          _("Web Database Quota"),
                                                          _("The web database quota of the security origin in bytes"),
                                                          0, G_MAXUINT64, 0,
                                                          WEBKIT_PARAM_READWRITE));

    g_type_class_add_private(klass, sizeof(WebKitSecurityOriginPrivate));
}

static void webkit_security_origin_init(WebKitSecurityOrigin* securityOrigin)
{
    WebKitSecurityOriginPrivate* priv = WEBKIT_SECURITY_ORIGIN_GET_PRIVATE(securityOrigin);
    priv->webDatabases = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
    securityOrigin->priv = priv;
}

/**
 * webkit_security_origin_get_protocol:
 * @security_origin: a #WebKitSecurityOrigin
 *
 * Returns the protocol for the security origin.
 *
 * Returns: the protocol for the security origin
 *
 * Since: 1.1.14
 **/
G_CONST_RETURN gchar* webkit_security_origin_get_protocol(WebKitSecurityOrigin* securityOrigin)
{
    g_return_val_if_fail(WEBKIT_IS_SECURITY_ORIGIN(securityOrigin), NULL);

    WebKitSecurityOriginPrivate* priv = securityOrigin->priv;
    WTF::String protocol =  priv->coreOrigin->protocol();

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

    return priv->protocol;
}

/**
 * webkit_security_origin_get_host:
 * @security_origin: a #WebKitSecurityOrigin
 *
 * Returns the hostname for the security origin.
 *
 * Returns: the hostname for the security origin
 *
 * Since: 1.1.14
 **/
G_CONST_RETURN gchar* webkit_security_origin_get_host(WebKitSecurityOrigin* securityOrigin)
{
    g_return_val_if_fail(WEBKIT_IS_SECURITY_ORIGIN(securityOrigin), NULL);

    WebKitSecurityOriginPrivate* priv = securityOrigin->priv;
    WTF::String host =  priv->coreOrigin->host();

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

    return priv->host;
}

/**
 * webkit_security_origin_get_port:
 * @security_origin: a #WebKitSecurityOrigin
 *
 * Returns the port for the security origin.
 *
 * Returns: the port for the security origin
 *
 * Since: 1.1.14
 **/
guint webkit_security_origin_get_port(WebKitSecurityOrigin* securityOrigin)
{
    g_return_val_if_fail(WEBKIT_IS_SECURITY_ORIGIN(securityOrigin), 0);

    WebCore::SecurityOrigin* coreOrigin = core(securityOrigin);
    return coreOrigin->port();
}

/**
 * webkit_security_origin_get_web_database_usage:
 * @security_origin: a #WebKitSecurityOrigin
 *
 * Returns the cumulative size of all Web Database database's in the origin
 * in bytes.
 *
 * Returns: the cumulative size of all databases 
 *
 * Since: 1.1.14
 **/
guint64 webkit_security_origin_get_web_database_usage(WebKitSecurityOrigin* securityOrigin)
{
    g_return_val_if_fail(WEBKIT_IS_SECURITY_ORIGIN(securityOrigin), 0);

#if ENABLE(DATABASE)
    WebCore::SecurityOrigin* coreOrigin = core(securityOrigin);
    return WebCore::DatabaseTracker::tracker().usageForOrigin(coreOrigin);
#else
    return 0;
#endif
}

/**
 * webkit_security_origin_get_web_database_quota:
 * @security_origin: a #WebKitSecurityOrigin
 *
 * Returns the quota for Web Database storage of the security origin
 * in bytes.
 *
 * Returns: the Web Database quota
 *
 * Since: 1.1.14
 **/
guint64 webkit_security_origin_get_web_database_quota(WebKitSecurityOrigin* securityOrigin)
{
    g_return_val_if_fail(WEBKIT_IS_SECURITY_ORIGIN(securityOrigin), 0);

#if ENABLE(DATABASE)
    WebCore::SecurityOrigin* coreOrigin = core(securityOrigin);
    return WebCore::DatabaseTracker::tracker().quotaForOrigin(coreOrigin);
#else
    return 0;
#endif
}

/**
 * webkit_security_origin_set_web_database_quota:
 * @security_origin: a #WebKitSecurityOrigin
 * @quota: a new Web Database quota in bytes
 *
 * Adjust the quota for Web Database storage of the security origin
 *
 * Since: 1.1.14
 **/
void webkit_security_origin_set_web_database_quota(WebKitSecurityOrigin* securityOrigin, guint64 quota)
{
    g_return_if_fail(WEBKIT_IS_SECURITY_ORIGIN(securityOrigin));

#if ENABLE(DATABASE)
    WebCore::SecurityOrigin* coreOrigin = core(securityOrigin);
    WebCore::DatabaseTracker::tracker().setQuota(coreOrigin, quota);
#endif
}

/**
 * webkit_security_origin_get_all_web_databases:
 * @security_origin: a #WebKitSecurityOrigin
 *
 * Returns a list of all Web Databases in the security origin.
 *
 * Returns: (transfer container) (element-type WebKitWebDatabase): a
 * #GList of databases in the security origin.
 *
 * Since: 1.1.14
 **/
GList* webkit_security_origin_get_all_web_databases(WebKitSecurityOrigin* securityOrigin)
{
    g_return_val_if_fail(WEBKIT_IS_SECURITY_ORIGIN(securityOrigin), NULL);
    GList* databases = NULL;

#if ENABLE(DATABASE)
    WebCore::SecurityOrigin* coreOrigin = core(securityOrigin);
    Vector<WTF::String> databaseNames;

    if (!WebCore::DatabaseTracker::tracker().databaseNamesForOrigin(coreOrigin, databaseNames))
        return NULL;

    for (unsigned i = 0; i < databaseNames.size(); ++i) {
        WebKitWebDatabase* database = webkit_security_origin_get_web_database(securityOrigin, databaseNames[i].utf8().data());
        databases = g_list_append(databases, database);
    }
#endif

    return databases;
}

WebKitSecurityOrigin* WebKit::kit(WebCore::SecurityOrigin* coreOrigin)
{
    ASSERT(coreOrigin);

    GHashTable* table = webkit_security_origins();
    WebKitSecurityOrigin* origin = (WebKitSecurityOrigin*) g_hash_table_lookup(table, coreOrigin);

    if (!origin) {
        origin = WEBKIT_SECURITY_ORIGIN(g_object_new(WEBKIT_TYPE_SECURITY_ORIGIN, NULL));
        origin->priv->coreOrigin = coreOrigin;
        g_hash_table_insert(table, coreOrigin, origin);
    }

    return origin;
}


WebCore::SecurityOrigin* WebKit::core(WebKitSecurityOrigin* securityOrigin)
{
    ASSERT(securityOrigin);

    return securityOrigin->priv->coreOrigin.get();
}

WebKitWebDatabase* webkit_security_origin_get_web_database(WebKitSecurityOrigin* securityOrigin, const gchar* databaseName)
{
    g_return_val_if_fail(WEBKIT_IS_SECURITY_ORIGIN(securityOrigin), NULL);

    WebKitSecurityOriginPrivate* priv = securityOrigin->priv;
    GHashTable* databaseHash = priv->webDatabases;
    WebKitWebDatabase* database = (WebKitWebDatabase*) g_hash_table_lookup(databaseHash, databaseName);

    if (!database) {
        database =  WEBKIT_WEB_DATABASE(g_object_new(WEBKIT_TYPE_WEB_DATABASE,
                                       "security-origin", securityOrigin,
                                       "name", databaseName,
                                        NULL));
        g_hash_table_insert(databaseHash, g_strdup(databaseName), database);
    }

    return database;
}