summaryrefslogtreecommitdiff
path: root/libnautilus/nautilus-bonobo-workarounds.c
blob: 082ab6b4e3608d261c0513b5045707fb83fe7639 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: 8; c-basic-offset: 8 -*- */

/*
 *  libnautilus: A library for nautilus view implementations.
 *
 *  Copyright (C) 2000, 2001 Eazel, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Author: Darin Adler <darin@bentspoon.com>
 *
 */

#include <config.h>
#include "nautilus-bonobo-workarounds.h"

#include <bonobo/bonobo-stream.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>

#define RELY_ON_BONOBO_INTERNALS

#ifdef RELY_ON_BONOBO_INTERNALS

/* This is incredibly unsafe and relies on details of the Bonobo
 * internals. I should be shot and killed for even thinking of doing
 * this.
 */
typedef struct {
	int   ref_count;
	GList *objs;
} StartOfBonoboAggregateObject;

typedef struct {
	StartOfBonoboAggregateObject *ao;
	int destroy_id;
} StartOfBonoboObjectPrivate;

#endif

/* By waiting a minute before self-destructing, we minimize the race
 * conditions that can occur if we self-destruct right when we notice
 * an X window going away.
 */
#define MINIMIZE_RACE_CONDITIONS_DELAY 60000

/* FIXME bugzilla.gnome.org 42456: Is a hard-coded 20 seconds wait to
 * detect that a remote object's process is hung acceptable? Can a
 * component that is working still take 20 seconds to respond?
 */
/* in milliseconds */
#define REMOTE_CHECK_TIME_INTERVAL 20000
#define REMOTE_CHECK_DATA_KEY "nautilus-bonobo-workarounds/RemoteCheckData"

typedef struct {
	BonoboObject *object;
	Bonobo_Unknown remote_object;
	guint timeout_id;
	guint destroy_handler_id;
	NautilusBonoboObjectCallback function;
	gpointer callback_data;
} RemoteCheckData;

typedef struct {
	BonoboObject *object;
	guint timeout_id;
	guint destroy_handler_id;
} DestroyLaterData;

POA_Bonobo_Unknown__epv *
nautilus_bonobo_object_get_epv (void)
{
	static POA_Bonobo_Unknown__epv bonobo_object_epv;
	static gboolean set_up;
	POA_Bonobo_Unknown__epv *epv;

	/* Make our own copy. */
 	if (!set_up) {
		epv = bonobo_object_get_epv ();
		bonobo_object_epv = *epv;
		g_free (epv);
		set_up = TRUE;
	}

	return &bonobo_object_epv;
}

POA_Bonobo_Stream__epv *
nautilus_bonobo_stream_get_epv (void)
{
	static POA_Bonobo_Stream__epv bonobo_stream_epv;
	static gboolean set_up;
	POA_Bonobo_Stream__epv *epv;

	/* Make our own copy. */
 	if (!set_up) {
		epv = bonobo_stream_get_epv ();
		bonobo_stream_epv = *epv;
		g_free (epv);
		set_up = TRUE;
	}

	return &bonobo_stream_epv;
}

/* The following is the most evil function in the world. But on the
 * other hand, it works and prevents us from having tons of lingering
 * processes when Nautilus crashes. It's unsafe to call it if there
 * are any direct references to the bonobo object, but it's OK to have
 * any number of references to it through CORBA.
 */

#ifndef RELY_ON_BONOBO_INTERNALS

/* This version of the function doesn't rely on Bonobo internals as
 * much as the other one does, but it gets screwed up by incoming
 * unref calls while the object is being destroyed. This was actually
 * happening, which is why I had to write the other version.
 */

static void
set_gone_flag (GtkObject *object,
	       gpointer callback_data)
{
	gboolean *gone_flag;

	gone_flag = callback_data;
	*gone_flag = TRUE;
}

void
nautilus_bonobo_object_force_destroy (BonoboObject *object)
{
	gboolean gone;

	if (object == NULL) {
		return;
	}

	g_return_if_fail (BONOBO_IS_OBJECT (object));
	g_return_if_fail (!GTK_OBJECT_DESTROYED (object));

	gone = FALSE;
	gtk_signal_connect (GTK_OBJECT (object), "destroy",
			    set_gone_flag, &gone);
	do {
		bonobo_object_unref (object);
	} while (!gone);
}

#else /* RELY_ON_BONOBO_INTERNALS */

void
nautilus_bonobo_object_force_destroy (BonoboObject *object)
{
	StartOfBonoboAggregateObject *aggregate;
	GList *node;
	GtkObject *subobject;
	guint *id;

	if (object == NULL) {
		return;
	}

	g_return_if_fail (BONOBO_IS_OBJECT (object));

	aggregate = ((StartOfBonoboObjectPrivate *) object->priv)->ao;

	/* Do all the destroying with a normal reference count. This
	 * lets us live through unrefs that happen during the destroy
	 * process.
	 */
	bonobo_object_ref (object);
	for (node = aggregate->objs; node != NULL; node = node->next) {
		subobject = GTK_OBJECT (node->data);
		id = &((StartOfBonoboObjectPrivate *) BONOBO_OBJECT (subobject)->priv)->destroy_id;
		if (*id != 0) {
			gtk_signal_disconnect (subobject, *id);
			*id = 0;
		}
		gtk_object_destroy (subobject);
	}

	/* Now force a destroy by forcing the reference count to 1. */
	aggregate->ref_count = 1;
	bonobo_object_unref (object);
}

#endif /* RELY_ON_BONOBO_INTERNALS */

