static void
infinoted_plugin_document_stream_close_stream(
  InfinotedPluginDocumentStreamStream* stream)
{
  stream->plugin->streams = g_slist_remove(stream->plugin->streams, stream);

  if(stream->proxy != NULL || stream->subscribe_request != NULL)
    infinoted_plugin_document_stream_stop(stream, FALSE);

  if(stream->navigate_handle != NULL)
  {
    infinoted_plugin_util_navigate_cancel(stream->navigate_handle);
    stream->navigate_handle = NULL;
  }

  infinoted_plugin_document_stream_queue_finalize(&stream->send_queue);
  infinoted_plugin_document_stream_queue_finalize(&stream->recv_queue);

  inf_io_remove_watch(
    infinoted_plugin_manager_get_io(stream->plugin->manager),
    stream->watch
  );

  g_free(stream->username);
  stream->username = NULL;

  close(stream->socket);
  stream->socket = -1;

  if(stream->status == INFINOTED_PLUGIN_DOCUMENT_STREAM_NORMAL)
    g_slice_free(InfinotedPluginDocumentStreamStream, stream);
  else if(stream->status == INFINOTED_PLUGIN_DOCUMENT_STREAM_RECEIVING)
    stream->status = INFINOTED_PLUGIN_DOCUMENT_STREAM_CLOSED;
}
static void
infinoted_plugin_dbus_invocation_unref(gpointer data)
{
  InfinotedPluginDbusInvocation* invocation;
  invocation = (InfinotedPluginDbusInvocation*)data;

  if(g_atomic_int_dec_and_test(&invocation->ref_count) == TRUE)
  {
    if(invocation->navigate != NULL)
      infinoted_plugin_util_navigate_cancel(invocation->navigate);

    if(invocation->request != NULL)
    {
      g_signal_handlers_disconnect_by_func(
        G_OBJECT(invocation->request),
        G_CALLBACK(invocation->request_func),
        invocation
      );
    }

    g_free(invocation->method_name);
    g_variant_unref(invocation->parameters);
    g_object_unref(invocation->invocation);

    g_slice_free(InfinotedPluginDbusInvocation, invocation);
  }
}