File: WebKitFeature.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (434 lines) | stat: -rw-r--r-- 13,247 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2023 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 "WebKitFeature.h"

#include "APIFeature.h"
#include "WebKitFeaturePrivate.h"
#include <wtf/Algorithms.h>
#include <wtf/RefPtr.h>

static inline WebKitFeatureStatus toFeatureStatus(API::FeatureStatus status)
{
    switch (status) {
    case API::FeatureStatus::Embedder:
        return WEBKIT_FEATURE_STATUS_EMBEDDER;
    case API::FeatureStatus::Unstable:
        return WEBKIT_FEATURE_STATUS_UNSTABLE;
    case API::FeatureStatus::Internal:
        return WEBKIT_FEATURE_STATUS_INTERNAL;
    case API::FeatureStatus::Developer:
        return WEBKIT_FEATURE_STATUS_DEVELOPER;
    case API::FeatureStatus::Testable:
        return WEBKIT_FEATURE_STATUS_TESTABLE;
    case API::FeatureStatus::Preview:
        return WEBKIT_FEATURE_STATUS_PREVIEW;
    case API::FeatureStatus::Stable:
        return WEBKIT_FEATURE_STATUS_STABLE;
    case API::FeatureStatus::Mature:
        return WEBKIT_FEATURE_STATUS_MATURE;
    }
    RELEASE_ASSERT_NOT_REACHED();
}

static inline const char* toFeatureCategory(API::FeatureCategory category)
{
    switch (category) {
    case API::FeatureCategory::None:
        return "Other";
    case API::FeatureCategory::Animation:
        return "Animation";
    case API::FeatureCategory::CSS:
        return "CSS";
    case API::FeatureCategory::DOM:
        return "DOM";
    case API::FeatureCategory::Extensions:
        return "Extensions";
    case API::FeatureCategory::HTML:
        return "HTML";
    case API::FeatureCategory::Javascript:
        return "JavaScript";
    case API::FeatureCategory::Media:
        return "Media";
    case API::FeatureCategory::Networking:
        return "Network";
    case API::FeatureCategory::Privacy:
        return "Privacy";
    case API::FeatureCategory::Security:
        return "Security";
    }
    RELEASE_ASSERT_NOT_REACHED();
}

/**
 * WebKitFeature: (ref-func webkit_feature_ref) (unref-func webkit_feature_unref)
 *
 * Describes a web engine feature that may be toggled at runtime.
 *
 * The WebKit web engine includes a set of features which may be toggled
 * programmatically, each one represented by a #WebKitFeature that provides
 * information about it:
 *
 * - A unique “identifier”: [method@Feature.get_identifier].
 * - A “default value”, which indicates whether the option is enabled
 *   automatically: [method@Feature.get_default_value].
 * - Its “status”, which determines whether it should be considered
 *   user-settable and its development stage (see [enum@FeatureStatus]
 *   for details): [method@Feature.get_status].
 * - A category, which may be used to group features together:
 *   [method@Feature.get_category].
 * - An optional short “name” which can be presented to an user:
 *   [method@Feature.get_name].
 * - An optional longer “detailed” description:
 *   [method@Feature.get_details].
 *
 * The lists of available features can be obtained with
 * [func@Settings.get_all_features], [func@Settings.get_experimental_features],
 * and [func@Settings.get_development_features]). As a rule of thumb,
 * applications which may want to allow users (i.e. web developers) to test
 * WebKit features should use the list of experimental features. Additionally,
 * applications might want to expose development features *when targeting
 * technically inclined users* for early testing of in-development features
 * (i.e. in “technology preview” or “canary” builds).
 *
 * Applications **must not** expose the list of all features to end users
 * because they often lack descriptions and control parts of the web engine
 * which are either intended to be used during development of WebKit itself,
 * or in specific scenarios to tweak how WebKit integrates with the
 * application.
 *
 * Since: 2.42
 */
struct _WebKitFeature {
    _WebKitFeature(RefPtr<API::Feature>&& feature)
        : feature(WTFMove(feature))
    {
    }

    RefPtr<API::Feature> feature;
    CString identifier { toIdentifier(feature->key()) };
    CString name { feature->name().utf8() };
    CString details { feature->details().utf8() };
    int referenceCount { 1 };

    static CString toIdentifier(const String& key)
    {
        if (key.endsWith("Enabled"_s))
            return StringView(key).left(key.length() - (sizeof("Enabled") - 1)).utf8();
        return key.utf8();
    }
};

