diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9429307..84cf6eb 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,12 +4,12 @@ jobs: build: runs-on: windows-2019 steps: - - uses: actions/checkout@v3 - - uses: microsoft/setup-msbuild@v1.1 - - uses: NuGet/setup-nuget@v1 + - uses: actions/checkout@v4 + - uses: microsoft/setup-msbuild@v2 + - uses: NuGet/setup-nuget@v2 - run: nuget restore src/FirefoxAction.sln - run: msbuild src/FirefoxAction.sln -t:rebuild -property:Configuration=Release - - uses: actions/upload-artifact@v3 + - uses: actions/upload-artifact@v4 with: name: dll path: ./src/bin/Release/FirefoxAction.CA.dll \ No newline at end of file diff --git a/src/FirefoxAction.cs b/src/FirefoxAction.cs index 109c0f1..b7051ab 100644 --- a/src/FirefoxAction.cs +++ b/src/FirefoxAction.cs @@ -23,6 +23,7 @@ using Microsoft.Deployment.WindowsInstaller; using Microsoft.Win32; using Newtonsoft.Json.Linq; +using System; namespace FirefoxAction { @@ -36,12 +37,11 @@ public static ActionResult ExtensionSettingsInstall(Session session) // with error status 1603, but when letting exceptions through the raw exception // stack trace will be logged in installer log which gives more information. - var extensionSettings = GetExtensionSettingsFromSession(session); + var extensionSettings = session.GetExtensionSettings(); session.Log("Begin ExtensionSettingsInstall " + extensionSettings.UUID); using (RegistryKey firefox = Utils.FirefoxKey()) { - string value = firefox.GetStringValue("ExtensionSettings", "{}"); - JObject json = JObject.Parse(value); + var json = firefox.GetJSON("ExtensionSettings", "{}"); json[extensionSettings.UUID] = new JObject { ["installation_mode"] = "normal_installed", @@ -54,26 +54,28 @@ public static ActionResult ExtensionSettingsInstall(Session session) [CustomAction] public static ActionResult ExtensionSettingsRemove(Session session) - { - var extensionSettings = GetExtensionSettingsFromSession(session); + { + var extensionSettings = session.GetExtensionSettings(); session.Log("Begin ExtensionSettingsRemove " + extensionSettings.UUID); using (RegistryKey firefox = Utils.FirefoxKey()) { - string value = firefox.GetStringValue("ExtensionSettings"); - if (value != null) + var json = firefox.GetJSON("ExtensionSettings"); + if (json != null) { - JObject json = JObject.Parse(value); json[extensionSettings.UUID] = new JObject { ["installation_mode"] = "blocked" }; firefox.SetValue("ExtensionSettings", json.ToString().Split('\n')); } - return ActionResult.Success; } + return ActionResult.Success; } + } - private static (string UUID, string URL) GetExtensionSettingsFromSession(Session session) + internal static class Utils + { + internal static (string UUID, string URL) GetExtensionSettings(this Session session) { // Deferred custom actions cannot directly access installer properties from session, // only the CustomActionData property is available, see README how to populate it. @@ -82,10 +84,7 @@ private static (string UUID, string URL) GetExtensionSettingsFromSession(Session session.CustomActionData["EXTENSIONSETTINGS_URL"] ); } - } - internal static class Utils - { internal static RegistryKey FirefoxKey() { using (RegistryKey mozilla = Registry.LocalMachine.OpenOrCreateSubKey(@"Software\Policies\Mozilla", true)) @@ -101,7 +100,7 @@ internal static RegistryKey OpenOrCreateSubKey(this RegistryKey registryKey, str internal static string GetStringValue(this RegistryKey registryKey, string name, string defaultValue = null) { - if (!registryKey.ContainsName(name)) + if (Array.IndexOf(registryKey.GetValueNames(), name) < 0) return defaultValue; switch (registryKey.GetValueKind(name)) { @@ -114,14 +113,21 @@ internal static string GetStringValue(this RegistryKey registryKey, string name, } } - internal static bool ContainsName(this RegistryKey key, string name) - { - foreach (string value in key.GetValueNames()) - { - if (value == name) - return true; - } - return false; + internal static JObject GetJSON(this RegistryKey registryKey, string name, string defaultValue = null) + { + string value = registryKey.GetStringValue(name, defaultValue); + if (value == null) + { + return null; + } + try + { + return JObject.Parse(value); + } + catch + { + return new JObject(); + } } } }