[epiphany/wip/sync: 85/86] synchronizable: Implement _from_bso() method
- From: Gabriel Ivașcu <gabrielivascu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/wip/sync: 85/86] synchronizable: Implement _from_bso() method
- Date: Wed, 22 Mar 2017 15:37:04 +0000 (UTC)
commit 4ab38bc1b2ab95b01f9878435fcd99aca9dadd7c
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 | 69 +++++++++++++++++++++++++++++++++++-----
src/sync/ephy-synchronizable.h | 27 +++++++++-------
2 files changed, 76 insertions(+), 20 deletions(-)
---
diff --git a/src/sync/ephy-synchronizable.c b/src/sync/ephy-synchronizable.c
index 1179b55..9d67555 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,10 +180,10 @@ ephy_synchronizable_to_bso (EphySynchronizable *synchronizable)
/**
* ephy_synchronizable_from_bso:
- * @bso: an #JsonObject holding the JSON representation of a Basic Storage Object
+ * @bso: the JSON string representation of a Basic Storage Object
* @gtype: the #GType of object to construct
*
- * Converts a JSON representation of a Basic Storage Object
+ * Converts the JSON string representation of a 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
* (https://docs.services.mozilla.com/storage/apis-1.5.html#basic-storage-object)
@@ -196,12 +196,65 @@ 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 (const char *bso,
+ GType gtype,
+ SyncCryptoKeyBundle *bundle)
{
+ GObject *object = NULL;
+ JsonParser *parser;
+ JsonNode *root;
+ JsonObject *json;
+ GError *error = NULL;
+ char *serialized;
+ const char *payload;
+ double modified;
+
g_return_val_if_fail (bso, NULL);
+ g_return_val_if_fail (bundle, NULL);
+
+ parser = json_parser_new ();
+ json_parser_load_from_data (parser, bso, -1, &error);
+ if (error) {
+ g_warning ("Failed to parse JSON string BSO: %s", error->message);
+ g_error_free (error);
+ goto free_parser;
+ }
+
+ root = json_parser_get_root (parser);
+ if (!JSON_NODE_HOLDS_OBJECT (root)) {
+ g_warning ("BSO does not hold JSON object");
+ goto free_parser;
+ }
+
+ json = json_node_get_object (root);
+ if (!json_object_has_member (json, "id") ||
+ !json_object_has_member (json, "payload") ||
+ !json_object_has_member (json, "modified")) {
+ g_warning ("BSO has missing members");
+ goto free_parser;
+ }
+
+ payload = json_object_get_string_member (json, "payload");
+ modified = json_object_get_double_member (json, "modified");
+ serialized = ephy_sync_crypto_decrypt_record (payload, bundle);
+ if (!serialized) {
+ g_warning ("Failed to decrypt the BSO payload");
+ goto free_parser;
+ }
+
+ 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);
- /* TODO: Implement this. */
+free_serialized:
+ g_free (serialized);
+free_parser:
+ g_object_unref (parser);
- return NULL;
+ return object;
}
diff --git a/src/sync/ephy-synchronizable.h b/src/sync/ephy-synchronizable.h
index 241c978..178e710 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 (const char *bso,
+ GType gtype,
+ SyncCryptoKeyBundle *bundle);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]