2009-02-10 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / accessible-register.c
index d4e1cbd..f92c078 100644 (file)
 #include "bridge.h"
 #include "accessible-register.h"
 
-#define ATK_BRIDGE_OBJECT_PATH_PREFIX "/org/freedesktop/atspi/accessible"
-#define ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE ATK_BRIDGE_OBJECT_PATH_PREFIX "/%d"
-#define ATK_BRIDGE_PATH_PREFIX_LENGTH 33
-
 /*
  * This module is responsible for keeping track of all the AtkObjects in
  * the application, so that they can be accessed remotely and placed in
@@ -39,7 +35,7 @@
  *
  * To access an AtkObject remotely we need to provide a D-Bus object 
  * path for it. The D-Bus object paths used have a standard prefix
- * (ATK_BRIDGE_OBJECT_PATH_PREFIX). Appended to this prefix is a string
+ * (SPI_ATK_OBJECT_PATH_PREFIX). Appended to this prefix is a string
  * representation of an integer reference. So to access an AtkObject 
  * remotely we keep a Hashtable that maps the given reference to 
  * the AtkObject pointer. An object in this hash table is said to be 'registered'.
  * the client side cache. This is done using the 'update' signal of the 
  * org.freedesktop.atspi.Tree interface. The update signal should send out
  * all of the cacheable data for an Accessible object.
+ *
  */
 
-GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
+/*
+ * FIXME
+ *
+ * This code seems very brittle.
+ * I would prefer changes to be made to
+ * gail and the ATK interface so that all Accessible
+ * objects are registered with an exporting module.
+ *
+ * This is the same system as Qt has with the QAccessibleBridge
+ * and QAccessibleBridgePlugin. It entails some rather
+ * large structural changes to ATK though:
+ *
+ * Removing infinite spaces (Child access no longer references child).
+ * Removing lazy creation of accessible objects.
+ */
+
+#define SPI_ATK_OBJECT_PATH_PREFIX  "/org/freedesktop/atspi/accessible"
+#define SPI_ATK_OBJECT_PATH_DESKTOP "/root"
+
+#define SPI_ATK_PATH_PREFIX_LENGTH 33
+#define SPI_ATK_OBJECT_REFERENCE_TEMPLATE SPI_ATK_OBJECT_PATH_PREFIX "/%d"
+
+
+static GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
 
 static guint counter = 1;
 
+static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
+
+/*---------------------------------------------------------------------------*/
+
+static GStaticMutex   recursion_check_guard = G_STATIC_MUTEX_INIT;
+static gboolean       recursion_check = FALSE;
+
+static gboolean
+recursion_check_and_set ()
+{
+  gboolean ret;
+  g_static_mutex_lock   (&recursion_check_guard);
+  ret = recursion_check;
+  recursion_check = TRUE;
+  g_static_mutex_unlock (&recursion_check_guard);
+  return ret;
+}
+
+static void
+recursion_check_unset ()
+{
+  g_static_mutex_lock   (&recursion_check_guard);
+  recursion_check = FALSE;
+  g_static_mutex_unlock (&recursion_check_guard);
+}
+
 /*---------------------------------------------------------------------------*/
 
 /*
@@ -74,6 +120,7 @@ assign_reference(void)
   /* Reference of 0 not allowed as used as direct key in hash table */
   if (counter == 0)
     counter++;
+  return counter;
 }
 
 /*
@@ -91,25 +138,24 @@ object_to_ref (AtkObject *accessible)
 static gchar *
 ref_to_path (guint ref)
 {
-  return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, ref);
+  return g_strdup_printf(SPI_ATK_OBJECT_REFERENCE_TEMPLATE, ref);
 }
 
 /*---------------------------------------------------------------------------*/
 
 /*
- * Called when a registered AtkObject is deleted.
- * Removes the AtkObject from the reference lookup tables.
+ * Callback for when a registered AtkObject is destroyed.
+ *
+ * Removes the AtkObject from the reference lookup tables, meaning
+ * it is no longer exposed over D-Bus.
  */
 static void
