Skip to content

Commit

Permalink
SDK: Add get_property support, remove callbacks and vectors from prop…
Browse files Browse the repository at this point in the history
…erties (#153)
  • Loading branch information
JeffreySaathoff authored Feb 8, 2023
1 parent 11e67b8 commit 17b1997
Show file tree
Hide file tree
Showing 19 changed files with 956 additions and 679 deletions.
2 changes: 1 addition & 1 deletion azure-pipelines/build/windows/dosdkcpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pr:
- sdk-cpp/build/cleanup-install.sh

pool:
vmImage: "windows-2019"
vmImage: "windows-2022"

jobs:
- job: Debug
Expand Down
2 changes: 1 addition & 1 deletion build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def compiler(self):
def generator(self):
# No need to specify architecture here as the default target platform name (architecture) is that of the host and is provided in the CMAKE_VS_PLATFORM_NAME_DEFAULT variable
# https://cmake.org/cmake/help/latest/generator/Visual%20Studio%2016%202019.html
return super().generator or 'Visual Studio 16 2019'
return super().generator or 'Visual Studio 17 2022'

@property
def generate_options(self):
Expand Down
41 changes: 35 additions & 6 deletions sdk-cpp/include/do_download.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,49 @@ class download
std::error_code finalize() noexcept;
std::error_code abort() noexcept;
std::error_code get_status(download_status& status) noexcept;
std::error_code set_status_callback(status_callback_t callback) noexcept;

std::error_code start_and_wait_until_completion(std::chrono::seconds timeoutSecs = std::chrono::hours(24)) noexcept;
std::error_code start_and_wait_until_completion(const std::atomic_bool& isCancelled, std::chrono::seconds timeoutSecs = std::chrono::hours(24)) noexcept;
static std::error_code download_url_to_path(const std::string& uri, const std::string& downloadFilePath, std::chrono::seconds timeoutSecs = std::chrono::hours(24)) noexcept;
static std::error_code download_url_to_path(const std::string& uri, const std::string& downloadFilePath, const std::atomic_bool& isCancelled, std::chrono::seconds timeoutSecs = std::chrono::hours(24)) noexcept;

/*
For devices running windows before 20H1, dosvc exposed a now-deprecated com interface for setting certain download properties.
After 20H1, these properties were added to newer com interface, which this SDK is using.
Attempting to set a download property on a version of windows earlier than 20H1 will not set the property and throw an exception with
error code msdo::errc::do_e_unknown_property_id.
Certain properties are not supported on older versions of Windows, resulting in
msdo::errc::do_e_unknown_property_id from the following methods. See do_download_property.h.
*/
std::error_code set_property(download_property key, const download_property_value& value) noexcept;
std::error_code get_property(download_property key, download_property_value& value) noexcept;
std::error_code set_property(download_property prop, const download_property_value& value) noexcept;
std::error_code get_property(download_property prop, download_property_value& value) noexcept;

template <typename T>
std::error_code set_property(download_property prop, const T& value) noexcept
{
download_property_value propVal;
std::error_code ec = download_property_value::make(value, propVal);
if (!ec)
{
ec = set_property(prop, propVal);
}
return ec;
}

template <typename T>
std::error_code get_property(download_property prop, T& value) noexcept
{
value = {};
download_property_value propVal;
std::error_code ec = get_property(prop, propVal);
if (!ec)
{
ec = propVal.as(value);
}
return ec;
}

std::error_code set_cost_policy(download_cost_policy value) noexcept
{
return set_property(download_property::cost_policy, static_cast<uint32_t>(value));
}

private:
download();
Expand Down
66 changes: 33 additions & 33 deletions sdk-cpp/include/do_download_property.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#ifndef _DELIVERY_OPTIMIZATION_DO_DOWNLOAD_PROPERTY_H
#define _DELIVERY_OPTIMIZATION_DO_DOWNLOAD_PROPERTY_H

#include <functional>
#include <memory>
#include <string>
#include <vector>
Expand All @@ -23,69 +22,70 @@ class CDownloadPropertyValueInternal;
class download;
class download_status;

/*
For REST interface, these download properties are not yet supported
SDK will throw/return msdo::errc::e_notimpl if attempting to set/get a property
*/
enum class download_property
{
id, // std::string
uri, // std::string
catalog_id, // std::string
caller_name, // std::string
download_file_path, // std::string
http_custom_headers, // std::string
cost_policy, // uint32
id = 0, // string (readonly)
uri, // string
catalog_id, // string
caller_name, // string
download_file_path, // string
http_custom_headers, // string
cost_policy, // uint32 (see download_cost_policy enum)
security_flags, // uint32
callback_freq_percent, // uint32
callback_freq_seconds, // uint32
no_progress_timeout_seconds, // uint32
use_foreground_priority, // bool
blocking_mode, // bool
callback_interface, // void*, but used for storing lambda expressions
stream_interface, // void*
security_context, // byte array
network_token, // bool
correlation_vector, // std::string
decryption_info, // std::string
integrity_check_info, // std::string
integrity_check_mandatory, // boolean

// Available beginning in Windows 20H1 (build 19041)
correlation_vector, // string
decryption_info, // string
integrity_check_info, // string
integrity_check_mandatory, // bool
total_size_bytes, // uint64

// For the COM interface, the following properties are available only in Windows 21H2 (Build Number 22000) and beyond
// Available beginning in Windows 21H2 (build 22000)
disallow_on_cellular, // bool
http_custom_auth_headers, // std::string
http_custom_auth_headers, // string

// Available beginning in Windows 22H2 (build 22621)
allow_http_to_https_redirect, // bool
non_volatile, // bool
};

class download_property_value
// Values for download_property::cost_policy
enum class download_cost_policy : uint32_t
{
always = 0, // download regardless of cost (foreground default)
unrestricted_network, // pause download on any metered network
standard, // pause download if over or near data limit (background default)
no_roaming, // pause download if roaming
no_surcharge, // pause download if over data limit
};

/*
CDownloadImpl is declared as a friend class because it needs to access the platform-specific native value for download_property_value
The type of the native value is defined in CDownloadPropertyValueInternal, because DO header files are platform agnostic
This is so any user of the SDK does not have to worry about supplying platform specific compile definitions to use the SDK
*/
class download_property_value
{
friend class details::CDownloadImpl;

public:
using status_callback_t = std::function<void(download&, download_status&)>;

download_property_value();
~download_property_value() = default;

static std::error_code make(const std::string& val, download_property_value& out);
static std::error_code make(const std::wstring& val, download_property_value& out);
static std::error_code make(const char* val, download_property_value& out) { return make(std::string(val), out); }
static std::error_code make(const wchar_t* val, download_property_value& out) { return make(std::wstring(val), out); }
static std::error_code make(uint32_t val, download_property_value& out);
static std::error_code make(uint64_t val, download_property_value& out);
static std::error_code make(bool val, download_property_value& out);
static std::error_code make(std::vector<unsigned char>& val, download_property_value& out);
static std::error_code make(const status_callback_t& val, download_property_value& out);

std::error_code as(bool& val) const noexcept;
std::error_code as(uint32_t& val) const noexcept;
std::error_code as(uint64_t& val) const noexcept;
std::error_code as(std::string& val) const noexcept;
std::error_code as(std::vector<unsigned char>& val) const noexcept;
std::error_code as(status_callback_t& val) const noexcept;
std::error_code as(std::wstring& val) const noexcept;

private:
std::shared_ptr<details::CDownloadPropertyValueInternal> _val;
Expand Down
4 changes: 4 additions & 0 deletions sdk-cpp/include/do_download_status.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define _DELIVERY_OPTIMIZATION_DO_DOWNLOAD_STATUS_H

#include <cstdint>
#include <functional>
#include <system_error>

#include "do_errors.h"
Expand Down Expand Up @@ -71,6 +72,9 @@ class download_status

};

class download;
using status_callback_t = std::function<void(download&, download_status&)>;

} // namespace deliveryoptimization
} // namespace microsoft

