summaryrefslogtreecommitdiffstats
path: root/plugins/epiphany/gs-plugin-epiphany.c
blob: 537ed400c4703d851a0d598734afe7238a2b501a (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 * vi:set noexpandtab tabstop=8 shiftwidth=8:
 *
 * Copyright (C) 2021-2022 Matthew Leeds <mwleeds@protonmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include <config.h>
#include <glib/gi18n.h>
#include <gnome-software.h>
#include <fcntl.h>
#include <gio/gunixfdlist.h>
#include <glib/gstdio.h>

#include "gs-epiphany-generated.h"
#include "gs-plugin-epiphany.h"
#include "gs-plugin-private.h"

/*
 * SECTION:
 * This plugin uses Epiphany to install, launch, and uninstall web applications.
 *
 * If the org.gnome.Epiphany.WebAppProvider D-Bus interface is not present or
 * the DynamicLauncher portal is not available then it self-disables. This
 * should work with both Flatpak'd and traditionally packaged Epiphany, for new
 * enough versions of Epiphany.
 *
 * It's worth noting that this plugin has to deal with two different app IDs
 * for installed web apps:
 *
 * 1. The app ID used in the <id> element in the AppStream metainfo file, which
 *    looks like "org.gnome.Software.WebApp_527a2dd6729c3574227c145bbc447997f0048537.desktop"
 *    See https://gitlab.gnome.org/mwleeds/gnome-pwa-list/-/blob/6e8b17b018f99dbf00b1fa956ed75c4a0ccbf389/pwa-metainfo-generator.py#L84-89
 *    This app ID is used for gs_app_new() so that the appstream plugin
 *    refines the apps created here, and used for the plugin cache.
 *
 * 2. The app ID generated by Epiphany when installing a web app, which looks
 *    like "org.gnome.Epiphany.WebApp_e9d0e1e4b0a10856aa3b38d9eb4375de4070d043.desktop"
 *    though it can have a different prefix if Epiphany was built with, for
 *    example, a development profile. Throughout this plugin this type of app
 *    ID is handled with a variable called "installed_app_id". This app ID is
 *    used in method calls to the org.gnome.Epiphany.WebAppProvider interface,
 *    and used for gs_app_set_launchable() and g_desktop_app_info_new().
 *
 * Since: 43
 */

struct _GsPluginEpiphany
{
	GsPlugin parent;

	GsWorkerThread *worker;  /* (owned) */

	GsEphyWebAppProvider *epiphany_proxy;  /* (owned) */
	GDBusProxy *launcher_portal_proxy;  /* (owned) */
	GFileMonitor *monitor; /* (owned) */
	guint changed_id;
	/* protects installed_apps_cached, url_id_map, and the plugin cache */
	GMutex installed_apps_mutex;
	/* installed_apps_cached: whether the plugin cache has all installed apps */
	gboolean installed_apps_cached;
	GHashTable *url_id_map; /* (owned) (not nullable) (element-type utf8 utf8) */

	/* default permissions, shared between all applications */
	GsAppPermissions *permissions; /* (owned) (not nullable) */
};

G_DEFINE_TYPE (GsPluginEpiphany, gs_plugin_epiphany, GS_TYPE_PLUGIN)

#define assert_in_worker(self) \
	g_assert (gs_worker_thread_is_in_worker_context (self->worker))

static void
gs_epiphany_error_convert (GError **perror)
{
	GError *error = perror != NULL ? *perror : NULL;

	/* not set */
	if (error == NULL)
		return;

	/* parse remote epiphany-webapp-provider error */
	if (g_dbus_error_is_remote_error (error)) {
		g_autofree gchar *remote_error = g_dbus_error_get_remote_error (error);

		g_dbus_error_strip_remote_error (error);

		if (g_str_equal (remote_error, "org.freedesktop.DBus.Error.ServiceUnknown")) {
			error->code = GS_PLUGIN_ERROR_NOT_SUPPORTED;
		} else if (g_str_has_prefix (remote_error, "org.gnome.Epiphany.WebAppProvider.Error")) {
			error->code = GS_PLUGIN_ERROR_FAILED;
		} else {
			g_warning ("Can’t reliably fixup remote error ‘%s’", remote_error);
			error->code = GS_PLUGIN_ERROR_FAILED;
		}
		error->domain = GS_PLUGIN_ERROR;
		return;
	}

	/* this is allowed for low-level errors */
	if (gs_utils_error_convert_gio (perror))
		return;

	/* this is allowed for low-level errors */
	if (gs_utils_error_convert_gdbus (perror))
		return;
}

/* Run in the main thread. */
static void
gs_plugin_epiphany_changed_cb (GFileMonitor      *monitor,
                               GFile             *file,
                               GFile             *other_file,
                               GFileMonitorEvent  event_type,
                               gpointer           user_data)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (user_data);

	{
	  g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&self->installed_apps_mutex);
	  gs_plugin_cache_invalidate (GS_PLUGIN (self));
	  g_hash_table_remove_all (self->url_id_map);
	  self->installed_apps_cached = FALSE;
	}

	/* FIXME: With the current API this is the only way to reload the list
	 * of installed apps.
	 */
	gs_plugin_reload (GS_PLUGIN (self));
}

static void
epiphany_web_app_provider_proxy_created_cb (GObject      *source_object,
                                            GAsyncResult *result,
                                            gpointer      user_data);

