[gcr] gcr: Allow using GBytes with GcrParser



commit 6924fa9fb478af6624cdd262bb9dfca7818c23af
Author: Stef Walter <stefw gnome org>
Date:   Wed Sep 25 10:49:33 2013 +0200

    gcr: Allow using GBytes with GcrParser
    
    This also solves a corner case where GcrParser would keep around a
    pointer to the data passed into gcr_parser_parse_data(), even after
    that data had been freed.
    
    gcr_parser_parse_data() now copies the data passed in, where as
    the new gcr_parser_parse_bytes() simply keeps a reference to the
    GBytes.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=708736

 docs/reference/gcr/gcr-sections.txt |    4 +
 gcr/gcr-base.symbols                |    3 +
 gcr/gcr-parser.c                    |  100 +++++++++++++++++++++++++++-----
 gcr/gcr-parser.h                    |    8 +++
 gcr/tests/frob-parser.c             |    6 +-
 gcr/tests/test-fingerprint.c        |    3 +-
 gcr/tests/test-parser.c             |  109 +++++++++++++++++++++++++++++++----
 gcr/tests/test-subject-public-key.c |    3 +-
 ui/gcr-ui.symbols                   |    1 +
 ui/gcr-unlock-renderer.c            |   26 ++------
 ui/gcr-unlock-renderer.h            |    6 +-
 ui/gcr-viewer-widget.c              |   51 ++++++++++++----
 ui/gcr-viewer-widget.h              |    4 +
 ui/tests/frob-certificate.c         |    6 +-
 ui/tests/frob-combo-selector.c      |    6 +-
 ui/tests/frob-key.c                 |    6 +-
 ui/tests/frob-request.c             |    4 +-
 ui/tests/frob-tree-selector.c       |    6 +-
 ui/tests/frob-unlock.c              |    6 +-
 19 files changed, 281 insertions(+), 77 deletions(-)
---
diff --git a/docs/reference/gcr/gcr-sections.txt b/docs/reference/gcr/gcr-sections.txt
index ed2a764..3144d00 100644
--- a/docs/reference/gcr/gcr-sections.txt
+++ b/docs/reference/gcr/gcr-sections.txt
@@ -7,6 +7,7 @@ GcrDataFormat
 GcrParser
 GcrParserClass
 gcr_parser_new
+gcr_parser_parse_bytes
 gcr_parser_parse_data
 gcr_parser_parse_stream
 gcr_parser_parse_stream_async
@@ -19,12 +20,14 @@ gcr_parser_get_parsed_label
 gcr_parser_get_parsed_description
 gcr_parser_get_parsed_attributes
 gcr_parser_get_parsed_block
+gcr_parser_get_parsed_bytes
 gcr_parser_get_parsed_format
 GcrParsed
 gcr_parsed_ref
 gcr_parsed_unref
 gcr_parsed_get_attributes
 gcr_parsed_get_data
+gcr_parsed_get_bytes
 gcr_parsed_get_description
 gcr_parsed_get_format
 gcr_parsed_get_label
@@ -605,6 +608,7 @@ GcrUnlockOptionsWidgetPrivate
 <FILE>gcr-viewer-widget</FILE>
 GcrViewerWidget
 gcr_viewer_widget_new
+gcr_viewer_widget_load_bytes
 gcr_viewer_widget_load_data
 gcr_viewer_widget_load_file
 gcr_viewer_widget_get_parser
diff --git a/gcr/gcr-base.symbols b/gcr/gcr-base.symbols
index ec27371..935bf84 100644
--- a/gcr/gcr-base.symbols
+++ b/gcr/gcr-base.symbols
@@ -114,6 +114,7 @@ gcr_mock_prompter_start
 gcr_mock_prompter_stop
 gcr_mock_prompter_disconnect
 gcr_parsed_get_attributes
+gcr_parsed_get_bytes
 gcr_parsed_get_data
 gcr_parsed_get_description
 gcr_parsed_get_format
@@ -127,12 +128,14 @@ gcr_parser_format_enable
 gcr_parser_format_supported
 gcr_parser_get_parsed
 gcr_parser_get_parsed_attributes
+gcr_parser_get_parsed_bytes
 gcr_parser_get_parsed_block
 gcr_parser_get_parsed_description
 gcr_parser_get_parsed_format
 gcr_parser_get_parsed_label
 gcr_parser_get_type
 gcr_parser_new
+gcr_parser_parse_bytes
 gcr_parser_parse_data
 gcr_parser_parse_stream
 gcr_parser_parse_stream_async
