File: WebKitUserContent.cpp

package info (click to toggle)
webkit2gtk 2.42.2-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 362,452 kB
  • sloc: cpp: 2,881,971; javascript: 282,447; ansic: 134,088; python: 43,789; ruby: 18,308; perl: 15,872; asm: 14,389; xml: 4,395; yacc: 2,350; sh: 2,074; java: 1,734; lex: 1,323; makefile: 288; pascal: 60
file content (435 lines) | stat: -rw-r--r-- 15,804 bytes parent folder | download | duplicates (4)
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
425
426
427
428
429
430
431
432
433
434
435
/*
 * Copyright (C) 2014, 2018-2019 Igalia S.L.
 *
 * 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 "WebKitUserContent.h"

#include "WebKitUserContentPrivate.h"
#include <wtf/HashMap.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/text/CString.h>

using namespace WebCore;

API::ContentWorld& webkitContentWorld(const char* worldName)
{
    static NeverDestroyed<HashMap<CString, RefPtr<API::ContentWorld>>> map;
    return *map.get().ensure(worldName, [worldName = String::fromUTF8(worldName)] {
        return API::ContentWorld::sharedWorldWithName(worldName);
    }).iterator->value;
}

static inline UserContentInjectedFrames toUserContentInjectedFrames(WebKitUserContentInjectedFrames injectedFrames)
{
    switch (injectedFrames) {
    case WEBKIT_USER_CONTENT_INJECT_TOP_FRAME:
        return UserContentInjectedFrames::InjectInTopFrameOnly;
    case WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES:
        return UserContentInjectedFrames::InjectInAllFrames;
    default:
        ASSERT_NOT_REACHED();
        return UserContentInjectedFrames::InjectInAllFrames;
    }
}

static inline UserStyleLevel toUserStyleLevel(WebKitUserStyleLevel styleLevel)
{
    switch (styleLevel) {
    case WEBKIT_USER_STYLE_LEVEL_USER:
        return UserStyleUserLevel;
    case WEBKIT_USER_STYLE_LEVEL_AUTHOR:
        return UserStyleAuthorLevel;
    default:
        ASSERT_NOT_REACHED();
        return UserStyleAuthorLevel;
    }
}

static inline UserScriptInjectionTime toUserScriptInjectionTime(WebKitUserScriptInjectionTime injectionTime)
{
    switch (injectionTime) {
    case WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START:
        return UserScriptInjectionTime::DocumentStart;
    case WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END:
        return UserScriptInjectionTime::DocumentEnd;
    default:
        ASSERT_NOT_REACHED();
        return UserScriptInjectionTime::DocumentStart;
    }
}

static inline Vector<String> toStringVector(const char* const* strv)
{
    if (!strv)
        return Vector<String>();

    Vector<String> result;
    for (auto str = strv; *str; ++str)
        result.append(String::fromUTF8(*str));
    return result;
}

/**
 * WebKitUserStyleSheet: (ref-func webkit_user_style_sheet_ref) (unref-func webkit_user_style_sheet_unref)
 *
 * A CSS style sheet which can be injected in loaded pages.
 *
 * Since: 2.6
 */

struct _WebKitUserStyleSheet {
    _WebKitUserStyleSheet(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserStyleLevel level, const char* const* allowList, const char* const* blockList, API::ContentWorld& world)
        : userStyleSheet(adoptRef(new API::UserStyleSheet(UserStyleSheet {
            String::fromUTF8(source), URL { },
            toStringVector(allowList), toStringVector(blockList),
            toUserContentInjectedFrames(injectedFrames),
            toUserStyleLevel(level) }, world)))
        , referenceCount(1)
    {
    }

    RefPtr<API::UserStyleSheet> userStyleSheet;
    int referenceCount;
};

G_DEFINE_BOXED_TYPE(WebKitUserStyleSheet, webkit_user_style_sheet, webkit_user_style_sheet_ref, webkit_user_style_sheet_unref)

/**
 * webkit_user_style_sheet_ref:
 * @user_style_sheet: a #WebKitUserStyleSheet
 *
 * Atomically increments the reference count of @user_style_sheet by one.
 *
 * This function is MT-safe and may be called from any thread.
 *
 * Returns: The passed #WebKitUserStyleSheet
 *
 * Since: 2.6
 */
