[banshee] Implement OssiferSession and cookie notifications



commit 4391c36d767d5d14a6912b1afc2e9fe3e6f86bb7
Author: Aaron Bockover <abockover novell com>
Date:   Mon Jul 12 13:45:46 2010 -0400

    Implement OssiferSession and cookie notifications
    
    Split the global/static features out from OssiferWebView into a new
    class called OssiferSession. Support raising the "changed" event from
    SoupCookieJar on the OssiferSession.

 .../Banshee.WebBrowser/Banshee.WebBrowser.csproj   |    3 +
 .../Banshee.WebBrowser/OssiferCookie.cs            |   74 ++++++++++++++
 .../Banshee.WebBrowser/OssiferSession.cs           |  106 ++++++++++++++++++++
 .../Banshee.WebBrowser/OssiferWebView.cs           |   46 +--------
 src/Core/Banshee.WebBrowser/Makefile.am            |    2 +
 src/Core/Banshee.WebBrowser/libossifer/Makefile.am |    4 +-
 .../libossifer/ossifer-session.c                   |   83 +++++++++++++++
 .../libossifer/ossifer-web-view.c                  |   59 -----------
 .../libossifer/ossifer-web-view.h                  |    1 -
 .../Banshee.AmazonMp3.Store/StoreView.cs           |    2 +-
 10 files changed, 273 insertions(+), 107 deletions(-)
---
diff --git a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser.csproj b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser.csproj
index c466b4f..6519ef6 100644
--- a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser.csproj
+++ b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser.csproj
@@ -89,6 +89,7 @@
   <ItemGroup>
     <None Include="libossifer\ossifer-web-view.c" />
     <None Include="libossifer\ossifer-web-view.h" />
+    <None Include="libossifer\ossifer-session.c" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Banshee.WebBrowser\" />
@@ -99,5 +100,7 @@
     <Compile Include="Banshee.WebBrowser\OssiferDownloadStatus.cs" />
     <Compile Include="Banshee.WebBrowser\NavigationControl.cs" />
     <Compile Include="Banshee.WebBrowser\OssiferLoadStatus.cs" />
+    <Compile Include="Banshee.WebBrowser\OssiferCookie.cs" />
+    <Compile Include="Banshee.WebBrowser\OssiferSession.cs" />
   </ItemGroup>
 </Project>
