CefRenderProcessHandler.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /// Class used to implement render process callbacks. The methods of this class
  10. /// will be called on the render process main thread (TID_RENDERER) unless
  11. /// otherwise indicated.
  12. /// </summary>
  13. public abstract unsafe partial class CefRenderProcessHandler
  14. {
  15. private void on_web_kit_initialized(cef_render_process_handler_t* self)
  16. {
  17. CheckSelf(self);
  18. OnWebKitInitialized();
  19. }
  20. /// <summary>
  21. /// Called after WebKit has been initialized.
  22. /// </summary>
  23. protected virtual void OnWebKitInitialized()
  24. {
  25. }
  26. private void on_browser_created(cef_render_process_handler_t* self, cef_browser_t* browser, cef_dictionary_value_t* extra_info)
  27. {
  28. CheckSelf(self);
  29. var m_browser = CefBrowser.FromNative(browser);
  30. var m_extraInfo = CefDictionaryValue.FromNative(extra_info); // TODO dispose?
  31. OnBrowserCreated(m_browser, m_extraInfo);
  32. }
  33. /// <summary>
  34. /// Called after a browser has been created. When browsing cross-origin a new
  35. /// browser will be created before the old browser with the same identifier is
  36. /// destroyed. |extra_info| is a read-only value originating from
  37. /// CefBrowserHost::CreateBrowser(), CefBrowserHost::CreateBrowserSync(),
  38. /// CefLifeSpanHandler::OnBeforePopup() or CefBrowserView::CreateBrowserView().
  39. /// </summary>
  40. protected virtual void OnBrowserCreated(CefBrowser browser, CefDictionaryValue extraInfo)
  41. {
  42. }
  43. private void on_browser_destroyed(cef_render_process_handler_t* self, cef_browser_t* browser)
  44. {
  45. CheckSelf(self);
  46. var m_browser = CefBrowser.FromNative(browser);
  47. OnBrowserDestroyed(m_browser);
  48. }
  49. /// <summary>
  50. /// Called before a browser is destroyed.
  51. /// </summary>
  52. protected virtual void OnBrowserDestroyed(CefBrowser browser)
  53. {
  54. }
  55. private cef_load_handler_t* get_load_handler(cef_render_process_handler_t* self)
  56. {
  57. CheckSelf(self);
  58. var result = GetLoadHandler();
  59. return result != null ? result.ToNative() : null;
  60. }
  61. /// <summary>
  62. /// Return the handler for browser load status events.
  63. /// </summary>
  64. protected virtual CefLoadHandler GetLoadHandler()
  65. {
  66. return null;
  67. }
  68. private void on_context_created(cef_render_process_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_v8context_t* context)
  69. {
  70. CheckSelf(self);
  71. var m_browser = CefBrowser.FromNative(browser);
  72. var m_frame = CefFrame.FromNative(frame);
  73. var m_context = CefV8Context.FromNative(context);
  74. OnContextCreated(m_browser, m_frame, m_context);
  75. }
  76. /// <summary>
  77. /// Called immediately after the V8 context for a frame has been created. To
  78. /// retrieve the JavaScript 'window' object use the CefV8Context::GetGlobal()
  79. /// method. V8 handles can only be accessed from the thread on which they are
  80. /// created. A task runner for posting tasks on the associated thread can be
  81. /// retrieved via the CefV8Context::GetTaskRunner() method.
  82. /// </summary>
  83. protected virtual void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
  84. {
  85. }
  86. private void on_context_released(cef_render_process_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_v8context_t* context)
  87. {
  88. CheckSelf(self);
  89. var m_browser = CefBrowser.FromNative(browser);
  90. var m_frame = CefFrame.FromNative(frame);
  91. var m_context = CefV8Context.FromNative(context);
  92. OnContextReleased(m_browser, m_frame, m_context);
  93. }
  94. /// <summary>
  95. /// Called immediately before the V8 context for a frame is released. No
  96. /// references to the context should be kept after this method is called.
  97. /// </summary>
  98. protected virtual void OnContextReleased(CefBrowser browser, CefFrame frame, CefV8Context context)
  99. {
  100. }
  101. private void on_uncaught_exception(cef_render_process_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_v8context_t* context, cef_v8exception_t* exception, cef_v8stack_trace_t* stackTrace)
  102. {
  103. CheckSelf(self);
  104. var mBrowser = CefBrowser.FromNative(browser);
  105. var mFrame = CefFrame.FromNative(frame);
  106. var mContext = CefV8Context.FromNative(context); // TODO dispose?
  107. var mException = CefV8Exception.FromNative(exception); // TODO dispose?
  108. var mStackTrace = CefV8StackTrace.FromNative(stackTrace); // TODO dispose?
  109. OnUncaughtException(mBrowser, mFrame, mContext, mException, mStackTrace);
  110. }
  111. /// <summary>
  112. /// Called for global uncaught exceptions in a frame. Execution of this
  113. /// callback is disabled by default. To enable set
  114. /// CefSettings.uncaught_exception_stack_size &gt; 0.
  115. /// </summary>
  116. protected virtual void OnUncaughtException(CefBrowser browser, CefFrame frame, CefV8Context context, CefV8Exception exception, CefV8StackTrace stackTrace)
  117. {
  118. }
  119. private void on_focused_node_changed(cef_render_process_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_domnode_t* node)
  120. {
  121. CheckSelf(self);
  122. var m_browser = CefBrowser.FromNative(browser);
  123. var m_frame = CefFrame.FromNative(frame);
  124. var m_node = CefDomNode.FromNativeOrNull(node);
  125. OnFocusedNodeChanged(m_browser, m_frame, m_node);
  126. if (m_node != null) m_node.Dispose();
  127. }
  128. /// <summary>
  129. /// Called when a new node in the the browser gets focus. The |node| value may
  130. /// be empty if no specific node has gained focus. The node object passed to
  131. /// this method represents a snapshot of the DOM at the time this method is
  132. /// executed. DOM objects are only valid for the scope of this method. Do not
  133. /// keep references to or attempt to access any DOM objects outside the scope
  134. /// of this method.
  135. /// </summary>
  136. protected virtual void OnFocusedNodeChanged(CefBrowser browser, CefFrame frame, CefDomNode node)
  137. {
  138. }
  139. private int on_process_message_received(cef_render_process_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, CefProcessId source_process, cef_process_message_t* message)
  140. {
  141. CheckSelf(self);
  142. var m_browser = CefBrowser.FromNative(browser);
  143. var m_frame = CefFrame.FromNative(frame);
  144. using (var m_message = CefProcessMessage.FromNative(message))
  145. {
  146. var result = OnProcessMessageReceived(m_browser, m_frame, source_process, m_message);
  147. return result ? 1 : 0;
  148. }
  149. }
  150. /// <summary>
  151. /// Called when a new message is received from a different process. Return true
  152. /// if the message was handled or false otherwise. Do not keep a reference to
  153. /// or attempt to access the message outside of this callback.
  154. /// </summary>
  155. protected virtual bool OnProcessMessageReceived(CefBrowser browser, CefFrame frame, CefProcessId sourceProcess, CefProcessMessage message)
  156. {
  157. return false;
  158. }
  159. }
  160. }