Expand Down
20 changes: 7 additions & 13 deletions sdk-cpp/src/do_download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ download::~download() = default;
std::error_code download::make(const std::string& uri, const std::string& downloadFilePath, std::unique_ptr<download>& out) noexcept
{
out.reset();
// using 'new' to access non-public constructor
std::unique_ptr<download> tmp(new download());
tmp->_download = std::make_shared<msdod::CDownloadImpl>();
std::error_code code = tmp->_download->Init(uri, downloadFilePath);
Expand Down Expand Up @@ -69,6 +68,11 @@ std::error_code download::get_status(download_status& status) noexcept
return _download->GetStatus(status);
}

std::error_code download::set_status_callback(status_callback_t callback) noexcept
{
return _download->SetStatusCallback(callback, *this);
}

std::error_code download::start_and_wait_until_completion(std::chrono::seconds timeOut) noexcept
{
std::atomic_bool isCancelled{ false };
Expand Down Expand Up @@ -157,18 +161,8 @@ static std::error_code g_TryOverrideDownlevelOsSetPropertyError(download_propert

std::error_code download::set_property(download_property prop, const download_property_value& val) noexcept
{
if (prop == download_property::callback_interface)
{
download_property_value::status_callback_t userCallback;
DO_RETURN_IF_FAILED(val.as(userCallback));

return _download->SetCallback(userCallback, *this);
}
else
{
auto ec = _download->SetProperty(prop, val);
return g_TryOverrideDownlevelOsSetPropertyError(prop, ec);
}
auto ec = _download->SetProperty(prop, val);
return g_TryOverrideDownlevelOsSetPropertyError(prop, ec);
}

std::error_code download::get_property(download_property prop, download_property_value& val) noexcept
Expand Down
25 changes: 5 additions & 20 deletions sdk-cpp/src/do_download_property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ std::error_code download_property_value::make(const std::string& val, download_p
return DO_OK;
}

std::error_code download_property_value::make(uint32_t val, download_property_value& out)
std::error_code download_property_value::make(const std::wstring& val, download_property_value& out)
{
download_property_value temp;
std::error_code code = temp._val->Init(val);
Expand All @@ -38,17 +38,7 @@ std::error_code download_property_value::make(uint32_t val, download_property_va
return DO_OK;
}

std::error_code download_property_value::make(uint64_t val, download_property_value& out)
{
download_property_value temp;
std::error_code code = temp._val->Init(val);
DO_RETURN_IF_FAILED(code);

out = temp;
return DO_OK;
}

std::error_code download_property_value::make(bool val, download_property_value& out)
std::error_code download_property_value::make(uint32_t val, download_property_value& out)
{
download_property_value temp;
std::error_code code = temp._val->Init(val);
Expand All @@ -58,7 +48,7 @@ std::error_code download_property_value::make(bool val, download_property_value&
return DO_OK;
}

std::error_code download_property_value::make(std::vector<unsigned char>& val, download_property_value& out)
std::error_code download_property_value::make(uint64_t val, download_property_value& out)
{
download_property_value temp;
std::error_code code = temp._val->Init(val);
Expand All @@ -68,7 +58,7 @@ std::error_code download_property_value::make(std::vector<unsigned char>& val, d
return DO_OK;
}

std::error_code download_property_value::make(const status_callback_t& val, download_property_value& out)
std::error_code download_property_value::make(bool val, download_property_value& out)
{
download_property_value temp;
std::error_code code = temp._val->Init(val);
Expand Down Expand Up @@ -98,12 +88,7 @@ std::error_code download_property_value::as(std::string& val) const noexcept
return _val->As(val);
}

std::error_code download_property_value::as(std::vector<unsigned char>& val) const noexcept
{
return _val->As(val);
}

std::error_code download_property_value::as(status_callback_t& val) const noexcept
std::error_code download_property_value::as(std::wstring& val) const noexcept
{
return _val->As(val);
}
Expand Down
Loading

0 comments on commit 17b1997

Please sign in to comment.