CefCookieManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 for managing cookies. The methods of this class may be called on
  10. /// any thread unless otherwise indicated.
  11. /// </summary>
  12. public sealed unsafe partial class CefCookieManager
  13. {
  14. /// <summary>
  15. /// Returns the global cookie manager. By default data will be stored at
  16. /// CefSettings.cache_path if specified or in memory otherwise. If |callback|
  17. /// is non-NULL it will be executed asnychronously on the UI thread after the
  18. /// manager's storage has been initialized. Using this method is equivalent to
  19. /// calling CefRequestContext::GetGlobalContext()->GetDefaultCookieManager().
  20. /// </summary>
  21. public static CefCookieManager GetGlobal(CefCompletionCallback callback)
  22. {
  23. var n_callback = callback != null ? callback.ToNative() : null;
  24. return CefCookieManager.FromNativeOrNull(
  25. cef_cookie_manager_t.get_global_manager(n_callback)
  26. );
  27. }
  28. /// <summary>
  29. /// Visit all cookies on the UI thread. The returned cookies are ordered by
  30. /// longest path, then by earliest creation date. Returns false if cookies
  31. /// cannot be accessed.
  32. /// </summary>
  33. public bool VisitAllCookies(CefCookieVisitor visitor)
  34. {
  35. if (visitor == null) throw new ArgumentNullException("visitor");
  36. return cef_cookie_manager_t.visit_all_cookies(_self, visitor.ToNative()) != 0;
  37. }
  38. /// <summary>
  39. /// Visit a subset of cookies on the UI thread. The results are filtered by the
  40. /// given url scheme, host, domain and path. If |includeHttpOnly| is true
  41. /// HTTP-only cookies will also be included in the results. The returned
  42. /// cookies are ordered by longest path, then by earliest creation date.
  43. /// Returns false if cookies cannot be accessed.
  44. /// </summary>
  45. public bool VisitUrlCookies(string url, bool includeHttpOnly, CefCookieVisitor visitor)
  46. {
  47. if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
  48. if (visitor == null) throw new ArgumentNullException("visitor");
  49. fixed (char* url_str = url)
  50. {
  51. var n_url = new cef_string_t(url_str, url.Length);
  52. return cef_cookie_manager_t.visit_url_cookies(_self, &n_url, includeHttpOnly ? 1 : 0, visitor.ToNative()) != 0;
  53. }
  54. }
  55. /// <summary>
  56. /// Sets a cookie given a valid URL and explicit user-provided cookie
  57. /// attributes. This function expects each attribute to be well-formed. It will
  58. /// check for disallowed characters (e.g. the ';' character is disallowed
  59. /// within the cookie value attribute) and fail without setting the cookie if
  60. /// such characters are found. If |callback| is non-NULL it will be executed
  61. /// asnychronously on the UI thread after the cookie has been set. Returns
  62. /// false if an invalid URL is specified or if cookies cannot be accessed.
  63. /// </summary>
  64. public bool SetCookie(string url, CefCookie cookie, CefSetCookieCallback callback)
  65. {
  66. if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
  67. if (cookie == null) throw new ArgumentNullException("cookie");
  68. int n_result;
  69. var n_cookie = cookie.ToNative();
  70. fixed (char* url_str = url)
  71. {
  72. var n_url = new cef_string_t(url_str, url.Length);
  73. var n_callback = callback != null ? callback.ToNative() : null;
  74. n_result = cef_cookie_manager_t.set_cookie(_self, &n_url, n_cookie, n_callback);
  75. }
  76. CefCookie.Free(n_cookie);
  77. return n_result != 0;
  78. }
  79. /// <summary>
  80. /// Delete all cookies that match the specified parameters. If both |url| and
  81. /// |cookie_name| values are specified all host and domain cookies matching
  82. /// both will be deleted. If only |url| is specified all host cookies (but not
  83. /// domain cookies) irrespective of path will be deleted. If |url| is empty all
  84. /// cookies for all hosts and domains will be deleted. If |callback| is
  85. /// non-NULL it will be executed asnychronously on the UI thread after the
  86. /// cookies have been deleted. Returns false if a non-empty invalid URL is
  87. /// specified or if cookies cannot be accessed. Cookies can alternately be
  88. /// deleted using the Visit*Cookies() methods.
  89. /// </summary>
  90. public bool DeleteCookies(string url, string cookieName, CefDeleteCookiesCallback callback)
  91. {
  92. fixed (char* url_str = url)
  93. fixed (char* cookieName_str = cookieName)
  94. {
  95. var n_url = new cef_string_t(url_str, url != null ? url.Length : 0);
  96. var n_cookieName = new cef_string_t(cookieName_str, cookieName != null ? cookieName.Length : 0);
  97. var n_callback = callback != null ? callback.ToNative() : null;
  98. return cef_cookie_manager_t.delete_cookies(_self, &n_url, &n_cookieName, n_callback) != 0;
  99. }
  100. }
  101. /// <summary>
  102. /// Flush the backing store (if any) to disk. If |callback| is non-NULL it will
  103. /// be executed asnychronously on the UI thread after the flush is complete.
  104. /// Returns false if cookies cannot be accessed.
  105. /// </summary>
  106. public bool FlushStore(CefCompletionCallback callback)
  107. {
  108. var n_handler = callback != null ? callback.ToNative() : null;
  109. return cef_cookie_manager_t.flush_store(_self, n_handler) != 0;
  110. }
  111. }
  112. }