Skip to content

Commit

Permalink
[mms-ofono] Hold a reference to MMS ofono context during async calls
Browse files Browse the repository at this point in the history
Even if asynchronous GDBusProxy call is cancelled, the completion callback
is still invoked. We need to hold an additional reference to MMSOfonoContext
for the duration of the call.
  • Loading branch information
monich committed Oct 21, 2014
1 parent 814b665 commit 510b702
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
67 changes: 47 additions & 20 deletions mms-ofono/src/mms_ofono_context.c
Expand Up @@ -100,6 +100,9 @@ mms_ofono_context_activate_done(
}
g_error_free(error);
}

/* Release the reference we have been holding during the call */
mms_ofono_context_unref(context);
}

static
Expand All @@ -125,14 +128,17 @@ mms_ofono_context_deactivate_done(
/* Regardless of whether or nor deactivation has succeeded, we mark this
* connection as closed. */
mms_ofono_context_drop_connection(context, MMS_CONNECTION_STATE_CLOSED);

/* Release the reference we have been holding during the call */
mms_ofono_context_unref(context);
}

void
mms_ofono_context_set_active(
MMSOfonoContext* context,
gboolean active)
{
MMS_DEBUG("%s connection %s", active ? "Opening":"Closing",
MMS_DEBUG("%s connection %s", active ? "Opening" : "Closing",
context->modem->imsi);
if (context->set_active_cancel) {
g_cancellable_cancel(context->set_active_cancel);
Expand All @@ -141,9 +147,9 @@ mms_ofono_context_set_active(
context->set_active_cancel = g_cancellable_new();
org_ofono_connection_context_call_set_property(context->proxy,
OFONO_CONTEXT_PROPERTY_ACTIVE, g_variant_new_variant(
g_variant_new_boolean(active)), context->set_active_cancel,
active ? mms_ofono_context_activate_done :
mms_ofono_context_deactivate_done, context);
g_variant_new_boolean(active)), context->set_active_cancel, active ?
mms_ofono_context_activate_done : mms_ofono_context_deactivate_done,
mms_ofono_context_ref(context));
}

MMSOfonoContext*
Expand All @@ -165,6 +171,8 @@ mms_ofono_context_new(
context->active = g_variant_get_boolean(value);
g_variant_unref(value);
}

context->ref_count = 1;
context->path = g_strdup(path);
context->proxy = proxy;
context->modem = modem;
Expand All @@ -183,27 +191,46 @@ mms_ofono_context_new(
}
}

static
void
mms_ofono_context_free(
mms_ofono_context_finalize(
MMSOfonoContext* context)
{
if (context->connection) {
context->connection->context = NULL;
mms_ofono_connection_cancel(context->connection);
mms_ofono_connection_unref(context->connection);
}
if (context->set_active_cancel) {
g_object_unref(context->set_active_cancel);
}
g_signal_handler_disconnect(context->proxy,
context->property_change_signal_id);
g_object_unref(context->proxy);
g_free(context->path);
}

MMSOfonoContext*
mms_ofono_context_ref(
MMSOfonoContext* context)
{
if (context) {
if (context->connection) {
context->connection->context = NULL;
mms_ofono_connection_cancel(context->connection);
mms_ofono_connection_unref(context->connection);
}
if (context->set_active_cancel) {
g_cancellable_cancel(context->set_active_cancel);
g_object_unref(context->set_active_cancel);
}
if (context->proxy) {
g_signal_handler_disconnect(context->proxy,
context->property_change_signal_id);
g_object_unref(context->proxy);
MMS_ASSERT(context->ref_count > 0);
g_atomic_int_inc(&context->ref_count);
}
return context;
}

void
mms_ofono_context_unref(
MMSOfonoContext* context)
{
if (context) {
MMS_ASSERT(context->ref_count > 0);
if (g_atomic_int_dec_and_test(&context->ref_count)) {
mms_ofono_context_finalize(context);
g_free(context);
}
g_free(context->path);
g_free(context);
}
}

Expand Down
7 changes: 6 additions & 1 deletion mms-ofono/src/mms_ofono_context.h
Expand Up @@ -18,6 +18,7 @@
#include "mms_ofono_types.h"

struct mms_ofono_context {
gint ref_count;
MMSOfonoModem* modem;
char* path;
gboolean active;
Expand All @@ -38,8 +39,12 @@ mms_ofono_context_set_active(
MMSOfonoContext* context,
gboolean active);

MMSOfonoContext*
mms_ofono_context_ref(
MMSOfonoContext* context);

void
mms_ofono_context_free(
mms_ofono_context_unref(
MMSOfonoContext* context);

#endif /* JOLLA_MMS_OFONO_CONTEXT_H */
Expand Down
14 changes: 7 additions & 7 deletions mms-ofono/src/mms_ofono_modem.c
Expand Up @@ -152,7 +152,7 @@ mms_ofono_modem_gprs_context_removed(
MMS_ASSERT(proxy == modem->gprs_proxy);
if (modem->mms_context && !g_strcmp0(modem->mms_context->path, path)) {
MMS_DEBUG("MMS context %s removed", path);
mms_ofono_context_free(modem->mms_context);
mms_ofono_context_unref(modem->mms_context);
modem->mms_context = NULL;
}
}
Expand Down Expand Up @@ -214,8 +214,8 @@ mms_ofono_modem_scan_interfaces(
GVariantIter iter;
GVariant* child;
for (g_variant_iter_init(&iter, ifs);
(child = g_variant_iter_next_value(&iter)) &&
(!sim_interface || !gprs_interface);
(!sim_interface || !gprs_interface) &&
(child = g_variant_iter_next_value(&iter));
g_variant_unref(child)) {
const char* ifname = NULL;
g_variant_get(child, "&s", &ifname);
Expand Down Expand Up @@ -286,7 +286,7 @@ mms_ofono_modem_scan_interfaces(
if (context_info) {
if (m->mms_context &&
!g_strcmp0(context_info->path, m->mms_context->path)) {
mms_ofono_context_free(m->mms_context);
mms_ofono_context_unref(m->mms_context);
m->mms_context = NULL;
}
if (!m->mms_context) {
Expand All @@ -301,15 +301,15 @@ mms_ofono_modem_scan_interfaces(
} else {
MMS_DEBUG("No MMS context");
if (m->mms_context) {
mms_ofono_context_free(m->mms_context);
mms_ofono_context_unref(m->mms_context);
m->mms_context = NULL;
}
}
} else if (m->gprs_proxy) {
mms_ofono_modem_disconnect_gprs_proxy(m);
if (m->mms_context) {
MMS_DEBUG("No MMS context");
mms_ofono_context_free(m->mms_context);
mms_ofono_context_unref(m->mms_context);
m->mms_context = NULL;
}
}
Expand Down Expand Up @@ -388,7 +388,7 @@ mms_ofono_modem_free(
MMS_VERBOSE_("%p '%s'", modem, modem->path);
mms_ofono_modem_disconnect_sim_proxy(modem);
mms_ofono_modem_disconnect_gprs_proxy(modem);
mms_ofono_context_free(modem->mms_context);
mms_ofono_context_unref(modem->mms_context);
if (modem->proxy) {
g_signal_handler_disconnect(modem->proxy,
modem->property_change_signal_id);
Expand Down

0 comments on commit 510b702

Please sign in to comment.