[epiphany] downloads: Move session inhibition handling to EphyDownloadsManager



commit 350f8dd1312f755a237bf0a4d972c51088f4d65d
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Wed Oct 7 12:30:25 2015 +0200

    downloads: Move session inhibition handling to EphyDownloadsManager
    
    Instead of calling gtk_application_inhibit() for every download, the
    download manager calls it only when not inhibited already. When there
    aren't actives downloads left, gtk_application_uninhibit() is called
    only once too. This is because gtk_application_inhibit/uninhibit() make
    a synchronous DBUS call to the session manager every time it's called.

 embed/ephy-download.c          |   53 ----------------------------------------
 embed/ephy-downloads-manager.c |   47 ++++++++++++++++++++++++++++++++++-
 2 files changed, 46 insertions(+), 54 deletions(-)
---
diff --git a/embed/ephy-download.c b/embed/ephy-download.c
index 9c91997..603a966 100644
--- a/embed/ephy-download.c
+++ b/embed/ephy-download.c
@@ -55,8 +55,6 @@ struct _EphyDownloadPrivate
   GError *error;
 
   GtkWindow *window;
-
-  guint inhibitor_cookie;
 };
 
 enum
@@ -467,40 +465,6 @@ ephy_download_get_start_time (EphyDownload *download)
   return download->priv->start_time;
 }
 
