/*
* Copyright (c) 2023 Proton AG
*
* This file is part of ProtonVPN.
*
* ProtonVPN is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonVPN is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonVPN. If not, see .
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Win32;
namespace ProtonVPN.Common.Legacy.OS.Systems;
public class SystemState : ISystemState
{
private readonly List _keysExist = new()
{
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending",
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootInProgress",
@"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired",
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending",
@"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting",
};
private readonly Dictionary> _valuesEqual = new()
{
{@"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", new List {"DVDRebootSignal"}},
};
public bool PendingReboot()
{
return _keysExist.Any(DoesSubKeyExist) || DoesAnyValueMatch()
? true
: PendingUpdates();
}
private bool DoesAnyValueMatch()
{
foreach (KeyValuePair> item in _valuesEqual)
{
using (RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(item.Key))
{
string value = (string)key?.GetValue("");
if (value != null && item.Value.Contains(value))
{
return true;
}
}
}
return false;
}
private bool DoesSubKeyExist(string keyName)
{
using (RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(keyName))
{
return key != null;
}
}
private bool PendingUpdates()
{
object val;
using (RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Updates"))
{
val = key?.GetValue("UpdateExeVolatile");
}
if (val != null)
{
int.TryParse((string)val, out int result);
if (result != 0)
{
return true;
}
}
using (RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Services\Pending"))
{
return key?.SubKeyCount > 0;
}
}
}