G_DEFINE_BOXED_TYPE(WebKitFeature, webkit_feature, webkit_feature_ref, webkit_feature_unref)

static WebKitFeature* webkitFeatureCreate(const RefPtr<API::Object>& apiFeature)
{
    ASSERT(apiFeature->type() == API::Object::Type::Feature);
    WebKitFeature* feature = static_cast<WebKitFeature*>(fastMalloc(sizeof(WebKitFeature)));
    new (feature) WebKitFeature(static_pointer_cast<API::Feature>(apiFeature));
    return feature;
}

API::Feature& webkitFeatureGetFeature(WebKitFeature* feature)
{
    ASSERT(feature);
    return *feature->feature;
}

/**
 * webkit_feature_ref:
 * @feature: a #WebKitFeature
 *
 * Atomically acquires a reference on the given @feature.
 *
 * This function is MT-safe and may be called from any thread.
 *
 * Returns: The same @feature with an additional reference.
 *
 * Since: 2.42
 */
WebKitFeature* webkit_feature_ref(WebKitFeature* feature)
{
    g_return_val_if_fail(feature, nullptr);
    g_atomic_int_inc(&feature->referenceCount);
    return feature;
}

/**
 * webkit_feature_unref:
 * @feature: a #WebKitFeature
 *
 * Atomically releases a reference on the given @feature.
 *
 * If the reference was the last, the resources associated to the
 * @feature are freed. This function is MT-safe and may be called from
 * any thread.
 *
 * Since: 2.42
 */
void webkit_feature_unref(WebKitFeature* feature)
{
    g_return_if_fail(feature);
    if (g_atomic_int_dec_and_test(&feature->referenceCount)) {
        feature->~WebKitFeature();
        fastFree(feature);
    }
}

/**
 * webkit_feature_get_identifier:
 * @feature: a #WebKitFeature
 *
 * Gets a string that uniquely identifies the @feature.
 *
 * Returns: (transfer none): The identifier string for the feature.
 *
 * Since: 2.42
 */
const char* webkit_feature_get_identifier(WebKitFeature* feature)
{
    g_return_val_if_fail(feature, nullptr);
    return feature->identifier.data();
}

/**
 * webkit_feature_get_name:
 * @feature: a #WebKitFeature
 *
 * Gets a short name for the @feature.
 *
 * The returned string is suitable to be displayed to end users, but it
 * should not be relied upon being localized.
 *
 * Note that some *features may not* have a short name, and @NULL
 * is returned in this case.
 *
 * Returns: (transfer none) (nullable): Short feature name.
 *
 * Since: 2.42
 */
const char* webkit_feature_get_name(WebKitFeature* feature)
{
    g_return_val_if_fail(feature, nullptr);
    return feature->name.length() ? feature->name.data() : nullptr;
}

/**
 * webkit_feature_get_details:
 * @feature: a #WebKitFeature
 *
 * Gets a description for the @feature.
 *
 * The detailed description should be considered an additional clarification
 * on the purpose of the feature, to be used as complementary aid to be
 * displayed along the feature name returned by [method@Feature.get_name].
 * The returned string is suitable to be displayed to end users, but it
 * should not be relied upon being localized.
 *
 * Note that some *features may not* have a detailed description, and @NULL
 * is returned in this case.
 *
 * Returns: (transfer none) (nullable): Feature description.
 *
 * Since: 2.42
 */
const char* webkit_feature_get_details(WebKitFeature* feature)
{
    g_return_val_if_fail(feature, nullptr);
    return feature->details.length() ? feature->details.data() : nullptr;
}

/**
 * webkit_feature_get_status:
 * @feature: a #WebKitFeature
 *
 * Gets the status of the feature.
 *
 * Returns: Feature status.
 *
 * Since: 2.42
 */
WebKitFeatureStatus webkit_feature_get_status(WebKitFeature* feature)
{
    g_return_val_if_fail(feature, WEBKIT_FEATURE_STATUS_EMBEDDER);
    return toFeatureStatus(feature->feature->status());
}

/**
 * webkit_feature_get_category:
 * @feature: a #WebKitFeature
 *
 * Gets the category of the feature.
 *
 * Applications which include user interface to toggle features may want
 * to use the category to group related features together.
 *
 * Returns: Feature category.
 *
 * Since: 2.42
 */
const char* webkit_feature_get_category(WebKitFeature* feature)
{
    g_return_val_if_fail(feature, "None");
    return toFeatureCategory(feature->feature->category());
}