static gboolean
destroy_later_callback (gpointer callback_data)
{
	DestroyLaterData *data;

	data = callback_data;
	g_assert (BONOBO_IS_OBJECT (data->object));

	gtk_signal_disconnect (GTK_OBJECT (data->object),
			       data->destroy_handler_id);
	nautilus_bonobo_object_force_destroy (data->object);
	g_free (data);
	return FALSE;
}

static void
destroyed_before_timeout_callback (GtkObject *object,
				   gpointer callback_data)
{
	DestroyLaterData *data;

	data = callback_data;
	g_assert (data->object == BONOBO_OBJECT (object));

	gtk_timeout_remove (data->timeout_id);
	g_free (data);
}

void
nautilus_bonobo_object_force_destroy_later (BonoboObject *object)
{
	DestroyLaterData *data;

	if (object == NULL) {
		return;
	}

	g_return_if_fail (BONOBO_IS_OBJECT (object));
	g_return_if_fail (!GTK_OBJECT_DESTROYED (object));

	data = g_new (DestroyLaterData, 1);
	data->object = object;
	data->timeout_id = gtk_timeout_add
		(MINIMIZE_RACE_CONDITIONS_DELAY,
		 destroy_later_callback, data);
	data->destroy_handler_id = gtk_signal_connect
		(GTK_OBJECT (object), "destroy",
		 destroyed_before_timeout_callback, data);
}

/* Same as bonobo_unknown_ping, but this one works. */
static gboolean
object_is_gone (Bonobo_Unknown object)
{
	CORBA_Environment ev;
	gboolean gone;

	CORBA_exception_init (&ev);
	gone = CORBA_Object_non_existent (object, &ev);
	if (ev._major != CORBA_NO_EXCEPTION) {
		gone = TRUE;
	}
	CORBA_exception_free (&ev);
	
	return gone;
}

static void
remote_check_data_free (RemoteCheckData *data)
{
	CORBA_Environment ev;

	if (data == NULL) {
		return;
	}

	gtk_object_remove_data (GTK_OBJECT (data->object), REMOTE_CHECK_DATA_KEY);

	CORBA_exception_init (&ev);
	CORBA_Object_release (data->remote_object, &ev);
	CORBA_exception_free (&ev);

	if (data->timeout_id != 0) {
		g_source_remove (data->timeout_id);
	}

	if (data->destroy_handler_id != 0) {
		gtk_signal_disconnect (GTK_OBJECT (data->object),
				       data->destroy_handler_id);
	}

	g_free (data);
}

static gboolean
remote_check_timed_callback (gpointer callback_data)
{
	RemoteCheckData *data;
	BonoboObject *object;
	NautilusBonoboObjectCallback function;
	gpointer function_data;

	data = callback_data;
	g_assert (BONOBO_IS_OBJECT (data->object));
	g_assert (!GTK_OBJECT_DESTROYED (data->object));
	g_assert (data->remote_object != CORBA_OBJECT_NIL);
	g_assert (data->timeout_id != 0);
	g_assert (data->destroy_handler_id != 0);
	g_assert (data->function != NULL);

	if (!object_is_gone (data->remote_object)) {
		return TRUE;
	}

	object = data->object;
	function = data->function;
	function_data = data->callback_data;

	data->timeout_id = 0;
	remote_check_data_free (data);

	(* function) (object, function_data);

	return FALSE;
}

static void
remote_check_destroy_callback (GtkObject *object,
			       gpointer callback_data)
{
	RemoteCheckData *data;

	g_assert (BONOBO_IS_OBJECT (object));

	data = callback_data;
	g_assert (data->object == BONOBO_OBJECT (object));
	g_assert (data->remote_object != CORBA_OBJECT_NIL);
	g_assert (data->timeout_id != 0);
	g_assert (data->destroy_handler_id != 0);
	g_assert (data->function != NULL);

	remote_check_data_free (data);
}

void
nautilus_bonobo_object_call_when_remote_object_disappears (BonoboObject *object,
							   Bonobo_Unknown remote_object,
							   NautilusBonoboObjectCallback function,
							   gpointer callback_data)
{
	RemoteCheckData *data;
	CORBA_Environment ev;

	g_return_if_fail (BONOBO_IS_OBJECT (object));

	data = gtk_object_get_data (GTK_OBJECT (object), REMOTE_CHECK_DATA_KEY);

	if (GTK_OBJECT_DESTROYED (object)
	    || remote_object == CORBA_OBJECT_NIL
	    || function == NULL) {
		remote_check_data_free (data);
		return;
	}

	if (data == NULL) {
		data = g_new0 (RemoteCheckData, 1);
		data->object = object;
	}
	CORBA_exception_init (&ev);
	data->remote_object = CORBA_Object_duplicate (remote_object, &ev);
	CORBA_exception_free (&ev);
	data->function = function;
	data->callback_data = callback_data;

	if (data->timeout_id == 0) {
		data->timeout_id = g_timeout_add
			(REMOTE_CHECK_TIME_INTERVAL,
			 remote_check_timed_callback, data);
	}
	if (data->destroy_handler_id == 0) {
		data->destroy_handler_id = gtk_signal_connect
			(GTK_OBJECT (object), "destroy",
			 remote_check_destroy_callback, data);
	}

	gtk_object_set_data (GTK_OBJECT (object), REMOTE_CHECK_DATA_KEY, data);
}

static void
force_destroy_cover (BonoboObject *object,
		     gpointer callback_data)
{
	nautilus_bonobo_object_force_destroy (object);
}

void
nautilus_bonobo_object_force_destroy_when_owner_disappears (BonoboObject *object,
							    Bonobo_Unknown owner)
{
	nautilus_bonobo_object_call_when_remote_object_disappears
		(object, owner, force_destroy_cover, NULL);
}