Skip to content

Commit 63859db

Browse files
[GTK] Add support to load/save session in MiniBrowser
https://bugs.webkit.org/show_bug.cgi?id=153201 Reviewed by Michael Catanzaro. It makes it easier to test the new WebView session API. * MiniBrowser/gtk/BrowserWindow.c: (browserWindowFinalize): Free the session file path. (browserWindowSaveSession): Save the current WebView session if there's a session file path. (browserWindowDeleteEvent): Call browserWindowSaveSession(). (browser_window_load_session): Try to load the session from the given file path, otherwise fall back to homepage and keep the session file to save the session on window close. * MiniBrowser/gtk/BrowserWindow.h: * MiniBrowser/gtk/main.c: (createBrowserWindow): Pass the given session file path when shouldLoadSession is TRUE. (main): Only allow to restore/save session when MiniBrowser is launched without URL arguments. git-svn-id: http://svn.webkit.org/repository/webkit/trunk@195286 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 426cf13 commit 63859db

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

Tools/ChangeLog

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
2016-01-19 Carlos Garcia Campos <[email protected]>
2+
3+
[GTK] Add support to load/save session in MiniBrowser
4+
https://bugs.webkit.org/show_bug.cgi?id=153201
5+
6+
Reviewed by Michael Catanzaro.
7+
8+
It makes it easier to test the new WebView session API.
9+
10+
* MiniBrowser/gtk/BrowserWindow.c:
11+
(browserWindowFinalize): Free the session file path.
12+
(browserWindowSaveSession): Save the current WebView session if
13+
there's a session file path.
14+
(browserWindowDeleteEvent): Call browserWindowSaveSession().
15+
(browser_window_load_session): Try to load the session from the
16+
given file path, otherwise fall back to homepage and keep the
17+
session file to save the session on window close.
18+
* MiniBrowser/gtk/BrowserWindow.h:
19+
* MiniBrowser/gtk/main.c:
20+
(createBrowserWindow): Pass the given session file path when
21+
shouldLoadSession is TRUE.
22+
(main): Only allow to restore/save session when MiniBrowser is
23+
launched without URL arguments.
24+
125
2016-01-19 Carlos Garcia Campos <[email protected]>
226

327
Unreviewed. Fix GTK+ test /webkit2/WebKitWebView/geolocation-permission-requests after r195075.

Tools/MiniBrowser/gtk/BrowserWindow.c

+44
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct _BrowserWindow {
7171
GtkWindow *parentWindow;
7272
guint fullScreenMessageLabelId;
7373
guint resetEntryProgressTimeoutId;
74+
gchar *sessionFile;
7475
};
7576

7677
struct _BrowserWindowClass {
@@ -779,6 +780,8 @@ static void browserWindowFinalize(GObject *gObject)
779780
if (window->resetEntryProgressTimeoutId)
780781
g_source_remove(window->resetEntryProgressTimeoutId);
781782

783+
g_free(window->sessionFile);
784+
782785
G_OBJECT_CLASS(browser_window_parent_class)->finalize(gObject);
783786

784787
if (g_atomic_int_dec_and_test(&windowCount))
@@ -1151,9 +1154,22 @@ static void browserWindowConstructed(GObject *gObject)
11511154
webkit_web_view_load_html(window->webView, "<html></html>", "file:///");
11521155
}
11531156

1157+
static void browserWindowSaveSession(BrowserWindow *window)
1158+
{
1159+
if (!window->sessionFile)
1160+
return;
1161+
1162+
WebKitWebViewSessionState *state = webkit_web_view_get_session_state(window->webView);
1163+
GBytes *bytes = webkit_web_view_session_state_serialize(state);
1164+
webkit_web_view_session_state_unref(state);
1165+
g_file_set_contents(window->sessionFile, g_bytes_get_data(bytes, NULL), g_bytes_get_size(bytes), NULL);
1166+
g_bytes_unref(bytes);
1167+
}
1168+
11541169
static gboolean browserWindowDeleteEvent(GtkWidget *widget, GdkEventAny* event)
11551170
{
11561171
BrowserWindow *window = BROWSER_WINDOW(widget);
1172+
browserWindowSaveSession(window);
11571173
webkit_web_view_try_close(window->webView);
11581174
return TRUE;
11591175
}
@@ -1217,6 +1233,34 @@ void browser_window_load_uri(BrowserWindow *window, const char *uri)
12171233
webkit_web_view_run_javascript(window->webView, strstr(uri, "javascript:"), NULL, NULL, NULL);
12181234
}
12191235