diff --git a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferCookie.cs b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferCookie.cs
new file mode 100644
index 0000000..9a8c856
--- /dev/null
+++ b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferCookie.cs
@@ -0,0 +1,74 @@
+// 
+// OssiferCookie.cs
+// 
+// Author:
+//   Aaron Bockover <abockover novell com>
+// 
+// Copyright 2010 Novell, Inc.
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace Banshee.WebBrowser
+{
+    public class OssiferCookie
+    {
+        [StructLayout (LayoutKind.Sequential)]
+        private struct NativeSoupCookie
+        {
+            public IntPtr name;
+            public IntPtr value;
+            public IntPtr domain;
+            public IntPtr path;
+            public IntPtr expires;
+            public bool secure;
+            public bool http_only;
+        }
+
+        public string Name { get; set; }
+        public string Value { get; set; }
+        public string Domain { get; set; }
+        public string Path { get; set; }
+        public bool Secure { get; set; }
+        public bool HttpOnly { get; set; }
+
+        public OssiferCookie ()
+        {
+        }
+
+        public OssiferCookie (IntPtr raw)
+        {
+            ReadFromNative (raw);
+        }
+
+        private void ReadFromNative (IntPtr raw)
+        {
+            var cookie = (NativeSoupCookie)Marshal.PtrToStructure (raw, typeof (NativeSoupCookie));
+            Name = GLib.Marshaller.Utf8PtrToString (cookie.name);
+            Value = GLib.Marshaller.Utf8PtrToString (cookie.value);
+            Domain = GLib.Marshaller.Utf8PtrToString (cookie.domain);
+            Path = GLib.Marshaller.Utf8PtrToString (cookie.path);
+            Secure = cookie.secure;
+            HttpOnly = cookie.http_only;
+        }
+    }
+}
+
diff --git a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferSession.cs b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferSession.cs
new file mode 100644
index 0000000..fdc910c
--- /dev/null
+++ b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferSession.cs
@@ -0,0 +1,106 @@
+// 
+// OssiferSession.cs
+// 
+// Author:
+//   Aaron Bockover <abockover novell com>
+// 
+// Copyright 2010 Novell, Inc.
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace Banshee.WebBrowser
+{
+    public static class OssiferSession
+    {
+        private const string LIBOSSIFER = "ossifer";
+
+        [UnmanagedFunctionPointer (CallingConvention.Cdecl)]
+        private delegate void CookieJarChangedCallback (IntPtr session, IntPtr old_cookie, IntPtr new_cookie);
+
+        private static IntPtr handle;
+        private static CookieJarChangedCallback cookie_jar_changed_callback;
+
+        [DllImport (LIBOSSIFER)]
+        private static extern IntPtr ossifer_session_initialize (IntPtr cookie_db_path,
+            CookieJarChangedCallback cookie_jar_changed_callback);
+
+        public static event Action<OssiferCookie, OssiferCookie> CookieChanged;
+
+        public static void Initialize ()
+        {
+            if (handle != IntPtr.Zero) {
+                return;
+            }
+
+            var path = System.IO.Path.Combine (Hyena.Paths.ApplicationData, "ossifer-browser-cookies");
+            var path_raw = IntPtr.Zero;
+            try {
+                cookie_jar_changed_callback = new CookieJarChangedCallback (HandleCookieJarChanged);
+                handle = ossifer_session_initialize (path_raw = GLib.Marshaller.StringToPtrGStrdup (path),
+                    cookie_jar_changed_callback);
+            } finally {
+                GLib.Marshaller.Free (path_raw);
+            }
+        }
+
+        [DllImport (LIBOSSIFER)]
+        private static extern void ossifer_session_set_cookie (IntPtr name, IntPtr value,
+            IntPtr domain, IntPtr path, int max_age);
+
+        public static void SetCookie (string name, string value, string domain, string path, TimeSpan maxAge)
+        {
+            var name_raw = IntPtr.Zero;
+            var value_raw = IntPtr.Zero;
+            var domain_raw = IntPtr.Zero;
+            var path_raw = IntPtr.Zero;
+
+            try {
+                ossifer_session_set_cookie (
+                    name_raw = GLib.Marshaller.StringToPtrGStrdup (name),
+                    value_raw = GLib.Marshaller.StringToPtrGStrdup (value),
+                    domain_raw = GLib.Marshaller.StringToPtrGStrdup (domain),
+                    path_raw = GLib.Marshaller.StringToPtrGStrdup (path),
+                    (int)Math.Round (maxAge.TotalSeconds));
+            } finally {
+                GLib.Marshaller.Free (name_raw);
+                GLib.Marshaller.Free (value_raw);
+                GLib.Marshaller.Free (domain_raw);
+                GLib.Marshaller.Free (path_raw);
+            }
+        }
+
+        private static void HandleCookieJarChanged (IntPtr session, IntPtr old_cookie, IntPtr new_cookie)
+        {
+            OnCookieJarChanged (
+                old_cookie == IntPtr.Zero ? null : new OssiferCookie (old_cookie),
+                new_cookie == IntPtr.Zero ? null : new OssiferCookie (new_cookie));
+        }
+
+        private static void OnCookieJarChanged (OssiferCookie oldCookie, OssiferCookie newCookie)
+        {
+            var handler = CookieChanged;
+            if (handler != null) {
+                handler (oldCookie, newCookie);
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferWebView.cs b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferWebView.cs
index 3513e6c..36c0120 100644
--- a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferWebView.cs
+++ b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferWebView.cs
@@ -67,7 +67,7 @@ namespace Banshee.WebBrowser
 
         public OssiferWebView ()
         {
-            Initialize ();
+            OssiferSession.Initialize ();
             CreateNativeObject (new string[0], new GLib.Value[0]);
 
             callbacks = new Callbacks () {
@@ -249,49 +249,5 @@ namespace Banshee.WebBrowser
 
 #endregion
 
-#region Static API
-
-        [DllImport (LIBOSSIFER)]
-        private static extern void ossifer_web_view_global_initialize (IntPtr cookie_db_path);
-
-        public static void Initialize ()
-        {
-            var path = System.IO.Path.Combine (Hyena.Paths.ApplicationData, "ossifer-browser-cookies");
-            var path_raw = IntPtr.Zero;
-            try {
-                ossifer_web_view_global_initialize (path_raw = GLib.Marshaller.StringToPtrGStrdup (path));
-            } finally {
-                GLib.Marshaller.Free (path_raw);
-            }
-        }
-
-        [DllImport (LIBOSSIFER)]
-        private static extern void ossifer_web_view_global_set_cookie (IntPtr name, IntPtr value,
-            IntPtr domain, IntPtr path, int max_age);
-
-        public static void SetCookie (string name, string value, string domain, string path, TimeSpan maxAge)
-        {
-            var name_raw = IntPtr.Zero;
-            var value_raw = IntPtr.Zero;
-            var domain_raw = IntPtr.Zero;
-            var path_raw = IntPtr.Zero;
-
-            try {
-                ossifer_web_view_global_set_cookie (
-                    name_raw = GLib.Marshaller.StringToPtrGStrdup (name),
-                    value_raw = GLib.Marshaller.StringToPtrGStrdup (value),
-                    domain_raw = GLib.Marshaller.StringToPtrGStrdup (domain),
-                    path_raw = GLib.Marshaller.StringToPtrGStrdup (path),
-                    (int)Math.Round (maxAge.TotalSeconds));
-            } finally {
-                GLib.Marshaller.Free (name_raw);
-                GLib.Marshaller.Free (value_raw);
-                GLib.Marshaller.Free (domain_raw);
-                GLib.Marshaller.Free (path_raw);
-            }
-        }
-
-#endregion
-
     }
 }
\ No newline at end of file
diff --git a/src/Core/Banshee.WebBrowser/Makefile.am b/src/Core/Banshee.WebBrowser/Makefile.am
index 5aff845..44a2f84 100644
--- a/src/Core/Banshee.WebBrowser/Makefile.am
+++ b/src/Core/Banshee.WebBrowser/Makefile.am
@@ -7,9 +7,11 @@ INSTALL_DIR = $(DEFAULT_INSTALL_DIR)
 
 SOURCES =  \
 	Banshee.WebBrowser/NavigationControl.cs \
+	Banshee.WebBrowser/OssiferCookie.cs \
 	Banshee.WebBrowser/OssiferDownloadStatus.cs \
 	Banshee.WebBrowser/OssiferLoadStatus.cs \
 	Banshee.WebBrowser/OssiferNavigationResponse.cs \
+	Banshee.WebBrowser/OssiferSession.cs \
 	Banshee.WebBrowser/OssiferWebView.cs
 
 if HAVE_LIBWEBKIT
diff --git a/src/Core/Banshee.WebBrowser/libossifer/Makefile.am b/src/Core/Banshee.WebBrowser/libossifer/Makefile.am
index 906c196..9329bd7 100644
--- a/src/Core/Banshee.WebBrowser/libossifer/Makefile.am
+++ b/src/Core/Banshee.WebBrowser/libossifer/Makefile.am
@@ -1,4 +1,6 @@
-SOURCES = ossifer-web-view.c
+SOURCES = \
+	ossifer-web-view.c \
+	ossifer-session.c
 
 if HAVE_LIBWEBKIT
 ossiferdir = $(pkglibdir)
diff --git a/src/Core/Banshee.WebBrowser/libossifer/ossifer-session.c b/src/Core/Banshee.WebBrowser/libossifer/ossifer-session.c
new file mode 100644
index 0000000..447d6e4
--- /dev/null
+++ b/src/Core/Banshee.WebBrowser/libossifer/ossifer-session.c
@@ -0,0 +1,83 @@
+#include <config.h>
+#include <webkit/webkit.h>
+
+#ifdef HAVE_LIBSOUP_GNOME
+#  include <libsoup/soup-gnome.h>
+#endif
+
+typedef struct OssiferSession OssiferSession;
+
+typedef void (* OssiferSessionCookieJarChanged)
+    (OssiferSession *session, SoupCookie *old_cookie, SoupCookie *new_cookie);
+
+struct OssiferSession {
+    OssiferSessionCookieJarChanged cookie_jar_changed;
+};
+
+static void
+ossifer_session_cookie_jar_changed (SoupCookieJar *jar,
+    SoupCookie *old_cookie, SoupCookie *new_cookie, gpointer user_data)
+{
+    OssiferSession *session = (OssiferSession *)user_data;
+    if (session->cookie_jar_changed != NULL) {
+        session->cookie_jar_changed (session, old_cookie, new_cookie);
+    }
+}
+
+OssiferSession *
+ossifer_session_initialize (const gchar *cookie_db_path,
+    OssiferSessionCookieJarChanged cookie_jar_changed_callback)
+{
+    static OssiferSession *session_instance = NULL;
+
+    SoupSession *session;
+    SoupCookieJar *cookie_jar;
+    gchar *path;
+
+    if (session_instance != NULL) {
+        return session_instance;
+    }
+
+    session_instance = g_new0 (OssiferSession, 1);
+    session_instance->cookie_jar_changed = cookie_jar_changed_callback;
+
+    session = webkit_get_default_session ();
+
+#ifdef HAVE_LIBSOUP_GNOME
+    path = g_strdup_printf ("%s.sqlite", cookie_db_path);
+    cookie_jar = soup_cookie_jar_sqlite_new (path, FALSE);
+#else
+    path = g_strdup_printf ("%s.txt", cookie_db_path);
+    cookie_jar = soup_cookie_jar_text_new (path, FALSE);
+#endif
+    soup_session_add_feature (session, SOUP_SESSION_FEATURE (cookie_jar));
+    g_object_unref (cookie_jar);
+    g_free (path);
+
+    g_signal_connect (cookie_jar, "changed",
+        G_CALLBACK (ossifer_session_cookie_jar_changed),
+        session_instance);
+
+#ifdef HAVE_LIBSOUP_GNOME
+    soup_session_add_feature_by_type (session, SOUP_TYPE_PROXY_RESOLVER_GNOME);
+#endif
+
+    return session_instance;
+}
+
+void
+ossifer_session_set_cookie (const gchar *name, const gchar *value,
+    const gchar *domain, const gchar *path, gint max_age)
+{
+    SoupSession *session;
+    SoupCookieJar *cookies;
+    SoupCookie *cookie;
+
+    session = webkit_get_default_session ();
+    cookies = (SoupCookieJar *)soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR);
+
+    g_return_if_fail (cookies != NULL);
+
+    cookie = soup_cookie_new (name, value, domain, path, max_age);
+    soup_cookie_jar_add_cookie (cookies, cookie);
+}
\ No newline at end of file
diff --git a/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.c b/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.c
index 2e33707..4c96f88 100644
--- a/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.c
+++ b/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.c
@@ -27,10 +27,6 @@
 #include <config.h>
 #include "ossifer-web-view.h"
 
-#ifdef HAVE_LIBSOUP_GNOME
-#  include <libsoup/soup-gnome.h>
-#endif
-
 G_DEFINE_TYPE (OssiferWebView, ossifer_web_view, WEBKIT_TYPE_WEB_VIEW);
 
 typedef WebKitNavigationResponse (* OssiferWebViewMimeTypePolicyDecisionRequestedCallback)
@@ -199,61 +195,6 @@ ossifer_web_view_init (OssiferWebView *ossifer)
         G_CALLBACK (ossifer_web_view_notify_load_status), NULL);
 }
 
-
-// ---------------------------------------------------------------------------
-// OssiferWebView Public Static API
-// ---------------------------------------------------------------------------
-
-void
-ossifer_web_view_global_initialize (const gchar *cookie_db_path)
-{
-    static gboolean already_initialized = FALSE;
-
-    SoupSession *session;
-    SoupCookieJar *cookies;
-    gchar *path;
-
-    if (already_initialized) {
-        return;
-    }
-
-    already_initialized = TRUE;
-
-    session = webkit_get_default_session ();
-
-#ifdef HAVE_LIBSOUP_GNOME
-    path = g_strdup_printf ("%s.sqlite", cookie_db_path);
-    cookies = soup_cookie_jar_sqlite_new (path, FALSE);
-#else
-    path = g_strdup_printf ("%s.txt", cookie_db_path);
-    cookies = soup_cookie_jar_text_new (path, FALSE);
-#endif
-    soup_session_add_feature (session, SOUP_SESSION_FEATURE (cookies));
-    g_object_unref (cookies);
-    g_free (path);
-
-#ifdef HAVE_LIBSOUP_GNOME
-    soup_session_add_feature_by_type (session, SOUP_TYPE_PROXY_RESOLVER_GNOME);
-#endif
-}
-
-void
-ossifer_web_view_global_set_cookie (const gchar *name, const gchar *value,
-    const gchar *domain, const gchar *path, gint max_age)
-{
-    SoupSession *session;
-    SoupCookieJar *cookies;
-    SoupCookie *cookie;
-
-    session = webkit_get_default_session ();
-    cookies = (SoupCookieJar *)soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR);
-
-    g_return_if_fail (cookies != NULL);
-
-    cookie = soup_cookie_new (name, value, domain, path, max_age);
-    soup_cookie_jar_add_cookie (cookies, cookie);
-}
-
 // ---------------------------------------------------------------------------
 // OssiferWebView Public Instance API
 // ---------------------------------------------------------------------------
diff --git a/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.h b/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.h
index 86061e3..b948662 100644
--- a/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.h
+++ b/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.h
@@ -1,7 +1,6 @@
 #ifndef OSSIFER_WEB_VIEW_H
 #define OSSIFER_WEB_VIEW_H
 
-#include <gtk/gtk.h>
 #include <webkit/webkit.h>
 
 G_BEGIN_DECLS
diff --git a/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/StoreView.cs b/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/StoreView.cs
index 0057219..4d4a0f5 100644
--- a/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/StoreView.cs
+++ b/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/StoreView.cs
@@ -45,7 +45,7 @@ namespace Banshee.AmazonMp3.Store
             // Ensure that Amazon knows a valid downloader is available,
             // otherwise the purchase experience is interrupted with a
             // confusing message about downloading and installing software.
-            SetCookie ("dmusic_download_manager_enabled",
+            OssiferSession.SetCookie ("dmusic_download_manager_enabled",
                 AmzMp3Downloader.AmazonMp3DownloaderCompatVersion,
                 ".amazon.com", "/", TimeSpan.FromDays (365.2422));
 



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