WebKitUserStyleSheet* webkit_user_style_sheet_ref(WebKitUserStyleSheet* userStyleSheet)
{
    g_atomic_int_inc(&userStyleSheet->referenceCount);
    return userStyleSheet;
}

/**
 * webkit_user_style_sheet_unref:
 * @user_style_sheet: a #WebKitUserStyleSheet
 *
 * Atomically decrements the reference count of @user_style_sheet by one.
 *
 * If the reference count drops to 0, all memory allocated by
 * #WebKitUserStyleSheet is released. This function is MT-safe and may be
 * called from any thread.
 *
 * Since: 2.6
 */
void webkit_user_style_sheet_unref(WebKitUserStyleSheet* userStyleSheet)
{
    if (g_atomic_int_dec_and_test(&userStyleSheet->referenceCount)) {
        userStyleSheet->~WebKitUserStyleSheet();
        fastFree(userStyleSheet);
    }
}

/**
 * webkit_user_style_sheet_new: (constructor)
 * @source: Source code of the user style sheet.
 * @injected_frames: A #WebKitUserContentInjectedFrames value
 * @level: A #WebKitUserStyleLevel
 * @allow_list: (array zero-terminated=1) (allow-none): An allow_list of URI patterns or %NULL
 * @block_list: (array zero-terminated=1) (allow-none): A block_list of URI patterns or %NULL
 *
 * Creates a new user style sheet.
 *
 * Style sheets can be applied to some URIs
 * only by passing non-null values for @allow_list or @block_list. Passing a
 * %NULL allow_list implies that all URIs are on the allow_list. The style
 * sheet is applied if an URI matches the allow_list and not the block_list.
 * URI patterns must be of the form `[protocol]://[host]/[path]`, where the
 * *host* and *path* components can contain the wildcard character (`*`) to
 * represent zero or more other characters.
 *
 * Returns: A new #WebKitUserStyleSheet
 *
 * Since: 2.6
 */
WebKitUserStyleSheet* webkit_user_style_sheet_new(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserStyleLevel level, const char* const* allowList, const char* const* blockList)
{
    g_return_val_if_fail(source, nullptr);
    WebKitUserStyleSheet* userStyleSheet = static_cast<WebKitUserStyleSheet*>(fastMalloc(sizeof(WebKitUserStyleSheet)));
    new (userStyleSheet) WebKitUserStyleSheet(source, injectedFrames, level, allowList, blockList, API::ContentWorld::pageContentWorld());
    return userStyleSheet;
}

/**
 * webkit_user_style_sheet_new_for_world: (constructor)
 * @source: Source code of the user style sheet.
 * @injected_frames: A #WebKitUserContentInjectedFrames value
 * @level: A #WebKitUserStyleLevel
 * @world_name: the name of a #WebKitScriptWorld
 * @allow_list: (array zero-terminated=1) (allow-none): An allow_list of URI patterns or %NULL
 * @block_list: (array zero-terminated=1) (allow-none): A block_list of URI patterns or %NULL
 *
 * Creates a new user style sheet for script world.
 *
 * Creates a new user style sheet for script world with name @world_name.
 * See webkit_user_style_sheet_new() for a full description.
 *
 * Returns: A new #WebKitUserStyleSheet
 *
 * Since: 2.22
 */
WebKitUserStyleSheet* webkit_user_style_sheet_new_for_world(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserStyleLevel level, const char* worldName, const char* const* allowList, const char* const* blockList)
{
    g_return_val_if_fail(source, nullptr);
    g_return_val_if_fail(worldName, nullptr);

    WebKitUserStyleSheet* userStyleSheet = static_cast<WebKitUserStyleSheet*>(fastMalloc(sizeof(WebKitUserStyleSheet)));
    new (userStyleSheet) WebKitUserStyleSheet(source, injectedFrames, level, allowList, blockList, webkitContentWorld(worldName));
    return userStyleSheet;
}

API::UserStyleSheet& webkitUserStyleSheetGetUserStyleSheet(WebKitUserStyleSheet* userStyleSheet)
{
    return *userStyleSheet->userStyleSheet;
}

