refactor: centralize permission URL log constant and clean up minor issues
- Move maxPermissionLogURLLen to logging.PermissionLogURLMaxLen (was duplicated in 3 files) - Replace stringly-typed "website_data_access" with entity constant in webview.go - Cache parsePermFlags result in permission dialog instead of computing twice - Remove redundant hadGhost guard in omnibox clearGhostText
- commit
- 5efc1b980e016b5fd2af07d30603ef50de900029
- author
- bnema · · 2026-03-27 22:07 UTC
- parent
- 3cb3770
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
5 files · +16 -18
M internal/application/usecase/handle_permission.go +2 -4
| @@ -58,8 +58,6 @@ func (uc *HandlePermissionUseCase) logger(ctx context.Context) *zerolog.Logger { | |||
| 58 | 58 | return log | |
| 59 | 59 | } | |
| 60 | 60 | ||
| 61 | - | const maxPermissionLogURLLen = 96 | |
| 62 | - | ||
| 63 | 61 | func isWebsiteDataAccessPermission(permTypes []entity.PermissionType) bool { | |
| 64 | 62 | for _, permType := range permTypes { | |
| 65 | 63 | if permType == entity.PermissionTypeWebsiteDataAccess { | |
| @@ -76,10 +74,10 @@ func permissionLoggerWithMetadata(log *zerolog.Logger, metadata entity.Permissio | |||
| 76 | 74 | } | |
| 77 | 75 | ctx := log.With() | |
| 78 | 76 | if requesting := metadata[entity.PermissionMetadataKeyRequestingDomain]; requesting != "" { | |
| 79 | - | ctx = ctx.Str("requesting_domain", logging.TruncateURL(requesting, maxPermissionLogURLLen)) | |
| 77 | + | ctx = ctx.Str("requesting_domain", logging.TruncateURL(requesting, logging.PermissionLogURLMaxLen)) | |
| 80 | 78 | } | |
| 81 | 79 | if current := metadata[entity.PermissionMetadataKeyCurrentDomain]; current != "" { | |
| 82 | - | ctx = ctx.Str("current_domain", logging.TruncateURL(current, maxPermissionLogURLLen)) | |
| 80 | + | ctx = ctx.Str("current_domain", logging.TruncateURL(current, logging.PermissionLogURLMaxLen)) | |
| 83 | 81 | } | |
| 84 | 82 | enriched := ctx.Logger() | |
| 85 | 83 | return &enriched | |
| @@ -58,8 +58,6 @@ func (uc *HandlePermissionUseCase) logger(ctx context.Context) *zerolog.Logger { | |||||
| 58 | return log | 58 | return log | ||
| 59 | } | 59 | } | ||
| 60 | 60 | ||||
| 61 | - | const maxPermissionLogURLLen = 96 | |||
| 62 | - | ||||
| 63 | func isWebsiteDataAccessPermission(permTypes []entity.PermissionType) bool { | 61 | func isWebsiteDataAccessPermission(permTypes []entity.PermissionType) bool { | ||
| 64 | for _, permType := range permTypes { | 62 | for _, permType := range permTypes { | ||
| 65 | if permType == entity.PermissionTypeWebsiteDataAccess { | 63 | if permType == entity.PermissionTypeWebsiteDataAccess { | ||
| @@ -76,10 +74,10 @@ func permissionLoggerWithMetadata(log *zerolog.Logger, metadata entity.Permissio | |||||
| 76 | } | 74 | } | ||
| 77 | ctx := log.With() | 75 | ctx := log.With() | ||
| 78 | if requesting := metadata[entity.PermissionMetadataKeyRequestingDomain]; requesting != "" { | 76 | if requesting := metadata[entity.PermissionMetadataKeyRequestingDomain]; requesting != "" { | ||
| 79 | - | ctx = ctx.Str("requesting_domain", logging.TruncateURL(requesting, maxPermissionLogURLLen)) | 77 | + | ctx = ctx.Str("requesting_domain", logging.TruncateURL(requesting, logging.PermissionLogURLMaxLen)) |
| 80 | } | 78 | } | ||
| 81 | if current := metadata[entity.PermissionMetadataKeyCurrentDomain]; current != "" { | 79 | if current := metadata[entity.PermissionMetadataKeyCurrentDomain]; current != "" { | ||
| 82 | - | ctx = ctx.Str("current_domain", logging.TruncateURL(current, maxPermissionLogURLLen)) | 80 | + | ctx = ctx.Str("current_domain", logging.TruncateURL(current, logging.PermissionLogURLMaxLen)) |
| 83 | } | 81 | } | ||
| 84 | enriched := ctx.Logger() | 82 | enriched := ctx.Logger() | ||
| 85 | return &enriched | 83 | return &enriched | ||
M internal/infrastructure/webkit/webview.go +3 -4
| @@ -608,7 +608,6 @@ func (wv *WebView) connectFaviconSignal() { | |||
| 608 | 608 | ||
| 609 | 609 | // progressThrottleInterval limits progress callbacks to ~60fps to reduce UI overhead. | |
| 610 | 610 | const progressThrottleInterval = 16 * time.Millisecond | |
| 611 | - | const maxPermissionLogURLLen = 96 | |
| 612 | 611 | ||
| 613 | 612 | func (wv *WebView) connectProgressSignal() { | |
| 614 | 613 | progressCb := func() { | |
| @@ -1047,11 +1046,11 @@ func (wv *WebView) connectPermissionRequestSignal() { | |||
| 1047 | 1046 | wv.logger.Warn().Msg("permission request with unknown type, denying") | |
| 1048 | 1047 | return false | |
| 1049 | 1048 | } | |
| 1050 | - | if len(permTypes) == 1 && permTypes[0] == "website_data_access" { | |
| 1049 | + | if len(permTypes) == 1 && permTypes[0] == string(entity.PermissionTypeWebsiteDataAccess) { | |
| 1051 | 1050 | wv.logger.Info(). | |
| 1052 | 1051 | Str("origin", origin). | |
| 1053 | - | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], maxPermissionLogURLLen)). | |
| 1054 | - | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], maxPermissionLogURLLen)). | |
| 1052 | + | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], logging.PermissionLogURLMaxLen)). | |
| 1053 | + | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], logging.PermissionLogURLMaxLen)). | |
| 1055 | 1054 | Msg("website data access permission requested") | |
| 1056 | 1055 | } | |
| 1057 | 1056 | ||
| @@ -608,7 +608,6 @@ func (wv *WebView) connectFaviconSignal() { | |||||
| 608 | 608 | ||||
| 609 | // progressThrottleInterval limits progress callbacks to ~60fps to reduce UI overhead. | 609 | // progressThrottleInterval limits progress callbacks to ~60fps to reduce UI overhead. | ||
| 610 | const progressThrottleInterval = 16 * time.Millisecond | 610 | const progressThrottleInterval = 16 * time.Millisecond | ||
| 611 | - | const maxPermissionLogURLLen = 96 | |||
| 612 | 611 | ||||
| 613 | func (wv *WebView) connectProgressSignal() { | 612 | func (wv *WebView) connectProgressSignal() { | ||
| 614 | progressCb := func() { | 613 | progressCb := func() { | ||
| @@ -1047,11 +1046,11 @@ func (wv *WebView) connectPermissionRequestSignal() { | |||||
| 1047 | wv.logger.Warn().Msg("permission request with unknown type, denying") | 1046 | wv.logger.Warn().Msg("permission request with unknown type, denying") | ||
| 1048 | return false | 1047 | return false | ||
| 1049 | } | 1048 | } | ||
| 1050 | - | if len(permTypes) == 1 && permTypes[0] == "website_data_access" { | 1049 | + | if len(permTypes) == 1 && permTypes[0] == string(entity.PermissionTypeWebsiteDataAccess) { |
| 1051 | wv.logger.Info(). | 1050 | wv.logger.Info(). | ||
| 1052 | Str("origin", origin). | 1051 | Str("origin", origin). | ||
| 1053 | - | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], maxPermissionLogURLLen)). | 1052 | + | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], logging.PermissionLogURLMaxLen)). |
| 1054 | - | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], maxPermissionLogURLLen)). | 1053 | + | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], logging.PermissionLogURLMaxLen)). |
| 1055 | Msg("website data access permission requested") | 1054 | Msg("website data access permission requested") | ||
| 1056 | } | 1055 | } | ||
| 1057 | 1056 | ||||
M internal/logging/logger.go +3 -0
| @@ -190,6 +190,9 @@ func ParseLevel(level string) zerolog.Level { | |||
| 190 | 190 | } | |
| 191 | 191 | } | |
| 192 | 192 | ||
| 193 | + | // PermissionLogURLMaxLen is the standard max length for permission-related URL logging. | |
| 194 | + | const PermissionLogURLMaxLen = 96 | |
| 195 | + | ||
| 193 | 196 | // TruncateURL truncates a URL to maxLen characters for logging. | |
| 194 | 197 | func TruncateURL(url string, maxLen int) string { | |
| 195 | 198 | if len(url) <= maxLen { | |
| @@ -190,6 +190,9 @@ func ParseLevel(level string) zerolog.Level { | |||||
| 190 | } | 190 | } | ||
| 191 | } | 191 | } | ||
| 192 | 192 | ||||
| 193 | + | // PermissionLogURLMaxLen is the standard max length for permission-related URL logging. | |||
| 194 | + | const PermissionLogURLMaxLen = 96 | |||
| 195 | + | ||||
| 193 | // TruncateURL truncates a URL to maxLen characters for logging. | 196 | // TruncateURL truncates a URL to maxLen characters for logging. | ||
| 194 | func TruncateURL(url string, maxLen int) string { | 197 | func TruncateURL(url string, maxLen int) string { | ||
| 195 | if len(url) <= maxLen { | 198 | if len(url) <= maxLen { | ||
M internal/ui/component/omnibox.go +1 -2
| @@ -951,14 +951,13 @@ func (o *Omnibox) setGhostText(originalInput, suffix string) { | |||
| 951 | 951 | func (o *Omnibox) clearGhostText() { | |
| 952 | 952 | log := logging.FromContext(o.ctx) | |
| 953 | 953 | o.mu.Lock() | |
| 954 | - | hadGhost := o.ghostSuffix != "" | |
| 955 | 954 | ghostSuffix := o.ghostSuffix | |
| 956 | 955 | realInput := o.realInput | |
| 957 | 956 | o.ghostSuffix = "" | |
| 958 | 957 | ||
| 959 | 958 | o.mu.Unlock() | |
| 960 | 959 | ||
| 961 | - | if !hadGhost || ghostSuffix == "" || o.entry == nil { | |
| 960 | + | if ghostSuffix == "" || o.entry == nil { | |
| 962 | 961 | return | |
| 963 | 962 | } | |
| 964 | 963 | ||
| @@ -951,14 +951,13 @@ func (o *Omnibox) setGhostText(originalInput, suffix string) { | |||||
| 951 | func (o *Omnibox) clearGhostText() { | 951 | func (o *Omnibox) clearGhostText() { | ||
| 952 | log := logging.FromContext(o.ctx) | 952 | log := logging.FromContext(o.ctx) | ||
| 953 | o.mu.Lock() | 953 | o.mu.Lock() | ||
| 954 | - | hadGhost := o.ghostSuffix != "" | |||
| 955 | ghostSuffix := o.ghostSuffix | 954 | ghostSuffix := o.ghostSuffix | ||
| 956 | realInput := o.realInput | 955 | realInput := o.realInput | ||
| 957 | o.ghostSuffix = "" | 956 | o.ghostSuffix = "" | ||
| 958 | 957 | ||||
| 959 | o.mu.Unlock() | 958 | o.mu.Unlock() | ||
| 960 | 959 | ||||
| 961 | - | if !hadGhost || ghostSuffix == "" || o.entry == nil { | 960 | + | if ghostSuffix == "" || o.entry == nil { |
| 962 | return | 961 | return | ||
| 963 | } | 962 | } | ||
| 964 | 963 | ||||
M internal/ui/dialog/permission.go +7 -8
| @@ -13,8 +13,6 @@ import ( | |||
| 13 | 13 | "github.com/bnema/dumber/internal/ui/component" | |
| 14 | 14 | ) | |
| 15 | 15 | ||
| 16 | - | const maxPermissionLogURLLen = 96 | |
| 17 | - | ||
| 18 | 16 | type permissionPopup interface { | |
| 19 | 17 | Show(ctx context.Context, heading, body string, callback func(allowed, persistent bool)) | |
| 20 | 18 | } | |
| @@ -102,20 +100,21 @@ func (d *PermissionDialog) showRequest(req permissionDialogRequest) { | |||
| 102 | 100 | heading := d.buildHeading(permTypes) | |
| 103 | 101 | body := d.buildBody(origin, permTypes, metadata) | |
| 104 | 102 | ||
| 105 | - | if parsePermFlags(permTypes).dataAccess { | |
| 103 | + | isDataAccess := parsePermFlags(permTypes).dataAccess | |
| 104 | + | if isDataAccess { | |
| 106 | 105 | log.Info(). | |
| 107 | 106 | Str("origin", origin). | |
| 108 | - | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], maxPermissionLogURLLen)). | |
| 109 | - | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], maxPermissionLogURLLen)). | |
| 107 | + | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], logging.PermissionLogURLMaxLen)). | |
| 108 | + | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], logging.PermissionLogURLMaxLen)). | |
| 110 | 109 | Msg("showing website data access permission dialog") | |
| 111 | 110 | } | |
| 112 | 111 | ||
| 113 | 112 | d.popup.Show(ctx, heading, body, func(allowed, persistent bool) { | |
| 114 | - | if parsePermFlags(permTypes).dataAccess { | |
| 113 | + | if isDataAccess { | |
| 115 | 114 | log.Info(). | |
| 116 | 115 | Str("origin", origin). | |
| 117 | - | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], maxPermissionLogURLLen)). | |
| 118 | - | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], maxPermissionLogURLLen)). | |
| 116 | + | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], logging.PermissionLogURLMaxLen)). | |
| 117 | + | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], logging.PermissionLogURLMaxLen)). | |
| 119 | 118 | Bool("allowed", allowed). | |
| 120 | 119 | Bool("persistent", persistent). | |
| 121 | 120 | Msg("website data access permission dialog response") | |
| @@ -13,8 +13,6 @@ import ( | |||||
| 13 | "github.com/bnema/dumber/internal/ui/component" | 13 | "github.com/bnema/dumber/internal/ui/component" | ||
| 14 | ) | 14 | ) | ||
| 15 | 15 | ||||
| 16 | - | const maxPermissionLogURLLen = 96 | |||
| 17 | - | ||||
| 18 | type permissionPopup interface { | 16 | type permissionPopup interface { | ||
| 19 | Show(ctx context.Context, heading, body string, callback func(allowed, persistent bool)) | 17 | Show(ctx context.Context, heading, body string, callback func(allowed, persistent bool)) | ||
| 20 | } | 18 | } | ||
| @@ -102,20 +100,21 @@ func (d *PermissionDialog) showRequest(req permissionDialogRequest) { | |||||
| 102 | heading := d.buildHeading(permTypes) | 100 | heading := d.buildHeading(permTypes) | ||
| 103 | body := d.buildBody(origin, permTypes, metadata) | 101 | body := d.buildBody(origin, permTypes, metadata) | ||
| 104 | 102 | ||||
| 105 | - | if parsePermFlags(permTypes).dataAccess { | 103 | + | isDataAccess := parsePermFlags(permTypes).dataAccess |
| 104 | + | if isDataAccess { | |||
| 106 | log.Info(). | 105 | log.Info(). | ||
| 107 | Str("origin", origin). | 106 | Str("origin", origin). | ||
| 108 | - | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], maxPermissionLogURLLen)). | 107 | + | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], logging.PermissionLogURLMaxLen)). |
| 109 | - | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], maxPermissionLogURLLen)). | 108 | + | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], logging.PermissionLogURLMaxLen)). |
| 110 | Msg("showing website data access permission dialog") | 109 | Msg("showing website data access permission dialog") | ||
| 111 | } | 110 | } | ||
| 112 | 111 | ||||
| 113 | d.popup.Show(ctx, heading, body, func(allowed, persistent bool) { | 112 | d.popup.Show(ctx, heading, body, func(allowed, persistent bool) { | ||
| 114 | - | if parsePermFlags(permTypes).dataAccess { | 113 | + | if isDataAccess { |
| 115 | log.Info(). | 114 | log.Info(). | ||
| 116 | Str("origin", origin). | 115 | Str("origin", origin). | ||
| 117 | - | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], maxPermissionLogURLLen)). | 116 | + | Str("requesting_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyRequestingDomain], logging.PermissionLogURLMaxLen)). |
| 118 | - | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], maxPermissionLogURLLen)). | 117 | + | Str("current_domain", logging.TruncateURL(metadata[entity.PermissionMetadataKeyCurrentDomain], logging.PermissionLogURLMaxLen)). |
| 119 | Bool("allowed", allowed). | 118 | Bool("allowed", allowed). | ||
| 120 | Bool("persistent", persistent). | 119 | Bool("persistent", persistent). | ||
| 121 | Msg("website data access permission dialog response") | 120 | Msg("website data access permission dialog response") | ||