-deregister_accessible(gpointer data, GObject *accessible)
+deregister_accessible (gpointer data, GObject *accessible)
 {
   guint ref;
-  gchar *path;
-
-  g_assert(ATK_IS_OBJECT(accessible));
-
-  ref = atk_dbus_object_to_ref (ATK_OBJECT(accessible));
+  g_assert (ATK_IS_OBJECT (accessible));
 
+  ref = object_to_ref (ATK_OBJECT(accessible));
   if (ref != 0)
     {
       g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
@@ -134,41 +180,85 @@ register_accessible (AtkObject *accessible)
 
 /*---------------------------------------------------------------------------*/
 
-typedef void     (*ActionFunc) (AtkObject *);
-
-/* Return true if action should be performed */
-typedef gboolean (*FilterFunc) (AtkObject *);
-
+#ifdef SPI_ATK_DEBUG
 /*
- * This function performs a depth first traversal of a tree of AtkObjects.
+ * This function checks that the ref-count of an accessible
+ * is greater than 1.
  *
- * It uses a FilterFunc to determine if a node needs to have an action
- * performed on it.
+ * There is not currently any remote reference counting
+ * in AT-SPI D-Bus so objects that are remotely owned are not
+ * allowed.
  *
- * Nodes that are filtered out become leaves and no recursion is performed on
- * them.
+ * TODO Add debug wrapper
+ */
+static gboolean
+non_owned_accessible (AtkObject *accessible)
+{
+   if ((G_OBJECT (accessible))->ref_count <= 1)
+     {
+       g_warning ("AT-SPI: Child referenced that is not owned by its parent");
+       return TRUE;
+     }
+   else
+     {
+       return FALSE;
+     }
+}
+#endif /* SPI_ATK_DEBUG */
+
+/*---------------------------------------------------------------------------*/
+
+static gboolean
+has_manages_descendants (AtkObject *accessible)
+{
+   AtkStateSet *state;
+   gboolean result = FALSE;
+
+   /* This is dangerous, refing the state set
+    * seems to do wierd things to the tree & cause recursion
+    * by modifying the tree alot.
+    */
+   state = atk_object_ref_state_set (accessible);
+   if (atk_state_set_contains_state (state, ATK_STATE_MANAGES_DESCENDANTS))
+     {
+#ifdef SPI_ATK_DEBUG
+       g_warning ("AT-SPI: Object with 'Manages descendants' states not currently handled by AT-SPI");
+#endif
+       result = TRUE;
+     }
+   g_object_unref (state);
+
+   return result;
+}
+
+/*
+ * Registers a subtree of accessible objects
+ * rooted at the accessible object provided.
  *
- * Nodes where an action was performed are returned in a GList.
+ * The leaf nodes do not have their children
+ * registered. A node is considered a leaf
+ * if it has the state "manages-descendants"
+ * or if it has already been registered.
  */
 void
-traverse_atk_tree (AtkObject *accessible,
-                   GList **visited,
-                   ActionFunc action,
-                   FilterFunc filter)
+register_subtree (AtkObject *accessible)
 {
   AtkObject *current, *tmp;
   GQueue    *stack;
-  GList     *uplist = NULL;
-  guint      i, ref;
+  guint      i;
   gboolean   recurse;
 
-  if (!filter (accessible))
+
+  current = g_object_ref (accessible);
+  if (has_manages_descendants (current))
+    {
+      g_object_unref (current);
       return;
+    }
 
   stack = g_queue_new ();
-  current = g_object_ref (accessible);
-  action (current)
-  *visited = g_list_prepend (*visited, current);
+
+  register_accessible (current);
   g_queue_push_head (stack, GINT_TO_POINTER (0));
 
   /*
@@ -178,44 +268,63 @@ traverse_atk_tree (AtkObject *accessible,
   while (!g_queue_is_empty (stack))
     {
       /* Find the next child node that needs processing */
+
       i = GPOINTER_TO_INT(g_queue_peek_head (stack));
       recurse = FALSE;
+
       while (i < atk_object_get_n_accessible_children (current) &&
              recurse == FALSE)
         {
           tmp = atk_object_ref_accessible_child (current, i);
-          /* If filter function */
-          if (filter (tmp))
+
+#ifdef SPI_ATK_DEBUG
+          non_owned_accessible (tmp);
+#endif
+
+          if (object_to_ref (tmp))
             {
-              recurse = TRUE;
+              /* If its already registered, just update */
+              spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
+            }
+          else if (has_manages_descendants (tmp))
+            {
+              /* If it has manages descendants, just register and update */
+              register_accessible (tmp);
+              spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
             }
           else
             {
-              i++;
+              recurse = TRUE;
+            }
+
+          if (!recurse)
+            {
               g_object_unref (G_OBJECT (tmp));
             }
+
+          i++;
         }
 
       if (recurse)
         {
           /* Push onto stack */
           current = tmp;
-          action (current);
-          *visited = g_list_prepend (*visited, current);
-          g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i+1);
+          register_accessible (current);
+
+          g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i);
           g_queue_push_head (stack, GINT_TO_POINTER (0));
         }
       else
         {
           /* Pop from stack */
+          spi_emit_cache_update (current, atk_adaptor_app_data->bus);
           tmp = current;
           current = atk_object_get_parent (current);
           g_object_unref (G_OBJECT (tmp));
           g_queue_pop_head (stack);
         }
     }
-
-  return ref;
+    g_queue_free (stack);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -230,7 +339,7 @@ update_accessible (AtkObject *accessible)
   guint  ref = 0;
   g_assert(ATK_IS_OBJECT(accessible));
 
-  ref = atk_dbus_object_to_ref (accessible);
+  ref = object_to_ref (accessible);
   if (ref)
     {
       spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
@@ -256,12 +365,12 @@ atk_dbus_path_to_object (const char *path)
 
   g_assert (path);
 
-  if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0) 
+  if (strncmp(path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH) != 0)
     return NULL;
 
