<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[195286] trunk/Tools</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/195286">195286</a></dd>
<dt>Author</dt> <dd>carlosgc@webkit.org</dd>
<dt>Date</dt> <dd>2016-01-19 05:15:59 -0800 (Tue, 19 Jan 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>[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.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsMiniBrowsergtkBrowserWindowc">trunk/Tools/MiniBrowser/gtk/BrowserWindow.c</a></li>
<li><a href="#trunkToolsMiniBrowsergtkBrowserWindowh">trunk/Tools/MiniBrowser/gtk/BrowserWindow.h</a></li>
<li><a href="#trunkToolsMiniBrowsergtkmainc">trunk/Tools/MiniBrowser/gtk/main.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (195285 => 195286)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog        2016-01-19 13:14:17 UTC (rev 195285)
+++ trunk/Tools/ChangeLog        2016-01-19 13:15:59 UTC (rev 195286)
</span><span class="lines">@@ -1,5 +1,29 @@
</span><span class="cx"> 2016-01-19  Carlos Garcia Campos  &lt;cgarcia@igalia.com&gt;
</span><span class="cx"> 
</span><ins>+        [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  &lt;cgarcia@igalia.com&gt;
+
</ins><span class="cx">         Unreviewed. Fix GTK+ test /webkit2/WebKitWebView/geolocation-permission-requests after r195075.
</span><span class="cx"> 
</span><span class="cx">         Geolocation is no longer allowed for unique origins after r195075.
</span></span></pre></div>
<a id="trunkToolsMiniBrowsergtkBrowserWindowc"></a>
<div class="modfile"><h4>Modified: trunk/Tools/MiniBrowser/gtk/BrowserWindow.c (195285 => 195286)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -71,6 +71,7 @@
</span><span class="cx">     GtkWindow *parentWindow;
</span><span class="cx">     guint fullScreenMessageLabelId;
</span><span class="cx">     guint resetEntryProgressTimeoutId;
</span><ins>+    gchar *sessionFile;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> struct _BrowserWindowClass {
</span><span class="lines">@@ -779,6 +780,8 @@
</span><span class="cx">     if (window-&gt;resetEntryProgressTimeoutId)
</span><span class="cx">         g_source_remove(window-&gt;resetEntryProgressTimeoutId);
</span><span class="cx"> 
</span><ins>+    g_free(window-&gt;sessionFile);
+
</ins><span class="cx">     G_OBJECT_CLASS(browser_window_parent_class)-&gt;finalize(gObject);
</span><span class="cx"> 
</span><span class="cx">     if (g_atomic_int_dec_and_test(&amp;windowCount))
</span><span class="lines">@@ -1151,9 +1154,22 @@
</span><span class="cx">         webkit_web_view_load_html(window-&gt;webView, &quot;&lt;html&gt;&lt;/html&gt;&quot;, &quot;file:///&quot;);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void browserWindowSaveSession(BrowserWindow *window)
+{
+    if (!window-&gt;sessionFile)
+        return;
+
+    WebKitWebViewSessionState *state = webkit_web_view_get_session_state(window-&gt;webView);
+    GBytes *bytes = webkit_web_view_session_state_serialize(state);
+    webkit_web_view_session_state_unref(state);
+    g_file_set_contents(window-&gt;sessionFile, g_bytes_get_data(bytes, NULL), g_bytes_get_size(bytes), NULL);
+    g_bytes_unref(bytes);
+}
+
</ins><span class="cx"> static gboolean browserWindowDeleteEvent(GtkWidget *widget, GdkEventAny* event)
</span><span class="cx"> {
</span><span class="cx">     BrowserWindow *window = BROWSER_WINDOW(widget);
</span><ins>+    browserWindowSaveSession(window);
</ins><span class="cx">     webkit_web_view_try_close(window-&gt;webView);
</span><span class="cx">     return TRUE;
</span><span class="cx"> }
</span><span class="lines">@@ -1217,6 +1233,34 @@
</span><span class="cx">     webkit_web_view_run_javascript(window-&gt;webView, strstr(uri, &quot;javascript:&quot;), NULL, NULL, NULL);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void browser_window_load_session(BrowserWindow *window, const char *sessionFile)
+{
+    g_return_if_fail(BROWSER_IS_WINDOW(window));
+    g_return_if_fail(sessionFile);
+
+    window-&gt;sessionFile = g_strdup(sessionFile);
+    gchar *data = NULL;
+    gsize dataLength;
+    if (g_file_get_contents(sessionFile, &amp;data, &amp;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-&gt;webView, state);
+            webkit_web_view_session_state_unref(state);
+        }
+    }
+
+    WebKitBackForwardList *bfList = webkit_web_view_get_back_forward_list(window-&gt;webView);
+    WebKitBackForwardListItem *item = webkit_back_forward_list_get_current_item(bfList);
+    if (item)
+        webkit_web_view_go_to_back_forward_list_item(window-&gt;webView, item);
+    else
+        webkit_web_view_load_uri(window-&gt;webView, BROWSER_DEFAULT_URL);
+
+}
+
</ins><span class="cx"> void browser_window_set_background_color(BrowserWindow *window, GdkRGBA *rgba)
</span><span class="cx"> {
</span><span class="cx">     g_return_if_fail(BROWSER_IS_WINDOW(window));
</span></span></pre></div>
<a id="trunkToolsMiniBrowsergtkBrowserWindowh"></a>
<div class="modfile"><h4>Modified: trunk/Tools/MiniBrowser/gtk/BrowserWindow.h (195285 => 195286)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -47,6 +47,7 @@
</span><span class="cx"> GtkWidget* browser_window_new(WebKitWebView*, GtkWindow*);
</span><span class="cx"> WebKitWebView* browser_window_get_view(BrowserWindow*);
</span><span class="cx"> void browser_window_load_uri(BrowserWindow *, const char *uri);
</span><ins>+void browser_window_load_session(BrowserWindow *, const char *sessionFile);
</ins><span class="cx"> void browser_window_set_background_color(BrowserWindow*, GdkRGBA*);
</span><span class="cx"> 
</span><span class="cx"> G_END_DECLS
</span></span></pre></div>
<a id="trunkToolsMiniBrowsergtkmainc"></a>
<div class="modfile"><h4>Modified: trunk/Tools/MiniBrowser/gtk/main.c (195285 => 195286)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -39,6 +39,7 @@
</span><span class="cx"> static const char *miniBrowserAboutScheme = &quot;minibrowser-about&quot;;
</span><span class="cx"> static GdkRGBA *backgroundColor;
</span><span class="cx"> static gboolean editorMode;
</span><ins>+static const char *sessionFile;
</ins><span class="cx"> 
</span><span class="cx"> typedef enum {
</span><span class="cx">     MINI_BROWSER_ERROR_INVALID_ABOUT_PATH
</span><span class="lines">@@ -58,7 +59,7 @@
</span><span class="cx">     return fileURL;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static void createBrowserWindow(const gchar *uri, WebKitSettings *webkitSettings)
</del><ins>+static void createBrowserWindow(const gchar *uri, WebKitSettings *webkitSettings, gboolean shouldLoadSession)
</ins><span class="cx"> {
</span><span class="cx">     GtkWidget *webView = webkit_web_view_new();
</span><span class="cx">     if (editorMode)
</span><span class="lines">@@ -71,9 +72,13 @@
</span><span class="cx">         webkit_web_view_set_settings(WEBKIT_WEB_VIEW(webView), webkitSettings);
</span><span class="cx"> 
</span><span class="cx">     if (!editorMode) {
</span><del>-        gchar *url = argumentToURL(uri);
-        browser_window_load_uri(BROWSER_WINDOW(mainWindow), url);
-        g_free(url);
</del><ins>+        if (shouldLoadSession &amp;&amp; sessionFile)
+            browser_window_load_session(BROWSER_WINDOW(mainWindow), sessionFile);
+        else {
+            gchar *url = argumentToURL(uri);
+            browser_window_load_uri(BROWSER_WINDOW(mainWindow), url);
+            g_free(url);
+        }
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     gtk_widget_grab_focus(webView);
</span><span class="lines">@@ -96,6 +101,7 @@
</span><span class="cx"> {
</span><span class="cx">     { &quot;bg-color&quot;, 0, 0, G_OPTION_ARG_CALLBACK, parseBackgroundColor, &quot;Background color&quot;, NULL },
</span><span class="cx">     { &quot;editor-mode&quot;, 'e', 0, G_OPTION_ARG_NONE, &amp;editorMode, &quot;Run in editor mode&quot;, NULL },
</span><ins>+    { &quot;session-file&quot;, 's', 0, G_OPTION_ARG_FILENAME, &amp;sessionFile, &quot;Session file&quot;, &quot;FILE&quot; },
</ins><span class="cx">     { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &amp;uriArguments, 0, &quot;[URL…]&quot; },
</span><span class="cx">     { 0, 0, 0, 0, 0, 0, 0 }
</span><span class="cx"> };
</span><span class="lines">@@ -299,9 +305,9 @@
</span><span class="cx">         int i;
</span><span class="cx"> 
</span><span class="cx">         for (i = 0; uriArguments[i]; i++)
</span><del>-            createBrowserWindow(uriArguments[i], webkitSettings);
</del><ins>+            createBrowserWindow(uriArguments[i], webkitSettings, FALSE);
</ins><span class="cx">     } else
</span><del>-        createBrowserWindow(BROWSER_DEFAULT_URL, webkitSettings);
</del><ins>+        createBrowserWindow(BROWSER_DEFAULT_URL, webkitSettings, TRUE);
</ins><span class="cx"> 
</span><span class="cx">     g_clear_object(&amp;webkitSettings);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>