daemon: Implement GInitableIface in transactions

Towards having the Transaction wrapper class follow a template pattern.
This commit is contained in:
Matthew Barnes 2015-08-13 10:03:56 -04:00
parent cfc52adfea
commit 1934d631c0
5 changed files with 74 additions and 41 deletions

View File

@ -817,7 +817,11 @@ osstub_handle_pull_dir (RPMOSTreeOS *interface,
goto out; goto out;
transaction = transaction_monitor_new_transaction (os->transaction_monitor, transaction = transaction_monitor_new_transaction (os->transaction_monitor,
invocation, cancellable); invocation, cancellable,
&local_error);
if (transaction == NULL)
goto out;
data = g_slice_new0 (TaskData); data = g_slice_new0 (TaskData);
data->sysroot = g_object_ref (sysroot); data->sysroot = g_object_ref (sysroot);
@ -857,7 +861,11 @@ osstub_handle_deploy (RPMOSTreeOS *interface,
goto out; goto out;
transaction = transaction_monitor_new_transaction (os->transaction_monitor, transaction = transaction_monitor_new_transaction (os->transaction_monitor,
invocation, cancellable); invocation, cancellable,
&local_error);
if (transaction == NULL)
goto out;
data = g_slice_new0 (TaskData); data = g_slice_new0 (TaskData);
data->sysroot = g_object_ref (sysroot); data->sysroot = g_object_ref (sysroot);
@ -959,7 +967,11 @@ osstub_handle_rollback (RPMOSTreeOS *interface,
goto out; goto out;
transaction = transaction_monitor_new_transaction (os->transaction_monitor, transaction = transaction_monitor_new_transaction (os->transaction_monitor,
invocation, cancellable); invocation, cancellable,
&local_error);
if (transaction == NULL)
goto out;
data = g_slice_new0 (TaskData); data = g_slice_new0 (TaskData);
data->sysroot = g_object_ref (sysroot); data->sysroot = g_object_ref (sysroot);
@ -998,7 +1010,11 @@ osstub_handle_clear_rollback_target (RPMOSTreeOS *interface,
goto out; goto out;
transaction = transaction_monitor_new_transaction (os->transaction_monitor, transaction = transaction_monitor_new_transaction (os->transaction_monitor,
invocation, cancellable); invocation, cancellable,
&local_error);
if (transaction == NULL)
goto out;
data = g_slice_new0 (TaskData); data = g_slice_new0 (TaskData);
data->sysroot = g_object_ref (sysroot); data->sysroot = g_object_ref (sysroot);

View File

@ -184,7 +184,8 @@ transaction_monitor_new (void)
RPMOSTreeTransaction * RPMOSTreeTransaction *
transaction_monitor_new_transaction (TransactionMonitor *monitor, transaction_monitor_new_transaction (TransactionMonitor *monitor,
GDBusMethodInvocation *invocation, GDBusMethodInvocation *invocation,
GCancellable *method_cancellable) GCancellable *cancellable,
GError **error)
{ {
RPMOSTreeTransaction *transaction; RPMOSTreeTransaction *transaction;
const char *object_path; const char *object_path;
@ -193,7 +194,10 @@ transaction_monitor_new_transaction (TransactionMonitor *monitor,
g_return_val_if_fail (IS_TRANSACTION_MONITOR (monitor), NULL); g_return_val_if_fail (IS_TRANSACTION_MONITOR (monitor), NULL);
g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL); g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
transaction = transaction_new (invocation, method_cancellable); transaction = transaction_new (invocation, cancellable, error);
if (transaction == NULL)
goto out;
g_signal_connect_object (transaction, "notify::active", g_signal_connect_object (transaction, "notify::active",
G_CALLBACK (transaction_monitor_notify_active_cb), G_CALLBACK (transaction_monitor_notify_active_cb),
@ -219,6 +223,7 @@ transaction_monitor_new_transaction (TransactionMonitor *monitor,
g_queue_push_head (monitor->transactions, g_object_ref (transaction)); g_queue_push_head (monitor->transactions, g_object_ref (transaction));
g_object_notify (G_OBJECT (monitor), "active-transaction"); g_object_notify (G_OBJECT (monitor), "active-transaction");
out:
return transaction; return transaction;
} }

View File

@ -32,7 +32,8 @@ RPMOSTreeTransaction *
transaction_monitor_new_transaction transaction_monitor_new_transaction
(TransactionMonitor *monitor, (TransactionMonitor *monitor,
GDBusMethodInvocation *invocation, GDBusMethodInvocation *invocation,
GCancellable *method_cancellable); GCancellable *cancellable,
GError **error);
RPMOSTreeTransaction * RPMOSTreeTransaction *
transaction_monitor_ref_active_transaction transaction_monitor_ref_active_transaction
(TransactionMonitor *monitor); (TransactionMonitor *monitor);

View File

@ -29,7 +29,7 @@ typedef struct _TransactionClass TransactionClass;
struct _Transaction struct _Transaction
{ {
RPMOSTreeTransactionSkeleton parent; RPMOSTreeTransactionSkeleton parent;
GCancellable *method_cancellable; GCancellable *cancellable;
gboolean success; gboolean success;
char *message; char *message;
@ -43,8 +43,7 @@ struct _TransactionClass
}; };
enum { enum {
PROP_0, PROP_0
PROP_CANCELLABLE
}; };
enum { enum {
@ -56,12 +55,15 @@ enum {
static guint signals[LAST_SIGNAL]; static guint signals[LAST_SIGNAL];
static void transaction_iface_init (RPMOSTreeTransactionIface *iface); static void transaction_initable_iface_init (GInitableIface *iface);
static void transaction_dbus_iface_init (RPMOSTreeTransactionIface *iface);
G_DEFINE_TYPE_WITH_CODE (Transaction, transaction, G_DEFINE_TYPE_WITH_CODE (Transaction, transaction,
RPMOSTREE_TYPE_TRANSACTION_SKELETON, RPMOSTREE_TYPE_TRANSACTION_SKELETON,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
transaction_initable_iface_init)
G_IMPLEMENT_INTERFACE (RPMOSTREE_TYPE_TRANSACTION, G_IMPLEMENT_INTERFACE (RPMOSTREE_TYPE_TRANSACTION,
transaction_iface_init)) transaction_dbus_iface_init))
static gboolean static gboolean
transaction_check_sender_is_owner (RPMOSTreeTransaction *transaction, transaction_check_sender_is_owner (RPMOSTreeTransaction *transaction,
@ -215,9 +217,6 @@ transaction_set_property (GObject *object,
switch (property_id) switch (property_id)
{ {
case PROP_CANCELLABLE:
transaction->method_cancellable = g_value_dup_object (value);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break; break;
@ -234,9 +233,6 @@ transaction_get_property (GObject *object,
switch (property_id) switch (property_id)
{ {
case PROP_CANCELLABLE:
g_value_set_object (value, transaction->method_cancellable);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break; break;
@ -248,7 +244,7 @@ transaction_dispose (GObject *object)
{ {
Transaction *transaction = TRANSACTION (object); Transaction *transaction = TRANSACTION (object);
g_clear_object (&transaction->method_cancellable); g_clear_object (&transaction->cancellable);
G_OBJECT_CLASS (transaction_parent_class)->dispose (object); G_OBJECT_CLASS (transaction_parent_class)->dispose (object);
} }
@ -266,13 +262,26 @@ transaction_finalize (GObject *object)
G_OBJECT_CLASS (transaction_parent_class)->finalize (object); G_OBJECT_CLASS (transaction_parent_class)->finalize (object);
} }
static gboolean
transaction_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
Transaction *transaction = TRANSACTION (initable);
if (G_IS_CANCELLABLE (cancellable))
transaction->cancellable = g_object_ref (cancellable);
return TRUE;
}
static gboolean static gboolean
transaction_handle_cancel (RPMOSTreeTransaction *transaction, transaction_handle_cancel (RPMOSTreeTransaction *transaction,
GDBusMethodInvocation *invocation) GDBusMethodInvocation *invocation)
{ {
Transaction *real_transaction = TRANSACTION (transaction); Transaction *real_transaction = TRANSACTION (transaction);
if (real_transaction->method_cancellable == NULL) if (real_transaction->cancellable == NULL)
return FALSE; return FALSE;
if (!transaction_check_sender_is_owner (transaction, invocation)) if (!transaction_check_sender_is_owner (transaction, invocation))
@ -284,7 +293,7 @@ transaction_handle_cancel (RPMOSTreeTransaction *transaction,
} }
else else
{ {
g_cancellable_cancel (real_transaction->method_cancellable); g_cancellable_cancel (real_transaction->cancellable);
g_signal_emit (transaction, signals[CANCELLED], 0); g_signal_emit (transaction, signals[CANCELLED], 0);
rpmostree_transaction_complete_cancel (transaction, invocation); rpmostree_transaction_complete_cancel (transaction, invocation);
} }
@ -335,16 +344,6 @@ transaction_class_init (TransactionClass *class)
object_class->dispose = transaction_dispose; object_class->dispose = transaction_dispose;
object_class->finalize = transaction_finalize; object_class->finalize = transaction_finalize;
g_object_class_install_property (object_class,
PROP_CANCELLABLE,
g_param_spec_object ("cancellable",
NULL,
NULL,
G_TYPE_CANCELLABLE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
signals[CANCELLED] = g_signal_new ("cancelled", signals[CANCELLED] = g_signal_new ("cancelled",
TYPE_TRANSACTION, TYPE_TRANSACTION,
G_SIGNAL_RUN_LAST, G_SIGNAL_RUN_LAST,
@ -365,7 +364,13 @@ transaction_class_init (TransactionClass *class)
} }
static void static void
transaction_iface_init (RPMOSTreeTransactionIface *iface) transaction_initable_iface_init (GInitableIface *iface)
{
iface->init = transaction_initable_init;
}
static void
transaction_dbus_iface_init (RPMOSTreeTransactionIface *iface)
{ {
iface->handle_cancel = transaction_handle_cancel; iface->handle_cancel = transaction_handle_cancel;
iface->handle_finish = transaction_handle_finish; iface->handle_finish = transaction_handle_finish;
@ -378,7 +383,8 @@ transaction_init (Transaction *transaction)
RPMOSTreeTransaction * RPMOSTreeTransaction *
transaction_new (GDBusMethodInvocation *invocation, transaction_new (GDBusMethodInvocation *invocation,
GCancellable *method_cancellable) GCancellable *cancellable,
GError **error)
{ {
Transaction *transaction; Transaction *transaction;
GDBusConnection *connection; GDBusConnection *connection;
@ -390,12 +396,15 @@ transaction_new (GDBusMethodInvocation *invocation,
method_name = g_dbus_method_invocation_get_method_name (invocation); method_name = g_dbus_method_invocation_get_method_name (invocation);
sender = g_dbus_method_invocation_get_sender (invocation); sender = g_dbus_method_invocation_get_sender (invocation);
transaction = g_object_new (TYPE_TRANSACTION, transaction = g_initable_new (TYPE_TRANSACTION,
"cancellable", method_cancellable, cancellable, error,
"method-name", method_name, "method-name", method_name,
"owner", sender, "owner", sender,
"active", TRUE, "active", TRUE,
NULL); NULL);
if (transaction == NULL)
goto out;
/* XXX Would be handy if GDBusInterfaceSkeleton had an export() /* XXX Would be handy if GDBusInterfaceSkeleton had an export()
* class method so subclasses can know when a GDBusConnection * class method so subclasses can know when a GDBusConnection
@ -412,7 +421,8 @@ transaction_new (GDBusMethodInvocation *invocation,
transaction, transaction,
NULL); NULL);
return RPMOSTREE_TRANSACTION (transaction); out:
return (RPMOSTreeTransaction *) transaction;
} }
void void

View File

@ -27,7 +27,8 @@
GType transaction_get_type (void) G_GNUC_CONST; GType transaction_get_type (void) G_GNUC_CONST;
RPMOSTreeTransaction * RPMOSTreeTransaction *
transaction_new (GDBusMethodInvocation *invocation, transaction_new (GDBusMethodInvocation *invocation,
GCancellable *method_cancellable); GCancellable *cancellable,
GError **error);
void transaction_done (RPMOSTreeTransaction *transaction, void transaction_done (RPMOSTreeTransaction *transaction,
gboolean success, gboolean success,
const char *message); const char *message);