GCC Code Coverage Report


Directory: src/
File: src/backend/dbus.c
Date: 2023-01-04 17:35:37
Exec Total Coverage
Lines: 0 44 0.0%
Branches: 0 20 0.0%

Line Branch Exec Source
1 /* main.c
2 *
3 * Copyright 2022 Jan-Michael Brummer
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "px-manager.h"
23 #include "px-module.h"
24 #include "px-interface.h"
25
26 #include <gio/gio.h>
27
28 static void
29 handle_method_call (GDBusConnection *connection,
30 const gchar *sender,
31 const gchar *object_path,
32 const gchar *interface_name,
33 const gchar *method_name,
34 GVariant *parameters,
35 GDBusMethodInvocation *invocation,
36 gpointer user_data)
37 {
38 PxManager *manager = PX_MANAGER (user_data);
39 GVariantBuilder *result;
40 g_auto (GStrv) proxies = NULL;
41 g_autoptr (GError) error = NULL;
42 const gchar *url;
43 int idx;
44
45 if (g_strcmp0 (method_name, "query") != 0) {
46 g_warning ("Invalid method name '%s', aborting.", method_name);
47 g_dbus_method_invocation_return_error (invocation,
48 PX_MANAGER_ERROR,
49 PX_MANAGER_ERROR_UNKNOWN_METHOD,
50 "Unknown method");
51 return;
52 }
53
54 g_variant_get (parameters, "(&s)", &url);
55
56 g_print ("%s: ENTER\n", __FUNCTION__);
57 proxies = px_manager_get_proxies_sync (manager, url, &error);
58 if (error) {
59 g_warning ("Could not query proxy servers: %s", error->message);
60 g_dbus_method_invocation_return_gerror (invocation, error);
61 return;
62 }
63
64 result = g_variant_builder_new (G_VARIANT_TYPE ("as"));
65 if (proxies) {
66 for (idx = 0; proxies[idx]; idx++)
67 g_variant_builder_add (result, "s", proxies[idx]);
68 }
69
70 g_dbus_method_invocation_return_value (invocation,
71 g_variant_new ("(as)", result));
72 }
73
74 static GVariant *
75 handle_get_property (GDBusConnection *connection,
76 const gchar *sender,
77 const gchar *object_path,
78 const gchar *interface_name,
79 const gchar *property_name,
80 GError **error,
81 gpointer user_data)
82 {
83 GVariant *ret = NULL;
84
85 if (g_strcmp0 (property_name, "APIVersion") == 0)
86 ret = g_variant_new_string ("1.0");
87
88 return ret;
89 }
90
91 static const GDBusInterfaceVTable interface_vtable = {
92 handle_method_call,
93 handle_get_property
94 };
95
96 static void
97 on_bus_acquired (GDBusConnection *connection,
98 const gchar *name,
99 gpointer user_data)
100 {
101 g_autoptr (GError) error = NULL;
102 PxManager *manager = NULL;
103
104 manager = px_manager_new ();
105 g_dbus_connection_register_object (connection,
106 "/org/libproxy/proxy",
107 (GDBusInterfaceInfo *)&org_libproxy_proxy_interface,
108 &interface_vtable,
109 manager,
110 g_object_unref,
111 &error);
112 if (error) {
113 g_warning ("Could not register dbus object: %s", error->message);
114 g_main_loop_quit (user_data);
115 return;
116 }
117 }
118
119 static void
120 on_name_lost (GDBusConnection *connection,
121 const gchar *name,
122 gpointer user_data)
123 {
124 if (!connection) {
125 g_warning ("Can't connect proxy bus");
126 g_main_loop_quit (user_data);
127 } else {
128 g_warning ("Unknown name lost error");
129 }
130 }
131
132 int
133 main (int argc,
134 char **argv)
135 {
136 GMainLoop *loop;
137
138 loop = g_main_loop_new (NULL, FALSE);
139
140 g_bus_own_name (G_BUS_TYPE_SESSION,
141 "org.libproxy.proxy",
142 G_BUS_NAME_OWNER_FLAGS_NONE,
143 on_bus_acquired,
144 NULL,
145 on_name_lost,
146 loop,
147 NULL);
148
149 g_main_loop_run (loop);
150
151 return 0;
152 }
153