debug_cgo.go RawHistoryFolder
1//go:build webkit_cgo
2
3package webkit
4
5/*
6#cgo pkg-config: webkitgtk-6.0 gtk4
7#cgo CFLAGS: -I/usr/include/webkitgtk-6.0
8#include <webkit/webkit.h>
9#include <stdlib.h>
10
11// WebKit debug logging callback
12extern void webkit_debug_log(char* message);
13
14// Forward declaration
15static void webkit_debug_log_handler(const gchar *log_domain,
16 GLogLevelFlags log_level,
17 const gchar *message,
18 gpointer user_data);
19
20static void setup_webkit_debug_logging() {
21 // Enable WebKit debug logging if WEBKIT_DEBUG environment variable is set
22 const char* debug_env = g_getenv("WEBKIT_DEBUG");
23 if (debug_env) {
24 g_setenv("G_MESSAGES_DEBUG", "WebKit", TRUE);
25 g_setenv("WEBKIT_DEBUG", debug_env, TRUE);
26
27 // Set up custom log handler for better integration
28 g_log_set_handler("WebKit",
29 G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE,
30 (GLogFunc)webkit_debug_log_handler, NULL);
31 }
32}
33
34static void webkit_debug_log_handler(const gchar *log_domain,
35 GLogLevelFlags log_level,
36 const gchar *message,
37 gpointer user_data) {
38 // Format log message and send to Go
39 char* formatted_msg = g_strdup_printf("[%s] %s", log_domain, message);
40 webkit_debug_log(formatted_msg);
41 g_free(formatted_msg);
42}
43
44// Enable detailed content filtering debug logs
45static void enable_filtering_debug() {
46 g_setenv("WEBKIT_DEBUG", "Network:preconnectTo,ContentFilters", TRUE);
47}
48
49// Check if WebView is in a valid state for operations
50static gboolean check_webview_state(WebKitWebView *webview) {
51 if (!webview) return FALSE;
52 if (!WEBKIT_IS_WEB_VIEW(webview)) return FALSE;
53
54 // Check if WebView is not being destroyed
55 GObject *obj = G_OBJECT(webview);
56 if (g_object_get_data(obj, "webkit-destroying")) return FALSE;
57
58 return TRUE;
59}
60*/
61import "C"
62
63import (
64 "os"
65
66 "github.com/bnema/dumber/internal/config"
67 "github.com/bnema/dumber/internal/logging"
68)
69
70//export webkit_debug_log
71func webkit_debug_log(message *C.char) {
72 msg := C.GoString(message)
73 logging.Debug("[webkit-internal] " + msg)
74}
75
76// SetupWebKitDebugLogging initializes WebKit debug logging based on config and environment variables
77func SetupWebKitDebugLogging(cfg *config.Config) {
78 // Check if debug mode is enabled via config or environment variables
79 debugEnabled := (cfg != nil && cfg.Debug.EnableWebKitDebug) ||
80 os.Getenv("WEBKIT_DEBUG") != "" ||
81 os.Getenv("DUMBER_DEBUG") != "" ||
82 (cfg != nil && cfg.Debug.EnableGeneralDebug)
83
84 if debugEnabled {
85 logging.Info("[webkit] Enabling WebKit debug logging")
86
87 // Set debug categories from config or environment
88 var debugCategories string
89 if cfg != nil && cfg.Debug.WebKitDebugCategories != "" {
90 debugCategories = cfg.Debug.WebKitDebugCategories
91 } else if envDebug := os.Getenv("WEBKIT_DEBUG"); envDebug != "" {
92 debugCategories = envDebug
93 } else {
94 debugCategories = "Network:preconnectTo,ContentFilters"
95 }
96
97 // Set the environment variable for WebKit
98 os.Setenv("WEBKIT_DEBUG", debugCategories)
99
100 C.setup_webkit_debug_logging()
101
102 // Enable filtering-specific debug logs if configured
103 if cfg != nil && cfg.Debug.EnableFilteringDebug {
104 C.enable_filtering_debug()
105 logging.Info("[webkit] Content filtering debug logging enabled")
106 }
107
108 logging.Info("[webkit] WebKit debug logging enabled with categories: " + debugCategories)
109 }
110}
111
112// CheckWebViewState validates that a WebView is in a safe state for operations
113func (w *WebView) CheckWebViewState() bool {
114 if w == nil || w.destroyed || w.native == nil || w.native.wv == nil {
115 return false
116 }
117
118 return C.check_webview_state(w.native.wv) != 0
119}
120
121// LogDebugInfo logs debug information about the current WebView state
122func (w *WebView) LogDebugInfo() {
123 if !w.CheckWebViewState() {
124 logging.Debug("[webkit] WebView is not in valid state")
125 return
126 }
127
128 logging.Debug("[webkit] WebView state: valid and ready for operations")
129
130 // Log content manager state
131 manager := C.webkit_web_view_get_user_content_manager(w.native.wv)
132 if manager != nil {
133 logging.Debug("[webkit] User content manager: available")
134 } else {
135 logging.Debug("[webkit] User content manager: not available")
136 }
137}