2020-04-21 10:30:12 +03:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
#include <common/version/version.h>
|
|
|
|
|
#include <common/version/helper.h>
|
2020-04-21 10:30:12 +03:00
|
|
|
|
2020-06-23 15:53:02 +03:00
|
|
|
#include "http_client.h"
|
2020-10-20 14:00:06 +03:00
|
|
|
#include "notifications.h"
|
2020-04-21 10:30:12 +03:00
|
|
|
#include "updating.h"
|
|
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
#include <common/utils/json.h>
|
|
|
|
|
#include <common/SettingsAPI/settings_helpers.h>
|
|
|
|
|
#include <common/notifications/notifications.h>
|
2020-04-21 10:30:12 +03:00
|
|
|
|
2020-10-22 19:02:59 +03:00
|
|
|
namespace // Strings in this namespace should not be localized
|
2020-04-21 10:30:12 +03:00
|
|
|
{
|
|
|
|
|
const wchar_t LATEST_RELEASE_ENDPOINT[] = L"https://api.github.com/repos/microsoft/PowerToys/releases/latest";
|
2020-12-10 19:05:43 +03:00
|
|
|
const wchar_t ALL_RELEASES_ENDPOINT[] = L"https://api.github.com/repos/microsoft/PowerToys/releases";
|
2020-04-27 13:39:47 +03:00
|
|
|
|
|
|
|
|
const size_t MAX_DOWNLOAD_ATTEMPTS = 3;
|
2020-04-21 10:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace updating
|
|
|
|
|
{
|
2020-12-10 19:05:43 +03:00
|
|
|
std::optional<VersionHelper> extract_version_from_release_object(const json::JsonObject& release_object)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return VersionHelper{ winrt::to_string(release_object.GetNamedString(L"tag_name")) };
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<Uri, std::wstring> extract_installer_asset_download_info(const json::JsonObject& release_object)
|
|
|
|
|
{
|
|
|
|
|
const std::wstring_view required_architecture = get_architecture_string(get_current_architecture());
|
|
|
|
|
constexpr const std::wstring_view required_filename_pattern = updating::INSTALLER_FILENAME_PATTERN;
|
|
|
|
|
// Desc-sorted by its priority
|
|
|
|
|
const std::array<std::wstring_view, 2> asset_extensions = { L".exe", L".msi" };
|
|
|
|
|
for (const auto asset_extension : asset_extensions)
|
|
|
|
|
{
|
|
|
|
|
for (auto asset_elem : release_object.GetNamedArray(L"assets"))
|
|
|
|
|
{
|
|
|
|
|
auto asset{ asset_elem.GetObjectW() };
|
|
|
|
|
std::wstring filename_lower = asset.GetNamedString(L"name", {}).c_str();
|
|
|
|
|
std::transform(begin(filename_lower), end(filename_lower), begin(filename_lower), ::towlower);
|
|
|
|
|
|
|
|
|
|
const bool extension_matched = filename_lower.ends_with(asset_extension);
|
|
|
|
|
const bool architecture_matched = filename_lower.find(required_architecture) != std::wstring::npos;
|
|
|
|
|
const bool filename_matched = filename_lower.find(required_filename_pattern) != std::wstring::npos;
|
|
|
|
|
const bool asset_matched = extension_matched && architecture_matched && filename_matched;
|
|
|
|
|
if (extension_matched && architecture_matched && filename_matched)
|
|
|
|
|
{
|
|
|
|
|
return std::make_pair(Uri{ asset.GetNamedString(L"browser_download_url") }, std::move(filename_lower));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw std::runtime_error("Release object doesn't have the required asset");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::future<nonstd::expected<new_version_download_info, std::wstring>> get_new_github_version_info_async(const notifications::strings& strings, const bool prerelease)
|
2020-04-21 10:30:12 +03:00
|
|
|
{
|
2020-10-20 12:35:51 +03:00
|
|
|
// If the current version starts with 0.0.*, it means we're on a local build from a farm and shouldn't check for updates.
|
|
|
|
|
if (VERSION_MAJOR == 0 && VERSION_MINOR == 0)
|
|
|
|
|
{
|
2020-11-18 17:31:15 +03:00
|
|
|
co_return nonstd::make_unexpected(strings.GITHUB_NEW_VERSION_USING_LOCAL_BUILD_ERROR);
|
2020-10-20 12:35:51 +03:00
|
|
|
}
|
2020-04-21 10:30:12 +03:00
|
|
|
try
|
|
|
|
|
{
|
2020-06-23 15:53:02 +03:00
|
|
|
http::HttpClient client;
|
2020-12-10 19:05:43 +03:00
|
|
|
json::JsonObject release_object;
|
|
|
|
|
const VersionHelper current_version(VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION);
|
|
|
|
|
VersionHelper github_version = current_version;
|
|
|
|
|
if (prerelease)
|
2020-04-21 10:30:12 +03:00
|
|
|
{
|
2020-12-10 19:05:43 +03:00
|
|
|
const auto body = co_await client.request(Uri{ ALL_RELEASES_ENDPOINT });
|
|
|
|
|
for (const auto& json : json::JsonValue::Parse(body).GetArray())
|
2020-04-21 10:30:12 +03:00
|
|
|
{
|
2020-12-10 19:05:43 +03:00
|
|
|
auto potential_release_object = json.GetObjectW();
|
|
|
|
|
const bool is_prerelease = potential_release_object.GetNamedBoolean(L"prerelease", false);
|
|
|
|
|
auto extracted_version = extract_version_from_release_object(potential_release_object);
|
|
|
|
|
if (!is_prerelease || !extracted_version || *extracted_version <= github_version)
|
2020-04-21 10:30:12 +03:00
|
|
|
{
|
2020-12-10 19:05:43 +03:00
|
|
|
continue;
|
2020-04-21 10:30:12 +03:00
|
|
|
}
|
2020-12-10 19:05:43 +03:00
|
|
|
// Do not break, since https://developer.github.com/v3/repos/releases/#list-releases
|
|
|
|
|
// doesn't specify the order in which release object appear
|
|
|
|
|
github_version = std::move(*extracted_version);
|
|
|
|
|
release_object = std::move(potential_release_object);
|
2020-04-21 10:30:12 +03:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-10 19:05:43 +03:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const auto body = co_await client.request(Uri{ LATEST_RELEASE_ENDPOINT });
|
|
|
|
|
release_object = json::JsonValue::Parse(body).GetObjectW();
|
|
|
|
|
if (auto extracted_version = extract_version_from_release_object(release_object))
|
|
|
|
|
{
|
|
|
|
|
github_version = *extracted_version;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (github_version <= current_version)
|
|
|
|
|
{
|
|
|
|
|
co_return nonstd::make_unexpected(strings.GITHUB_NEW_VERSION_UP_TO_DATE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uri release_page_url{ release_object.GetNamedString(L"html_url") };
|
|
|
|
|
auto installer_download_url = extract_installer_asset_download_info(release_object);
|
|
|
|
|
co_return new_version_download_info{ std::move(release_page_url),
|
|
|
|
|
std::move(github_version),
|
|
|
|
|
std::move(installer_download_url.first),
|
|
|
|
|
std::move(installer_download_url.second) };
|
2020-04-21 10:30:12 +03:00
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-12-10 19:05:43 +03:00
|
|
|
co_return nonstd::make_unexpected(strings.GITHUB_NEW_VERSION_CHECK_ERROR);
|
2020-04-21 10:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool could_be_costly_connection()
|
|
|
|
|
{
|
|
|
|
|
using namespace winrt::Windows::Networking::Connectivity;
|
|
|
|
|
ConnectionProfile internetConnectionProfile = NetworkInformation::GetInternetConnectionProfile();
|
|
|
|
|
return internetConnectionProfile.IsWwanConnectionProfile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::filesystem::path get_pending_updates_path()
|
|
|
|
|
{
|
|
|
|
|
auto path_str{ PTSettingsHelper::get_root_save_folder_location() };
|
|
|
|
|
path_str += L"\\Updates";
|
|
|
|
|
return { std::move(path_str) };
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 15:53:02 +03:00
|
|
|
std::filesystem::path create_download_path()
|
2020-04-27 13:39:47 +03:00
|
|
|
{
|
2020-06-23 15:53:02 +03:00
|
|
|
auto installer_download_dst = get_pending_updates_path();
|
|
|
|
|
std::error_code _;
|
|
|
|
|
std::filesystem::create_directories(installer_download_dst, _);
|
|
|
|
|
return installer_download_dst;
|
2020-04-27 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
2020-10-22 19:02:59 +03:00
|
|
|
std::future<void> try_autoupdate(const bool download_updates_automatically, const notifications::strings& strings)
|
2020-04-21 10:30:12 +03:00
|
|
|
{
|
2020-11-18 17:31:15 +03:00
|
|
|
const auto new_version = co_await get_new_github_version_info_async(strings);
|
2020-04-21 10:30:12 +03:00
|
|
|
if (!new_version)
|
|
|
|
|
{
|
|
|
|
|
co_return;
|
|
|
|
|
}
|
2020-07-27 19:53:29 +03:00
|
|
|
|
2020-04-21 10:30:12 +03:00
|
|
|
if (download_updates_automatically && !could_be_costly_connection())
|
|
|
|
|
{
|
2020-06-23 15:53:02 +03:00
|
|
|
auto installer_download_dst = create_download_path() / new_version->installer_filename;
|
2020-04-27 13:39:47 +03:00
|
|
|
bool download_success = false;
|
|
|
|
|
for (size_t i = 0; i < MAX_DOWNLOAD_ATTEMPTS; ++i)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-06-23 15:53:02 +03:00
|
|
|
http::HttpClient client;
|
|
|
|
|
co_await client.download(new_version->installer_download_url, installer_download_dst);
|
2020-04-27 13:39:47 +03:00
|
|
|
download_success = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
// reattempt to download or do nothing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!download_success)
|
|
|
|
|
{
|
2020-10-22 19:02:59 +03:00
|
|
|
updating::notifications::show_install_error(new_version.value(), strings);
|
2020-04-27 13:39:47 +03:00
|
|
|
co_return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 19:02:59 +03:00
|
|
|
updating::notifications::show_version_ready(new_version.value(), strings);
|
2020-04-21 10:30:12 +03:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-10-22 19:02:59 +03:00
|
|
|
updating::notifications::show_visit_github(new_version.value(), strings);
|
2020-06-23 15:53:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 19:02:59 +03:00
|
|
|
std::future<std::wstring> download_update(const notifications::strings& strings)
|
2020-06-23 15:53:02 +03:00
|
|
|
{
|
2020-11-18 17:31:15 +03:00
|
|
|
const auto new_version = co_await get_new_github_version_info_async(strings);
|
2020-06-23 15:53:02 +03:00
|
|
|
if (!new_version)
|
|
|
|
|
{
|
|
|
|
|
co_return L"";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto installer_download_dst = create_download_path() / new_version->installer_filename;
|
2020-10-22 19:02:59 +03:00
|
|
|
updating::notifications::show_download_start(new_version.value(), strings);
|
2020-06-23 15:53:02 +03:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-07-27 19:53:29 +03:00
|
|
|
auto progressUpdateHandle = [&](float progress) {
|
2020-10-22 19:02:59 +03:00
|
|
|
updating::notifications::update_download_progress(new_version.value(), progress, strings);
|
2020-06-23 15:53:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
http::HttpClient client;
|
|
|
|
|
co_await client.download(new_version->installer_download_url, installer_download_dst, progressUpdateHandle);
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
2020-10-22 19:02:59 +03:00
|
|
|
updating::notifications::show_install_error(new_version.value(), strings);
|
2020-06-23 15:53:02 +03:00
|
|
|
co_return L"";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
co_return new_version->installer_filename;
|
2020-04-21 10:30:12 +03:00
|
|
|
}
|
2020-11-18 17:31:15 +03:00
|
|
|
}
|