CefDevToolsMessageObserver.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /// Callback interface for CefBrowserHost::AddDevToolsMessageObserver. The
  10. /// methods of this class will be called on the browser process UI thread.
  11. /// </summary>
  12. public abstract unsafe partial class CefDevToolsMessageObserver
  13. {
  14. private int on_dev_tools_message(cef_dev_tools_message_observer_t* self, cef_browser_t* browser, void* message, UIntPtr message_size)
  15. {
  16. CheckSelf(self);
  17. var m_browser = CefBrowser.FromNative(browser);
  18. var m_result = OnDevToolsMessage(m_browser, (IntPtr)message, checked((int)message_size));
  19. return m_result ? 1 : 0;
  20. }
  21. /// <summary>
  22. /// Method that will be called on receipt of a DevTools protocol message.
  23. /// |browser| is the originating browser instance. |message| is a UTF8-encoded
  24. /// JSON dictionary representing either a method result or an event. |message|
  25. /// is only valid for the scope of this callback and should be copied if
  26. /// necessary. Return true if the message was handled or false if the message
  27. /// should be further processed and passed to the OnDevToolsMethodResult or
  28. /// OnDevToolsEvent methods as appropriate.
  29. /// Method result dictionaries include an "id" (int) value that identifies the
  30. /// orginating method call sent from CefBrowserHost::SendDevToolsMessage, and
  31. /// optionally either a "result" (dictionary) or "error" (dictionary) value.
  32. /// The "error" dictionary will contain "code" (int) and "message" (string)
  33. /// values. Event dictionaries include a "method" (string) value and optionally
  34. /// a "params" (dictionary) value. See the DevTools protocol documentation at
  35. /// https://chromedevtools.github.io/devtools-protocol/ for details of
  36. /// supported method calls and the expected "result" or "params" dictionary
  37. /// contents. JSON dictionaries can be parsed using the CefParseJSON function
  38. /// if desired, however be aware of performance considerations when parsing
  39. /// large messages (some of which may exceed 1MB in size).
  40. /// </summary>
  41. protected abstract bool OnDevToolsMessage(CefBrowser browser, IntPtr message, int messageSize);
  42. private void on_dev_tools_method_result(cef_dev_tools_message_observer_t* self, cef_browser_t* browser, int message_id, int success, void* result, UIntPtr result_size)
  43. {
  44. CheckSelf(self);
  45. var m_browser = CefBrowser.FromNative(browser);
  46. OnDevToolsMethodResult(m_browser, message_id, success != 0, (IntPtr)result, checked((int)result_size));
  47. }
  48. /// <summary>
  49. /// Method that will be called after attempted execution of a DevTools protocol
  50. /// method. |browser| is the originating browser instance. |message_id| is the
  51. /// "id" value that identifies the originating method call message. If the
  52. /// method succeeded |success| will be true and |result| will be the
  53. /// UTF8-encoded JSON "result" dictionary value (which may be empty). If the
  54. /// method failed |success| will be false and |result| will be the UTF8-encoded
  55. /// JSON "error" dictionary value. |result| is only valid for the scope of this
  56. /// callback and should be copied if necessary. See the OnDevToolsMessage
  57. /// documentation for additional details on |result| contents.
  58. /// </summary>
  59. protected abstract void OnDevToolsMethodResult(CefBrowser browser, int messageId, bool success, IntPtr result, int resultSize);
  60. private void on_dev_tools_event(cef_dev_tools_message_observer_t* self, cef_browser_t* browser, cef_string_t* method, void* @params, UIntPtr params_size)
  61. {
  62. CheckSelf(self);
  63. var m_browser = CefBrowser.FromNative(browser);
  64. var m_method = cef_string_t.ToString(method);
  65. OnDevToolsEvent(m_browser, m_method, (IntPtr)@params, checked((int)params_size));
  66. }
  67. /// <summary>
  68. /// Method that will be called on receipt of a DevTools protocol event.
  69. /// |browser| is the originating browser instance. |method| is the "method"
  70. /// value. |params| is the UTF8-encoded JSON "params" dictionary value (which
  71. /// may be empty). |params| is only valid for the scope of this callback and
  72. /// should be copied if necessary. See the OnDevToolsMessage documentation for
  73. /// additional details on |params| contents.
  74. /// </summary>
  75. protected abstract void OnDevToolsEvent(CefBrowser browser, string method, IntPtr parameters, int parametersSize);
  76. private void on_dev_tools_agent_attached(cef_dev_tools_message_observer_t* self, cef_browser_t* browser)
  77. {
  78. CheckSelf(self);
  79. var m_browser = CefBrowser.FromNative(browser);
  80. OnDevToolsAgentAttached(m_browser);
  81. }
  82. /// <summary>
  83. /// Method that will be called when the DevTools agent has attached. |browser|
  84. /// is the originating browser instance. This will generally occur in response
  85. /// to the first message sent while the agent is detached.
  86. /// </summary>
  87. protected abstract void OnDevToolsAgentAttached(CefBrowser browser);
  88. private void on_dev_tools_agent_detached(cef_dev_tools_message_observer_t* self, cef_browser_t* browser)
  89. {
  90. CheckSelf(self);
  91. var m_browser = CefBrowser.FromNative(browser);
  92. OnDevToolsAgentDetached(m_browser);
  93. }
  94. /// <summary>
  95. /// Method that will be called when the DevTools agent has detached. |browser|
  96. /// is the originating browser instance. Any method results that were pending
  97. /// before the agent became detached will not be delivered, and any active
  98. /// event subscriptions will be canceled.
  99. /// </summary>
  100. protected abstract void OnDevToolsAgentDetached(CefBrowser browser);
  101. }
  102. }