CefLoadHandler.cs 5.0 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 handle events related to browser load status. The
  10. /// methods of this class will be called on the browser process UI thread or
  11. /// render process main thread (TID_RENDERER).
  12. /// </summary>
  13. public abstract unsafe partial class CefLoadHandler
  14. {
  15. private void on_loading_state_change(cef_load_handler_t* self, cef_browser_t* browser, int isLoading, int canGoBack, int canGoForward)
  16. {
  17. CheckSelf(self);
  18. var mBrowser = CefBrowser.FromNative(browser);
  19. OnLoadingStateChange(mBrowser, isLoading != 0, canGoBack != 0, canGoForward != 0);
  20. }
  21. /// <summary>
  22. /// Called when the loading state has changed. This callback will be executed
  23. /// twice -- once when loading is initiated either programmatically or by user
  24. /// action, and once when loading is terminated due to completion, cancellation
  25. /// of failure. It will be called before any calls to OnLoadStart and after all
  26. /// calls to OnLoadError and/or OnLoadEnd.
  27. /// </summary>
  28. protected virtual void OnLoadingStateChange(CefBrowser browser, bool isLoading, bool canGoBack, bool canGoForward)
  29. {
  30. }
  31. private void on_load_start(cef_load_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, CefTransitionType transition_type)
  32. {
  33. CheckSelf(self);
  34. var m_browser = CefBrowser.FromNative(browser);
  35. var m_frame = CefFrame.FromNative(frame);
  36. OnLoadStart(m_browser, m_frame, transition_type);
  37. }
  38. /// <summary>
  39. /// Called after a navigation has been committed and before the browser begins
  40. /// loading contents in the frame. The |frame| value will never be empty --
  41. /// call the IsMain() method to check if this frame is the main frame.
  42. /// |transition_type| provides information about the source of the navigation
  43. /// and an accurate value is only available in the browser process. Multiple
  44. /// frames may be loading at the same time. Sub-frames may start or continue
  45. /// loading after the main frame load has ended. This method will not be called
  46. /// for same page navigations (fragments, history state, etc.) or for
  47. /// navigations that fail or are canceled before commit. For notification of
  48. /// overall browser load status use OnLoadingStateChange instead.
  49. /// </summary>
  50. protected virtual void OnLoadStart(CefBrowser browser, CefFrame frame, CefTransitionType transitionType)
  51. {
  52. }
  53. private void on_load_end(cef_load_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, int httpStatusCode)
  54. {
  55. CheckSelf(self);
  56. var m_browser = CefBrowser.FromNative(browser);
  57. var m_frame = CefFrame.FromNative(frame);
  58. OnLoadEnd(m_browser, m_frame, httpStatusCode);
  59. }
  60. /// <summary>
  61. /// Called when the browser is done loading a frame. The |frame| value will
  62. /// never be empty -- call the IsMain() method to check if this frame is the
  63. /// main frame. Multiple frames may be loading at the same time. Sub-frames may
  64. /// start or continue loading after the main frame load has ended. This method
  65. /// will not be called for same page navigations (fragments, history state,
  66. /// etc.) or for navigations that fail or are canceled before commit. For
  67. /// notification of overall browser load status use OnLoadingStateChange
  68. /// instead.
  69. /// </summary>
  70. protected virtual void OnLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode)
  71. {
  72. }
  73. private void on_load_error(cef_load_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, CefErrorCode errorCode, cef_string_t* errorText, cef_string_t* failedUrl)
  74. {
  75. CheckSelf(self);
  76. var m_browser = CefBrowser.FromNative(browser);
  77. var m_frame = CefFrame.FromNative(frame);
  78. var m_errorText = cef_string_t.ToString(errorText);
  79. var m_failedUrl = cef_string_t.ToString(failedUrl);
  80. OnLoadError(m_browser, m_frame, errorCode, m_errorText, m_failedUrl);
  81. }
  82. /// <summary>
  83. /// Called when a navigation fails or is canceled. This method may be called
  84. /// by itself if before commit or in combination with OnLoadStart/OnLoadEnd if
  85. /// after commit. |errorCode| is the error code number, |errorText| is the
  86. /// error text and |failedUrl| is the URL that failed to load.
  87. /// See net\base\net_error_list.h for complete descriptions of the error codes.
  88. /// </summary>
  89. protected virtual void OnLoadError(CefBrowser browser, CefFrame frame, CefErrorCode errorCode, string errorText, string failedUrl)
  90. {
  91. }
  92. }
  93. }