CefRequestContextHandler.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Implement this interface to provide handler implementations. The handler
  10. /// instance will not be released until all objects related to the context have
  11. /// been destroyed.
  12. /// </summary>
  13. public abstract unsafe partial class CefRequestContextHandler
  14. {
  15. private void on_request_context_initialized(cef_request_context_handler_t* self, cef_request_context_t* request_context)
  16. {
  17. CheckSelf(self);
  18. var mRequestContext = CefRequestContext.FromNative(request_context); // TODO dispose?
  19. OnRequestContextInitialized(mRequestContext);
  20. }
  21. /// <summary>
  22. /// Called on the browser process UI thread immediately after the request
  23. /// context has been initialized.
  24. /// </summary>
  25. protected virtual void OnRequestContextInitialized(CefRequestContext requestContext) { }
  26. private int on_before_plugin_load(cef_request_context_handler_t* self, cef_string_t* mime_type, cef_string_t* plugin_url, int is_main_frame, cef_string_t* top_origin_url, cef_web_plugin_info_t* plugin_info, CefPluginPolicy* plugin_policy)
  27. {
  28. CheckSelf(self);
  29. var mMimeType = cef_string_t.ToString(mime_type);
  30. var mPluginUrl = cef_string_t.ToString(plugin_url);
  31. var mIsMainFrame = is_main_frame != 0;
  32. var mTopOriginUrl = cef_string_t.ToString(top_origin_url);
  33. var mPluginInfo = CefWebPluginInfo.FromNative(plugin_info); // TODO dispose?
  34. var mPluginPolicy = *plugin_policy;
  35. var result = OnBeforePluginLoad(mMimeType, mPluginUrl, mIsMainFrame, mTopOriginUrl, mPluginInfo, ref mPluginPolicy);
  36. *plugin_policy = mPluginPolicy;
  37. return result ? 1 : 0;
  38. }
  39. /// <summary>
  40. /// Called on multiple browser process threads before a plugin instance is
  41. /// loaded. |mime_type| is the mime type of the plugin that will be loaded.
  42. /// |plugin_url| is the content URL that the plugin will load and may be empty.
  43. /// |is_main_frame| will be true if the plugin is being loaded in the main
  44. /// (top-level) frame, |top_origin_url| is the URL for the top-level frame that
  45. /// contains the plugin when loading a specific plugin instance or empty when
  46. /// building the initial list of enabled plugins for 'navigator.plugins'
  47. /// JavaScript state. |plugin_info| includes additional information about the
  48. /// plugin that will be loaded. |plugin_policy| is the recommended policy.
  49. /// Modify |plugin_policy| and return true to change the policy. Return false
  50. /// to use the recommended policy. The default plugin policy can be set at
  51. /// runtime using the `--plugin-policy=[allow|detect|block]` command-line flag.
  52. /// Decisions to mark a plugin as disabled by setting |plugin_policy| to
  53. /// PLUGIN_POLICY_DISABLED may be cached when |top_origin_url| is empty. To
  54. /// purge the plugin list cache and potentially trigger new calls to this
  55. /// method call CefRequestContext::PurgePluginListCache.
  56. /// </summary>
  57. protected virtual bool OnBeforePluginLoad(string mimeType, string pluginUrl, bool isMainFrame, string topOriginUrl, CefWebPluginInfo pluginInfo, ref CefPluginPolicy pluginPolicy)
  58. {
  59. return false;
  60. }
  61. private cef_resource_request_handler_t* get_resource_request_handler(cef_request_context_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_request_t* request, int is_navigation, int is_download, cef_string_t* request_initiator, int* disable_default_handling)
  62. {
  63. CheckSelf(self);
  64. var m_browser = CefBrowser.FromNativeOrNull(browser);
  65. var m_frame = CefFrame.FromNativeOrNull(frame);
  66. var m_request = CefRequest.FromNative(request); // TODO dispose?
  67. var m_isNavigation = is_navigation != 0;
  68. var m_isDownload = is_download != 0;
  69. var m_requestInitiator = cef_string_t.ToString(request_initiator);
  70. var m_disableDefaultHandling = *disable_default_handling != 0;
  71. var m_result = GetResourceRequestHandler(m_browser, m_frame, m_request, m_isNavigation, m_isDownload, m_requestInitiator, ref m_disableDefaultHandling);
  72. *disable_default_handling = m_disableDefaultHandling ? 1 : 0;
  73. return m_result != null ? m_result.ToNative() : null;
  74. }
  75. /// <summary>
  76. /// Called on the browser process IO thread before a resource request is
  77. /// initiated. The |browser| and |frame| values represent the source of the
  78. /// request, and may be NULL for requests originating from service workers or
  79. /// CefURLRequest. |request| represents the request contents and cannot be
  80. /// modified in this callback. |is_navigation| will be true if the resource
  81. /// request is a navigation. |is_download| will be true if the resource request
  82. /// is a download. |request_initiator| is the origin (scheme + domain) of the
  83. /// page that initiated the request. Set |disable_default_handling| to true to
  84. /// disable default handling of the request, in which case it will need to be
  85. /// handled via CefResourceRequestHandler::GetResourceHandler or it will be
  86. /// canceled. To allow the resource load to proceed with default handling
  87. /// return NULL. To specify a handler for the resource return a
  88. /// CefResourceRequestHandler object. This method will not be called if the
  89. /// client associated with |browser| returns a non-NULL value from
  90. /// CefRequestHandler::GetResourceRequestHandler for the same request
  91. /// (identified by CefRequest::GetIdentifier).
  92. /// </summary>
  93. protected abstract CefResourceRequestHandler GetResourceRequestHandler(CefBrowser browser, CefFrame frame, CefRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling);
  94. }
  95. }