[epiphany/gnome-3-18] snapshot-service: Always return snapshots immediately if available



commit 1278d83dbb2832938f997cea976aefe92bb8a968
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Wed Apr 13 17:05:19 2016 -0500

    snapshot-service: Always return snapshots immediately if available
    
    Return a stale snapshot, then schedule creation of a new snapshot. This
    way, we show a preexisting snapshot even if snapshot creation fails. The
    new snapshot will be used only for future requests.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=763184

 lib/ephy-snapshot-service.c |   30 +++++++++++++++++++++++++++---
 1 files changed, 27 insertions(+), 3 deletions(-)
---
diff --git a/lib/ephy-snapshot-service.c b/lib/ephy-snapshot-service.c
index a44bc93..bae0bac 100644
--- a/lib/ephy-snapshot-service.c
+++ b/lib/ephy-snapshot-service.c
@@ -602,6 +602,28 @@ got_snapshot_for_url (EphySnapshotService *service,
   ephy_snapshot_service_take_from_webview (task);
 }
 
+/* We want to return an existing snapshot immediately, even if it is stale,
+ * because snapshot creation is best-effort and often fails (e.g. if the user
+ * navigates away from the page too soon), and we must be sure to return an old
+ * result if a new one does not yet exist.
+ */
+static void
+ensure_snapshot_freshness_for_web_view (EphySnapshotService *service,
+                                        WebKitWebView       *web_view)
+{
+  GTask *task;
+  const char *uri;
+
+  uri = webkit_web_view_get_uri (web_view);
+  if (ephy_snapshot_service_lookup_snapshot_freshness (service, uri) != SNAPSHOT_FRESH) {
+    task = g_task_new (service, NULL, NULL, NULL);
+    g_task_set_task_data (task,
+                          snapshot_async_data_new (web_view, time (NULL)),
+                          (GDestroyNotify)snapshot_async_data_free);
+    g_idle_add ((GSourceFunc)ephy_snapshot_service_take_from_webview, task);
+  }
+}
+
 /**
  * ephy_snapshot_service_get_snapshot_async:
  * @service: a #EphySnapshotService
@@ -638,11 +660,12 @@ ephy_snapshot_service_get_snapshot_async (EphySnapshotService *service,
    * the snapshot path is in memory cache; this is an indication that the
    * snapshot is fresh. */
   uri = webkit_web_view_get_uri (web_view);
-  if (uri && ephy_snapshot_service_lookup_snapshot_freshness (service, uri) == SNAPSHOT_FRESH) {
+  if (uri) {
     ephy_snapshot_service_get_snapshot_for_url_async (service,
                                                       uri, mtime, cancellable,
                                                       (GAsyncReadyCallback)got_snapshot_for_url,
                                                       task);
+    ensure_snapshot_freshness_for_web_view (service, web_view);
   } else {
     g_idle_add ((GSourceFunc)ephy_snapshot_service_take_from_webview, task);
   }
@@ -892,8 +915,7 @@ ephy_snapshot_service_get_snapshot_path_async (EphySnapshotService *service,
   task = g_task_new (service, cancellable, callback, user_data);
 
   uri = webkit_web_view_get_uri (web_view);
-
-  if (uri && ephy_snapshot_service_lookup_snapshot_freshness (service, uri) == SNAPSHOT_FRESH) {
+  if (uri) {
     const char *path = ephy_snapshot_service_lookup_snapshot_path (service, uri);
     if (path) {
       g_task_return_pointer (task, g_strdup (path), g_free);
@@ -907,6 +929,8 @@ ephy_snapshot_service_get_snapshot_path_async (EphySnapshotService *service,
                                                              (GAsyncReadyCallback)got_snapshot_path_for_url,
                                                              task);
     }
+
+    ensure_snapshot_freshness_for_web_view (service, web_view);
   } else {
     g_task_set_task_data (task,
                           snapshot_async_data_new (web_view, mtime),


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