Fix an errant g_object_unref
[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 #include <string.h>
25
26 #include "accessible-cache.h"
27 #include "accessible-register.h"
28 #include "bridge.h"
29
30 SpiCache *spi_global_cache = NULL;
31
32 static gboolean
33 child_added_listener (GSignalInvocationHint * signal_hint,
34                       guint n_param_values,
35                       const GValue * param_values, gpointer data);
36
37 static void
38 toplevel_added_listener (AtkObject * accessible,
39                          guint index, AtkObject * child);
40
41 static void
42 remove_object (GObject * source, GObject * gobj, gpointer data);
43
44 static void
45 add_object (SpiCache * cache, GObject * gobj);
46
47 static void
48 add_subtree (SpiCache *cache, AtkObject * accessible);
49
50 static gboolean
51 add_pending_items (gpointer data);
52
53 /*---------------------------------------------------------------------------*/
54
55 static void
56 spi_cache_finalize (GObject * object);
57
58 static void
59 spi_cache_dispose (GObject * object);
60
61 /*---------------------------------------------------------------------------*/
62
63 enum
64 {
65   OBJECT_ADDED,
66   OBJECT_REMOVED,
67   LAST_SIGNAL
68 };
69 static guint cache_signals[LAST_SIGNAL] = { 0 };
70
71 /*---------------------------------------------------------------------------*/
72
73 G_DEFINE_TYPE (SpiCache, spi_cache, G_TYPE_OBJECT)
74
75 static void spi_cache_class_init (SpiCacheClass * klass)
76 {
77   GObjectClass *object_class = (GObjectClass *) klass;
78
79   spi_cache_parent_class = g_type_class_ref (G_TYPE_OBJECT);
80
81   object_class->finalize = spi_cache_finalize;
82   object_class->dispose = spi_cache_dispose;
83
84   cache_signals [OBJECT_ADDED] = \
85       g_signal_new ("object-added",
86                     SPI_CACHE_TYPE,
87                     G_SIGNAL_ACTION,
88                     0,
89                     NULL,
90                     NULL,
91                     g_cclosure_marshal_VOID__OBJECT,
92                     G_TYPE_NONE,
93                     1,
94                     G_TYPE_OBJECT);
95
96   cache_signals [OBJECT_REMOVED] = \
97       g_signal_new ("object-removed",
98                     SPI_CACHE_TYPE,
99                     G_SIGNAL_ACTION,
100                     0,
101                     NULL,
102                     NULL,
103                     g_cclosure_marshal_VOID__OBJECT,
104                     G_TYPE_NONE,
105                     1,
106                     G_TYPE_OBJECT);
107 }
108
109 static void
110 spi_cache_init (SpiCache * cache)
111 {
112   cache->objects = g_hash_table_new (g_direct_hash, g_direct_equal);
113   cache->add_traversal = g_queue_new ();
114
115 #ifdef SPI_ATK_DEBUG
116   if (g_thread_supported ())
117     g_message ("AT-SPI: Threads enabled");
118
119   g_debug ("AT-SPI: Initial Atk tree regisration");
120 #endif
121
122   g_signal_connect (spi_global_register,
123                     "object-deregistered",
124                     (GCallback) remove_object, cache);
125
126   add_subtree (cache, spi_global_app_data->root);
127
128   cache->child_added_listener = atk_add_global_event_listener (child_added_listener,
129                                                                "Gtk:AtkObject:children-changed"); 
130
131   g_signal_connect (G_OBJECT (spi_global_app_data->root),
132                     "children-changed::add",
133                     (GCallback) toplevel_added_listener, NULL);
134 }
135
136 static void
137 spi_cache_finalize (GObject * object)
138 {
139   SpiCache *cache = SPI_CACHE (object);
140
141   while (!g_queue_is_empty (cache->add_traversal))
142     g_object_unref (G_OBJECT (g_queue_pop_head (cache->add_traversal)));
143   g_queue_free (cache->add_traversal);
144   g_hash_table_unref (cache->objects);
145
146   g_signal_handlers_disconnect_by_func (spi_global_register,
147                                         (GCallback) remove_object, cache);
148
149   g_signal_handlers_disconnect_by_func (G_OBJECT (spi_global_app_data->root),
150                                         (GCallback) toplevel_added_listener, NULL);
151
152   atk_remove_global_event_listener (cache->child_added_listener);
153
154   G_OBJECT_CLASS (spi_cache_parent_class)->finalize (object);
155 }
156
157 static void
158 spi_cache_dispose (GObject * object)
159 {
160   SpiCache *cache = SPI_CACHE (object);
161
162   G_OBJECT_CLASS (spi_cache_parent_class)->dispose (object);
163 }
164
165 /*---------------------------------------------------------------------------*/
166
167 static void
168 remove_object (GObject * source, GObject * gobj, gpointer data)
169 {
170   SpiCache *cache = SPI_CACHE (data);
171   
172   if (spi_cache_in (cache, gobj))
173     {
174 #ifdef SPI_ATK_DEBUG
175   g_debug ("CACHE REM - %s - %d - %s\n", atk_object_get_name (ATK_OBJECT (gobj)),
176             atk_object_get_role (ATK_OBJECT (gobj)),
177             spi_register_object_to_path (spi_global_register, gobj));
178 #endif
179       g_signal_emit (cache, cache_signals [OBJECT_REMOVED], 0, gobj);
180       g_hash_table_remove (cache->objects, gobj);
181     }
182   else if (g_queue_remove (cache->add_traversal, gobj))
183     {
184       g_object_unref (gobj);
185     }
186 }
187
188 static void
189 add_object (SpiCache * cache, GObject * gobj)
190 {
191   g_return_if_fail (G_IS_OBJECT (gobj));
192
193   g_hash_table_insert (cache->objects, gobj, NULL);
194
195 #ifdef SPI_ATK_DEBUG
196   g_debug ("CACHE ADD - %s - %d - %s\n", atk_object_get_name (ATK_OBJECT (gobj)),
197             atk_object_get_role (ATK_OBJECT (gobj)),
198             spi_register_object_to_path (spi_global_register, gobj));
199 #endif
200
201   g_signal_emit (cache, cache_signals [OBJECT_ADDED], 0, gobj);
202 }
203
204 /*---------------------------------------------------------------------------*/
205
206 static GStaticRecMutex cache_mutex        = G_STATIC_REC_MUTEX_INIT;
207 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
208
209 static gboolean recursion_check = FALSE;
210
211 static gboolean
212 recursion_check_and_set ()
213 {
214   gboolean ret;
215   g_static_mutex_lock (&recursion_check_guard);
216   ret = recursion_check;
217   recursion_check = TRUE;
218   g_static_mutex_unlock (&recursion_check_guard);
219   return ret;
220 }
221
222 static void
223 recursion_check_unset ()
224 {
225   g_static_mutex_lock (&recursion_check_guard);
226   recursion_check = FALSE;
227   g_static_mutex_unlock (&recursion_check_guard);
228 }
229
230 /*---------------------------------------------------------------------------*/
231
232 static void
233 append_children (AtkObject * accessible, GQueue * traversal)
234 {
235   AtkObject *current;
236   guint i;
237   gint count = atk_object_get_n_accessible_children (accessible);
238
239   if (count < 0)
240     count = 0;
241   for (i = 0; i < count; i++)
242     {
243       current = atk_object_ref_accessible_child (accessible, i);
244       if (current)
245         {
246           g_queue_push_tail (traversal, current);
247         }
248     }
249 }
250
251 /*
252  * Adds a subtree of accessible objects
253  * to the cache at the accessible object provided.
254  *
255  * The leaf nodes do not have their children
256  * registered. A node is considered a leaf
257  * if it has the state "manages-descendants"
258  * or if it has already been registered.
259  */
260 static void
261 add_subtree (SpiCache *cache, AtkObject * accessible)
262 {
263   g_return_if_fail (ATK_IS_OBJECT (accessible));
264
265   g_object_ref (accessible);
266   g_queue_push_tail (cache->add_traversal, accessible);
267   add_pending_items (cache);
268 }
269
270 static gboolean
271 add_pending_items (gpointer data)
272 {
273   SpiCache *cache = SPI_CACHE (data);
274   AtkObject *current;
275   GQueue *to_add;
276
277   to_add = g_queue_new ();
278
279   while (!g_queue_is_empty (cache->add_traversal))
280     {
281       AtkStateSet *set;
282
283       /* cache->add_traversal holds a ref to current */
284       current = g_queue_pop_head (cache->add_traversal);
285       set = atk_object_ref_state_set (current);
286
287       if (set && !atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
288         {
289           /* transfer the ref into to_add */
290           g_queue_push_tail (to_add, current);
291           if (!spi_cache_in (cache, G_OBJECT (current)) &&
292               !atk_state_set_contains_state  (set, ATK_STATE_MANAGES_DESCENDANTS) &&
293               !atk_state_set_contains_state  (set, ATK_STATE_DEFUNCT))
294             {
295               append_children (current, cache->add_traversal);
296             }
297         }
298       else
299         {
300           /* drop the ref for the removed object */
301           g_object_unref (current);
302         }
303
304       if (set)
305         g_object_unref (set);
306     }
307
308   while (!g_queue_is_empty (to_add))
309     {
310       current = g_queue_pop_head (to_add);
311
312       /* Make sure object is registerd so we are notified if it goes away */
313       g_free (spi_register_object_to_path (spi_global_register,
314               G_OBJECT (current)));
315
316       add_object (cache, G_OBJECT(current));
317       g_object_unref (G_OBJECT (current));
318     }
319
320   g_queue_free (to_add);
321   cache->add_pending_idle = 0;
322   return FALSE;
323 }
324
325 /*---------------------------------------------------------------------------*/
326
327 static gboolean
328 child_added_listener (GSignalInvocationHint * signal_hint,
329                       guint n_param_values,
330                       const GValue * param_values, gpointer data)
331 {
332   SpiCache *cache = spi_global_cache;
333   AtkObject *accessible;
334
335   const gchar *detail = NULL;
336
337   g_static_rec_mutex_lock (&cache_mutex);
338
339   /* 
340    * Ensure that only accessibles already in the cache
341    * have their signals processed.
342    */
343   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
344   g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
345
346   if (spi_cache_in (cache, G_OBJECT(accessible)))
347     {
348 #ifdef SPI_ATK_DEBUG
349       if (recursion_check_and_set ())
350         g_warning ("AT-SPI: Recursive use of cache module");
351
352       g_debug ("AT-SPI: Tree update listener");
353 #endif
354       if (signal_hint->detail)
355         detail = g_quark_to_string (signal_hint->detail);
356
357       if (detail && !strncmp (detail, "add", 3))
358         {
359           gpointer child;
360           int index = g_value_get_uint (param_values + 1);
361           child = g_value_get_pointer (param_values + 2);
362           if (!child)
363             {
364               g_static_rec_mutex_unlock (&cache_mutex);
365               return;
366             }
367
368           g_object_ref (child);
369           g_queue_push_tail (cache->add_traversal, child);
370
371           if (cache->add_pending_idle == 0)
372             cache->add_pending_idle = g_idle_add (add_pending_items, cache);
373         }
374 #ifdef SPI_ATK_DEBUG
375       recursion_check_unset ();
376 #endif
377     }
378
379   g_static_rec_mutex_unlock (&cache_mutex);
380
381   return TRUE;
382 }
383
384 /*---------------------------------------------------------------------------*/
385
386 static void
387 toplevel_added_listener (AtkObject * accessible,
388                          guint index, AtkObject * child)
389 {
390   SpiCache *cache = spi_global_cache;
391
392   g_static_rec_mutex_lock (&cache_mutex);
393
394   g_return_if_fail (ATK_IS_OBJECT (accessible));
395
396   if (spi_cache_in (cache, G_OBJECT(accessible)))
397     {
398 #ifdef SPI_ATK_DEBUG
399       if (recursion_check_and_set ())
400         g_warning ("AT-SPI: Recursive use of registration module");
401
402       g_debug ("AT-SPI: Toplevel added listener");
403 #endif
404       if (!ATK_IS_OBJECT (child))
405         {
406           child = atk_object_ref_accessible_child (accessible, index);
407         }
408       else
409         g_object_ref (child);
410
411       g_queue_push_tail (cache->add_traversal, child);
412
413       if (cache->add_pending_idle == 0)
414         cache->add_pending_idle = g_idle_add (add_pending_items, cache);
415 #ifdef SPI_ATK_DEBUG
416       recursion_check_unset ();
417 #endif
418     }
419
420   g_static_rec_mutex_unlock (&cache_mutex);
421 }
422
423 /*---------------------------------------------------------------------------*/
424
425 void
426 spi_cache_foreach (SpiCache * cache, GHFunc func, gpointer data)
427 {
428   g_hash_table_foreach (cache->objects, func, data);
429 }
430
431 gboolean
432 spi_cache_in (SpiCache * cache, GObject * object)
433 {
434   if (!cache)
435     return FALSE;
436
437   if (g_hash_table_lookup_extended (cache->objects,
438                                     object,
439                                     NULL,
440                                     NULL))
441     return TRUE;
442   else
443     return FALSE;
444 }
445
446 #ifdef SPI_ATK_DEBUG
447 void
448 spi_cache_print_info (GObject * obj)
449 {
450   char * path = spi_register_object_to_path (spi_global_register, obj);
451  
452   if (spi_cache_in (spi_global_cache, obj))
453       g_printf ("%s IC\n", path);
454   else
455       g_printf ("%s NC\n", path);
456
457   if (path)
458       g_free (path);
459 }
460 #endif
461
462 /*END------------------------------------------------------------------------*/