static void
dynamic_launcher_portal_proxy_created_cb (GObject      *source_object,
                                          GAsyncResult *result,
                                          gpointer      user_data);

static void
gs_plugin_epiphany_setup_async (GsPlugin            *plugin,
                                GCancellable        *cancellable,
                                GAsyncReadyCallback  callback,
                                gpointer             user_data)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (plugin);
	g_autoptr(GTask) task = NULL;
	g_autoptr(GError) local_error = NULL;
	g_autofree char *portal_apps_path = NULL;
	g_autoptr(GFile) portal_apps_file = NULL;
	GDBusConnection *connection;

	task = g_task_new (plugin, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_epiphany_setup_async);

	g_debug ("%s", G_STRFUNC);

	self->installed_apps_cached = FALSE;

	/* This is a mapping from URL to app ID, where the app ID comes from
	 * Epiphany. This allows us to use that app ID rather than the
	 * AppStream app ID in certain contexts (see the comment at the top of
	 * this file).
	 */
	self->url_id_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

	/* Watch for changes to the set of installed apps in the main thread.
	 * This will also trigger when other apps' dynamic launchers are
	 * installed or removed but that is expected to be infrequent.
	 */
	portal_apps_path = g_build_filename (g_get_user_data_dir (), "xdg-desktop-portal", "applications", NULL);
	portal_apps_file = g_file_new_for_path (portal_apps_path);
	/* Monitoring the directory works even if it doesn't exist yet */
	self->monitor = g_file_monitor_directory (portal_apps_file, G_FILE_MONITOR_WATCH_MOVES,
			                          cancellable, &local_error);
	if (self->monitor == NULL) {
		gs_epiphany_error_convert (&local_error);
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	self->changed_id = g_signal_connect (self->monitor, "changed",
					     G_CALLBACK (gs_plugin_epiphany_changed_cb), self);

	connection = gs_plugin_get_session_bus_connection (GS_PLUGIN (self));
	g_assert (connection != NULL);

	gs_ephy_web_app_provider_proxy_new (connection,
					    G_DBUS_PROXY_FLAGS_NONE,
					    "org.gnome.Epiphany.WebAppProvider",
					    "/org/gnome/Epiphany/WebAppProvider",
					    cancellable,
					    epiphany_web_app_provider_proxy_created_cb,
					    g_steal_pointer (&task));
}

static void
epiphany_web_app_provider_proxy_created_cb (GObject      *source_object,
                                            GAsyncResult *result,
                                            gpointer      user_data)
{
	g_autoptr(GTask) task = G_TASK (user_data);
	g_autofree gchar *name_owner = NULL;
	g_autoptr(GError) local_error = NULL;
	GsPluginEpiphany *self = g_task_get_source_object (task);
	GDBusConnection *connection;
	GCancellable *cancellable;

	/* Check that the proxy exists (and is owned; it should auto-start) so
	 * we can disable the plugin for systems which don’t have new enough
	 * Epiphany.
	 */
	self->epiphany_proxy = gs_ephy_web_app_provider_proxy_new_finish (result, &local_error);

	if (self->epiphany_proxy == NULL) {
		gs_epiphany_error_convert (&local_error);
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (self->epiphany_proxy));
	if (name_owner == NULL) {
		g_task_return_new_error (task, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_NOT_SUPPORTED,
					 "Couldn’t create Epiphany WebAppProvider proxy: couldn’t get name owner");
		return;
	}

	connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (self->epiphany_proxy));
	cancellable = g_task_get_cancellable (task);

	g_dbus_proxy_new (connection,
			  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
			  NULL,
			  "org.freedesktop.portal.Desktop",
			  "/org/freedesktop/portal/desktop",
			  "org.freedesktop.portal.DynamicLauncher",
			  cancellable,
			  dynamic_launcher_portal_proxy_created_cb,
			  g_steal_pointer (&task));
}