1236+
void browser_window_load_session(BrowserWindow *window, const char *sessionFile)
1237+
{
1238+
g_return_if_fail(BROWSER_IS_WINDOW(window));
1239+
g_return_if_fail(sessionFile);
1240+
1241+
window->sessionFile = g_strdup(sessionFile);
1242+
gchar *data = NULL;
1243+
gsize dataLength;
1244+
if (g_file_get_contents(sessionFile, &data, &dataLength, NULL)) {
1245+
GBytes *bytes = g_bytes_new_take(data, dataLength);
1246+
WebKitWebViewSessionState *state = webkit_web_view_session_state_new(bytes);
1247+
g_bytes_unref(bytes);
1248+
1249+
if (state) {
1250+
webkit_web_view_restore_session_state(window->webView, state);
1251+
webkit_web_view_session_state_unref(state);
1252+
}
1253+
}
1254+
1255+
WebKitBackForwardList *bfList = webkit_web_view_get_back_forward_list(window->webView);
1256+
WebKitBackForwardListItem *item = webkit_back_forward_list_get_current_item(bfList);
1257+
if (item)
1258+
webkit_web_view_go_to_back_forward_list_item(window->webView, item);
1259+
else
1260+
webkit_web_view_load_uri(window->webView, BROWSER_DEFAULT_URL);
1261+
1262+
}
1263+
12201264
void browser_window_set_background_color(BrowserWindow *window, GdkRGBA *rgba)
12211265
{
12221266
g_return_if_fail(BROWSER_IS_WINDOW(window));

Tools/MiniBrowser/gtk/BrowserWindow.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ GType browser_window_get_type(void);
4747
GtkWidget* browser_window_new(WebKitWebView*, GtkWindow*);
4848
WebKitWebView* browser_window_get_view(BrowserWindow*);
4949
void browser_window_load_uri(BrowserWindow *, const char *uri);
50+
void browser_window_load_session(BrowserWindow *, const char *sessionFile);
5051
void browser_window_set_background_color(BrowserWindow*, GdkRGBA*);
5152

5253
G_END_DECLS

Tools/MiniBrowser/gtk/main.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static const gchar **uriArguments = NULL;
3939
static const char *miniBrowserAboutScheme = "minibrowser-about";
4040
static GdkRGBA *backgroundColor;
4141
static gboolean editorMode;
42+
static const char *sessionFile;
4243

4344
typedef enum {
4445
MINI_BROWSER_ERROR_INVALID_ABOUT_PATH
@@ -58,7 +59,7 @@ static gchar *argumentToURL(const char *filename)
5859
return fileURL;
5960
}
6061

61-
static void createBrowserWindow(const gchar *uri, WebKitSettings *webkitSettings)
62+
static void createBrowserWindow(const gchar *uri, WebKitSettings *webkitSettings, gboolean shouldLoadSession)
6263
{
6364
GtkWidget *webView = webkit_web_view_new();
6465
if (editorMode)
@@ -71,9 +72,13 @@ static void createBrowserWindow(const gchar *uri, WebKitSettings *webkitSettings
7172
webkit_web_view_set_settings(WEBKIT_WEB_VIEW(webView), webkitSettings);
7273

7374
if (!editorMode) {
74-
gchar *url = argumentToURL(uri);
75-
browser_window_load_uri(BROWSER_WINDOW(mainWindow), url);
76-
g_free(url);
75+
if (shouldLoadSession && sessionFile)
76+
browser_window_load_session(BROWSER_WINDOW(mainWindow), sessionFile);
77+
else {
78+
gchar *url = argumentToURL(uri);
79+
browser_window_load_uri(BROWSER_WINDOW(mainWindow), url);
80+
g_free(url);
81+
}
7782
}
7883

7984
gtk_widget_grab_focus(webView);
@@ -96,6 +101,7 @@ static const GOptionEntry commandLineOptions[] =
96101
{
97102
{ "bg-color", 0, 0, G_OPTION_ARG_CALLBACK, parseBackgroundColor, "Background color", NULL },
98103
{ "editor-mode", 'e', 0, G_OPTION_ARG_NONE, &editorMode, "Run in editor mode", NULL },
104+
{ "session-file", 's', 0, G_OPTION_ARG_FILENAME, &sessionFile, "Session file", "FILE" },
99105
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &uriArguments, 0, "[URL…]" },
100106
{ 0, 0, 0, 0, 0, 0, 0 }
101107
};
@@ -299,9 +305,9 @@ int main(int argc, char *argv[])
299305
int i;
300306

301307
for (i = 0; uriArguments[i]; i++)
302-
createBrowserWindow(uriArguments[i], webkitSettings);
308+
createBrowserWindow(uriArguments[i], webkitSettings, FALSE);
303309
} else
304-
createBrowserWindow(BROWSER_DEFAULT_URL, webkitSettings);
310+
createBrowserWindow(BROWSER_DEFAULT_URL, webkitSettings, TRUE);
305311

306312
g_clear_object(&webkitSettings);
307313

0 commit comments

Comments
 (0)