diff --git a/MVVM/Services/RegistryService.ps1 b/MVVM/Services/RegistryService.ps1 new file mode 100644 index 0000000..9274599 --- /dev/null +++ b/MVVM/Services/RegistryService.ps1 @@ -0,0 +1,68 @@ +class RegistryService { + + static [string] $BasePath = "HKLM:\SOFTWARE\Policies\Mozilla\Firefox" + static [string] $ExtPath = "HKLM:\SOFTWARE\Policies\Mozilla\Firefox\ExtensionSettings" + + static [void] EnsureKey([string]$path) { + if (-not (Test-Path $path)) { New-Item -Path $path -Force | Out-Null } + } + + static [void] SetString([string]$path, [string]$name, [string]$value) { + [RegistryService]::EnsureKey($path) + Set-ItemProperty -Path $path -Name $name -Value $value -Type String + } + + static [void] SetDWord([string]$path, [string]$name, [int]$value) { + [RegistryService]::EnsureKey($path) + Set-ItemProperty -Path $path -Name $name -Value $value -Type DWord + } + + static [void] RemoveValue([string]$path, [string]$name) { + Remove-ItemProperty -Path $path -Name $name -ErrorAction SilentlyContinue + } + + static [void] RemoveKey([string]$path) { + if (Test-Path $path) { Remove-Item -Path $path -Recurse -Force } + } + + static [void] DeployAddon([AddonModel]$addon) { + $key = Join-Path ([RegistryService]::ExtPath) $addon.Id + [RegistryService]::SetString($key, "installation_mode", "force_installed") + if ($addon.InstallUrl) { + [RegistryService]::SetString($key, "install_url", $addon.InstallUrl) + } + } + + static [void] BlockAddon([string]$addonId) { + $key = Join-Path ([RegistryService]::ExtPath) $addonId + [RegistryService]::SetString($key, "installation_mode", "blocked") + } + + static [void] SetWildcard([string]$mode) { + $key = Join-Path ([RegistryService]::ExtPath) "*" + [RegistryService]::SetString($key, "installation_mode", $mode) + } + + static [void] RemoveJsonString() { + [RegistryService]::RemoveValue([RegistryService]::BasePath, "ExtensionSettings") + } + + static [hashtable] GetExtensionSettings() { + $result = @{} + if (-not (Test-Path ([RegistryService]::ExtPath))) { return $result } + Get-ChildItem ([RegistryService]::ExtPath) -ErrorAction SilentlyContinue | ForEach-Object { + $props = Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue + $result[$_.PSChildName] = @{ + installation_mode = $props.installation_mode + install_url = $props.install_url + } + } + return $result + } + + static [string] GetValue([string]$path, [string]$name) { + try { + return (Get-ItemProperty $path -Name $name -ErrorAction Stop).$name + } catch { return $null } + } +}