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