Title: [195286] trunk/Tools
Revision
195286
Author
[email protected]
Date
2016-01-19 05:15:59 -0800 (Tue, 19 Jan 2016)

Log Message

[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.

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (195285 => 195286)


--- trunk/Tools/ChangeLog	2016-01-19 13:14:17 UTC (rev 195285)
+++ trunk/Tools/ChangeLog	2016-01-19 13:15:59 UTC (rev 195286)
@@ -1,5 +1,29 @@
 2016-01-19  Carlos Garcia Campos  <[email protected]>
 
+        [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.
+
+2016-01-19  Carlos Garcia Campos  <[email protected]>
+
         Unreviewed. Fix GTK+ test /webkit2/WebKitWebView/geolocation-permission-requests after r195075.
 
         Geolocation is no longer allowed for unique origins after r195075.

Modified: trunk/Tools/MiniBrowser/gtk/BrowserWindow.c (195285 => 195286)


--- trunk/Tools/MiniBrowser/gtk/BrowserWindow.c	2016-01-19 13:14:17 UTC (rev 195285)
+++ trunk/Tools/MiniBrowser/gtk/BrowserWindow.c	2016-01-19 13:15:59 UTC (rev 195286)
@@ -71,6 +71,7 @@
     GtkWindow *parentWindow;
     guint fullScreenMessageLabelId;
     guint resetEntryProgressTimeoutId;
+    gchar *sessionFile;
 };
 
 struct _BrowserWindowClass {
@@ -779,6 +780,8 @@
     if (window->resetEntryProgressTimeoutId)
         g_source_remove(window->resetEntryProgressTimeoutId);
 
+    g_free(window->sessionFile);
+
     G_OBJECT_CLASS(browser_window_parent_class)->finalize(gObject);
 
     if (g_atomic_int_dec_and_test(&windowCount))
@@ -1151,9 +1154,22 @@
         webkit_web_view_load_html(window->webView, "<html></html>", "file:///");
 }
 
+static void browserWindowSaveSession(BrowserWindow *window)
+{
+    if (!window->sessionFile)
+        return;
+
+    WebKitWebViewSessionState *state = webkit_web_view_get_session_state(window->webView);
+    GBytes *bytes = webkit_web_view_session_state_serialize(state);
+    webkit_web_view_session_state_unref(state);
+    g_file_set_contents(window->sessionFile, g_bytes_get_data(bytes, NULL), g_bytes_get_size(bytes), NULL);
+    g_bytes_unref(bytes);
+}
+
 static gboolean browserWindowDeleteEvent(GtkWidget *widget, GdkEventAny* event)
 {
     BrowserWindow *window = BROWSER_WINDOW(widget);
+    browserWindowSaveSession(window);
     webkit_web_view_try_close(window->webView);
     return TRUE;
 }
@@ -1217,6 +1233,34 @@
     webkit_web_view_run_javascript(window->webView, strstr(uri, "_javascript_:"), NULL, NULL, NULL);
 }
 
