CefV8Accessor.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 be called on the thread associated with the V8 function.
  11. /// </summary>
  12. public abstract unsafe partial class CefV8Accessor
  13. {
  14. private int get(cef_v8accessor_t* self, cef_string_t* name, cef_v8value_t* @object, cef_v8value_t** retval, cef_string_t* exception)
  15. {
  16. CheckSelf(self);
  17. var m_name = cef_string_t.ToString(name);
  18. var m_obj = CefV8Value.FromNative(@object); // TODO dispose?
  19. CefV8Value m_returnValue;
  20. string mException;
  21. var handled = Get(m_name, m_obj, out m_returnValue, out mException);
  22. if (handled)
  23. {
  24. if (mException != null)
  25. {
  26. cef_string_t.Copy(mException, exception);
  27. }
  28. else if (m_returnValue != null)
  29. {
  30. *retval = m_returnValue.ToNative();
  31. }
  32. }
  33. return handled ? 1 : 0;
  34. }
  35. /// <summary>
  36. /// Handle retrieval the accessor value identified by |name|. |object| is the
  37. /// receiver ('this' object) of the accessor. If retrieval succeeds set
  38. /// |retval| to the return value. If retrieval fails set |exception| to the
  39. /// exception that will be thrown. Return true if accessor retrieval was
  40. /// handled.
  41. /// </summary>
  42. protected abstract bool Get(string name, CefV8Value obj, out CefV8Value returnValue, out string exception);
  43. private int set(cef_v8accessor_t* self, cef_string_t* name, cef_v8value_t* @object, cef_v8value_t* value, cef_string_t* exception)
  44. {
  45. CheckSelf(self);
  46. var m_name = cef_string_t.ToString(name);
  47. var m_obj = CefV8Value.FromNative(@object); // TODO dispose?
  48. var m_value = CefV8Value.FromNative(value); // TODO dispose?
  49. string mException;
  50. var handled = this.Set(m_name, m_obj, m_value, out mException);
  51. if (handled)
  52. {
  53. if (mException != null)
  54. {
  55. cef_string_t.Copy(mException, exception);
  56. }
  57. }
  58. return handled ? 1 : 0;
  59. }
  60. /// <summary>
  61. /// Handle assignment of the accessor value identified by |name|. |object| is
  62. /// the receiver ('this' object) of the accessor. |value| is the new value
  63. /// being assigned to the accessor. If assignment fails set |exception| to the
  64. /// exception that will be thrown. Return true if accessor assignment was
  65. /// handled.
  66. /// </summary>
  67. protected abstract bool Set(string name, CefV8Value obj, CefV8Value value, out string exception);
  68. }
  69. }