Added debug option to remove all recoverable
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / accessible-register.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2008 Novell, Inc.
6  * Copyright 2008, 2009 Codethink Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "bridge.h"
29 #include "accessible-register.h"
30
31 #define ATK_BRIDGE_OBJECT_PATH_PREFIX "/org/freedesktop/atspi/accessible"
32 #define ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE ATK_BRIDGE_OBJECT_PATH_PREFIX "/%d"
33 #define ATK_BRIDGE_PATH_PREFIX_LENGTH 33
34
35 /*
36  * This module is responsible for keeping track of all the AtkObjects in
37  * the application, so that they can be accessed remotely and placed in
38  * a client side cache.
39  *
40  * To access an AtkObject remotely we need to provide a D-Bus object 
41  * path for it. The D-Bus object paths used have a standard prefix
42  * (ATK_BRIDGE_OBJECT_PATH_PREFIX). Appended to this prefix is a string
43  * representation of an integer reference. So to access an AtkObject 
44  * remotely we keep a Hashtable that maps the given reference to 
45  * the AtkObject pointer. An object in this hash table is said to be 'registered'.
46  *
47  * The architecture of AT-SPI dbus is such that AtkObjects are not
48  * remotely reference counted. This means that we need to keep track of
49  * object destruction. When an object is destroyed it must be 'deregistered'
50  * To do this lookup we keep a dbus-id attribute on each AtkObject.
51  *
52  * In addition to accessing the objects remotely we must be able to update
53  * the client side cache. This is done using the 'update' signal of the 
54  * org.freedesktop.atspi.Tree interface. The update signal should send out
55  * all of the cacheable data for an Accessible object.
56  *
57  */
58
59 /*
60  * FIXME
61  *
62  * While traversing the ATK tree we may modify it unintentionally.
63  * This is either a bug in the Gail implementation or this module.
64  * If a change is caused that recurses, via a signal into this module
65  * we should catch it.
66  *
67  * Things could also be changed that do not cause signal emission,
68  * but do cause a failure. Not sure what these would be.
69  *
70  * The other option is that there are threads that modify the GUI.
71  * This IS A BUG IN THE PROGRAM. But it may happen. If seeing very 
72  * odd bugs change this to take the GDK lock. Just to make sure.
73  */
74
75 static GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
76
77 static guint counter = 1;
78
79 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
80
81 /*---------------------------------------------------------------------------*/
82
83 static GStaticMutex   recursion_check_guard = G_STATIC_MUTEX_INIT;
84 static gboolean       recursion_check = FALSE;
85
86 static gboolean
87 recursion_check_and_set ()
88 {
89   gboolean ret;
90   g_static_mutex_lock   (&recursion_check_guard);
91   ret = recursion_check;
92   recursion_check = TRUE;
93   g_static_mutex_unlock (&recursion_check_guard);
94   return ret;
95 }
96
97 static void
98 recursion_check_unset ()
99 {
100   g_static_mutex_lock   (&recursion_check_guard);
101   recursion_check = FALSE;
102   g_static_mutex_unlock (&recursion_check_guard);
103 }
104
105 /*---------------------------------------------------------------------------*/
106
107 /*
108  * Each AtkObject must be asssigned a D-Bus path (Reference)
109  *
110  * This function provides an integer reference for a new
111  * AtkObject.
112  */
113 static guint
114 assign_reference(void)
115 {
116   counter++;
117   /* Reference of 0 not allowed as used as direct key in hash table */
118   if (counter == 0)
119     counter++;
120 }
121
122 /*
123  * Returns the reference of the object, or 0 if it is not registered.
124  */
125 static guint
126 object_to_ref (AtkObject *accessible)
127 {
128   return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
129 }
130
131 /*
132  * Converts the Accessible object reference to its D-Bus object path
133  */
134 static gchar *
135 ref_to_path (guint ref)
136 {
137   return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, ref);
138 }
139
140 /*---------------------------------------------------------------------------*/
141
142 /*
143  * Callback for when a registered AtkObject is destroyed.
144  *
145  * Removes the AtkObject from the reference lookup tables, meaning
146  * it is no longer exposed over D-Bus.
147  */
148 static void
149 deregister_accessible (gpointer data, GObject *accessible)
150 {
151   guint ref;
152   g_assert (ATK_IS_OBJECT (accessible));
153
154   ref = object_to_ref (ATK_OBJECT(accessible));
155   if (ref != 0)
156     {
157       g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
158     }
159 }
160
161 /*
162  * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
163  */
164 static void
165 register_accessible (AtkObject *accessible)
166 {
167   guint ref;
168   g_assert(ATK_IS_OBJECT(accessible));
169
170   ref = assign_reference();
171
172   g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), accessible);
173   g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(ref));
174   g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
175 }
176
177 /*---------------------------------------------------------------------------*/
178
179 #ifdef SPI_ATK_DEBUG
180 /*
181  * This function checks that the ref-count of an accessible
182  * is greater than 1.
183  *
184  * There is not currently any remote reference counting
185  * in AT-SPI D-Bus so objects that are remotely owned are not
186  * allowed.
187  *
188  * TODO Add debug wrapper
189  */
190 static gboolean
191 non_owned_accessible (AtkObject *accessible)
192 {
193    if ((G_OBJECT (accessible))->ref_count <= 1)
194      {
195        g_warning ("AT-SPI: Child referenced that is not owned by its parent");
196        return TRUE;
197      }
198    else
199      {
200        return FALSE;
201      }
202 }
203 #endif /* SPI_ATK_DEBUG */
204
205 /*---------------------------------------------------------------------------*/
206
207 static gboolean
208 has_manages_descendants (AtkObject *accessible)
209 {
210    AtkStateSet *state;
211    gboolean result = FALSE;
212
213    /* This is dangerous, refing the state set
214     * seems to do wierd things to the tree & cause recursion
215     * by modifying the tree alot.
216     */
217    state = atk_object_ref_state_set (accessible);
218    if (atk_state_set_contains_state (state, ATK_STATE_MANAGES_DESCENDANTS))
219      {
220 #ifdef SPI_ATK_DEBUG
221        g_warning ("AT-SPI: Object with 'Manages descendants' states not currently handled by AT-SPI");
222 #endif
223        result = TRUE;
224      }
225    g_object_unref (state);
226
227    return result;
228 }
229
230 /*
231  * Registers a subtree of accessible objects
232  * rooted at the accessible object provided.
233  *
234  * The leaf nodes do not have their children
235  * registered. A node is considered a leaf
236  * if it has the state "manages-descendants"
237  * or if it has already been registered.
238  */
239 void
240 register_subtree (AtkObject *accessible)
241 {
242   AtkObject *current, *tmp;
243   GQueue    *stack;
244   guint      i;
245   gboolean   recurse;
246
247
248   current = g_object_ref (accessible);
249   if (has_manages_descendants (current))
250     {
251       g_object_unref (current);
252       return;
253     }
254
255   stack = g_queue_new ();
256
257   register_accessible (current);
258   g_queue_push_head (stack, GINT_TO_POINTER (0));
259
260   /*
261    * The index held on the stack is the next child node
262    * that needs processing at the corresponding level in the tree.
263    */
264   while (!g_queue_is_empty (stack))
265     {
266       /* Find the next child node that needs processing */
267
268       i = GPOINTER_TO_INT(g_queue_peek_head (stack));
269       recurse = FALSE;
270
271       while (i < atk_object_get_n_accessible_children (current) &&
272              recurse == FALSE)
273         {
274           tmp = atk_object_ref_accessible_child (current, i);
275
276 #ifdef SPI_ATK_DEBUG
277           non_owned_accessible (tmp);
278 #endif
279
280           if (object_to_ref (tmp))
281             {
282               /* If its already registered, just update */
283               spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
284             }
285           else if (has_manages_descendants (tmp))
286             {
287               /* If it has manages descendants, just register and update */
288               register_accessible (tmp);
289               spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
290             }
291           else
292             {
293               recurse = TRUE;
294             }
295
296           if (!recurse)
297             {
298               g_object_unref (G_OBJECT (tmp));
299             }
300
301           i++;
302         }
303
304       if (recurse)
305         {
306           /* Push onto stack */
307           current = tmp;
308           register_accessible (current);
309
310           g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i);
311           g_queue_push_head (stack, GINT_TO_POINTER (0));
312         }
313       else
314         {
315           /* Pop from stack */
316           spi_emit_cache_update (current, atk_adaptor_app_data->bus);
317           tmp = current;
318           current = atk_object_get_parent (current);
319           g_object_unref (G_OBJECT (tmp));
320           g_queue_pop_head (stack);
321         }
322     }
323     g_queue_free (stack);
324 }
325
326 /*---------------------------------------------------------------------------*/
327
328 /*
329  * Called when an already registered object is updated in such a
330  * way that client side cache needs to be updated.
331  */
332 static void
333 update_accessible (AtkObject *accessible)
334 {
335   guint  ref = 0;
336   g_assert(ATK_IS_OBJECT(accessible));
337
338   ref = object_to_ref (accessible);
339   if (ref)
340     {
341       spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
342     }
343 }
344
345 /*---------------------------------------------------------------------------*/
346
347 void
348 atk_dbus_foreach_registered(GHFunc func, gpointer data)
349 {
350   g_hash_table_foreach(ref2ptr, func, data);
351 }
352
353 /*
354  * Used to lookup an AtkObject from its D-Bus path.
355  */
356 AtkObject *
357 atk_dbus_path_to_object (const char *path)
358 {
359   guint index;
360   void *data;
361
362   g_assert (path);
363
364   if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0) 
365     return NULL;
366
367   path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
368
369   if (path[0] == '\0')
370      return atk_get_root();
371   if (path[0] != '/')
372      return NULL;
373
374   path++;
375   index = atoi (path);
376   data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
377   if (data)
378     return ATK_OBJECT (data);
379   else
380     return NULL;
381 }
382
383 /*
384  * Used to lookup a D-Bus path from the AtkObject.
385  */
386 gchar *
387 atk_dbus_object_to_path (AtkObject *accessible)
388 {
389   guint ref;
390
391   ref = object_to_ref (accessible);
392   if (!ref)
393       return NULL;
394   else
395       return ref_to_path (ref);
396 }
397
398 /*---------------------------------------------------------------------------*/
399
400 /*
401  * Events are not evaluated for non-registered accessibles.
402  *
403  * When a property change is made on a registered accessible
404  * the client side cache should be updated.
405  *
406  * When a parent is changed the subtree is de-registered
407  * if the parent is not attached to the root accessible.
408  */
409 static gboolean
410 tree_update_listener (GSignalInvocationHint *signal_hint,
411                       guint                  n_param_values,
412                       const GValue          *param_values,
413                       gpointer               data)
414 {
415   AtkObject *accessible;
416   AtkPropertyValues *values;
417   const gchar *pname = NULL;
418
419   g_static_rec_mutex_lock (&registration_mutex);
420
421   /* Ensure that only registered accessibles
422    * have their signals processed.
423    */
424   accessible = g_value_get_object (&param_values[0]);
425   g_assert (ATK_IS_OBJECT (accessible));
426
427   if (object_to_ref (accessible))
428     {
429 #ifdef SPI_ATK_DEBUG
430       if (recursion_check_and_set ())
431           g_warning ("AT-SPI: Recursive use of registration module");
432 #endif
433
434       values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
435       pname = values[0].property_name;
436       if (strcmp (pname, "accessible-name") == 0 ||
437           strcmp (pname, "accessible-description") == 0)
438         {
439           update_accessible (accessible);
440         }
441       /* Parent value us updated by child-add signal of parent object */
442
443       recursion_check_unset ();
444     }
445
446   g_static_rec_mutex_unlock (&registration_mutex);
447
448   return TRUE;
449 }
450
451 /*
452  * Events are not evaluated for non registered accessibles.
453  *
454  * When the children of a registered accessible are changed
455  * the subtree, rooted at the child is registered.
456  */
457 static gboolean
458 tree_update_children_listener (GSignalInvocationHint *signal_hint,
459                                guint                  n_param_values,
460                                const GValue          *param_values,
461                                gpointer               data)
462 {
463   AtkObject *accessible;
464   const gchar *detail = NULL;
465   AtkObject *child;
466
467   g_static_rec_mutex_lock (&registration_mutex);
468
469   /* Ensure that only registered accessibles
470    * have their signals processed.
471    */
472   accessible = g_value_get_object (&param_values[0]);
473   g_assert (ATK_IS_OBJECT (accessible));
474
475   if (object_to_ref (accessible))
476     {
477 #ifdef SPI_ATK_DEBUG
478       if (recursion_check_and_set ())
479           g_warning ("AT-SPI: Recursive use of registration module");
480 #endif
481
482       if (signal_hint->detail)
483           detail = g_quark_to_string (signal_hint->detail);
484
485       if (!strcmp (detail, "add"))
486         {
487           gpointer child;
488           int index = g_value_get_uint (param_values + 1);
489           child = g_value_get_pointer (param_values + 2);
490
491           if (!ATK_IS_OBJECT (child))
492             {
493               child = atk_object_ref_accessible_child (accessible, index);
494 #ifdef SPI_ATK_DEBUG
495               non_owned_accessible (child);
496 #endif
497             }
498           register_subtree (child);
499         }
500
501       recursion_check_unset ();
502     }
503
504   g_static_rec_mutex_unlock (&registration_mutex);
505
506   return TRUE;
507 }
508
509 /*
510  * Initializes required global data. The update and removal lists
511  * and the reference lookup tables.
512  *
513  * Initializes all of the required D-Bus interfaces.
514  */
515 void
516 atk_dbus_initialize (AtkObject *root)
517 {
518   if (!ref2ptr)
519     ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
520
521 #ifdef SPI_ATK_DEBUG
522   if (g_thread_supported ())
523       g_message ("AT-SPI: Threads enabled");
524 #endif
525
526   register_subtree (root);
527
528   atk_add_global_event_listener (tree_update_listener, "Gtk:AtkObject:property-change");
529   atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
530 }
531
532 /*END------------------------------------------------------------------------*/
533