static void
dynamic_launcher_portal_proxy_created_cb (GObject      *source_object,
                                          GAsyncResult *result,
                                          gpointer      user_data)
{
	g_autoptr(GTask) task = G_TASK (user_data);
	g_autoptr(GVariant) version = NULL;
	g_autoptr(GError) local_error = NULL;
	GsPluginEpiphany *self = g_task_get_source_object (task);

	/* Check that the proxy exists (and is owned; it should auto-start) so
	 * we can disable the plugin for systems which don’t have new enough
	 * Epiphany.
	 */
	self->launcher_portal_proxy = g_dbus_proxy_new_finish (result, &local_error);

	if (self->launcher_portal_proxy == NULL) {
		gs_epiphany_error_convert (&local_error);
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	version = g_dbus_proxy_get_cached_property (self->launcher_portal_proxy, "version");
	if (version == NULL) {
		g_task_return_new_error (task, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_NOT_SUPPORTED,
					 "Dynamic launcher portal not available");
		return;
	} else {
		g_debug ("Found version %" G_GUINT32_FORMAT " of the dynamic launcher portal",
			 g_variant_get_uint32 (version));
	}

	/* Start up a worker thread to process all the plugin’s function calls. */
	self->worker = gs_worker_thread_new ("gs-plugin-epiphany");

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_epiphany_setup_finish (GsPlugin      *plugin,
                                 GAsyncResult  *result,
                                 GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void shutdown_cb (GObject      *source_object,
                         GAsyncResult *result,
                         gpointer      user_data);

static void
gs_plugin_epiphany_shutdown_async (GsPlugin            *plugin,
                                   GCancellable        *cancellable,
                                   GAsyncReadyCallback  callback,
                                   gpointer             user_data)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (plugin);
	g_autoptr(GTask) task = NULL;

	task = g_task_new (self, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_epiphany_shutdown_async);

	/* Stop the worker thread. */
	gs_worker_thread_shutdown_async (self->worker, cancellable, shutdown_cb, g_steal_pointer (&task));
}

static void
shutdown_cb (GObject      *source_object,
             GAsyncResult *result,
             gpointer      user_data)
{
	g_autoptr(GTask) task = G_TASK (user_data);
	GsPluginEpiphany *self = g_task_get_source_object (task);
	g_autoptr(GsWorkerThread) worker = NULL;
	g_autoptr(GError) local_error = NULL;

	worker = g_steal_pointer (&self->worker);

	if (!gs_worker_thread_shutdown_finish (worker, result, &local_error)) {
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_epiphany_shutdown_finish (GsPlugin      *plugin,
                                    GAsyncResult  *result,
                                    GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void
gs_plugin_epiphany_init (GsPluginEpiphany *self)
{
	/* Re-used permissions by all GsApp instances; do not modify it out
	   of this place. */
	self->permissions = gs_app_permissions_new ();
	gs_app_permissions_set_flags (self->permissions, GS_APP_PERMISSIONS_FLAGS_NETWORK);
	gs_app_permissions_seal (self->permissions);

	/* set name of MetaInfo file */
	gs_plugin_set_appstream_id (GS_PLUGIN (self), "org.gnome.Software.Plugin.Epiphany");

	/* need help from appstream */
	gs_plugin_add_rule (GS_PLUGIN (self), GS_PLUGIN_RULE_RUN_AFTER, "appstream");

	/* prioritize over packages */
	gs_plugin_add_rule (GS_PLUGIN (self), GS_PLUGIN_RULE_BETTER_THAN, "packagekit");
}

static void
gs_plugin_epiphany_dispose (GObject *object)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (object);

	if (self->changed_id > 0) {
		g_signal_handler_disconnect (self->monitor, self->changed_id);
		self->changed_id = 0;
	}

	g_clear_object (&self->epiphany_proxy);
	g_clear_object (&self->launcher_portal_proxy);
	g_clear_object (&self->monitor);
	g_clear_object (&self->worker);
	g_clear_pointer (&self->url_id_map, g_hash_table_unref);

	G_OBJECT_CLASS (gs_plugin_epiphany_parent_class)->dispose (object);
}

static void
gs_plugin_epiphany_finalize (GObject *object)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (object);

	g_mutex_clear (&self->installed_apps_mutex);
	g_clear_object (&self->permissions);

	G_OBJECT_CLASS (gs_plugin_epiphany_parent_class)->finalize (object);
}

static gboolean ensure_installed_apps_cache (GsPluginEpiphany  *self,
					     GCancellable      *cancellable,
					     GError           **error);

/* Run in @worker. The caller must have already done ensure_installed_apps_cache() */
static void
gs_epiphany_refine_app_state (GsPlugin *plugin,
			      GsApp    *app)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (plugin);

	assert_in_worker (self);

	if (gs_app_get_state (app) == GS_APP_STATE_UNKNOWN) {
		g_autoptr(GsApp) cached_app = NULL;
		const char *appstream_source;

		/* If we have a cached app, set the state from there. Otherwise
		 * only set the state to available if the app came from
		 * appstream data, because there's no way to re-install an app
		 * in Software that was originally installed from Epiphany,
		 * unless we have appstream metainfo for it.
		 */
		cached_app = gs_plugin_cache_lookup (plugin, gs_app_get_id (app));
		appstream_source = gs_app_get_metadata_item (app, "appstream::source-file");
		if (cached_app)
			gs_app_set_state (app, gs_app_get_state (cached_app));
		else if (appstream_source)
			gs_app_set_state (app, GS_APP_STATE_AVAILABLE);
		else {
			gs_app_set_state (app, GS_APP_STATE_UNAVAILABLE);
			gs_app_set_url_missing (app,
						"https://gitlab.gnome.org/GNOME/gnome-software/-/wikis/How-to-reinstall-a-web-app");
		}
	}
}

void
gs_plugin_adopt_app (GsPlugin *plugin,
		     GsApp    *app)
{
	if (gs_app_get_kind (app) == AS_COMPONENT_KIND_WEB_APP &&
	    gs_app_get_bundle_kind (app) != AS_BUNDLE_KIND_PACKAGE) {
		gs_app_set_management_plugin (app, plugin);
	}
}

static gint
get_priority_for_interactivity (gboolean interactive)
{
	return interactive ? G_PRIORITY_DEFAULT : G_PRIORITY_LOW;
}

static void list_apps_thread_cb (GTask        *task,
                                 gpointer      source_object,
                                 gpointer      task_data,
                                 GCancellable *cancellable);

static void
gs_plugin_epiphany_list_apps_async (GsPlugin              *plugin,
                                    GsAppQuery            *query,
                                    GsPluginListAppsFlags  flags,
                                    GCancellable          *cancellable,
                                    GAsyncReadyCallback    callback,
                                    gpointer               user_data)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_LIST_APPS_FLAGS_INTERACTIVE);

	task = gs_plugin_list_apps_data_new_task (plugin, query, flags,
						  cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_epiphany_list_apps_async);

	/* Queue a job to get the apps. */
	gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
				list_apps_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker */
static void
refine_app (GsPluginEpiphany    *self,
	    GsApp               *app,
	    GsPluginRefineFlags  flags,
	    GUri                *uri,
	    const char          *url)
{
	const char *hostname;
	const char *installed_app_id;
	const struct {
		const gchar *hostname;
		const gchar *license_spdx;
	} app_licenses[] = {
	/* Keep in alphabetical order by hostname */
	{ "app.diagrams.net", "Apache-2.0" },
	{ "devdocs.io", "MPL-2.0" },
	{ "discourse.flathub.org", "GPL-2.0-or-later" },
	{ "discourse.gnome.org", "GPL-2.0-or-later" },
	{ "excalidraw.com", "MIT" },
	{ "pinafore.social", "AGPL-3.0-only" },
	{ "snapdrop.net", "GPL-3.0-only" },
	{ "stackedit.io", "Apache-2.0" },
	{ "squoosh.app", "Apache-2.0" },
	};

	g_return_if_fail (GS_IS_APP (app));
	g_return_if_fail (uri != NULL);
	g_return_if_fail (url != NULL);

	gs_app_set_origin (app, "gnome-web");
	gs_app_set_origin_ui (app, _("GNOME Web"));

	gs_app_set_scope (app, AS_COMPONENT_SCOPE_USER);
	gs_app_set_launchable (app, AS_LAUNCHABLE_KIND_URL, url);

	installed_app_id = g_hash_table_lookup (self->url_id_map, url);
	if (installed_app_id) {
		gs_app_set_launchable (app, AS_LAUNCHABLE_KIND_DESKTOP_ID, installed_app_id);
	}

	/* Hard-code the licenses as it's hard to get them programmatically. We
	 * can move them to an AppStream file if needed.
	 */
	hostname = g_uri_get_host (uri);
	if (gs_app_get_license (app) == NULL && hostname != NULL) {
		for (gsize i = 0; i < G_N_ELEMENTS (app_licenses); i++) {
			if (g_str_equal (hostname, app_licenses[i].hostname)) {
				gs_app_set_license (app, GS_APP_QUALITY_NORMAL,
						    app_licenses[i].license_spdx);
				break;
			}
		}
	}

	gs_app_set_size_download (app, GS_SIZE_TYPE_VALID, 0);

	/* Use the default permissions */
	gs_app_set_permissions (app, self->permissions);

	if (gs_app_get_url (app, AS_URL_KIND_HOMEPAGE) == NULL)
		gs_app_set_url (app, AS_URL_KIND_HOMEPAGE, url);

	/* Use the domain name (e.g. "discourse.gnome.org") as a fallback summary.
	 * FIXME: Fetch the summary from the site's webapp manifest.
	 */
	if (gs_app_get_summary (app) == NULL) {
		if (hostname != NULL && *hostname != '\0')
			gs_app_set_summary (app, GS_APP_QUALITY_LOWEST, hostname);
		else
			gs_app_set_summary (app, GS_APP_QUALITY_LOWEST, url);
	}

	if (installed_app_id == NULL)
		return;

	{
		const gchar *name;
		g_autofree char *icon_path = NULL;
		goffset desktop_size = 0, icon_size = 0;
		g_autoptr(GDesktopAppInfo) desktop_info = NULL;
		g_autoptr(GFileInfo) file_info = NULL;
		g_autoptr(GFile) icon_file = NULL;

		desktop_info = g_desktop_app_info_new (installed_app_id);

		if (desktop_info == NULL) {
			g_warning ("Couldn't get GDesktopAppInfo for app %s", installed_app_id);
			return;
		}

		name = g_app_info_get_name (G_APP_INFO (desktop_info));
		gs_app_set_name (app, GS_APP_QUALITY_NORMAL, name);

		if (flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SIZE) {
			g_autoptr(GFile) desktop_file = NULL;
			const gchar *desktop_path;
			guint64 install_date = 0;

			desktop_path = g_desktop_app_info_get_filename (desktop_info);
			g_assert (desktop_path);
			desktop_file = g_file_new_for_path (desktop_path);

			file_info = g_file_query_info (desktop_file,
						       G_FILE_ATTRIBUTE_TIME_CREATED "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
						       0, NULL, NULL);
			if (file_info) {
				install_date = g_file_info_get_attribute_uint64 (file_info, G_FILE_ATTRIBUTE_TIME_CREATED);
				desktop_size = g_file_info_get_size (file_info);
			}
			if (install_date) {
				gs_app_set_install_date (app, install_date);
			}
		}

		icon_path = g_desktop_app_info_get_string (desktop_info, "Icon");
		if (flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SIZE &&
		    icon_path) {
			icon_file = g_file_new_for_path (icon_path);

			g_clear_object (&file_info);
			file_info = g_file_query_info (icon_file,
						       G_FILE_ATTRIBUTE_STANDARD_SIZE,
						       0, NULL, NULL);
			if (file_info)
				icon_size = g_file_info_get_size (file_info);
		}
		if (flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON &&
		    gs_app_get_icons (app) == NULL &&
		    icon_path) {
			g_autoptr(GIcon) icon = g_file_icon_new (icon_file);
			g_autofree char *icon_dir = g_path_get_dirname (icon_path);
			g_autofree char *icon_dir_basename = g_path_get_basename (icon_dir);
			const char *x;
			guint64 width = 0;

			/* dir should be either scalable or e.g. 512x512 */
			if (g_strcmp0 (icon_dir_basename, "scalable") == 0) {
				/* Ensure scalable icons are preferred */
				width = 4096;
			} else if ((x = strchr (icon_dir_basename, 'x')) != NULL) {
				g_ascii_string_to_unsigned (x + 1, 10, 1, G_MAXINT, &width, NULL);
			}
			if (width > 0 && width <= 4096) {
				gs_icon_set_width (icon, width);
				gs_icon_set_height (icon, width);
			} else {
				g_warning ("Unexpectedly unable to determine width of icon %s", icon_path);
			}

			gs_app_add_icon (app, icon);
		}
		if (desktop_size > 0 || icon_size > 0) {
			gs_app_set_size_installed (app, GS_SIZE_TYPE_VALID, desktop_size + icon_size);
		}
	}
}

/* Run in @worker */
static GsApp *
gs_epiphany_create_app (GsPluginEpiphany *self,
			const char       *id)
{
	g_autoptr(GsApp) app = NULL;

	assert_in_worker (self);

	app = gs_plugin_cache_lookup (GS_PLUGIN (self), id);
	if (app != NULL)
		return g_steal_pointer (&app);

	app = gs_app_new (id);
	gs_app_set_management_plugin (app, GS_PLUGIN (self));
	gs_app_set_kind (app, AS_COMPONENT_KIND_WEB_APP);
	gs_app_set_metadata (app, "GnomeSoftware::Creator",
			     gs_plugin_get_name (GS_PLUGIN (self)));

	gs_plugin_cache_add (GS_PLUGIN (self), id, app);
	return g_steal_pointer (&app);
}

static gchar * /* (transfer full) */
generate_app_id_for_url (const gchar *url)
{
	/* Generate the app ID used in the AppStream data using the
	 * same method as pwa-metainfo-generator.py in
	 * https://gitlab.gnome.org/mwleeds/gnome-pwa-list
	 * Using this app ID rather than the one provided by Epiphany
	 * makes it possible for the appstream plugin to refine the
	 * GsApp we create (see the comment at the top of this file).
	 */
	g_autofree gchar *url_hash = g_compute_checksum_for_string (G_CHECKSUM_SHA1, url, -1);
	return g_strconcat ("org.gnome.Software.WebApp_", url_hash, ".desktop", NULL);
}

/* Run in @worker */
static gboolean
ensure_installed_apps_cache (GsPluginEpiphany  *self,
			     GCancellable      *cancellable,
			     GError           **error)
{
	g_autoptr(GVariant) webapps_v = NULL;
	g_auto(GStrv) webapps = NULL;
	guint n_webapps;
	g_autoptr(GsAppList) installed_cache = gs_app_list_new ();
	g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&self->installed_apps_mutex);

	assert_in_worker (self);

	if (self->installed_apps_cached)
		return TRUE;

	if (!gs_ephy_web_app_provider_call_get_installed_apps_sync (self->epiphany_proxy,
								    &webapps,
								    cancellable,
								    error)) {
		gs_epiphany_error_convert (error);
		return FALSE;
	}

	n_webapps = g_strv_length (webapps);
	g_debug ("%s: epiphany-webapp-provider returned %u installed web apps", G_STRFUNC, n_webapps);
	for (guint i = 0; i < n_webapps; i++) {
		const gchar *desktop_file_id = webapps[i];
		const gchar *url = NULL;
		g_autofree char *metainfo_app_id = NULL;
		const gchar *exec;
		int argc;
		GsPluginRefineFlags refine_flags;
		g_auto(GStrv) argv = NULL;
		g_autoptr(GsApp) app = NULL;
		g_autoptr(GDesktopAppInfo) desktop_info = NULL;
		g_autoptr(GUri) uri = NULL;

		g_debug ("%s: Working on installed web app %s", G_STRFUNC, desktop_file_id);

		desktop_info = g_desktop_app_info_new (desktop_file_id);

		if (desktop_info == NULL) {
			g_warning ("Epiphany returned a non-existent or invalid desktop ID %s", desktop_file_id);
			continue;
		}

		/* This way of getting the URL is a bit hacky but it's what
		 * Epiphany does, specifically in
		 * ephy_web_application_for_profile_directory() which lives in
		 * https://gitlab.gnome.org/GNOME/epiphany/-/blob/master/lib/ephy-web-app-utils.c
		 */
		exec = g_app_info_get_commandline (G_APP_INFO (desktop_info));
		if (g_shell_parse_argv (exec, &argc, &argv, NULL)) {
			g_assert (argc > 0);
			url = argv[argc - 1];
		}
		if (!url || !(uri = g_uri_parse (url, G_URI_FLAGS_NONE, NULL))) {
			g_warning ("Failed to parse URL for web app %s: %s",
				   desktop_file_id, url ? url : "(null)");
			continue;
		}

		/* Store the installed app id for use in refine_app() */
		g_hash_table_insert (self->url_id_map, g_strdup (url),
				     g_strdup (desktop_file_id));

		metainfo_app_id = generate_app_id_for_url (url);
		g_debug ("Creating GsApp for webapp with URL %s using app ID %s (desktop file id: %s)",
			 url, metainfo_app_id, desktop_file_id);

		/* App gets added to the plugin cache here */
		app = gs_epiphany_create_app (self, metainfo_app_id);

		gs_app_set_state (app, GS_APP_STATE_INSTALLED);

		refine_flags = GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON |
			       GS_PLUGIN_REFINE_FLAGS_REQUIRE_SIZE |
			       GS_PLUGIN_REFINE_FLAGS_REQUIRE_ID;
		refine_app (self, app, refine_flags, uri, url);
	}

	/* Update the state on any apps that were uninstalled outside
	 * gnome-software
	 */
	gs_plugin_cache_lookup_by_state (GS_PLUGIN (self), installed_cache, GS_APP_STATE_INSTALLED);
	for (guint i = 0; i < gs_app_list_length (installed_cache); i++) {
		GsApp *app = gs_app_list_index (installed_cache, i);
		const char *installed_app_id;
		const char *appstream_source;

		installed_app_id = gs_app_get_launchable (app, AS_LAUNCHABLE_KIND_DESKTOP_ID);
		if (installed_app_id == NULL) {
			g_warning ("Installed app unexpectedly has no desktop id: %s", gs_app_get_id (app));
			continue;
		}

		if (g_strv_contains ((const char * const *)webapps, installed_app_id))
			continue;

		gs_plugin_cache_remove (GS_PLUGIN (self), gs_app_get_id (app));

		appstream_source = gs_app_get_metadata_item (app, "appstream::source-file");
		if (appstream_source)
			gs_app_set_state (app, GS_APP_STATE_AVAILABLE);
		else
			gs_app_set_state (app, GS_APP_STATE_UNKNOWN);
	}

	self->installed_apps_cached = TRUE;
	return TRUE;
}

