[vala/gdbus: 11/11] D-Bus: Require gio-unix only when using file descriptor passing



commit 86d71a477448bff3a3a8f0760f450db60738147b
Author: Jürg Billeter <j bitron ch>
Date:   Fri Oct 22 13:06:25 2010 +0200

    D-Bus: Require gio-unix only when using file descriptor passing

 codegen/valagdbusclientmodule.vala |   32 +++++++++++++++++++++-----------
 codegen/valagdbusmodule.vala       |   26 ++++++++++++++++++++++++++
 codegen/valagdbusservermodule.vala |   32 ++++++++++++++++++++------------
 3 files changed, 67 insertions(+), 23 deletions(-)
---
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index 95eb641..27cda25 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -415,11 +415,15 @@ public class Vala.GDBusClientModule : GDBusModule {
 	}
 
 	void generate_marshalling (Method m, CallType call_type, string? iface_name, string? method_name) {
-		cfile.add_include ("gio/gunixfdlist.h");
 
 		var connection = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_proxy_get_connection"));
 		connection.add_argument (new CCodeIdentifier ("self"));
 
+		bool uses_fd = dbus_method_uses_file_descriptor (m);
+		if (uses_fd) {
+			cfile.add_include ("gio/gunixfdlist.h");
+		}
+
 		if (call_type != CallType.FINISH) {
 			var destination = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_proxy_get_name"));
 			destination.add_argument (new CCodeIdentifier ("self"));
@@ -464,8 +468,10 @@ public class Vala.GDBusClientModule : GDBusModule {
 			builder_init.add_argument (new CCodeIdentifier ("G_VARIANT_TYPE_TUPLE"));
 			ccode.add_expression (builder_init);
 
-			ccode.add_declaration ("GUnixFDList", new CCodeVariableDeclarator ("*_fd_list"));
-			ccode.add_expression (new CCodeAssignment (new CCodeIdentifier ("_fd_list"), new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_new"))));
+			if (uses_fd) {
+				ccode.add_declaration ("GUnixFDList", new CCodeVariableDeclarator ("*_fd_list"));
+				ccode.add_expression (new CCodeAssignment (new CCodeIdentifier ("_fd_list"), new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_new"))));
+			}
 
 			CCodeExpression cancellable = new CCodeConstant ("NULL");
 
@@ -494,14 +500,16 @@ public class Vala.GDBusClientModule : GDBusModule {
 			set_body.add_argument (new CCodeIdentifier ("_arguments"));
 			ccode.add_expression (set_body);
 
-			ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_message_set_unix_fd_list"));
-			ccall.add_argument (new CCodeIdentifier ("_message"));
-			ccall.add_argument (new CCodeIdentifier ("_fd_list"));
-			ccode.add_expression (ccall);
+			if (uses_fd) {
+				ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_message_set_unix_fd_list"));
+				ccall.add_argument (new CCodeIdentifier ("_message"));
+				ccall.add_argument (new CCodeIdentifier ("_fd_list"));
+				ccode.add_expression (ccall);
 
-			ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_unref"));
-			ccall.add_argument (new CCodeIdentifier ("_fd_list"));
-			ccode.add_expression (ccall);
+				ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_unref"));
+				ccall.add_argument (new CCodeIdentifier ("_fd_list"));
+				ccode.add_expression (ccall);
+			}
 
 			// send D-Bus message
 
@@ -590,7 +598,9 @@ public class Vala.GDBusClientModule : GDBusModule {
 
 			bool has_result = !(m.return_type is VoidType);
 
-			ccode.add_declaration ("gint", new CCodeVariableDeclarator.zero ("_fd_index", new CCodeConstant ("0")));
+			if (uses_fd) {
+				ccode.add_declaration ("gint", new CCodeVariableDeclarator.zero ("_fd_index", new CCodeConstant ("0")));
+			}
 
 			foreach (FormalParameter param in m.get_parameters ()) {
 				if (param.direction == ParameterDirection.OUT) {
diff --git a/codegen/valagdbusmodule.vala b/codegen/valagdbusmodule.vala
index 7105017..6667771 100644
--- a/codegen/valagdbusmodule.vala
+++ b/codegen/valagdbusmodule.vala
@@ -111,6 +111,32 @@ public class Vala.GDBusModule : GVariantModule {
 		cfile.add_function (cquark_fun);
 	}
 
+	bool is_file_descriptor (DataType type) {
+		if (type is ObjectType) {
+			if (type.data_type.get_full_name () == "GLib.UnixInputStream" ||
+			    type.data_type.get_full_name () == "GLib.UnixOutputStream" ||
+			    type.data_type.get_full_name () == "GLib.Socket") {
+				return true;
+			}
+		}
+
+		return false;
+	}
+
+	public bool dbus_method_uses_file_descriptor (Method method) {
+		foreach (FormalParameter param in method.get_parameters ()) {
+			if (is_file_descriptor (param.variable_type)) {
+				return true;
+			}
+		}
+
+		if (is_file_descriptor (method.return_type)) {
+			return true;
+		}
+
+		return false;
+	}
+
 	CCodeExpression? get_file_descriptor (DataType type, CCodeExpression expr) {
 		if (type is ObjectType) {
 			if (type.data_type.get_full_name () == "GLib.UnixInputStream") {
diff --git a/codegen/valagdbusservermodule.vala b/codegen/valagdbusservermodule.vala
index 9cb79c3..9f8813b 100644
--- a/codegen/valagdbusservermodule.vala
+++ b/codegen/valagdbusservermodule.vala
@@ -74,8 +74,6 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 
 		push_function (function);
 
-		cfile.add_include ("gio/gunixfdlist.h");
-
 		if (ready) {
 			ccode.add_declaration ("GDBusMethodInvocation *", new CCodeVariableDeclarator ("invocation", new CCodeIdentifier ("_user_data_")));
 		}
@@ -84,6 +82,10 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 		connection.add_argument (new CCodeIdentifier ("invocation"));
 
 		bool no_reply = is_dbus_no_reply (m);
+		bool uses_fd = dbus_method_uses_file_descriptor (m);
+		if (uses_fd) {
+			cfile.add_include ("gio/gunixfdlist.h");
+		}
 
 		if (!m.coroutine || ready) {
 			ccode.add_declaration ("GError*", new CCodeVariableDeclarator ("error", new CCodeConstant ("NULL")));
@@ -109,7 +111,9 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 		}
 
 		if (!ready) {
-			ccode.add_declaration ("gint", new CCodeVariableDeclarator.zero ("_fd_index", new CCodeConstant ("0")));
+			if (uses_fd) {
+				ccode.add_declaration ("gint", new CCodeVariableDeclarator.zero ("_fd_index", new CCodeConstant ("0")));
+			}
 
 			foreach (FormalParameter param in m.get_parameters ()) {
 				if (param.direction != ParameterDirection.IN) {
@@ -245,8 +249,10 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 			builder_init.add_argument (new CCodeIdentifier ("G_VARIANT_TYPE_TUPLE"));
 			ccode.add_expression (builder_init);
 
-			ccode.add_declaration ("GUnixFDList", new CCodeVariableDeclarator ("*_fd_list"));
-			ccode.add_expression (new CCodeAssignment (new CCodeIdentifier ("_fd_list"), new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_new"))));
+			if (uses_fd) {
+				ccode.add_declaration ("GUnixFDList", new CCodeVariableDeclarator ("*_fd_list"));
+				ccode.add_expression (new CCodeAssignment (new CCodeIdentifier ("_fd_list"), new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_new"))));
+			}
 
 			foreach (FormalParameter param in m.get_parameters ()) {
 				if (param.direction != ParameterDirection.OUT) {
@@ -324,14 +330,16 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 			set_body.add_argument (new CCodeIdentifier ("_reply"));
 			ccode.add_expression (set_body);
 
-			ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_message_set_unix_fd_list"));
-			ccall.add_argument (new CCodeIdentifier ("_reply_message"));
-			ccall.add_argument (new CCodeIdentifier ("_fd_list"));
-			ccode.add_expression (ccall);
+			if (uses_fd) {
+				ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_message_set_unix_fd_list"));
+				ccall.add_argument (new CCodeIdentifier ("_reply_message"));
+				ccall.add_argument (new CCodeIdentifier ("_fd_list"));
+				ccode.add_expression (ccall);
 
-			ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_unref"));
-			ccall.add_argument (new CCodeIdentifier ("_fd_list"));
-			ccode.add_expression (ccall);
+				ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_unref"));
+				ccall.add_argument (new CCodeIdentifier ("_fd_list"));
+				ccode.add_expression (ccall);
+			}
 		} else {
 			ccode.add_expression (ccall);
 		}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]