CefBrowser.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 used to represent a browser window. When used in the browser process
  10. /// the methods of this class may be called on any thread unless otherwise
  11. /// indicated in the comments. When used in the render process the methods of
  12. /// this class may only be called on the main thread.
  13. /// </summary>
  14. public sealed unsafe partial class CefBrowser
  15. {
  16. /// <summary>
  17. /// Returns the browser host object. This method can only be called in the
  18. /// browser process.
  19. /// </summary>
  20. public CefBrowserHost GetHost()
  21. {
  22. return CefBrowserHost.FromNative(
  23. cef_browser_t.get_host(_self)
  24. );
  25. }
  26. /// <summary>
  27. /// Returns true if the browser can navigate backwards.
  28. /// </summary>
  29. public bool CanGoBack
  30. {
  31. get { return cef_browser_t.can_go_back(_self) != 0; }
  32. }
  33. /// <summary>
  34. /// Navigate backwards.
  35. /// </summary>
  36. public void GoBack()
  37. {
  38. cef_browser_t.go_back(_self);
  39. }
  40. /// <summary>
  41. /// Returns true if the browser can navigate forwards.
  42. /// </summary>
  43. public bool CanGoForward
  44. {
  45. get { return cef_browser_t.can_go_forward(_self) != 0; }
  46. }
  47. /// <summary>
  48. /// Navigate forwards.
  49. /// </summary>
  50. public void GoForward()
  51. {
  52. cef_browser_t.go_forward(_self);
  53. }
  54. /// <summary>
  55. /// Returns true if the browser is currently loading.
  56. /// </summary>
  57. public bool IsLoading
  58. {
  59. get { return cef_browser_t.is_loading(_self) != 0; }
  60. }
  61. /// <summary>
  62. /// Reload the current page.
  63. /// </summary>
  64. public void Reload()
  65. {
  66. cef_browser_t.reload(_self);
  67. }
  68. /// <summary>
  69. /// Reload the current page ignoring any cached data.
  70. /// </summary>
  71. public void ReloadIgnoreCache()
  72. {
  73. cef_browser_t.reload_ignore_cache(_self);
  74. }
  75. /// <summary>
  76. /// Stop loading the page.
  77. /// </summary>
  78. public void StopLoad()
  79. {
  80. cef_browser_t.stop_load(_self);
  81. }
  82. /// <summary>
  83. /// Returns the globally unique identifier for this browser. This value is also
  84. /// used as the tabId for extension APIs.
  85. /// </summary>
  86. public int Identifier
  87. {
  88. get { return cef_browser_t.get_identifier(_self); }
  89. }
  90. /// <summary>
  91. /// Returns true if this object is pointing to the same handle as |that|
  92. /// object.
  93. /// </summary>
  94. public bool IsSame(CefBrowser that)
  95. {
  96. if (that == null) return false;
  97. return cef_browser_t.is_same(_self, that.ToNative()) != 0;
  98. }
  99. /// <summary>
  100. /// Returns true if the window is a popup window.
  101. /// </summary>
  102. public bool IsPopup
  103. {
  104. get { return cef_browser_t.is_popup(_self) != 0; }
  105. }
  106. /// <summary>
  107. /// Returns true if a document has been loaded in the browser.
  108. /// </summary>
  109. public bool HasDocument
  110. {
  111. get { return cef_browser_t.has_document(_self) != 0; }
  112. }
  113. /// <summary>
  114. /// Returns the main (top-level) frame for the browser window.
  115. /// </summary>
  116. public CefFrame GetMainFrame()
  117. {
  118. return CefFrame.FromNativeOrNull(
  119. cef_browser_t.get_main_frame(_self)
  120. );
  121. }
  122. /// <summary>
  123. /// Returns the focused frame for the browser window.
  124. /// </summary>
  125. public CefFrame GetFocusedFrame()
  126. {
  127. return CefFrame.FromNativeOrNull(
  128. cef_browser_t.get_focused_frame(_self)
  129. );
  130. }
  131. /// <summary>
  132. /// Returns the frame with the specified identifier, or NULL if not found.
  133. /// </summary>
  134. public CefFrame GetFrame(long identifier)
  135. {
  136. return CefFrame.FromNativeOrNull(
  137. cef_browser_t.get_frame_byident(_self, identifier)
  138. );
  139. }
  140. /// <summary>
  141. /// Returns the frame with the specified name, or NULL if not found.
  142. /// </summary>
  143. public CefFrame GetFrame(string name)
  144. {
  145. fixed (char* name_str = name)
  146. {
  147. var n_name = new cef_string_t(name_str, name.Length);
  148. return CefFrame.FromNativeOrNull(
  149. cef_browser_t.get_frame(_self, &n_name)
  150. );
  151. }
  152. }
  153. /// <summary>
  154. /// Returns the number of frames that currently exist.
  155. /// </summary>
  156. public int FrameCount
  157. {
  158. get { return (int)cef_browser_t.get_frame_count(_self); }
  159. }
  160. /// <summary>
  161. /// Returns the identifiers of all existing frames.
  162. /// </summary>
  163. public long[] GetFrameIdentifiers()
  164. {
  165. var frameCount = FrameCount;
  166. var identifiers = new long[frameCount * 2];
  167. UIntPtr n_count = (UIntPtr)frameCount;
  168. fixed (long* identifiers_ptr = identifiers)
  169. {
  170. cef_browser_t.get_frame_identifiers(_self, &n_count, identifiers_ptr);
  171. }
  172. if ((int)n_count < 0)
  173. {
  174. throw new InvalidOperationException("Invalid number of frames.");
  175. }
  176. if ((int)n_count > identifiers.Length)
  177. {
  178. throw new InvalidOperationException("Number of returned frames are too big.");
  179. }
  180. Array.Resize(ref identifiers, (int)n_count);
  181. return identifiers;
  182. }
  183. /// <summary>
  184. /// Returns the names of all existing frames.
  185. /// </summary>
  186. public string[] GetFrameNames()
  187. {
  188. var list = libcef.string_list_alloc();
  189. cef_browser_t.get_frame_names(_self, list);
  190. var result = cef_string_list.ToArray(list);
  191. libcef.string_list_free(list);
  192. return result;
  193. }
  194. }
  195. }