/* Run in @worker */
static void
list_apps_thread_cb (GTask        *task,
                     gpointer      source_object,
                     gpointer      task_data,
                     GCancellable *cancellable)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (source_object);
	g_autoptr(GsAppList) list = gs_app_list_new ();
	GsPluginListAppsData *data = task_data;
	GsAppQueryTristate is_installed = GS_APP_QUERY_TRISTATE_UNSET;
	const gchar * const *keywords = NULL;
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	if (data->query != NULL) {
		is_installed = gs_app_query_get_is_installed (data->query);
		keywords = gs_app_query_get_keywords (data->query);
	}

	/* Currently only support a subset of query properties, and only one set at once.
	 * Also don’t currently support GS_APP_QUERY_TRISTATE_FALSE. */
	if ((is_installed == GS_APP_QUERY_TRISTATE_UNSET &&
	     keywords == NULL) ||
	    is_installed == GS_APP_QUERY_TRISTATE_FALSE ||
	    gs_app_query_get_n_properties_set (data->query) != 1) {
		g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
					 "Unsupported query");
		return;
	}

	/* Ensure the cache is up to date. */
	if (!ensure_installed_apps_cache (self, cancellable, &local_error)) {
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	if (is_installed == GS_APP_QUERY_TRISTATE_TRUE)
		gs_plugin_cache_lookup_by_state (GS_PLUGIN (self), list, GS_APP_STATE_INSTALLED);
	else if (keywords != NULL) {
		for (gsize i = 0; keywords[i]; i++) {
			GHashTableIter iter;
			gpointer key, value;
			g_hash_table_iter_init (&iter, self->url_id_map);
			while (g_hash_table_iter_next (&iter, &key, &value)) {
				const gchar *url = key;
				const gchar *app_id = value;
				if (g_strcmp0 (app_id, keywords[i]) == 0) {
					g_autoptr(GsApp) app = NULL;
					g_autofree gchar *metainfo_app_id = NULL;
					metainfo_app_id = generate_app_id_for_url (url);
					app = gs_plugin_cache_lookup (GS_PLUGIN (self), metainfo_app_id);
					if (app != NULL)
						gs_app_list_add (list, app);
					break;
				}
			}
		}
	}

	g_task_return_pointer (task, g_steal_pointer (&list), g_object_unref);
}

