CefRequest.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.Diagnostics;
  7. using System.Runtime.InteropServices;
  8. using Xilium.CefGlue.Interop;
  9. /// <summary>
  10. /// Class used to represent a web request. The methods of this class may be
  11. /// called on any thread.
  12. /// </summary>
  13. public sealed unsafe partial class CefRequest
  14. {
  15. /// <summary>
  16. /// Create a new CefRequest object.
  17. /// </summary>
  18. public static CefRequest Create()
  19. {
  20. return CefRequest.FromNative(
  21. cef_request_t.create()
  22. );
  23. }
  24. /// <summary>
  25. /// Returns true if this object is read-only.
  26. /// </summary>
  27. public bool IsReadOnly
  28. {
  29. get { return cef_request_t.is_read_only(_self) != 0; }
  30. }
  31. /// <summary>
  32. /// Gets or sets the fully qualified URL.
  33. /// </summary>
  34. public string Url
  35. {
  36. get
  37. {
  38. var n_result = cef_request_t.get_url(_self);
  39. return cef_string_userfree.ToString(n_result);
  40. }
  41. set
  42. {
  43. if (value == null) throw new ArgumentNullException("value");
  44. fixed (char* value_str = value)
  45. {
  46. var n_value = new cef_string_t(value_str, value.Length);
  47. cef_request_t.set_url(_self, &n_value);
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// Gets or sets the request method type.
  53. /// The value will default to POST if post data is provided and GET otherwise.
  54. /// </summary>
  55. public string Method
  56. {
  57. get
  58. {
  59. var n_result = cef_request_t.get_method(_self);
  60. return cef_string_userfree.ToString(n_result);
  61. }
  62. set
  63. {
  64. fixed (char* value_str = value)
  65. {
  66. var n_value = new cef_string_t(value_str, value != null ? value.Length : 0);
  67. cef_request_t.set_method(_self, &n_value);
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Set the referrer URL and policy. If non-empty the referrer URL must be
  73. /// fully qualified with an HTTP or HTTPS scheme component. Any username,
  74. /// password or ref component will be removed.
  75. /// </summary>
  76. public void SetReferrer(string referrerUrl, CefReferrerPolicy policy)
  77. {
  78. fixed (char* referrerUrl_str = referrerUrl)
  79. {
  80. var n_referrerUrl = new cef_string_t(referrerUrl_str, referrerUrl != null ? referrerUrl.Length : 0);
  81. cef_request_t.set_referrer(_self, &n_referrerUrl, policy);
  82. }
  83. }
  84. /// <summary>
  85. /// Get the referrer URL.
  86. /// </summary>
  87. public string ReferrerURL
  88. {
  89. get
  90. {
  91. var n_result = cef_request_t.get_referrer_url(_self);
  92. return cef_string_userfree.ToString(n_result);
  93. }
  94. }
  95. /// <summary>
  96. /// Get the referrer policy.
  97. /// </summary>
  98. public CefReferrerPolicy ReferrerPolicy
  99. {
  100. get
  101. {
  102. return cef_request_t.get_referrer_policy(_self);
  103. }
  104. }
  105. /// <summary>
  106. /// Get the post data.
  107. /// </summary>
  108. public CefPostData PostData
  109. {
  110. get
  111. {
  112. return CefPostData.FromNativeOrNull(
  113. cef_request_t.get_post_data(_self)
  114. );
  115. }
  116. set
  117. {
  118. var n_value = value != null ? value.ToNative() : null;
  119. cef_request_t.set_post_data(_self, n_value);
  120. }
  121. }
  122. /// <summary>
  123. /// Get the header values. Will not include the Referer value if any.
  124. /// </summary>
  125. public NameValueCollection GetHeaderMap()
  126. {
  127. var headerMap = libcef.string_multimap_alloc();
  128. cef_request_t.get_header_map(_self, headerMap);
  129. var result = cef_string_multimap.ToNameValueCollection(headerMap);
  130. libcef.string_multimap_free(headerMap);
  131. return result;
  132. }
  133. /// <summary>
  134. /// Set the header values. If a Referer value exists in the header map it will
  135. /// be removed and ignored.
  136. /// </summary>
  137. public void SetHeaderMap(NameValueCollection headers)
  138. {
  139. var headerMap = cef_string_multimap.From(headers);
  140. cef_request_t.set_header_map(_self, headerMap);
  141. libcef.string_multimap_free(headerMap);
  142. }
  143. /// <summary>
  144. /// Returns the first header value for |name| or an empty string if not found.
  145. /// Will not return the Referer value if any. Use GetHeaderMap instead if
  146. /// |name| might have multiple values.
  147. /// </summary>
  148. public string GetHeaderByName(string name)
  149. {
  150. fixed (char* name_str = name)
  151. {
  152. var n_name = new cef_string_t(name_str, name != null ? name.Length : 0);
  153. var n_result = cef_request_t.get_header_by_name(_self, &n_name);
  154. return cef_string_userfree.ToString(n_result);
  155. }
  156. }
  157. /// <summary>
  158. /// Set the header |name| to |value|. If |overwrite| is true any existing
  159. /// values will be replaced with the new value. If |overwrite| is false any
  160. /// existing values will not be overwritten. The Referer value cannot be set
  161. /// using this method.
  162. /// </summary>
  163. public void SetHeaderByName(string name, string value, bool overwrite)
  164. {
  165. fixed (char* name_str = name)
  166. fixed (char* value_str = value)
  167. {
  168. var n_name = new cef_string_t(name_str, name != null ? name.Length : 0);
  169. var n_value = new cef_string_t(value_str, value != null ? value.Length : 0);
  170. cef_request_t.set_header_by_name(_self, &n_name, &n_value, overwrite ? 1 : 0);
  171. }
  172. }
  173. /// <summary>
  174. /// Set all values at one time.
  175. /// </summary>
  176. public void Set(string url, string method, CefPostData postData, NameValueCollection headers)
  177. {
  178. fixed (char* url_str = url)
  179. fixed (char* method_str = method)
  180. {
  181. var n_url = new cef_string_t(url_str, url != null ? url.Length : 0);
  182. var n_method = new cef_string_t(method_str, method_str != null ? method.Length : 0);
  183. var n_postData = postData != null ? postData.ToNative() : null;
  184. var n_headerMap = cef_string_multimap.From(headers);
  185. cef_request_t.set(_self, &n_url, &n_method, n_postData, n_headerMap);
  186. libcef.string_multimap_free(n_headerMap);
  187. }
  188. }
  189. /// <summary>
  190. /// Get the options used in combination with CefUrlRequest.
  191. /// </summary>
  192. public CefUrlRequestOptions Options
  193. {
  194. get { return (CefUrlRequestOptions)cef_request_t.get_flags(_self); }
  195. set { cef_request_t.set_flags(_self, (int)value); }
  196. }
  197. /// <summary>
  198. /// Gets or sets the URL to the first party for cookies used in combination with
  199. /// CefURLRequest.
  200. /// </summary>
  201. public string FirstPartyForCookies
  202. {
  203. get
  204. {
  205. var n_result = cef_request_t.get_first_party_for_cookies(_self);
  206. return cef_string_userfree.ToString(n_result);
  207. }
  208. set
  209. {
  210. fixed (char* value_str = value)
  211. {
  212. var n_value = new cef_string_t(value_str, value != null ? value.Length : 0);
  213. cef_request_t.set_first_party_for_cookies(_self, &n_value);
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// Get the resource type for this request. Only available in the browser
  219. /// process.
  220. /// </summary>
  221. public CefResourceType ResourceType
  222. {
  223. get
  224. {
  225. return cef_request_t.get_resource_type(_self);
  226. }
  227. }
  228. /// <summary>
  229. /// Get the transition type for this request. Only available in the browser
  230. /// process and only applies to requests that represent a main frame or
  231. /// sub-frame navigation.
  232. /// </summary>
  233. public CefTransitionType TransitionType
  234. {
  235. get
  236. {
  237. return cef_request_t.get_transition_type(_self);
  238. }
  239. }
  240. /// <summary>
  241. /// Returns the globally unique identifier for this request or 0 if not
  242. /// specified. Can be used by CefResourceRequestHandler implementations in the
  243. /// browser process to track a single request across multiple callbacks.
  244. /// </summary>
  245. public ulong Identifier
  246. {
  247. get
  248. {
  249. return cef_request_t.get_identifier(_self);
  250. }
  251. }
  252. }
  253. }