/
WebKitWebContext.cpp
2055 lines (1837 loc) · 72.2 KB
/
WebKitWebContext.cpp
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011 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 "WebKitWebContext.h"
#include "APIAutomationClient.h"
#include "APIInjectedBundleClient.h"
#include "APIPageConfiguration.h"
#include "APIProcessPoolConfiguration.h"
#include "APIString.h"
#include "LegacyGlobalSettings.h"
#include "NetworkProcessMessages.h"
#include "TextChecker.h"
#include "TextCheckerState.h"
#include "WebAutomationSession.h"
#include "WebKitAutomationSessionPrivate.h"
#include "WebKitDownloadPrivate.h"
#include "WebKitGeolocationManagerPrivate.h"
#include "WebKitInitialize.h"
#include "WebKitInjectedBundleClient.h"
#include "WebKitMemoryPressureSettings.h"
#include "WebKitMemoryPressureSettingsPrivate.h"
#include "WebKitNotificationProvider.h"
#include "WebKitPrivate.h"
#include "WebKitProtocolHandler.h"
#include "WebKitSecurityManagerPrivate.h"
#include "WebKitSecurityOriginPrivate.h"
#include "WebKitSettingsPrivate.h"
#include "WebKitURISchemeRequestPrivate.h"
#include "WebKitUserContentManagerPrivate.h"
#include "WebKitUserMessagePrivate.h"
#include "WebKitWebContextPrivate.h"
#include "WebKitWebViewPrivate.h"
#include "WebKitWebsiteDataManagerPrivate.h"
#include "WebKitWebsitePoliciesPrivate.h"
#include "WebNotificationManagerProxy.h"
#include "WebProcessMessages.h"
#include "WebURLSchemeHandler.h"
#include "WebsiteDataStore.h"
#include "WebsiteDataType.h"
#include <JavaScriptCore/RemoteInspector.h>
#include <WebCore/ContentSecurityPolicy.h>
#include <WebCore/ResourceLoaderIdentifier.h>
#include <cstdlib>
#include <glib/gi18n-lib.h>
#include <libintl.h>
#include <memory>
#include <pal/HysteresisActivity.h>
#include <wtf/DateMath.h>
#include <wtf/FileSystem.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
#include <wtf/URLParser.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GUniquePtr.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>
#if PLATFORM(GTK)
#include "WebKitRemoteInspectorProtocolHandler.h"
#endif
#if ENABLE(2022_GLIB_API)
#include "WebKitNetworkSession.h"
#endif
#if !ENABLE(2022_GLIB_API)
#include "WebKitFaviconDatabasePrivate.h"
#endif
using namespace WebKit;
/**
* WebKitWebContext:
*
* Manages aspects common to all #WebKitWebView<!-- -->s
*
* The #WebKitWebContext manages all aspects common to all
* #WebKitWebView<!-- -->s.
*
* You can define the #WebKitCacheModel with
* webkit_web_context_set_cache_model(), depending on the needs of
* your application. You can access the #WebKitSecurityManager to specify
* the behaviour of your application regarding security using
* webkit_web_context_get_security_manager().
*
* It is also possible to change your preferred language or enable
* spell checking, using webkit_web_context_set_preferred_languages(),
* webkit_web_context_set_spell_checking_languages() and
* webkit_web_context_set_spell_checking_enabled().
*
* You can use webkit_web_context_register_uri_scheme() to register
* custom URI schemes, and manage several other settings.
*
* TLS certificate validation failure is now treated as a transport
* error by default. To handle TLS failures differently, you can
* connect to #WebKitWebView::load-failed-with-tls-errors.
* Alternatively, you can use webkit_web_context_set_tls_errors_policy()
* to set the policy %WEBKIT_TLS_ERRORS_POLICY_IGNORE; however, this is
* not appropriate for Internet applications.
*/
enum {
PROP_0,
#if PLATFORM(GTK) && !USE(GTK4)
PROP_LOCAL_STORAGE_DIRECTORY,
#endif
#if !ENABLE(2022_GLIB_API)
PROP_WEBSITE_DATA_MANAGER,
#endif
#if PLATFORM(GTK) && !USE(GTK4)
PROP_PSON_ENABLED,
PROP_USE_SYSTEM_APPEARANCE_FOR_SCROLLBARS,
#endif
PROP_MEMORY_PRESSURE_SETTINGS,
PROP_TIME_ZONE_OVERRIDE,
N_PROPERTIES,
};
static GParamSpec* sObjProperties[N_PROPERTIES] = { nullptr, };
enum {
#if !ENABLE(2022_GLIB_API)
DOWNLOAD_STARTED,
#endif
INITIALIZE_WEB_PROCESS_EXTENSIONS,
INITIALIZE_NOTIFICATION_PERMISSIONS,
AUTOMATION_STARTED,
USER_MESSAGE_RECEIVED,
LAST_SIGNAL
};
class WebKitURISchemeHandler final : public WebURLSchemeHandler {
public:
static Ref<WebKitURISchemeHandler> create(WebKitWebContext* context, WebKitURISchemeRequestCallback callback, void* userData, GDestroyNotify destroyNotify)
{
return adoptRef(*new WebKitURISchemeHandler(context, callback, userData, destroyNotify));
}
~WebKitURISchemeHandler()
{
if (m_destroyNotify)
m_destroyNotify(m_userData);
}
private:
WebKitURISchemeHandler(WebKitWebContext* context, WebKitURISchemeRequestCallback callback, void* userData, GDestroyNotify destroyNotify)
: m_context(context)
, m_callback(callback)
, m_userData(userData)
, m_destroyNotify(destroyNotify)
{
}
void platformStartTask(WebPageProxy& page, WebURLSchemeTask& task) final
{
if (!m_callback)
return;
GRefPtr<WebKitURISchemeRequest> request = adoptGRef(webkitURISchemeRequestCreate(m_context, page, task));
auto addResult = m_requests.add({ task.resourceLoaderID(), task.pageProxyID() }, WTFMove(request));
ASSERT(addResult.isNewEntry);
m_callback(addResult.iterator->value.get(), m_userData);
}
void platformStopTask(WebPageProxy&, WebURLSchemeTask& task) final
{
auto it = m_requests.find({ task.resourceLoaderID(), task.pageProxyID() });
if (it == m_requests.end())
return;
webkitURISchemeRequestCancel(it->value.get());
m_requests.remove(it);
}
void platformTaskCompleted(WebURLSchemeTask& task) final
{
m_requests.remove({ task.resourceLoaderID(), task.pageProxyID() });
}
WebKitWebContext* m_context { nullptr };
WebKitURISchemeRequestCallback m_callback { nullptr };
void* m_userData { nullptr };
GDestroyNotify m_destroyNotify { nullptr };
HashMap<std::pair<WebCore::ResourceLoaderIdentifier, WebPageProxyIdentifier>, GRefPtr<WebKitURISchemeRequest>> m_requests;
};
typedef HashMap<String, RefPtr<WebKitURISchemeHandler> > URISchemeHandlerMap;
class WebKitAutomationClient;
struct _WebKitWebContextPrivate {
#if !ENABLE(2022_GLIB_API)
_WebKitWebContextPrivate()
: dnsPrefetchHystereris([this](PAL::HysteresisState state) { if (state == PAL::HysteresisState::Stopped) dnsPrefetchedHosts.clear(); })
{
}
#endif
RefPtr<WebProcessPool> processPool;
bool clientsDetached;
#if PLATFORM(GTK) && !USE(GTK4)
bool psonEnabled;
bool useSystemAppearanceForScrollbars;
#endif
#if !ENABLE(2022_GLIB_API)
GRefPtr<WebKitFaviconDatabase> faviconDatabase;
CString faviconDatabaseDirectory;
#endif
GRefPtr<WebKitSecurityManager> securityManager;
URISchemeHandlerMap uriSchemeHandlers;
GRefPtr<WebKitGeolocationManager> geolocationManager;
std::unique_ptr<WebKitNotificationProvider> notificationProvider;
#if !ENABLE(2022_GLIB_API)
GRefPtr<WebKitWebsiteDataManager> websiteDataManager;
#endif
HashMap<WebPageProxyIdentifier, WebKitWebView*> webViews;
CString webProcessExtensionsDirectory;
GRefPtr<GVariant> webProcessExtensionsInitializationUserData;
CString localStorageDirectory;
#if ENABLE(REMOTE_INSPECTOR)
#if PLATFORM(GTK)
std::unique_ptr<RemoteInspectorProtocolHandler> remoteInspectorProtocolHandler;
#endif
std::unique_ptr<WebKitAutomationClient> automationClient;
GRefPtr<WebKitAutomationSession> automationSession;
#if ENABLE(2022_GLIB_API)
GRefPtr<WebKitNetworkSession> automationNetworkSession;
#endif
#endif
std::unique_ptr<WebKitProtocolHandler> webkitProtocolHandler;
#if !ENABLE(2022_GLIB_API)
HashSet<String> dnsPrefetchedHosts;
PAL::HysteresisActivity dnsPrefetchHystereris;
#endif
WebKitMemoryPressureSettings* memoryPressureSettings;
CString timeZoneOverride;
};
static guint signals[LAST_SIGNAL] = { 0, };
WEBKIT_DEFINE_FINAL_TYPE(WebKitWebContext, webkit_web_context, G_TYPE_OBJECT, GObject)
std::unique_ptr<WebKitNotificationProvider> s_serviceWorkerNotificationProvider;
#if ENABLE(REMOTE_INSPECTOR)
class WebKitAutomationClient final : Inspector::RemoteInspector::Client {
WTF_MAKE_FAST_ALLOCATED;
public:
explicit WebKitAutomationClient(WebKitWebContext* context)
: m_webContext(context)
{
Inspector::RemoteInspector::singleton().setClient(this);
}
~WebKitAutomationClient()
{
Inspector::RemoteInspector::singleton().setClient(nullptr);
}
private:
bool remoteAutomationAllowed() const override { return true; }
String browserName() const override
{
if (!m_webContext->priv->automationSession)
return { };
return webkitAutomationSessionGetBrowserName(m_webContext->priv->automationSession.get());
}
String browserVersion() const override
{
if (!m_webContext->priv->automationSession)
return { };
return webkitAutomationSessionGetBrowserVersion(m_webContext->priv->automationSession.get());
}
void requestAutomationSession(const String& sessionIdentifier, const Inspector::RemoteInspector::Client::SessionCapabilities& capabilities) override
{
ASSERT(!m_webContext->priv->automationSession);
m_webContext->priv->automationSession = adoptGRef(webkitAutomationSessionCreate(m_webContext, sessionIdentifier.utf8().data(), capabilities));
g_signal_emit(m_webContext, signals[AUTOMATION_STARTED], 0, m_webContext->priv->automationSession.get());
m_webContext->priv->processPool->setAutomationSession(&webkitAutomationSessionGetSession(m_webContext->priv->automationSession.get()));
}
WebKitWebContext* m_webContext;
};
void webkitWebContextWillCloseAutomationSession(WebKitWebContext* webContext)
{
webContext->priv->processPool->setAutomationSession(nullptr);
webContext->priv->automationSession = nullptr;
}
#endif
#if PLATFORM(GTK)
#define INJECTED_BUNDLE_FILENAME "libwebkit" WEBKITGTK_API_INFIX "gtkinjectedbundle.so"
#elif PLATFORM(WPE)
#define INJECTED_BUNDLE_FILENAME "libWPEInjectedBundle.so"
#endif
static const char* injectedBundleDirectory()
{
#if ENABLE(DEVELOPER_MODE)
const char* bundleDirectory = g_getenv("WEBKIT_INJECTED_BUNDLE_PATH");
if (bundleDirectory && g_file_test(bundleDirectory, G_FILE_TEST_IS_DIR))
return bundleDirectory;
#endif
static const char* injectedBundlePath = PKGLIBDIR G_DIR_SEPARATOR_S "injected-bundle" G_DIR_SEPARATOR_S;
return injectedBundlePath;
}
static void webkitWebContextGetProperty(GObject* object, guint propID, GValue* value, GParamSpec* paramSpec)
{
WebKitWebContext* context = WEBKIT_WEB_CONTEXT(object);
switch (propID) {
#if PLATFORM(GTK) && !USE(GTK4)
case PROP_LOCAL_STORAGE_DIRECTORY:
g_value_set_string(value, context->priv->localStorageDirectory.data());
break;
#endif
#if !ENABLE(2022_GLIB_API)
case PROP_WEBSITE_DATA_MANAGER:
g_value_set_object(value, webkit_web_context_get_website_data_manager(context));
break;
#endif
#if PLATFORM(GTK) && !USE(GTK4)
case PROP_PSON_ENABLED:
g_value_set_boolean(value, context->priv->psonEnabled);
break;
case PROP_USE_SYSTEM_APPEARANCE_FOR_SCROLLBARS:
g_value_set_boolean(value, webkit_web_context_get_use_system_appearance_for_scrollbars(context));
break;
#endif
case PROP_TIME_ZONE_OVERRIDE:
g_value_set_string(value, webkit_web_context_get_time_zone_override(context));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, paramSpec);
}
}
static void webkitWebContextSetProperty(GObject* object, guint propID, const GValue* value, GParamSpec* paramSpec)
{
WebKitWebContext* context = WEBKIT_WEB_CONTEXT(object);
switch (propID) {
#if PLATFORM(GTK) && !USE(GTK4)
case PROP_LOCAL_STORAGE_DIRECTORY:
context->priv->localStorageDirectory = g_value_get_string(value);
break;
#endif
#if !ENABLE(2022_GLIB_API)
case PROP_WEBSITE_DATA_MANAGER: {
gpointer manager = g_value_get_object(value);
context->priv->websiteDataManager = manager ? WEBKIT_WEBSITE_DATA_MANAGER(manager) : nullptr;
break;
}
#endif
#if PLATFORM(GTK) && !USE(GTK4)
case PROP_PSON_ENABLED:
context->priv->psonEnabled = g_value_get_boolean(value);
break;
case PROP_USE_SYSTEM_APPEARANCE_FOR_SCROLLBARS:
webkit_web_context_set_use_system_appearance_for_scrollbars(context, g_value_get_boolean(value));
break;
#endif
case PROP_MEMORY_PRESSURE_SETTINGS: {
gpointer settings = g_value_get_boxed(value);
context->priv->memoryPressureSettings = settings ? webkit_memory_pressure_settings_copy(static_cast<WebKitMemoryPressureSettings*>(settings)) : nullptr;
break;
}
case PROP_TIME_ZONE_OVERRIDE: {
const auto* timeZone = g_value_get_string(value);
if (isTimeZoneValid(StringView::fromLatin1(timeZone)))
context->priv->timeZoneOverride = timeZone;
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, paramSpec);
}
}
static void webkitWebContextConstructed(GObject* object)
{
G_OBJECT_CLASS(webkit_web_context_parent_class)->constructed(object);
GUniquePtr<char> bundleFilename(g_build_filename(injectedBundleDirectory(), INJECTED_BUNDLE_FILENAME, nullptr));
WebKitWebContext* webContext = WEBKIT_WEB_CONTEXT(object);
WebKitWebContextPrivate* priv = webContext->priv;
API::ProcessPoolConfiguration configuration;
configuration.setInjectedBundlePath(FileSystem::stringFromFileSystemRepresentation(bundleFilename.get()));
configuration.setUsesWebProcessCache(true);
#if PLATFORM(GTK) && !USE(GTK4)
configuration.setProcessSwapsOnNavigation(priv->psonEnabled);
configuration.setUseSystemAppearanceForScrollbars(priv->useSystemAppearanceForScrollbars);
#else
configuration.setProcessSwapsOnNavigation(true);
#endif
if (priv->memoryPressureSettings) {
configuration.setMemoryPressureHandlerConfiguration(webkitMemoryPressureSettingsGetMemoryPressureHandlerConfiguration(priv->memoryPressureSettings));
// Once the settings have been passed to the ProcessPoolConfiguration, we don't need them anymore so we can free them.
g_clear_pointer(&priv->memoryPressureSettings, webkit_memory_pressure_settings_free);
}
configuration.setTimeZoneOverride(String::fromUTF8(priv->timeZoneOverride.data(), priv->timeZoneOverride.length()));
#if !ENABLE(2022_GLIB_API)
if (!priv->websiteDataManager)
priv->websiteDataManager = adoptGRef(webkit_website_data_manager_new("local-storage-directory", priv->localStorageDirectory.data(), nullptr));
#endif
priv->processPool = WebProcessPool::create(configuration);
priv->processPool->setUserMessageHandler([webContext](UserMessage&& message, CompletionHandler<void(UserMessage&&)>&& completionHandler) {
// Sink the floating ref.
GRefPtr<WebKitUserMessage> userMessage = webkitUserMessageCreate(WTFMove(message), WTFMove(completionHandler));
gboolean returnValue;
g_signal_emit(webContext, signals[USER_MESSAGE_RECEIVED], 0, userMessage.get(), &returnValue);
});
#if ENABLE(2022_GLIB_API)
priv->processPool->setSandboxEnabled(true);
#endif
priv->processPool->addSandboxPath(injectedBundleDirectory(), SandboxPermission::ReadOnly);
#if ENABLE(MEMORY_SAMPLER)
if (getenv("WEBKIT_SAMPLE_MEMORY"))
priv->processPool->startMemorySampler(0);
#endif
attachInjectedBundleClientToContext(webContext);
priv->geolocationManager = adoptGRef(webkitGeolocationManagerCreate(priv->processPool->supplement<WebGeolocationManagerProxy>()));
priv->notificationProvider = makeUnique<WebKitNotificationProvider>(priv->processPool->supplement<WebNotificationManagerProxy>(), webContext);
#if PLATFORM(GTK) && ENABLE(REMOTE_INSPECTOR)
priv->remoteInspectorProtocolHandler = makeUnique<RemoteInspectorProtocolHandler>(webContext);
#endif
priv->webkitProtocolHandler = makeUnique<WebKitProtocolHandler>(webContext);
}
static void webkitWebContextDispose(GObject* object)
{
WebKitWebContextPrivate* priv = WEBKIT_WEB_CONTEXT(object)->priv;
if (!priv->clientsDetached) {
priv->clientsDetached = true;
priv->processPool->setInjectedBundleClient(nullptr);
}
#if !ENABLE(2022_GLIB_API)
if (priv->faviconDatabase) {
webkitFaviconDatabaseClose(priv->faviconDatabase.get());
priv->faviconDatabase = nullptr;
}
#endif
if (priv->processPool) {
priv->processPool->setUserMessageHandler(nullptr);
priv->processPool = nullptr;
}
G_OBJECT_CLASS(webkit_web_context_parent_class)->dispose(object);
}
static void webkit_web_context_class_init(WebKitWebContextClass* webContextClass)
{
webkitInitialize();
GObjectClass* gObjectClass = G_OBJECT_CLASS(webContextClass);
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
s_serviceWorkerNotificationProvider = makeUnique<WebKitNotificationProvider>(&WebNotificationManagerProxy::sharedServiceWorkerManager(), nullptr);
gObjectClass->get_property = webkitWebContextGetProperty;
gObjectClass->set_property = webkitWebContextSetProperty;
gObjectClass->constructed = webkitWebContextConstructed;
gObjectClass->dispose = webkitWebContextDispose;
#if PLATFORM(GTK) && !USE(GTK4)
/**
* WebKitWebContext:local-storage-directory:
*
* The directory where local storage data will be saved.
*
* Since: 2.8
*
* Deprecated: 2.10. Use #WebKitWebsiteDataManager:local-storage-directory instead.
*/
sObjProperties[PROP_LOCAL_STORAGE_DIRECTORY] =
g_param_spec_string(
"local-storage-directory",
nullptr, nullptr,
nullptr,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
#endif
#if !ENABLE(2022_GLIB_API)
/**
* WebKitWebContext:website-data-manager:
*
* The #WebKitWebsiteDataManager associated with this context.
*
* Since: 2.10
*/
sObjProperties[PROP_WEBSITE_DATA_MANAGER] =
g_param_spec_object(
"website-data-manager",
nullptr, nullptr,
WEBKIT_TYPE_WEBSITE_DATA_MANAGER,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
#endif
#if PLATFORM(GTK) && !USE(GTK4)
/**
* WebKitWebContext:process-swap-on-cross-site-navigation-enabled:
*
* Whether swap Web processes on cross-site navigations is enabled.
*
* When enabled, pages from each security origin will be handled by
* their own separate Web processes, which are started (and
* terminated) on demand as the user navigates across different
* domains. This is an important security measure which helps prevent
* websites stealing data from other visited pages.
*
* Since: 2.28
*/
sObjProperties[PROP_PSON_ENABLED] =
g_param_spec_boolean(
"process-swap-on-cross-site-navigation-enabled",
nullptr, nullptr,
FALSE,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/**
* WebKitWebContext:use-system-appearance-for-scrollbars:
*
* Whether to use system appearance for rendering scrollbars.
*
* This is enabled by default for backwards compatibility, but it's only
* recommened to use when the application includes other widgets to ensure
* consistency, or when consistency with other applications is required too.
*
* Since: 2.30
*/
sObjProperties[PROP_USE_SYSTEM_APPEARANCE_FOR_SCROLLBARS] =
g_param_spec_boolean(
"use-system-appearance-for-scrollbars",
nullptr, nullptr,
TRUE,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT));
#endif
/**
* WebKitWebContext:memory-pressure-settings:
*
* The #WebKitMemoryPressureSettings applied to the web processes created by this context.
*
* Since: 2.34
*/
sObjProperties[PROP_MEMORY_PRESSURE_SETTINGS] =
g_param_spec_boxed(
"memory-pressure-settings",
nullptr, nullptr,
WEBKIT_TYPE_MEMORY_PRESSURE_SETTINGS,
static_cast<GParamFlags>(WEBKIT_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
/**
* WebKitWebContext:time-zone-override:
*
* The timezone override for this web context. Setting this property provides a better
* alternative to configure the timezone information for all webviews managed by the WebContext.
* The other, less optimal, approach is to globally set the TZ environment variable in the
* process before creating the context. However this approach might not be very convenient and
* can have side-effects in your application.
*
* The expected values for this property are defined in the IANA timezone database. See this
* wikipedia page for instance, https://en.wikipedia.org/wiki/List_of_tz_database_time_zones.
*
* Since: 2.38
*/
sObjProperties[PROP_TIME_ZONE_OVERRIDE] =
g_param_spec_string(
"time-zone-override",
nullptr, nullptr,
nullptr,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_properties(gObjectClass, N_PROPERTIES, sObjProperties);
#if !ENABLE(2022_GLIB_API)
/**
* WebKitWebContext::download-started:
* @context: the #WebKitWebContext
* @download: the #WebKitDownload associated with this event
*
* This signal is emitted when a new download request is made.
*/
signals[DOWNLOAD_STARTED] =
g_signal_new("download-started",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(WebKitWebContextClass, download_started),
nullptr, nullptr,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
WEBKIT_TYPE_DOWNLOAD);
#endif
signals[INITIALIZE_WEB_PROCESS_EXTENSIONS] =
#if ENABLE(2022_GLIB_API)
g_signal_new("initialize-web-process-extensions",
#else
g_signal_new("initialize-web-extensions",
#endif
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
#if ENABLE(2022_GLIB_API)
0,
#else
G_STRUCT_OFFSET(WebKitWebContextClass, initialize_web_extensions),
#endif
nullptr, nullptr,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* WebKitWebContext::initialize-notification-permissions:
* @context: the #WebKitWebContext
*
* This signal is emitted when a #WebKitWebContext needs to set
* initial notification permissions for a web process. It is emitted
* when a new web process is about to be launched, and signals the
* most appropriate moment to use
* webkit_web_context_initialize_notification_permissions(). If no
* notification permissions have changed since the last time this
* signal was emitted, then there is no need to call
* webkit_web_context_initialize_notification_permissions() again.
*
* Since: 2.16
*/
signals[INITIALIZE_NOTIFICATION_PERMISSIONS] =
g_signal_new("initialize-notification-permissions",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
#if ENABLE(2022_GLIB_API)
0,
#else
G_STRUCT_OFFSET(WebKitWebContextClass, initialize_notification_permissions),
#endif
nullptr, nullptr,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* WebKitWebContext::automation-started:
* @context: the #WebKitWebContext
* @session: the #WebKitAutomationSession associated with this event
*
* This signal is emitted when a new automation request is made.
* Note that it will never be emitted if automation is not enabled in @context,
* see webkit_web_context_set_automation_allowed() for more details.
*
* Since: 2.18
*/
signals[AUTOMATION_STARTED] =
g_signal_new("automation-started",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
#if ENABLE(2022_GLIB_API)
0,
#else
G_STRUCT_OFFSET(WebKitWebContextClass, automation_started),
#endif
nullptr, nullptr,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
WEBKIT_TYPE_AUTOMATION_SESSION);
/**
* WebKitWebContext::user-message-received:
* @context: the #WebKitWebContext
* @message: the #WebKitUserMessage received
*
* This signal is emitted when a #WebKitUserMessage is received from a
* web process extension. You can reply to the message using
* webkit_user_message_send_reply().
*
* You can handle the user message asynchronously by calling g_object_ref() on
* @message and returning %TRUE.
*
* Returns: %TRUE if the message was handled, or %FALSE otherwise.
*
* Since: 2.28
*/
signals[USER_MESSAGE_RECEIVED] = g_signal_new(
"user-message-received",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
#if ENABLE(2022_GLIB_API)
0,
#else
G_STRUCT_OFFSET(WebKitWebContextClass, user_message_received),
#endif
g_signal_accumulator_true_handled, nullptr,
g_cclosure_marshal_generic,
G_TYPE_BOOLEAN, 1,
WEBKIT_TYPE_USER_MESSAGE);
}
static gpointer createDefaultWebContext(gpointer)
{
static GRefPtr<WebKitWebContext> webContext = adoptGRef(WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, nullptr)));
return webContext.get();
}
/**
* webkit_web_context_get_default:
*
* Gets the default web context.
*
* Returns: (transfer none): a #WebKitWebContext
*/
WebKitWebContext* webkit_web_context_get_default(void)
{
static GOnce onceInit = G_ONCE_INIT;
return WEBKIT_WEB_CONTEXT(g_once(&onceInit, createDefaultWebContext, 0));
}
/**
* webkit_web_context_new:
*
* Create a new #WebKitWebContext.
*
* Returns: (transfer full): a newly created #WebKitWebContext
*
* Since: 2.8
*/
WebKitWebContext* webkit_web_context_new(void)
{
return WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, nullptr));
}
#if !ENABLE(2022_GLIB_API)
/**
* webkit_web_context_new_ephemeral:
*
* Create a new ephemeral #WebKitWebContext.
*
* An ephemeral #WebKitWebContext is a context
* created with an ephemeral #WebKitWebsiteDataManager. This is just a convenient method
* to create ephemeral contexts without having to create your own #WebKitWebsiteDataManager.
* All #WebKitWebView<!-- -->s associated with this context will also be ephemeral. Websites will
* not store any data in the client storage.
* This is normally used to implement private instances.
*
* Returns: (transfer full): a new ephemeral #WebKitWebContext.
*
* Since: 2.16
*/
WebKitWebContext* webkit_web_context_new_ephemeral()
{
GRefPtr<WebKitWebsiteDataManager> manager = adoptGRef(webkit_website_data_manager_new_ephemeral());
return WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, "website-data-manager", manager.get(), nullptr));
}
/**
* webkit_web_context_new_with_website_data_manager:
* @manager: a #WebKitWebsiteDataManager
*
* Create a new #WebKitWebContext with a #WebKitWebsiteDataManager.
*
* Returns: (transfer full): a newly created #WebKitWebContext
*
* Since: 2.10
*/
WebKitWebContext* webkit_web_context_new_with_website_data_manager(WebKitWebsiteDataManager* manager)
{
g_return_val_if_fail(WEBKIT_IS_WEBSITE_DATA_MANAGER(manager), nullptr);
return WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, "website-data-manager", manager, nullptr));
}
/**
* webkit_web_context_get_website_data_manager:
* @context: the #WebKitWebContext
*
* Get the #WebKitWebsiteDataManager of @context.
*
* Returns: (transfer none): a #WebKitWebsiteDataManager
*
* Since: 2.10
*/
WebKitWebsiteDataManager* webkit_web_context_get_website_data_manager(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
return context->priv->websiteDataManager.get();
}
/**
* webkit_web_context_is_ephemeral:
* @context: the #WebKitWebContext
*
* Get whether a #WebKitWebContext is ephemeral.
*
* Returns: %TRUE if @context is ephemeral or %FALSE otherwise.
*
* Since: 2.16
*/
gboolean webkit_web_context_is_ephemeral(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), FALSE);
return webkit_website_data_manager_is_ephemeral(context->priv->websiteDataManager.get());
}
#endif
/**
* webkit_web_context_is_automation_allowed:
* @context: the #WebKitWebContext
*
* Get whether automation is allowed in @context.
*
* See also webkit_web_context_set_automation_allowed().
*
* Returns: %TRUE if automation is allowed or %FALSE otherwise.
*
* Since: 2.18
*/
gboolean webkit_web_context_is_automation_allowed(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), FALSE);
#if ENABLE(REMOTE_INSPECTOR)
return !!context->priv->automationClient;
#else
return FALSE;
#endif
}
/**
* webkit_web_context_set_automation_allowed:
* @context: the #WebKitWebContext
* @allowed: value to set
*
* Set whether automation is allowed in @context.
*
* When automation is enabled the browser could
* be controlled by another process by requesting an automation session. When a new automation
* session is requested the signal #WebKitWebContext::automation-started is emitted.
* Automation is disabled by default, so you need to explicitly call this method passing %TRUE
* to enable it.
*
* Note that only one #WebKitWebContext can have automation enabled, so this will do nothing
* if there's another #WebKitWebContext with automation already enabled.
*
* Since: 2.18
*/
void webkit_web_context_set_automation_allowed(WebKitWebContext* context, gboolean allowed)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
if (webkit_web_context_is_automation_allowed(context) == allowed)
return;
#if ENABLE(REMOTE_INSPECTOR)
if (allowed) {
if (Inspector::RemoteInspector::singleton().client()) {
g_warning("Not enabling automation on WebKitWebContext because there's another context with automation enabled, only one is allowed");
return;
}
context->priv->automationClient = makeUnique<WebKitAutomationClient>(context);
} else
context->priv->automationClient = nullptr;
#endif
}
#if ENABLE(2022_GLIB_API)
/**
* webkit_web_context_get_network_session_for_automation:
* @context: the #WebKitWebContext
*
* Get the #WebKitNetworkSession used for automation sessions started in @context.
*
* Returns: (transfer none) (nullable): a #WebKitNetworkSession, or %NULL if automation is not enabled
*
* Since: 2.40
*/
WebKitNetworkSession* webkit_web_context_get_network_session_for_automation(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
#if ENABLE(REMOTE_INSPECTOR)
if (!context->priv->automationNetworkSession && context->priv->automationClient)
context->priv->automationNetworkSession = adoptGRef(webkit_network_session_new_ephemeral());
return context->priv->automationNetworkSession.get();
#else
return nullptr;
#endif
}
#endif
/**
* webkit_web_context_set_cache_model:
* @context: the #WebKitWebContext
* @cache_model: a #WebKitCacheModel
*
* Specifies a usage model for WebViews.
*
* Specifies a usage model for WebViews, which WebKit will use to
* determine its caching behavior. All web views follow the cache
* model. This cache model determines the RAM and disk space to use
* for caching previously viewed content .
*
* Research indicates that users tend to browse within clusters of
* documents that hold resources in common, and to revisit previously
* visited documents. WebKit and the frameworks below it include
* built-in caches that take advantage of these patterns,
* substantially improving document load speed in browsing
* situations. The WebKit cache model controls the behaviors of all of
* these caches, including various WebCore caches.
*
* Browsers can improve document load speed substantially by
* specifying %WEBKIT_CACHE_MODEL_WEB_BROWSER. Applications without a
* browsing interface can reduce memory usage substantially by
* specifying %WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER. The default value is
* %WEBKIT_CACHE_MODEL_WEB_BROWSER.
*/
void webkit_web_context_set_cache_model(WebKitWebContext*, WebKitCacheModel model)
{
CacheModel cacheModel;
switch (model) {
case WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER:
cacheModel = CacheModel::DocumentViewer;
break;
case WEBKIT_CACHE_MODEL_WEB_BROWSER:
cacheModel = CacheModel::PrimaryWebBrowser;
break;
case WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER:
cacheModel = CacheModel::DocumentBrowser;
break;
default:
g_assert_not_reached();
}
if (cacheModel != LegacyGlobalSettings::singleton().cacheModel())
LegacyGlobalSettings::singleton().setCacheModel(cacheModel);
}
/**
* webkit_web_context_get_cache_model:
* @context: the #WebKitWebContext
*
* Returns the current cache model.
*
* For more information about this
* value check the documentation of the function
* webkit_web_context_set_cache_model().
*
* Returns: the current #WebKitCacheModel
*/
WebKitCacheModel webkit_web_context_get_cache_model(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), WEBKIT_CACHE_MODEL_WEB_BROWSER);
switch (LegacyGlobalSettings::singleton().cacheModel()) {