static GsAppList *
gs_plugin_epiphany_list_apps_finish (GsPlugin      *plugin,
                                     GAsyncResult  *result,
                                     GError       **error)
{
	g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) == gs_plugin_epiphany_list_apps_async, FALSE);
	return g_task_propagate_pointer (G_TASK (result), error);
}

static void
gs_epiphany_refine_app (GsPluginEpiphany    *self,
			GsApp               *app,
			GsPluginRefineFlags  refine_flags,
			const char          *url)
{
	g_autoptr(GUri) uri = NULL;

	g_return_if_fail (url != NULL && *url != '\0');

	if (!(uri = g_uri_parse (url, G_URI_FLAGS_NONE, NULL))) {
		g_warning ("Failed to parse URL for web app %s: %s", gs_app_get_id (app), url);
		return;
	}

	refine_app (self, app, refine_flags, uri, url);
}

static void refine_thread_cb (GTask        *task,
                              gpointer      source_object,
                              gpointer      task_data,
                              GCancellable *cancellable);

static void
gs_plugin_epiphany_refine_async (GsPlugin            *plugin,
                                 GsAppList           *list,
                                 GsPluginRefineFlags  flags,
                                 GCancellable        *cancellable,
                                 GAsyncReadyCallback  callback,
                                 gpointer             user_data)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = gs_plugin_has_flags (GS_PLUGIN (self), GS_PLUGIN_FLAGS_INTERACTIVE);

	task = gs_plugin_refine_data_new_task (plugin, list, flags, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_epiphany_refine_async);

	/* Queue a job for the refine. */
	gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
				refine_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