-static void
-acquire_session_inhibitor (EphyDownload *download)
-{
-  EphyDownloadPrivate *priv;
-  EphyEmbedShell *shell;
-
-  priv = download->priv;
-  shell = ephy_embed_shell_get_default ();
-
-  if (priv->inhibitor_cookie)
-    return;
-
-  priv->inhibitor_cookie = gtk_application_inhibit (GTK_APPLICATION (shell),
-                                                    priv->window,
-                                                    GTK_APPLICATION_INHIBIT_LOGOUT | 
GTK_APPLICATION_INHIBIT_SUSPEND,
-                                                    "Downloading");
-}
-
-static void
-release_session_inhibitor (EphyDownload *download)
-{
-  EphyDownloadPrivate *priv;
-  EphyEmbedShell *shell;
-
-  priv = download->priv;
-  shell = ephy_embed_shell_get_default ();
-
-  if (!priv->inhibitor_cookie)
-    return;
-
-  gtk_application_uninhibit (GTK_APPLICATION (shell), priv->inhibitor_cookie);
-  priv->inhibitor_cookie = 0;
-}
-
 /**
  * ephy_download_cancel:
  * @download: an #EphyDownload
@@ -614,8 +578,6 @@ ephy_download_dispose (GObject *object)
 
   priv = download->priv;
 
-  release_session_inhibitor (download);
-
   if (priv->download) {
     g_signal_handlers_disconnect_matched (priv->download, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, download);
     g_object_unref (priv->download);
@@ -784,14 +746,6 @@ ephy_download_init (EphyDownload *download)
   download->priv->window = NULL;
 }
 
-static void
-download_created_destination_cb (WebKitDownload *wk_download,
-                                 const gchar *destination,
-                                 EphyDownload *download)
-{
-  acquire_session_inhibitor (download);
-}
-
 static gboolean
 download_decide_destination_cb (WebKitDownload *wk_download,
                                 const gchar *suggested_filename,
@@ -824,8 +778,6 @@ download_finished_cb (WebKitDownload *wk_download,
     ephy_download_do_download_action (download, EPHY_DOWNLOAD_ACTION_AUTO);
   else
     ephy_download_do_download_action (download, priv->action);
-
-  release_session_inhibitor (download);
 }
 
 static void
@@ -839,8 +791,6 @@ download_failed_cb (WebKitDownload *wk_download,
   download->priv->finished = TRUE;
   download->priv->error = g_error_copy (error);
   g_signal_emit (download, signals[ERROR], 0, download->priv->error);
-
-  release_session_inhibitor (download);
 }
 
 /**
@@ -862,9 +812,6 @@ ephy_download_new (WebKitDownload *download,
 
   ephy_download = g_object_new (EPHY_TYPE_DOWNLOAD, "window", parent, NULL);
 
-  g_signal_connect (download, "created-destination",
-                    G_CALLBACK (download_created_destination_cb),
-                    ephy_download);
   g_signal_connect (download, "decide-destination",
                     G_CALLBACK (download_decide_destination_cb),
                     ephy_download);
diff --git a/embed/ephy-downloads-manager.c b/embed/ephy-downloads-manager.c
index d5feac7..cf34545 100644
--- a/embed/ephy-downloads-manager.c
+++ b/embed/ephy-downloads-manager.c
@@ -21,6 +21,8 @@
 #include "config.h"
 #include "ephy-downloads-manager.h"
 
+#include "ephy-embed-shell.h"
+
 enum {
   DOWNLOAD_ADDED,
   DOWNLOAD_REMOVED,
@@ -35,6 +37,9 @@ struct _EphyDownloadsManager
   GObject parent;
 
   GList *downloads;
+
+  guint inhibitors;
+  guint inhibitor_cookie;
 };
 
 struct _EphyDownloadsManagerClass
@@ -47,6 +52,31 @@ static guint signals[LAST_SIGNAL];
 G_DEFINE_TYPE (EphyDownloadsManager, ephy_downloads_manager, G_TYPE_OBJECT)
 
 static void
+ephy_downloads_manager_acquire_session_inhibitor (EphyDownloadsManager *manager)
+{
+  if (++manager->inhibitors > 1)
+    return;
+
+  g_assert (manager->inhibitor_cookie == 0);
+  manager->inhibitor_cookie = gtk_application_inhibit (GTK_APPLICATION (ephy_embed_shell_get_default ()),
+                                                       NULL,
+                                                       GTK_APPLICATION_INHIBIT_LOGOUT | 
GTK_APPLICATION_INHIBIT_SUSPEND,
+                                                      "Downloading");
+}
+
+static void
+ephy_downloads_manager_release_session_inhibitor (EphyDownloadsManager *manager)
+{
+  if (--manager->inhibitors > 0)
+    return;
+
+  g_assert (manager->inhibitor_cookie > 0);
+  gtk_application_uninhibit (GTK_APPLICATION (ephy_embed_shell_get_default ()),
+                             manager->inhibitor_cookie);
+  manager->inhibitor_cookie = 0;
+}
+
+static void
 ephy_downloads_manager_init (EphyDownloadsManager *manager)
 {
 }
@@ -100,6 +130,7 @@ download_completed_cb (EphyDownload         *download,
                        EphyDownloadsManager *manager)
 {
   g_signal_emit (manager, signals[ESTIMATED_PROGRESS_CHANGED], 0);
+  ephy_downloads_manager_release_session_inhibitor (manager);
 }
 
 static void
@@ -110,6 +141,7 @@ download_failed_cb (EphyDownload         *download,
   if (g_error_matches (error, WEBKIT_DOWNLOAD_ERROR, WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER))
     ephy_downloads_manager_remove_download (manager, download);
   g_signal_emit (manager, signals[ESTIMATED_PROGRESS_CHANGED], 0);
+  ephy_downloads_manager_release_session_inhibitor (manager);
 }
 
 static void
@@ -118,10 +150,18 @@ download_estimated_progress_changed_cb (EphyDownloadsManager *manager)
   g_signal_emit (manager, signals[ESTIMATED_PROGRESS_CHANGED], 0);
 }
 
+static void
+download_created_destination_cb (EphyDownloadsManager *manager)
+{
+  ephy_downloads_manager_acquire_session_inhibitor (manager);
+}
+
 void
 ephy_downloads_manager_add_download (EphyDownloadsManager *manager,
                                      EphyDownload         *download)
 {
+  WebKitDownload *wk_download;
+
   g_return_if_fail (EPHY_IS_DOWNLOADS_MANAGER (manager));
   g_return_if_fail (EPHY_IS_DOWNLOAD (download));
 
@@ -135,9 +175,14 @@ ephy_downloads_manager_add_download (EphyDownloadsManager *manager,
   g_signal_connect (download, "error",
                     G_CALLBACK (download_failed_cb),
                     manager);
-  g_signal_connect_swapped (ephy_download_get_webkit_download (download), "notify::estimated-progress",
+
+  wk_download = ephy_download_get_webkit_download (download);
+  g_signal_connect_swapped (wk_download, "notify::estimated-progress",
                             G_CALLBACK (download_estimated_progress_changed_cb),
                             manager);
+  g_signal_connect_swapped (wk_download, "created-destination",
+                            G_CALLBACK (download_created_destination_cb),
+                            manager);
   g_signal_emit (manager, signals[DOWNLOAD_ADDED], 0, download);
   g_signal_emit (manager, signals[ESTIMATED_PROGRESS_CHANGED], 0);
 }


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