/**
 * WebKitUserScript: (ref-func webkit_user_script_ref) (unref-func webkit_user_script_unref)
 *
 * A JavaScript snippet which can be injected in loaded pages.
 *
 * Since: 2.6
 */

struct _WebKitUserScript {
    _WebKitUserScript(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserScriptInjectionTime injectionTime, const gchar* const* allowList, const gchar* const* blockList, API::ContentWorld& world)
        : userScript(adoptRef(new API::UserScript(UserScript {
            String::fromUTF8(source), URL { },
            toStringVector(allowList), toStringVector(blockList),
            toUserScriptInjectionTime(injectionTime),
            toUserContentInjectedFrames(injectedFrames), WebCore::WaitForNotificationBeforeInjecting::No }, world)))
        , referenceCount(1)
    {
    }

    RefPtr<API::UserScript> userScript;
    int referenceCount;
};

G_DEFINE_BOXED_TYPE(WebKitUserScript, webkit_user_script, webkit_user_script_ref, webkit_user_script_unref)

/**
 * webkit_user_script_ref:
 * @user_script: a #WebKitUserScript
 *
 * Atomically increments the reference count of @user_script by one.
 *
 * This function is MT-safe and may be called from any thread.
 *
 * Returns: The passed #WebKitUserScript
 *
 * Since: 2.6
 */
WebKitUserScript* webkit_user_script_ref(WebKitUserScript* userScript)
{
    g_atomic_int_inc(&userScript->referenceCount);
    return userScript;
}

/**
 * webkit_user_script_unref:
 * @user_script: a #WebKitUserScript
 *
 * Atomically decrements the reference count of @user_script by one.
 *
 * If the reference count drops to 0, all memory allocated by
 * #WebKitUserScript is released. This function is MT-safe and may be called
 * from any thread.
 *
 * Since: 2.6
 */
void webkit_user_script_unref(WebKitUserScript* userScript)
{
    if (g_atomic_int_dec_and_test(&userScript->referenceCount)) {
        userScript->~WebKitUserScript();
        fastFree(userScript);
    }
}

/**
 * webkit_user_script_new: (constructor)
 * @source: Source code of the user script.
 * @injected_frames: A #WebKitUserContentInjectedFrames value
 * @injection_time: A #WebKitUserScriptInjectionTime value
 * @allow_list: (array zero-terminated=1) (allow-none): An allow_list of URI patterns or %NULL
 * @block_list: (array zero-terminated=1) (allow-none): A block_list of URI patterns or %NULL
 *
 * Creates a new user script.
 *
 * Scripts can be applied to some URIs
 * only by passing non-null values for @allow_list or @block_list. Passing a
 * %NULL allow_list implies that all URIs are on the allow_list. The script
 * is applied if an URI matches the allow_list and not the block_list.
 * URI patterns must be of the form `[protocol]://[host]/[path]`, where the
 * *host* and *path* components can contain the wildcard character (`*`) to
 * represent zero or more other characters.
 *
 * Returns: A new #WebKitUserScript
 *
 * Since: 2.6
 */
WebKitUserScript* webkit_user_script_new(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserScriptInjectionTime injectionTime, const gchar* const* allowList, const gchar* const* blockList)
{
    g_return_val_if_fail(source, nullptr);
    WebKitUserScript* userScript = static_cast<WebKitUserScript*>(fastMalloc(sizeof(WebKitUserScript)));
    new (userScript) WebKitUserScript(source, injectedFrames, injectionTime, allowList, blockList, API::ContentWorld::pageContentWorld());
    return userScript;
}

/**
 * webkit_user_script_new_for_world: (constructor)
 * @source: Source code of the user script.
 * @injected_frames: A #WebKitUserContentInjectedFrames value
 * @injection_time: A #WebKitUserScriptInjectionTime value
 * @world_name: the name of a #WebKitScriptWorld
 * @allow_list: (array zero-terminated=1) (allow-none): An allow_list of URI patterns or %NULL
 * @block_list: (array zero-terminated=1) (allow-none): A block_list of URI patterns or %NULL
 *
 * Creates a new user script for script world with name @world_name.
 *
 * See webkit_user_script_new() for a full description.
 *
 * Returns: A new #WebKitUserScript
 *
 * Since: 2.22
 */