refine_thread_cb (GTask        *task,
                  gpointer      source_object,
                  gpointer      task_data,
                  GCancellable *cancellable)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (source_object);
	GsPluginRefineData *data = task_data;
	GsPluginRefineFlags flags = data->flags;
	GsAppList *list = data->list;
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	if (!ensure_installed_apps_cache (self, cancellable, &local_error)) {
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	for (guint i = 0; i < gs_app_list_length (list); i++) {
		GsApp *app = gs_app_list_index (list, i);
		const char *url;

		/* not us */
		if (gs_app_get_kind (app) != AS_COMPONENT_KIND_WEB_APP ||
		    gs_app_get_bundle_kind (app) == AS_BUNDLE_KIND_PACKAGE)
			continue;

		url = gs_app_get_launchable (app, AS_LAUNCHABLE_KIND_URL);
		if (url == NULL || *url == '\0') {
			/* A launchable URL is required by the AppStream spec */
			g_warning ("Web app %s missing launchable url", gs_app_get_id (app));
			continue;
		}

		g_debug ("epiphany: refining app %s", gs_app_get_id (app));
		gs_epiphany_refine_app (self, app, flags, url);
		gs_epiphany_refine_app_state (GS_PLUGIN (self), app);

		/* Usually the way to refine wildcard apps is to create a new
		 * GsApp and add it to the results list, but in this case we
		 * need to use the app that was refined by the appstream plugin
		 * as it has all the metadata set already, and this is the only
		 * plugin for dealing with web apps, so it should be safe to
		 * adopt the wildcard app.
		 */
		if (gs_app_has_quirk (app, GS_APP_QUIRK_IS_WILDCARD)) {
			gs_app_remove_quirk (app, GS_APP_QUIRK_IS_WILDCARD);
			gs_app_set_management_plugin (app, GS_PLUGIN (self));
			gs_plugin_cache_add (GS_PLUGIN (self), gs_app_get_id (app), app);
		}
	}

	/* success */
	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_epiphany_refine_finish (GsPlugin      *plugin,
                                  GAsyncResult  *result,
                                  GError       **error)
{
	g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) == gs_plugin_epiphany_refine_async, FALSE);
	return g_task_propagate_boolean (G_TASK (result), error);
}

