forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestObject.cs
More file actions
428 lines (361 loc) · 15 KB
/
Copy pathRequestObject.cs
File metadata and controls
428 lines (361 loc) · 15 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Microsoft.PackageManagement.Internal.Implementation {
using System;
using System.Collections.Generic;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using Internal.Api;
using Resources;
using Utility.Async;
using Utility.Extensions;
using Microsoft.PackageManagement.Internal.Packaging;
using System.Net;
public abstract class RequestObject : AsyncAction, IRequest, IHostApi {
private static int _c;
protected Action<RequestObject> _action;
private IHostApi _hostApi;
protected Task _invocationTask;
protected readonly ProviderBase Provider;
internal RequestObject(ProviderBase provider, IHostApi hostApi, Action<RequestObject> action) {
// construct request object
_hostApi = hostApi;
Provider = provider;
_action = action;
}
internal RequestObject(ProviderBase provider, IHostApi hostApi) {
// construct request object
_hostApi = hostApi;
Provider = provider;
}
private bool CanCallHost {
get {
if (IsCompleted || IsAborted) {
return false;
}
Activity();
return _hostApi != null;
}
}
public override bool IsCanceled {
get {
return base.IsCanceled || (CanCallHost && _hostApi.IsCanceled);
}
}
public override void WarnBeforeResponsivenessCancellation() {
if (CanCallHost) {
_hostApi.Warning((GetMessageString(Constants.Messages.ProviderNotResponsive, null) ?? "Provider '{0}' is not respecting the responsiveness threshold in a timely fashion; Canceling request.").format(Provider.ProviderName));
}
}
public override void WarnBeforeTimeoutCancellation() {
if (CanCallHost) {
_hostApi.Warning((GetMessageString(Constants.Messages.ProviderTimeoutExceeded, null) ?? "Provider '{0}' is not completing the request in the time allowed; Canceling request.").format(Provider.ProviderName));
}
}
protected void InvokeImpl() {
_invocationTask = Task.Factory.StartNew(() => {
_invocationThread = Thread.CurrentThread;
_invocationThread.Name = Provider.ProviderName + ":" + _c++;
try {
_action(this);
#if !CORECLR
} catch (ThreadAbortException) {
#if DEEP_DEBUG
Console.WriteLine("Thread Aborted for {0} : {1}", _invocationThread.Name, DateTime.Now.Subtract(_callStart).TotalSeconds);
#endif
Thread.ResetAbort();
#endif
} catch (Exception e) {
e.Dump();
} finally {
try {
Complete();
}
catch (Exception e)
{
e.Dump();
}
}
}, _cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); // .ContinueWith(antecedent => Complete());
// start thread, call function
StartCall();
}
#region HostApi Wrapper
public string DropMsgPrefix(string messageText) {
if (string.IsNullOrWhiteSpace(messageText)) {
return messageText;
}
return messageText.StartsWith("MSG:", StringComparison.OrdinalIgnoreCase) ? messageText.Substring(4) : messageText;
}
public string GetMessageString(string messageText, string defaultText) {
if (CanCallHost) {
if (string.IsNullOrWhiteSpace(defaultText) || defaultText.StartsWith("MSG:", StringComparison.OrdinalIgnoreCase)) {
defaultText = Messages.ResourceManager.GetString(DropMsgPrefix(messageText));
}
return _hostApi.GetMessageString(messageText, defaultText);
}
return null;
}
public bool Warning(string messageText) {
if (CanCallHost) {
return _hostApi.Warning(GetMessageString(messageText, null) ?? messageText);
}
return true;
}
public bool Error(string id, string category, string targetObjectValue, string messageText) {
if (CanCallHost) {
return _hostApi.Error(id, category, targetObjectValue, GetMessageString(messageText, null) ?? messageText);
}
return true;
}
public bool Message(string messageText) {
if (CanCallHost) {
return _hostApi.Message(GetMessageString(messageText, null) ?? messageText);
}
return true;
}
public bool Verbose(string messageText) {
if (CanCallHost) {
return _hostApi.Verbose(GetMessageString(messageText, null) ?? messageText);
}
return true;
}
public bool Debug(string messageText) {
if (CanCallHost) {
return _hostApi.Debug(GetMessageString(messageText, null) ?? messageText);
}
return true;
}
public int StartProgress(int parentActivityId, string messageText) {
if (CanCallHost) {
return _hostApi.StartProgress(parentActivityId, messageText);
}
return 0;
}
public bool Progress(int activityId, int progressPercentage, string messageText) {
if (CanCallHost) {
return _hostApi.Progress(activityId, progressPercentage, GetMessageString(messageText, null) ?? messageText);
}
return true;
}
public bool Progress(string activity, string status, int id, int percentcomplete, int secondsremaining, string currentoperation, int parentid, bool completed)
{
if (CanCallHost)
{
return _hostApi.Progress(activity, status, id, percentcomplete, secondsremaining, currentoperation, parentid, completed);
}
return true;
}
public bool CompleteProgress(int activityId, bool isSuccessful) {
if (CanCallHost) {
return _hostApi.CompleteProgress(activityId, isSuccessful);
}
return true;
}
public IEnumerable<string> OptionKeys {
get {
if (CanCallHost) {
return _hostApi.OptionKeys;
}
return new string[0];
}
}
public IEnumerable<string> GetOptionValues(string key) {
if (CanCallHost) {
return _hostApi.GetOptionValues(key);
}
return new string[0];
}
public IEnumerable<string> Sources {
get {
if (CanCallHost) {
return _hostApi.Sources;
}
return new string[0];
}
}
public IWebProxy WebProxy
{
get
{
return CanCallHost ? _hostApi.WebProxy : null;
}
}
public string CredentialUsername {
get {
return CanCallHost ? _hostApi.CredentialUsername : null;
}
}
public SecureString CredentialPassword {
get {
return CanCallHost ? _hostApi.CredentialPassword : null;
}
}
public bool ShouldBootstrapProvider(string requestor, string providerName, string providerVersion, string providerType, string location, string destination) {
if (CanCallHost) {
return _hostApi.ShouldBootstrapProvider(requestor, providerName, providerVersion, providerType, location, destination);
}
return false;
}
public bool ShouldContinueWithUntrustedPackageSource(string package, string packageSource) {
if (CanCallHost) {
return _hostApi.ShouldContinueWithUntrustedPackageSource(package, packageSource);
}
return false;
}
public bool ShouldContinue(string query, string caption, ref bool yesToAll, ref bool noToAll) {
if (CanCallHost) {
return _hostApi.ShouldContinue(query, caption, ref yesToAll, ref noToAll);
}
return false;
}
public bool ShouldContinue(string query, string caption)
{
if (CanCallHost) {
return _hostApi.ShouldContinue(query, caption);
}
return false;
}
public bool AskPermission(string permission) {
if (CanCallHost) {
return _hostApi.AskPermission(permission);
}
return false;
}
public bool IsInteractive {
get {
if (CanCallHost) {
return _hostApi.IsInteractive;
}
return false;
}
}
public int CallCount {
get {
if (CanCallHost) {
return _hostApi.CallCount;
}
return 0;
}
}
#endregion
#region CoreApi implementation
public IPackageManagementService PackageManagementService {
get {
Activity();
return PackageManager.Instance;
}
}
public IProviderServices ProviderServices {
get {
Activity();
return ProviderServicesImpl.Instance;
}
}
#endregion
#region response api implementation
public virtual string YieldSoftwareIdentity(string fastPath, string name, string version, string versionScheme, string summary, string source, string searchKey, string fullPath, string packageFileName) {
Debug("Unexpected call to YieldSoftwareIdentity in RequestObject");
return null;
}
public virtual string YieldSoftwareIdentityXml(string xmlSwidTag, bool commitImmediately)
{
Debug("Unexpected call to YieldSoftwareIdentityXml in RequestObject");
return null;
}
public virtual string AddMetadata(string name, string value) {
Debug("Unexpected call to AddMetaData in RequestObject");
return null;
}
public virtual string AddTagId(string tagId) {
Debug("Unexpected call to AddMetaData in RequestObject");
return null;
}
public virtual string AddCulture(string xmlLang)
{
Debug("Unexpected call to Culture in RequestObject");
return null;
}
public virtual string AddMetadata(string elementPath, string name, string value) {
Debug("Unexpected call to AddMetaData in RequestObject");
return null;
}
public virtual string AddMetadata(string elementPath, Uri @namespace, string name, string value) {
Debug("Unexpected call to AddMetaData in RequestObject");
return null;
}
public virtual string AddMeta(string elementPath) {
Debug("Unexpected call to AddMeta in RequestObject");
return null;
}
public virtual string AddPayload() {
Debug("Unexpected call to AddPayload in RequestObject");
return null;
}
public virtual string AddEvidence(DateTime date, string deviceId) {
Debug("Unexpected call to AddEvidence in RequestObject");
return null;
}
public virtual string AddDirectory(string elementPath, string directoryName, string location, string root, bool isKey) {
Debug("Unexpected call to AddDirectory in RequestObject");
return null;
}
public virtual string AddFile(string elementPath, string fileName, string location, string root, bool isKey, long size, string version) {
Debug("Unexpected call to AddFile in RequestObject");
return null;
}
public virtual string AddProcess(string elementPath, string processName, int pid) {
Debug("Unexpected call to AddProcess in RequestObject");
return null;
}
public virtual string AddResource(string elementPath, string type) {
Debug("Unexpected call to AddResource in RequestObject");
return null;
}
public virtual string AddEntity(string name, string regid, string role, string thumbprint) {
Debug("Unexpected call to AddEntity in RequestObject");
return null;
}
public virtual string AddLink(Uri referenceUri, string relationship, string mediaType, string ownership, string use, string appliesToMedia, string artifact) {
Debug("Unexpected call to AddLink in RequestObject");
return null;
}
public virtual string AddDependency(string providerName, string packageName, string version, string source, string appliesTo) {
Debug("Unexpected call to AddDependency in RequestObject");
return null;
}
public virtual bool YieldPackageSource(string name, string location, bool isTrusted, bool isRegistered, bool isValidated) {
Console.WriteLine("SHOULD NOT GET HERE [YieldSoftwareIdentity] ================================================");
// todo: give an actual error here
return true; // cancel
}
public virtual bool YieldDynamicOption(string name, string expectedType, bool isRequired) {
Console.WriteLine("SHOULD NOT GET HERE [YieldSoftwareIdentity] ================================================");
// todo: give an actual error here
return true; // cancel
}
public virtual bool YieldKeyValuePair(string key, string value) {
Console.WriteLine("SHOULD NOT GET HERE [YieldSoftwareIdentity] ================================================");
// todo: give an actual error here
return true; // cancel
}
public virtual bool YieldValue(string value) {
Console.WriteLine("SHOULD NOT GET HERE [YieldSoftwareIdentity] ================================================");
// todo: give an actual error here
return true; // cancel
}
#endregion
}
}