aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ephy-shlib-loader.c
blob: 8c6db15b01f3cda650d006526861f0654e0c98da (plain) (blame)
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
/*
 *  Copyright (C) 2003 Marco Pesenti Gritti
 *  Copyright (C) 2003, 2004 Christian Persch
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ephy-shlib-loader.h"
#include "ephy-loader.h"
#include "ephy-module.h"
#include "ephy-debug.h"

#include <string.h>

#define DATA_KEY "EphyShlibLoader::LoaderData"

typedef struct
{
    EphyModule *module;
    GObject *object;
} LoaderData;

static GQuark library_quark = 0;

#define EPHY_SHLIB_LOADER_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_SHLIB_LOADER, EphyShlibLoaderPrivate))

struct _EphyShlibLoaderPrivate
{
    GSList *data;
};

static void ephy_shlib_loader_class_init (EphyShlibLoaderClass *klass);
static void ephy_shlib_loader_iface_init (EphyLoaderIface *iface);
static void ephy_shlib_loader_init    (EphyShlibLoader *loader);

static GObjectClass *parent_class = NULL;

GType
ephy_shlib_loader_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0))
    {
        static const GTypeInfo our_info =
        {
            sizeof (EphyShlibLoaderClass),
            NULL, /* base_init */
            NULL, /* base_finalize */
            (GClassInitFunc) ephy_shlib_loader_class_init,
            NULL,
            NULL, /* class_data */
            sizeof (EphyShlibLoader),
            0, /* n_preallocs */
            (GInstanceInitFunc) ephy_shlib_loader_init
        };

        static const GInterfaceInfo loader_info =
        {
            (GInterfaceInitFunc) ephy_shlib_loader_iface_init,
            NULL,
            NULL
        };

        type = g_type_register_static (G_TYPE_OBJECT,
                           "EphyShlibLoader",
                           &our_info, 0);

        g_type_add_interface_static (type,
                         EPHY_TYPE_LOADER,
                         &loader_info);
    }

    return type;
}

static void
free_loader_data (LoaderData *data)
{
    g_return_if_fail (data != NULL);

    /* data->module must NOT be unreffed! */

    if (data->object != NULL)
    {
        g_object_unref (data->object);
    }

    g_free (data);
}

static void
ephy_shlib_loader_init (EphyShlibLoader *loader)
{
    loader->priv = EPHY_SHLIB_LOADER_GET_PRIVATE (loader);

    LOG ("EphyShlibLoader initialising")
}

static void
ephy_shlib_loader_finalize (GObject *object)
{
    EphyShlibLoader *loader = EPHY_SHLIB_LOADER (object);

    LOG ("EphyShlibLoader finalising")

    g_slist_foreach (loader->priv->data, (GFunc) free_loader_data, NULL);
    g_slist_free (loader->priv->data);

    parent_class->finalize (object);
}

static int
find_library (const LoaderData *data,
          const char *library)
{
    return strcmp (ephy_module_get_path (data->module), library);
}

static int
find_object (const LoaderData *data,
         const GObject *object)
{
    return data->object != object;
}

static gboolean
idle_unref (GObject *object)
{
    g_object_unref (object);

    return FALSE;
}

static GObject *
impl_get_object (EphyLoader *eloader,
         GData **attributes)
{
    EphyShlibLoader *loader = EPHY_SHLIB_LOADER (eloader);
    GSList *l;
    LoaderData *data = NULL;
    const char *library;

    library = g_datalist_id_get_data (attributes, library_quark);
    if (library == NULL)
    {
        g_warning ("NULL library name!\n");
        return NULL;
    }

    l = g_slist_find_custom (loader->priv->data, library,
                (GCompareFunc) find_library);

    if (l != NULL)
    {
        data = l->data;
        g_return_val_if_fail (data != NULL, NULL);

        if (data->object != NULL)
        {
            return g_object_ref (data->object);
        }
    }
    else
    {
        data = g_new0 (LoaderData, 1);
        loader->priv->data = g_slist_prepend (loader->priv->data, data);
    }

    if (data->module == NULL)
    {
        data->module = ephy_module_new (library);
    }

    g_return_val_if_fail (data->object == NULL, data->object);

    if (g_type_module_use (G_TYPE_MODULE (data->module)) == FALSE)
    {
        g_warning ("Could not load extension file at %s\n",
               ephy_module_get_path (data->module));
        return NULL;
    }

    data->object = ephy_module_new_object (data->module);

    g_type_module_unuse (G_TYPE_MODULE (data->module));

    if (data->object != NULL)
    {
        g_object_set_data (G_OBJECT (data->object), DATA_KEY, data);
    }

    return data->object;
}

static void
impl_release_object (EphyLoader *eloader,
             GObject *object)
{
    EphyShlibLoader *loader = EPHY_SHLIB_LOADER (eloader);
    GSList *l;
    LoaderData *data;

    l = g_slist_find_custom (loader->priv->data, object,
                (GCompareFunc) find_object);
    g_return_if_fail (l != NULL);
    data = l->data;

    /* FIXME: should we consider those extensions broken?
     * Only unref the extension in the idle loop; if the extension has its
     * own functions queued in the idle loop, the functions must exist in
     * memory before being called.
     */
    g_idle_add ((GSourceFunc) idle_unref, data->object);
    data->object = NULL;
}

static void
ephy_shlib_loader_iface_init (EphyLoaderIface *iface)
{
    iface->type = "shlib";
    iface->get_object = impl_get_object;
    iface->release_object = impl_release_object;
}

static void
ephy_shlib_loader_class_init (EphyShlibLoaderClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    object_class->finalize = ephy_shlib_loader_finalize;

    g_type_class_add_private (object_class, sizeof (EphyShlibLoaderPrivate));

    library_quark = g_quark_from_string ("library");
}