webkit_context.go RawHistoryFolder
1// Package port defines interfaces for external dependencies.
2package port
3
4import "context"
5
6// WebKitCookiePolicy controls cookie acceptance behavior for NetworkSession.
7type WebKitCookiePolicy string
8
9const (
10 WebKitCookiePolicyAlways WebKitCookiePolicy = "always"
11 WebKitCookiePolicyNoThirdParty WebKitCookiePolicy = "no_third_party"
12 WebKitCookiePolicyNever WebKitCookiePolicy = "never"
13)
14
15// MemoryPressureConfig holds memory pressure settings for a WebKit process.
16// Zero values mean "use WebKit defaults".
17type MemoryPressureConfig struct {
18 // MemoryLimitMB sets the memory limit in megabytes.
19 // 0 means unset (uses WebKit default: system RAM capped at 3GB).
20 MemoryLimitMB int
21
22 // PollIntervalSec sets the interval for memory usage checks.
23 // 0 means unset (uses WebKit default: 30 seconds).
24 PollIntervalSec float64
25
26 // ConservativeThreshold sets threshold for conservative memory release.
27 // Must be in (0, 1). 0 means unset (uses WebKit default: 0.33).
28 ConservativeThreshold float64
29
30 // StrictThreshold sets threshold for strict memory release.
31 // Must be in (0, 1). 0 means unset (uses WebKit default: 0.5).
32 StrictThreshold float64
33}
34
35// IsConfigured returns true if any memory pressure setting is configured.
36func (c *MemoryPressureConfig) IsConfigured() bool {
37 if c == nil {
38 return false
39 }
40 return c.MemoryLimitMB > 0 ||
41 c.PollIntervalSec > 0 ||
42 c.ConservativeThreshold > 0 ||
43 c.StrictThreshold > 0
44}
45
46// WebKitContextOptions configures WebKitContext creation.
47type WebKitContextOptions struct {
48 // DataDir is the directory for persistent data (cookies, storage, etc.).
49 DataDir string
50
51 // CacheDir is the directory for cache data.
52 CacheDir string
53
54 // CookiePolicy controls cookie acceptance behavior.
55 // Empty value means runtime defaults are used.
56 CookiePolicy WebKitCookiePolicy
57
58 // ITPEnabled controls Intelligent Tracking Prevention for NetworkSession.
59 // False means ITP remains disabled unless enabled explicitly.
60 ITPEnabled bool
61
62 // WebProcessMemory configures memory pressure for web processes.
63 // nil means use WebKit defaults.
64 WebProcessMemory *MemoryPressureConfig
65
66 // NetworkProcessMemory configures memory pressure for the network process.
67 // nil means use WebKit defaults.
68 NetworkProcessMemory *MemoryPressureConfig
69}
70
71// IsWebProcessMemoryConfigured returns true if web process memory settings are configured.
72func (o *WebKitContextOptions) IsWebProcessMemoryConfigured() bool {
73 if o == nil {
74 return false
75 }
76 return o.WebProcessMemory.IsConfigured()
77}
78
79// IsNetworkProcessMemoryConfigured returns true if network process memory settings are configured.
80func (o *WebKitContextOptions) IsNetworkProcessMemoryConfigured() bool {
81 if o == nil {
82 return false
83 }
84 return o.NetworkProcessMemory.IsConfigured()
85}
86
87// MemoryPressureApplier applies memory pressure settings to WebKit processes.
88// This interface abstracts the WebKit-specific memory pressure configuration
89// to allow testing without WebKit dependencies.
90type MemoryPressureApplier interface {
91 // ApplyNetworkProcessSettings applies memory pressure settings to the network process.
92 // Must be called BEFORE creating any NetworkSession.
93 ApplyNetworkProcessSettings(ctx context.Context, cfg *MemoryPressureConfig) error
94
95 // ApplyWebProcessSettings applies memory pressure settings to web processes.
96 // Returns an opaque settings object that should be passed to WebContext creation.
97 // Returns nil if no settings are configured.
98 ApplyWebProcessSettings(ctx context.Context, cfg *MemoryPressureConfig) (any, error)
99}
100
101// WebKitContextProvider creates and manages WebKit contexts.
102// This interface abstracts WebKit context creation for testability.
103type WebKitContextProvider interface {
104 // Initialize sets up the WebKit context with the given options.
105 // Must be called before any other methods.
106 Initialize(ctx context.Context, opts WebKitContextOptions) error
107
108 // DataDir returns the data directory path.
109 DataDir() string
110
111 // CacheDir returns the cache directory path.
112 CacheDir() string
113
114 // PrefetchDNS prefetches DNS for the given hostname.
115 PrefetchDNS(hostname string)
116
117 // Close releases resources held by the context.
118 Close() error
119}