/
LicenseManager.cs
365 lines (322 loc) · 12.3 KB
/
LicenseManager.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.Versioning;
using System.Threading;
namespace System.ComponentModel
{
/// <summary>
/// Provides properties and methods to add a license
/// to a component and to manage a <see cref='System.ComponentModel.LicenseProvider'/>. This class cannot be inherited.
/// </summary>
public sealed partial class LicenseManager
{
private static readonly object s_selfLock = new object();
private static volatile LicenseContext? s_context;
private static object? s_contextLockHolder;
private static volatile Hashtable? s_providers;
private static volatile Hashtable? s_providerInstances;
private static readonly object s_internalSyncObject = new object();
// not creatable...
private LicenseManager()
{
}
/// <summary>
/// Gets or sets the current <see cref='System.ComponentModel.LicenseContext'/> which specifies when the licensed object can be
/// used.
/// </summary>
public static LicenseContext CurrentContext
{
get
{
if (s_context == null)
{
lock (s_internalSyncObject)
{
s_context ??= new RuntimeLicenseContext();
}
}
return s_context;
}
set
{
lock (s_internalSyncObject)
{
if (s_contextLockHolder != null)
{
throw new InvalidOperationException(SR.LicMgrContextCannotBeChanged);
}
s_context = value;
}
}
}
/// <summary>
/// Gets the <see cref='System.ComponentModel.LicenseUsageMode'/> that
/// specifies when the licensed object can be used, for the <see cref='System.ComponentModel.LicenseManager.CurrentContext'/>.
/// </summary>
public static LicenseUsageMode UsageMode
{
get
{
if (s_context != null)
{
return s_context.UsageMode;
}
return LicenseUsageMode.Runtime;
}
}
/// <summary>
/// Caches the provider, both in the instance cache, and the type
/// cache.
/// </summary>
private static void CacheProvider(Type type, LicenseProvider? provider)
{
if (s_providers == null)
{
Interlocked.CompareExchange(ref s_providers, new Hashtable(), null);
}
lock (s_providers)
{
s_providers[type] = provider;
}
if (provider != null)
{
if (s_providerInstances == null)
{
Interlocked.CompareExchange(ref s_providerInstances, new Hashtable(), null);
}
Type providerType = provider.GetType();
lock (s_providerInstances)
{
s_providerInstances[providerType] = provider;
}
}
}
/// <summary>
/// Creates an instance of the specified type, using
/// creationContext
/// as the context in which the licensed instance can be used.
/// </summary>
[UnsupportedOSPlatform("browser")]
public static object? CreateWithContext(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type,
LicenseContext creationContext)
{
return CreateWithContext(type, creationContext, Array.Empty<object>());
}
/// <summary>
/// Creates an instance of the specified type with the
/// specified arguments, using creationContext as the context in which the licensed
/// instance can be used.
/// </summary>
[UnsupportedOSPlatform("browser")]
public static object? CreateWithContext(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type,
LicenseContext creationContext,
object[] args)
{
object? created = null;
lock (s_internalSyncObject)
{
LicenseContext normal = CurrentContext;
try
{
CurrentContext = creationContext;
LockContext(s_selfLock);
try
{
created = Activator.CreateInstance(type, args);
}
catch (TargetInvocationException e)
{
throw e.InnerException!;
}
}
finally
{
UnlockContext(s_selfLock);
CurrentContext = normal;
}
}
return created;
}
/// <summary>
/// Determines if type was actually cached to have _no_ provider,
/// as opposed to not being cached.
/// </summary>
private static bool GetCachedNoLicenseProvider(Type type)
{
if (s_providers != null)
{
return s_providers.ContainsKey(type);
}
return false;
}
/// <summary>
/// Retrieves a cached instance of the provider associated with the
/// specified type.
/// </summary>
private static LicenseProvider? GetCachedProvider(Type type)
{
return (LicenseProvider?)s_providers?[type];
}
/// <summary>
/// Retrieves a cached instance of the provider of the specified
/// type.
/// </summary>
private static LicenseProvider? GetCachedProviderInstance(Type providerType)
{
Debug.Assert(providerType != null, "Type cannot ever be null");
return (LicenseProvider?)s_providerInstances?[providerType];
}
/// <summary>
/// Determines if the given type has a valid license or not.
/// </summary>
public static bool IsLicensed(Type type)
{
Debug.Assert(type != null, "IsValid Type cannot ever be null");
bool value = ValidateInternal(type, null, false, out License? license);
license?.Dispose();
return value;
}
/// <summary>
/// Determines if a valid license can be granted for the specified type.
/// </summary>
public static bool IsValid(Type type)
{
Debug.Assert(type != null, "IsValid Type cannot ever be null");
bool value = ValidateInternal(type, null, false, out License? license);
license?.Dispose();
return value;
}
/// <summary>
/// Determines if a valid license can be granted for the
/// specified instance of the type. This method creates a valid <see cref='System.ComponentModel.License'/>.
/// </summary>
public static bool IsValid(Type type, object? instance, out License? license)
{
return ValidateInternal(type, instance, false, out license);
}
public static void LockContext(object contextUser)
{
lock (s_internalSyncObject)
{
if (s_contextLockHolder != null)
{
throw new InvalidOperationException(SR.LicMgrAlreadyLocked);
}
s_contextLockHolder = contextUser;
}
}
public static void UnlockContext(object contextUser)
{
lock (s_internalSyncObject)
{
if (s_contextLockHolder != contextUser)
{
throw new ArgumentException(SR.LicMgrDifferentUser);
}
s_contextLockHolder = null;
}
}
/// <summary>
/// Internal validation helper.
/// </summary>
private static bool ValidateInternal(Type type, object? instance, bool allowExceptions, out License? license)
{
return ValidateInternalRecursive(CurrentContext,
type,
instance,
allowExceptions,
out license,
out _);
}
/// <summary>
/// Since we want to walk up the entire inheritance change, when not
/// give an instance, we need another helper method to walk up
/// the chain...
/// </summary>
private static bool ValidateInternalRecursive(LicenseContext context, Type type, object? instance, bool allowExceptions, out License? license, out string? licenseKey)
{
LicenseProvider? provider = GetCachedProvider(type);
if (provider == null && !GetCachedNoLicenseProvider(type))
{
// NOTE : Must look directly at the class, we want no inheritance.
LicenseProviderAttribute? attr = (LicenseProviderAttribute?)Attribute.GetCustomAttribute(type, typeof(LicenseProviderAttribute), false);
if (attr != null)
{
Type providerType = attr.LicenseProvider!;
provider = GetCachedProviderInstance(providerType) ?? (LicenseProvider)Activator.CreateInstance(providerType)!;
}
CacheProvider(type, provider);
}
license = null;
bool isValid = true;
licenseKey = null;
if (provider != null)
{
license = provider.GetLicense(context, type, instance, allowExceptions);
if (license == null)
{
isValid = false;
}
else
{
// For the case where a COM client is calling "RequestLicKey",
// we try to squirrel away the first found license key
licenseKey = license.LicenseKey;
}
}
// When looking only at a type, we need to recurse up the inheritance
// chain, however, we can't give out the license, since this may be
// from more than one provider.
if (isValid && instance == null)
{
Type? baseType = type.BaseType;
if (baseType != typeof(object) && baseType != null)
{
if (license != null)
{
license.Dispose();
#pragma warning disable IDE0059 // ValidateInternalRecursive does not null licence all the time (https://github.com/dotnet/roslyn/issues/42761)
license = null;
#pragma warning restore IDE0059
}
isValid = ValidateInternalRecursive(context, baseType, null, allowExceptions, out license, out _);
if (license != null)
{
license.Dispose();
license = null;
}
}
}
return isValid;
}
/// <summary>
/// Determines if a license can be granted for the specified type.
/// </summary>
public static void Validate(Type type)
{
if (!ValidateInternal(type, null, true, out License? lic))
{
throw new LicenseException(type);
}
lic?.Dispose();
}
/// <summary>
/// Determines if a license can be granted for the instance of the specified type.
/// </summary>
public static License? Validate(Type type, object? instance)
{
if (!ValidateInternal(type, instance, true, out License? lic))
{
throw new LicenseException(type, instance);
}
return lic;
}
}
}