[banshee] [WebBrowser] Bind nav and resource request callbacks
- From: Gabriel Burt <gburt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [banshee] [WebBrowser] Bind nav and resource request callbacks
- Date: Wed, 14 Jul 2010 05:05:03 +0000 (UTC)
commit d393e2992430bb9fee61d24c42468fffc6d20475
Author: Gabriel Burt <gabriel burt gmail com>
Date: Tue Jul 13 20:36:35 2010 -0700
[WebBrowser] Bind nav and resource request callbacks
With these you can reject navigation requests and reject or redirect
resource (eg images in a page) requests.
.../Banshee.WebBrowser/OssiferWebView.cs | 32 +++++++++-
.../libossifer/ossifer-web-view.c | 65 +++++++++++++++++++-
2 files changed, 95 insertions(+), 2 deletions(-)
---
diff --git a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferWebView.cs b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferWebView.cs
index 61e3c27..0e5a4e1 100644
--- a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferWebView.cs
+++ b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferWebView.cs
@@ -32,7 +32,9 @@ namespace Banshee.WebBrowser
public class OssiferWebView : Gtk.Widget
{
private delegate OssiferNavigationResponse MimeTypePolicyDecisionRequestedCallback (IntPtr ossifer, IntPtr mimetype);
+ private delegate OssiferNavigationResponse NavigationPolicyDecisionRequestedCallback (IntPtr ossifer, IntPtr uri);
private delegate IntPtr DownloadRequestedCallback (IntPtr ossifer, IntPtr mimetype, IntPtr uri, IntPtr suggested_filename);
+ private delegate IntPtr ResourceRequestStartingCallback (IntPtr ossifer, IntPtr uri);
private delegate void LoadStatusChangedCallback (IntPtr ossifer, OssiferLoadStatus status);
private delegate void DownloadStatusChangedCallback (IntPtr ossifer, OssiferDownloadStatus status, IntPtr mimetype, IntPtr destnation_uri);
@@ -40,7 +42,9 @@ namespace Banshee.WebBrowser
private struct Callbacks
{
public MimeTypePolicyDecisionRequestedCallback MimeTypePolicyDecisionRequested;
+ public NavigationPolicyDecisionRequestedCallback NavigationPolicyDecisionRequested;
public DownloadRequestedCallback DownloadRequested;
+ public ResourceRequestStartingCallback ResourceRequestStarting;
public LoadStatusChangedCallback LoadStatusChanged;
public DownloadStatusChangedCallback DownloadStatusChanged;
}
@@ -73,7 +77,10 @@ namespace Banshee.WebBrowser
callbacks = new Callbacks () {
MimeTypePolicyDecisionRequested =
new MimeTypePolicyDecisionRequestedCallback (HandleMimeTypePolicyDecisionRequested),
+ NavigationPolicyDecisionRequested =
+ new NavigationPolicyDecisionRequestedCallback (HandleNavigationPolicyDecisionRequested),
DownloadRequested = new DownloadRequestedCallback (HandleDownloadRequested),
+ ResourceRequestStarting = new ResourceRequestStartingCallback (HandleResourceRequestStarting),
LoadStatusChanged = new LoadStatusChangedCallback (HandleLoadStatusChanged),
DownloadStatusChanged = new DownloadStatusChangedCallback (HandleDownloadStatusChanged)
};
@@ -93,6 +100,16 @@ namespace Banshee.WebBrowser
return OssiferNavigationResponse.Unhandled;
}
+ private OssiferNavigationResponse HandleNavigationPolicyDecisionRequested (IntPtr ossifer, IntPtr mimetype)
+ {
+ return OnNavigationPolicyDecisionRequested (GLib.Marshaller.Utf8PtrToString (mimetype));
+ }
+
+ protected virtual OssiferNavigationResponse OnNavigationPolicyDecisionRequested (string mimetype)
+ {
+ return OssiferNavigationResponse.Unhandled;
+ }
+
private IntPtr HandleDownloadRequested (IntPtr ossifer, IntPtr mimetype, IntPtr uri, IntPtr suggested_filename)
{
var destination_uri = OnDownloadRequested (
@@ -109,6 +126,19 @@ namespace Banshee.WebBrowser
return null;
}
+ private IntPtr HandleResourceRequestStarting (IntPtr ossifer, IntPtr old_uri)
+ {
+ string new_uri = OnResourceRequestStarting (GLib.Marshaller.Utf8PtrToString (old_uri));
+ return new_uri == null
+ ? IntPtr.Zero
+ : GLib.Marshaller.StringToPtrGStrdup (new_uri);
+ }
+
+ protected virtual string OnResourceRequestStarting (string old_uri)
+ {
+ return null;
+ }
+
private void HandleLoadStatusChanged (IntPtr ossifer, OssiferLoadStatus status)
{
OnLoadStatusChanged (status);
@@ -263,4 +293,4 @@ namespace Banshee.WebBrowser
#endregion
}
-}
\ 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 2fda2b2..920904a 100644
--- a/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.c
+++ b/src/Core/Banshee.WebBrowser/libossifer/ossifer-web-view.c
@@ -32,9 +32,15 @@ G_DEFINE_TYPE (OssiferWebView, ossifer_web_view, WEBKIT_TYPE_WEB_VIEW);
typedef WebKitNavigationResponse (* OssiferWebViewMimeTypePolicyDecisionRequestedCallback)
(OssiferWebView *ossifer, const gchar *mimetype);
+typedef WebKitNavigationResponse (* OssiferWebViewNavigationPolicyDecisionRequestedCallback)
+ (OssiferWebView *ossifer, const gchar *uri);
+
typedef gchar * (* OssiferWebViewDownloadRequestedCallback)
(OssiferWebView *ossifer, const gchar *mimetype, const gchar *uri, const gchar *suggested_filename);
+typedef gchar * (* OssiferWebViewResourceRequestStartingCallback)
+ (OssiferWebView *ossifer, const gchar *uri);
+
typedef void (* OssiferWebViewDownloadStatusChanged)
(OssiferWebView *ossifer, WebKitDownloadStatus status, const gchar *mimetype, const gchar *uri);
@@ -43,7 +49,9 @@ typedef void (* OssiferWebViewLoadStatusChanged)
typedef struct {
OssiferWebViewMimeTypePolicyDecisionRequestedCallback mime_type_policy_decision_requested;
+ OssiferWebViewNavigationPolicyDecisionRequestedCallback navigation_policy_decision_requested;
OssiferWebViewDownloadRequestedCallback download_requested;
+ OssiferWebViewResourceRequestStartingCallback resource_request_starting;
OssiferWebViewLoadStatusChanged load_status_changed;
OssiferWebViewDownloadStatusChanged download_status_changed;
} OssiferWebViewCallbacks;
@@ -71,6 +79,25 @@ ossifer_web_view_create_web_view (WebKitWebView *web_view, WebKitWebFrame *frame
return WEBKIT_WEB_VIEW (g_object_new (OSSIFER_TYPE_WEB_VIEW, NULL));
}
+static void
+ossifer_web_view_resource_request_starting (WebKitWebView *web_view, WebKitWebFrame *frame,
+ WebKitWebResource *resource, WebKitNetworkRequest *request,
+ WebKitNetworkResponse *response, gpointer user_data)
+{
+ OssiferWebView *ossifer = OSSIFER_WEB_VIEW (web_view);
+ const gchar *old_uri;
+ gchar *new_uri = NULL;
+
+ if (ossifer->priv->callbacks.resource_request_starting != NULL) {
+ old_uri = webkit_network_request_get_uri (request);
+ new_uri = ossifer->priv->callbacks.resource_request_starting (ossifer, old_uri);
+ if (new_uri) {
+ webkit_network_request_set_uri (request, new_uri);
+ g_free (new_uri);
+ }
+ }
+}
+
static gboolean
ossifer_web_view_mime_type_policy_decision_requested (WebKitWebView *web_view, WebKitWebFrame *frame,
WebKitNetworkRequest *request, gchar *mimetype, WebKitWebPolicyDecision *policy_decision, gpointer user_data)
@@ -99,6 +126,35 @@ ossifer_web_view_mime_type_policy_decision_requested (WebKitWebView *web_view, W
return TRUE;
}
+static gboolean
+ossifer_web_view_navigation_policy_decision_requested (WebKitWebView *web_view, WebKitWebFrame *frame,
+ WebKitNetworkRequest *request, WebKitWebNavigationAction *action, WebKitWebPolicyDecision *policy_decision, gpointer user_data)
+{
+ OssiferWebView *ossifer = OSSIFER_WEB_VIEW (web_view);
+
+ if (ossifer->priv->callbacks.navigation_policy_decision_requested == NULL) {
+ return FALSE;
+ }
+
+ const gchar * uri = webkit_network_request_get_uri (request);
+ switch ((gint)ossifer->priv->callbacks.navigation_policy_decision_requested (ossifer, uri)) {
+ case 1000 /* Ossifer addition for 'unhandled' */:
+ return FALSE;
+ case (gint)WEBKIT_NAVIGATION_RESPONSE_DOWNLOAD:
+ webkit_web_policy_decision_download (policy_decision);
+ break;
+ case (gint)WEBKIT_NAVIGATION_RESPONSE_IGNORE:
+ webkit_web_policy_decision_ignore (policy_decision);
+ break;
+ case (gint)WEBKIT_NAVIGATION_RESPONSE_ACCEPT:
+ default:
+ webkit_web_policy_decision_use (policy_decision);
+ break;
+ }
+
+ return TRUE;
+}
+
static void
ossifer_web_view_download_notify_status (GObject* object, GParamSpec* pspec, gpointer user_data)
{
@@ -185,12 +241,16 @@ ossifer_web_view_init (OssiferWebView *ossifer)
g_object_get (ossifer, "settings", &settings, NULL);
g_object_set (settings,
"enable-plugins", FALSE,
+ "enable-page-cache", TRUE,
"enable-default-context-menu", FALSE,
NULL);
g_signal_connect (ossifer, "mime-type-policy-decision-requested",
G_CALLBACK (ossifer_web_view_mime_type_policy_decision_requested), NULL);
+ g_signal_connect (ossifer, "navigation-policy-decision-requested",
+ G_CALLBACK (ossifer_web_view_navigation_policy_decision_requested), NULL);
+
g_signal_connect (ossifer, "download-requested",
G_CALLBACK (ossifer_web_view_download_requested), NULL);
@@ -202,6 +262,9 @@ ossifer_web_view_init (OssiferWebView *ossifer)
g_signal_connect (ossifer, "create-web-view",
G_CALLBACK (ossifer_web_view_create_web_view), NULL);
+
+ g_signal_connect (ossifer, "resource-request-starting",
+ G_CALLBACK (ossifer_web_view_resource_request_starting), NULL);
}
// ---------------------------------------------------------------------------
@@ -298,4 +361,4 @@ ossifer_web_view_execute_script (OssiferWebView *ossifer, const gchar *script)
{
g_return_if_fail (OSSIFER_WEB_VIEW (ossifer));
return webkit_web_view_execute_script (WEBKIT_WEB_VIEW (ossifer), script);
-}
\ No newline at end of file
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]