Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yao-msft committed Sep 25, 2023
1 parent 4920193 commit c676480
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
28 changes: 26 additions & 2 deletions src/Microsoft.Management.Deployment/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ namespace winrt::Microsoft::Management::Deployment::implementation
try
{
auto [hrGetCallerId, callerProcessId] = GetCallerProcessId();
THROW_IF_FAILED(hrGetCallerId);
callerName = AppInstaller::Utility::ConvertToUTF8(TryGetCallerProcessInfo(callerProcessId));
if (SUCCEEDED(hrGetCallerId))
{
callerName = AppInstaller::Utility::ConvertToUTF8(TryGetCallerProcessInfo(callerProcessId));
}
}
CATCH_LOG();
}
Expand All @@ -143,4 +145,26 @@ namespace winrt::Microsoft::Management::Deployment::implementation

return callerName;
}

bool ShouldDelayBackgroundUpdateByCaller()
{
bool shouldDelayBackgroundUpdateInterval = false;
try
{
auto [hrGetCallerId, callerProcessId] = GetCallerProcessId();
if (SUCCEEDED(hrGetCallerId) && callerProcessId != GetCurrentProcessId())
{
// OutOfProc case, we check for explorer.exe
auto callerNameWide = AppInstaller::Utility::ConvertToUTF16(GetCallerName());
auto processName = AppInstaller::Utility::ConvertToUTF8(std::filesystem::path{ callerNameWide }.filename().wstring());
if (::AppInstaller::Utility::CaseInsensitiveEquals("explorer.exe", processName))
{
shouldDelayBackgroundUpdateInterval = true;
}
}
}
CATCH_LOG();

return shouldDelayBackgroundUpdateInterval;
}
}
1 change: 1 addition & 0 deletions src/Microsoft.Management.Deployment/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ namespace winrt::Microsoft::Management::Deployment::implementation
std::pair<HRESULT, DWORD> GetCallerProcessId();
std::wstring TryGetCallerProcessInfo(DWORD callerProcessId);
std::string GetCallerName();
bool ShouldDelayBackgroundUpdateByCaller();
}
30 changes: 9 additions & 21 deletions src/Microsoft.Management.Deployment/PackageCatalogReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,19 @@ namespace winrt::Microsoft::Management::Deployment::implementation
{
void PackageCatalogReference::Initialize(winrt::Microsoft::Management::Deployment::PackageCatalogInfo packageCatalogInfo, ::AppInstaller::Repository::Source sourceReference)
{
static constexpr winrt::Windows::Foundation::TimeSpan s_PackageCatalogUpdateIntervalDelay = 168h; //1 week

m_info = packageCatalogInfo;
m_sourceReference = std::move(sourceReference);
m_packageCatalogBackgroundUpdateInterval = ::AppInstaller::Settings::User().Get<::AppInstaller::Settings::Setting::AutoUpdateTimeInMinutes>();

bool shouldDelayBackgroundUpdateInterval = false;
try
if (ShouldDelayBackgroundUpdateByCaller())
{
auto [hrGetCallerId, callerProcessId] = GetCallerProcessId();
THROW_IF_FAILED(hrGetCallerId);
if (callerProcessId != GetCurrentProcessId())
{
// OutOfProc case, we check for explorer.exe
auto callerNameWide = AppInstaller::Utility::ConvertToUTF16(GetCallerName());
auto processName = AppInstaller::Utility::ConvertToUTF8(std::filesystem::path{ callerNameWide }.filename().wstring());
if (::AppInstaller::Utility::CaseInsensitiveEquals("explorer.exe", processName))
{
shouldDelayBackgroundUpdateInterval = true;
}
}
}
CATCH_LOG();
static constexpr winrt::Windows::Foundation::TimeSpan s_PackageCatalogUpdateIntervalDelay_Base = 168h; //1 week

if (shouldDelayBackgroundUpdateInterval)
{
m_packageCatalogBackgroundUpdateInterval = s_PackageCatalogUpdateIntervalDelay;
// Add a bit of randomness to the default interval time
std::default_random_engine randomEngine(std::random_device{}());
std::uniform_int_distribution<long long> distribution(0, 168);

m_packageCatalogBackgroundUpdateInterval = s_PackageCatalogUpdateIntervalDelay_Base + std::chrono::hours(distribution(randomEngine));
}
}
void PackageCatalogReference::Initialize(winrt::Microsoft::Management::Deployment::CreateCompositePackageCatalogOptions options)
Expand Down Expand Up @@ -108,6 +94,7 @@ namespace winrt::Microsoft::Management::Deployment::implementation
winrt::Microsoft::Management::Deployment::implementation::PackageCatalogReference* catalogImpl = get_self<winrt::Microsoft::Management::Deployment::implementation::PackageCatalogReference>(catalog);
auto copy = catalogImpl->m_sourceReference;
copy.SetCaller(callerName);
copy.SetBackgroundUpdateInterval(catalog.PackageCatalogBackgroundUpdateInterval());
copy.Open(progress);
remoteSources.emplace_back(std::move(copy));
}
Expand Down Expand Up @@ -149,6 +136,7 @@ namespace winrt::Microsoft::Management::Deployment::implementation

source = m_sourceReference;
source.SetCaller(callerName);
source.SetBackgroundUpdateInterval(PackageCatalogBackgroundUpdateInterval());
source.Open(progress);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.Management.Deployment/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>

#include <mutex>
#include <mutex>
#include <random>

0 comments on commit c676480

Please sign in to comment.