mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-23 19:49:43 +01:00
refactor(updating): replace std::future with IAsyncOperation for async methods
This commit is contained in:
@@ -57,7 +57,7 @@ std::optional<fs::path> ObtainInstaller(bool& isUpToDate)
|
||||
|
||||
auto state = UpdateState::read();
|
||||
|
||||
const auto new_version_info = get_github_version_info_async().get();
|
||||
const auto new_version_info = get_github_version_info_async();
|
||||
if (std::holds_alternative<version_up_to_date>(*new_version_info))
|
||||
{
|
||||
isUpToDate = true;
|
||||
@@ -76,7 +76,7 @@ std::optional<fs::path> ObtainInstaller(bool& isUpToDate)
|
||||
// Cleanup old updates before downloading the latest
|
||||
updating::cleanup_updates();
|
||||
|
||||
auto downloaded_installer = download_new_version(std::get<new_version_download_info>(*new_version_info)).get();
|
||||
auto downloaded_installer = download_new_version(std::get<new_version_download_info>(*new_version_info));
|
||||
if (!downloaded_installer)
|
||||
{
|
||||
Logger::error("Couldn't download new installer");
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace // Strings in this namespace should not be localized
|
||||
|
||||
namespace updating
|
||||
{
|
||||
std::future<bool> uninstall_previous_msix_version_async()
|
||||
winrt::Windows::Foundation::IAsyncOperation<bool> uninstall_previous_msix_version_async()
|
||||
{
|
||||
winrt::Windows::Management::Deployment::PackageManager package_manager;
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <future>
|
||||
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
#include <common/version/helper.h>
|
||||
|
||||
namespace updating
|
||||
{
|
||||
std::future<bool> uninstall_previous_msix_version_async();
|
||||
winrt::Windows::Foundation::IAsyncOperation<bool> uninstall_previous_msix_version_async();
|
||||
}
|
||||
@@ -83,18 +83,18 @@ namespace updating
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4702)
|
||||
#if USE_STD_EXPECTED
|
||||
std::future<std::expected<github_version_info, std::wstring>> get_github_version_info_async(const bool prerelease)
|
||||
std::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease)
|
||||
#else
|
||||
std::future<nonstd::expected<github_version_info, std::wstring>> get_github_version_info_async(const bool prerelease)
|
||||
nonstd::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease)
|
||||
#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
|
||||
co_return std::unexpected(LOCAL_BUILD_ERROR);
|
||||
return std::unexpected(LOCAL_BUILD_ERROR);
|
||||
#else
|
||||
co_return nonstd::make_unexpected(LOCAL_BUILD_ERROR);
|
||||
return nonstd::make_unexpected(LOCAL_BUILD_ERROR);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ namespace updating
|
||||
|
||||
if (prerelease)
|
||||
{
|
||||
const auto body = co_await client.request(Uri{ ALL_RELEASES_ENDPOINT });
|
||||
const auto body = client.request(Uri{ ALL_RELEASES_ENDPOINT }).get();
|
||||
for (const auto& json : json::JsonValue::Parse(body).GetArray())
|
||||
{
|
||||
auto potential_release_object = json.GetObjectW();
|
||||
@@ -125,7 +125,7 @@ namespace updating
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto body = co_await client.request(Uri{ LATEST_RELEASE_ENDPOINT });
|
||||
const auto body = client.request(Uri{ LATEST_RELEASE_ENDPOINT }).get();
|
||||
release_object = json::JsonValue::Parse(body).GetObjectW();
|
||||
if (auto extracted_version = extract_version_from_release_object(release_object))
|
||||
{
|
||||
@@ -135,11 +135,11 @@ namespace updating
|
||||
|
||||
if (github_version <= current_version)
|
||||
{
|
||||
co_return version_up_to_date{};
|
||||
return version_up_to_date{};
|
||||
}
|
||||
|
||||
auto [installer_download_url, installer_filename] = extract_installer_asset_download_info(release_object);
|
||||
co_return new_version_download_info{ extract_release_page_url(release_object),
|
||||
return new_version_download_info{ extract_release_page_url(release_object),
|
||||
std::move(github_version),
|
||||
std::move(installer_download_url),
|
||||
std::move(installer_filename) };
|
||||
@@ -148,9 +148,9 @@ namespace updating
|
||||
{
|
||||
}
|
||||
#if USE_STD_EXPECTED
|
||||
co_return std::unexpected(NETWORK_ERROR);
|
||||
return std::unexpected(NETWORK_ERROR);
|
||||
#else
|
||||
co_return nonstd::make_unexpected(NETWORK_ERROR);
|
||||
return nonstd::make_unexpected(NETWORK_ERROR);
|
||||
#endif
|
||||
}
|
||||
#pragma warning(pop)
|
||||
@@ -170,12 +170,12 @@ namespace updating
|
||||
return !ec ? std::optional{ installer_download_path } : std::nullopt;
|
||||
}
|
||||
|
||||
std::future<std::optional<std::filesystem::path>> download_new_version(const new_version_download_info& new_version)
|
||||
std::optional<std::filesystem::path> download_new_version(const new_version_download_info& new_version)
|
||||
{
|
||||
auto installer_download_path = create_download_path();
|
||||
if (!installer_download_path)
|
||||
{
|
||||
co_return std::nullopt;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
*installer_download_path /= new_version.installer_filename;
|
||||
@@ -186,7 +186,7 @@ namespace updating
|
||||
try
|
||||
{
|
||||
http::HttpClient client;
|
||||
co_await client.download(new_version.installer_download_url, *installer_download_path);
|
||||
client.download(new_version.installer_download_url, *installer_download_path).get();
|
||||
download_success = true;
|
||||
break;
|
||||
}
|
||||
@@ -195,7 +195,7 @@ namespace updating
|
||||
// reattempt to download or do nothing
|
||||
}
|
||||
}
|
||||
co_return download_success ? installer_download_path : std::nullopt;
|
||||
return download_success ? installer_download_path : std::nullopt;
|
||||
}
|
||||
|
||||
void cleanup_updates()
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <future>
|
||||
#include <filesystem>
|
||||
#include <variant>
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
@@ -32,12 +31,12 @@ namespace updating
|
||||
};
|
||||
using github_version_info = std::variant<new_version_download_info, version_up_to_date>;
|
||||
|
||||
std::future<std::optional<std::filesystem::path>> download_new_version(const new_version_download_info& new_version);
|
||||
std::optional<std::filesystem::path> download_new_version(const new_version_download_info& new_version);
|
||||
std::filesystem::path get_pending_updates_path();
|
||||
#if USE_STD_EXPECTED
|
||||
std::future<std::expected<github_version_info, std::wstring>> get_github_version_info_async(const bool prerelease = false);
|
||||
std::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease = false);
|
||||
#else
|
||||
std::future<nonstd::expected<github_version_info, std::wstring>> get_github_version_info_async(const bool prerelease = false);
|
||||
nonstd::expected<github_version_info, std::wstring> get_github_version_info_async(const bool prerelease = false);
|
||||
#endif
|
||||
void cleanup_updates();
|
||||
|
||||
|
||||
@@ -28,8 +28,6 @@
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>..\;..\..\;..\..\..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<!-- Required for std::future coroutines in HttpClient.h -->
|
||||
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
|
||||
</ClCompile>
|
||||
<Lib>
|
||||
<AdditionalDependencies>Version.lib</AdditionalDependencies>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <future>
|
||||
#include <functional>
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
#include <winrt/Windows.Storage.Streams.h>
|
||||
#include <winrt/Windows.Web.Http.h>
|
||||
@@ -21,15 +21,15 @@ namespace http
|
||||
headers.UserAgent().TryParseAdd(USER_AGENT);
|
||||
}
|
||||
|
||||
std::future<std::wstring> request(const winrt::Windows::Foundation::Uri& url)
|
||||
winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> request(const winrt::Windows::Foundation::Uri& url)
|
||||
{
|
||||
auto response = co_await m_client.GetAsync(url);
|
||||
(void)response.EnsureSuccessStatusCode();
|
||||
auto body = co_await response.Content().ReadAsStringAsync();
|
||||
co_return std::wstring(body);
|
||||
co_return body;
|
||||
}
|
||||
|
||||
std::future<void> download(const winrt::Windows::Foundation::Uri& url, const std::wstring& dstFilePath)
|
||||
winrt::Windows::Foundation::IAsyncAction download(const winrt::Windows::Foundation::Uri& url, const std::wstring& dstFilePath)
|
||||
{
|
||||
auto response = co_await m_client.GetAsync(url);
|
||||
(void)response.EnsureSuccessStatusCode();
|
||||
@@ -38,7 +38,7 @@ namespace http
|
||||
file_stream.Close();
|
||||
}
|
||||
|
||||
std::future<void> download(const winrt::Windows::Foundation::Uri& url, const std::wstring& dstFilePath, const std::function<void(float)>& progressUpdateCallback)
|
||||
winrt::Windows::Foundation::IAsyncAction download(const winrt::Windows::Foundation::Uri& url, const std::wstring& dstFilePath, const std::function<void(float)>& progressUpdateCallback)
|
||||
{
|
||||
auto response = co_await m_client.GetAsync(url, HttpCompletionOption::ResponseHeadersRead);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
@@ -5140,7 +5140,7 @@ bool IsPenInverted( WPARAM wParam )
|
||||
// Captures the specified screen using the capture APIs
|
||||
//
|
||||
//----------------------------------------------------------------------------
|
||||
std::future<winrt::com_ptr<ID3D11Texture2D>> CaptureScreenshotAsync(winrt::IDirect3DDevice const& device, winrt::GraphicsCaptureItem const& item, winrt::DirectXPixelFormat const& pixelFormat)
|
||||
winrt::Windows::Foundation::IAsyncAction CaptureScreenshotAsync(winrt::IDirect3DDevice const& device, winrt::GraphicsCaptureItem const& item, winrt::DirectXPixelFormat const& pixelFormat, winrt::com_ptr<ID3D11Texture2D>& result)
|
||||
{
|
||||
auto d3dDevice = GetDXGIInterfaceFromObject<ID3D11Device>(device);
|
||||
winrt::com_ptr<ID3D11DeviceContext> d3dContext;
|
||||
@@ -5176,9 +5176,7 @@ std::future<winrt::com_ptr<ID3D11Texture2D>> CaptureScreenshotAsync(winrt::IDire
|
||||
framePool.Close();
|
||||
|
||||
auto texture = GetDXGIInterfaceFromObject<ID3D11Texture2D>(frame.Surface());
|
||||
auto result = util::CopyD3DTexture(d3dDevice, texture, true);
|
||||
|
||||
co_return result;
|
||||
result = util::CopyD3DTexture(d3dDevice, texture, true);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
@@ -5205,10 +5203,9 @@ winrt::com_ptr<ID3D11Texture2D>CaptureScreenshot(winrt::DirectXPixelFormat const
|
||||
|
||||
auto item = util::CreateCaptureItemForMonitor(hMon);
|
||||
|
||||
auto capture = CaptureScreenshotAsync(device, item, pixelFormat);
|
||||
capture.wait();
|
||||
|
||||
return capture.get();
|
||||
winrt::com_ptr<ID3D11Texture2D> result;
|
||||
CaptureScreenshotAsync(device, item, pixelFormat, result).get();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -173,7 +173,7 @@ 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).get())
|
||||
if (download_new_version(new_version_info))
|
||||
{
|
||||
state.state = UpdateState::readyToInstall;
|
||||
state.downloadedInstallerFilename = new_version_info.installer_filename;
|
||||
@@ -232,7 +232,7 @@ void PeriodicUpdateWorker()
|
||||
bool version_info_obtained = false;
|
||||
try
|
||||
{
|
||||
const auto new_version_info = get_github_version_info_async().get();
|
||||
const auto new_version_info = get_github_version_info_async();
|
||||
if (new_version_info.has_value())
|
||||
{
|
||||
version_info_obtained = true;
|
||||
@@ -272,7 +272,7 @@ void CheckForUpdatesCallback()
|
||||
auto state = UpdateState::read();
|
||||
try
|
||||
{
|
||||
auto new_version_info = get_github_version_info_async().get();
|
||||
auto new_version_info = get_github_version_info_async();
|
||||
if (!new_version_info)
|
||||
{
|
||||
// We couldn't get a new version from github for some reason, log error
|
||||
|
||||
Reference in New Issue
Block a user