PageRenderTime 87ms CodeModel.GetById 74ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/NHTraceVSPackage/WebBrowserEx.cs

#
C# | 146 lines | 126 code | 20 blank | 0 comment | 5 complexity | 03299462349ce5251df6c6a45145a171 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Runtime.InteropServices;
  4using System.Text;
  5using System.Windows.Forms;
  6
  7namespace NHTrace
  8{
  9	public class WebBrowserEx : System.Windows.Forms.WebBrowser
 10	{
 11		#region WebBrowserSiteEx Class
 12		protected class WebBrowserSiteEx : System.Windows.Forms.WebBrowser.WebBrowserSite, IServiceProvider, IInternetSecurityManager
 13		{
 14			private static Guid IID_IInternetSecurityManager = 
 15				Marshal.GenerateGuidForType(typeof(IInternetSecurityManager));
 16
 17			private WebBrowserEx _webBrowser;
 18
 19			public WebBrowserSiteEx(WebBrowserEx webBrowser) : base(webBrowser)
 20			{
 21				_webBrowser = webBrowser;
 22			}
 23
 24			#region IServiceProvider Members
 25			public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
 26			{
 27				if(guidService == IID_IInternetSecurityManager &&
 28					riid == IID_IInternetSecurityManager)
 29				{
 30					ppvObject = Marshal.GetComInterfaceForObject(this,
 31						typeof(IInternetSecurityManager));
 32					return Constants.S_OK;
 33				}
 34				ppvObject = IntPtr.Zero;
 35				return Constants.E_NOINTERFACE;
 36			}
 37			#endregion IServiceProvider Members
 38
 39			#region IInternetSecurityManager Members
 40			public unsafe int SetSecuritySite(void* pSite)
 41			{
 42			    return Constants.INET_E_DEFAULT_ACTION;
 43			}
 44
 45			public unsafe int GetSecuritySite(void** ppSite)
 46			{
 47			    return Constants.INET_E_DEFAULT_ACTION;
 48			}
 49
 50			public unsafe int MapUrlToZone(string url, int* pdwZone, int dwFlags)
 51			{
 52        *pdwZone = 0;//local -> "Local", "Intranet", "Trusted", "Internet", "Restricted"
 53        return Constants.S_OK;
 54			}
 55
 56			public unsafe int GetSecurityId(string url, byte* pbSecurityId, int* pcbSecurityId, int dwReserved)
 57			{
 58        return Constants.INET_E_DEFAULT_ACTION;
 59			}
 60
 61			public unsafe int ProcessUrlAction(string url, int dwAction, byte* pPolicy, int cbPolicy,
 62				byte* pContext, int cbContext, int dwFlags, int dwReserved)
 63			{
 64        *((int*)pPolicy) = (int)Constants.UrlPolicy.URLPOLICY_ALLOW;
 65        return Constants.S_OK;
 66			}
 67
 68			public unsafe int QueryCustomPolicy(string pwszUrl, void* guidKey, byte** ppPolicy, int* pcbPolicy, byte* pContext, int cbContext, int dwReserved)
 69			{
 70			    return Constants.INET_E_DEFAULT_ACTION;
 71			}
 72
 73			public int SetZoneMapping(int dwZone, string lpszPattern, int dwFlags)
 74			{
 75			    return Constants.INET_E_DEFAULT_ACTION;
 76			}
 77
 78			public unsafe int GetZoneMappings(int dwZone, void** ppenumString, int dwFlags)
 79			{
 80			    return Constants.INET_E_DEFAULT_ACTION;
 81			}
 82			#endregion
 83
 84		}
 85		#endregion WebBrowserSiteEx Class
 86
 87		private WebBrowserSiteEx _site;
 88
 89		public WebBrowserEx()
 90		{
 91		}
 92
 93		protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
 94		{
 95			if(_site == null)
 96				_site = new WebBrowserSiteEx(this);
 97			return _site;
 98		}
 99
100	}
101
102  [ComImport]
103  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
104  [Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
105  public interface IServiceProvider
106  {
107    [PreserveSig]
108    int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject);
109  }
110
111  [ComImport]
112  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
113  [Guid("79eac9ee-baf9-11ce-8c82-00aa004ba90b")]
114  public interface IInternetSecurityManager
115  {
116    [PreserveSig]
117    unsafe int SetSecuritySite(void* pSite);
118    [PreserveSig]
119    unsafe int GetSecuritySite(void** ppSite);
120    [PreserveSig]
121    unsafe int MapUrlToZone([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, int* pdwZone, [In] int dwFlags);
122    [PreserveSig]
123    unsafe int GetSecurityId([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, byte* pbSecurityId, int* pcbSecurityId, int dwReserved);
124    [PreserveSig]
125    unsafe int ProcessUrlAction([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, int dwAction, byte* pPolicy, int cbPolicy, byte* pContext, int cbContext, int dwFlags, int dwReserved);
126    [PreserveSig]
127    unsafe int QueryCustomPolicy([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, void* guidKey, byte** ppPolicy, int* pcbPolicy, byte* pContext, int cbContext, int dwReserved);
128    [PreserveSig]
129    int SetZoneMapping(int dwZone, [In, MarshalAs(UnmanagedType.LPWStr)] string lpszPattern, int dwFlags);
130    [PreserveSig]
131    unsafe int GetZoneMappings(int dwZone, void** ppenumString, int dwFlags);
132  }
133
134  public static class Constants
135  {
136    public const int S_OK = 0;
137    public const int E_NOINTERFACE = unchecked((int)0x80004002);
138    public const int INET_E_DEFAULT_ACTION = unchecked((int)0x800C0011);
139    public enum UrlPolicy
140    {
141      URLPOLICY_ALLOW = 0,
142      URLPOLICY_QUERY = 1,
143      URLPOLICY_DISALLOW = 3,
144    }
145  }
146}