CefV8Handler.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /// Interface that should be implemented to handle V8 function calls. The methods
  10. /// of this class will always be called on the render process main thread.
  11. /// </summary>
  12. public abstract unsafe partial class CefV8Handler
  13. {
  14. private static readonly CefV8Value[] emtpyArgs = new CefV8Value[0];
  15. private int execute(cef_v8handler_t* self, cef_string_t* name, cef_v8value_t* @object, UIntPtr argumentsCount, cef_v8value_t** arguments, cef_v8value_t** retval, cef_string_t* exception)
  16. {
  17. using (CefObjectTracker.StartTracking())
  18. {
  19. CheckSelf(self);
  20. var m_name = cef_string_t.ToString(name);
  21. var m_obj = CefV8Value.FromNative(@object);
  22. var argc = (int)argumentsCount;
  23. CefV8Value[] m_arguments;
  24. if (argc == 0) { m_arguments = emtpyArgs; }
  25. else
  26. {
  27. m_arguments = new CefV8Value[argc];
  28. for (var i = 0; i < argc; i++)
  29. {
  30. m_arguments[i] = CefV8Value.FromNative(arguments[i]);
  31. }
  32. }
  33. CefV8Value m_returnValue;
  34. string m_exception;
  35. var handled = Execute(m_name, m_obj, m_arguments, out m_returnValue, out m_exception);
  36. if (handled)
  37. {
  38. if (m_exception != null)
  39. {
  40. cef_string_t.Copy(m_exception, exception);
  41. }
  42. else if (m_returnValue != null)
  43. {
  44. *retval = m_returnValue.ToNative();
  45. }
  46. }
  47. return handled ? 1 : 0;
  48. }
  49. }
  50. /// <summary>
  51. /// Handle execution of the function identified by |name|. |object| is the
  52. /// receiver ('this' object) of the function. |arguments| is the list of
  53. /// arguments passed to the function. If execution succeeds set |retval| to the
  54. /// function return value. If execution fails set |exception| to the exception
  55. /// that will be thrown. Return true if execution was handled.
  56. /// </summary>
  57. protected abstract bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception);
  58. }
  59. }