diff --git a/src/Update/PowerToys.Update.cpp b/src/Update/PowerToys.Update.cpp index 07a6a1a730..1715140e51 100644 --- a/src/Update/PowerToys.Update.cpp +++ b/src/Update/PowerToys.Update.cpp @@ -57,7 +57,12 @@ std::optional ObtainInstaller(bool& isUpToDate) auto state = UpdateState::read(); - const auto new_version_info = get_github_version_info_async(); +#if USE_STD_EXPECTED + std::expected new_version_info; +#else + nonstd::expected new_version_info; +#endif + get_github_version_info_async(false, new_version_info).get(); if (std::holds_alternative(*new_version_info)) { isUpToDate = true; @@ -76,7 +81,8 @@ std::optional ObtainInstaller(bool& isUpToDate) // Cleanup old updates before downloading the latest updating::cleanup_updates(); - auto downloaded_installer = download_new_version(std::get(*new_version_info)); + std::optional downloaded_installer; + download_new_version(std::get(*new_version_info), downloaded_installer).get(); if (!downloaded_installer) { Logger::error("Couldn't download new installer"); diff --git a/src/common/updating/updating.cpp b/src/common/updating/updating.cpp index 977daf46a6..27c21d5e8e 100644 --- a/src/common/updating/updating.cpp +++ b/src/common/updating/updating.cpp @@ -83,18 +83,20 @@ namespace updating #pragma warning(push) #pragma warning(disable : 4702) #if USE_STD_EXPECTED - std::expected get_github_version_info_async(const bool prerelease) + winrt::Windows::Foundation::IAsyncAction get_github_version_info_async(const bool prerelease, std::expected& result) #else - nonstd::expected get_github_version_info_async(const bool prerelease) + winrt::Windows::Foundation::IAsyncAction get_github_version_info_async(const bool prerelease, nonstd::expected& result) #endif { // 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 constexpr (VERSION_MAJOR == 0 && VERSION_MINOR == 0) { #if USE_STD_EXPECTED - return std::unexpected(LOCAL_BUILD_ERROR); + result = std::unexpected(LOCAL_BUILD_ERROR); + co_return; #else - return nonstd::make_unexpected(LOCAL_BUILD_ERROR); + result = nonstd::make_unexpected(LOCAL_BUILD_ERROR); + co_return; #endif } @@ -107,7 +109,7 @@ namespace updating if (prerelease) { - const auto body = client.request(Uri{ ALL_RELEASES_ENDPOINT }).get(); + const auto body = co_await client.request(Uri{ ALL_RELEASES_ENDPOINT }); for (const auto& json : json::JsonValue::Parse(body).GetArray()) { auto potential_release_object = json.GetObjectW(); @@ -125,7 +127,7 @@ namespace updating } else { - const auto body = client.request(Uri{ LATEST_RELEASE_ENDPOINT }).get(); + 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)) { @@ -135,23 +137,26 @@ namespace updating if (github_version <= current_version) { - return version_up_to_date{}; + result = version_up_to_date{}; + co_return; } auto [installer_download_url, installer_filename] = extract_installer_asset_download_info(release_object); - return new_version_download_info{ extract_release_page_url(release_object), + result = new_version_download_info{ extract_release_page_url(release_object), std::move(github_version), std::move(installer_download_url), std::move(installer_filename) }; + co_return; } catch (...) { } #if USE_STD_EXPECTED - return std::unexpected(NETWORK_ERROR); + result = std::unexpected(NETWORK_ERROR); #else - return nonstd::make_unexpected(NETWORK_ERROR); + result = nonstd::make_unexpected(NETWORK_ERROR); #endif + co_return; } #pragma warning(pop) @@ -170,12 +175,13 @@ namespace updating return !ec ? std::optional{ installer_download_path } : std::nullopt; } - std::optional download_new_version(const new_version_download_info& new_version) + winrt::Windows::Foundation::IAsyncAction download_new_version(const new_version_download_info& new_version, std::optional& result) { auto installer_download_path = create_download_path(); if (!installer_download_path) { - return std::nullopt; + result = std::nullopt; + co_return; } *installer_download_path /= new_version.installer_filename; @@ -186,7 +192,7 @@ namespace updating try { http::HttpClient client; - client.download(new_version.installer_download_url, *installer_download_path).get(); + co_await client.download(new_version.installer_download_url, *installer_download_path); download_success = true; break; } @@ -195,7 +201,8 @@ namespace updating // reattempt to download or do nothing } } - return download_success ? installer_download_path : std::nullopt; + result = download_success ? installer_download_path : std::nullopt; + co_return; } void cleanup_updates() diff --git a/src/common/updating/updating.h b/src/common/updating/updating.h index 9aaee7739b..2e7239eefc 100644 --- a/src/common/updating/updating.h +++ b/src/common/updating/updating.h @@ -31,12 +31,12 @@ namespace updating }; using github_version_info = std::variant; - std::optional download_new_version(const new_version_download_info& new_version); + winrt::Windows::Foundation::IAsyncAction download_new_version(const new_version_download_info& new_version, std::optional& result); std::filesystem::path get_pending_updates_path(); #if USE_STD_EXPECTED - std::expected get_github_version_info_async(const bool prerelease = false); + winrt::Windows::Foundation::IAsyncAction get_github_version_info_async(const bool prerelease, std::expected& result); #else - nonstd::expected get_github_version_info_async(const bool prerelease = false); + winrt::Windows::Foundation::IAsyncAction get_github_version_info_async(const bool prerelease, nonstd::expected& result); #endif void cleanup_updates(); diff --git a/src/runner/UpdateUtils.cpp b/src/runner/UpdateUtils.cpp index 7e4c007a3f..ca23db7271 100644 --- a/src/runner/UpdateUtils.cpp +++ b/src/runner/UpdateUtils.cpp @@ -173,7 +173,9 @@ void ProcessNewVersionInfo(const github_version_info& version_info, // Cleanup old updates before downloading the latest updating::cleanup_updates(); - if (download_new_version(new_version_info)) + std::optional downloaded_installer; + download_new_version(new_version_info, downloaded_installer).get(); + if (downloaded_installer) { state.state = UpdateState::readyToInstall; state.downloadedInstallerFilename = new_version_info.installer_filename; @@ -232,7 +234,12 @@ void PeriodicUpdateWorker() bool version_info_obtained = false; try { - const auto new_version_info = get_github_version_info_async(); +#if USE_STD_EXPECTED + std::expected new_version_info; +#else + nonstd::expected new_version_info; +#endif + get_github_version_info_async(false, new_version_info).get(); if (new_version_info.has_value()) { version_info_obtained = true; @@ -272,7 +279,12 @@ void CheckForUpdatesCallback() auto state = UpdateState::read(); try { - auto new_version_info = get_github_version_info_async(); + #if USE_STD_EXPECTED + std::expected new_version_info; + #else + nonstd::expected new_version_info; + #endif + get_github_version_info_async(false, new_version_info).get(); if (!new_version_info) { // We couldn't get a new version from github for some reason, log error