-  path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
+  path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
 
-  if (path[0] == '\0')
+  if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_DESKTOP, path))
      return atk_get_root();
   if (path[0] != '/')
      return NULL;
@@ -282,13 +391,18 @@ gchar *
 atk_dbus_object_to_path (AtkObject *accessible)
 {
   guint ref;
-  g_assert(ATK_IS_OBJECT(accessible));
 
-  ref = atk_dbus_object_to_ref (accessible);
+  ref = object_to_ref (accessible);
   if (!ref)
       return NULL;
   else
-      return atk_dbus_ref_to_path (ref);
+      return ref_to_path (ref);
+}
+
+gchar *
+atk_dbus_desktop_object_path ()
+{
+  return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_DESKTOP);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -312,26 +426,35 @@ tree_update_listener (GSignalInvocationHint *signal_hint,
   AtkPropertyValues *values;
   const gchar *pname = NULL;
 
-  accessible = g_value_get_object (&param_values[0]);
-  values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
-
-  pname = values[0].property_name;
+  g_static_rec_mutex_lock (&registration_mutex);
 
-  if (!atk_dbus_object_to_ref (accessible))
-      return TRUE;
+  /* Ensure that only registered accessibles
+   * have their signals processed.
+   */
+  accessible = g_value_get_object (&param_values[0]);
+  g_assert (ATK_IS_OBJECT (accessible));
 
-  if (strcmp (pname, "accessible-name") == 0 ||
-      strcmp (pname, "accessible-description"))
-    {
-      atk_dbus_update_accessible (accessible);
-    }
-  else if (strcmp (pname, "accessible-parent"))
+  if (object_to_ref (accessible))
     {
-      guint ref;
+#ifdef SPI_ATK_DEBUG
+      if (recursion_check_and_set ())
+          g_warning ("AT-SPI: Recursive use of registration module");
+#endif
+
+      values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
+      pname = values[0].property_name;
+      if (strcmp (pname, "accessible-name") == 0 ||
+          strcmp (pname, "accessible-description") == 0)
+        {
+          update_accessible (accessible);
+        }
+      /* Parent value us updated by child-add signal of parent object */
 
-      ref = atk_dbus_object_to_ref;
-      if (!ref)
+      recursion_check_unset ();
     }
+
+  g_static_rec_mutex_unlock (&registration_mutex);
+
   return TRUE;
 }
 
@@ -350,26 +473,46 @@ tree_update_children_listener (GSignalInvocationHint *signal_hint,
   AtkObject *accessible;
   const gchar *detail = NULL;
   AtkObject *child;
-  gboolean child_needs_unref = FALSE;
 
-  if (signal_hint->detail)
-    detail = g_quark_to_string (signal_hint->detail);
+  g_static_rec_mutex_lock (&registration_mutex);
 
+  /* Ensure that only registered accessibles
+   * have their signals processed.
+   */
   accessible = g_value_get_object (&param_values[0]);
-  if (!strcmp (detail, "add"))
+  g_assert (ATK_IS_OBJECT (accessible));
+
+  if (object_to_ref (accessible))
     {
-      gpointer child;
-      int index = g_value_get_uint (param_values + 1);
-      child = g_value_get_pointer (param_values + 2);
+#ifdef SPI_ATK_DEBUG
+      if (recursion_check_and_set ())
+          g_warning ("AT-SPI: Recursive use of registration module");
+#endif
 
-      if (ATK_IS_OBJECT (child))
-          g_object_ref (child);
-      else
-          child = atk_object_ref_accessible_child (accessible, index);
+      if (signal_hint->detail)
+          detail = g_quark_to_string (signal_hint->detail);
+
+      if (!strcmp (detail, "add"))
+        {
+          gpointer child;
+          int index = g_value_get_uint (param_values + 1);
+          child = g_value_get_pointer (param_values + 2);
 
-      atk_dbus_register_subtree (child);
-      g_object_unref (child);
+          if (!ATK_IS_OBJECT (child))
+            {
+              child = atk_object_ref_accessible_child (accessible, index);
+#ifdef SPI_ATK_DEBUG
+              non_owned_accessible (child);
+#endif
+            }
+          register_subtree (child);
+        }
+
+      recursion_check_unset ();
     }
+
+  g_static_rec_mutex_unlock (&registration_mutex);
+
   return TRUE;
 }
 
@@ -385,7 +528,12 @@ atk_dbus_initialize (AtkObject *root)
   if (!ref2ptr)
     ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
 
-  atk_dbus_register_accessible (root);
+#ifdef SPI_ATK_DEBUG
+  if (g_thread_supported ())
+      g_message ("AT-SPI: Threads enabled");
+#endif
+
+  register_subtree (root);
 
   atk_add_global_event_listener (tree_update_listener, "Gtk:AtkObject:property-change");
   atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");