[epiphany] ephy-session: Add ephy_session_resume() and use it instead of queueing a resume command



commit 59b0526207799db5e5e3039963e1cb0d08cc7642
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Mon Oct 8 11:39:31 2012 +0200

    ephy-session: Add ephy_session_resume() and use it instead of queueing a resume command
    
    https://bugzilla.gnome.org/show_bug.cgi?id=641739

 src/ephy-session.c        |  171 +++++++++++++++++++++++++--------------------
 src/ephy-session.h        |   10 +++-
 src/ephy-shell.c          |   34 +++++++---
 tests/ephy-session-test.c |    7 +--
 4 files changed, 130 insertions(+), 92 deletions(-)
---
diff --git a/src/ephy-session.c b/src/ephy-session.c
index 653fc44..bf96236 100644
--- a/src/ephy-session.c
+++ b/src/ephy-session.c
@@ -192,56 +192,6 @@ session_command_free (SessionCommand *cmd)
 	g_object_unref (ephy_shell_get_default ());
 }
 
-static int
-session_command_find (const SessionCommand *cmd,
-		      gpointer cmdptr)
-{
-	EphySessionCommand command = GPOINTER_TO_INT (cmdptr);
-
-	return command != cmd->command;
-}
-
-static void
-session_command_autoresume (EphySession *session,
-			    guint32 user_time)
-{
-	GFile *saved_session_file;
-	char *saved_session_file_path;
-	gboolean has_session_state;
-	EphyPrefsRestoreSessionPolicy policy;
-	EphyShell *shell;
-
-	LOG ("ephy_session_autoresume");
-
-	saved_session_file = get_session_file (SESSION_STATE);
-	saved_session_file_path = g_file_get_path (saved_session_file);
-	g_object_unref (saved_session_file);
-	has_session_state = g_file_test (saved_session_file_path, G_FILE_TEST_EXISTS);
-	
-	g_free (saved_session_file_path);
-
-	policy = g_settings_get_enum (EPHY_SETTINGS_MAIN,
-				      EPHY_PREFS_RESTORE_SESSION_POLICY);
-
-	shell = ephy_shell_get_default ();
-
-	if (has_session_state == FALSE ||
-	    policy == EPHY_PREFS_RESTORE_SESSION_POLICY_NEVER)
-	{
-		/* If we are auto-resuming, and we never want to
-		 * restore the session, clobber the session state
-		 * file. */
-		if (policy == EPHY_PREFS_RESTORE_SESSION_POLICY_NEVER)
-			session_delete (session, SESSION_STATE);
-
-		ephy_session_queue_command (session,
-					    EPHY_SESSION_CMD_MAYBE_OPEN_WINDOW,
-					    NULL, NULL, user_time, FALSE);
-	}
-	else if (ephy_shell_get_n_windows (shell) == 0)
-		ephy_session_load (session, SESSION_STATE, user_time, NULL, NULL, NULL);
-}
-
 static void
 session_command_open_uris (EphySession *session,
 			   char **uris,
@@ -345,9 +295,6 @@ session_command_dispatch (EphySession *session)
 
 	switch (cmd->command)
 	{
-		case EPHY_SESSION_CMD_RESUME_SESSION:
-			session_command_autoresume (session, cmd->user_time);
-			break;
 		case EPHY_SESSION_CMD_OPEN_URIS:
 			session_command_open_uris (session, cmd->args, cmd->arg, cmd->user_time);
 			break;
@@ -1335,6 +1282,101 @@ ephy_session_load_finish (EphySession *session,
 	return !g_simple_async_result_propagate_error (simple, error);
 }
 
+static gboolean
+session_state_file_exists (EphySession *session)
+{
+	GFile *saved_session_file;
+	char *saved_session_file_path;
+	gboolean retval;
+
+	saved_session_file = get_session_file (SESSION_STATE);
+	saved_session_file_path = g_file_get_path (saved_session_file);
+	g_object_unref (saved_session_file);
+	retval = g_file_test (saved_session_file_path, G_FILE_TEST_EXISTS);
+	g_free (saved_session_file_path);
+
+	return retval;
+}
+
+static void
+session_resumed_cb (GObject *object,
+		    GAsyncResult *result,
+		    gpointer user_data)
+{
+	EphySession *session = EPHY_SESSION (object);
+	GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
+	GError *error = NULL;
+
+	if (!ephy_session_load_finish (session, result, &error))
+		g_simple_async_result_take_error (simple, error);
+	g_simple_async_result_complete (simple);
+	g_object_unref (simple);
+}
+
+void
+ephy_session_resume (EphySession *session,
+		     guint32 user_time,
+		     GCancellable *cancellable,
+		     GAsyncReadyCallback callback,
+		     gpointer user_data)
+{
+	GSimpleAsyncResult *result;
+	gboolean has_session_state;
+	EphyPrefsRestoreSessionPolicy policy;
+	EphyShell *shell;
+
+	LOG ("ephy_session_autoresume");
+
+	result = g_simple_async_result_new (G_OBJECT (session), callback, user_data, ephy_session_resume);
+
+	has_session_state = session_state_file_exists (session);
+
+	policy = g_settings_get_enum (EPHY_SETTINGS_MAIN,
+				      EPHY_PREFS_RESTORE_SESSION_POLICY);
+
+	shell = ephy_shell_get_default ();
+
+	if (has_session_state == FALSE ||
+	    policy == EPHY_PREFS_RESTORE_SESSION_POLICY_NEVER)
+	{
+		/* If we are auto-resuming, and we never want to
+		 * restore the session, clobber the session state
+		 * file. */
+		if (policy == EPHY_PREFS_RESTORE_SESSION_POLICY_NEVER)
+			session_delete (session, SESSION_STATE);
+
+		ephy_session_queue_command (session,
+					    EPHY_SESSION_CMD_MAYBE_OPEN_WINDOW,
+					    NULL, NULL, user_time, FALSE);
+	}
+	else if (ephy_shell_get_n_windows (shell) == 0)
+	{
+		ephy_session_load (session, SESSION_STATE, user_time, cancellable,
+				   session_resumed_cb, result);
+		return;
+	}
+
+	g_simple_async_result_complete_in_idle (result);
+	g_object_unref (result);
+}
+
+
+gboolean
+ephy_session_resume_finish (EphySession *session,
+			    GAsyncResult *result,
+			    GError **error)
+{
+	GSimpleAsyncResult *simple;
+
+	g_return_val_if_fail (EPHY_IS_SESSION (session), FALSE);
+	g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
+
+	simple = G_SIMPLE_ASYNC_RESULT (result);
+	g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == ephy_session_resume);
+
+	return !g_simple_async_result_propagate_error (simple, error);
+}
+
 /**
  * ephy_session_queue_command:
  * @session: a #EphySession
@@ -1348,7 +1390,6 @@ ephy_session_queue_command (EphySession *session,
 			    gboolean priority)
 {
 	EphySessionPrivate *priv;
-	GList *element;
 	SessionCommand *cmd;
 
 	LOG ("queue_command command:%d", command);
@@ -1358,28 +1399,6 @@ ephy_session_queue_command (EphySession *session,
 
 	priv = session->priv;
 
-	/* First look if the same command is already queued */
-	if (command > EPHY_SESSION_CMD_RESUME_SESSION &&
-	    command < EPHY_SESSION_CMD_OPEN_URIS)
-	{
-		element = g_queue_find_custom (priv->queue,
-					       GINT_TO_POINTER (command),
-					       (GCompareFunc) session_command_find);
-		if (element != NULL)
-		{
-			cmd = (SessionCommand *) element->data;
-
-			if (command == EPHY_SESSION_CMD_RESUME_SESSION)
-			{
-				cmd->user_time = user_time;
-				g_queue_remove (priv->queue, cmd);
-				g_queue_push_tail (priv->queue, cmd);
-
-				return;
-			}
-		}
-	}
-
 	cmd = g_slice_new0 (SessionCommand);
 	cmd->command = command;
 	cmd->arg = arg ? g_strdup (arg) : NULL;
diff --git a/src/ephy-session.h b/src/ephy-session.h
index c264e0c..5d18cd4 100644
--- a/src/ephy-session.h
+++ b/src/ephy-session.h
@@ -45,7 +45,6 @@ typedef struct _EphySessionClass	EphySessionClass;
 
 typedef enum
 {
-	EPHY_SESSION_CMD_RESUME_SESSION,
 	EPHY_SESSION_CMD_OPEN_URIS,
 	EPHY_SESSION_CMD_MAYBE_OPEN_WINDOW,
 	EPHY_SESSION_CMD_MAYBE_OPEN_WINDOW_RESTORE,
@@ -89,6 +88,15 @@ void             ephy_session_load_from_stream        (EphySession *session,
 gboolean         ephy_session_load_from_stream_finish (EphySession *session,
                                                        GAsyncResult *result,
                                                        GError **error);
+void             ephy_session_resume                  (EphySession *session,
+                                                       guint32 user_time,
+                                                       GCancellable *cancellable,
+                                                       GAsyncReadyCallback callback,
+                                                       gpointer user_data);
+gboolean         ephy_session_resume_finish           (EphySession *session,
+                                                       GAsyncResult *result,
+                                                       GError **error);
+
 
 void             ephy_session_close                   (EphySession *session);
 
diff --git a/src/ephy-shell.c b/src/ephy-shell.c
index 1b24387..ee947ff 100644
--- a/src/ephy-shell.c
+++ b/src/ephy-shell.c
@@ -123,13 +123,6 @@ queue_commands (EphyShell *shell)
 
   ctx = shell->priv->startup_context;
 
-  /* We only get here when starting a new instance, so autoresume the
-   * session unless we are in application mode. */
-  if (ephy_embed_shell_get_mode (EPHY_EMBED_SHELL (shell)) != EPHY_EMBED_SHELL_MODE_APPLICATION)
-    ephy_session_queue_command (session,
-                                EPHY_SESSION_CMD_RESUME_SESSION,
-                                NULL, NULL, ctx->user_time, TRUE);
-
   if (ctx->session_filename != NULL)
     ephy_session_load (session, (const char *)ctx->session_filename,
                        ctx->user_time, NULL, NULL, NULL);
@@ -261,13 +254,36 @@ ephy_shell_startup (GApplication* application)
 }
 
 static void
+session_load_cb (GObject *object,
+                 GAsyncResult *result,
+                 gpointer user_data)
+{
+  EphySession *session = EPHY_SESSION (object);
+  EphyShell *shell = EPHY_SHELL (user_data);
+
+  ephy_session_resume_finish (session, result, NULL);
+  queue_commands (shell);
+}
+
+static void
 ephy_shell_activate (GApplication *application)
 {
+  EphyShell *shell = EPHY_SHELL (application);
+
   /*
-   * We get here on each new instance (remote or not). Queue the
+   * We get here on each new instance (remote or not). Autoresume the
+   * session unless we are in application mode and queue the
    * commands.
    */
-  queue_commands (EPHY_SHELL (application));
+  if (ephy_embed_shell_get_mode (EPHY_EMBED_SHELL (shell)) != EPHY_EMBED_SHELL_MODE_APPLICATION) {
+    EphyShellStartupContext *ctx;
+
+    ctx = shell->priv->startup_context;
+    ephy_session_resume (EPHY_SESSION (ephy_shell_get_session (shell)),
+                         ctx->user_time, NULL, session_load_cb, shell);
+  }
+  else
+    queue_commands (shell);
 }
 
 /*
diff --git a/tests/ephy-session-test.c b/tests/ephy-session-test.c
index c8d2dc7..554544d 100644
--- a/tests/ephy-session-test.c
+++ b/tests/ephy-session-test.c
@@ -223,12 +223,7 @@ open_uris_after_loading_session (const char** uris, int final_num_windows)
      */
     ephy_session_save (session, "type:session_state");
 
-    ephy_session_queue_command (session,
-                                EPHY_SESSION_CMD_RESUME_SESSION,
-                                "type:session_state",
-                                NULL,
-                                user_time,
-                                FALSE);
+    ephy_session_resume (session, user_time, NULL, NULL, NULL);
 
     /* Ensure the queue is processed. */
     while (gtk_events_pending ())



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]