1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* Copyright (c) 2021 Matthew Leeds <mwleeds@protonmail.com>
*
* This file is part of Epiphany.
*
* Epiphany is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Epiphany is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Epiphany. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "ephy-webapp-provider.h"
#include "ephy-web-app-utils.h"
#include "ephy-flatpak-utils.h"
#include <gio/gio.h>
#include <glib/gi18n.h>
struct _EphyWebAppProviderService {
GApplication parent_instance;
EphyWebAppProvider *skeleton;
};
struct _EphyWebAppProviderServiceClass {
GApplicationClass parent_class;
};
G_DEFINE_TYPE (EphyWebAppProviderService, ephy_web_app_provider_service, G_TYPE_APPLICATION)
#define INACTIVITY_TIMEOUT 60 * 1000 /* One minute, in milliseconds */
typedef enum {
EPHY_WEBAPP_PROVIDER_ERROR_FAILED,
EPHY_WEBAPP_PROVIDER_ERROR_INVALID_ARGS,
EPHY_WEBAPP_PROVIDER_ERROR_NOT_INSTALLED,
EPHY_WEBAPP_PROVIDER_ERROR_LAST = EPHY_WEBAPP_PROVIDER_ERROR_NOT_INSTALLED, /*< skip >*/
} EphyWebAppProviderError;
static const GDBusErrorEntry ephy_webapp_provider_error_entries[] = {
{ EPHY_WEBAPP_PROVIDER_ERROR_FAILED, "org.gnome.Epiphany.WebAppProvider.Error.Failed" },
{ EPHY_WEBAPP_PROVIDER_ERROR_INVALID_ARGS, "org.gnome.Epiphany.WebAppProvider.Error.InvalidArgs" },
{ EPHY_WEBAPP_PROVIDER_ERROR_NOT_INSTALLED, "org.gnome.Epiphany.WebAppProvider.Error.NotInstalled" },
};
/* Ensure that every error code has an associated D-Bus error name */
G_STATIC_ASSERT (G_N_ELEMENTS (ephy_webapp_provider_error_entries) == EPHY_WEBAPP_PROVIDER_ERROR_LAST + 1);
#define EPHY_WEBAPP_PROVIDER_ERROR (ephy_webapp_provider_error_quark ())
GQuark
ephy_webapp_provider_error_quark (void)
{
static gsize quark = 0;
g_dbus_error_register_error_domain ("ephy-webapp-provider-error-quark",
&quark,
ephy_webapp_provider_error_entries,
G_N_ELEMENTS (ephy_webapp_provider_error_entries));
return (GQuark)quark;
}
static gboolean
handle_get_installed_apps (EphyWebAppProvider *skeleton,
GDBusMethodInvocation *invocation,
EphyWebAppProviderService *self)
{
g_auto (GStrv) desktop_ids = NULL;
g_debug ("%s", G_STRFUNC);
g_application_hold (G_APPLICATION (self));
desktop_ids = ephy_web_application_get_desktop_id_list ();
ephy_web_app_provider_complete_get_installed_apps (skeleton, invocation,
(const gchar * const *)desktop_ids);
g_application_release (G_APPLICATION (self));
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
static gboolean
handle_install (EphyWebAppProvider *skeleton,
GDBusMethodInvocation *invocation,
char *url,
char *name,
char *install_token,
EphyWebAppProviderService *self)
{
g_autofree char *id = NULL;
g_autofree char *desktop_path = NULL;
g_autofree char *desktop_file_id = NULL;
g_autoptr (GError) local_error = NULL;
g_debug ("%s", G_STRFUNC);
g_application_hold (G_APPLICATION (self));
/* We need an install token acquired by a trusted system component such as
* gnome-software because otherwise the Flatpak/Snap sandbox prevents us from
* installing the app without using a portal (which would not be appropriate
* since Epiphany is not the focused application). We use the same code path
* when not running under a sandbox too.
*/
if (!install_token || *install_token == '\0') {
g_dbus_method_invocation_return_error (invocation, EPHY_WEBAPP_PROVIDER_ERROR,
EPHY_WEBAPP_PROVIDER_ERROR_INVALID_ARGS,
_("The install_token is required for the Install() method"));
goto out;
}
if (!g_uri_is_valid (url, G_URI_FLAGS_NONE, NULL)) {
g_dbus_method_invocation_return_error (invocation, EPHY_WEBAPP_PROVIDER_ERROR,
EPHY_WEBAPP_PROVIDER_ERROR_INVALID_ARGS,
_("The url passed was not valid: ‘%s’"), url);
goto out;
}
if (!name || *name == '\0') {
g_dbus_method_invocation_return_error (invocation, EPHY_WEBAPP_PROVIDER_ERROR,
EPHY_WEBAPP_PROVIDER_ERROR_INVALID_ARGS,
_("The name passed was not valid"));
goto out;
}
id = ephy_web_application_get_app_id_from_name (name);
if (!ephy_web_application_create (id, url,
install_token,
EPHY_WEB_APPLICATION_NONE,
&local_error)) {
g_dbus_method_invocation_return_error (invocation, EPHY_WEBAPP_PROVIDER_ERROR,
EPHY_WEBAPP_PROVIDER_ERROR_FAILED,
_("Installing the web application ‘%s’ (%s) failed: %s"),
name, url, local_error->message);
g_clear_error (&local_error);
goto out;
}
desktop_path = ephy_web_application_get_desktop_path (id);
desktop_file_id = g_path_get_basename (desktop_path);
ephy_web_app_provider_complete_install (skeleton, invocation, desktop_file_id);
out:
g_application_release (G_APPLICATION (self));
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
static gboolean
handle_uninstall (EphyWebAppProvider *skeleton,
GDBusMethodInvocation *invocation,
char *desktop_file_id,
EphyWebAppProviderService *self)
{
EphyWebAppFound app_found;
g_debug ("%s", G_STRFUNC);
g_application_hold (G_APPLICATION (self));
if (!desktop_file_id || !g_str_has_suffix (desktop_file_id, ".desktop") ||
!g_str_has_prefix (desktop_file_id, EPHY_WEB_APP_GAPPLICATION_ID_PREFIX)) {
g_dbus_method_invocation_return_error (invocation, EPHY_WEBAPP_PROVIDER_ERROR,
EPHY_WEBAPP_PROVIDER_ERROR_INVALID_ARGS,
_("The desktop file ID passed ‘%s’ was not valid"),
desktop_file_id ? desktop_file_id : "(null)");
goto out;
}
if (!ephy_web_application_delete_by_desktop_file_id (desktop_file_id, &app_found)) {
if (app_found == EPHY_WEB_APP_NOT_FOUND) {
g_dbus_method_invocation_return_error (invocation, EPHY_WEBAPP_PROVIDER_ERROR,
EPHY_WEBAPP_PROVIDER_ERROR_NOT_INSTALLED,
_("The web application ‘%s’ does not exist"),
desktop_file_id);
} else {
g_dbus_method_invocation_return_error (invocation, EPHY_WEBAPP_PROVIDER_ERROR,
EPHY_WEBAPP_PROVIDER_ERROR_FAILED,
_("The web application ‘%s’ could not be deleted"),
desktop_file_id);
}
goto out;
}
ephy_web_app_provider_complete_uninstall (skeleton, invocation);
out:
g_application_release (G_APPLICATION (self));
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
static void
ephy_web_app_provider_service_init (EphyWebAppProviderService *self)
{
g_application_set_flags (G_APPLICATION (self), G_APPLICATION_IS_SERVICE);
g_application_set_inactivity_timeout (G_APPLICATION (self), INACTIVITY_TIMEOUT);
}
static gboolean
ephy_web_app_provider_service_dbus_register (GApplication *application,
GDBusConnection *connection,
const gchar *object_path,
GError **error)
{
EphyWebAppProviderService *self;
g_debug ("registering at object path %s", object_path);
if (!G_APPLICATION_CLASS (ephy_web_app_provider_service_parent_class)->dbus_register (application,
connection,
object_path,
error))
return FALSE;
self = EPHY_WEB_APP_PROVIDER_SERVICE (application);
self->skeleton = ephy_web_app_provider_skeleton_new ();
ephy_web_app_provider_set_version (EPHY_WEB_APP_PROVIDER (self->skeleton), 1);
g_signal_connect (self->skeleton, "handle-get-installed-apps",
G_CALLBACK (handle_get_installed_apps), self);
g_signal_connect (self->skeleton, "handle-install",
G_CALLBACK (handle_install), self);
g_signal_connect (self->skeleton, "handle-uninstall",
G_CALLBACK (handle_uninstall), self);
return g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->skeleton),
connection, object_path, error);
}
static void
ephy_web_app_provider_service_dbus_unregister (GApplication *application,
GDBusConnection *connection,
const gchar *object_path)
{
EphyWebAppProviderService *self;
GDBusInterfaceSkeleton *skeleton;
g_debug ("unregistering at object path %s", object_path);
self = EPHY_WEB_APP_PROVIDER_SERVICE (application);
skeleton = G_DBUS_INTERFACE_SKELETON (self->skeleton);
if (g_dbus_interface_skeleton_has_connection (skeleton, connection))
g_dbus_interface_skeleton_unexport_from_connection (skeleton, connection);
g_clear_object (&self->skeleton);
G_APPLICATION_CLASS (ephy_web_app_provider_service_parent_class)->dbus_unregister (application,
connection,
object_path);
}
static void
ephy_web_app_provider_service_class_init (EphyWebAppProviderServiceClass *klass)
{
GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
application_class->dbus_register = ephy_web_app_provider_service_dbus_register;
application_class->dbus_unregister = ephy_web_app_provider_service_dbus_unregister;
}
EphyWebAppProviderService *
ephy_web_app_provider_service_new (void)
{
/* Note the application ID is constant for release/devel/canary builds
* because we want to always use the same well-known D-Bus name.
*/
g_autofree gchar *app_id = g_strconcat ("org.gnome.Epiphany.WebAppProvider", NULL);
return g_object_new (EPHY_TYPE_WEB_APP_PROVIDER_SERVICE,
"application-id", app_id,
NULL);
}
|