Skip to content

Commit

Permalink
Follow up to cefsharp#2010
Browse files Browse the repository at this point in the history
ResourceFactoryItem renamed to DefaultResourceHandlerFactory - it's only used in the context of that class
Change persist to OneTimeUse and invert logic (false is the default)
Remove unnecessary RegisterHandler overload - user default value instead
  • Loading branch information
amaitland committed Apr 10, 2017
1 parent 955867d commit a683ead
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CefSharp/CefSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
<Compile Include="PdfPrintSettings.cs" />
<Compile Include="ICookieManager.cs" />
<Compile Include="IMenuModel.cs" />
<Compile Include="ResourceFactoryItem.cs" />
<Compile Include="DefaultResourceHandlerFactoryItem.cs" />
<Compile Include="Structs\ViewRect.cs" />
<Compile Include="ISslInfo.cs" />
<Compile Include="Structs\KeyEvent.cs" />
Expand Down
27 changes: 8 additions & 19 deletions CefSharp/DefaultResourceHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,45 @@ public class DefaultResourceHandlerFactory : IResourceHandlerFactory
/// <summary>
/// Resource handler thread safe dictionary
/// </summary>
public ConcurrentDictionary<string, ResourceFactoryItem> Handlers { get; private set; }
public ConcurrentDictionary<string, DefaultResourceHandlerFactoryItem> Handlers { get; private set; }

/// <summary>
/// Create a new instance of DefaultResourceHandlerFactory
/// </summary>
/// <param name="comparer">string equality comparer</param>
public DefaultResourceHandlerFactory(IEqualityComparer<string> comparer = null)
{
Handlers = new ConcurrentDictionary<string, ResourceFactoryItem>(comparer ?? StringComparer.OrdinalIgnoreCase);
Handlers = new ConcurrentDictionary<string, DefaultResourceHandlerFactoryItem>(comparer ?? StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Register a handler for the specified Url
/// </summary>
/// <param name="url">url</param>
/// <param name="handler">handler</param>
/// <param name="persist">Whether or not the handler should be used once (false) or until manually unregistered (true)</param>
/// <param name="oneTimeUse">Whether or not the handler should be used once (true) or until manually unregistered (false)</param>
/// <returns>returns true if the Url was successfully parsed into a Uri otherwise false</returns>
public virtual bool RegisterHandler(string url, IResourceHandler handler, bool persist)
public virtual bool RegisterHandler(string url, IResourceHandler handler, bool oneTimeUse = false)
{
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
ResourceFactoryItem entry = new ResourceFactoryItem(handler, persist);
var entry = new DefaultResourceHandlerFactoryItem(handler, oneTimeUse);

Handlers.AddOrUpdate(uri.AbsoluteUri, entry, (k, v) => entry);
return true;
}
return false;
}

/// <summary>
/// Register a persistant handler for the specified Url
/// </summary>
/// <param name="url">url</param>
/// <param name="handler">handler</param>
/// <returns>returns true if the Url was successfully parsed into a Uri otherwise false</returns>
public virtual bool RegisterHandler(string url, IResourceHandler handler)
{
return RegisterHandler(url, handler, true);
}

/// <summary>
/// Unregister a handler for the specified Url
/// </summary>
/// <param name="url">Url</param>
/// <returns>returns true if successfully removed</returns>
public virtual bool UnregisterHandler(string url)
{
ResourceFactoryItem entry;
DefaultResourceHandlerFactoryItem entry;
return Handlers.TryRemove(url, out entry);
}

Expand All @@ -91,11 +80,11 @@ public virtual IResourceHandler GetResourceHandler(IWebBrowser browserControl, I
{
try
{
ResourceFactoryItem entry;
DefaultResourceHandlerFactoryItem entry;

if (Handlers.TryGetValue(request.Url, out entry))
{
if (!entry.Persist)
if (entry.OneTimeUse)
{
Handlers.TryRemove(request.Url, out entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

namespace CefSharp
{
public class ResourceFactoryItem
public class DefaultResourceHandlerFactoryItem
{
/// <summary>
/// The handler for a specific Url
/// </summary>
public IResourceHandler Handler { get; private set; }

/// <summary>
/// Whether or not the handler should be used once (false) or until manually unregistered (true)
/// Whether or not the handler should be used once (true) or until manually unregistered (false)
/// </summary>
public bool Persist { get; private set; }
public bool OneTimeUse { get; private set; }

/// <param name="handler">The handler for a specific Url</param>
/// <param name="persist">Whether or not the handler should be used once (false) or until manually unregistered (true)</param>
public ResourceFactoryItem(IResourceHandler handler, bool persist)
/// <param name="oneTimeUse">Whether or not the handler should be used once (true) or until manually unregistered (false)</param>
public DefaultResourceHandlerFactoryItem(IResourceHandler handler, bool oneTimeUse)
{
Handler = handler;
Persist = persist;
OneTimeUse = oneTimeUse;
}
}
}
5 changes: 3 additions & 2 deletions CefSharp/WebBrowserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,9 @@ public static void LoadHtml(this IWebBrowser browser, string html, bool base64En
/// <param name="html">The HTML content.</param>
/// <param name="url">The URL that will be treated as the address of the content.</param>
/// <param name="encoding">Character Encoding</param>
/// <param name="oneTimeUse">Whether or not the handler should be used once (true) or until manually unregistered (false)</param>
/// <returns>returns false if the Url was not successfully parsed into a Uri</returns>
public static bool LoadHtml(this IWebBrowser browser, string html, string url, Encoding encoding)
public static bool LoadHtml(this IWebBrowser browser, string html, string url, Encoding encoding, bool oneTimeUse = false)
{
var handler = browser.ResourceHandlerFactory;
if (handler == null)
Expand All @@ -309,7 +310,7 @@ public static bool LoadHtml(this IWebBrowser browser, string html, string url, E
throw new Exception("LoadHtml can only be used with the default IResourceHandlerFactory(DefaultResourceHandlerFactory) implementation");
}

if (resourceHandler.RegisterHandler(url, ResourceHandler.FromString(html, encoding, true), false))
if (resourceHandler.RegisterHandler(url, ResourceHandler.FromString(html, encoding, true), oneTimeUse))
{
browser.Load(url);
return true;
Expand Down

0 comments on commit a683ead

Please sign in to comment.