/* This can get used from scheduled idles, hence the boolean return */
static gboolean
auth_call_complete (AuthCall *call)
{
	nm_auth_chain_remove_call (call->chain, call);
	nm_auth_chain_check_done (call->chain);
	auth_call_free (call);
	return FALSE;
}
static void
auth_call_cancel (gpointer user_data)
{
	AuthCall *call = user_data;

	if (call->cancellable) {
		/* we don't free call immediately. Instead we cancel the async operation
		 * and set cancellable to NULL. pk_call_cb() will check for this and
		 * do the final cleanup. */
		g_cancellable_cancel (call->cancellable);
		g_clear_object (&call->cancellable);
	} else {
		g_source_remove (call->call_idle_id);
		auth_call_free (call);
	}
}
static void
pk_call_cb (GObject *object, GAsyncResult *result, gpointer user_data)
{
	AuthCall *call = user_data;
	GError *error = NULL;
	gboolean is_authorized, is_challenge;

	nm_auth_manager_polkit_authority_check_authorization_finish (NM_AUTH_MANAGER (object),
	                                                             result,
	                                                             &is_authorized,
	                                                             &is_challenge,
	                                                             &error);

	/* If the call is already canceled do nothing */
	if (!call->cancellable) {
		nm_log_dbg (LOGD_CORE, "callback already cancelled");
		g_clear_error (&error);
		auth_call_free (call);
		return;
	}

	if (error) {
		nm_log_warn (LOGD_CORE, "error requesting auth for %s: %s",
		             call->permission, error->message);

		if (!call->chain->error) {
			call->chain->error = error;
			error = NULL;
		} else
			g_clear_error (&error);
	} else {
		guint call_result = NM_AUTH_CALL_RESULT_UNKNOWN;

		if (is_authorized) {
			/* Caller has the permission */
			call_result = NM_AUTH_CALL_RESULT_YES;
		} else if (is_challenge) {
			/* Caller could authenticate to get the permission */
			call_result = NM_AUTH_CALL_RESULT_AUTH;
		} else
			call_result = NM_AUTH_CALL_RESULT_NO;

		nm_auth_chain_set_data (call->chain, call->permission, GUINT_TO_POINTER (call_result), NULL);
	}

	auth_call_complete (call);
}
static void
pk_call_cb (GObject *object, GAsyncResult *result, gpointer user_data)
{
	AuthCall *call = user_data;
	PolkitAuthorizationResult *pk_result;
	GError *error = NULL;

	pk_result = polkit_authority_check_authorization_finish ((PolkitAuthority *) object, result, &error);

	/* If the call is already canceled do nothing */
	if (!call->cancellable) {
		g_clear_error (&error);
		g_clear_object (&pk_result);
		auth_call_free (call);
		return;
	}

	if (error) {
		if (!call->chain->error)
			call->chain->error = g_error_copy (error);

		nm_log_warn (LOGD_CORE, "error requesting auth for %s: (%d) %s",
		             call->permission, error->code, error->message);
		g_clear_error (&error);
	} else {
		guint call_result = NM_AUTH_CALL_RESULT_UNKNOWN;

		if (polkit_authorization_result_get_is_authorized (pk_result)) {
			/* Caller has the permission */
			call_result = NM_AUTH_CALL_RESULT_YES;
		} else if (polkit_authorization_result_get_is_challenge (pk_result)) {
			/* Caller could authenticate to get the permission */
			call_result = NM_AUTH_CALL_RESULT_AUTH;
		} else
			call_result = NM_AUTH_CALL_RESULT_NO;

		nm_auth_chain_set_data (call->chain, call->permission, GUINT_TO_POINTER (call_result), NULL);
		g_object_unref (pk_result);
	}

	auth_call_complete (call);
}
static gboolean
auth_call_complete (AuthCall *call)
{
	NMAuthChain *self;

	g_return_val_if_fail (call, G_SOURCE_REMOVE);

	self = call->chain;

	g_return_val_if_fail (self, G_SOURCE_REMOVE);
	g_return_val_if_fail (g_slist_find (self->calls, call), G_SOURCE_REMOVE);

	self->calls = g_slist_remove (self->calls, call);

	if (!self->calls) {
		g_assert (!self->idle_id && !self->done);
		self->idle_id = g_idle_add (auth_chain_finish, self);
	}
	auth_call_free (call);
	return FALSE;
}