// This file was generated by the Gtk# code generator.
// Any changes made will be lost if regenerated.

namespace WebKitSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	internal delegate void URISchemeRequestCallbackNative(IntPtr request, IntPtr user_data);

	internal class URISchemeRequestCallbackInvoker {

		URISchemeRequestCallbackNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

		~URISchemeRequestCallbackInvoker ()
		{
			if (__notify == null)
				return;
			__notify (__data);
		}

		internal URISchemeRequestCallbackInvoker (URISchemeRequestCallbackNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal URISchemeRequestCallbackInvoker (URISchemeRequestCallbackNative native_cb, IntPtr data) : this (native_cb, data, null) {}

		internal URISchemeRequestCallbackInvoker (URISchemeRequestCallbackNative native_cb, IntPtr data, GLib.DestroyNotify notify)
		{
			this.native_cb = native_cb;
			__data = data;
			__notify = notify;
		}

		internal WebKit.URISchemeRequestCallback Handler {
			get {
				return new WebKit.URISchemeRequestCallback(InvokeNative);
			}
		}

		void InvokeNative (WebKit.URISchemeRequest request)
		{
			native_cb (request == null ? IntPtr.Zero : request.Handle, __data);
		}
	}

	internal class URISchemeRequestCallbackWrapper {

		public void NativeCallback (IntPtr request, IntPtr user_data)
		{
			try {
				managed (GLib.Object.GetObject(request) as WebKit.URISchemeRequest);
				if (release_on_call)
					gch.Free ();
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
			}
		}

		bool release_on_call = false;
		GCHandle gch;

		public void PersistUntilCalled ()
		{
			release_on_call = true;
			gch = GCHandle.Alloc (this);
		}

		internal URISchemeRequestCallbackNative NativeDelegate;
		WebKit.URISchemeRequestCallback managed;

		public URISchemeRequestCallbackWrapper (WebKit.URISchemeRequestCallback managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new URISchemeRequestCallbackNative (NativeCallback);
		}

		public static WebKit.URISchemeRequestCallback GetManagedDelegate (URISchemeRequestCallbackNative native)
		{
			if (native == null)
				return null;
			URISchemeRequestCallbackWrapper wrapper = (URISchemeRequestCallbackWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
