CefResourceRequestHandler.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 handle events related to browser requests. The
  10. /// methods of this class will be called on the IO thread unless otherwise
  11. /// indicated.
  12. /// </summary>
  13. public abstract unsafe partial class CefResourceRequestHandler
  14. {
  15. private cef_cookie_access_filter_t* get_cookie_access_filter(cef_resource_request_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_request_t* request)
  16. {
  17. CheckSelf(self);
  18. var m_browser = CefBrowser.FromNativeOrNull(browser);
  19. var m_frame = CefFrame.FromNativeOrNull(frame);
  20. var m_request = CefRequest.FromNative(request); // TODO dispose?
  21. var m_result = GetCookieAccessFilter(m_browser, m_frame, m_request);
  22. return m_result != null ? m_result.ToNative() : null;
  23. }
  24. /// <summary>
  25. /// Called on the IO thread before a resource request is loaded. The |browser|
  26. /// and |frame| values represent the source of the request, and may be NULL for
  27. /// requests originating from service workers or CefURLRequest. To optionally
  28. /// filter cookies for the request return a CefCookieAccessFilter object. The
  29. /// |request| object cannot not be modified in this callback.
  30. /// </summary>
  31. protected abstract CefCookieAccessFilter GetCookieAccessFilter(CefBrowser browser, CefFrame frame, CefRequest request);
  32. private CefReturnValue on_before_resource_load(cef_resource_request_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_request_t* request, cef_request_callback_t* callback)
  33. {
  34. CheckSelf(self);
  35. var m_browser = CefBrowser.FromNativeOrNull(browser);
  36. var m_frame = CefFrame.FromNativeOrNull(frame);
  37. var m_request = CefRequest.FromNative(request);
  38. var m_callback = CefRequestCallback.FromNative(callback);
  39. var result = OnBeforeResourceLoad(m_browser, m_frame, m_request, m_callback);
  40. if (result != CefReturnValue.ContinueAsync)
  41. {
  42. m_browser.Dispose();
  43. m_frame.Dispose();
  44. m_request.Dispose();
  45. m_callback.Dispose();
  46. }
  47. return result;
  48. }
  49. /// <summary>
  50. /// Called on the IO thread before a resource request is loaded. The |browser|
  51. /// and |frame| values represent the source of the request, and may be NULL for
  52. /// requests originating from service workers or CefURLRequest. To redirect or
  53. /// change the resource load optionally modify |request|. Modification of the
  54. /// request URL will be treated as a redirect. Return RV_CONTINUE to continue
  55. /// the request immediately. Return RV_CONTINUE_ASYNC and call
  56. /// CefRequestCallback:: Continue() at a later time to continue or cancel the
  57. /// request asynchronously. Return RV_CANCEL to cancel the request immediately.
  58. /// </summary>
  59. protected virtual CefReturnValue OnBeforeResourceLoad(CefBrowser browser, CefFrame frame, CefRequest request, CefRequestCallback callback)
  60. {
  61. return CefReturnValue.Continue;
  62. }
  63. private cef_resource_handler_t* get_resource_handler(cef_resource_request_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_request_t* request)
  64. {
  65. CheckSelf(self);
  66. var m_browser = CefBrowser.FromNativeOrNull(browser);
  67. var m_frame = CefFrame.FromNativeOrNull(frame);
  68. using (var m_request = CefRequest.FromNative(request))
  69. {
  70. var handler = GetResourceHandler(m_browser, m_frame, m_request);
  71. return handler != null ? handler.ToNative() : null;
  72. }
  73. }
  74. /// <summary>
  75. /// Called on the IO thread before a resource is loaded. The |browser| and
  76. /// |frame| values represent the source of the request, and may be NULL for
  77. /// requests originating from service workers or CefURLRequest. To allow the
  78. /// resource to load using the default network loader return NULL. To specify a
  79. /// handler for the resource return a CefResourceHandler object. The |request|
  80. /// object cannot not be modified in this callback.
  81. /// </summary>
  82. protected virtual CefResourceHandler GetResourceHandler(CefBrowser browser, CefFrame frame, CefRequest request)
  83. {
  84. return null;
  85. }
  86. private void on_resource_redirect(cef_resource_request_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_request_t* request, cef_response_t* response, cef_string_t* new_url)
  87. {
  88. CheckSelf(self);
  89. var m_browser = CefBrowser.FromNativeOrNull(browser);
  90. var m_frame = CefFrame.FromNativeOrNull(frame);
  91. var m_request = CefRequest.FromNative(request); // TODO dispose?
  92. var m_response = CefResponse.FromNative(response); // TODO dispose?
  93. var m_newUrl = cef_string_t.ToString(new_url);
  94. var o_newUrl = m_newUrl;
  95. OnResourceRedirect(m_browser, m_frame, m_request, m_response, ref m_newUrl);
  96. if ((object)m_newUrl != (object)o_newUrl)
  97. {
  98. cef_string_t.Copy(m_newUrl, new_url);
  99. }
  100. }
  101. /// <summary>
  102. /// Called on the IO thread when a resource load is redirected. The |browser|
  103. /// and |frame| values represent the source of the request, and may be NULL for
  104. /// requests originating from service workers or CefURLRequest. The |request|
  105. /// parameter will contain the old URL and other request-related information.
  106. /// The |response| parameter will contain the response that resulted in the
  107. /// redirect. The |new_url| parameter will contain the new URL and can be
  108. /// changed if desired. The |request| and |response| objects cannot be modified
  109. /// in this callback.
  110. /// </summary>
  111. protected virtual void OnResourceRedirect(CefBrowser browser, CefFrame frame, CefRequest request, CefResponse response, ref string newUrl)
  112. {
  113. }
  114. private int on_resource_response(cef_resource_request_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_request_t* request, cef_response_t* response)
  115. {
  116. CheckSelf(self);
  117. var m_browser = CefBrowser.FromNativeOrNull(browser);
  118. var m_frame = CefFrame.FromNativeOrNull(frame);
  119. var m_request = CefRequest.FromNative(request); // TODO dispose?
  120. var m_response = CefResponse.FromNative(response); // TODO dispose?
  121. var m_result = OnResourceResponse(m_browser, m_frame, m_request, m_response);
  122. return m_result ? 1 : 0;
  123. }
  124. /// <summary>
  125. /// Called on the IO thread when a resource response is received. The |browser|
  126. /// and |frame| values represent the source of the request, and may be NULL for
  127. /// requests originating from service workers or CefURLRequest. To allow the
  128. /// resource load to proceed without modification return false. To redirect or
  129. /// retry the resource load optionally modify |request| and return true.
  130. /// Modification of the request URL will be treated as a redirect. Requests
  131. /// handled using the default network loader cannot be redirected in this
  132. /// callback. The |response| object cannot be modified in this callback.
  133. /// WARNING: Redirecting using this method is deprecated. Use
  134. /// OnBeforeResourceLoad or GetResourceHandler to perform redirects.
  135. /// </summary>
  136. protected virtual bool OnResourceResponse(CefBrowser browser, CefFrame frame, CefRequest request, CefResponse response)
  137. {
  138. return false;
  139. }
  140. private cef_response_filter_t* get_resource_response_filter(cef_resource_request_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_request_t* request, cef_response_t* response)
  141. {
  142. CheckSelf(self);
  143. var mBrowser = CefBrowser.FromNativeOrNull(browser);
  144. var mFrame = CefFrame.FromNativeOrNull(frame);
  145. var mRequest = CefRequest.FromNative(request); // TODO dispose?
  146. var mResponse = CefResponse.FromNative(response); // TODO dispose?
  147. var result = GetResourceResponseFilter(mBrowser, mFrame, mRequest, mResponse);
  148. if (result != null)
  149. {
  150. return result.ToNative();
  151. }
  152. return null;
  153. }
  154. /// <summary>
  155. /// Called on the IO thread to optionally filter resource response content. The
  156. /// |browser| and |frame| values represent the source of the request, and may
  157. /// be NULL for requests originating from service workers or CefURLRequest.
  158. /// |request| and |response| represent the request and response respectively
  159. /// and cannot be modified in this callback.
  160. /// </summary>
  161. protected virtual CefResponseFilter GetResourceResponseFilter(CefBrowser browser, CefFrame frame, CefRequest request, CefResponse response)
  162. {
  163. return null;
  164. }
  165. private void on_resource_load_complete(cef_resource_request_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_request_t* request, cef_response_t* response, CefUrlRequestStatus status, long received_content_length)
  166. {
  167. CheckSelf(self);
  168. var mBrowser = CefBrowser.FromNativeOrNull(browser);
  169. var mFrame = CefFrame.FromNativeOrNull(frame);
  170. var mRequest = CefRequest.FromNative(request); // TODO dispose?
  171. var mResponse = CefResponse.FromNative(response); // TODO dispose?
  172. OnResourceLoadComplete(mBrowser, mFrame, mRequest, mResponse, status, received_content_length);
  173. }
  174. /// <summary>
  175. /// Called on the IO thread when a resource load has completed. The |browser|
  176. /// and |frame| values represent the source of the request, and may be NULL for
  177. /// requests originating from service workers or CefURLRequest. |request| and
  178. /// |response| represent the request and response respectively and cannot be
  179. /// modified in this callback. |status| indicates the load completion status.
  180. /// |received_content_length| is the number of response bytes actually read.
  181. /// This method will be called for all requests, including requests that are
  182. /// aborted due to CEF shutdown or destruction of the associated browser. In
  183. /// cases where the associated browser is destroyed this callback may arrive
  184. /// after the CefLifeSpanHandler::OnBeforeClose callback for that browser. The
  185. /// CefFrame::IsValid method can be used to test for this situation, and care
  186. /// should be taken not to call |browser| or |frame| methods that modify state
  187. /// (like LoadURL, SendProcessMessage, etc.) if the frame is invalid.
  188. /// </summary>
  189. protected virtual void OnResourceLoadComplete(CefBrowser browser, CefFrame frame, CefRequest request, CefResponse response, CefUrlRequestStatus status, long receivedContentLength)
  190. {
  191. }
  192. private void on_protocol_execution(cef_resource_request_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_request_t* request, int* allow_os_execution)
  193. {
  194. CheckSelf(self);
  195. var m_browser = CefBrowser.FromNativeOrNull(browser);
  196. var m_frame = CefFrame.FromNativeOrNull(frame);
  197. var m_request = CefRequest.FromNative(request); // TODO dispose?
  198. var m_allow_os_execution = *allow_os_execution != 0;
  199. OnProtocolExecution(m_browser, m_frame, m_request, ref m_allow_os_execution);
  200. *allow_os_execution = m_allow_os_execution ? 1 : 0;
  201. }
  202. /// <summary>
  203. /// Called on the IO thread to handle requests for URLs with an unknown
  204. /// protocol component. The |browser| and |frame| values represent the source
  205. /// of the request, and may be NULL for requests originating from service
  206. /// workers or CefURLRequest. |request| cannot be modified in this callback.
  207. /// Set |allow_os_execution| to true to attempt execution via the registered OS
  208. /// protocol handler, if any.
  209. /// SECURITY WARNING: YOU SHOULD USE THIS METHOD TO ENFORCE RESTRICTIONS BASED
  210. /// ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION.
  211. /// </summary>
  212. protected virtual void OnProtocolExecution(CefBrowser browser, CefFrame frame, CefRequest request, ref bool allowOSExecution)
  213. {
  214. }
  215. }
  216. }