From 4e3ff1b24118a1913a77285efc33ca38685a3ea5 Mon Sep 17 00:00:00 2001
From: Carlos Garcia Campos
Date: Wed, 27 Mar 2013 17:58:32 +0100
Subject: Make ephy-about-handler a GObject and handle all request using the
same API
about:plugins was handled differently because it's asynchrounous, now
all handlers can be implemented asynchronously too.
https://bugzilla.gnome.org/show_bug.cgi?id=696728
---
embed/ephy-about-handler.c | 287 +++++++++++++++++++++++++++++++++------------
embed/ephy-about-handler.h | 35 +++++-
embed/ephy-embed-shell.c | 56 +--------
3 files changed, 250 insertions(+), 128 deletions(-)
diff --git a/embed/ephy-about-handler.c b/embed/ephy-about-handler.c
index 31eb775c7..ef15a120d 100644
--- a/embed/ephy-about-handler.c
+++ b/embed/ephy-about-handler.c
@@ -28,40 +28,123 @@
#include
#include
-#include
-static gchar *css_style = NULL;
+struct _EphyAboutHandlerPrivate {
+ char *style_sheet;
+ EphySMaps *smaps;
+};
+
+G_DEFINE_TYPE (EphyAboutHandler, ephy_about_handler, G_TYPE_OBJECT)
+
+static void
+ephy_about_handler_finalize (GObject *object)
+{
+ EphyAboutHandler *handler = EPHY_ABOUT_HANDLER (object);
+
+ g_free (handler->priv->style_sheet);
+ g_clear_object (&handler->priv->smaps);
+
+ G_OBJECT_CLASS (ephy_about_handler_parent_class)->finalize (object);
+}
+
+static void
+ephy_about_handler_init (EphyAboutHandler *handler)
+{
+ handler->priv = G_TYPE_INSTANCE_GET_PRIVATE (handler, EPHY_TYPE_ABOUT_HANDLER, EphyAboutHandlerPrivate);
+}
static void
-read_css_style (void)
+ephy_about_handler_class_init (EphyAboutHandlerClass *klass)
{
- GError *error = NULL;
- const gchar *file;
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
- if (css_style)
- return;
+ object_class->finalize = ephy_about_handler_finalize;
+ g_type_class_add_private (object_class, sizeof (EphyAboutHandlerPrivate));
+}
- file = ephy_file ("about.css");
- if (file && !g_file_get_contents (file, &css_style, NULL, &error)) {
- g_debug ("%s", error->message);
- g_error_free (error);
+static const char *
+ephy_about_handler_get_style_sheet (EphyAboutHandler *handler)
+{
+ if (!handler->priv->style_sheet) {
+ const gchar *file;
+ GError *error = NULL;
+
+ file = ephy_file ("about.css");
+ if (file && !g_file_get_contents (file, &handler->priv->style_sheet, NULL, &error)) {
+ g_debug ("%s", error->message);
+ g_error_free (error);
+ }
}
+
+ return handler->priv->style_sheet;
}
-void
-_ephy_about_handler_handle_plugins (GString *data_str, GList *plugin_list)
+static EphySMaps *
+ephy_about_handler_get_smaps (EphyAboutHandler *handler)
{
- GList *p;
+ if (!handler->priv->smaps)
+ handler->priv->smaps = ephy_smaps_new ();
- read_css_style ();
+ return handler->priv->smaps;
+}
+static void
+ephy_about_handler_finish_request (WebKitURISchemeRequest *request,
+ gchar *data,
+ gsize data_length)
+{
+ GInputStream *stream;
+
+ data_length = data_length != -1 ? data_length : strlen (data);
+ stream = g_memory_input_stream_new_from_data (data, data_length, g_free);
+ webkit_uri_scheme_request_finish (request, stream, data_length, "text/html");
+ g_object_unref (stream);
+}
+
+typedef struct {
+ EphyAboutHandler *handler;
+ WebKitURISchemeRequest *request;
+} EphyAboutRequest;
+
+static EphyAboutRequest *
+ephy_about_request_new (EphyAboutHandler *handler,
+ WebKitURISchemeRequest *request)
+{
+ EphyAboutRequest *about_request;
+
+ about_request = g_slice_new (EphyAboutRequest);
+ about_request->handler = g_object_ref (handler);
+ about_request->request = g_object_ref (request);
+
+ return about_request;
+}
+
+static void
+ephy_about_request_free (EphyAboutRequest *about_request)
+{
+ g_object_unref (about_request->handler);
+ g_object_unref (about_request->request);
+
+ g_slice_free (EphyAboutRequest, about_request);
+}
+
+static void
+get_plugins_cb (WebKitWebContext *web_context,
+ GAsyncResult *result,
+ EphyAboutRequest *about_request)
+{
+ GString *data_str;
+ gsize data_length;
+ GList *plugin_list, *p;
+
+ data_str = g_string_new ("");
g_string_append_printf (data_str, "%s" \
"",
_("Installed plugins"),
- css_style);
-
+ ephy_about_handler_get_style_sheet (about_request->handler));
g_string_append_printf (data_str, "%s
", _("Installed plugins"));
+ plugin_list = webkit_web_context_get_plugins_finish (web_context, result, NULL);
for (p = plugin_list; p; p = p->next) {
WebKitPlugin *plugin = WEBKIT_PLUGIN (p->data);
GList *m, *mime_types;
@@ -96,67 +179,97 @@ _ephy_about_handler_handle_plugins (GString *data_str, GList *plugin_list)
g_string_append (data_str, "");
}
+ g_string_append (data_str, "");
- g_string_append (data_str, "
",
_("Memory usage"),
- css_style);
+ ephy_about_handler_get_style_sheet (handler));
g_string_append_printf (data_str, "%s
", _("Memory usage"));
g_string_append (data_str, memory);
g_free (memory);
}
+
+ g_string_append (data_str, "");
+ g_list_free_full (plugin_list, g_object_unref);
+
+ data_length = data_str->len;
+ ephy_about_handler_finish_request (about_request->request, g_string_free (data_str, FALSE), data_length);
+ ephy_about_request_free (about_request);
}
-static void
-ephy_about_handler_handle_plugins (GString *data_str)
+static gboolean
+ephy_about_handler_handle_plugins (EphyAboutHandler *handler,
+ WebKitURISchemeRequest *request)
{
- g_string_append (data_str, "");
+ webkit_web_context_get_plugins (webkit_web_context_get_default (),
+ NULL,
+ (GAsyncReadyCallback)get_plugins_cb,
+ ephy_about_request_new (handler, request));
+
+ return TRUE;
}
-static void
-ephy_about_handler_handle_memory (GString *data_str)
+static gboolean
+ephy_about_handler_handle_memory (EphyAboutHandler *handler,
+ WebKitURISchemeRequest *request)
{
+ GString *data_str;
+ gsize data_length;
char *memory;
- static EphySMaps *smaps = NULL;
- if (!smaps)
- smaps = ephy_smaps_new ();
- memory = ephy_smaps_to_html (smaps);
+ memory = ephy_smaps_to_html (ephy_about_handler_get_smaps (handler));
+
+ data_str = g_string_new ("");
if (memory) {
g_string_append_printf (data_str, "