ICookieManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2024 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System.Threading.Tasks;
  15. namespace Vuplex.WebView {
  16. /// <summary>
  17. /// Provides methods for getting, setting, and deleting HTTP cookies.
  18. /// You can access the ICookieManager via Web.CookieManager.
  19. /// </summary>
  20. /// <remarks>
  21. /// When developing code that interacts with
  22. /// cookies, it may also be helpful to view a webview's cookies using
  23. /// [remote debugging](https://support.vuplex.com/articles/how-to-debug-web-content).
  24. /// </remarks>
  25. public interface ICookieManager {
  26. /// <summary>
  27. /// Deletes all of the cookies that match the given URL and returns a
  28. /// Task&lt;bool&gt; indicating whether the deletion succeeded. A `cookieName`
  29. /// can be optionally passed as a second parameter to further filter
  30. /// to a specific cookie.
  31. /// If a deletion fails, it could be because the URL was invalid.
  32. /// For more details regarding a failure, check the Unity logs.
  33. /// </summary>
  34. /// <remarks>
  35. /// Important notes:
  36. /// <list type="bullet">
  37. /// <item>On Windows and macOS, if this method is called without a `cookieName` it only deletes cookies that were set without an explicit Domain attribute.</item>
  38. /// <item>On versions of iOS older than iOS 11, session cookies are excluded because WKHTTPCookieStore is only supported in iOS 11 and newer.</item>
  39. /// </list>
  40. /// </remarks>
  41. /// <example>
  42. /// <code>
  43. /// if (Web.CookieManager == null) {
  44. /// Debug.Log("Web.CookieManager isn't supported on this platform.");
  45. /// return;
  46. /// }
  47. /// // Delete all the cookies for this cookie test page, which will reset the test.
  48. /// var succeeded = await Web.CookieManager.DeleteCookies("http://www.whatarecookies.com/cookietest.asp");
  49. /// Debug.Log("Cookie deletion succeeded: " + succeeded);
  50. /// </code>
  51. /// </example>
  52. Task<bool> DeleteCookies(string url, string cookieName = null);
  53. /// <summary>
  54. /// Gets all of the cookies that match the given URL. A `cookieName`
  55. /// can be optionally passed as a second parameter to further filter
  56. /// results to a specific cookie.
  57. /// </summary>
  58. /// <remarks>
  59. /// Important notes:
  60. /// <list type="bullet">
  61. /// <item>On Android, the cookies returned only have their Name and Value fields set. The other fields (e.g. Domain, Path) are set to their default values because Android doesn't provide a way to access those values.</item>
  62. /// <item>On versions of iOS older than iOS 11, session cookies are excluded because WKHTTPCookieStore is only supported in iOS 11 and newer.</item>
  63. /// </list>
  64. /// </remarks>
  65. /// <example>
  66. /// <code>
  67. /// if (Web.CookieManager == null) {
  68. /// Debug.Log("Web.CookieManager isn't supported on this platform.");
  69. /// return;
  70. /// }
  71. /// // Get the cookie named "NID" set by google.com.
  72. /// var cookies = await Web.CookieManager.GetCookies("https://www.google.com", "NID");
  73. /// if (cookies.Length > 0) {
  74. /// Debug.Log("Cookie: " + cookies[0]);
  75. /// } else {
  76. /// Debug.Log("Cookie not found.");
  77. /// }
  78. /// </code>
  79. /// </example>
  80. Task<Cookie[]> GetCookies(string url, string cookieName = null);
  81. /// <summary>
  82. /// Sets the given cookie and returns a Task&lt;bool&gt; indicating
  83. /// whether the cookie was set successfully.
  84. /// If setting the cookie fails, it could be because the data in the provided Cookie
  85. /// was malformed. For more details regarding a failure, please check the Unity logs.
  86. /// </summary>
  87. /// <example>
  88. /// <code>
  89. /// if (Web.CookieManager == null) {
  90. /// Debug.Log("Web.CookieManager isn't supported on this platform.");
  91. /// return;
  92. /// }
  93. /// var success = await Web.CookieManager.SetCookie(new Cookie {
  94. /// Domain = "vuplex.com",
  95. /// Path = "/",
  96. /// Name = "example_name",
  97. /// Value = "example_value",
  98. /// Secure = true,
  99. /// // Expire one day from now
  100. /// ExpirationDate = (int)DateTimeOffset.Now.ToUnixTimeSeconds() + 60 * 60 * 24
  101. /// });
  102. /// </code>
  103. /// </example>
  104. Task<bool> SetCookie(Cookie cookie);
  105. }
  106. }