static GVariant *
get_serialized_icon (GsApp *app,
		     GIcon *icon)
{
	g_autofree char *icon_path = NULL;
	g_autoptr(GInputStream) stream = NULL;
	g_autoptr(GBytes) bytes = NULL;
	g_autoptr(GIcon) bytes_icon = NULL;
	g_autoptr(GVariant) icon_v = NULL;
	guint icon_width;

	/* Note: GsRemoteIcon will work on this GFileIcon code path.
	 * The icons plugin should have called
	 * gs_app_ensure_icons_downloaded() for us.
	 */
	if (!G_IS_FILE_ICON (icon))
		return NULL;

	icon_path = g_file_get_path (g_file_icon_get_file (G_FILE_ICON (icon)));
	if (!g_str_has_suffix (icon_path, ".png") &&
	    !g_str_has_suffix (icon_path, ".svg") &&
	    !g_str_has_suffix (icon_path, ".jpeg") &&
	    !g_str_has_suffix (icon_path, ".jpg")) {
		g_warning ("Icon for app %s has unsupported file extension: %s",
			   gs_app_get_id (app), icon_path);
		return NULL;
	}

	/* Scale down to the portal's size limit if needed */
	icon_width = gs_icon_get_width (icon);
	if (icon_width > 512)
		icon_width = 512;

	/* Serialize the icon as a #GBytesIcon since that's
	 * what the dynamic launcher portal requires.
	 */
	stream = g_loadable_icon_load (G_LOADABLE_ICON (icon), icon_width, NULL, NULL, NULL);

	/* Icons are usually smaller than 1 MiB. Set a 10 MiB
	 * limit so we can't use a huge amount of memory or hit
	 * the D-Bus message size limit
	 */
	if (stream)
		bytes = g_input_stream_read_bytes (stream, 10485760 /* 10 MiB */, NULL, NULL);
	if (bytes)
		bytes_icon = g_bytes_icon_new (bytes);
	if (bytes_icon)
		icon_v = g_icon_serialize (bytes_icon);

	return g_steal_pointer (&icon_v);
}

