runner: implement automatic MSIX package uninstallation on startup (#1920)

This commit is contained in:
Andrey Nekrasov 2020-04-03 19:51:28 +03:00 committed by GitHub
parent 38d537bb8a
commit 7cfe74b384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 7 deletions

View File

@ -27,8 +27,3 @@ VersionHelper::VersionHelper(int major, int minor, int revision) :
revision(revision)
{
}
bool VersionHelper::operator>(const VersionHelper& rhs)
{
return std::tie(major, minor, revision) > std::tie(rhs.major, rhs.minor, rhs.revision);
}

View File

@ -1,13 +1,14 @@
#pragma once
#include <string>
#include <compare>
struct VersionHelper
{
VersionHelper(std::string str);
VersionHelper(int major, int minor, int revision);
bool operator>(const VersionHelper& rhs);
auto operator<=>(const VersionHelper&) const = default;
int major;
int minor;

View File

@ -14,6 +14,7 @@
#include <winrt/Windows.Web.Http.h>
#include <winrt/Windows.Web.Http.Headers.h>
#include <winrt/Windows.Management.Deployment.h>
#include "VersionHelper.h"
@ -23,6 +24,8 @@ namespace
const wchar_t* DONT_SHOW_AGAIN_RECORD_REGISTRY_PATH = L"delete_previous_powertoys_confirm";
const wchar_t* USER_AGENT = L"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
const wchar_t* LATEST_RELEASE_ENDPOINT = L"https://api.github.com/repos/microsoft/PowerToys/releases/latest";
const wchar_t* MSIX_PACKAGE_NAME = L"Microsoft.PowerToys";
const wchar_t* MSIX_PACKAGE_PUBLISHER = L"CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US";
}
namespace localized_strings
@ -125,4 +128,30 @@ std::future<std::optional<new_version_download_info>> check_for_new_github_relea
{
co_return std::nullopt;
}
}
}
std::future<bool> uninstall_previous_msix_version_async()
{
winrt::Windows::Management::Deployment::PackageManager package_manager;
try
{
auto packages = package_manager.FindPackagesForUser({}, MSIX_PACKAGE_NAME, MSIX_PACKAGE_PUBLISHER);
VersionHelper current_version(VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION);
for (auto package : packages)
{
VersionHelper msix_version(package.Id().Version().Major, package.Id().Version().Minor, package.Id().Version().Revision);
if (msix_version < current_version)
{
co_await package_manager.RemovePackageAsync(package.Id().FullName());
co_return true;
}
}
}
catch (...)
{
}
co_return false;
}

View File

@ -10,6 +10,8 @@ std::wstring get_msi_package_path();
bool uninstall_msi_version(const std::wstring& package_path);
bool offer_msi_uninstallation();
std::future<bool> uninstall_previous_msix_version_async();
struct new_version_download_info
{
winrt::Windows::Foundation::Uri release_page_uri;

View File

@ -32,6 +32,8 @@ extern "C" IMAGE_DOS_HEADER __ImageBase;
namespace localized_strings
{
const wchar_t MSI_VERSION_IS_ALREADY_RUNNING[] = L"An older version of PowerToys is already running.";
const wchar_t OLDER_MSIX_UNINSTALLED[] = L"An older MSIX version of PowerToys was uninstalled.";
const wchar_t GITHUB_NEW_VERSION_AVAILABLE_OFFER_VISIT[] = L"An update to PowerToys is available. Visit our GitHub page to get ";
const wchar_t GITHUB_NEW_VERSION_AGREE[] = L"Visit";
}
@ -180,6 +182,16 @@ int runner(bool isProcessElevated)
start_msi_uninstallation_sequence();
} }.detach();
}
else
{
std::thread{[] {
if(uninstall_previous_msix_version_async().get())
{
notifications::show_toast(localized_strings::OLDER_MSIX_UNINSTALLED);
}
}}.detach();
}
notifications::register_background_toast_handler();