~ubuntu-branches/ubuntu/natty/moon/natty

« back to all changes in this revision

Viewing changes to test/harness/shocker/shutdown-manager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Permission is hereby granted, free of charge, to any person obtaining
 
3
 * a copy of this software and associated documentation files (the
 
4
 * "Software"), to deal in the Software without restriction, including
 
5
 * without limitation the rights to use, copy, modify, merge, publish,
 
6
 * distribute, sublicense, and/or sell copies of the Software, and to
 
7
 * permit persons to whom the Software is furnished to do so, subject to
 
8
 * the following conditions:
 
9
 *
 
10
 * The above copyright notice and this permission notice shall be
 
11
 * included in all copies or substantial portions of the Software.
 
12
 *
 
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
14
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
15
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
16
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
17
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
18
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
19
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
20
 *
 
21
 *
 
22
 * Author:
 
23
 *   Jackson Harper (jackson@ximian.com)
 
24
 *
 
25
 * Copyright 2007-2008 Novell, Inc. (http://www.novell.com)
 
26
 *
 
27
 *
 
28
 * shutdown-manager.cpp: When shutdown is signaled, the shutdown manager
 
29
 *                       will wait until everything (image capture) is done
 
30
 *                       before it will actually shutdown the app.
 
31
 * 
 
32
 */
 
33
 
 
34
 
 
35
#include <stdlib.h>
 
36
#include <stdio.h>
 
37
#include <glib.h>
 
38
#include <prinit.h>
 
39
#include <gtk/gtk.h>
 
40
 
 
41
#ifdef DBUS_ENABLED
 
42
#include <dbus/dbus-glib.h>
 
43
#endif
 
44
 
 
45
#include "shutdown-manager.h"
 
46
 
 
47
 
 
48
#define DRT_AGSERVER_SERVICE    "mono.moonlight.agserver"
 
49
#define DRT_AGSERVER_PATH       "/mono/moonlight/agserver"
 
50
#define DRT_AGSERVER_INTERFACE  "mono.moonlight.agserver.IAgserver"
 
51
 
 
52
 
 
53
#define TIMEOUT_INTERVAL 10000
 
54
 
 
55
static GMutex* shutdown_mutex = NULL;
 
56
static GCond*  shutdown_cond = NULL;
 
57
static gint    wait_count = 0;
 
58
 
 
59
static void execute_shutdown (ShockerScriptableControlObject *shocker);
 
60
static gboolean attempt_clean_shutdown (gpointer data);
 
61
 
 
62
void
 
63
shutdown_manager_init ()
 
64
{
 
65
        wait_count = 0;
 
66
        shutdown_mutex = g_mutex_new ();
 
67
        shutdown_cond = g_cond_new ();
 
68
}
 
69
 
 
70
void
 
71
shutdown_manager_shutdown ()
 
72
{
 
73
        wait_count = 0;
 
74
 
 
75
        g_mutex_free (shutdown_mutex);
 
76
        g_cond_free (shutdown_cond);
 
77
}
 
78
 
 
79
void
 
80
shutdown_manager_wait_increment ()
 
81
{
 
82
        g_assert (shutdown_mutex);
 
83
        g_assert (shutdown_cond);
 
84
 
 
85
        g_mutex_lock (shutdown_mutex);
 
86
 
 
87
 
 
88
        wait_count++;
 
89
        g_mutex_unlock (shutdown_mutex);
 
90
}
 
91
 
 
92
void
 
93
shutdown_manager_wait_decrement ()
 
94
{
 
95
        g_assert (shutdown_mutex);
 
96
        g_assert (shutdown_cond);
 
97
 
 
98
        g_mutex_lock (shutdown_mutex);
 
99
 
 
100
        wait_count--;
 
101
        if (wait_count == 0)
 
102
                g_cond_signal (shutdown_cond);
 
103
 
 
104
        g_mutex_unlock (shutdown_mutex);
 
105
}
 
106
 
 
107
void
 
108
shutdown_manager_wait ()
 
109
{
 
110
        g_assert (shutdown_mutex);
 
111
        g_assert (shutdown_cond);
 
112
 
 
113
        while (wait_count > 0)
 
114
                g_cond_wait (shutdown_cond, shutdown_mutex);
 
115
}
 
116
 
 
117
static void
 
118
execute_shutdown (ShockerScriptableControlObject *shocker)
 
119
{
 
120
        char *dont_die = getenv ("MOONLIGHT_SHOCKER_DONT_DIE");
 
121
        if (dont_die != NULL && dont_die [0] != 0)
 
122
                return;
 
123
 
 
124
        g_type_init ();
 
125
 
 
126
        DBusGConnection* connection;
 
127
        GError* error = NULL;  
 
128
 
 
129
        error = NULL;
 
130
        connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 
131
        if (!connection) {
 
132
                g_warning ("Failed to open connection to bus: %s\n", error->message);
 
133
                g_error_free (error);
 
134
        }
 
135
 
 
136
        DBusGProxy* dbus_proxy = dbus_g_proxy_new_for_name (connection,
 
137
                        DRT_AGSERVER_SERVICE,
 
138
                        DRT_AGSERVER_PATH,
 
139
                        DRT_AGSERVER_INTERFACE);
 
140
        
 
141
 
 
142
        dbus_g_proxy_call_no_reply (dbus_proxy, "SignalShutdown", G_TYPE_INVALID, G_TYPE_INVALID);
 
143
 
 
144
//      if (!dbus_g_proxy_call (dbus_proxy, "SignalShutdown", &error, G_TYPE_INVALID, G_TYPE_INVALID)) {
 
145
//              g_warning ("unable to make signal shutdown call:  %s\n", error->message);
 
146
//      }
 
147
 
 
148
 
 
149
 
 
150
        if (gtk_main_level ()) {
 
151
                // We are running inside the embedded agviewer, so we can use gtk to signal shutdown
 
152
//              gtk_main_quit ();
 
153
        } else {
 
154
                // This block never actually gets called, since firefox is also using gtk_main.
 
155
                PR_ProcessExit (0);
 
156
        }
 
157
}
 
158
 
 
159
static gboolean
 
160
attempt_clean_shutdown (gpointer data)
 
161
{
 
162
        ShockerScriptableControlObject *shocker = (ShockerScriptableControlObject *) data;
 
163
        char *dont_die = getenv ("MOONLIGHT_SHOCKER_DONT_DIE");
 
164
        if (dont_die != NULL && dont_die [0] != 0)
 
165
                return FALSE;
 
166
 
 
167
        g_assert (shutdown_mutex);
 
168
        g_assert (shutdown_cond);
 
169
 
 
170
        bool ready_for_shutdown = false;
 
171
 
 
172
        g_mutex_lock (shutdown_mutex);
 
173
        if (wait_count <= 0)
 
174
                ready_for_shutdown = true;
 
175
        g_mutex_unlock (shutdown_mutex);
 
176
 
 
177
        if (ready_for_shutdown) {
 
178
                execute_shutdown (shocker);
 
179
                return FALSE;
 
180
        }
 
181
 
 
182
        return TRUE;
 
183
}
 
184
 
 
185
void
 
186
shutdown_manager_queue_shutdown (ShockerScriptableControlObject* shocker)
 
187
{
 
188
        g_assert (shutdown_mutex);
 
189
        g_assert (shutdown_cond);
 
190
 
 
191
        if (!wait_count)
 
192
                return execute_shutdown (shocker);
 
193
 
 
194
        if (!g_timeout_add (TIMEOUT_INTERVAL, attempt_clean_shutdown, shocker)) {
 
195
                g_error ("Unable to create timeout for queued shutdown, executing immediate shutdown.");
 
196
                execute_shutdown (shocker);
 
197
        }
 
198
}
 
199