gboolean
gs_plugin_app_install (GsPlugin      *plugin,
		       GsApp         *app,
		       GCancellable  *cancellable,
		       GError       **error)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (plugin);
	const char *url;
	const char *name;
	const char *token = NULL;
	g_autofree char *installed_app_id = NULL;
	g_autoptr(GVariant) token_v = NULL;
	g_autoptr(GVariant) icon_v = NULL;
	GVariantBuilder opt_builder;
	const int icon_sizes[] = {512, 192, 128, 1};

	if (!gs_app_has_management_plugin (app, plugin))
		return TRUE;

	url = gs_app_get_url (app, AS_URL_KIND_HOMEPAGE);
	if (url == NULL || *url == '\0') {
		g_set_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
			     "Can't install web app %s without url",
			     gs_app_get_id (app));
		return FALSE;
	}
	name = gs_app_get_name (app);
	if (name == NULL || *name == '\0') {
		g_set_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
			     "Can't install web app %s without name",
			     gs_app_get_id (app));
		return FALSE;
	}
	for (guint i = 0; i < G_N_ELEMENTS (icon_sizes); i++) {
		GIcon *icon = gs_app_get_icon_for_size (app, icon_sizes[i], 1, NULL);
		if (icon != NULL)
			icon_v = get_serialized_icon (app, icon);
		if (icon_v != NULL)
			break;
	}
	if (icon_v == NULL) {
		g_set_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
			     "Can't install web app %s without icon",
			     gs_app_get_id (app));
		return FALSE;
	}

	gs_app_set_state (app, GS_APP_STATE_INSTALLING);
	/* First get a token from xdg-desktop-portal so Epiphany can do the
	 * installation without user confirmation
	 */
	g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
	token_v = g_dbus_proxy_call_sync (self->launcher_portal_proxy,
					  "RequestInstallToken",
					  g_variant_new ("(sva{sv})",
							 name, icon_v, &opt_builder),
					  G_DBUS_CALL_FLAGS_NONE,
					  -1, cancellable, error);
	if (token_v == NULL) {
		gs_epiphany_error_convert (error);
		gs_app_set_state_recover (app);
		return FALSE;
	}

	/* Then pass the token to Epiphany which will use xdg-desktop-portal to
	 * complete the installation
	 */
	g_variant_get (token_v, "(&s)", &token);
	if (!gs_ephy_web_app_provider_call_install_sync (self->epiphany_proxy,
							 url, name, token,
							 &installed_app_id,
							 cancellable,
							 error)) {
		gs_epiphany_error_convert (error);
		gs_app_set_state_recover (app);
		return FALSE;
	}

	{
		g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&self->installed_apps_mutex);
		g_hash_table_insert (self->url_id_map, g_strdup (url),
				     g_strdup (installed_app_id));
	}

	gs_app_set_launchable (app, AS_LAUNCHABLE_KIND_DESKTOP_ID, installed_app_id);
	gs_app_set_state (app, GS_APP_STATE_INSTALLED);

	return TRUE;
}

gboolean
gs_plugin_app_remove (GsPlugin      *plugin,
		      GsApp         *app,
		      GCancellable  *cancellable,
		      GError       **error)
{
	GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (plugin);
	const char *installed_app_id;
	const char *url;

	if (!gs_app_has_management_plugin (app, plugin))
		return TRUE;

	installed_app_id = gs_app_get_launchable (app, AS_LAUNCHABLE_KIND_DESKTOP_ID);
	if (installed_app_id == NULL) {
		g_set_error_literal (error,
				     GS_PLUGIN_ERROR,
				     GS_PLUGIN_ERROR_NOT_SUPPORTED,
				     "App can't be uninstalled without installed app ID");
		gs_app_set_state_recover (app);
		return FALSE;
	}

	gs_app_set_state (app, GS_APP_STATE_REMOVING);
	if (!gs_ephy_web_app_provider_call_uninstall_sync (self->epiphany_proxy,
							   installed_app_id,
							   cancellable,
							   error)) {
		gs_epiphany_error_convert (error);
		gs_app_set_state_recover (app);
		return FALSE;
	}

	url = gs_app_get_launchable (app, AS_LAUNCHABLE_KIND_URL);
	if (url != NULL && *url != '\0') {
		g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&self->installed_apps_mutex);
		g_hash_table_remove (self->url_id_map, url);
	}

	/* The app is not necessarily available; it may have been installed
	 * directly in Epiphany
	 */
	gs_app_set_state (app, GS_APP_STATE_UNKNOWN);

	return TRUE;
}

gboolean
gs_plugin_launch (GsPlugin      *plugin,
		  GsApp         *app,
		  GCancellable  *cancellable,
		  GError       **error)
{
	if (!gs_app_has_management_plugin (app, plugin))
		return TRUE;

	return gs_plugin_app_launch (plugin, app, error);
}

static void
gs_plugin_epiphany_class_init (GsPluginEpiphanyClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GsPluginClass *plugin_class = GS_PLUGIN_CLASS (klass);

	object_class->dispose = gs_plugin_epiphany_dispose;
	object_class->finalize = gs_plugin_epiphany_finalize;

	plugin_class->setup_async = gs_plugin_epiphany_setup_async;
	plugin_class->setup_finish = gs_plugin_epiphany_setup_finish;
	plugin_class->shutdown_async = gs_plugin_epiphany_shutdown_async;
	plugin_class->shutdown_finish = gs_plugin_epiphany_shutdown_finish;
	plugin_class->refine_async = gs_plugin_epiphany_refine_async;
	plugin_class->refine_finish = gs_plugin_epiphany_refine_finish;
	plugin_class->list_apps_async = gs_plugin_epiphany_list_apps_async;
	plugin_class->list_apps_finish = gs_plugin_epiphany_list_apps_finish;
}

GType
gs_plugin_query_type (void)
{
	return GS_TYPE_PLUGIN_EPIPHANY;
}