Skip to content

Commit

Permalink
Core - Add IRequestContext.GetCookieManagerAsync extension method
Browse files Browse the repository at this point in the history
- Will return the cookie manager when the backing store has loaded
  otherwise returns null
  • Loading branch information
amaitland committed Oct 8, 2021
1 parent b82fa05 commit 15cf58c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CefSharp.Test/Framework/RequestContextExtensionFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Moq;
using Xunit;
using Xunit.Abstractions;

namespace CefSharp.Test.Framework
{
//NOTE: All Test classes must be part of this collection as it manages the Cef Initialize/Shutdown lifecycle
[Collection(CefSharpFixtureCollection.Key)]
public class RequestContextExtensionFacts
{
private const string ProxyPreferenceKey = "proxy";
Expand Down Expand Up @@ -70,5 +73,20 @@ public void SetProxyThrowsExceptionOnInvalidScheme()
mockRequestContext.Object.SetProxy("myscheme", "localhost", 0, out msg);
});
}

[Fact]
public async Task CanGetCookieManagerForRequestContextAsync()
{
var requestContext = RequestContext
.Configure()
.Create();

var cookieManager = await requestContext.GetCookieManagerAsync();

var cookies = await cookieManager.VisitAllCookiesAsync();

Assert.NotNull(cookies);
output.WriteLine("Cookie Count {0}", cookies.Count);
}
}
}
23 changes: 23 additions & 0 deletions CefSharp/RequestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ public static void LoadExtensionsFromDirectory(this IRequestContext requestConte
}
}

/// <summary>
/// Gets the cookie manager associated with the <see cref="IRequestContext"/>. Once the cookie manager
/// storage has been initialized the method will return.
/// </summary>
/// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
/// <param name="requestContext">The <see cref="IRequestContext"/> instance this method extends.</param>
/// <returns>returns <see cref="ICookieManager"/> if the store was successfully loaded otherwise null. </returns>
public static async Task<ICookieManager> GetCookieManagerAsync(this IRequestContext requestContext)
{
if (requestContext == null)
{
throw new Exception("RequestContext is null, unable to obtain cookie manager");
}

var callback = new TaskCompletionCallback();

var cookieManager = requestContext.GetCookieManager(callback);

var success = await callback.Task;

return success ? cookieManager : null;
}

/// <summary>
/// Set the value associated with preference name. If value is null the
/// preference will be restored to its default value. If setting the preference
Expand Down

0 comments on commit 15cf58c

Please sign in to comment.