1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2024-11-23 17:42:46 +01:00
TweetDuck/linux/TweetImpl.CefGlue/Utils/SchemeHandlerFactory.cs
2022-02-19 18:19:13 +01:00

40 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using TweetImpl.CefGlue.Adapters;
using TweetLib.Browser.CEF.Logic;
using TweetLib.Browser.Interfaces;
using Xilium.CefGlue;
namespace TweetImpl.CefGlue.Utils {
public sealed class SchemeHandlerFactory {
private static readonly Dictionary<string, SchemeHandlerFactory> Factories = new ();
internal static CefResourceHandler? TryGetHandler(CefRequest request) {
if (Uri.TryCreate(request.Url, UriKind.Absolute, out Uri? uri) && Factories.TryGetValue(uri.Scheme, out var factory)) {
return factory.Create(request);
}
else {
return null;
}
}
[SuppressMessage("ReSharper", "BitwiseOperatorOnEnumWithoutFlags")]
public static void Register(CefSchemeRegistrar registrar, ICustomSchemeHandler handler) {
string protocol = handler.Protocol;
Factories.Add(protocol, new SchemeHandlerFactory(handler));
registrar.AddCustomScheme(protocol, CefSchemeOptions.Secure | CefSchemeOptions.CorsEnabled | CefSchemeOptions.CspBypassing);
}
private readonly SchemeHandlerFactoryLogic<CefRequest, CefResourceHandler> logic;
private SchemeHandlerFactory(ICustomSchemeHandler handler) {
this.logic = new SchemeHandlerFactoryLogic<CefRequest, CefResourceHandler>(handler, CefRequestAdapter.Instance, CefResourceHandlerFactory.Instance);
}
private CefResourceHandler? Create(CefRequest request) {
return logic.Create(request);
}
}
}