From f3f5b88550b99fba97b96410bd94774535add6c9 Mon Sep 17 00:00:00 2001
From: chylex <contact@chylex.com>
Date: Mon, 22 Jan 2018 14:53:38 +0100
Subject: [PATCH] Refactor resource handler related extension methods

---
 Core/Notification/FormNotificationBase.cs |  2 +-
 Core/TweetDeckBrowser.cs                  | 11 +----------
 Core/Utils/BrowserUtils.cs                | 11 +++++++++--
 3 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/Core/Notification/FormNotificationBase.cs b/Core/Notification/FormNotificationBase.cs
index 67e119a7..a4ae66d3 100644
--- a/Core/Notification/FormNotificationBase.cs
+++ b/Core/Notification/FormNotificationBase.cs
@@ -139,7 +139,7 @@ protected FormNotificationBase(FormBrowser owner, bool enableContextMenu){
 
             DpiScale = this.GetDPIScale();
 
-            browser.GetHandlerFactory().RegisterHandler(TwitterUtils.TweetDeckURL, this.resourceHandler);
+            browser.SetupResourceHandler(TwitterUtils.TweetDeckURL, this.resourceHandler);
 
             Controls.Add(browser);
 
diff --git a/Core/TweetDeckBrowser.cs b/Core/TweetDeckBrowser.cs
index 47fb7ab6..3680b39b 100644
--- a/Core/TweetDeckBrowser.cs
+++ b/Core/TweetDeckBrowser.cs
@@ -178,16 +178,7 @@ private void UserConfig_SoundNotificationInfoChanged(object sender, EventArgs e)
             bool hasCustomSound = Program.UserConfig.IsCustomSoundNotificationSet;
 
             if (prevSoundNotificationPath != Program.UserConfig.NotificationSoundPath){
-                DefaultResourceHandlerFactory handlerFactory = browser.GetHandlerFactory();
-                IResourceHandler resourceHandler = hasCustomSound ? SoundNotification.CreateFileHandler(Program.UserConfig.NotificationSoundPath) : null;
-            
-                if (resourceHandler != null){
-                    handlerFactory.RegisterHandler(soundUrl, resourceHandler);
-                }
-                else{
-                    handlerFactory.UnregisterHandler(soundUrl);
-                }
-
+                browser.SetupResourceHandler(soundUrl, hasCustomSound ? SoundNotification.CreateFileHandler(Program.UserConfig.NotificationSoundPath) : null);
                 prevSoundNotificationPath = Program.UserConfig.NotificationSoundPath;
             }
 
diff --git a/Core/Utils/BrowserUtils.cs b/Core/Utils/BrowserUtils.cs
index 9d33167c..44c3ca44 100644
--- a/Core/Utils/BrowserUtils.cs
+++ b/Core/Utils/BrowserUtils.cs
@@ -38,8 +38,15 @@ public static ChromiumWebBrowser AsControl(this IWebBrowser browserControl){
             return (ChromiumWebBrowser)browserControl;
         }
 
-        public static DefaultResourceHandlerFactory GetHandlerFactory(this ChromiumWebBrowser browser){
-            return (DefaultResourceHandlerFactory)browser.ResourceHandlerFactory;
+        public static void SetupResourceHandler(this ChromiumWebBrowser browser, string url, IResourceHandler handler){
+            DefaultResourceHandlerFactory factory = (DefaultResourceHandlerFactory)browser.ResourceHandlerFactory;
+
+            if (handler == null){
+                factory.UnregisterHandler(url);
+            }
+            else{
+                factory.RegisterHandler(url, handler);
+            }
         }
 
         private const string TwitterTrackingUrl = "t.co";