Skip to content

Commit

Permalink
Made it possible to register a "one-time use" ResourceHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Apr 7, 2017
1 parent 9bbe75f commit 563505c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions CefSharp/CefSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
<Compile Include="PdfPrintSettings.cs" />
<Compile Include="ICookieManager.cs" />
<Compile Include="IMenuModel.cs" />
<Compile Include="ResourceFactoryItem.cs" />
<Compile Include="Structs\ViewRect.cs" />
<Compile Include="ISslInfo.cs" />
<Compile Include="Structs\KeyEvent.cs" />
Expand Down
43 changes: 33 additions & 10 deletions CefSharp/DefaultResourceHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,57 @@ public class DefaultResourceHandlerFactory : IResourceHandlerFactory
/// <summary>
/// Resource handler thread safe dictionary
/// </summary>
public ConcurrentDictionary<string, IResourceHandler> Handlers { get; private set; }
public ConcurrentDictionary<string, ResourceFactoryItem> 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, IResourceHandler>(comparer ?? StringComparer.OrdinalIgnoreCase);
Handlers = new ConcurrentDictionary<string, ResourceFactoryItem>(comparer ?? StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Register handler with the specified Url
/// 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>
/// <returns>returns true if the Url was successfully parsed into a Uri otherwise false</returns>
public virtual bool RegisterHandler(string url, IResourceHandler handler)
public virtual bool RegisterHandler(string url, IResourceHandler handler, bool persist)
{
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
Handlers.AddOrUpdate(uri.AbsoluteUri, handler, (k, v) => handler);
ResourceFactoryItem entry = new ResourceFactoryItem(handler, persist);

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)
{
IResourceHandler handler;
return Handlers.TryRemove(url, out handler);
ResourceFactoryItem entry;
return Handlers.TryRemove(url, out entry);
}

/// <summary>
Expand All @@ -77,10 +91,19 @@ public virtual IResourceHandler GetResourceHandler(IWebBrowser browserControl, I
{
try
{
IResourceHandler handler;
Handlers.TryGetValue(request.Url, out handler);
ResourceFactoryItem entry;

if (Handlers.TryGetValue(request.Url, out entry))
{
if (!entry.Persist)
{
Handlers.TryRemove(request.Url, out entry);
}

return entry.Handler;
}

return handler;
return null;
}
finally
{
Expand Down
27 changes: 27 additions & 0 deletions CefSharp/ResourceFactoryItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

namespace CefSharp
{
public class ResourceFactoryItem
{
/// <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)
/// </summary>
public bool Persist { 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)
{
Handler = handler;
Persist = persist;
}
}
}

0 comments on commit 563505c

Please sign in to comment.