Remove inclusion of sys/socket.h from nntp-thread.c
[claws.git] / src / plugins / fancy / fancy_web_extension.c
blob26072bfa250538155c70ab47404154e16a7d2f7f
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2022 the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #define G_LOG_DOMAIN "Fancy-Web-Ext"
21 #include <glib-object.h>
22 #include <webkit2/webkit-web-extension.h>
24 static gboolean load_remote_content = FALSE;
26 static gboolean web_page_send_request_cb(WebKitWebPage *web_page,
27 WebKitURIRequest *request,
28 WebKitURIResponse *redirected_response,
29 gpointer user_data)
31 gboolean is_remote = TRUE;
32 gboolean should_block;
33 const char *request_uri = webkit_uri_request_get_uri(request);
34 #if GLIB_CHECK_VERSION(2,66,0)
35 const char *scheme = g_uri_peek_scheme(request_uri);
36 #else
37 gchar *scheme = g_uri_parse_scheme(request_uri);
38 #endif
39 if (scheme == NULL)
40 return TRUE;
41 #if GLIB_CHECK_VERSION(2,66,0)
42 if (strcmp(scheme, "cid") == 0 ||
43 strcmp(scheme, "file") == 0 ||
44 strcmp(scheme, "about") == 0)
45 is_remote = FALSE;
46 #else
47 if (g_ascii_strcasecmp(scheme, "cid") == 0 ||
48 g_ascii_strcasecmp(scheme, "file") == 0 ||
49 g_ascii_strcasecmp(scheme, "about") == 0)
50 is_remote = FALSE;
51 g_free(scheme);
52 #endif
53 if (is_remote)
54 should_block = !load_remote_content;
55 else
56 should_block = FALSE;
58 g_debug("Filter page: %s request: %s => %s uri %s",
59 webkit_web_page_get_uri(web_page),
60 request_uri,
61 is_remote ? "remote" : "local",
62 should_block ? "blocked" : "allowed");
64 return should_block;
67 static gboolean web_page_user_message_received_cb(WebKitWebPage *page,
68 WebKitUserMessage *message,
69 gpointer user_data)
71 const char *name = webkit_user_message_get_name(message);
73 g_debug("WebPage user message received callback - %s", name);
75 if (g_strcmp0(name, "LoadRemoteContent") == 0) {
76 GVariant *parameters;
78 parameters = webkit_user_message_get_parameters(message);
79 if (!parameters)
80 return FALSE;
82 load_remote_content = g_variant_get_boolean(parameters);
84 g_debug("LoadRemoteContent param - %d", load_remote_content);
86 return TRUE;
89 return FALSE;
92 static void web_page_created_cb(WebKitWebExtension *extension,
93 WebKitWebPage *web_page,
94 gpointer user_data)
96 g_debug("Page %ld created for %s",
97 webkit_web_page_get_id(web_page),
98 webkit_web_page_get_uri(web_page));
100 g_signal_connect_object(web_page,
101 "send-request",
102 G_CALLBACK(web_page_send_request_cb),
103 NULL,
106 g_signal_connect_object(web_page,
107 "user-message-received",
108 G_CALLBACK(web_page_user_message_received_cb),
109 NULL,
113 G_MODULE_EXPORT void webkit_web_extension_initialize(WebKitWebExtension *extension)
115 if (!g_setenv("G_MESSAGES_DEBUG", G_LOG_DOMAIN, FALSE))
116 g_warning("could not set G_MESSAGES_DEBUG\n");
118 g_debug("Initializing Fancy web process extension");
120 g_signal_connect(extension,
121 "page-created",
122 G_CALLBACK(web_page_created_cb),
123 NULL);