/**
 * webkit_feature_get_default_value:
 * @feature: a #WebKitFeature
 *
 * Gets whether the feature is enabled by default.
 *
 * The default value may be used by applications which include user interface
 * to toggle features to restore its settings to their defaults. Note that
 * whether a feature is actually enabled must be checked with
 * [method@Settings.get_feature_enabled].
 *
 * Returns: Whether the feature is enabled by default.
 *
 * Since: 2.42
 */
gboolean webkit_feature_get_default_value(WebKitFeature* feature)
{
    g_return_val_if_fail(feature, FALSE);
    return feature->feature->defaultValue() ? TRUE : FALSE;
}

/**
 * WebKitFeatureList: (ref-func webkit_feature_list_ref) (unref-func webkit_feature_list_unref)
 *
 * Contains a set of toggle-able web engine features.
 *
 * The list supports passing around a set of [struct@Feature] objects and
 * iterating over them:
 *
 * ```c
 * g_autoptr(WebKitFeatureList) list = webkit_settings_get_experimental_features();
 * for (gsize i = 0; i < webkit_feature_list_get_length(list): i++) {
 *     WebKitFeature *feature = webkit_feature_list_get(list, i);
 *     // Do something with "feature".
 * }
 * ```
 *
 * Lists of features can be obtained with
 * [func@Settings.get_experimental_features],
 * [func@Settings.get_development_features], and
 * [func@Settings.get_all_features].
 *
 * Since: 2.42
 */
struct _WebKitFeatureList {
    _WebKitFeatureList(const Vector<RefPtr<API::Object>>& features)
        : items(features.map(webkitFeatureCreate))
    {
    }

    ~_WebKitFeatureList()
    {
        for (auto* item : items)
            webkit_feature_unref(item);
    }

    Vector<WebKitFeature*> items;
    int referenceCount { 1 };
};

G_DEFINE_BOXED_TYPE(WebKitFeatureList, webkit_feature_list, webkit_feature_list_ref, webkit_feature_list_unref)

WebKitFeatureList* webkitFeatureListCreate(const Vector<RefPtr<API::Object>>& features)
{
    WebKitFeatureList* featureList = static_cast<WebKitFeatureList*>(fastMalloc(sizeof(WebKitFeatureList)));
    new (featureList) WebKitFeatureList(features);
    return featureList;
}

/**
 * webkit_feature_list_ref:
 * @feature_list: a #WebKitFeatureList
 *
 * Atomically acquires a reference on the given @feature_list.
 *
 * This function is MT-safe and may be called from any thread.
 *
 * Returns: The same @feature_list with an additional reference.
 *
 * Since: 2.42
 */
WebKitFeatureList* webkit_feature_list_ref(WebKitFeatureList* featureList)
{
    g_return_val_if_fail(featureList, nullptr);
    g_atomic_int_inc(&featureList->referenceCount);
    return featureList;
}

/**
 * webkit_feature_list_unref:
 * @feature_list: a #WebKitFeatureList
 *
 * Atomically releases a reference on the given @feature_list.
 *
 * If the reference was the last, the resources associated to the
 * @feature_list are freed. This function is MT-safe and may be called
 * from any thread.
 *
 * Since: 2.42
 */
void webkit_feature_list_unref(WebKitFeatureList* featureList)
{
    g_return_if_fail(featureList);
    if (g_atomic_int_dec_and_test(&featureList->referenceCount)) {
        featureList->~WebKitFeatureList();
        fastFree(featureList);
    }
}

/**
 * webkit_feature_list_get_length:
 * @feature_list: a #WebKitFeatureList
 *
 * Gets the number of elements in the feature list.
 *
 * Returns: number of elements.
 *
 * Since 2.42
 */
gsize webkit_feature_list_get_length(WebKitFeatureList* featureList)
{
    g_return_val_if_fail(featureList, 0);
    return featureList->items.size();
}

/**
 * webkit_feature_list_get:
 * @feature_list: a #WebKitFeatureList
 * @index: index of the feature
 *
 * Gets a feature given its index.
 *
 * Returns: (transfer none): The feature at @index.
 *
 * Since: 2.42
 */
WebKitFeature* webkit_feature_list_get(WebKitFeatureList* featureList, gsize index)
{
    g_return_val_if_fail(featureList, nullptr);
    g_return_val_if_fail(index < featureList->items.size(), nullptr);
    return featureList->items[index];
}