+void browser_window_load_session(BrowserWindow *window, const char *sessionFile)
+{
+    g_return_if_fail(BROWSER_IS_WINDOW(window));
+    g_return_if_fail(sessionFile);
+
+    window->sessionFile = g_strdup(sessionFile);
+    gchar *data = ""
+    gsize dataLength;
+    if (g_file_get_contents(sessionFile, &data, &dataLength, NULL)) {
+        GBytes *bytes = g_bytes_new_take(data, dataLength);
+        WebKitWebViewSessionState *state = webkit_web_view_session_state_new(bytes);
+        g_bytes_unref(bytes);
+
+        if (state) {
+            webkit_web_view_restore_session_state(window->webView, state);
+            webkit_web_view_session_state_unref(state);
+        }
+    }
+
+    WebKitBackForwardList *bfList = webkit_web_view_get_back_forward_list(window->webView);
+    WebKitBackForwardListItem *item = webkit_back_forward_list_get_current_item(bfList);
+    if (item)
+        webkit_web_view_go_to_back_forward_list_item(window->webView, item);
+    else
+        webkit_web_view_load_uri(window->webView, BROWSER_DEFAULT_URL);
+
+}
+
 void browser_window_set_background_color(BrowserWindow *window, GdkRGBA *rgba)
 {
     g_return_if_fail(BROWSER_IS_WINDOW(window));

Modified: trunk/Tools/MiniBrowser/gtk/BrowserWindow.h (195285 => 195286)


--- trunk/Tools/MiniBrowser/gtk/BrowserWindow.h	2016-01-19 13:14:17 UTC (rev 195285)
+++ trunk/Tools/MiniBrowser/gtk/BrowserWindow.h	2016-01-19 13:15:59 UTC (rev 195286)
@@ -47,6 +47,7 @@
 GtkWidget* browser_window_new(WebKitWebView*, GtkWindow*);
 WebKitWebView* browser_window_get_view(BrowserWindow*);
 void browser_window_load_uri(BrowserWindow *, const char *uri);
+void browser_window_load_session(BrowserWindow *, const char *sessionFile);
 void browser_window_set_background_color(BrowserWindow*, GdkRGBA*);
 
 G_END_DECLS

Modified: trunk/Tools/MiniBrowser/gtk/main.c (195285 => 195286)


--- trunk/Tools/MiniBrowser/gtk/main.c	2016-01-19 13:14:17 UTC (rev 195285)
+++ trunk/Tools/MiniBrowser/gtk/main.c	2016-01-19 13:15:59 UTC (rev 195286)
@@ -39,6 +39,7 @@
 static const char *miniBrowserAboutScheme = "minibrowser-about";
 static GdkRGBA *backgroundColor;
 static gboolean editorMode;
+static const char *sessionFile;
 
 typedef enum {
     MINI_BROWSER_ERROR_INVALID_ABOUT_PATH
@@ -58,7 +59,7 @@
     return fileURL;
 }
 
-static void createBrowserWindow(const gchar *uri, WebKitSettings *webkitSettings)
+static void createBrowserWindow(const gchar *uri, WebKitSettings *webkitSettings, gboolean shouldLoadSession)
 {
     GtkWidget *webView = webkit_web_view_new();
     if (editorMode)
@@ -71,9 +72,13 @@
         webkit_web_view_set_settings(WEBKIT_WEB_VIEW(webView), webkitSettings);
 
     if (!editorMode) {
-        gchar *url = ""
-        browser_window_load_uri(BROWSER_WINDOW(mainWindow), url);
-        g_free(url);
+        if (shouldLoadSession && sessionFile)
+            browser_window_load_session(BROWSER_WINDOW(mainWindow), sessionFile);
+        else {
+            gchar *url = ""
+            browser_window_load_uri(BROWSER_WINDOW(mainWindow), url);
+            g_free(url);
+        }
     }
 
     gtk_widget_grab_focus(webView);
@@ -96,6 +101,7 @@
 {
     { "bg-color", 0, 0, G_OPTION_ARG_CALLBACK, parseBackgroundColor, "Background color", NULL },
     { "editor-mode", 'e', 0, G_OPTION_ARG_NONE, &editorMode, "Run in editor mode", NULL },
+    { "session-file", 's', 0, G_OPTION_ARG_FILENAME, &sessionFile, "Session file", "FILE" },
     { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &uriArguments, 0, "[URL…]" },
     { 0, 0, 0, 0, 0, 0, 0 }
 };
@@ -299,9 +305,9 @@
         int i;
 
         for (i = 0; uriArguments[i]; i++)
-            createBrowserWindow(uriArguments[i], webkitSettings);
+            createBrowserWindow(uriArguments[i], webkitSettings, FALSE);
     } else
-        createBrowserWindow(BROWSER_DEFAULT_URL, webkitSettings);
+        createBrowserWindow(BROWSER_DEFAULT_URL, webkitSettings, TRUE);
 
     g_clear_object(&webkitSettings);
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to