[epiphany/wip/google-safe-browsing: 6/6] gsb-service: Schedule next update when the current one ends



commit e0f0c259c4b796fe92af325923437fcafe15dffc
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Wed Sep 13 20:09:26 2017 +0300

    gsb-service: Schedule next update when the current one ends

 lib/safe-browsing/ephy-gsb-service.c |   42 +++++++++++++++++++++++++++++----
 1 files changed, 37 insertions(+), 5 deletions(-)
---
diff --git a/lib/safe-browsing/ephy-gsb-service.c b/lib/safe-browsing/ephy-gsb-service.c
index 6f7b14f..a2dfe3b 100644
--- a/lib/safe-browsing/ephy-gsb-service.c
+++ b/lib/safe-browsing/ephy-gsb-service.c
@@ -40,6 +40,7 @@ struct _EphyGSBService {
   char           *api_key;
   EphyGSBStorage *storage;
   gboolean        is_updating;
+  guint           source_id;
   SoupSession    *session;
 };
 
@@ -54,6 +55,8 @@ enum {
 
 static GParamSpec *obj_properties[LAST_PROP];
 
+static gboolean ephy_gsb_service_update (EphyGSBService *self);
+
 static inline gboolean
 json_object_has_non_null_string_member (JsonObject *object,
                                         const char *member)
@@ -71,6 +74,20 @@ json_object_has_non_null_array_member (JsonObject *object,
 }
 
 static void
+ephy_gsb_service_schedule_update (EphyGSBService *self,
+                                  gint64          interval)
+{
+  g_assert (EPHY_IS_GSB_SERVICE (self));
+  g_assert (ephy_gsb_storage_is_operable (self->storage));
+  g_assert (interval > 0);
+
+  self->source_id = g_timeout_add_seconds (interval,
+                                           (GSourceFunc)ephy_gsb_service_update,
+                                           self);
+  LOG ("Next update scheduled in %ld seconds", interval);
+}
+
+static void
 ephy_gsb_service_update_thread (GTask          *task,
                                 EphyGSBService *self,
                                 gpointer        task_data,
@@ -211,6 +228,8 @@ out:
   g_free (url);
   g_object_unref (msg);
   g_list_free_full (threat_lists, (GDestroyNotify)ephy_gsb_threat_list_free);
+
+  g_task_return_int (task, next_update_time);
 }
 
 static void
@@ -218,11 +237,14 @@ ephy_gsb_service_update_finished_cb (EphyGSBService *self,
                                      GAsyncResult   *result,
                                      gpointer        user_data)
 {
-  /* TODO: Schedule a next update in (next_update_time - CURRENT_TIME) seconds. */
+  gint64 next_update_time;
+
+  next_update_time = g_task_propagate_int (G_TASK (result), NULL);
+  ephy_gsb_service_schedule_update (self, next_update_time - CURRENT_TIME);
   self->is_updating = FALSE;
 }
 
-static void
+static gboolean
 ephy_gsb_service_update (EphyGSBService *self)
 {
   GTask *task;
@@ -236,6 +258,8 @@ ephy_gsb_service_update (EphyGSBService *self)
                      NULL);
   g_task_run_in_thread (task, (GTaskThreadFunc)ephy_gsb_service_update_thread);
   g_object_unref (task);
+
+  return G_SOURCE_REMOVE;
 }
 
 static void
@@ -299,6 +323,11 @@ ephy_gsb_service_dispose (GObject *object)
   g_clear_object (&self->storage);
   g_clear_object (&self->session);
 
+  if (self->source_id != 0) {
+    g_source_remove (self->source_id);
+    self->source_id = 0;
+  }
+
   G_OBJECT_CLASS (ephy_gsb_service_parent_class)->dispose (object);
 }
 
@@ -306,16 +335,19 @@ static void
 ephy_gsb_service_constructed (GObject *object)
 {
   EphyGSBService *self = EPHY_GSB_SERVICE (object);
-  gint64 next_update_time;
+  gint64 interval;
 
   G_OBJECT_CLASS (ephy_gsb_service_parent_class)->constructed (object);
 
   if (!ephy_gsb_storage_is_operable (self->storage))
     return;
 
-  next_update_time = ephy_gsb_storage_get_next_update_time (self->storage);
-  if (CURRENT_TIME >= next_update_time)
+  interval = ephy_gsb_storage_get_next_update_time (self->storage) - CURRENT_TIME;
+
+  if (interval <= 0)
     ephy_gsb_service_update (self);
+  else
+    ephy_gsb_service_schedule_update (self, interval);
 }
 
 static void


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