CefV8Context.cs 5.8 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. /// Class representing a V8 context handle. V8 handles can only be accessed from
  10. /// the thread on which they are created. Valid threads for creating a V8 handle
  11. /// include the render process main thread (TID_RENDERER) and WebWorker threads.
  12. /// A task runner for posting tasks on the associated thread can be retrieved via
  13. /// the CefV8Context::GetTaskRunner() method.
  14. /// </summary>
  15. public sealed unsafe partial class CefV8Context
  16. {
  17. /// <summary>
  18. /// Returns the current (top) context object in the V8 context stack.
  19. /// </summary>
  20. public static CefV8Context GetCurrentContext()
  21. {
  22. return CefV8Context.FromNative(
  23. cef_v8context_t.get_current_context()
  24. );
  25. }
  26. /// <summary>
  27. /// Returns the entered (bottom) context object in the V8 context stack.
  28. /// </summary>
  29. public static CefV8Context GetEnteredContext()
  30. {
  31. return CefV8Context.FromNative(
  32. cef_v8context_t.get_entered_context()
  33. );
  34. }
  35. /// <summary>
  36. /// Returns true if V8 is currently inside a context.
  37. /// </summary>
  38. public static bool InContext
  39. {
  40. get { return cef_v8context_t.in_context() != 0; }
  41. }
  42. /// <summary>
  43. /// Returns the task runner associated with this context. V8 handles can only
  44. /// be accessed from the thread on which they are created. This method can be
  45. /// called on any render process thread.
  46. /// </summary>
  47. public CefTaskRunner GetTaskRunner()
  48. {
  49. return CefTaskRunner.FromNative(
  50. cef_v8context_t.get_task_runner(_self)
  51. );
  52. }
  53. /// <summary>
  54. /// Returns true if the underlying handle is valid and it can be accessed on
  55. /// the current thread. Do not call any other methods if this method returns
  56. /// false.
  57. /// </summary>
  58. public bool IsValid
  59. {
  60. get { return cef_v8context_t.is_valid(_self) != 0; }
  61. }
  62. /// <summary>
  63. /// Returns the browser for this context. This method will return an empty
  64. /// reference for WebWorker contexts.
  65. /// </summary>
  66. public CefBrowser GetBrowser()
  67. {
  68. return CefBrowser.FromNativeOrNull(
  69. cef_v8context_t.get_browser(_self)
  70. );
  71. }
  72. /// <summary>
  73. /// Returns the frame for this context. This method will return an empty
  74. /// reference for WebWorker contexts.
  75. /// </summary>
  76. public CefFrame GetFrame()
  77. {
  78. return CefFrame.FromNativeOrNull(
  79. cef_v8context_t.get_frame(_self)
  80. );
  81. }
  82. /// <summary>
  83. /// Returns the global object for this context. The context must be entered
  84. /// before calling this method.
  85. /// </summary>
  86. public CefV8Value GetGlobal()
  87. {
  88. return CefV8Value.FromNative(
  89. cef_v8context_t.get_global(_self)
  90. );
  91. }
  92. /// <summary>
  93. /// Enter this context. A context must be explicitly entered before creating a
  94. /// V8 Object, Array, Function or Date asynchronously. Exit() must be called
  95. /// the same number of times as Enter() before releasing this context. V8
  96. /// objects belong to the context in which they are created. Returns true if
  97. /// the scope was entered successfully.
  98. /// </summary>
  99. public bool Enter()
  100. {
  101. return cef_v8context_t.enter(_self) != 0;
  102. }
  103. /// <summary>
  104. /// Exit this context. Call this method only after calling Enter(). Returns
  105. /// true if the scope was exited successfully.
  106. /// </summary>
  107. public bool Exit()
  108. {
  109. return cef_v8context_t.exit(_self) != 0;
  110. }
  111. /// <summary>
  112. /// Returns true if this object is pointing to the same handle as |that|
  113. /// object.
  114. /// </summary>
  115. public bool IsSame(CefV8Context that)
  116. {
  117. if (that == null) return false;
  118. return cef_v8context_t.is_same(_self, that.ToNative()) != 0;
  119. }
  120. /// <summary>
  121. /// Execute a string of JavaScript code in this V8 context. The |script_url|
  122. /// parameter is the URL where the script in question can be found, if any.
  123. /// The |start_line| parameter is the base line number to use for error
  124. /// reporting. On success |retval| will be set to the return value, if any, and
  125. /// the function will return true. On failure |exception| will be set to the
  126. /// exception, if any, and the function will return false.
  127. /// </summary>
  128. public bool TryEval(string code, string scriptUrl, int startLine, out CefV8Value returnValue, out CefV8Exception exception)
  129. {
  130. bool result;
  131. cef_v8value_t* n_retval = null;
  132. cef_v8exception_t* n_exception = null;
  133. fixed (char* code_str = code)
  134. fixed (char* scriptUrl_str = scriptUrl)
  135. {
  136. var n_code = new cef_string_t(code_str, code != null ? code.Length : 0);
  137. var n_scriptUrl = new cef_string_t(scriptUrl_str, scriptUrl != null ? scriptUrl.Length : 0);
  138. result = cef_v8context_t.eval(_self, &n_code, &n_scriptUrl, startLine, &n_retval, &n_exception) != 0;
  139. }
  140. returnValue = n_retval != null ? CefV8Value.FromNative(n_retval) : null;
  141. exception = n_exception != null ? CefV8Exception.FromNative(n_exception) : null;
  142. return result;
  143. }
  144. }
  145. }