diff --git a/Browser/Data/ResourceHandlers.cs b/Browser/Data/ResourceHandlers.cs index 95abe936..5667484d 100644 --- a/Browser/Data/ResourceHandlers.cs +++ b/Browser/Data/ResourceHandlers.cs @@ -4,19 +4,19 @@ namespace TweetDuck.Browser.Data{ sealed class ResourceHandlers{ - private readonly ConcurrentDictionary<string, IResourceHandler> handlers = new ConcurrentDictionary<string, IResourceHandler>(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary<string, Func<IResourceHandler>> handlers = new ConcurrentDictionary<string, Func<IResourceHandler>>(StringComparer.OrdinalIgnoreCase); public bool HasHandler(IRequest request){ return handlers.ContainsKey(request.Url); } public IResourceHandler GetHandler(IRequest request){ - return handlers.TryGetValue(request.Url, out var handler) ? handler : null; + return handlers.TryGetValue(request.Url, out var factory) ? factory() : null; } - public bool Register(string url, IResourceHandler handler){ + public bool Register(string url, Func<IResourceHandler> factory){ if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri)){ - handlers.AddOrUpdate(uri.AbsoluteUri, handler, (key, prev) => handler); + handlers.AddOrUpdate(uri.AbsoluteUri, factory, (key, prev) => factory); return true; } @@ -24,15 +24,27 @@ public bool Register(string url, IResourceHandler handler){ } public bool Register(ResourceLink link){ - return Register(link.Url, link.Handler); + return Register(link.Url, link.Factory); } public bool Unregister(string url){ - return handlers.TryRemove(url, out IResourceHandler _); + return handlers.TryRemove(url, out _); } public bool Unregister(ResourceLink link){ return Unregister(link.Url); } + + public static Func<IResourceHandler> ForString(string str){ + return () => ResourceHandler.FromString(str); + } + + public static Func<IResourceHandler> ForString(string str, string mimeType){ + return () => ResourceHandler.FromString(str, mimeType: mimeType); + } + + public static Func<IResourceHandler> ForBytes(byte[] bytes, string mimeType){ + return () => ResourceHandler.FromByteArray(bytes, mimeType); + } } } diff --git a/Browser/Data/ResourceLink.cs b/Browser/Data/ResourceLink.cs index b133bbd6..a960bc8d 100644 --- a/Browser/Data/ResourceLink.cs +++ b/Browser/Data/ResourceLink.cs @@ -1,13 +1,14 @@ -using CefSharp; +using System; +using CefSharp; namespace TweetDuck.Browser.Data{ sealed class ResourceLink{ public string Url { get; } - public IResourceHandler Handler { get; } + public Func<IResourceHandler> Factory { get; } - public ResourceLink(string url, IResourceHandler handler){ + public ResourceLink(string url, Func<IResourceHandler> factory){ this.Url = url; - this.Handler = handler; + this.Factory = factory; } } } diff --git a/Browser/Notification/FormNotificationBase.cs b/Browser/Notification/FormNotificationBase.cs index f0446e58..24ec9485 100644 --- a/Browser/Notification/FormNotificationBase.cs +++ b/Browser/Notification/FormNotificationBase.cs @@ -1,6 +1,5 @@ using System.Drawing; using System.Windows.Forms; -using CefSharp; using CefSharp.WinForms; using TweetDuck.Browser.Data; using TweetDuck.Browser.Handling; @@ -14,7 +13,7 @@ namespace TweetDuck.Browser.Notification{ abstract partial class FormNotificationBase : Form, AnalyticsFile.IProvider{ - public static readonly ResourceLink AppLogo = new ResourceLink("https://ton.twimg.com/tduck/avatar", ResourceHandler.FromByteArray(Properties.Resources.avatar, "image/png")); + public static readonly ResourceLink AppLogo = new ResourceLink("https://ton.twimg.com/tduck/avatar", ResourceHandlers.ForBytes(Properties.Resources.avatar, "image/png")); protected const string BlankURL = TwitterUrls.TweetDeck + "/?blank"; @@ -135,8 +134,8 @@ protected FormNotificationBase(FormBrowser owner, bool enableContextMenu){ var resourceRequestHandler = new ResourceRequestHandlerBase(); var resourceHandlers = resourceRequestHandler.ResourceHandlers; - resourceHandlers.Register(BlankURL, ResourceHandler.FromString(string.Empty)); - resourceHandlers.Register(TwitterUrls.TweetDeck, this.resourceHandler); + resourceHandlers.Register(BlankURL, ResourceHandlers.ForString(string.Empty)); + resourceHandlers.Register(TwitterUrls.TweetDeck, () => this.resourceHandler); resourceHandlers.Register(AppLogo); this.browser = new ChromiumWebBrowser(BlankURL){ diff --git a/Browser/Notification/SoundNotification.cs b/Browser/Notification/SoundNotification.cs index 93f9e92d..6f6a3b12 100644 --- a/Browser/Notification/SoundNotification.cs +++ b/Browser/Notification/SoundNotification.cs @@ -1,7 +1,9 @@ -using System.Drawing; +using System; +using System.Drawing; using System.IO; using System.Windows.Forms; using CefSharp; +using TweetDuck.Browser.Data; using TweetDuck.Controls; using TweetDuck.Dialogs; using TweetDuck.Dialogs.Settings; @@ -11,7 +13,7 @@ namespace TweetDuck.Browser.Notification{ static class SoundNotification{ public const string SupportedFormats = "*.wav;*.ogg;*.mp3;*.flac;*.opus;*.weba;*.webm"; - public static IResourceHandler CreateFileHandler(string path){ + public static Func<IResourceHandler> CreateFileHandler(string path){ string mimeType = Path.GetExtension(path) switch{ ".weba" => "audio/webm", ".webm" => "audio/webm", @@ -24,7 +26,7 @@ public static IResourceHandler CreateFileHandler(string path){ }; try{ - return ResourceHandler.FromFilePath(path, mimeType); + return ResourceHandlers.ForBytes(File.ReadAllBytes(path), mimeType); }catch{ FormBrowser browser = FormManager.TryFind<FormBrowser>(); diff --git a/Browser/TweetDeckBrowser.cs b/Browser/TweetDeckBrowser.cs index 5568cc11..96457d33 100644 --- a/Browser/TweetDeckBrowser.cs +++ b/Browser/TweetDeckBrowser.cs @@ -137,7 +137,7 @@ private void browser_FrameLoadStart(object sender, FrameLoadStartEventArgs e){ if (TwitterUrls.IsTwitter(url)){ string css = Program.Resources.Load("styles/twitter.css"); - resourceHandlers.Register(TwitterStyleUrl, ResourceHandler.FromString(css, mimeType: "text/css")); + resourceHandlers.Register(TwitterStyleUrl, ResourceHandlers.ForString(css, "text/css")); CefScriptExecutor.RunFile(frame, "twitter.js"); } @@ -192,7 +192,7 @@ private void browser_LoadError(object sender, LoadErrorEventArgs e){ string errorName = Enum.GetName(typeof(CefErrorCode), e.ErrorCode); string errorTitle = StringUtils.ConvertPascalCaseToScreamingSnakeCase(errorName ?? string.Empty); - resourceHandlers.Register(ErrorUrl, ResourceHandler.FromString(errorPage.Replace("{err}", errorTitle))); + resourceHandlers.Register(ErrorUrl, ResourceHandlers.ForString(errorPage.Replace("{err}", errorTitle))); browser.Load(ErrorUrl); } } diff --git a/Dialogs/FormGuide.cs b/Dialogs/FormGuide.cs index da0775b5..b33ca7f1 100644 --- a/Dialogs/FormGuide.cs +++ b/Dialogs/FormGuide.cs @@ -17,7 +17,7 @@ sealed partial class FormGuide : Form, FormManager.IAppDialog{ private const string GuideUrl = "https://tweetduck.chylex.com/guide/v2/"; private const string GuidePathRegex = @"^guide(?:/v\d+)?(?:/(#.*))?"; - private static readonly ResourceLink DummyPage = new ResourceLink("http://td/dummy", ResourceHandler.FromString("")); + private static readonly ResourceLink DummyPage = new ResourceLink("http://td/dummy", ResourceHandlers.ForString(string.Empty)); public static bool CheckGuideUrl(string url, out string hash){ if (!url.Contains("//tweetduck.chylex.com/guide")){ diff --git a/Utils/TwitterUtils.cs b/Utils/TwitterUtils.cs index 1316df23..bf4aa18f 100644 --- a/Utils/TwitterUtils.cs +++ b/Utils/TwitterUtils.cs @@ -19,7 +19,7 @@ static class TwitterUtils{ public static readonly Color BackgroundColor = Color.FromArgb(28, 99, 153); public const string BackgroundColorOverride = "setTimeout(function f(){let h=document.head;if(!h){setTimeout(f,5);return;}let e=document.createElement('style');e.innerHTML='body,body::before{background:#1c6399!important;margin:0}';h.appendChild(e);},1)"; - public static readonly ResourceLink LoadingSpinner = new ResourceLink("https://ton.twimg.com/tduck/spinner", ResourceHandler.FromByteArray(Properties.Resources.spinner, "image/apng")); + public static readonly ResourceLink LoadingSpinner = new ResourceLink("https://ton.twimg.com/tduck/spinner", ResourceHandlers.ForBytes(Properties.Resources.spinner, "image/apng")); public static readonly string[] DictionaryWords = { "tweetdeck", "TweetDeck", "tweetduck", "TweetDuck", "TD"