[gtk/tracing: 1335/1335] GtkApplication: Add a profiler dbus api



commit 34120b6af17088712dbcd9311612c54e0582e29c
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat May 19 20:36:00 2018 +0100

    GtkApplication: Add a profiler dbus api
    
    Add a simple D-Bus api to let sysprof start and stop
    tracing at runtime, and get the data directly, via
    a passed fd.

 gtk/gtkapplication.c        | 120 ++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkapplicationprivate.h |   1 +
 2 files changed, 121 insertions(+)
---
diff --git a/gtk/gtkapplication.c b/gtk/gtkapplication.c
index c4646dbd88..158fb73834 100644
--- a/gtk/gtkapplication.c
+++ b/gtk/gtkapplication.c
@@ -21,6 +21,9 @@
 #include "config.h"
 
 #include "gtkapplication.h"
+#include "gdkprofiler.h"
+#include <gio/gunixfdlist.h>
+
 
 #include <stdlib.h>
 
@@ -602,6 +605,121 @@ gtk_application_finalize (GObject *object)
   G_OBJECT_CLASS (gtk_application_parent_class)->finalize (object);
 }
 
+static const gchar org_gtk_Profiler_xml[] =
+  "<node>"
+    "<interface name='org.gtk.Profiler'>"
+      "<method name='Start'>"
+        "<arg type='h' name='fd' direction='in'/>"
+      "</method>"
+      "<method name='Stop'>"
+      "</method>"
+    "<property name='Version' type='u' access='read'/>"
+    "</interface>"
+  "</node>";
+
+static GDBusInterfaceInfo *org_gtk_Profiler;
+
+static void
+gtk_profiler_method_call (GDBusConnection       *connection,
+                          const gchar           *sender,
+                          const gchar           *object_path,
+                          const gchar           *interface_name,
+                          const gchar           *method_name,
+                          GVariant              *parameters,
+                          GDBusMethodInvocation *invocation,
+                          gpointer               user_data)
+{
+  if (strcmp (method_name, "Start") == 0)
+    {
+      GDBusMessage *message;
+      GUnixFDList *fd_list;
+      int fd = -1;
+      int idx;
+
+      g_variant_get (parameters, "(h)", &idx);
+
+      message = g_dbus_method_invocation_get_message (invocation);
+      fd_list = g_dbus_message_get_unix_fd_list (message);
+      if (fd_list)
+        fd = g_unix_fd_list_get (fd_list, idx, NULL);
+
+      gdk_profiler_start (fd);
+    }
+  else if (strcmp (method_name, "Stop") == 0)
+    {
+      gdk_profiler_stop ();
+    }
+  else
+    g_assert_not_reached ();
+
+  g_dbus_method_invocation_return_value (invocation, NULL);
+}
+
+static GVariant *
+gtk_profiler_get_property (GDBusConnection *connection,
+                           const gchar  *sender,
+                           const gchar  *object_path,
+                           const gchar  *interface_name,
+                           const gchar  *property_name,
+                           GError      **error,
+                           gpointer      user_data)
+{
+  if (strcmp (property_name, "Version") == 0)
+    return g_variant_new_uint32 (1);
+  else
+    g_assert_not_reached ();
+
+  return NULL;
+}
+
+
+static gboolean
+gtk_application_dbus_register (GApplication     *application,
+                               GDBusConnection  *connection,
+                               const char       *obect_path,
+                               GError          **error)
+{
+  GtkApplicationImplDBus *dbus = (GtkApplicationImplDBus *) application;
+  GDBusInterfaceVTable vtable = {
+    gtk_profiler_method_call,
+    gtk_profiler_get_property,
+    NULL
+  };
+
+  if (org_gtk_Profiler == NULL)
+    {
+      GDBusNodeInfo *info;
+
+      info = g_dbus_node_info_new_for_xml (org_gtk_Profiler_xml, error);
+      if (info == NULL)
+        return FALSE;
+
+      org_gtk_Profiler = g_dbus_node_info_lookup_interface (info, "org.gtk.Profiler");
+      g_dbus_interface_info_ref (org_gtk_Profiler);
+      g_dbus_node_info_unref (info);
+    }
+
+  dbus->profiler_id = g_dbus_connection_register_object (connection,
+                                                         "/org/gtk/Profiler",
+                                                         org_gtk_Profiler,
+                                                         &vtable,
+                                                         NULL,
+                                                         NULL,
+                                                         error);
+
+  return TRUE;
+}
+
+static void
+gtk_application_dbus_unregister (GApplication     *application,
+                                 GDBusConnection  *connection,
+                                 const char       *obect_path)
+{
+  GtkApplicationImplDBus *dbus = (GtkApplicationImplDBus *) application;
+
+  g_dbus_connection_unregister_object (connection, dbus->profiler_id);
+}
+
 static void
 gtk_application_class_init (GtkApplicationClass *class)
 {
@@ -618,6 +736,8 @@ gtk_application_class_init (GtkApplicationClass *class)
   application_class->after_emit = gtk_application_after_emit;
   application_class->startup = gtk_application_startup;
   application_class->shutdown = gtk_application_shutdown;
+  application_class->dbus_register = gtk_application_dbus_register;
+  application_class->dbus_unregister = gtk_application_dbus_unregister;
 
   class->window_added = gtk_application_window_added;
   class->window_removed = gtk_application_window_removed;
diff --git a/gtk/gtkapplicationprivate.h b/gtk/gtkapplicationprivate.h
index 8f6cdb001d..bbca31024b 100644
--- a/gtk/gtkapplicationprivate.h
+++ b/gtk/gtkapplicationprivate.h
@@ -127,6 +127,7 @@ typedef struct
 
   gchar           *menubar_path;
   guint            menubar_id;
+  guint            profiler_id;
 
   /* Session management... */
   GDBusProxy      *sm_proxy;


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