[epiphany] Port to wk2 webview's snapshotting API



commit 628831d4ca65188f268ed933e3129ceca28702da
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Sun Dec 16 20:22:41 2012 +0100

    Port to wk2 webview's snapshotting API
    
    https://bugzilla.gnome.org/show_bug.cgi?id=695347

 embed/ephy-web-view.c       |   54 +++++++++++++++++++++++++++++++++++++-
 lib/ephy-snapshot-service.c |   59 +++++++++++++++++++++++++++++++++++--------
 2 files changed, 100 insertions(+), 13 deletions(-)
---
diff --git a/embed/ephy-web-view.c b/embed/ephy-web-view.c
index a5346b0..16e1d83 100644
--- a/embed/ephy-web-view.c
+++ b/embed/ephy-web-view.c
@@ -2032,26 +2032,70 @@ ephy_web_view_location_changed (EphyWebView *view,
   g_object_thaw_notify (object);
 }
 
-#ifndef HAVE_WEBKIT2
+#ifdef HAVE_WEBKIT2
+static void
+on_snapshot_ready (WebKitWebView *webview,
+                   GAsyncResult *res,
+                   GtkTreeRowReference *ref)
+{
+  GtkTreeModel *model;
+  GtkTreePath *path;
+  GtkTreeIter iter;
+  cairo_surface_t *surface;
+  GError *error = NULL;
+
+  surface = webkit_web_view_get_snapshot_finish (webview, res, &error);
+  if (error) {
+    g_warning ("%s(): %s", G_STRFUNC, error->message);
+    g_error_free (error);
+    return;
+  }
+
+  model = gtk_tree_row_reference_get_model (ref);
+  path = gtk_tree_row_reference_get_path (ref);
+  gtk_tree_model_get_iter (model, &iter, path);
+  gtk_tree_path_free (path);
+
+  ephy_overview_store_set_snapshot (EPHY_OVERVIEW_STORE (model), &iter, surface);
+  cairo_surface_destroy (surface);
+}
+#endif
+
+/* FIXME: We should be using the snapshot service for this instead of
+   using the WK API directly. */
 static gboolean
 web_view_check_snapshot (WebKitWebView *web_view)
 {
   EphyOverviewStore *store;
   GtkTreeIter iter;
+#ifdef HAVE_WEBKIT2
+  GtkTreeRowReference *ref;
+  GtkTreePath *path;
+#else
   cairo_surface_t *surface;
+#endif
   EphyEmbedShell *embed_shell = ephy_embed_shell_get_default ();
 
   store = EPHY_OVERVIEW_STORE (ephy_embed_shell_get_frecent_store (embed_shell));
   if (ephy_overview_store_find_url (store, webkit_web_view_get_uri (web_view), &iter) &&
       ephy_overview_store_needs_snapshot (store, &iter)) {
+#ifdef HAVE_WEBKIT2
+    path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
+    ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store), path);
+    gtk_tree_path_free (path);
+    webkit_web_view_get_snapshot (web_view, WEBKIT_SNAPSHOT_REGION_VISIBLE,
+                                  WEBKIT_SNAPSHOT_OPTIONS_NONE,
+                                  NULL, (GAsyncReadyCallback)on_snapshot_ready,
+                                  ref);
+#else
     surface = webkit_web_view_get_snapshot (web_view);
     ephy_overview_store_set_snapshot (store, &iter, surface);
     cairo_surface_destroy (surface);
+#endif
   }
 
   return FALSE;
 }
-#endif
 
 #ifdef HAVE_WEBKIT2
 static void
@@ -2158,6 +2202,12 @@ load_changed_cb (WebKitWebView *web_view,
     /* Reset visit type. */
     priv->visit_type = EPHY_PAGE_VISIT_NONE;
 
+    if (!ephy_web_view_is_history_frozen (view)) {
+      if (priv->snapshot_idle_id)
+        g_source_remove (priv->snapshot_idle_id);
+      priv->snapshot_idle_id = g_idle_add_full (G_PRIORITY_LOW, (GSourceFunc)web_view_check_snapshot, 
web_view, NULL);
+    }
+
     break;
   }
 
diff --git a/lib/ephy-snapshot-service.c b/lib/ephy-snapshot-service.c
index 27b373c..bc1bd2e 100644
--- a/lib/ephy-snapshot-service.c
+++ b/lib/ephy-snapshot-service.c
@@ -162,21 +162,63 @@ snapshot_saved (EphySnapshotService *service,
   g_object_unref (simple);
 }
 
+static void
+save_snapshot (cairo_surface_t *surface,
+               GSimpleAsyncResult *result)
+{
+  SnapshotAsyncData *data;
+  EphySnapshotService *service;
+
+  data = (SnapshotAsyncData *)g_simple_async_result_get_op_res_gpointer (result);
+  data->snapshot = ephy_snapshot_service_crop_snapshot (surface);
+
+  service = (EphySnapshotService *)g_async_result_get_source_object (G_ASYNC_RESULT (result));
+  ephy_snapshot_service_save_snapshot_async (service, data->snapshot,
+                                             webkit_web_view_get_uri (data->web_view),
+                                             data->mtime, data->cancellable,
+                                             (GAsyncReadyCallback)snapshot_saved, result);
+}
+
+#ifdef HAVE_WEBKIT2
+static void
+on_snapshot_ready (WebKitWebView *webview,
+                   GAsyncResult *result,
+                   GSimpleAsyncResult *simple)
+{
+  cairo_surface_t *surface;
+  GError *error = NULL;
+
+  surface = webkit_web_view_get_snapshot_finish (webview, result, &error);
+  if (error) {
+    g_simple_async_result_take_error (simple, error);
+    g_simple_async_result_complete (simple);
+    g_object_unref (simple);
+    return;
+  }
+
+  save_snapshot (surface, simple);
+  cairo_surface_destroy (surface);
+}
+#endif
+
 static gboolean
 retrieve_snapshot_from_web_view (GSimpleAsyncResult *result)
 {
-  EphySnapshotService *service;
+#ifndef HAVE_WEBKIT2
   cairo_surface_t *surface;
+#endif
   SnapshotAsyncData *data;
 
   data = (SnapshotAsyncData *)g_simple_async_result_get_op_res_gpointer (result);
 
 #ifdef HAVE_WEBKIT2
-  /* FIXME: We need to add this API to WebKit2. */
-  surface = NULL;
+  webkit_web_view_get_snapshot (data->web_view,
+                                WEBKIT_SNAPSHOT_REGION_VISIBLE,
+                                WEBKIT_SNAPSHOT_OPTIONS_NONE,
+                                NULL, (GAsyncReadyCallback)on_snapshot_ready,
+                                result);
 #else
   surface = webkit_web_view_get_snapshot (data->web_view);
-#endif
 
   if (surface == NULL) {
     g_simple_async_result_set_error (result,
@@ -189,14 +231,9 @@ retrieve_snapshot_from_web_view (GSimpleAsyncResult *result)
     return FALSE;
   }
 
-  data->snapshot = ephy_snapshot_service_crop_snapshot (surface);
+  save_snapshot (surface, result);
   cairo_surface_destroy (surface);
-
-  service = (EphySnapshotService *)g_async_result_get_source_object (G_ASYNC_RESULT (result));
-  ephy_snapshot_service_save_snapshot_async (service, data->snapshot,
-                                             webkit_web_view_get_uri (data->web_view),
-                                             data->mtime, data->cancellable,
-                                             (GAsyncReadyCallback)snapshot_saved, result);
+#endif
 
   return FALSE;
 }


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