WebKitUserScript* webkit_user_script_new_for_world(const gchar* source, WebKitUserContentInjectedFrames injectedFrames, WebKitUserScriptInjectionTime injectionTime, const char* worldName, const gchar* const* allowList, const gchar* const* blockList)
{
    g_return_val_if_fail(source, nullptr);
    g_return_val_if_fail(worldName, nullptr);

    WebKitUserScript* userScript = static_cast<WebKitUserScript*>(fastMalloc(sizeof(WebKitUserScript)));
    new (userScript) WebKitUserScript(source, injectedFrames, injectionTime, allowList, blockList, webkitContentWorld(worldName));
    return userScript;
}

API::UserScript& webkitUserScriptGetUserScript(WebKitUserScript* userScript)
{
    return *userScript->userScript;
}


/**
 * WebKitUserContentFilter: (ref-func webkit_user_content_filter_ref) (unref-func webkit_user_content_filter_unref)
 *
 * A compiled set of rules which applied to resource loads.
 *
 * Since: 2.24
 */

struct _WebKitUserContentFilter {
    _WebKitUserContentFilter(RefPtr<API::ContentRuleList>&& contentRuleList)
        : contentRuleList(WTFMove(contentRuleList))
#if ENABLE(CONTENT_EXTENSIONS)
        , identifier(this->contentRuleList->name().utf8())
#endif
        , referenceCount(1)
    {
    }

    RefPtr<API::ContentRuleList> contentRuleList;
    CString identifier;
    int referenceCount;
};

G_DEFINE_BOXED_TYPE(WebKitUserContentFilter, webkit_user_content_filter, webkit_user_content_filter_ref, webkit_user_content_filter_unref)

/**
 * webkit_user_content_filter_ref:
 * @user_content_filter: A #WebKitUserContentFilter
 *
 * Atomically increments the reference count of @user_content_filter by one.
 *
 * This function is MT-safe and may be called from any thread.
 *
 * Returns: the @user_content_filter passed in.
 *
 * Since: 2.24
 */
WebKitUserContentFilter* webkit_user_content_filter_ref(WebKitUserContentFilter* userContentFilter)
{
    g_return_val_if_fail(userContentFilter, nullptr);
    g_atomic_int_inc(&userContentFilter->referenceCount);
    return userContentFilter;
}

/**
 * webkit_user_content_filter_unref:
 * @user_content_filter: A #WebKitUserContentFilter
 *
 * Atomically decrements the reference count of @user_content_filter by one.
 *
 * If the reference count drops to 0, all the memory allocated by the
 * #WebKitUserContentFilter is released. This function is MT-safe and may
 * be called from any thread.
 *
 * Since: 2.24
 */
void webkit_user_content_filter_unref(WebKitUserContentFilter* userContentFilter)
{
    g_return_if_fail(userContentFilter);
    if (g_atomic_int_dec_and_test(&userContentFilter->referenceCount)) {
        userContentFilter->~WebKitUserContentFilter();
        fastFree(userContentFilter);
    }
}

/**
 * webkit_user_content_filter_get_identifier:
 * @user_content_filter: A #WebKitUserContentFilter
 *
 * Obtain the identifier previously used to save the @user_content_filter.
 *
 * Obtain the identifier previously used to save the @user_content_filter in the
 * #WebKitUserContentFilterStore.
 *
 * Returns: (transfer none): the identifier for the filter
 *
 * Since: 2.24
 */
const char* webkit_user_content_filter_get_identifier(WebKitUserContentFilter* userContentFilter)
{
    g_return_val_if_fail(userContentFilter, nullptr);
    return userContentFilter->identifier.data();
}

WebKitUserContentFilter* webkitUserContentFilterCreate(RefPtr<API::ContentRuleList>&& contentRuleList)
{
    WebKitUserContentFilter* userContentFilter = static_cast<WebKitUserContentFilter*>(fastMalloc(sizeof(WebKitUserContentFilter)));
    new (userContentFilter) WebKitUserContentFilter(WTFMove(contentRuleList));
    return userContentFilter;
}

API::ContentRuleList& webkitUserContentFilterGetContentRuleList(WebKitUserContentFilter* userContentFilter)
{
    ASSERT(userContentFilter);
    return *userContentFilter->contentRuleList;
}