-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathProgress.cpp
More file actions
155 lines (131 loc) · 4.22 KB
/
Progress.cpp
File metadata and controls
155 lines (131 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "AppInstallerProgress.h"
#include <AppInstallerErrors.h>
namespace AppInstaller
{
HRESULT ToHRESULT(CancelReason reason)
{
HRESULT hr = E_ABORT;
switch (reason)
{
case CancelReason::CtrlCSignal:
hr = APPINSTALLER_CLI_ERROR_CTRL_SIGNAL_RECEIVED;
break;
case CancelReason::AppShutdown:
hr = APPINSTALLER_CLI_ERROR_APPTERMINATION_RECEIVED;
break;
}
return hr;
}
ProgressCallback::ProgressCallback(IProgressSink* sink) : m_sink(sink)
{
}
void ProgressCallback::BeginProgress()
{
IProgressSink* sink = GetSink();
if (sink)
{
sink->BeginProgress();
}
}
void ProgressCallback::OnProgress(uint64_t current, uint64_t maximum, ProgressType type)
{
IProgressSink* sink = GetSink();
if (sink)
{
sink->OnProgress(current, maximum, type);
}
}
void ProgressCallback::SetProgressMessage(std::string_view message)
{
IProgressSink* sink = GetSink();
if (sink)
{
sink->SetProgressMessage(message);
}
}
void ProgressCallback::EndProgress(bool hideProgressWhenDone)
{
IProgressSink* sink = GetSink();
if (sink)
{
sink->EndProgress(hideProgressWhenDone);
}
};
bool ProgressCallback::IsCancelledBy(CancelReason cancelReasons)
{
THROW_HR_IF(E_UNEXPECTED, cancelReasons == CancelReason::None);
return WI_IsAnyFlagSet(cancelReasons, m_cancelReason);
}
[[nodiscard]] IProgressCallback::CancelFunctionRemoval ProgressCallback::SetCancellationFunction(std::function<void()>&& f)
{
m_cancellationFunction = std::move(f);
if (m_cancellationFunction)
{
return IProgressCallback::CancelFunctionRemoval(this);
}
else
{
return {};
}
}
bool ProgressCallback::Wait(IProgressCallback& progress, std::chrono::milliseconds millisecondsToWait)
{
wil::unique_event calledEvent;
calledEvent.create();
auto cancellationFunc = progress.SetCancellationFunction([&calledEvent]()
{
calledEvent.SetEvent();
});
if (calledEvent.wait(static_cast<DWORD>(millisecondsToWait.count())))
{
return false;
}
return true;
}
void ProgressCallback::Cancel(CancelReason reason)
{
m_cancelReason = reason;
if (m_cancellationFunction)
{
m_cancellationFunction();
}
}
IProgressSink* ProgressCallback::GetSink()
{
return m_sink.load();
}
PartialPercentProgressCallback::PartialPercentProgressCallback(IProgressCallback& baseCallback, uint64_t globalMax) :
m_baseCallback(baseCallback), m_globalMax(globalMax)
{
}
void PartialPercentProgressCallback::BeginProgress()
{
THROW_HR(E_NOTIMPL);
}
void PartialPercentProgressCallback::OnProgress(uint64_t current, uint64_t maximum, ProgressType type)
{
THROW_HR_IF(E_UNEXPECTED, ProgressType::Percent != type);
m_baseCallback.OnProgress(m_rangeMin + (m_rangeMax - m_rangeMin) * current / maximum, m_globalMax, type);
}
void PartialPercentProgressCallback::SetProgressMessage(std::string_view message)
{
m_baseCallback.SetProgressMessage(message);
}
void PartialPercentProgressCallback::EndProgress(bool)
{
THROW_HR(E_NOTIMPL);
}
IProgressCallback::CancelFunctionRemoval PartialPercentProgressCallback::SetCancellationFunction(std::function<void()>&& f)
{
return m_baseCallback.SetCancellationFunction(std::move(f));
}
void PartialPercentProgressCallback::SetRange(uint64_t rangeMin, uint64_t rangeMax)
{
THROW_HR_IF(E_INVALIDARG, rangeMin > rangeMax || rangeMax > m_globalMax);
m_rangeMin = rangeMin;
m_rangeMax = rangeMax;
}
}