-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathInstallationConfirmation.cs
More file actions
181 lines (154 loc) · 6.82 KB
/
Copy pathInstallationConfirmation.cs
File metadata and controls
181 lines (154 loc) · 6.82 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using System.Diagnostics.CodeAnalysis;
using RpcInstallationDecision = GitHub.Copilot.Rpc.InstallationDecision;
using RpcInstallationsConfirmRequest = GitHub.Copilot.Rpc.InstallationsConfirmRequest;
using RpcInstallationsConfirmResult = GitHub.Copilot.Rpc.InstallationsConfirmResult;
using RpcInstallationsHandler = GitHub.Copilot.Rpc.IInstallationsHandler;
namespace GitHub.Copilot;
/// <summary>
/// The cancellable lifetime of one installation confirmation.
/// </summary>
[Experimental(Diagnostics.Experimental)]
public sealed class InstallationConfirmationContext
{
internal InstallationConfirmationContext(CancellationToken cancellationToken)
{
CancellationToken = cancellationToken;
}
/// <summary>
/// Gets a token cancelled when the runtime retires this request or the original connection closes.
/// </summary>
public CancellationToken CancellationToken { get; }
}
/// <summary>
/// Collects a fresh human decision for the complete installation review on its original connection.
/// </summary>
/// <param name="request">The generated installation confirmation request.</param>
/// <param name="context">The confirmation context, including a token cancelled by request cancellation or connection closure.</param>
/// <returns>An explicit confirm, decline or cancel decision.</returns>
[Experimental(Diagnostics.Experimental)]
public delegate ValueTask<RpcInstallationDecision> InstallationConfirmationHandler(
RpcInstallationsConfirmRequest request,
InstallationConfirmationContext context);
[Experimental(Diagnostics.Experimental)]
internal sealed class InstallationConfirmationAdapter(InstallationConfirmationHandler handler) : RpcInstallationsHandler
{
private readonly InstallationConfirmationHandler _handler =
handler ?? throw new ArgumentNullException(nameof(handler));
private CancellationToken _connectionClosed;
internal void Attach(JsonRpc rpc)
{
ArgumentNullException.ThrowIfNull(rpc);
_connectionClosed = rpc.ConnectionClosedToken;
}
public async Task<RpcInstallationsConfirmResult> ConfirmAsync(
RpcInstallationsConfirmRequest request,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(request);
ThrowIfRequestCancelled(cancellationToken);
ThrowIfConnectionClosed(_connectionClosed);
var confirmationId = request.ConfirmationId;
var reviewFingerprint = request.ReviewFingerprint;
var combinedCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _connectionClosed);
Task<RpcInstallationDecision>? handlerTask = null;
try
{
var context = new InstallationConfirmationContext(combinedCancellation.Token);
var cancellationSignal = CreateCancellationSignal(combinedCancellation.Token);
using var cancellationRegistration = RegisterCancellationSignal(cancellationSignal, combinedCancellation.Token);
handlerTask = InvokeHandlerAsync(request, context);
var completed = await Task.WhenAny(
handlerTask,
cancellationSignal.Task).ConfigureAwait(false);
ThrowIfRequestCancelled(cancellationToken);
ThrowIfConnectionClosed(_connectionClosed);
if (ReferenceEquals(completed, cancellationSignal.Task))
{
throw new InvalidOperationException("Installation confirmation cancelled.");
}
var decision = await handlerTask.ConfigureAwait(false);
ThrowIfRequestCancelled(cancellationToken);
ThrowIfConnectionClosed(_connectionClosed);
return new RpcInstallationsConfirmResult
{
ConfirmationId = confirmationId,
ReviewFingerprint = reviewFingerprint,
Decision = IsValidDecision(decision)
? decision
: throw new InvalidOperationException("Invalid installation confirmation decision."),
};
}
finally
{
DisposeCancellationWhenHandlerCompletes(combinedCancellation, handlerTask);
}
}
private async Task<RpcInstallationDecision> InvokeHandlerAsync(
RpcInstallationsConfirmRequest request,
InstallationConfirmationContext context)
{
return await _handler(request, context).ConfigureAwait(false);
}
private static bool IsValidDecision(RpcInstallationDecision decision)
{
return decision == RpcInstallationDecision.Confirm ||
decision == RpcInstallationDecision.Decline ||
decision == RpcInstallationDecision.Cancel;
}
private static TaskCompletionSource CreateCancellationSignal(CancellationToken cancellationToken)
{
var signal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
if (cancellationToken.IsCancellationRequested)
{
signal.TrySetResult();
}
return signal;
}
private static CancellationTokenRegistration RegisterCancellationSignal(
TaskCompletionSource signal,
CancellationToken cancellationToken)
{
return cancellationToken.CanBeCanceled
? cancellationToken.Register(static state => ((TaskCompletionSource)state!).TrySetResult(), signal)
: default;
}
private static void ThrowIfRequestCancelled(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
throw new LocalRpcInvocationException(
-32800,
"Installation confirmation request cancelled");
}
}
private static void ThrowIfConnectionClosed(CancellationToken connectionClosed)
{
if (connectionClosed.IsCancellationRequested)
{
throw new InvalidOperationException("Installation confirmation connection closed.");
}
}
private static void DisposeCancellationWhenHandlerCompletes(
CancellationTokenSource cancellation,
Task<RpcInstallationDecision>? handlerTask)
{
if (handlerTask is not null && !handlerTask.IsCompleted)
{
_ = handlerTask.ContinueWith(
static (task, state) =>
{
_ = task.Exception;
((CancellationTokenSource)state!).Dispose();
},
cancellation,
CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
return;
}
cancellation.Dispose();
}
}