CefV8Interceptor.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 interceptor calls. The
  10. /// methods of this class will be called on the thread associated with the V8
  11. /// interceptor. Interceptor's named property handlers (with first argument of
  12. /// type CefString) are called when object is indexed by string. Indexed property
  13. /// handlers (with first argument of type int) are called when object is indexed
  14. /// by integer.
  15. /// </summary>
  16. public abstract unsafe partial class CefV8Interceptor
  17. {
  18. private int get_byname(cef_v8interceptor_t* self, cef_string_t* name, cef_v8value_t* @object, cef_v8value_t** retval, cef_string_t* exception)
  19. {
  20. CheckSelf(self);
  21. var m_name = cef_string_t.ToString(name);
  22. var m_obj = CefV8Value.FromNative(@object); // TODO dispose?
  23. CefV8Value m_retval;
  24. string m_exception;
  25. if (GetByName(m_name, m_obj, out m_retval, out m_exception))
  26. {
  27. *retval = m_retval != null ? m_retval.ToNative() : null;
  28. cef_string_t.Copy(m_exception, exception);
  29. return 1;
  30. }
  31. else
  32. {
  33. return 0;
  34. }
  35. }
  36. /// <summary>
  37. /// Handle retrieval of the interceptor value identified by |name|. |object| is
  38. /// the receiver ('this' object) of the interceptor. If retrieval succeeds, set
  39. /// |retval| to the return value. If the requested value does not exist, don't
  40. /// set either |retval| or |exception|. If retrieval fails, set |exception| to
  41. /// the exception that will be thrown. If the property has an associated
  42. /// accessor, it will be called only if you don't set |retval|.
  43. /// Return true if interceptor retrieval was handled, false otherwise.
  44. /// </summary>
  45. protected virtual bool GetByName(string name, CefV8Value @object, out CefV8Value retval, out string exception)
  46. {
  47. retval = null;
  48. exception = null;
  49. return false;
  50. }
  51. private int get_byindex(cef_v8interceptor_t* self, int index, cef_v8value_t* @object, cef_v8value_t** retval, cef_string_t* exception)
  52. {
  53. CheckSelf(self);
  54. var m_obj = CefV8Value.FromNative(@object);
  55. CefV8Value m_retval;
  56. string m_exception;
  57. if (GetByIndex(index, m_obj, out m_retval, out m_exception))
  58. {
  59. *retval = m_retval != null ? m_retval.ToNative() : null;
  60. cef_string_t.Copy(m_exception, exception);
  61. return 1;
  62. }
  63. else
  64. {
  65. return 0;
  66. }
  67. }
  68. /// <summary>
  69. /// Handle retrieval of the interceptor value identified by |index|. |object|
  70. /// is the receiver ('this' object) of the interceptor. If retrieval succeeds,
  71. /// set |retval| to the return value. If the requested value does not exist,
  72. /// don't set either |retval| or |exception|. If retrieval fails, set
  73. /// |exception| to the exception that will be thrown.
  74. /// Return true if interceptor retrieval was handled, false otherwise.
  75. /// </summary>
  76. protected virtual bool GetByIndex(int index, CefV8Value @object, out CefV8Value retval, out string exception)
  77. {
  78. retval = null;
  79. exception = null;
  80. return false;
  81. }
  82. private int set_byname(cef_v8interceptor_t* self, cef_string_t* name, cef_v8value_t* @object, cef_v8value_t* value, cef_string_t* exception)
  83. {
  84. CheckSelf(self);
  85. var m_name = cef_string_t.ToString(name);
  86. var m_obj = CefV8Value.FromNative(@object); // TODO dispose?
  87. var m_value = CefV8Value.FromNative(value); // TODO dispose?
  88. string m_exception;
  89. if (SetByName(m_name, m_obj, m_value, out m_exception))
  90. {
  91. cef_string_t.Copy(m_exception, exception);
  92. return 1;
  93. }
  94. else
  95. {
  96. return 0;
  97. }
  98. }
  99. /// <summary>
  100. /// Handle assignment of the interceptor value identified by |name|. |object|
  101. /// is the receiver ('this' object) of the interceptor. |value| is the new
  102. /// value being assigned to the interceptor. If assignment fails, set
  103. /// |exception| to the exception that will be thrown. This setter will always
  104. /// be called, even when the property has an associated accessor.
  105. /// Return true if interceptor assignment was handled, false otherwise.
  106. /// </summary>
  107. protected virtual bool SetByName(string name, CefV8Value @object, CefV8Value value, out string exception)
  108. {
  109. exception = null;
  110. return false;
  111. }
  112. private int set_byindex(cef_v8interceptor_t* self, int index, cef_v8value_t* @object, cef_v8value_t* value, cef_string_t* exception)
  113. {
  114. CheckSelf(self);
  115. var m_obj = CefV8Value.FromNative(@object); // TODO dispose?
  116. var m_value = CefV8Value.FromNative(value); // TODO dispose?
  117. string m_exception;
  118. if (SetByIndex(index, m_obj, m_value, out m_exception))
  119. {
  120. cef_string_t.Copy(m_exception, exception);
  121. return 1;
  122. }
  123. else
  124. {
  125. return 0;
  126. }
  127. }
  128. /// <summary>
  129. /// Handle assignment of the interceptor value identified by |index|. |object|
  130. /// is the receiver ('this' object) of the interceptor. |value| is the new
  131. /// value being assigned to the interceptor. If assignment fails, set
  132. /// |exception| to the exception that will be thrown.
  133. /// Return true if interceptor assignment was handled, false otherwise.
  134. /// </summary>
  135. protected virtual bool SetByIndex(int index, CefV8Value @object, CefV8Value value, out string exception)
  136. {
  137. exception = null;
  138. return false;
  139. }
  140. }
  141. }