Move all necessary initialisation to newview()

Most all the settings and callbacks are set before view creation.
Create a related view (with inherited settings) if asked to.
This commit is contained in:
Quentin Rameau 2015-11-18 17:17:49 +01:00
parent ad538bb00a
commit 0f3e3bf669
2 changed files with 53 additions and 59 deletions

View file

@ -4,7 +4,7 @@ static char *useragent = "Mozilla/5.0 (X11; U; Unix; en-US) "
"Chrome/24.0.1295.0 Safari/537.15 Surf/"VERSION; "Chrome/24.0.1295.0 Safari/537.15 Surf/"VERSION;
static char *scriptfile = "~/.surf/script.js"; static char *scriptfile = "~/.surf/script.js";
static char *styledir = "~/.surf/styles/"; static char *styledir = "~/.surf/styles/";
static char *cachefolder = "~/.surf/cache/"; static char *cachedir = "~/.surf/cache/";
static Bool kioskmode = FALSE; /* Ignore shortcuts */ static Bool kioskmode = FALSE; /* Ignore shortcuts */
static Bool showindicators = TRUE; /* Show indicators in window title */ static Bool showindicators = TRUE; /* Show indicators in window title */

110
surf.c
View file

@ -334,6 +334,7 @@ cleanup(void)
g_free(cookiefile); g_free(cookiefile);
g_free(scriptfile); g_free(scriptfile);
g_free(stylefile); g_free(stylefile);
g_free(cachedir);
} }
WebKitCookieAcceptPolicy WebKitCookieAcceptPolicy
@ -800,6 +801,8 @@ newview(Client *c, WebKitWebView *rv)
{ {
WebKitWebView *v; WebKitWebView *v;
WebKitSettings *settings; WebKitSettings *settings;
WebKitUserContentManager *contentmanager;
WebKitWebContext *context;
char *ua; char *ua;
/* Webview */ /* Webview */
@ -807,43 +810,57 @@ newview(Client *c, WebKitWebView *rv)
v = WEBKIT_WEB_VIEW( v = WEBKIT_WEB_VIEW(
webkit_web_view_new_with_related_view(rv)); webkit_web_view_new_with_related_view(rv));
} else { } else {
v = WEBKIT_WEB_VIEW(webkit_web_view_new()); settings = webkit_settings_new_with_settings(
"auto-load-images", loadimages,
settings = webkit_web_view_get_settings(v); "default-font-size", defaultfontsize,
"enable-caret-browsing", enablecaretbrowsing,
"enable-developer-extras", enableinspector,
"enable-dns-prefetching", enablednsprefetching,
"enable-frame-flattening", enableframeflattening,
"enable-html5-database", enablecache,
"enable-html5-local-storage", enablecache,
"enable-javascript", enablescripts,
"enable-plugins", enableplugins,
NULL);
if (!(ua = getenv("SURF_USERAGENT"))) if (!(ua = getenv("SURF_USERAGENT")))
ua = useragent; ua = useragent;
g_object_set(G_OBJECT(settings), "user-agent", ua, NULL); webkit_settings_set_user_agent(settings, ua);
g_object_set(G_OBJECT(settings), /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
"auto-load-images", loadimages, NULL); * for more interesting settings */
g_object_set(G_OBJECT(settings),
"enable-plugins", enableplugins, NULL);
g_object_set(G_OBJECT(settings),
"enable-scripts", enablescripts, NULL);
g_object_set(G_OBJECT(settings),
"enable-spatial-navigation", enablespatialbrowsing, NULL);
g_object_set(G_OBJECT(settings),
"enable-developer-extras", enableinspector, NULL);
g_object_set(G_OBJECT(settings),
"enable-default-context-menu", kioskmode ^ 1, NULL);
g_object_set(G_OBJECT(settings),
"default-font-size", defaultfontsize, NULL);
g_object_set(G_OBJECT(settings),
"resizable-text-areas", 1, NULL);
if (enablestyle)
setstyle(c, getstyle("about:blank"));
if (enableinspector) { contentmanager = webkit_user_content_manager_new();
c->inspector = webkit_web_view_get_inspector(v);
g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view", context = webkit_web_context_new_with_website_data_manager(
G_CALLBACK(inspector_new), c); webkit_website_data_manager_new(
g_signal_connect(G_OBJECT(c->inspector), "show-window", "base-cache-directory", cachedir,
G_CALLBACK(inspector_show), c); "base-data-directory", cachedir,
g_signal_connect(G_OBJECT(c->inspector), "close-window", NULL));
G_CALLBACK(inspector_close), c);
g_signal_connect(G_OBJECT(c->inspector), "finished", /* rendering process model, can be a shared unique one or one for each
G_CALLBACK(inspector_finished), c); * view */
c->isinspecting = false; webkit_web_context_set_process_model(context,
} WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
/* ssl */
webkit_web_context_set_tls_errors_policy(context, strictssl ?
WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
/* disk cache */
webkit_web_context_set_cache_model(context, enablecache ?
WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
/* Currently only works with text file to be compatible with curl */
webkit_cookie_manager_set_persistent_storage(
webkit_web_context_get_cookie_manager(context), cookiefile,
WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
/* cookie policy */
webkit_cookie_manager_set_accept_policy(
webkit_web_context_get_cookie_manager(context),
cookiepolicy_get());
v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
"settings", settings,
"user-content-manager", contentmanager,
"web-context", context,
NULL);
} }
g_signal_connect(G_OBJECT(v), g_signal_connect(G_OBJECT(v),
@ -1196,7 +1213,7 @@ setup(void)
/* dirs and files */ /* dirs and files */
cookiefile = buildfile(cookiefile); cookiefile = buildfile(cookiefile);
scriptfile = buildfile(scriptfile); scriptfile = buildfile(scriptfile);
cachefolder = buildpath(cachefolder); cachedir = buildpath(cachedir);
if (stylefile == NULL) { if (stylefile == NULL) {
styledir = buildpath(styledir); styledir = buildpath(styledir);
for (i = 0; i < LENGTH(styles); i++) { for (i = 0; i < LENGTH(styles); i++) {
@ -1221,29 +1238,6 @@ setup(void)
stylefile = g_strconcat("file://", stylepath, NULL); stylefile = g_strconcat("file://", stylepath, NULL);
g_free(stylepath); g_free(stylepath);
} }
/* cookie policy */
webkit_cookie_manager_set_persistent_storage(
webkit_web_context_get_cookie_manager(context), cookiefile,
WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
webkit_cookie_manager_set_accept_policy(
webkit_web_context_get_cookie_manager(context),
cookiepolicy_get());
/* rendering process model, can be a shared unique one or one for each
* view */
webkit_web_context_set_process_model(context,
WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
/* disk cache */
webkit_web_context_set_cache_model(context, enablecache ?
WEBKIT_CACHE_MODEL_WEB_BROWSER :
WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
/* ssl */
webkit_web_context_set_tls_errors_policy(context, strictssl ?
WEBKIT_TLS_ERRORS_POLICY_FAIL :
WEBKIT_TLS_ERRORS_POLICY_IGNORE);
} }
void void