diff --git a/gcr/gcr-parser.c b/gcr/gcr-parser.c
index 1a93f54..23ae80e 100644
--- a/gcr/gcr-parser.c
+++ b/gcr/gcr-parser.c
@@ -2370,10 +2370,9 @@ gcr_parser_add_password (GcrParser *self, const gchar *password)
 }
 
 /**
- * gcr_parser_parse_data:
+ * gcr_parser_parse_bytes:
  * @self: The parser
- * @data: (array length=n_data): the data to parse
- * @n_data: The length of the data
+ * @data: the data to parse
  * @error: A location to raise an error on failure.
  *
  * Parse the data. The #GcrParser::parsed and #GcrParser::authenticate signals
@@ -2382,22 +2381,20 @@ gcr_parser_add_password (GcrParser *self, const gchar *password)
  * Returns: Whether the data was parsed successfully or not.
  */
 gboolean
-gcr_parser_parse_data (GcrParser *self,
-                       const guchar *data,
-                       gsize n_data,
-                       GError **error)
+gcr_parser_parse_bytes (GcrParser *self,
+                        GBytes *data,
+                        GError **error)
 {
        ForeachArgs args = { self, NULL, GCR_ERROR_UNRECOGNIZED };
        const gchar *message = NULL;
        gint i;
 
        g_return_val_if_fail (GCR_IS_PARSER (self), FALSE);
-       g_return_val_if_fail (data || !n_data, FALSE);
+       g_return_val_if_fail (data != NULL, FALSE);
        g_return_val_if_fail (!error || !*error, FALSE);
 
-       if (data && n_data) {
-               /* TODO: Once GBytes is in the API, copy here */
-               args.data = g_bytes_new_static (data, n_data);
+       if (g_bytes_get_size (data) > 0) {
+               args.data = g_bytes_ref (data);
 
                /* Just the specific formats requested */
                if (self->pv->specific_formats) {
@@ -2440,6 +2437,40 @@ gcr_parser_parse_data (GcrParser *self,
 }
 
 /**
+ * gcr_parser_parse_data:
+ * @self: The parser
+ * @data: (array length=n_data): the data to parse
+ * @n_data: The length of the data
+ * @error: A location to raise an error on failure.
+ *
+ * Parse the data. The #GcrParser::parsed and #GcrParser::authenticate signals
+ * may fire during the parsing.
+ *
+ * A copy of the data will be made. Use gcr_parser_parse_bytes() to avoid this.
+ *
+ * Returns: Whether the data was parsed successfully or not.
+ */
+gboolean
+gcr_parser_parse_data (GcrParser *self,
+                       const guchar *data,
+                       gsize n_data,
+                       GError **error)
+{
+       GBytes *bytes;
+       gboolean ret;
+
+       g_return_val_if_fail (GCR_IS_PARSER (self), FALSE);
+       g_return_val_if_fail (data || !n_data, FALSE);
+       g_return_val_if_fail (!error || !*error, FALSE);
+
+       bytes = g_bytes_new (data, n_data);
+       ret = gcr_parser_parse_bytes (self, bytes, error);
+       g_bytes_unref (bytes);
+
+       return ret;
+}
+
+/**
  * gcr_parser_format_enable:
  * @self: The parser
  * @format: The format identifier
@@ -2760,6 +2791,22 @@ gcr_parser_get_parsed_block (GcrParser *self,
        return gcr_parsed_get_data (self->pv->parsed, n_block);
 }
 
+
+/**
+ * gcr_parser_get_parsed_bytes:
+ * @self: a parser
+ *
+ * Get the raw data block that represents this parsed object. This is only
+ * valid during the #GcrParser::parsed signal.
+ *
+ * Returns: (transfer none): the raw data block of the currently parsed item
+ */
+GBytes *
+gcr_parser_get_parsed_bytes (GcrParser *self)
+{
+       return gcr_parsed_get_bytes (self->pv->parsed);
+}
+
 /**
  * gcr_parsed_get_data:
  * @parsed: a parsed item
@@ -2774,15 +2821,36 @@ const guchar *
 gcr_parsed_get_data (GcrParsed *parsed,
                      gsize *n_data)
 {
+       GBytes *bytes;
+
        g_return_val_if_fail (n_data != NULL, NULL);
 
+       bytes = gcr_parsed_get_bytes (parsed);
+       if (bytes == NULL) {
+               *n_data = 0;
+               return NULL;
+       }
+
+       return g_bytes_get_data (bytes, n_data);
+}
+
+/**
+ * gcr_parsed_get_bytes:
+ * @parsed: a parsed item
+ *
+ * Get the raw data block for the parsed item.
+ *
+ * Returns: (transfer none): the raw data of the parsed item, or %NULL
+ */
+GBytes *
+gcr_parsed_get_bytes (GcrParsed *parsed)
+{
        while (parsed != NULL) {
                if (parsed->data != NULL)
-                       return g_bytes_get_data (parsed->data, n_data);
+                       return parsed->data;
                parsed = parsed->next;
        }
 
-       *n_data = 0;
        return NULL;
 }
 
@@ -2920,12 +2988,16 @@ static void
 state_parse_buffer (GcrParsing *self, gboolean async)
 {
        GError *error = NULL;
+       GBytes *bytes;
        gboolean ret;
 
        g_assert (GCR_IS_PARSING (self));
        g_assert (self->buffer);
 
-       ret = gcr_parser_parse_data (self->parser, self->buffer->data, self->buffer->len, &error);
+       bytes = g_byte_array_free_to_bytes (self->buffer);
+       self->buffer = NULL;
+       ret = gcr_parser_parse_bytes (self->parser, bytes, &error);
+       g_bytes_unref (bytes);
 
        if (ret == TRUE) {
                next_state (self, state_complete);
diff --git a/gcr/gcr-parser.h b/gcr/gcr-parser.h
index d710e0e..db4bf18 100644
--- a/gcr/gcr-parser.h
+++ b/gcr/gcr-parser.h
@@ -76,6 +76,10 @@ void                     gcr_parser_format_disable         (GcrParser *self,
 gboolean                 gcr_parser_format_supported       (GcrParser *self,
                                                             GcrDataFormat format);
 
+gboolean                 gcr_parser_parse_bytes            (GcrParser *self,
+                                                            GBytes *data,
+                                                            GError **error);
+
 gboolean                 gcr_parser_parse_data             (GcrParser *self,
                                                             const guchar *data,
                                                             gsize n_data,
@@ -110,6 +114,8 @@ GckAttributes*           gcr_parser_get_parsed_attributes  (GcrParser *self);
 const guchar *           gcr_parser_get_parsed_block       (GcrParser *self,
                                                             gsize *n_block);
 
+GBytes *                 gcr_parser_get_parsed_bytes       (GcrParser *self);
+
 GcrDataFormat            gcr_parser_get_parsed_format      (GcrParser *self);
 
 #define                  GCR_TYPE_PARSED                   (gcr_parsed_get_type ())
@@ -129,6 +135,8 @@ GckAttributes*           gcr_parsed_get_attributes         (GcrParsed *parsed);
 const guchar *           gcr_parsed_get_data               (GcrParsed *parsed,
                                                             gsize *n_data);
 
+GBytes *                 gcr_parsed_get_bytes              (GcrParsed *parsed);
+
 GcrDataFormat            gcr_parsed_get_format             (GcrParsed *parsed);
 
 G_END_DECLS
diff --git a/gcr/tests/frob-parser.c b/gcr/tests/frob-parser.c
index 7061a5f..fa1e3ae 100644
--- a/gcr/tests/frob-parser.c
+++ b/gcr/tests/frob-parser.c
@@ -77,6 +77,7 @@ main(int argc, char *argv[])
        gsize len;
        GDir *dir;
        const gchar *filename;
+       GBytes *bytes;
        gchar *path;
 
 #if !GLIB_CHECK_VERSION(2,35,0)
@@ -116,8 +117,9 @@ main(int argc, char *argv[])
                        continue;
                }
 
-               gcr_parser_parse_data (parser, (const guchar *)contents, len, &error);
-               g_free (contents);
+               bytes = g_bytes_new_take (contents, len);
+               gcr_parser_parse_bytes (parser, bytes, &error);
+               g_bytes_unref (bytes);
 
                if (error) {
                        g_warning ("%s: couldn't parse: %s\n", filename, error->message);
diff --git a/gcr/tests/test-fingerprint.c b/gcr/tests/test-fingerprint.c
index d817412..589862c 100644
--- a/gcr/tests/test-fingerprint.c
+++ b/gcr/tests/test-fingerprint.c
@@ -98,8 +98,7 @@ parse_attributes_for_key (GBytes *data)
 
        parser = gcr_parser_new ();
        g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), &attrs);
-       gcr_parser_parse_data (parser, g_bytes_get_data (data, NULL),
-                              g_bytes_get_size (data), &error);
+       gcr_parser_parse_bytes (parser, data, &error);
        g_assert_no_error (error);
        g_object_unref (parser);
 
diff --git a/gcr/tests/test-parser.c b/gcr/tests/test-parser.c
index 510abc0..d274d71 100644
--- a/gcr/tests/test-parser.c
+++ b/gcr/tests/test-parser.c
@@ -60,21 +60,19 @@ typedef struct {
 
 static void
 ensure_block_can_be_parsed (GcrDataFormat format,
-                            gconstpointer block,
-                            gsize n_block)
+                            GBytes *block)
 {
        GcrParser *parser;
        gboolean result;
        GError *error = NULL;
 
-       g_assert (block);
-       g_assert (n_block);
+       g_assert (block != NULL);
 
        parser = gcr_parser_new ();
        g_object_add_weak_pointer (G_OBJECT (parser), (gpointer *)&parser);
        gcr_parser_format_disable (parser, -1);
        gcr_parser_format_enable (parser, format);
-       result = gcr_parser_parse_data (parser, block, n_block, &error);
+       result = gcr_parser_parse_bytes (parser, block, &error);
 
        if (!result) {
                g_critical ("The data returned from gcr_parser_get_parsed_block() "
@@ -93,8 +91,7 @@ parsed_item (GcrParser *par, gpointer user_data)
        const gchar *description;
        const gchar *label;
        Test *test = user_data;
-       gconstpointer block;
-       gsize n_block;
+       GBytes *block;
        GcrDataFormat format;
 
        g_assert (GCR_IS_PARSER (par));
@@ -105,9 +102,9 @@ parsed_item (GcrParser *par, gpointer user_data)
 
        description = gcr_parser_get_parsed_description (test->parser);
        label = gcr_parser_get_parsed_label (test->parser);
-       block = gcr_parser_get_parsed_block (test->parser, &n_block);
+       block = gcr_parser_get_parsed_bytes (test->parser);
        format = gcr_parser_get_parsed_format (test->parser);
-       ensure_block_can_be_parsed (format, block, n_block);
+       ensure_block_can_be_parsed (format, block);
 
        if (g_test_verbose ())
                g_print ("%s: '%s'\n", description, label);
@@ -157,17 +154,73 @@ test_parse_one (Test *test,
        gchar *contents;
        GError *error = NULL;
        gboolean result;
+       GBytes *bytes;
        gsize len;
 
        if (!g_file_get_contents (path, &contents, &len, NULL))
                g_assert_not_reached ();
 
        test->filedesc = path;
-       result = gcr_parser_parse_data (test->parser, (const guchar *)contents, len, &error);
+       bytes = g_bytes_new_take (contents, len);
+       result = gcr_parser_parse_bytes (test->parser, bytes, &error);
        g_assert_no_error (error);
        g_assert (result);
 
-       g_free (contents);
+       g_bytes_unref (bytes);
+}
+
+static void
+on_parsed_compare_bytes (GcrParser *parser,
+                         gpointer user_data)
+{
+       GBytes *original = user_data;
+       GBytes *bytes;
+       gconstpointer data;
+       gsize n_data;
+       GcrParsed *parsed;
+
+       bytes = gcr_parser_get_parsed_bytes (parser);
+       g_assert (bytes != NULL);
+       g_assert (g_bytes_equal (original, bytes));
+
+       data = gcr_parser_get_parsed_block (parser, &n_data);
+       g_assert (data != NULL);
+       g_assert_cmpint (n_data, ==, g_bytes_get_size (original));
+       g_assert (memcmp (data, g_bytes_get_data (original, NULL), n_data) == 0);
+
+       parsed = gcr_parser_get_parsed (parser);
+       g_assert (parsed != NULL);
+       bytes = gcr_parsed_get_bytes (parsed);
+       g_assert (bytes != NULL);
+       g_assert (g_bytes_equal (original, bytes));
+
+       data = gcr_parsed_get_data (parsed, &n_data);
+       g_assert (data != NULL);
+       g_assert_cmpint (n_data, ==, g_bytes_get_size (original));
+       g_assert (memcmp (data, g_bytes_get_data (original, NULL), n_data) == 0);
+}
+
+static void
+test_parsed_bytes (void)
+{
+       GcrParser *parser = gcr_parser_new ();
+       gchar *contents;
+       GError *error = NULL;
+       gboolean result;
+       GBytes *bytes;
+       gsize len;
+
+       if (!g_file_get_contents (SRCDIR "/files/cacert.org.cer", &contents, &len, NULL))
+               g_assert_not_reached ();
+
+       bytes = g_bytes_new_take (contents, len);
+       g_signal_connect (parser, "parsed", G_CALLBACK (on_parsed_compare_bytes), bytes);
+       result = gcr_parser_parse_bytes (parser, bytes, &error);
+       g_assert_no_error (error);
+       g_assert (result);
+
+       g_bytes_unref (bytes);
+       g_object_unref (parser);
 }
 
 static void
@@ -200,6 +253,38 @@ test_parse_empty (void)
        g_object_unref (parser);
 }
 
+static void
+test_parse_stream (void)
+{
+       GcrParser *parser = gcr_parser_new ();
+       GError *error = NULL;
+       gboolean result;
+       GFile *file;
+       GFileInputStream *fis;
+       gchar *contents;
+       gsize len;
+       GBytes *bytes;
+
+       file = g_file_new_for_path (SRCDIR "/files/cacert.org.cer");
+       fis = g_file_read (file, NULL, &error);
+       g_assert_no_error (error);
+
+       if (!g_file_get_contents (SRCDIR "/files/cacert.org.cer", &contents, &len, NULL))
+               g_assert_not_reached ();
+       bytes = g_bytes_new_take (contents, len);
+       g_signal_connect (parser, "parsed", G_CALLBACK (on_parsed_compare_bytes), bytes);
+
+       result = gcr_parser_parse_stream (parser, G_INPUT_STREAM (fis), NULL, &error);
+       g_assert_no_error (error);
+       g_assert (result);
+
+       g_bytes_unref (bytes);
+       g_object_unref (fis);
+       g_object_unref (file);
+       g_object_unref (parser);
+}
+
+
 int
 main (int argc, char **argv)
 {
@@ -250,6 +335,8 @@ main (int argc, char **argv)
 
        g_test_add_func ("/gcr/parser/parse_null", test_parse_null);
        g_test_add_func ("/gcr/parser/parse_empty", test_parse_empty);
+       g_test_add_func ("/gcr/parser/parse_stream", test_parse_stream);
+       g_test_add_func ("/gcr/parser/parsed_bytes", test_parsed_bytes);
 
        ret = g_test_run ();
        g_ptr_array_free (strings, TRUE);
diff --git a/gcr/tests/test-subject-public-key.c b/gcr/tests/test-subject-public-key.c
index 85927e2..3a92f85 100644
--- a/gcr/tests/test-subject-public-key.c
+++ b/gcr/tests/test-subject-public-key.c
@@ -71,8 +71,7 @@ parse_attributes (GBytes *data)
 
        parser = gcr_parser_new ();
        g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), &attrs);
-       gcr_parser_parse_data (parser, g_bytes_get_data (data, NULL),
-                              g_bytes_get_size (data), &error);
+       gcr_parser_parse_bytes (parser, data, &error);
        g_assert_no_error (error);
        g_object_unref (parser);
 
diff --git a/ui/gcr-ui.symbols b/ui/gcr-ui.symbols
index 7211397..d258eb5 100644
--- a/ui/gcr-ui.symbols
+++ b/ui/gcr-ui.symbols
@@ -108,6 +108,7 @@ gcr_viewer_widget_get_parser
 gcr_viewer_widget_get_display_name
 gcr_viewer_widget_get_type
 gcr_viewer_widget_get_viewer
+gcr_viewer_widget_load_bytes
 gcr_viewer_widget_load_data
 gcr_viewer_widget_load_file
 gcr_viewer_widget_new
diff --git a/ui/gcr-unlock-renderer.c b/ui/gcr-unlock-renderer.c
index 2e762df..828b4ab 100644
--- a/ui/gcr-unlock-renderer.c
+++ b/ui/gcr-unlock-renderer.c
@@ -39,8 +39,7 @@ struct _GcrUnlockRendererPrivate {
        GtkEntry *entry;
        GtkLabel *warning;
 
-       gpointer locked_data;
-       gsize n_locked_data;
+       GBytes *locked_data;
        gchar *label;
        gboolean unlocked;
        GList *renderers;
@@ -147,7 +146,7 @@ _gcr_unlock_renderer_finalize (GObject *obj)
 {
        GcrUnlockRenderer *self = GCR_UNLOCK_RENDERER (obj);
 
-       g_free (self->pv->locked_data);
+       g_bytes_unref (self->pv->locked_data);
        g_free (self->pv->label);
        g_list_free_full (self->pv->renderers, g_object_unref);
 
@@ -296,8 +295,7 @@ gcr_renderer_iface_init (GcrRendererIface *iface)
 
 GcrUnlockRenderer*
 _gcr_unlock_renderer_new (const gchar *label,
-                          gconstpointer locked_data,
-                          gsize n_locked_data)
+                          GBytes *locked_data)
 {
        GcrUnlockRenderer *renderer;
 
@@ -306,23 +304,16 @@ _gcr_unlock_renderer_new (const gchar *label,
                                 NULL);
        g_object_ref_sink (renderer);
 
-       renderer->pv->locked_data = g_memdup (locked_data, n_locked_data);
-       renderer->pv->n_locked_data = n_locked_data;
-
+       renderer->pv->locked_data = g_bytes_ref (locked_data);
        return renderer;
 }
 
 GcrUnlockRenderer *
 _gcr_unlock_renderer_new_for_parsed (GcrParser *parser)
 {
-       gconstpointer block;
-       gsize n_block;
-
        g_return_val_if_fail (GCR_IS_PARSER (parser), NULL);
-
-       block = gcr_parser_get_parsed_block (parser, &n_block);
        return _gcr_unlock_renderer_new (gcr_parser_get_parsed_label (parser),
-                                        block, n_block);
+                                        gcr_parser_get_parsed_bytes (parser));
 }
 
 const gchar *
@@ -348,12 +339,9 @@ _gcr_unlock_renderer_focus_password (GcrUnlockRenderer *self)
        gtk_widget_grab_focus (GTK_WIDGET (self->pv->entry));
 }
 
-gconstpointer
-_gcr_unlock_renderer_get_locked_data (GcrUnlockRenderer *self,
-                                      gsize *n_data)
+GBytes *
+_gcr_unlock_renderer_get_locked_data (GcrUnlockRenderer *self)
 {
        g_return_val_if_fail (GCR_IS_UNLOCK_RENDERER (self), NULL);
-       g_return_val_if_fail (n_data != NULL, NULL);
-       *n_data = self->pv->n_locked_data;
        return self->pv->locked_data;
 }
diff --git a/ui/gcr-unlock-renderer.h b/ui/gcr-unlock-renderer.h
index 63e89bc..57beb3c 100644
--- a/ui/gcr-unlock-renderer.h
+++ b/ui/gcr-unlock-renderer.h
@@ -62,8 +62,7 @@ struct _GcrUnlockRendererClass {
 GType                  _gcr_unlock_renderer_get_type          (void);
 
 GcrUnlockRenderer *    _gcr_unlock_renderer_new               (const gchar *label,
-                                                               gconstpointer locked_data,
-                                                               gsize n_locked_data);
+                                                               GBytes *locked_data);
 
 GcrUnlockRenderer *    _gcr_unlock_renderer_new_for_parsed    (GcrParser *parser);
 
@@ -77,8 +76,7 @@ void                   _gcr_unlock_renderer_focus_password    (GcrUnlockRenderer
 void                   _gcr_unlock_renderer_show_warning      (GcrUnlockRenderer *self,
                                                                const gchar *message);
 
-gconstpointer          _gcr_unlock_renderer_get_locked_data   (GcrUnlockRenderer *self,
-                                                               gsize *n_data);
+GBytes *               _gcr_unlock_renderer_get_locked_data   (GcrUnlockRenderer *self);
 
 G_END_DECLS
 
diff --git a/ui/gcr-viewer-widget.c b/ui/gcr-viewer-widget.c
index 32ddd6d..e81d636 100644
--- a/ui/gcr-viewer-widget.c
+++ b/ui/gcr-viewer-widget.c
@@ -184,16 +184,15 @@ on_unlock_renderer_clicked (GcrUnlockRenderer *unlock,
 {
        GcrViewerWidget *self = GCR_VIEWER_WIDGET (user_data);
        GError *error = NULL;
-       gconstpointer data;
-       gsize n_data;
+       GBytes *data;
        gulong sig;
 
        /* Override our main authenticate signal handler */
        sig = g_signal_connect (self->pv->parser, "authenticate",
                                G_CALLBACK (on_parser_authenticate_for_unlock), unlock);
 
-       data = _gcr_unlock_renderer_get_locked_data (unlock, &n_data);
-       if (gcr_parser_parse_data (self->pv->parser, data, n_data, &error)) {
+       data = _gcr_unlock_renderer_get_locked_data (unlock);
+       if (gcr_parser_parse_bytes (self->pv->parser, data, &error)) {
 
                /* Done with this unlock renderer */
                gcr_viewer_remove_renderer (self->pv->viewer, GCR_RENDERER (unlock));
@@ -527,31 +526,30 @@ gcr_viewer_widget_load_file (GcrViewerWidget *self,
 }
 
 /**
- * gcr_viewer_widget_load_data:
+ * gcr_viewer_widget_load_bytes:
  * @self: a viewer widget
  * @display_name: (allow-none): label for the loaded data
- * @data: (array length=n_data): data to load
- * @n_data: length of data to load
+ * @data: data to load
  *
  * Parse and load some data to be displayed into the viewer widgets. The data
  * may contain multiple parseable items if the format can contain multiple
  * items.
  */
 void
-gcr_viewer_widget_load_data (GcrViewerWidget *self,
-                             const gchar *display_name,
-                             const guchar *data,
-                             gsize n_data)
+gcr_viewer_widget_load_bytes (GcrViewerWidget *self,
+                              const gchar *display_name,
+                              GBytes *data)
 {
        GError *error = NULL;
        GcrRenderer *renderer;
 
        g_return_if_fail (GCR_IS_VIEWER_WIDGET (self));
+       g_return_if_fail (data != NULL);
 
        g_free (self->pv->display_name);
        self->pv->display_name = g_strdup (display_name);
 
-       if (!gcr_parser_parse_data (self->pv->parser, data, n_data, &error)) {
+       if (!gcr_parser_parse_bytes (self->pv->parser, data, &error)) {
                renderer = gcr_failure_renderer_new (display_name, error);
                gcr_viewer_add_renderer (self->pv->viewer, renderer);
                g_object_unref (renderer);
@@ -560,6 +558,35 @@ gcr_viewer_widget_load_data (GcrViewerWidget *self,
 }
 
 /**
+ * gcr_viewer_widget_load_data:
+ * @self: a viewer widget
+ * @display_name: (allow-none): label for the loaded data
+ * @data: (array length=n_data): data to load
+ * @n_data: length of data to load
+ *
+ * Parse and load some data to be displayed into the viewer widgets. The data
+ * may contain multiple parseable items if the format can contain multiple
+ * items.
+ *
+ * This function will copy the data. Use gcr_viewer_widget_load_bytes() to avoid
+ * copying the data.
+ */
+void
+gcr_viewer_widget_load_data (GcrViewerWidget *self,
+                             const gchar *display_name,
+                             const guchar *data,
+                             gsize n_data)
+{
+       GBytes *bytes;
+
+       g_return_if_fail (GCR_IS_VIEWER_WIDGET (self));
+
+       bytes = g_bytes_new (data, n_data);
+       gcr_viewer_widget_load_bytes (self, display_name, bytes);
+       g_bytes_unref (bytes);
+}
+
+/**
  * gcr_viewer_widget_get_viewer:
  * @self: a viewer widget
  *
diff --git a/ui/gcr-viewer-widget.h b/ui/gcr-viewer-widget.h
index 11980f2..a9363df 100644
--- a/ui/gcr-viewer-widget.h
+++ b/ui/gcr-viewer-widget.h
@@ -40,6 +40,10 @@ GcrViewerWidget *  gcr_viewer_widget_new              (void);
 void               gcr_viewer_widget_load_file        (GcrViewerWidget *self,
                                                        GFile *file);
 
+void               gcr_viewer_widget_load_bytes       (GcrViewerWidget *self,
+                                                       const gchar *display_name,
+                                                       GBytes *data);
+
 void               gcr_viewer_widget_load_data        (GcrViewerWidget *self,
                                                        const gchar *display_name,
                                                        const guchar *data,
diff --git a/ui/tests/frob-certificate.c b/ui/tests/frob-certificate.c
index 2524dc2..794abea 100644
--- a/ui/tests/frob-certificate.c
+++ b/ui/tests/frob-certificate.c
@@ -74,6 +74,7 @@ test_certificate (const gchar *path)
        GError *err = NULL;
        guchar *data;
        gsize n_data;
+       GBytes *bytes;
        GtkWidget *dialog;
 
        if (!g_file_get_contents (path, (gchar**)&data, &n_data, NULL))
@@ -84,11 +85,12 @@ test_certificate (const gchar *path)
 
        parser = gcr_parser_new ();
        g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), dialog);
-       if (!gcr_parser_parse_data (parser, data, n_data, &err))
+       bytes = g_bytes_new_take (data, n_data);
+       if (!gcr_parser_parse_bytes (parser, bytes, &err))
                g_error ("couldn't parse data: %s", err->message);
 
        g_object_unref (parser);
-       g_free (data);
+       g_bytes_unref (bytes);
 
        gtk_widget_show (dialog);
        g_signal_connect (dialog, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
diff --git a/ui/tests/frob-combo-selector.c b/ui/tests/frob-combo-selector.c
index 810ba90..856d09a 100644
--- a/ui/tests/frob-combo-selector.c
+++ b/ui/tests/frob-combo-selector.c
@@ -30,14 +30,16 @@ add_to_selector (GcrParser *parser, const gchar *path)
        GError *err = NULL;
        guchar *data;
        gsize n_data;
+       GBytes *bytes;
 
        if (!g_file_get_contents (path, (gchar**)&data, &n_data, NULL))
                g_error ("couldn't read file: %s", path);
 
-       if (!gcr_parser_parse_data (parser, data, n_data, &err))
+       bytes = g_bytes_new_take (data, n_data);
+       if (!gcr_parser_parse_bytes (parser, bytes, &err))
                g_error ("couldn't parse data: %s", err->message);
 
-       g_free (data);
+       g_bytes_unref (bytes);
 }
 
 int
diff --git a/ui/tests/frob-key.c b/ui/tests/frob-key.c
index 3eedb49..d67a51e 100644
--- a/ui/tests/frob-key.c
+++ b/ui/tests/frob-key.c
@@ -57,6 +57,7 @@ test_key (const gchar *path)
 {
        GcrParser *parser;
        GError *err = NULL;
+       GBytes *bytes;
        guchar *data;
        gsize n_data;
 
@@ -65,11 +66,12 @@ test_key (const gchar *path)
 
        parser = gcr_parser_new ();
        g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), NULL);
-       if (!gcr_parser_parse_data (parser, data, n_data, &err))
+       bytes = g_bytes_new_take (data, n_data);
+       if (!gcr_parser_parse_bytes (parser, bytes, &err))
                g_error ("couldn't parse data: %s", err->message);
 
        g_object_unref (parser);
-       g_free (data);
+       g_bytes_unref (bytes);
 }
 
 int
diff --git a/ui/tests/frob-request.c b/ui/tests/frob-request.c
index 10f54a2..af0233e 100644
--- a/ui/tests/frob-request.c
+++ b/ui/tests/frob-request.c
@@ -60,6 +60,7 @@ test_request (const gchar *path)
        guchar *data;
        gsize n_data;
        GtkWidget *dialog;
+       GBytes *bytes;
 
        if (!g_file_get_contents (path, (gchar**)&data, &n_data, NULL))
                g_error ("couldn't read file: %s", path);
@@ -69,11 +70,12 @@ test_request (const gchar *path)
 
        parser = gcr_parser_new ();
        g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), dialog);
+       bytes = g_bytes_new_take (data, n_data);
        if (!gcr_parser_parse_data (parser, data, n_data, &err))
                g_error ("couldn't parse data: %s", err->message);
 
        g_object_unref (parser);
-       g_free (data);
+       g_bytes_unref (bytes);
 
        gtk_widget_show (dialog);
        g_signal_connect (dialog, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
diff --git a/ui/tests/frob-tree-selector.c b/ui/tests/frob-tree-selector.c
index 313781e..ecf87d3 100644
--- a/ui/tests/frob-tree-selector.c
+++ b/ui/tests/frob-tree-selector.c
@@ -136,14 +136,16 @@ add_to_selector (GcrParser *parser, const gchar *path)
        GError *err = NULL;
        guchar *data;
        gsize n_data;
+       GBytes *bytes;
 
        if (!g_file_get_contents (path, (gchar**)&data, &n_data, NULL))
                g_error ("couldn't read file: %s", path);
 
-       if (!gcr_parser_parse_data (parser, data, n_data, &err))
+       bytes = g_bytes_new_take (data, n_data);
+       if (!gcr_parser_parse_bytes (parser, bytes, &err))
                g_error ("couldn't parse data: %s", err->message);
 
-       g_free (data);
+       g_bytes_unref (bytes);
 }
 
 int
diff --git a/ui/tests/frob-unlock.c b/ui/tests/frob-unlock.c
index d5d81d7..dab7eb8 100644
--- a/ui/tests/frob-unlock.c
+++ b/ui/tests/frob-unlock.c
@@ -93,6 +93,7 @@ test_key (const gchar *path)
        GError *err = NULL;
        guchar *data;
        gsize n_data;
+       GBytes *bytes;
 
        if (!g_file_get_contents (path, (gchar**)&data, &n_data, NULL))
                g_error ("couldn't read file: %s", path);
@@ -100,11 +101,12 @@ test_key (const gchar *path)
        parser = gcr_parser_new ();
        g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), NULL);
        g_signal_connect (parser, "authenticate", G_CALLBACK (on_parser_authenticate), NULL);
-       if (!gcr_parser_parse_data (parser, data, n_data, &err))
+       bytes = g_bytes_new_take (data, n_data);
+       if (!gcr_parser_parse_bytes (parser, bytes, &err))
                g_error ("couldn't parse data: %s", err->message);
 
        g_object_unref (parser);
-       g_free (data);
+       g_bytes_unref (bytes);
 }
 
 int


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