aboutsummaryrefslogtreecommitdiff
path: root/webview_webkit.c
blob: 3375e9f86548362873876ba265d15bdf4126f42e (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

/*

  Copyright (c) 2010 Samuel Lidén Borell <samuel@slbdata.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#include "webview.h"
#include "webview_private.h"

#include <webkit/webkit.h>

struct WebView_ {
    WebViewCommon common;
    
    GtkWidget *widget;
    guint link_blocker;
};


/**
 * Triggered when the document load status changes. Used to detect when the
 * document has finished loading.
 */
static void load_status_notify(GObject *gobject, GParamSpec *pspec, gpointer user_data) {
    WebKitLoadStatus status;
    g_object_get(gobject, "load-status", &status,  NULL);
    
    if (status != WEBKIT_LOAD_FINISHED) return;
    
    WebView *webview = (WebView*)user_data;
    GObject *settings = G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(webview->widget)));
    
    // Initialize the editor script
    g_object_set(settings, "enable-scripts", TRUE,  NULL);
    webview_private_initEditorScript(webview);
}


/**
 * Triggered on changes to the "window.status" property. Used for receiving
 * messages from Javascript code.
 */
static void status_changed(WebKitWebView *widget, const gchar *text, gpointer user_data) {
    if (!text) return;
    
    WebViewElementInfo *info = webview_private_createElementInfo(text);
    
    WebView *webview = (WebView*)user_data;
    webview->common.notifyFunction(webview, info);
    
    webview_private_freeElementInfo(info);
}


/**
 * Triggered when the document title is changed
 */
static void title_notify(GObject *gobject, GParamSpec *pspec, gpointer user_data) {
    gchar *title;
    g_object_get(gobject, "title", &title,  NULL);
    
    WebView *webview = (WebView*)user_data;
    webview->common.titleChangeFunction(webview, title ? title : "");
    
    g_free(title);
}


WebView *webview_new(WebViewNotifyFunction notifyFunction,
                     WebViewTitleChangeFunction titleChangeFunction) {
    WebView *webview = g_malloc(sizeof(WebView));
    webview->common.notifyFunction = notifyFunction;
    webview->common.titleChangeFunction = titleChangeFunction;
    
    // Create an editable WebKit view
    GtkWidget *widget = webkit_web_view_new();
    webview->widget = widget;
    webview->link_blocker = 0;
    
    // Prepare settings and values
    GObject *settings = G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(widget)));
    
    g_object_set(settings,
                 // Disable plugins, etc.
                 "enable-java-applet", FALSE,
                 "enable-plugins", FALSE,
                 "enable-site-specific-quirks", FALSE,
                 // Enable spell checking
                 // TODO check spelling of existing content
                 "enable-spell-checking", FALSE,
                 // Handle popup menus
                 "enable-default-context-menu", FALSE,
                 NULL);
    
    // Use "user-stylesheet-uri" for debugging?
    
    // Install handlers
    g_signal_connect(widget, "notify::load-status", G_CALLBACK(load_status_notify), webview);
    g_signal_connect(widget, "status-bar-text-changed", G_CALLBACK(status_changed), webview);
    g_signal_connect(widget, "notify::title", G_CALLBACK(title_notify), webview);
    
    // TODO release settings object?
    
    return webview;
}


void webview_free(WebView *webview) {
    g_free(webview);
}


GtkWidget *webview_getWidget(WebView *webview) {
    return webview->widget;
}


static gboolean block_navigation(WebKitWebView *widget, WebKitWebFrame *frame,
                                 WebKitNetworkRequest *request,
                                 WebKitWebNavigationAction *navigation_action,
                                 WebKitWebPolicyDecision *policy_decision,
                                 gpointer user_data) {
    webkit_web_policy_decision_ignore(policy_decision);
    return TRUE;
}


void webview_load(WebView *webview, const gchar *url, const gchar *content,
                  gboolean wholePageEditable) {
    WebKitWebView *widget = WEBKIT_WEB_VIEW(webview->widget);
    
    // Clean up from last time
    if (webview->link_blocker) {
        g_signal_handler_disconnect(widget, webview->link_blocker);
        webview->link_blocker = 0; // or is zero a valid handler?
    }
    
    GObject *settings = G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(widget)));
    g_object_set(settings, "enable-scripts", FALSE,  NULL);
    
    webview->common.wholePageEditable = wholePageEditable;
    
    // Load file
    webkit_web_view_load_string(widget, content, "text/html", NULL, url);
    
    // Disable links
    webview->link_blocker = g_signal_connect(widget,
            "navigation-policy-decision-requested",
            G_CALLBACK(block_navigation), NULL);
}


gchar *webview_getHTML(WebView *webview) {
    return webview_executeExpression(webview, "getHTML()");
}


void webview_setTitle(WebView *webview, const gchar *title) {
    gchar *escapedTitle = g_strescape(title, NULL);
    webview_executeFormattedScript(webview, "document.title = \"%s\";",
                                   escapedTitle);
    g_free(escapedTitle);
}


void webview_executeScript(WebView *webview, const gchar *script) {
    webkit_web_view_execute_script(WEBKIT_WEB_VIEW(webview->widget), script);
}


gchar *webview_executeExpression(WebView *webview, const gchar *expr) {
    WebKitWebView *widget = WEBKIT_WEB_VIEW(webview->widget);
    
    /*
        A "nicer" solution would be to catch "window-object-cleared" and add
        a special property/function there, but that's much more work for little
        practical benefit.
    */
    
    // Use the title to store the value of the expression
    webview_executeFormattedScript(webview, "var oldTitle = document.title;"
                                            "document.title = encodeURIComponent(%s);", expr);
    
    gchar *result = g_uri_unescape_string(webkit_web_view_get_title(widget), NULL);
    
    // Restore old title
    webview_executeScript(webview, "document.title = oldTitle; delete oldTitle;");
    return result;
}