File: frames.c

package info (click to toggle)
luakit 2012.09.13-r1-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,160 kB
  • ctags: 1,276
  • sloc: ansic: 6,086; makefile: 153; ruby: 79; sh: 38
file content (66 lines) | stat: -rw-r--r-- 2,230 bytes parent folder | download | duplicates (2)
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
/*
 * widgets/webview/frames.c - webkit webview frames functions
 *
 * Copyright © 2010-2011 Mason Larobina <mason.larobina@gmail.com>
 *
 * 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
 *
 */

GHashTable *frames_by_view = NULL;

typedef struct {
    WebKitWebView *view;
    WebKitWebFrame *frame;
} frame_destroy_callback_t;

static gint
luaH_webview_push_frames(lua_State *L, webview_data_t *d)
{
    GHashTable *frames = g_hash_table_lookup(frames_by_view, d->view);
    lua_createtable(L, g_hash_table_size(frames), 0);
    gint i = 1, tidx = lua_gettop(L);
    gpointer frame;
    GHashTableIter iter;
    g_hash_table_iter_init(&iter, frames);
    while (g_hash_table_iter_next(&iter, &frame, NULL)) {
        lua_pushlightuserdata(L, frame);
        lua_rawseti(L, tidx, i++);
    }
    return 1;
}

static void
frame_destroyed_cb(frame_destroy_callback_t *st)
{
    gpointer hash = g_hash_table_lookup(frames_by_view, st->view);
    /* the view might be destroyed before the frames */
    if (hash)
        g_hash_table_remove(hash, st->frame);
    g_slice_free(frame_destroy_callback_t, st);
}

static void
document_load_finished_cb(WebKitWebView *v, WebKitWebFrame *f,
        widget_t* UNUSED(w))
{
    /* add a bogus property to the frame so we get notified when it's destroyed */
    frame_destroy_callback_t *st = g_slice_new(frame_destroy_callback_t);
    st->view = v;
    st->frame = f;
    g_object_set_data_full(G_OBJECT(f), "dummy-destroy-notify", st,
            (GDestroyNotify)frame_destroyed_cb);
    GHashTable *frames = g_hash_table_lookup(frames_by_view, v);
    g_hash_table_insert(frames, f, NULL);
}