GApplication: Add a way to replace a unique instance

While uniqueness is great, sometimes you want to restart
a newer version of the same app. These two flags make that
possible.

We also add a ::name-lost signal, that is emitted when it
happens. The default handler for this signal just calls
g_application_quit(), but applications may want to connect
and do cleanup or state-saving here.
This commit is contained in:
Matthias Clasen 2018-08-18 15:35:33 -04:00
parent 051c9ada8e
commit 3a4b18f903
4 changed files with 117 additions and 7 deletions

View File

@ -212,6 +212,7 @@
* the @dbus_register vfunc. Since: 2.34 * the @dbus_register vfunc. Since: 2.34
* @handle_local_options: invoked locally after the parsing of the commandline * @handle_local_options: invoked locally after the parsing of the commandline
* options has occurred. Since: 2.40 * options has occurred. Since: 2.40
* @name_lost: invoked when another instance is taking over the name. Since: 2.60
* *
* Virtual function table for #GApplication. * Virtual function table for #GApplication.
* *
@ -277,6 +278,7 @@ enum
SIGNAL_ACTION, SIGNAL_ACTION,
SIGNAL_COMMAND_LINE, SIGNAL_COMMAND_LINE,
SIGNAL_HANDLE_LOCAL_OPTIONS, SIGNAL_HANDLE_LOCAL_OPTIONS,
SIGNAL_NAME_LOST,
NR_SIGNALS NR_SIGNALS
}; };
@ -476,6 +478,7 @@ g_application_parse_command_line (GApplication *application,
{ {
gboolean become_service = FALSE; gboolean become_service = FALSE;
gchar *app_id = NULL; gchar *app_id = NULL;
gboolean replace = FALSE;
GVariantDict *dict = NULL; GVariantDict *dict = NULL;
GOptionContext *context; GOptionContext *context;
GOptionGroup *gapplication_group; GOptionGroup *gapplication_group;
@ -557,6 +560,18 @@ g_application_parse_command_line (GApplication *application,
g_option_group_add_entries (gapplication_group, entries); g_option_group_add_entries (gapplication_group, entries);
} }
/* Allow replacing if the application allows it */
if (application->priv->flags & G_APPLICATION_ALLOW_REPLACEMENT)
{
GOptionEntry entries[] = {
{ "gapplication-replace", '\0', 0, G_OPTION_ARG_NONE, &replace,
N_("Replace the running instance") },
{ NULL }
};
g_option_group_add_entries (gapplication_group, entries);
}
/* Now we parse... */ /* Now we parse... */
if (!g_option_context_parse_strv (context, arguments, error)) if (!g_option_context_parse_strv (context, arguments, error))
goto out; goto out;
@ -569,6 +584,10 @@ g_application_parse_command_line (GApplication *application,
if (app_id) if (app_id)
g_application_set_application_id (application, app_id); g_application_set_application_id (application, app_id);
/* Check for --gapplication-replace */
if (replace)
application->priv->flags |= G_APPLICATION_REPLACE;
dict = g_variant_dict_new (NULL); dict = g_variant_dict_new (NULL);
if (application->priv->packed_options) if (application->priv->packed_options)
{ {
@ -1177,6 +1196,13 @@ g_application_real_dbus_unregister (GApplication *application,
{ {
} }
static gboolean
g_application_real_name_lost (GApplication *application)
{
g_application_quit (application);
return TRUE;
}
/* GObject implementation stuff {{{1 */ /* GObject implementation stuff {{{1 */
static void static void
g_application_set_property (GObject *object, g_application_set_property (GObject *object,
@ -1434,6 +1460,7 @@ g_application_class_init (GApplicationClass *class)
class->add_platform_data = g_application_real_add_platform_data; class->add_platform_data = g_application_real_add_platform_data;
class->dbus_register = g_application_real_dbus_register; class->dbus_register = g_application_real_dbus_register;
class->dbus_unregister = g_application_real_dbus_unregister; class->dbus_unregister = g_application_real_dbus_unregister;
class->name_lost = g_application_real_name_lost;
g_object_class_install_property (object_class, PROP_APPLICATION_ID, g_object_class_install_property (object_class, PROP_APPLICATION_ID,
g_param_spec_string ("application-id", g_param_spec_string ("application-id",
@ -1628,6 +1655,25 @@ g_application_class_init (GApplicationClass *class)
g_application_handle_local_options_accumulator, NULL, NULL, g_application_handle_local_options_accumulator, NULL, NULL,
G_TYPE_INT, 1, G_TYPE_VARIANT_DICT); G_TYPE_INT, 1, G_TYPE_VARIANT_DICT);
/**
* GApplication::name-lost:
* @application: the application
*
* The ::name-lost signal is emitted only on the registered primary instance
* when a new instance has taken over. This can only happen if the application
* is using the %G_APPLICATION_ALLOW_REPLACEMENT flag.
*
* The default handler for this signal calls g_application_quit().
*
* Returns: %TRUE if the signal has been handled
*
* Since: 2.60
*/
g_application_signals[SIGNAL_NAME_LOST] =
g_signal_new (I_("name-lost"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GApplicationClass, name_lost),
g_signal_accumulator_true_handled, NULL, NULL,
G_TYPE_BOOLEAN, 0);
} }
/* Application ID validity {{{1 */ /* Application ID validity {{{1 */

View File

@ -112,9 +112,10 @@ struct _GApplicationClass
const gchar *object_path); const gchar *object_path);
gint (* handle_local_options)(GApplication *application, gint (* handle_local_options)(GApplication *application,
GVariantDict *options); GVariantDict *options);
gboolean (* name_lost) (GApplication *application);
/*< private >*/ /*< private >*/
gpointer padding[8]; gpointer padding[7];
}; };
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL

View File

@ -111,6 +111,7 @@ struct _GApplicationImpl
GDBusConnection *session_bus; GDBusConnection *session_bus;
GActionGroup *exported_actions; GActionGroup *exported_actions;
const gchar *bus_name; const gchar *bus_name;
guint name_lost_signal;
gchar *object_path; gchar *object_path;
guint object_id; guint object_id;
@ -327,6 +328,25 @@ application_path_from_appid (const gchar *appid)
return appid_path; return appid_path;
} }
static void g_application_impl_stop_primary (GApplicationImpl *impl);
static void
name_lost (GDBusConnection *bus,
const char *sender_name,
const char *object_path,
const char *interface_name,
const char *signal_name,
GVariant *parameters,
gpointer user_data)
{
GApplicationImpl *impl = user_data;
gboolean handled;
impl->primary = FALSE;
g_application_impl_stop_primary (impl);
g_signal_emit_by_name (impl->app, "name-lost", &handled);
}
/* Attempt to become the primary instance. /* Attempt to become the primary instance.
* *
* Returns %TRUE if everything went OK, regardless of if we became the * Returns %TRUE if everything went OK, regardless of if we became the
@ -347,6 +367,8 @@ g_application_impl_attempt_primary (GApplicationImpl *impl,
NULL /* set_property */ NULL /* set_property */
}; };
GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app); GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
GBusNameOwnerFlags name_owner_flags;
GApplicationFlags app_flags;
GVariant *reply; GVariant *reply;
guint32 rval; guint32 rval;
@ -426,11 +448,33 @@ g_application_impl_attempt_primary (GApplicationImpl *impl,
* the well-known name and fall back to remote mode (!is_primary) * the well-known name and fall back to remote mode (!is_primary)
* in the case that we can't do that. * in the case that we can't do that.
*/ */
reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", name_owner_flags = G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE;
"org.freedesktop.DBus", "RequestName", app_flags = g_application_get_flags (impl->app);
g_variant_new ("(su)",
if (app_flags & G_APPLICATION_ALLOW_REPLACEMENT)
{
impl->name_lost_signal = g_dbus_connection_signal_subscribe (impl->session_bus,
"org.freedesktop.DBus",
"org.freedesktop.DBus",
"NameLost",
"/org/freedesktop/DBus",
impl->bus_name, impl->bus_name,
G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE), G_DBUS_SIGNAL_FLAGS_NONE,
name_lost,
impl,
NULL);
name_owner_flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
}
if (app_flags & G_APPLICATION_REPLACE)
name_owner_flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
reply = g_dbus_connection_call_sync (impl->session_bus,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"RequestName",
g_variant_new ("(su)", impl->bus_name, name_owner_flags),
G_VARIANT_TYPE ("(u)"), G_VARIANT_TYPE ("(u)"),
0, -1, cancellable, error); 0, -1, cancellable, error);
@ -443,6 +487,12 @@ g_application_impl_attempt_primary (GApplicationImpl *impl,
/* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */ /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
impl->primary = (rval != 3); impl->primary = (rval != 3);
if (!impl->primary && impl->name_lost_signal)
{
g_dbus_connection_signal_unsubscribe (impl->session_bus, impl->name_lost_signal);
impl->name_lost_signal = 0;
}
return TRUE; return TRUE;
} }
@ -485,6 +535,12 @@ g_application_impl_stop_primary (GApplicationImpl *impl)
impl->actions_id = 0; impl->actions_id = 0;
} }
if (impl->name_lost_signal)
{
g_dbus_connection_signal_unsubscribe (impl->session_bus, impl->name_lost_signal);
impl->name_lost_signal = 0;
}
if (impl->primary && impl->bus_name) if (impl->primary && impl->bus_name)
{ {
g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus", g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",

View File

@ -1474,6 +1474,11 @@ typedef enum
* @G_APPLICATION_CAN_OVERRIDE_APP_ID: Allow users to override the * @G_APPLICATION_CAN_OVERRIDE_APP_ID: Allow users to override the
* application ID from the command line with `--gapplication-app-id`. * application ID from the command line with `--gapplication-app-id`.
* Since: 2.48 * Since: 2.48
* @G_APPLICATION_ALLOW_REPLACEMENT: Allow another instance to take over
* the bus name. Since: 2.60
* @G_APPLICATION_REPLACE: Take over from another instance. This flag is
* usually set by passing `--gapplication-replace` on the commandline.
* Since: 2.60
* *
* Flags used to define the behaviour of a #GApplication. * Flags used to define the behaviour of a #GApplication.
* *
@ -1491,7 +1496,9 @@ typedef enum
G_APPLICATION_NON_UNIQUE = (1 << 5), G_APPLICATION_NON_UNIQUE = (1 << 5),
G_APPLICATION_CAN_OVERRIDE_APP_ID = (1 << 6) G_APPLICATION_CAN_OVERRIDE_APP_ID = (1 << 6),
G_APPLICATION_ALLOW_REPLACEMENT = (1 << 7),
G_APPLICATION_REPLACE = (1 << 8)
} GApplicationFlags; } GApplicationFlags;
/** /**