From 563505c3c6303b7a67d7c9b9dc727d0ea0210152 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 7 Apr 2017 14:18:05 +0200 Subject: [PATCH] Made it possible to register a "one-time use" ResourceHandler --- CefSharp/CefSharp.csproj | 1 + CefSharp/DefaultResourceHandlerFactory.cs | 43 +++++++++++++++++------ CefSharp/ResourceFactoryItem.cs | 27 ++++++++++++++ 3 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 CefSharp/ResourceFactoryItem.cs diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 508446a907..fa74a1ea9c 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -199,6 +199,7 @@ + diff --git a/CefSharp/DefaultResourceHandlerFactory.cs b/CefSharp/DefaultResourceHandlerFactory.cs index d8f998934b..4bec6d51ac 100644 --- a/CefSharp/DefaultResourceHandlerFactory.cs +++ b/CefSharp/DefaultResourceHandlerFactory.cs @@ -18,7 +18,7 @@ public class DefaultResourceHandlerFactory : IResourceHandlerFactory /// /// Resource handler thread safe dictionary /// - public ConcurrentDictionary Handlers { get; private set; } + public ConcurrentDictionary Handlers { get; private set; } /// /// Create a new instance of DefaultResourceHandlerFactory @@ -26,26 +26,40 @@ public class DefaultResourceHandlerFactory : IResourceHandlerFactory /// string equality comparer public DefaultResourceHandlerFactory(IEqualityComparer comparer = null) { - Handlers = new ConcurrentDictionary(comparer ?? StringComparer.OrdinalIgnoreCase); + Handlers = new ConcurrentDictionary(comparer ?? StringComparer.OrdinalIgnoreCase); } /// - /// Register handler with the specified Url + /// Register a handler for the specified Url /// /// url /// handler + /// Whether or not the handler should be used once (false) or until manually unregistered (true) /// returns true if the Url was successfully parsed into a Uri otherwise false - 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; } + /// + /// Register a persistant handler for the specified Url + /// + /// url + /// handler + /// returns true if the Url was successfully parsed into a Uri otherwise false + public virtual bool RegisterHandler(string url, IResourceHandler handler) + { + return RegisterHandler(url, handler, true); + } + /// /// Unregister a handler for the specified Url /// @@ -53,8 +67,8 @@ public virtual bool RegisterHandler(string url, IResourceHandler handler) /// returns true if successfully removed public virtual bool UnregisterHandler(string url) { - IResourceHandler handler; - return Handlers.TryRemove(url, out handler); + ResourceFactoryItem entry; + return Handlers.TryRemove(url, out entry); } /// @@ -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 { diff --git a/CefSharp/ResourceFactoryItem.cs b/CefSharp/ResourceFactoryItem.cs new file mode 100644 index 0000000000..b8f3f2b9db --- /dev/null +++ b/CefSharp/ResourceFactoryItem.cs @@ -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 + { + /// + /// The handler for a specific Url + /// + public IResourceHandler Handler { get; private set; } + + /// + /// Whether or not the handler should be used once (false) or until manually unregistered (true) + /// + public bool Persist { get; private set; } + + /// The handler for a specific Url + /// Whether or not the handler should be used once (false) or until manually unregistered (true) + public ResourceFactoryItem(IResourceHandler handler, bool persist) + { + Handler = handler; + Persist = persist; + } + } +}