[epiphany/wip/sync: 78/86] synchronizable: Implement _from_bso() method



commit 56c7c19330fcd3204cdecc49bc808e5690b84a20
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Wed Mar 22 15:05:59 2017 +0200

    synchronizable: Implement _from_bso() method

 src/sync/ephy-synchronizable.c |   56 ++++++++++++++++++++++++++++++++--------
 src/sync/ephy-synchronizable.h |   27 ++++++++++--------
 2 files changed, 60 insertions(+), 23 deletions(-)
---
diff --git a/src/sync/ephy-synchronizable.c b/src/sync/ephy-synchronizable.c
index 1179b55..6b3543e 100644
--- a/src/sync/ephy-synchronizable.c
+++ b/src/sync/ephy-synchronizable.c
@@ -159,13 +159,13 @@ ephy_synchronizable_set_is_uploaded (EphySynchronizable *synchronizable,
  * ephy_synchronizable_to_bso:
  * @synchronizable: an #EphySynchronizable
  *
- * Converts an #EphySynchronizable into its string representation
+ * Converts an #EphySynchronizable into its JSON string representation
  * of a Basic Storage Object from the client's point of view
  * (i.e. the %modified field is missing). Check the BSO format documentation
  * (https://docs.services.mozilla.com/storage/apis-1.5.html#basic-storage-object)
  * for more details.
  *
- * Return value: (transfer full): @synchronizable's BSO's string representation
+ * Return value: (transfer full): @synchronizable's BSO's JSON string representation
  **/
 char *
 ephy_synchronizable_to_bso (EphySynchronizable *synchronizable)
@@ -180,12 +180,12 @@ ephy_synchronizable_to_bso (EphySynchronizable *synchronizable)
 
 /**
  * ephy_synchronizable_from_bso:
- * @bso: an #JsonObject holding the JSON representation of a Basic Storage Object
+ * @bso: a JSON object representing the Basic Storage Object
  * @gtype: the #GType of object to construct
  *
- * Converts a JSON representation of a Basic Storage Object
+ * Converts a JSON object representing the Basic Storage Object
  * from the server's point of view (i.e. the %modified field is present)
- * into an object of type @gtype. Check the BSO format documentation
+ * into an object of type @gtype. See the BSO format documentation
  * (https://docs.services.mozilla.com/storage/apis-1.5.html#basic-storage-object)
  * for more details.
  *
@@ -196,12 +196,46 @@ ephy_synchronizable_to_bso (EphySynchronizable *synchronizable)
  *  Return value: (transfer full): a #GObject or %NULL
  **/
 GObject *
-ephy_synchronizable_from_bso (JsonObject *bso,
-                              GType       gtype)
+ephy_synchronizable_from_bso (JsonObject          *bso,
+                              GType                gtype,
+                              SyncCryptoKeyBundle *bundle)
 {
-  g_return_val_if_fail (bso, NULL);
-
-  /* TODO: Implement this. */
+  GObject *object = NULL;
+  GError *error = NULL;
+  char *serialized;
+  const char *payload;
+  double modified;
 
-  return NULL;
+  g_return_val_if_fail (bso, NULL);
+  g_return_val_if_fail (bundle, NULL);
+
+  if (!json_object_has_member (bso, "id") ||
+      !json_object_has_member (bso, "payload") ||
+      !json_object_has_member (bso, "modified")) {
+    g_warning ("BSO has missing members");
+    goto out;
+  }
+
+  payload = json_object_get_string_member (bso, "payload");
+  modified = json_object_get_double_member (bso, "modified");
+  serialized = ephy_sync_crypto_decrypt_record (payload, bundle);
+  if (!serialized) {
+    g_warning ("Failed to decrypt the BSO payload");
+    goto out;
+  }
+
+  object = json_gobject_from_data (gtype, serialized, -1, &error);
+  if (error) {
+    g_warning ("Failed to create GObject from BSO: %s", error->message);
+    g_error_free (error);
+    goto free_serialized;
+  }
+
+  ephy_synchronizable_set_modification_time (EPHY_SYNCHRONIZABLE (object), modified);
+  ephy_synchronizable_set_is_uploaded (EPHY_SYNCHRONIZABLE (object), TRUE);
+
+free_serialized:
+  g_free (serialized);
+out:
+  return object;
 }
diff --git a/src/sync/ephy-synchronizable.h b/src/sync/ephy-synchronizable.h
index 241c978..c90a19c 100644
--- a/src/sync/ephy-synchronizable.h
+++ b/src/sync/ephy-synchronizable.h
@@ -20,6 +20,8 @@
 
 #pragma once
 
+#include "ephy-sync-crypto.h"
+
 #include <glib-object.h>
 #include <json-glib/json-glib.h>
 
@@ -51,19 +53,20 @@ struct _EphySynchronizableInterface {
   char *       (*to_bso)                  (EphySynchronizable *synchronizable);
 };
 
-const char          *ephy_synchronizable_get_id                 (EphySynchronizable *synchronizable);
-void                 ephy_synchronizable_set_id                 (EphySynchronizable *synchronizable,
-                                                                 const char         *id);
-double               ephy_synchronizable_get_modification_time  (EphySynchronizable *synchronizable);
-void                 ephy_synchronizable_set_modification_time  (EphySynchronizable *synchronizable,
-                                                                 double              modified);
-gboolean             ephy_synchronizable_is_uploaded            (EphySynchronizable *synchronizable);
-void                 ephy_synchronizable_set_is_uploaded        (EphySynchronizable *synchronizable,
-                                                                 gboolean            uploaded);
-char                *ephy_synchronizable_to_bso                 (EphySynchronizable *synchronizable);
+const char          *ephy_synchronizable_get_id                 (EphySynchronizable  *synchronizable);
+void                 ephy_synchronizable_set_id                 (EphySynchronizable  *synchronizable,
+                                                                 const char          *id);
+double               ephy_synchronizable_get_modification_time  (EphySynchronizable  *synchronizable);
+void                 ephy_synchronizable_set_modification_time  (EphySynchronizable  *synchronizable,
+                                                                 double               modified);
+gboolean             ephy_synchronizable_is_uploaded            (EphySynchronizable  *synchronizable);
+void                 ephy_synchronizable_set_is_uploaded        (EphySynchronizable  *synchronizable,
+                                                                 gboolean             uploaded);
+char                *ephy_synchronizable_to_bso                 (EphySynchronizable  *synchronizable);
 /* This can't be an interface method because we lack the EphySynchronizable object.
  * Think of it as more of an utility function. */
-GObject             *ephy_synchronizable_from_bso   (JsonObject *bso,
-                                                     GType       gtype);
+GObject             *ephy_synchronizable_from_bso               (JsonObject          *bso,
+                                                                 GType                gtype,
+                                                                 SyncCryptoKeyBundle *bundle);
 
 G_END_DECLS


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