refactor(webkit-settings): pass context as first parameter instead of storing
Remove stored ctx field from SettingsManager and update all methods (CreateSettings, applySettings, UpdateFromConfig, ApplyToWebView) to accept context as first parameter, following project convention of using log := logging.FromContext(ctx) in each method.
- commit
- 401b02cd15abebcdc79a9e0e2bf6a0cf891efa85
- author
- brice · · 2025-12-17 06:44 UTC
- parent
- b106e60
- signature
- unsigned
1 file · +12 -12
M internal/infrastructure/webkit/settings.go +12 -12
| @@ -12,21 +12,21 @@ import ( | |||
| 12 | 12 | // SettingsManager creates and manages WebKit Settings instances from config. | |
| 13 | 13 | type SettingsManager struct { | |
| 14 | 14 | cfg *config.Config | |
| 15 | - | ctx context.Context | |
| 16 | 15 | mu sync.RWMutex | |
| 17 | 16 | } | |
| 18 | 17 | ||
| 19 | 18 | // NewSettingsManager creates a new SettingsManager with the given config. | |
| 20 | 19 | func NewSettingsManager(ctx context.Context, cfg *config.Config) *SettingsManager { | |
| 20 | + | log := logging.FromContext(ctx) | |
| 21 | + | log.Debug().Msg("creating settings manager") | |
| 21 | 22 | return &SettingsManager{ | |
| 22 | 23 | cfg: cfg, | |
| 23 | - | ctx: logging.WithComponent(ctx, "webkit-settings"), | |
| 24 | 24 | } | |
| 25 | 25 | } | |
| 26 | 26 | ||
| 27 | 27 | // CreateSettings creates a new webkit.Settings instance configured from the current config. | |
| 28 | - | func (sm *SettingsManager) CreateSettings() *webkit.Settings { | |
| 29 | - | log := logging.FromContext(sm.ctx) | |
| 28 | + | func (sm *SettingsManager) CreateSettings(ctx context.Context) *webkit.Settings { | |
| 29 | + | log := logging.FromContext(ctx) | |
| 30 | 30 | ||
| 31 | 31 | sm.mu.RLock() | |
| 32 | 32 | cfg := sm.cfg | |
| @@ -38,12 +38,13 @@ func (sm *SettingsManager) CreateSettings() *webkit.Settings { | |||
| 38 | 38 | return nil | |
| 39 | 39 | } | |
| 40 | 40 | ||
| 41 | - | sm.applySettings(settings, cfg) | |
| 41 | + | sm.applySettings(ctx, settings, cfg) | |
| 42 | 42 | return settings | |
| 43 | 43 | } | |
| 44 | 44 | ||
| 45 | 45 | // applySettings applies configuration to a webkit.Settings instance. | |
| 46 | - | func (sm *SettingsManager) applySettings(settings *webkit.Settings, cfg *config.Config) { | |
| 46 | + | func (sm *SettingsManager) applySettings(ctx context.Context, settings *webkit.Settings, cfg *config.Config) { | |
| 47 | + | log := logging.FromContext(ctx) | |
| 47 | 48 | // JavaScript settings | |
| 48 | 49 | settings.SetEnableJavascript(true) | |
| 49 | 50 | settings.SetEnableJavascriptMarkup(true) | |
| @@ -108,7 +109,6 @@ func (sm *SettingsManager) applySettings(settings *webkit.Settings, cfg *config. | |||
| 108 | 109 | // WebRTC | |
| 109 | 110 | settings.SetEnableWebrtc(true) | |
| 110 | 111 | ||
| 111 | - | log := logging.FromContext(sm.ctx) | |
| 112 | 112 | log.Debug(). | |
| 113 | 113 | Str("sans_font", cfg.Appearance.SansFont). | |
| 114 | 114 | Str("rendering_mode", string(cfg.RenderingMode)). | |
| @@ -119,8 +119,8 @@ func (sm *SettingsManager) applySettings(settings *webkit.Settings, cfg *config. | |||
| 119 | 119 | // UpdateFromConfig updates the manager with a new config (for hot-reload). | |
| 120 | 120 | // Note: This doesn't update already-created Settings instances. | |
| 121 | 121 | // New WebViews will use the updated config. | |
| 122 | - | func (sm *SettingsManager) UpdateFromConfig(cfg *config.Config) { | |
| 123 | - | log := logging.FromContext(sm.ctx) | |
| 122 | + | func (sm *SettingsManager) UpdateFromConfig(ctx context.Context, cfg *config.Config) { | |
| 123 | + | log := logging.FromContext(ctx) | |
| 124 | 124 | sm.mu.Lock() | |
| 125 | 125 | defer sm.mu.Unlock() | |
| 126 | 126 | sm.cfg = cfg | |
| @@ -129,12 +129,12 @@ func (sm *SettingsManager) UpdateFromConfig(cfg *config.Config) { | |||
| 129 | 129 | ||
| 130 | 130 | // ApplyToWebView applies current settings to an existing WebView. | |
| 131 | 131 | // This can be used to update a WebView's settings after config hot-reload. | |
| 132 | - | func (sm *SettingsManager) ApplyToWebView(wv *webkit.WebView) { | |
| 132 | + | func (sm *SettingsManager) ApplyToWebView(ctx context.Context, wv *webkit.WebView) { | |
| 133 | 133 | if wv == nil { | |
| 134 | 134 | return | |
| 135 | 135 | } | |
| 136 | 136 | ||
| 137 | - | settings := sm.CreateSettings() | |
| 137 | + | settings := sm.CreateSettings(ctx) | |
| 138 | 138 | if settings == nil { | |
| 139 | 139 | return | |
| 140 | 140 | } | |
| @@ -147,6 +147,6 @@ func (sm *SettingsManager) ApplyToWebView(wv *webkit.WebView) { | |||
| 147 | 147 | sm.mu.RLock() | |
| 148 | 148 | cfg := sm.cfg | |
| 149 | 149 | sm.mu.RUnlock() | |
| 150 | - | sm.applySettings(existingSettings, cfg) | |
| 150 | + | sm.applySettings(ctx, existingSettings, cfg) | |
| 151 | 151 | } | |
| 152 | 152 | } | |
| @@ -12,21 +12,21 @@ import ( | |||||
| 12 | // SettingsManager creates and manages WebKit Settings instances from config. | 12 | // SettingsManager creates and manages WebKit Settings instances from config. | ||
| 13 | type SettingsManager struct { | 13 | type SettingsManager struct { | ||
| 14 | cfg *config.Config | 14 | cfg *config.Config | ||
| 15 | - | ctx context.Context | |||
| 16 | mu sync.RWMutex | 15 | mu sync.RWMutex | ||
| 17 | } | 16 | } | ||
| 18 | 17 | ||||
| 19 | // NewSettingsManager creates a new SettingsManager with the given config. | 18 | // NewSettingsManager creates a new SettingsManager with the given config. | ||
| 20 | func NewSettingsManager(ctx context.Context, cfg *config.Config) *SettingsManager { | 19 | func NewSettingsManager(ctx context.Context, cfg *config.Config) *SettingsManager { | ||
| 20 | + | log := logging.FromContext(ctx) | |||
| 21 | + | log.Debug().Msg("creating settings manager") | |||
| 21 | return &SettingsManager{ | 22 | return &SettingsManager{ | ||
| 22 | cfg: cfg, | 23 | cfg: cfg, | ||
| 23 | - | ctx: logging.WithComponent(ctx, "webkit-settings"), | |||
| 24 | } | 24 | } | ||
| 25 | } | 25 | } | ||
| 26 | 26 | ||||
| 27 | // CreateSettings creates a new webkit.Settings instance configured from the current config. | 27 | // CreateSettings creates a new webkit.Settings instance configured from the current config. | ||
| 28 | - | func (sm *SettingsManager) CreateSettings() *webkit.Settings { | 28 | + | func (sm *SettingsManager) CreateSettings(ctx context.Context) *webkit.Settings { |
| 29 | - | log := logging.FromContext(sm.ctx) | 29 | + | log := logging.FromContext(ctx) |
| 30 | 30 | ||||
| 31 | sm.mu.RLock() | 31 | sm.mu.RLock() | ||
| 32 | cfg := sm.cfg | 32 | cfg := sm.cfg | ||
| @@ -38,12 +38,13 @@ func (sm *SettingsManager) CreateSettings() *webkit.Settings { | |||||
| 38 | return nil | 38 | return nil | ||
| 39 | } | 39 | } | ||
| 40 | 40 | ||||
| 41 | - | sm.applySettings(settings, cfg) | 41 | + | sm.applySettings(ctx, settings, cfg) |
| 42 | return settings | 42 | return settings | ||
| 43 | } | 43 | } | ||
| 44 | 44 | ||||
| 45 | // applySettings applies configuration to a webkit.Settings instance. | 45 | // applySettings applies configuration to a webkit.Settings instance. | ||
| 46 | - | func (sm *SettingsManager) applySettings(settings *webkit.Settings, cfg *config.Config) { | 46 | + | func (sm *SettingsManager) applySettings(ctx context.Context, settings *webkit.Settings, cfg *config.Config) { |
| 47 | + | log := logging.FromContext(ctx) | |||
| 47 | // JavaScript settings | 48 | // JavaScript settings | ||
| 48 | settings.SetEnableJavascript(true) | 49 | settings.SetEnableJavascript(true) | ||
| 49 | settings.SetEnableJavascriptMarkup(true) | 50 | settings.SetEnableJavascriptMarkup(true) | ||
| @@ -108,7 +109,6 @@ func (sm *SettingsManager) applySettings(settings *webkit.Settings, cfg *config. | |||||
| 108 | // WebRTC | 109 | // WebRTC | ||
| 109 | settings.SetEnableWebrtc(true) | 110 | settings.SetEnableWebrtc(true) | ||
| 110 | 111 | ||||
| 111 | - | log := logging.FromContext(sm.ctx) | |||
| 112 | log.Debug(). | 112 | log.Debug(). | ||
| 113 | Str("sans_font", cfg.Appearance.SansFont). | 113 | Str("sans_font", cfg.Appearance.SansFont). | ||
| 114 | Str("rendering_mode", string(cfg.RenderingMode)). | 114 | Str("rendering_mode", string(cfg.RenderingMode)). | ||
| @@ -119,8 +119,8 @@ func (sm *SettingsManager) applySettings(settings *webkit.Settings, cfg *config. | |||||
| 119 | // UpdateFromConfig updates the manager with a new config (for hot-reload). | 119 | // UpdateFromConfig updates the manager with a new config (for hot-reload). | ||
| 120 | // Note: This doesn't update already-created Settings instances. | 120 | // Note: This doesn't update already-created Settings instances. | ||
| 121 | // New WebViews will use the updated config. | 121 | // New WebViews will use the updated config. | ||
| 122 | - | func (sm *SettingsManager) UpdateFromConfig(cfg *config.Config) { | 122 | + | func (sm *SettingsManager) UpdateFromConfig(ctx context.Context, cfg *config.Config) { |
| 123 | - | log := logging.FromContext(sm.ctx) | 123 | + | log := logging.FromContext(ctx) |
| 124 | sm.mu.Lock() | 124 | sm.mu.Lock() | ||
| 125 | defer sm.mu.Unlock() | 125 | defer sm.mu.Unlock() | ||
| 126 | sm.cfg = cfg | 126 | sm.cfg = cfg | ||
| @@ -129,12 +129,12 @@ func (sm *SettingsManager) UpdateFromConfig(cfg *config.Config) { | |||||
| 129 | 129 | ||||
| 130 | // ApplyToWebView applies current settings to an existing WebView. | 130 | // ApplyToWebView applies current settings to an existing WebView. | ||
| 131 | // This can be used to update a WebView's settings after config hot-reload. | 131 | // This can be used to update a WebView's settings after config hot-reload. | ||
| 132 | - | func (sm *SettingsManager) ApplyToWebView(wv *webkit.WebView) { | 132 | + | func (sm *SettingsManager) ApplyToWebView(ctx context.Context, wv *webkit.WebView) { |
| 133 | if wv == nil { | 133 | if wv == nil { | ||
| 134 | return | 134 | return | ||
| 135 | } | 135 | } | ||
| 136 | 136 | ||||
| 137 | - | settings := sm.CreateSettings() | 137 | + | settings := sm.CreateSettings(ctx) |
| 138 | if settings == nil { | 138 | if settings == nil { | ||
| 139 | return | 139 | return | ||
| 140 | } | 140 | } | ||
| @@ -147,6 +147,6 @@ func (sm *SettingsManager) ApplyToWebView(wv *webkit.WebView) { | |||||
| 147 | sm.mu.RLock() | 147 | sm.mu.RLock() | ||
| 148 | cfg := sm.cfg | 148 | cfg := sm.cfg | ||
| 149 | sm.mu.RUnlock() | 149 | sm.mu.RUnlock() | ||
| 150 | - | sm.applySettings(existingSettings, cfg) | 150 | + | sm.applySettings(ctx, existingSettings, cfg) |
| 151 | } | 151 | } | ||
| 152 | } | 152 | } | ||