Fix object lifecycle errors.
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / accessible-cache.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2009, 2010 Codethink Ltd.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <atk/atk.h>
24
25 #include "accessible-cache.h"
26 #include "accessible-register.h"
27 #include "bridge.h"
28
29 SpiCache *spi_global_cache = NULL;
30
31 static gboolean
32 child_added_listener (GSignalInvocationHint * signal_hint,
33                       guint n_param_values,
34                       const GValue * param_values, gpointer data);
35
36 static void
37 toplevel_added_listener (AtkObject * accessible,
38                          guint index, AtkObject * child);
39
40 static void
41 remove_object (GObject * source, GObject * gobj, gpointer data);
42
43 static void
44 add_object (SpiCache * cache, GObject * gobj);
45
46 static void
47 add_subtree (SpiCache *cache, AtkObject * accessible);
48
49 /*---------------------------------------------------------------------------*/
50
51 static void
52 spi_cache_finalize (GObject * object);
53
54 static void
55 spi_cache_dispose (GObject * object);
56
57 /*---------------------------------------------------------------------------*/
58
59 enum
60 {
61   OBJECT_ADDED,
62   OBJECT_REMOVED,
63   LAST_SIGNAL
64 };
65 static guint cache_signals[LAST_SIGNAL] = { 0 };
66
67 /*---------------------------------------------------------------------------*/
68
69 G_DEFINE_TYPE (SpiCache, spi_cache, G_TYPE_OBJECT)
70
71 static void spi_cache_class_init (SpiCacheClass * klass)
72 {
73   GObjectClass *object_class = (GObjectClass *) klass;
74
75   spi_cache_parent_class = g_type_class_ref (G_TYPE_OBJECT);
76
77   object_class->finalize = spi_cache_finalize;
78   object_class->dispose = spi_cache_dispose;
79
80   cache_signals [OBJECT_ADDED] = \
81       g_signal_new ("object-added",
82                     SPI_CACHE_TYPE,
83                     G_SIGNAL_ACTION,
84                     0,
85                     NULL,
86                     NULL,
87                     g_cclosure_marshal_VOID__OBJECT,
88                     G_TYPE_NONE,
89                     1,
90                     G_TYPE_OBJECT);
91
92   cache_signals [OBJECT_REMOVED] = \
93       g_signal_new ("object-removed",
94                     SPI_CACHE_TYPE,
95                     G_SIGNAL_ACTION,
96                     0,
97                     NULL,
98                     NULL,
99                     g_cclosure_marshal_VOID__OBJECT,
100                     G_TYPE_NONE,
101                     1,
102                     G_TYPE_OBJECT);
103 }
104
105 static void
106 spi_cache_init (SpiCache * cache)
107 {
108   cache->objects = g_hash_table_new (g_direct_hash, g_direct_equal);
109
110 #ifdef SPI_ATK_DEBUG
111   if (g_thread_supported ())
112     g_message ("AT-SPI: Threads enabled");
113
114   g_debug ("AT-SPI: Initial Atk tree regisration");
115 #endif
116
117   g_signal_connect (spi_global_register,
118                     "object-deregistered",
119                     (GCallback) remove_object, cache);
120
121   add_subtree (cache, spi_global_app_data->root);
122
123   atk_add_global_event_listener (child_added_listener,
124                                  "Gtk:AtkObject:children-changed");
125
126   g_signal_connect (G_OBJECT (spi_global_app_data->root),
127                     "children-changed::add",
128                     (GCallback) toplevel_added_listener, NULL);
129 }
130
131 static void
132 spi_cache_finalize (GObject * object)
133 {
134   SpiCache *cache = SPI_CACHE (object);
135
136   g_free (cache->objects);
137
138   G_OBJECT_CLASS (spi_cache_parent_class)->finalize (object);
139 }
140
141 static void
142 spi_cache_dispose (GObject * object)
143 {
144   SpiCache *cache = SPI_CACHE (object);
145
146   G_OBJECT_CLASS (spi_cache_parent_class)->dispose (object);
147 }
148
149 /*---------------------------------------------------------------------------*/
150
151 static void
152 remove_object (GObject * source, GObject * gobj, gpointer data)
153 {
154   SpiCache *cache = SPI_CACHE (data);
155   
156   if (spi_cache_in (cache, gobj))
157     {
158       g_signal_emit (cache, cache_signals [OBJECT_REMOVED], 0, gobj);
159       g_hash_table_remove (cache->objects, gobj);
160     }
161 }
162
163 static void
164 add_object (SpiCache * cache, GObject * gobj)
165 {
166   g_return_if_fail (G_IS_OBJECT (gobj));
167
168   g_hash_table_insert (cache->objects, gobj, NULL);
169
170 #ifdef SPI_ATK_DEBUG
171   g_debug ("CACHE  - %s - %d - %s", atk_object_get_name (ATK_OBJECT (gobj)),
172             atk_object_get_role (ATK_OBJECT (gobj)),
173             spi_register_object_to_path (spi_global_register, gobj));
174 #endif
175
176   g_signal_emit (cache, cache_signals [OBJECT_ADDED], 0, gobj);
177 }
178
179 /*---------------------------------------------------------------------------*/
180
181 static GStaticRecMutex cache_mutex        = G_STATIC_REC_MUTEX_INIT;
182 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
183
184 static gboolean recursion_check = FALSE;
185
186 static gboolean
187 recursion_check_and_set ()
188 {
189   gboolean ret;
190   g_static_mutex_lock (&recursion_check_guard);
191   ret = recursion_check;
192   recursion_check = TRUE;
193   g_static_mutex_unlock (&recursion_check_guard);
194   return ret;
195 }
196
197 static void
198 recursion_check_unset ()
199 {
200   g_static_mutex_lock (&recursion_check_guard);
201   recursion_check = FALSE;
202   g_static_mutex_unlock (&recursion_check_guard);
203 }
204
205 /*---------------------------------------------------------------------------*/
206
207 static void
208 append_children (AtkObject * accessible, GQueue * traversal)
209 {
210   AtkObject *current;
211   guint i;
212   gint count = atk_object_get_n_accessible_children (accessible);
213
214   if (count < 0)
215     count = 0;
216   for (i = 0; i < count; i++)
217     {
218       current = atk_object_ref_accessible_child (accessible, i);
219       if (current)
220         {
221           g_queue_push_tail (traversal, current);
222         }
223     }
224 }
225
226 /*
227  * Adds a subtree of accessible objects
228  * to the cache at the accessible object provided.
229  *
230  * The leaf nodes do not have their children
231  * registered. A node is considered a leaf
232  * if it has the state "manages-descendants"
233  * or if it has already been registered.
234  */
235 static void
236 add_subtree (SpiCache *cache, AtkObject * accessible)
237 {
238   AtkObject *current;
239   GQueue *traversal;
240   GQueue *to_add;
241
242   g_return_if_fail (ATK_IS_OBJECT (accessible));
243
244   traversal = g_queue_new ();
245   to_add = g_queue_new ();
246
247   g_object_ref (accessible);
248   g_queue_push_tail (traversal, accessible);
249
250   while (!g_queue_is_empty (traversal))
251     {
252       AtkStateSet *set;
253       
254       current = g_queue_pop_head (traversal);
255       set = atk_object_ref_state_set (current);
256
257       if (!atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
258         {
259           g_queue_push_tail (to_add, current);
260           if (!spi_cache_in (cache, G_OBJECT (current)) &&
261               !atk_state_set_contains_state  (set, ATK_STATE_MANAGES_DESCENDANTS))
262             {
263               append_children (current, traversal);
264             }
265         }
266
267       g_object_unref (set);
268     }
269
270   while (!g_queue_is_empty (to_add))
271     {
272       current = g_queue_pop_head (to_add);
273       add_object (cache, G_OBJECT(current));
274       g_object_unref (G_OBJECT (current));
275     }
276
277   g_queue_free (traversal);
278   g_queue_free (to_add);
279 }
280
281 /*---------------------------------------------------------------------------*/
282
283 static gboolean
284 child_added_listener (GSignalInvocationHint * signal_hint,
285                       guint n_param_values,
286                       const GValue * param_values, gpointer data)
287 {
288   SpiCache *cache = spi_global_cache;
289
290   AtkObject *accessible;
291   AtkObject *child;
292
293   const gchar *detail = NULL;
294
295   g_static_rec_mutex_lock (&cache_mutex);
296
297   /* 
298    * Ensure that only accessibles already in the cache
299    * have their signals processed.
300    */
301   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
302   g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
303
304   if (spi_cache_in (cache, G_OBJECT(accessible)))
305     {
306 #ifdef SPI_ATK_DEBUG
307       if (recursion_check_and_set ())
308         g_warning ("AT-SPI: Recursive use of cache module");
309
310       g_debug ("AT-SPI: Tree update listener");
311 #endif
312       if (signal_hint->detail)
313         detail = g_quark_to_string (signal_hint->detail);
314
315       if (!strcmp (detail, "add"))
316         {
317           gpointer child;
318           int index = g_value_get_uint (param_values + 1);
319           child = g_value_get_pointer (param_values + 2);
320
321           if (!ATK_IS_OBJECT (child))
322            {
323              child = atk_object_ref_accessible_child (accessible, index);
324            }
325           add_subtree (cache, child);
326         }
327 #ifdef SPI_ATK_DEBUG
328       recursion_check_unset ();
329 #endif
330     }
331
332   g_static_rec_mutex_unlock (&cache_mutex);
333
334   return TRUE;
335 }
336
337 /*---------------------------------------------------------------------------*/
338
339 static void
340 toplevel_added_listener (AtkObject * accessible,
341                          guint index, AtkObject * child)
342 {
343   SpiCache *cache = spi_global_cache;
344
345   g_static_rec_mutex_lock (&cache_mutex);
346
347   g_return_if_fail (ATK_IS_OBJECT (accessible));
348
349   if (spi_cache_in (cache, G_OBJECT(accessible)))
350     {
351 #ifdef SPI_ATK_DEBUG
352       if (recursion_check_and_set ())
353         g_warning ("AT-SPI: Recursive use of registration module");
354
355       g_debug ("AT-SPI: Toplevel added listener");
356 #endif
357       if (!ATK_IS_OBJECT (child))
358         {
359           child = atk_object_ref_accessible_child (accessible, index);
360         }
361       add_subtree (cache, child);
362 #ifdef SPI_ATK_DEBUG
363       recursion_check_unset ();
364 #endif
365     }
366
367   g_static_rec_mutex_unlock (&cache_mutex);
368 }
369
370 /*---------------------------------------------------------------------------*/
371
372 void
373 spi_cache_foreach (SpiCache * cache, GHFunc func, gpointer data)
374 {
375   g_hash_table_foreach (cache->objects, func, data);
376 }
377
378 gboolean
379 spi_cache_in (SpiCache * cache, GObject * object)
380 {
381   if (g_hash_table_lookup_extended (cache->objects,
382                                     object,
383                                     NULL,
384                                     NULL))
385     return TRUE;
386   else
387     return FALSE;
388 }
389
390 #ifdef SPI_ATK_DEBUG
391 void
392 spi_cache_print_info (GObject * obj)
393 {
394   char * path = spi_register_object_to_path (spi_global_register, obj);
395  
396   if (spi_cache_in (spi_global_cache, obj))
397       g_printf ("%s IC\n", path);
398   else
399       g_printf ("%s NC\n", path);
400
401   if (path)
402       g_free (path);
403 }
404 #endif
405
406 /*END------------------------------------------------------------------------*/