4
4
using System . ComponentModel ;
5
5
using System . Globalization ;
6
6
using System . Runtime . InteropServices ;
7
+ using System . Runtime . InteropServices . WindowsRuntime ;
7
8
using System . Threading ;
8
9
using HyperVExtension . HostGuestCommunication ;
10
+ using HyperVExtension . Models ;
9
11
using HyperVExtension . Providers ;
10
12
using Microsoft . Windows . DevHome . SDK ;
11
13
using Windows . Foundation ;
14
+ using Windows . Storage ;
15
+ using Windows . Storage . Streams ;
12
16
using Windows . Win32 . Foundation ;
13
-
14
17
using SDK = Microsoft . Windows . DevHome . SDK ;
15
18
16
19
namespace HyperVExtension . CommunicationWithGuest ;
17
20
18
- internal sealed class ApplyConfigurationOperation : IApplyConfigurationOperation , IDisposable
21
+ public sealed class ApplyConfigurationOperation : IApplyConfigurationOperation , IDisposable
19
22
{
20
- private readonly IComputeSystem _computeSystem ;
23
+ private readonly HyperVVirtualMachine _virtualMachine ;
21
24
private readonly CancellationTokenSource _cancellationTokenSource = new ( ) ;
25
+
22
26
private bool _disposed ;
23
27
24
- public ApplyConfigurationOperation ( IComputeSystem computeSystem )
28
+ public string Configuration { get ; private set ; } = string . Empty ;
29
+
30
+ public ApplyConfigurationOperation ( HyperVVirtualMachine virtualMachine , string configuration )
25
31
{
26
- _computeSystem = computeSystem ;
32
+ _virtualMachine = virtualMachine ;
33
+ Configuration = configuration ;
27
34
}
28
35
29
- public ApplyConfigurationOperation ( IComputeSystem computeSystem , Exception result , string ? resultDescription = null )
36
+ public ApplyConfigurationOperation ( HyperVVirtualMachine virtualMachine , Exception result , string ? resultDescription = null )
30
37
{
31
- _computeSystem = computeSystem ;
32
- CompletionStatus = new SDK . ApplyConfigurationResult (
33
- result ,
34
- resultDescription ,
35
- null ,
36
- null ) ;
38
+ _virtualMachine = virtualMachine ;
39
+ CompletionStatus = new SDK . ApplyConfigurationResult ( result , result . Message , result . Message ) ;
37
40
}
38
41
39
42
public CancellationToken CancellationToken => _cancellationTokenSource . Token ;
40
43
41
- public event TypedEventHandler < IComputeSystem , SDK . ApplyConfigurationResult > ? Completed ;
44
+ public event TypedEventHandler < IApplyConfigurationOperation , ApplyConfigurationActionRequiredEventArgs > ActionRequired = ( s , e ) => { } ;
42
45
43
- public event TypedEventHandler < IComputeSystem , SDK . ConfigurationSetChangeData > ? Progress ;
46
+ public event TypedEventHandler < IApplyConfigurationOperation , ConfigurationSetStateChangedEventArgs > ConfigurationSetStateChanged = ( s , e ) => { } ;
44
47
45
48
public SDK . ApplyConfigurationResult ? CompletionStatus { get ; private set ; }
46
49
@@ -50,45 +53,45 @@ public ApplyConfigurationOperation(IComputeSystem computeSystem, Exception resul
50
53
SDK . ConfigurationSetState . Unknown ,
51
54
SDK . ConfigurationUnitState . Unknown ,
52
55
new SDK . ConfigurationUnitResultInformation ( null , null , null , SDK . ConfigurationUnitResultSource . None ) ,
53
- new SDK . ConfigurationUnit ( null , null , SDK . ConfigurationUnitState . Unknown , false , null , null , SDK . ConfigurationUnitIntent . Unknown ) ,
54
- null ) ;
56
+ new SDK . ConfigurationUnit ( null , null , SDK . ConfigurationUnitState . Unknown , false , null , null , SDK . ConfigurationUnitIntent . Unknown ) ) ;
55
57
56
- public void Cancel ( ) => throw new NotImplementedException ( ) ;
57
-
58
- public void SetState (
58
+ public void SetProgress (
59
59
SDK . ConfigurationSetState state ,
60
60
HostGuestCommunication . ConfigurationSetChangeData ? progressData ,
61
- HostGuestCommunication . ApplyConfigurationResult ? completionStatus ,
62
61
SDK . IExtensionAdaptiveCardSession2 ? adaptiveCardSession )
63
62
{
64
- var sdkProgressData = GetSdkProgressData ( state , progressData , adaptiveCardSession ) ;
63
+ var sdkProgressData = GetSdkProgressData ( state , progressData ) ;
65
64
66
65
if ( sdkProgressData != null )
67
66
{
68
67
ProgressData = sdkProgressData ;
69
- Progress ? . Invoke ( _computeSystem , ProgressData ) ;
68
+ ConfigurationSetStateChanged ? . Invoke ( this , new ( ProgressData ) ) ;
70
69
}
71
70
72
- // If the completionStatus is not null, then the operation is completed.
73
- if ( ( completionStatus != null ) || ( state == SDK . ConfigurationSetState . Completed ) )
71
+ if ( adaptiveCardSession != null )
74
72
{
75
- var sdkCompletionStatus = GetSdkConfigurationResult ( completionStatus ) ;
76
- if ( sdkCompletionStatus == null )
77
- {
78
- // No apply configuration result was provided, but state is "Completed"
79
- // so create ApplyConfigurationResult with no error (meaning operation is completed).
80
- sdkCompletionStatus = new SDK . ApplyConfigurationResult ( null , null , null , null ) ;
81
- }
73
+ ActionRequired ? . Invoke ( this , new ( adaptiveCardSession ) ) ;
74
+ }
75
+ }
82
76
83
- CompletionStatus = sdkCompletionStatus ;
84
- Completed ? . Invoke ( _computeSystem , CompletionStatus ) ;
77
+ public SDK . ApplyConfigurationResult CompleteOperation ( HostGuestCommunication . ApplyConfigurationResult ? completionStatus )
78
+ {
79
+ // If the completionStatus is not null, then the operation is completed.
80
+ // if ((completionStatus != null) || (state == SDK.ConfigurationSetState.Completed))
81
+ var sdkCompletionStatus = GetSdkConfigurationResult ( completionStatus ) ;
82
+ if ( sdkCompletionStatus == null )
83
+ {
84
+ // No apply configuration result was provided, but state is "Completed"
85
+ // so create ApplyConfigurationResult with no error (meaning operation is completed).
86
+ sdkCompletionStatus = new SDK . ApplyConfigurationResult ( null , null ) ;
85
87
}
88
+
89
+ return sdkCompletionStatus ;
86
90
}
87
91
88
92
private SDK . ConfigurationSetChangeData GetSdkProgressData (
89
93
SDK . ConfigurationSetState state ,
90
- HostGuestCommunication . ConfigurationSetChangeData ? progressData ,
91
- SDK . IExtensionAdaptiveCardSession2 ? adaptiveCardSession )
94
+ HostGuestCommunication . ConfigurationSetChangeData ? progressData )
92
95
{
93
96
SDK . ConfigurationSetChangeData sdkProgressData ;
94
97
if ( progressData != null )
@@ -114,8 +117,7 @@ private SDK.ConfigurationSetChangeData GetSdkProgressData(
114
117
( SDK . ConfigurationSetState ) progressData . SetState ,
115
118
( SDK . ConfigurationUnitState ) progressData . UnitState ,
116
119
resultInfo ,
117
- sdkUnit ,
118
- adaptiveCardSession ) ;
120
+ sdkUnit ) ;
119
121
}
120
122
else
121
123
{
@@ -124,8 +126,7 @@ private SDK.ConfigurationSetChangeData GetSdkProgressData(
124
126
state ,
125
127
SDK . ConfigurationUnitState . Unknown ,
126
128
null ,
127
- null ,
128
- adaptiveCardSession ) ;
129
+ null ) ;
129
130
}
130
131
131
132
return sdkProgressData ;
@@ -188,13 +189,15 @@ private SDK.ConfigurationSetChangeData GetSdkProgressData(
188
189
sdkUnitResults . AsReadOnly ( ) ) ;
189
190
}
190
191
191
- return new SDK . ApplyConfigurationResult (
192
- completionStatus . ResultCode == HRESULT . S_OK ?
193
- null :
194
- new HResultException ( completionStatus . ResultCode ) ,
195
- completionStatus . ResultDescription ,
196
- sdkOpenConfigurationSetResult ,
197
- sdkApplyConfigurationSetResult ) ;
192
+ var wasConfigurationSuccessful = completionStatus . ResultCode == HRESULT . S_OK ;
193
+ if ( wasConfigurationSuccessful )
194
+ {
195
+ return new SDK . ApplyConfigurationResult ( sdkOpenConfigurationSetResult , sdkApplyConfigurationSetResult ) ;
196
+ }
197
+
198
+ var hresultException = new HResultException ( completionStatus . ResultCode ) ;
199
+
200
+ return new SDK . ApplyConfigurationResult ( hresultException , completionStatus . ResultDescription , hresultException . Message ) ;
198
201
}
199
202
200
203
return null ;
@@ -252,4 +255,12 @@ private void Dispose(bool disposing)
252
255
_disposed = true ;
253
256
}
254
257
}
258
+
259
+ public IAsyncOperation < SDK . ApplyConfigurationResult > StartAsync ( )
260
+ {
261
+ return Task . Run ( ( ) =>
262
+ {
263
+ return _virtualMachine . ApplyConfiguration ( this ) ;
264
+ } ) . AsAsyncOperation ( ) ;
265
+ }
255
266
}
0 commit comments