CefFrame.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 frame in the browser window. When used in the
  10. /// browser process the methods of this class may be called on any thread unless
  11. /// otherwise indicated in the comments. When used in the render process the
  12. /// methods of this class may only be called on the main thread.
  13. /// </summary>
  14. public sealed unsafe partial class CefFrame
  15. {
  16. /// <summary>
  17. /// True if this object is currently attached to a valid frame.
  18. /// </summary>
  19. public bool IsValid
  20. {
  21. get { return cef_frame_t.is_valid(_self) != 0; }
  22. }
  23. /// <summary>
  24. /// Execute undo in this frame.
  25. /// </summary>
  26. public void Undo()
  27. {
  28. cef_frame_t.undo(_self);
  29. }
  30. /// <summary>
  31. /// Execute redo in this frame.
  32. /// </summary>
  33. public void Redo()
  34. {
  35. cef_frame_t.redo(_self);
  36. }
  37. /// <summary>
  38. /// Execute cut in this frame.
  39. /// </summary>
  40. public void Cut()
  41. {
  42. cef_frame_t.cut(_self);
  43. }
  44. /// <summary>
  45. /// Execute copy in this frame.
  46. /// </summary>
  47. public void Copy()
  48. {
  49. cef_frame_t.copy(_self);
  50. }
  51. /// <summary>
  52. /// Execute paste in this frame.
  53. /// </summary>
  54. public void Paste()
  55. {
  56. cef_frame_t.paste(_self);
  57. }
  58. /// <summary>
  59. /// Execute delete in this frame.
  60. /// </summary>
  61. public void Delete()
  62. {
  63. cef_frame_t.del(_self);
  64. }
  65. /// <summary>
  66. /// Execute select all in this frame.
  67. /// </summary>
  68. public void SelectAll()
  69. {
  70. cef_frame_t.select_all(_self);
  71. }
  72. /// <summary>
  73. /// Save this frame's HTML source to a temporary file and open it in the
  74. /// default text viewing application. This method can only be called from the
  75. /// browser process.
  76. /// </summary>
  77. public void ViewSource()
  78. {
  79. cef_frame_t.view_source(_self);
  80. }
  81. /// <summary>
  82. /// Retrieve this frame's HTML source as a string sent to the specified
  83. /// visitor.
  84. /// </summary>
  85. public void GetSource(CefStringVisitor visitor)
  86. {
  87. if (visitor == null) throw new ArgumentNullException("visitor");
  88. cef_frame_t.get_source(_self, visitor.ToNative());
  89. }
  90. /// <summary>
  91. /// Retrieve this frame's display text as a string sent to the specified
  92. /// visitor.
  93. /// </summary>
  94. public void GetText(CefStringVisitor visitor)
  95. {
  96. if (visitor == null) throw new ArgumentNullException("visitor");
  97. cef_frame_t.get_text(_self, visitor.ToNative());
  98. }
  99. /// <summary>
  100. /// Load the request represented by the |request| object.
  101. /// WARNING: This method will fail with "bad IPC message" reason
  102. /// INVALID_INITIATOR_ORIGIN (213) unless you first navigate to the
  103. /// request origin using some other mechanism (LoadURL, link click, etc).
  104. /// </summary>
  105. public void LoadRequest(CefRequest request)
  106. {
  107. if (request == null) throw new ArgumentNullException("request");
  108. cef_frame_t.load_request(_self, request.ToNative());
  109. }
  110. /// <summary>
  111. /// Load the specified |url|.
  112. /// </summary>
  113. public void LoadUrl(string url)
  114. {
  115. fixed (char* url_str = url)
  116. {
  117. var n_url = new cef_string_t(url_str, url != null ? url.Length : 0);
  118. cef_frame_t.load_url(_self, &n_url);
  119. }
  120. }
  121. /// <summary>
  122. /// Execute a string of JavaScript code in this frame. The |script_url|
  123. /// parameter is the URL where the script in question can be found, if any.
  124. /// The renderer may request this URL to show the developer the source of the
  125. /// error. The |start_line| parameter is the base line number to use for error
  126. /// reporting.
  127. /// </summary>
  128. public void ExecuteJavaScript(string code, string url, int line)
  129. {
  130. fixed (char* code_str = code)
  131. fixed (char* url_str = url)
  132. {
  133. var n_code = new cef_string_t(code_str, code != null ? code.Length : 0);
  134. var n_url = new cef_string_t(url_str, url != null ? url.Length : 0);
  135. cef_frame_t.execute_java_script(_self, &n_code, &n_url, line);
  136. }
  137. }
  138. /// <summary>
  139. /// Returns true if this is the main (top-level) frame.
  140. /// </summary>
  141. public bool IsMain
  142. {
  143. get { return cef_frame_t.is_main(_self) != 0; }
  144. }
  145. /// <summary>
  146. /// Returns true if this is the focused frame.
  147. /// </summary>
  148. public bool IsFocused
  149. {
  150. get { return cef_frame_t.is_focused(_self) != 0; }
  151. }
  152. /// <summary>
  153. /// Returns the name for this frame. If the frame has an assigned name (for
  154. /// example, set via the iframe "name" attribute) then that value will be
  155. /// returned. Otherwise a unique name will be constructed based on the frame
  156. /// parent hierarchy. The main (top-level) frame will always have an empty name
  157. /// value.
  158. /// </summary>
  159. public string Name
  160. {
  161. get
  162. {
  163. var n_result = cef_frame_t.get_name(_self);
  164. return cef_string_userfree.ToString(n_result);
  165. }
  166. }
  167. /// <summary>
  168. /// Returns the globally unique identifier for this frame or &lt; 0 if the
  169. /// underlying frame does not yet exist.
  170. /// </summary>
  171. public long Identifier
  172. {
  173. get { return cef_frame_t.get_identifier(_self); }
  174. }
  175. /// <summary>
  176. /// Returns the parent of this frame or NULL if this is the main (top-level)
  177. /// frame.
  178. /// </summary>
  179. public CefFrame Parent
  180. {
  181. get
  182. {
  183. return CefFrame.FromNativeOrNull(
  184. cef_frame_t.get_parent(_self)
  185. );
  186. }
  187. }
  188. /// <summary>
  189. /// Returns the URL currently loaded in this frame.
  190. /// </summary>
  191. public string Url
  192. {
  193. get
  194. {
  195. var n_result = cef_frame_t.get_url(_self);
  196. return cef_string_userfree.ToString(n_result);
  197. }
  198. }
  199. /// <summary>
  200. /// Returns the browser that this frame belongs to.
  201. /// </summary>
  202. public CefBrowser Browser
  203. {
  204. get
  205. {
  206. return CefBrowser.FromNative(
  207. cef_frame_t.get_browser(_self)
  208. );
  209. }
  210. }
  211. /// <summary>
  212. /// Get the V8 context associated with the frame. This method can only be
  213. /// called from the render process.
  214. /// </summary>
  215. public CefV8Context V8Context
  216. {
  217. get
  218. {
  219. return CefV8Context.FromNative(
  220. cef_frame_t.get_v8context(_self)
  221. );
  222. }
  223. }
  224. /// <summary>
  225. /// Visit the DOM document. This method can only be called from the render
  226. /// process.
  227. /// </summary>
  228. public void VisitDom(CefDomVisitor visitor)
  229. {
  230. if (visitor == null) throw new ArgumentNullException("visitor");
  231. cef_frame_t.visit_dom(_self, visitor.ToNative());
  232. }
  233. /// <summary>
  234. /// Create a new URL request that will be treated as originating from this
  235. /// frame and the associated browser. This request may be intercepted by the
  236. /// client via CefResourceRequestHandler or CefSchemeHandlerFactory. Use
  237. /// CefURLRequest::Create instead if you do not want the request to have this
  238. /// association, in which case it may be handled differently (see documentation
  239. /// on that method). Requests may originate from both the browser process and
  240. /// the render process.
  241. /// For requests originating from the browser process:
  242. /// - POST data may only contain a single element of type PDE_TYPE_FILE or
  243. /// PDE_TYPE_BYTES.
  244. /// For requests originating from the render process:
  245. /// - POST data may only contain a single element of type PDE_TYPE_BYTES.
  246. /// - If the response contains Content-Disposition or Mime-Type header values
  247. /// that would not normally be rendered then the response may receive
  248. /// special handling inside the browser (for example, via the file download
  249. /// code path instead of the URL request code path).
  250. /// The |request| object will be marked as read-only after calling this method.
  251. /// </summary>
  252. public CefUrlRequest CreateUrlRequest(CefRequest request, CefUrlRequestClient client = null)
  253. {
  254. var n_request = request.ToNative();
  255. var n_client = client != null ? client.ToNative() : null;
  256. var n_result = cef_frame_t.create_urlrequest(_self, n_request, n_client);
  257. return CefUrlRequest.FromNativeOrNull(n_result);
  258. }
  259. /// <summary>
  260. /// Send a message to the specified |target_process|. Message delivery is not
  261. /// guaranteed in all cases (for example, if the browser is closing,
  262. /// navigating, or if the target process crashes). Send an ACK message back
  263. /// from the target process if confirmation is required.
  264. /// </summary>
  265. public void SendProcessMessage(CefProcessId targetProcess, CefProcessMessage message)
  266. {
  267. if (message == null) throw new ArgumentNullException("message");
  268. cef_frame_t.send_process_message(_self, targetProcess, message.ToNative());
  269. }
  270. }
  271. }